~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebKit2/UIProcess/mac/WebContextMac.mm

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2010 Apple Inc. All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
7
 * 1. Redistributions of source code must retain the above copyright
 
8
 *    notice, this list of conditions and the following disclaimer.
 
9
 * 2. Redistributions in binary form must reproduce the above copyright
 
10
 *    notice, this list of conditions and the following disclaimer in the
 
11
 *    documentation and/or other materials provided with the distribution.
 
12
 *
 
13
 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
 
14
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 
15
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 
16
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
 
17
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
18
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
19
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
20
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
21
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
22
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 
23
 * THE POSSIBILITY OF SUCH DAMAGE.
 
24
 */
 
25
 
 
26
#import "config.h"
 
27
#import "WebContext.h"
 
28
 
 
29
#import "NetworkProcessManager.h"
 
30
#import "PluginProcessManager.h"
 
31
#import "SharedWorkerProcessManager.h"
 
32
#import "WKBrowsingContextControllerInternal.h"
 
33
#import "WebKitSystemInterface.h"
 
34
#import "WebProcessCreationParameters.h"
 
35
#import "WebProcessMessages.h"
 
36
#import <WebCore/Color.h>
 
37
#import <WebCore/FileSystem.h>
 
38
#include <WebCore/NotImplemented.h>
 
39
#import <WebCore/PlatformPasteboard.h>
 
40
#import <sys/param.h>
 
41
 
 
42
#if HAVE(HOSTED_CORE_ANIMATION) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
 
43
#import <QuartzCore/CARemoteLayerServer.h>
 
44
#endif
 
45
 
 
46
using namespace WebCore;
 
47
 
 
48
NSString *WebDatabaseDirectoryDefaultsKey = @"WebDatabaseDirectory";
 
49
NSString *WebKitLocalCacheDefaultsKey = @"WebKitLocalCache";
 
50
NSString *WebStorageDirectoryDefaultsKey = @"WebKitLocalStorageDatabasePathPreferenceKey";
 
51
NSString *WebKitKerningAndLigaturesEnabledByDefaultDefaultsKey = @"WebKitKerningAndLigaturesEnabledByDefault";
 
52
 
 
53
static NSString *WebKitApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification = @"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification";
 
54
 
 
55
// FIXME: <rdar://problem/9138817> - After this "backwards compatibility" radar is removed, this code should be removed to only return an empty String.
 
56
NSString *WebIconDatabaseDirectoryDefaultsKey = @"WebIconDatabaseDirectoryDefaultsKey";
 
57
 
 
58
namespace WebKit {
 
59
 
 
60
NSString *SchemeForCustomProtocolRegisteredNotificationName = @"WebKitSchemeForCustomProtocolRegisteredNotification";
 
61
NSString *SchemeForCustomProtocolUnregisteredNotificationName = @"WebKitSchemeForCustomProtocolUnregisteredNotification";
 
62
 
 
63
bool WebContext::s_applicationIsOccluded = false;
 
64
 
 
65
String WebContext::applicationCacheDirectory()
 
66
{
 
67
    NSString *appName = [[NSBundle mainBundle] bundleIdentifier];
 
68
    if (!appName)
 
69
        appName = [[NSProcessInfo processInfo] processName];
 
70
    
 
71
    ASSERT(appName);
 
72
    
 
73
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
 
74
    NSString *cacheDir = [defaults objectForKey:WebKitLocalCacheDefaultsKey];
 
75
 
 
76
    if (!cacheDir || ![cacheDir isKindOfClass:[NSString class]]) {
 
77
        char cacheDirectory[MAXPATHLEN];
 
78
        size_t cacheDirectoryLen = confstr(_CS_DARWIN_USER_CACHE_DIR, cacheDirectory, MAXPATHLEN);
 
79
    
 
80
        if (cacheDirectoryLen)
 
81
            cacheDir = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:cacheDirectory length:cacheDirectoryLen - 1];
 
82
    }
 
83
 
 
84
    return [cacheDir stringByAppendingPathComponent:appName];
 
85
}
 
86
 
 
87
static void registerUserDefaultsIfNeeded()
 
88
{
 
89
    static bool didRegister;
 
90
    if (didRegister)
 
91
        return;
 
92
 
 
93
    didRegister = true;
 
94
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
 
95
    [[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:WebKitKerningAndLigaturesEnabledByDefaultDefaultsKey]];
 
96
#endif
 
97
}
 
98
 
 
99
void WebContext::platformInitializeWebProcess(WebProcessCreationParameters& parameters)
 
