initial import
[vuplus_webkit] / Source / WebKit2 / UIProcess / mac / WebContextMac.mm
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 "WebKitSystemInterface.h"
30 #import "WebProcessCreationParameters.h"
31 #import <WebCore/FileSystem.h>
32 #import <sys/param.h>
33
34 using namespace WebCore;
35
36 NSString *WebDatabaseDirectoryDefaultsKey = @"WebDatabaseDirectory";
37 NSString *WebKitLocalCacheDefaultsKey = @"WebKitLocalCache";
38 NSString *WebStorageDirectoryDefaultsKey = @"WebKitLocalStorageDatabasePathPreferenceKey";
39
40 static NSString *WebKitApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification = @"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification";
41
42 // FIXME: <rdar://problem/9138817> - After this "backwards compatibility" radar is removed, this code should be removed to only return an empty String.
43 NSString *WebIconDatabaseDirectoryDefaultsKey = @"WebIconDatabaseDirectoryDefaultsKey";
44
45 namespace WebKit {
46
47 String WebContext::applicationCacheDirectory()
48 {
49     NSString *appName = [[NSBundle mainBundle] bundleIdentifier];
50     if (!appName)
51         appName = [[NSProcessInfo processInfo] processName];
52     
53     ASSERT(appName);
54     
55     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
56     NSString *cacheDir = [defaults objectForKey:WebKitLocalCacheDefaultsKey];
57
58     if (!cacheDir || ![cacheDir isKindOfClass:[NSString class]]) {
59         char cacheDirectory[MAXPATHLEN];
60         size_t cacheDirectoryLen = confstr(_CS_DARWIN_USER_CACHE_DIR, cacheDirectory, MAXPATHLEN);
61     
62         if (cacheDirectoryLen)
63             cacheDir = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:cacheDirectory length:cacheDirectoryLen - 1];
64     }
65
66     return [cacheDir stringByAppendingPathComponent:appName];
67 }
68
69
70 void WebContext::platformInitializeWebProcess(WebProcessCreationParameters& parameters)
71 {
72     // We want to use a PDF view in the UI process for PDF MIME types.
73     HashSet<String, CaseFoldingHash> mimeType = pdfAndPostScriptMIMETypes();
74     parameters.mimeTypesWithCustomRepresentation.appendRange(mimeType.begin(), mimeType.end());
75
76     RetainPtr<CFStringRef> cachePath(AdoptCF, WKCopyFoundationCacheDirectory());
77     if (!cachePath)
78         cachePath = reinterpret_cast<CFStringRef>(NSHomeDirectory());
79
80     NSURLCache *urlCache = [NSURLCache sharedURLCache];
81
82     parameters.parentProcessName = [[NSProcessInfo processInfo] processName];    
83     parameters.nsURLCachePath = [(NSString *)cachePath.get() stringByStandardizingPath];
84     parameters.nsURLCacheMemoryCapacity = [urlCache memoryCapacity];
85     parameters.nsURLCacheDiskCapacity = [urlCache diskCapacity];
86
87     ASSERT(!parameters.nsURLCachePath.isEmpty());
88
89 #if ENABLE(PLUGIN_PROCESS)
90     parameters.disablePluginProcessMessageTimeout = [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitDisablePluginProcessMessageTimeout"];
91 #endif
92
93 #if USE(ACCELERATED_COMPOSITING) && HAVE(HOSTED_CORE_ANIMATION)
94     mach_port_t renderServerPort = WKInitializeRenderServer();
95     if (renderServerPort != MACH_PORT_NULL)
96         parameters.acceleratedCompositingPort = CoreIPC::MachPort(renderServerPort, MACH_MSG_TYPE_COPY_SEND);
97 #endif
98
99     // FIXME: This should really be configurable; we shouldn't just blindly allow read access to the UI process bundle.
100     parameters.uiProcessBundleResourcePath = [[NSBundle mainBundle] resourcePath];
101
102 #if USE(CFURLSTORAGESESSIONS)
103     parameters.uiProcessBundleIdentifier = String([[NSBundle mainBundle] bundleIdentifier]);
104 #endif
105     
106     // Listen for enhanced accessibility changes and propagate them to the WebProcess.
107     m_enhancedAccessibilityObserver = [[NSNotificationCenter defaultCenter] addObserverForName:WebKitApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *note) {
108         setEnhancedAccessibility([[[note userInfo] objectForKey:@"AXEnhancedUserInterface"] boolValue]);
109     }];
110 }
111
112 void WebContext::platformInvalidateContext()
113 {
114     [[NSNotificationCenter defaultCenter] removeObserver:(id)m_enhancedAccessibilityObserver.get()];
115 }
116     
117 String WebContext::platformDefaultDatabaseDirectory() const
118 {
119     NSString *databasesDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebDatabaseDirectoryDefaultsKey];
120     if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
121         databasesDirectory = @"~/Library/WebKit/Databases";
122     return [databasesDirectory stringByStandardizingPath];
123 }
124
125 String WebContext::platformDefaultIconDatabasePath() const
126 {
127     // FIXME: <rdar://problem/9138817> - After this "backwards compatibility" radar is removed, this code should be removed to only return an empty String.
128     NSString *databasesDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebIconDatabaseDirectoryDefaultsKey];
129     if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
130         databasesDirectory = @"~/Library/Icons/WebpageIcons.db";
131     return [databasesDirectory stringByStandardizingPath];
132 }
133
134 String WebContext::platformDefaultLocalStorageDirectory() const
135 {
136     NSString *localStorageDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebStorageDirectoryDefaultsKey];
137     if (!localStorageDirectory || ![localStorageDirectory isKindOfClass:[NSString class]])
138         localStorageDirectory = @"~/Library/WebKit/LocalStorage";
139     return [localStorageDirectory stringByStandardizingPath];
140 }
141
142 } // namespace WebKit
143