dd579a510a9e757ce3204c64a09a7e1e7cdcec47
[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 @interface NSURLRequest (Details) 
52 + (void)setAllowsSpecificHTTPSCertificate:(NSArray *)allow forHost:(NSString *)host;
53 @end
54
55 namespace WebKit {
56
57 void NetworkProcess::initializeProcess(const ChildProcessInitializationParameters&)
58 {
59     // Having a window server connection in this process would result in spin logs (<rdar://problem/13239119>).
60     setApplicationIsDaemon();
61 }
62
63 void NetworkProcess::initializeProcessName(const ChildProcessInitializationParameters& parameters)
64 {
65     NSString *applicationName = [NSString stringWithFormat:WEB_UI_STRING("%@ Networking", "visible name of the network process. The argument is the application name."), (NSString *)parameters.uiProcessName];
66     WKSetVisibleApplicationName((CFStringRef)applicationName);
67 }
68
69 static void overrideSystemProxies(const String& httpProxy, const String& httpsProxy)
70 {
71     NSMutableDictionary *proxySettings = [NSMutableDictionary dictionary];
72
73     if (!httpProxy.isNull()) {
74         URL httpProxyURL(URL(), httpProxy);
75         if (httpProxyURL.isValid()) {
76             [proxySettings setObject:nsStringFromWebCoreString(httpProxyURL.host()) forKey:(NSString *)kCFNetworkProxiesHTTPProxy];
77             if (httpProxyURL.hasPort()) {
78                 NSNumber *port = [NSNumber numberWithInt:httpProxyURL.port()];
79                 [proxySettings setObject:port forKey:(NSString *)kCFNetworkProxiesHTTPPort];
80             }
81         }
82         else
83             NSLog(@"Malformed HTTP Proxy URL '%s'.  Expected 'http://<hostname>[:<port>]'\n", httpProxy.utf8().data());
84     }
85
86     if (!httpsProxy.isNull()) {
87         URL httpsProxyURL(URL(), httpsProxy);
88         if (httpsProxyURL.isValid()) {
89             [proxySettings setObject:nsStringFromWebCoreString(httpsProxyURL.host()) forKey:(NSString *)kCFNetworkProxiesHTTPSProxy];
90             if (httpsProxyURL.hasPort()) {
91                 NSNumber *port = [NSNumber numberWithInt:httpsProxyURL.port()];
92                 [proxySettings setObject:port forKey:(NSString *)kCFNetworkProxiesHTTPSPort];
93             }
94         } else
95             NSLog(@"Malformed HTTPS Proxy URL '%s'.  Expected 'https://<hostname>[:<port>]'\n", httpsProxy.utf8().data());
96     }
97
98     if ([proxySettings count] > 0)
99         _CFNetworkSetOverrideSystemProxySettings((CFDictionaryRef)proxySettings);
100 }
101
102 void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreationParameters& parameters)
103 {
104     platformInitializeNetworkProcessCocoa(parameters);
105
106 #if ENABLE(SEC_ITEM_SHIM)
107     SecItemShim::singleton().initialize(this);
108 #endif
109
110     if (!parameters.httpProxy.isNull() || !parameters.httpsProxy.isNull())
111         overrideSystemProxies(parameters.httpProxy, parameters.httpsProxy);
112 }
113
114 void NetworkProcess::allowSpecificHTTPSCertificateForHost(const CertificateInfo& certificateInfo, const String& host)
115 {
116     [NSURLRequest setAllowsSpecificHTTPSCertificate:(NSArray *)certificateInfo.certificateChain() forHost:(NSString *)host];
117 }
118
119 void NetworkProcess::initializeSandbox(const ChildProcessInitializationParameters& parameters, SandboxInitializationParameters& sandboxParameters)
120 {
121     // Need to overide the default, because service has a different bundle ID.
122     NSBundle *webkit2Bundle = [NSBundle bundleForClass:NSClassFromString(@"WKView")];
123     sandboxParameters.setOverrideSandboxProfilePath([webkit2Bundle pathForResource:@"com.apple.WebKit.NetworkProcess" ofType:@"sb"]);
124
125     ChildProcess::initializeSandbox(parameters, sandboxParameters);
126 }
127
128 void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
129 {
130     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
131     if (resourceCachesToClear == InMemoryResourceCachesOnly)
132         return;
133
134     clearDiskCache(std::chrono::system_clock::time_point::min(), [] { });
135 }
136
137 void NetworkProcess::platformTerminate()
138 {
139     if (m_clearCacheDispatchGroup) {
140         dispatch_group_wait(m_clearCacheDispatchGroup, DISPATCH_TIME_FOREVER);
141         dispatch_release(m_clearCacheDispatchGroup);
142         m_clearCacheDispatchGroup = 0;
143     }
144 }
145
146 } // namespace WebKit
147
148 #endif // PLATFORM(MAC)