<http://webkit.org/b/91015> Remove BUILDING_ON / TARGETING macros in favor of system...
[WebKit-https.git] / 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/Color.h>
32 #import <WebCore/FileSystem.h>
33 #import <WebCore/PlatformPasteboard.h>
34 #import <sys/param.h>
35
36 #if HAVE(HOSTED_CORE_ANIMATION) && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
37 #import <QuartzCore/CARemoteLayerServer.h>
38 #endif
39
40 using namespace WebCore;
41
42 NSString *WebDatabaseDirectoryDefaultsKey = @"WebDatabaseDirectory";
43 NSString *WebKitLocalCacheDefaultsKey = @"WebKitLocalCache";
44 NSString *WebStorageDirectoryDefaultsKey = @"WebKitLocalStorageDatabasePathPreferenceKey";
45
46 static NSString *WebKitApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification = @"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification";
47
48 // FIXME: <rdar://problem/9138817> - After this "backwards compatibility" radar is removed, this code should be removed to only return an empty String.
49 NSString *WebIconDatabaseDirectoryDefaultsKey = @"WebIconDatabaseDirectoryDefaultsKey";
50
51 namespace WebKit {
52
53 String WebContext::applicationCacheDirectory()
54 {
55     NSString *appName = [[NSBundle mainBundle] bundleIdentifier];
56     if (!appName)
57         appName = [[NSProcessInfo processInfo] processName];
58     
59     ASSERT(appName);
60     
61     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
62     NSString *cacheDir = [defaults objectForKey:WebKitLocalCacheDefaultsKey];
63
64     if (!cacheDir || ![cacheDir isKindOfClass:[NSString class]]) {
65         char cacheDirectory[MAXPATHLEN];
66         size_t cacheDirectoryLen = confstr(_CS_DARWIN_USER_CACHE_DIR, cacheDirectory, MAXPATHLEN);
67     
68         if (cacheDirectoryLen)
69             cacheDir = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:cacheDirectory length:cacheDirectoryLen - 1];
70     }
71
72     return [cacheDir stringByAppendingPathComponent:appName];
73 }
74
75
76 void WebContext::platformInitializeWebProcess(WebProcessCreationParameters& parameters)
77 {
78     if (!omitPDFSupport()) {
79         // We want to use a PDF view in the UI process for PDF MIME types.
80         HashSet<String, CaseFoldingHash> mimeType = pdfAndPostScriptMIMETypes();
81         parameters.mimeTypesWithCustomRepresentation.appendRange(mimeType.begin(), mimeType.end());
82     }
83
84     RetainPtr<CFStringRef> cachePath(AdoptCF, WKCopyFoundationCacheDirectory());
85     if (!cachePath)
86         cachePath = reinterpret_cast<CFStringRef>(NSHomeDirectory());
87
88     NSURLCache *urlCache = [NSURLCache sharedURLCache];
89
90     parameters.parentProcessName = [[NSProcessInfo processInfo] processName];    
91     parameters.nsURLCachePath = [(NSString *)cachePath.get() stringByStandardizingPath];
92     parameters.nsURLCacheMemoryCapacity = [urlCache memoryCapacity];
93     parameters.nsURLCacheDiskCapacity = [urlCache diskCapacity];
94
95     ASSERT(!parameters.nsURLCachePath.isEmpty());
96
97 #if ENABLE(PLUGIN_PROCESS)
98     parameters.disablePluginProcessMessageTimeout = [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitDisablePluginProcessMessageTimeout"];
99 #endif
100
101 #if USE(ACCELERATED_COMPOSITING) && HAVE(HOSTED_CORE_ANIMATION)
102 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
103     mach_port_t renderServerPort = [[CARemoteLayerServer sharedServer] serverPort];
104 #else
105     mach_port_t renderServerPort = WKInitializeRenderServer();
106 #endif
107     if (renderServerPort != MACH_PORT_NULL)
108         parameters.acceleratedCompositingPort = CoreIPC::MachPort(renderServerPort, MACH_MSG_TYPE_COPY_SEND);
109 #endif
110
111     // FIXME: This should really be configurable; we shouldn't just blindly allow read access to the UI process bundle.
112     parameters.uiProcessBundleResourcePath = [[NSBundle mainBundle] resourcePath];
113
114     parameters.uiProcessBundleIdentifier = String([[NSBundle mainBundle] bundleIdentifier]);
115     
116     // Listen for enhanced accessibility changes and propagate them to the WebProcess.
117     m_enhancedAccessibilityObserver = [[NSNotificationCenter defaultCenter] addObserverForName:WebKitApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification object:nil queue:[NSOperationQueue currentQueue] usingBlock:^(NSNotification *note) {
118         setEnhancedAccessibility([[[note userInfo] objectForKey:@"AXEnhancedUserInterface"] boolValue]);
119     }];
120 }
121
122 void WebContext::platformInvalidateContext()
123 {
124     [[NSNotificationCenter defaultCenter] removeObserver:(id)m_enhancedAccessibilityObserver.get()];
125 }
126     
127 String WebContext::platformDefaultDatabaseDirectory() const
128 {
129     NSString *databasesDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebDatabaseDirectoryDefaultsKey];
130     if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
131         databasesDirectory = @"~/Library/WebKit/Databases";
132     return [databasesDirectory stringByStandardizingPath];
133 }
134
135 String WebContext::platformDefaultIconDatabasePath() const
136 {
137     // FIXME: <rdar://problem/9138817> - After this "backwards compatibility" radar is removed, this code should be removed to only return an empty String.
138     NSString *databasesDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebIconDatabaseDirectoryDefaultsKey];
139     if (!databasesDirectory || ![databasesDirectory isKindOfClass:[NSString class]])
140         databasesDirectory = @"~/Library/Icons/WebpageIcons.db";
141     return [databasesDirectory stringByStandardizingPath];
142 }
143
144 String WebContext::platformDefaultLocalStorageDirectory() const
145 {
146     NSString *localStorageDirectory = [[NSUserDefaults standardUserDefaults] objectForKey:WebStorageDirectoryDefaultsKey];
147     if (!localStorageDirectory || ![localStorageDirectory isKindOfClass:[NSString class]])
148         localStorageDirectory = @"~/Library/WebKit/LocalStorage";
149     return [localStorageDirectory stringByStandardizingPath];
150 }
151
152 bool WebContext::omitPDFSupport()
153 {
154     // Since this is a "secret default" we don't bother registering it.
155     return [[NSUserDefaults standardUserDefaults] boolForKey:@"WebKitOmitPDFSupport"];
156 }
157
158 void WebContext::getPasteboardTypes(const String& pasteboardName, Vector<String>& pasteboardTypes)
159 {
160     PlatformPasteboard(pasteboardName).getTypes(pasteboardTypes);
161 }
162
163 void WebContext::getPasteboardPathnamesForType(const String& pasteboardName, const String& pasteboardType, Vector<String>& pathnames)
164 {
165     PlatformPasteboard(pasteboardName).getPathnamesForType(pathnames, pasteboardType);
166 }
167
168 void WebContext::getPasteboardStringForType(const String& pasteboardName, const String& pasteboardType, String& string)
169 {
170     string = PlatformPasteboard(pasteboardName).stringForType(pasteboardType);
171 }
172
173 void WebContext::getPasteboardBufferForType(const String& pasteboardName, const String& pasteboardType, SharedMemory::Handle& handle, uint64_t& size)
174 {
175     RefPtr<SharedBuffer> buffer = PlatformPasteboard(pasteboardName).bufferForType(pasteboardType);
176     if (!buffer)
177         return;
178     size = buffer->size();
179     RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(size);
180     memcpy(sharedMemoryBuffer->data(), buffer->data(), size);
181     sharedMemoryBuffer->createHandle(handle, SharedMemory::ReadOnly);
182 }
183
184 void WebContext::pasteboardCopy(const String& fromPasteboard, const String& toPasteboard)
185 {
186     PlatformPasteboard(toPasteboard).copy(fromPasteboard);
187 }
188
189 void WebContext::getPasteboardChangeCount(const String& pasteboardName, uint64_t& changeCount)
190 {
191     changeCount = PlatformPasteboard(pasteboardName).changeCount();
192 }
193
194 void WebContext::getPasteboardUniqueName(String& pasteboardName)
195 {
196     pasteboardName = PlatformPasteboard::uniqueName();
197 }
198
199 void WebContext::getPasteboardColor(const String& pasteboardName, WebCore::Color& color)
200 {
201     color = PlatformPasteboard(pasteboardName).color();    
202 }
203
204 void WebContext::getPasteboardURL(const String& pasteboardName, WTF::String& urlString)
205 {
206     urlString = PlatformPasteboard(pasteboardName).url().string();
207 }
208
209 void WebContext::addPasteboardTypes(const String& pasteboardName, const Vector<String>& pasteboardTypes)
210 {
211     PlatformPasteboard(pasteboardName).addTypes(pasteboardTypes);
212 }
213
214 void WebContext::setPasteboardTypes(const String& pasteboardName, const Vector<String>& pasteboardTypes)
215 {
216     PlatformPasteboard(pasteboardName).setTypes(pasteboardTypes);
217 }
218
219 void WebContext::setPasteboardPathnamesForType(const String& pasteboardName, const String& pasteboardType, const Vector<String>& pathnames)
220 {
221     PlatformPasteboard(pasteboardName).setPathnamesForType(pathnames, pasteboardType);
222 }
223
224 void WebContext::setPasteboardStringForType(const String& pasteboardName, const String& pasteboardType, const String& string)
225 {
226     PlatformPasteboard(pasteboardName).setStringForType(string, pasteboardType);    
227 }
228
229 void WebContext::setPasteboardBufferForType(const String& pasteboardName, const String& pasteboardType, const SharedMemory::Handle& handle, uint64_t size)
230 {
231     if (handle.isNull()) {
232         PlatformPasteboard(pasteboardName).setBufferForType(0, pasteboardType);
233         return;
234     }
235     RefPtr<SharedMemory> sharedMemoryBuffer = SharedMemory::create(handle, SharedMemory::ReadOnly);
236     RefPtr<SharedBuffer> buffer = SharedBuffer::create(static_cast<unsigned char *>(sharedMemoryBuffer->data()), size);
237     PlatformPasteboard(pasteboardName).setBufferForType(buffer, pasteboardType);
238 }
239
240 } // namespace WebKit
241