Fix the build with newer clang and other custom configuration options
[WebKit-https.git] / Source / WebKit2 / NetworkProcess / mac / NetworkProcessMac.mm
1 /*
2  * Copyright (C) 2012 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 "NetworkProcess.h"
28
29 #if PLATFORM(MAC)
30
31 #import "NetworkCache.h"
32 #import "NetworkProcessCreationParameters.h"
33 #import "NetworkResourceLoader.h"
34 #import "ResourceCachesToClear.h"
35 #import "SandboxExtension.h"
36 #import "SandboxInitializationParameters.h"
37 #import "SecItemShim.h"
38 #import "StringUtilities.h"
39 #import <WebCore/CFNetworkSPI.h>
40 #import <WebCore/CertificateInfo.h>
41 #import <WebCore/FileSystem.h>
42 #import <WebCore/LocalizedStrings.h>
43 #import <WebCore/MemoryPressureHandler.h>
44 #import <WebKitSystemInterface.h>
45 #import <notify.h>
46 #import <sysexits.h>
47 #import <wtf/text/WTFString.h>
48
49 using namespace WebCore;
50
51 namespace WebKit {
52
53 void NetworkProcess::initializeProcess(const ChildProcessInitializationParameters&)
54 {
55     // Having a window server connection in this process would result in spin logs (<rdar://problem/13239119>).
56     setApplicationIsDaemon();
57 }
58
59 void NetworkProcess::initializeProcessName(const ChildProcessInitializationParameters& parameters)
60 {
61     NSString *applicationName = [NSString stringWithFormat:WEB_UI_STRING("%@ Networking", "visible name of the network process. The argument is the application name."), (NSString *)parameters.uiProcessName];
62     WKSetVisibleApplicationName((CFStringRef)applicationName);
63 }
64
65 static void overrideSystemProxies(const String& httpProxy, const String& httpsProxy)
66 {
67     NSMutableDictionary *proxySettings = [NSMutableDictionary dictionary];
68
69     if (!httpProxy.isNull()) {
70         URL httpProxyURL(URL(), httpProxy);
71         if (httpProxyURL.isValid()) {
72             [proxySettings setObject:nsStringFromWebCoreString(httpProxyURL.host()) forKey:(NSString *)kCFNetworkProxiesHTTPProxy];
73             if (httpProxyURL.hasPort()) {
74                 NSNumber *port = [NSNumber numberWithInt:httpProxyURL.port()];
75                 [proxySettings setObject:port forKey:(NSString *)kCFNetworkProxiesHTTPPort];
76             }
77         }
78         else
79             NSLog(@"Malformed HTTP Proxy URL '%s'.  Expected 'http://<hostname>[:<port>]'\n", httpProxy.utf8().data());
80     }
81
82     if (!httpsProxy.isNull()) {
83         URL httpsProxyURL(URL(), httpsProxy);
84         if (httpsProxyURL.isValid()) {
85             [proxySettings setObject:nsStringFromWebCoreString(httpsProxyURL.host()) forKey:(NSString *)kCFNetworkProxiesHTTPSProxy];
86             if (httpsProxyURL.hasPort()) {
87                 NSNumber *port = [NSNumber numberWithInt:httpsProxyURL.port()];
88                 [proxySettings setObject:port forKey:(NSString *)kCFNetworkProxiesHTTPSPort];
89             }
90         } else
91             NSLog(@"Malformed HTTPS Proxy URL '%s'.  Expected 'https://<hostname>[:<port>]'\n", httpsProxy.utf8().data());
92     }
93
94     if ([proxySettings count] > 0)
95         _CFNetworkSetOverrideSystemProxySettings((CFDictionaryRef)proxySettings);
96 }
97
98 void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreationParameters& parameters)
99 {
100     platformInitializeNetworkProcessCocoa(parameters);
101
102 #if ENABLE(SEC_ITEM_SHIM)
103     SecItemShim::singleton().initialize(this);
104 #endif
105
106     if (!parameters.httpProxy.isNull() || !parameters.httpsProxy.isNull())
107         overrideSystemProxies(parameters.httpProxy, parameters.httpsProxy);
108 }
109
110 void NetworkProcess::allowSpecificHTTPSCertificateForHost(const CertificateInfo& certificateInfo, const String& host)
111 {
112     [NSURLRequest setAllowsSpecificHTTPSCertificate:(NSArray *)certificateInfo.certificateChain() forHost:(NSString *)host];
113 }
114
115 void NetworkProcess::initializeSandbox(const ChildProcessInitializationParameters& parameters, SandboxInitializationParameters& sandboxParameters)
116 {
117     // Need to overide the default, because service has a different bundle ID.
118     NSBundle *webkit2Bundle = [NSBundle bundleForClass:NSClassFromString(@"WKView")];
119     sandboxParameters.setOverrideSandboxProfilePath([webkit2Bundle pathForResource:@"com.apple.WebKit.NetworkProcess" ofType:@"sb"]);
120
121     ChildProcess::initializeSandbox(parameters, sandboxParameters);
122 }
123
124 void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
125 {
126     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
127     if (resourceCachesToClear == InMemoryResourceCachesOnly)
128         return;
129
130     clearDiskCache(std::chrono::system_clock::time_point::min(), [] { });
131 }
132
133 void NetworkProcess::platformTerminate()
134 {
135     if (m_clearCacheDispatchGroup) {
136         dispatch_group_wait(m_clearCacheDispatchGroup, DISPATCH_TIME_FOREVER);
137         dispatch_release(m_clearCacheDispatchGroup);
138         m_clearCacheDispatchGroup = 0;
139     }
140 }
141
142 } // namespace WebKit
143
144 #endif // PLATFORM(MAC)