100
{
 
101
    parameters.presenterApplicationPid = getpid();
 
102
 
 
103
    parameters.parentProcessName = [[NSProcessInfo processInfo] processName];    
 
104
 
 
105
    NSURLCache *urlCache = [NSURLCache sharedURLCache];
 
106
    parameters.nsURLCacheMemoryCapacity = [urlCache memoryCapacity];
 
107
    parameters.nsURLCacheDiskCapacity = [urlCache diskCapacity];
 
108
 
 
109
    registerUserDefaultsIfNeeded();
 
110
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
 
111
    parameters.shouldForceScreenFontSubstitution = [[NSUserDefaults standardUserDefaults] boolForKey:@"NSFontDefaultScreenFontSubstitutionEnabled"];
 
112
#endif
 
113
    parameters.shouldEnableKerningAndLigaturesByDefault = [[NSUserDefaults standardUserDefaults] boolForKey:WebKitKerningAndLigaturesEnabledByDefaultDefaultsKey];
 
114
 
 
115
#if USE(ACCELERATED_COMPOSITING) && HAVE(HOSTED_CORE_ANIMATION)
 
116
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
 
117
    mach_port_t renderServerPort = [[CARemoteLayerServer sharedServer] serverPort];
 
118
#else
 
119
    mach_port_t renderServerPort = WKInitializeRenderServer();
 
120
#endif
 
121
    if (renderServerPort != MACH_PORT_NULL)
 
122
        parameters.acceleratedCompositingPort = CoreIPC::MachPort(renderServerPort, MACH_MSG_TYPE_COPY_SEND);
 
123
#endif
 
124
 
 
125
    // FIXME: This should really be configurable; we shouldn't just blindly allow read access to the UI process bundle.
 
126
    parameters.uiProcessBundleResourcePath = [[NSBundle mainBundle] resourcePath];
 
127
    SandboxExtension::createHandle(parameters.uiProcessBundleResourcePath, SandboxExtension::ReadOnly, parameters.uiProcessBundleResourcePathExtensionHandle);
 
128
 
 
129
    parameters.uiProcessBundleIdentifier = String([[NSBundle mainBundle] bundleIdentifier]);
 
130
    
 
131
    NSArray *schemes = [[WKBrowsingContextController customSchemes] allObjects];
 
132
    for (size_t i = 0; i < [schemes count]; ++i)
 
133
        parameters.urlSchemesRegisteredForCustomProtocols.append([schemes objectAtIndex:i]);
 
134
    
 
135
    m_customSchemeRegisteredObserver = [[NSNotificationCenter defaultCenter] addObserverForName:WebKit::SchemeForCustomProtocolRegisteredNotificationName object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *notification) {
 
136
        NSString *scheme = [notification object];
 
137
        ASSERT([scheme isKindOfClass:[NSString class]]);
 
138
        sendToAllProcesses(Messages::WebProcess::RegisterSchemeForCustomProtocol(scheme));
 
139
    }];
 
140
    
 
141
    m_customSchemeUnregisteredObserver = [[NSNotificationCenter defaultCenter] addObserverForName:WebKit::SchemeForCustomProtocolUnregisteredNotificationName object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *notification) {
 
142
        NSString *scheme = [notification object];
 
143
        ASSERT([scheme isKindOfClass:[NSString class]]);
 
144
        sendToAllProcesses(Messages::WebProcess::UnregisterSchemeForCustomProtocol(scheme));
 
145
    }];
 
146
    
 
147
    // Listen for enhanced accessibility changes and propagate them to the WebProcess.
 
148
    m_enhancedAccessibilityObserver = [[NSNotificationCenter defaultCenter] addObserverForName:WebKitApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *note) {
 
149
        setEnhancedAccessibility([[[note userInfo] objectForKey:@"AXEnhancedUserInterface"] boolValue]);
 
150
    }];
 
151
}
 
152
 
 
153
void WebContext::platformInvalidateContext()
 
154
{
 
155
    [[NSNotificationCenter defaultCenter] removeObserver:(id)m_enhancedAccessibilityObserver.get()];
 
156
}
 
157
 
 
158
String WebContext::platformDefaultDiskCacheDirectory() const
 
159
{
 
160
    RetainPtr<NSString> cachePath(AdoptNS, (NSString *)WKCopyFoundationCacheDirectory());
 
161
    if (!cachePath)
 
162
        cachePath = @"~/Library/Caches/com.apple.WebKit2.WebProcess";
 
163
 
 
164
    return [cachePath.get() stringByStandardizingPath];
 
165
}
 
166
 
 
167
String WebContext::platformDefaultCookieStorageDirectory() const
 
168
{
 
169
    notImplemented();
 
170
    return [@"" stringByStandardizingPath];
 
171
}
 
172
 
 
173
String WebContext::platformDefaultDatabaseDirectory() const
 
174
{
 
175
    NSString *databasesDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebDatabaseDirectoryDefaultsKey];
 
