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
34using namespace WebCore;
35
36NSString *WebDatabaseDirectoryDefaultsKey = @"WebDatabaseDirectory";
37NSString *WebKitLocalCacheDefaultsKey = @"WebKitLocalCache";
38NSString *WebStorageDirectoryDefaultsKey = @"WebKitLocalStorageDatabasePathPreferenceKey";
39
40static 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.
43NSString *WebIconDatabaseDirectoryDefaultsKey = @"WebIconDatabaseDirectoryDefaultsKey";
44
45namespace WebKit {
46
47String 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#ifdef BUILDING_ON_TIGER
60        cacheDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Caches"];
61#else
62        char cacheDirectory[MAXPATHLEN];
63        size_t cacheDirectoryLen = confstr(_CS_DARWIN_USER_CACHE_DIR, cacheDirectory, MAXPATHLEN);
64
65        if (cacheDirectoryLen)
66            cacheDir = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:cacheDirectory length:cacheDirectoryLen - 1];
67#endif
68    }
69
70    return [cacheDir stringByAppendingPathComponent:appName];
71}
72
73
74void WebContext::platformInitializeWebProcess(WebProcessCreationParameters& parameters)
75{
76    // We want to use a PDF view in the UI process for PDF MIME types.
77    HashSet<String, CaseFoldingHash> mimeType = pdfAndPostScriptMIMETypes();
78    parameters.mimeTypesWithCustomRepresentation.appendRange(mimeType.begin(), mimeType.end());
79
80    RetainPtr<CFStringRef> cachePath(AdoptCF, WKCopyFoundationCacheDirectory());
81    if (!cachePath)
82        cachePath = reinterpret_cast<CFStringRef>(NSHomeDirectory());
83
84    NSURLCache *urlCache = [NSURLCache sharedURLCache];
85
86    parameters.parentProcessName = [[NSProcessInfo processInfo] processName];
87    parameters.nsURLCachePath = [(NSString *)cachePath.get() stringByStandardizingPath];
88    parameters.nsURLCacheMemoryCapacity = [urlCache memoryCapacity];
89    parameters.nsURLCacheDiskCapacity = [urlCache diskCapacity];
90
91    ASSERT(!parameters.nsURLCachePath.isEmpty());
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
112void WebContext::platformInvalidateContext()
113{
114    [[NSNotificationCenter defaultCenter] removeObserver:(id)m_enhancedAccessibilityObserver.get()];
115}
116
117String 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
125String 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
134String 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
144