176
    if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
 
177
        databasesDirectory = @"~/Library/WebKit/Databases";
 
178
    return [databasesDirectory stringByStandardizingPath];
 
179
}
 
180
 
 
181
String WebContext::platformDefaultIconDatabasePath() const
 
182
{
 
183
    // FIXME: <rdar://problem/9138817> - After this "backwards compatibility" radar is removed, this code should be removed to only return an empty String.
 
184
    NSString *databasesDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebIconDatabaseDirectoryDefaultsKey];
 
185
    if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
 
186
        databasesDirectory = @"~/Library/Icons/WebpageIcons.db";
 
187
    return [databasesDirectory stringByStandardizingPath];
 
188
}
 
189
 
 
190
String WebContext::platformDefaultLocalStorageDirectory() const
 
191
{
 
192
    NSString *localStorageDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebStorageDirectoryDefaultsKey];
 
193
    if (!localStorageDirectory || ![localStorageDirectory isKindOfClass:[NSString class]])
 
194
        localStorageDirectory = @"~/Library/WebKit/LocalStorage";
 
195
    return [localStorageDirectory stringByStandardizingPath];
 
196
}
 
197
 
 
198
bool WebContext::omitPDFSupport()
 
199
{
 
200
    // Since this is a "secret default" we don't bother registering it.
 
201
    return [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitOmitPDFSupport"];
 
202
}
 
203
 
 
204
void WebContext::getPasteboardTypes(const String& pasteboardName, Vector<String>& pasteboardTypes)
 
205
{
 
206
    PlatformPasteboard(pasteboardName).getTypes(pasteboardTypes);
 
207
}
 
208
 
 
209
void WebContext::getPasteboardPathnamesForType(const String& pasteboardName, const String& pasteboardType, Vector<String>& pathnames)
 
210
{
 
211
    PlatformPasteboard(pasteboardName).getPathnamesForType(pathnames, pasteboardType);
 
212
}
 
213
 
 
214
void WebContext::getPasteboardStringForType(const String& pasteboardName, const String& pasteboardType, String& string)
 
215
{
 
216
    string = PlatformPasteboard(pasteboardName).stringForType(pasteboardType);
 
217
}
 
218
 
 
219
void WebContext::getPasteboardBufferForType(const String& pasteboardName, const String& pasteboardType, SharedMemory::Handle& handle, uint64_t& size)
 
220
{
 
221
    RefPtr<SharedBuffer> buffer = PlatformPasteboard(pasteboardName).bufferForType(pasteboardType);
 
222
    if (!buffer)
 
223
        return;
 
224
    size = buffer->size();
 
225
    RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(size);
 
226
    memcpy(sharedMemoryBuffer->data(), buffer->data(), size);
 
227
    sharedMemoryBuffer->createHandle(handle, SharedMemory::ReadOnly);
 
228
}
 
229
 
 
230
void WebContext::pasteboardCopy(const String& fromPasteboard, const String& toPasteboard)
 
231
{
 
232
    PlatformPasteboard(toPasteboard).copy(fromPasteboard);
 
233
}
 
234
 
 
235
void WebContext::getPasteboardChangeCount(const String& pasteboardName, uint64_t& changeCount)
 
236
{
 
237
    changeCount = PlatformPasteboard(pasteboardName).changeCount();
 
238
}
 
239
 
 
240
void WebContext::getPasteboardUniqueName(String& pasteboardName)
 
241
{
 
242
    pasteboardName = PlatformPasteboard::uniqueName();
 
243
}
 
244
 
 
245
void WebContext::getPasteboardColor(const String& pasteboardName, WebCore::Color& color)
 
246
{
 
247
    color = PlatformPasteboard(pasteboardName).color();    
 
248
}
 
249
 
 
250
void WebContext::getPasteboardURL(const String& pasteboardName, WTF::String& urlString)
 
251
{
 
252
    urlString = PlatformPasteboard(pasteboardName).url().string();
 
253
}
 
254
 
 
255
void WebContext::addPasteboardTypes(const String& pasteboardName, const Vector<String>& pasteboardTypes)
 
256
{
 
257
    PlatformPasteboard(pasteboardName).addTypes(pasteboardTypes);
 
258
}
 
259
 
 
260
void WebContext::setPasteboardTypes(const String& pasteboardName, const Vector<String>& pasteboardTypes)
 
261
{
 
262
    PlatformPasteboard(pasteboardName).setTypes(pasteboardTypes);
 
263
}
 
264
 
 
265
void WebContext::setPasteboardPathnamesForType(const String& pasteboardName, const String& pasteboardType, const Vector<String>& pathnames)
 
266
{
 
267
    PlatformPasteboard(pasteboardName).setPathnamesForType(pathnames, pasteboardType);
 
268
}
 
269
 
 
270
void WebContext::setPasteboardStringForType(const String& pasteboardName, const String& pasteboardType, const String& string)
 
271
{
 
272
    PlatformPasteboard(pasteboardName).setStringForType(string, pasteboardType);    
 
273
}
 
274
 
 
275
void WebContext::setPasteboardBufferForType(const String& pasteboardName, const String& pasteboardType, const SharedMemory::Handle& handle, uint64_t size)
 
276
{
 
277
    if (handle.isNull()) {
 
278
        PlatformPasteboard(pasteboardName).setBufferForType(0, pasteboardType);
 
279
        return;
 
280
    }
 
281
    RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(handle, SharedMemory::ReadOnly);
 
282
    RefPtr<SharedBuffer> buffer = SharedBuffer::create(static_cast<unsigned char *>(sharedMemoryBuffer->data()), size);
 
283
    PlatformPasteboard(pasteboardName).setBufferForType(buffer, pasteboardType);
 
284
}
 
285
 
 
286
void WebContext::applicationBecameVisible(uint32_t, void*, uint32_t, void*, uint32_t)
 
287
{
 
288
    if (s_applicationIsOccluded) {
 
289
        s_applicationIsOccluded = false;
 
290
 
 
291
        const Vector<WebContext*>& contexts = WebContext::allContexts();
 
292
        for (size_t i = 0, count = contexts.size(); i < count; ++i)
 
293
            contexts[i]->sendToAllProcesses(Messages::WebProcess::SetApplicationIsOccluded(false));
 
294
 
 
295
#if ENABLE(PLUGIN_PROCESS)
 
296
        PluginProcessManager::shared().setApplicationIsOccluded(false);
 
297
#endif
 
298
#if ENABLE(NETWORK_PROCESS)
 
299
        NetworkProcessManager::shared().setApplicationIsOccluded(false);
 
300
#endif
 
301
#if ENABLE(SHARED_WORKER_PROCESS)
 
302
        SharedWorkerProcessManager::shared().setApplicationIsOccluded(false);
 
303
#endif
 
304
    }
 
305
}
 
306
 
 
307
void WebContext::applicationBecameOccluded(uint32_t, void*, uint32_t, void*, uint32_t)
 
308
{
 
309
    if (!s_applicationIsOccluded) {
 
310
        s_applicationIsOccluded = true;
 
311
        const Vector<WebContext*>& contexts = WebContext::allContexts();
 
312
        for (size_t i = 0, count = contexts.size(); i < count; ++i)
 
313
            contexts[i]->sendToAllProcesses(Messages::WebProcess::SetApplicationIsOccluded(true));
 
314
 
 
315
#if ENABLE(PLUGIN_PROCESS)
 
316
        PluginProcessManager::shared().setApplicationIsOccluded(true);
 
317
#endif
 
318
#if ENABLE(NETWORK_PROCESS)
 
319
        NetworkProcessManager::shared().setApplicationIsOccluded(true);
 
320
#endif
 
321
#if ENABLE(SHARED_WORKER_PROCESS)
 
322
        SharedWorkerProcessManager::shared().setApplicationIsOccluded(true);
 
323
#endif
 
324
    }
 
325
}
 
326
 
 
327
void WebContext::initializeProcessSuppressionSupport()
 
328
{
 
329
    static bool didInitialize = false;
 
330
    if (didInitialize)
 
331
        return;
 
332
 
 
333
    didInitialize = true;
 
334
    // A temporary default until process suppression is enabled by default, at which point a context setting can be added with the
 
335
    // interpretation that any context disabling process suppression disables it for plugin/network and shared worker processes.
 
336
    bool processSuppressionSupportEnabled = [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitProcessSuppressionSupportEnabled"];
 
337
    if (processSuppressionSupportEnabled)
 
338
        registerOcclusionNotificationHandlers();
 
339
}
 
340
 
 
341
void WebContext::registerOcclusionNotificationHandlers()
 
342
{
 
343
#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1090
 
344
    if (!WKRegisterOcclusionNotificationHandler(WKOcclusionNotificationTypeApplicationBecameVisible, applicationBecameVisible)) {
 
345
        WTFLogAlways("Registeration of \"App Became Visible\" notification handler failed.\n");
 
346
        return;
 
347
    }
 
348
 
 
349
    if (!WKRegisterOcclusionNotificationHandler(WKOcclusionNotificationTypeApplicationBecameOccluded, applicationBecameOccluded))
 
350
        WTFLogAlways("Registeration of \"App Became Occluded\" notification handler failed.\n");
 
351
#endif
 
352
}
 
353
 
 
354
} // namespace WebKit