Unreviewed, a quick build fix for r232276.
[WebKit-https.git] / Source / WebKit / NetworkProcess / mac / NetworkProcessMac.mm
1 /*
2  * Copyright (C) 2012-2017 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) || ENABLE(MINIMAL_SIMULATOR)
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 "WKFoundation.h"
40 #import <WebCore/CertificateInfo.h>
41 #import <WebCore/FileSystem.h>
42 #import <WebCore/LocalizedStrings.h>
43 #import <notify.h>
44 #import <pal/spi/cf/CFNetworkSPI.h>
45 #import <pal/spi/cocoa/LaunchServicesSPI.h>
46 #import <sysexits.h>
47 #import <wtf/MemoryPressureHandler.h>
48 #import <wtf/text/WTFString.h>
49
50 using namespace WebCore;
51
52 namespace WebKit {
53
54 void NetworkProcess::initializeProcess(const ChildProcessInitializationParameters&)
55 {
56     // Having a window server connection in this process would result in spin logs (<rdar://problem/13239119>).
57     setApplicationIsDaemon();
58 }
59
60 void NetworkProcess::initializeProcessName(const ChildProcessInitializationParameters& parameters)
61 {
62 #if !ENABLE(MINIMAL_SIMULATOR)
63     NSString *applicationName = [NSString stringWithFormat:WEB_UI_STRING("%@ Networking", "visible name of the network process. The argument is the application name."), (NSString *)parameters.uiProcessName];
64     _LSSetApplicationInformationItem(kLSDefaultSessionID, _LSGetCurrentApplicationASN(), _kLSDisplayNameKey, (CFStringRef)applicationName, nullptr);
65 #endif
66 }
67
68 static void overrideSystemProxies(const String& httpProxy, const String& httpsProxy)
69 {
70     NSMutableDictionary *proxySettings = [NSMutableDictionary dictionary];
71
72     if (!httpProxy.isNull()) {
73         URL httpProxyURL(URL(), httpProxy);
74         if (httpProxyURL.isValid()) {
75             [proxySettings setObject:nsStringFromWebCoreString(httpProxyURL.host().toString()) forKey:(NSString *)kCFNetworkProxiesHTTPProxy];
76             if (httpProxyURL.port()) {
77                 NSNumber *port = [NSNumber numberWithInt:httpProxyURL.port().value()];
78                 [proxySettings setObject:port forKey:(NSString *)kCFNetworkProxiesHTTPPort];
79             }
80         }
81         else
82             NSLog(@"Malformed HTTP Proxy URL '%s'.  Expected 'http://<hostname>[:<port>]'\n", httpProxy.utf8().data());
83     }
84
85     if (!httpsProxy.isNull()) {
86         URL httpsProxyURL(URL(), httpsProxy);
87         if (httpsProxyURL.isValid()) {
88 #if !ENABLE(MINIMAL_SIMULATOR)
89             [proxySettings setObject:nsStringFromWebCoreString(httpsProxyURL.host().toString()) forKey:(NSString *)kCFNetworkProxiesHTTPSProxy];
90             if (httpsProxyURL.port()) {
91                 NSNumber *port = [NSNumber numberWithInt:httpsProxyURL.port().value()];
92                 [proxySettings setObject:port forKey:(NSString *)kCFNetworkProxiesHTTPSPort];
93             }
94 #endif
95         } else
96             NSLog(@"Malformed HTTPS Proxy URL '%s'.  Expected 'https://<hostname>[:<port>]'\n", httpsProxy.utf8().data());
97     }
98
99     if ([proxySettings count] > 0)
100         _CFNetworkSetOverrideSystemProxySettings((CFDictionaryRef)proxySettings);
101 }
102
103 void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreationParameters& parameters)
104 {
105     platformInitializeNetworkProcessCocoa(parameters);
106
107 #if ENABLE(SEC_ITEM_SHIM)
108     // SecItemShim is needed for CFNetwork APIs that query Keychains beneath us.
109     initializeSecItemShim(*this);
110 #endif
111
112     if (!parameters.httpProxy.isNull() || !parameters.httpsProxy.isNull())
113         overrideSystemProxies(parameters.httpProxy, parameters.httpsProxy);
114 }
115
116 void NetworkProcess::allowSpecificHTTPSCertificateForHost(const CertificateInfo& certificateInfo, const String& host)
117 {
118     [NSURLRequest setAllowsSpecificHTTPSCertificate:(NSArray *)certificateInfo.certificateChain() forHost:(NSString *)host];
119 }
120
121 void NetworkProcess::initializeSandbox(const ChildProcessInitializationParameters& parameters, SandboxInitializationParameters& sandboxParameters)
122 {
123     // Need to overide the default, because service has a different bundle ID.
124 #if WK_API_ENABLED
125     NSBundle *webKit2Bundle = [NSBundle bundleForClass:NSClassFromString(@"WKWebView")];
126 #else
127     NSBundle *webKit2Bundle = [NSBundle bundleForClass:NSClassFromString(@"WKView")];
128 #endif
129
130     sandboxParameters.setOverrideSandboxProfilePath([webKit2Bundle pathForResource:@"com.apple.WebKit.NetworkProcess" ofType:@"sb"]);
131
132     ChildProcess::initializeSandbox(parameters, sandboxParameters);
133 }
134
135 void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
136 {
137     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
138     if (resourceCachesToClear == InMemoryResourceCachesOnly)
139         return;
140
141     clearDiskCache(-WallTime::infinity(), [] { });
142 }
143
144 void NetworkProcess::platformTerminate()
145 {
146     if (m_clearCacheDispatchGroup) {
147         dispatch_group_wait(m_clearCacheDispatchGroup, DISPATCH_TIME_FOREVER);
148         dispatch_release(m_clearCacheDispatchGroup);
149         m_clearCacheDispatchGroup = 0;
150     }
151 }
152
153 } // namespace WebKit
154
155 #endif // PLATFORM(MAC)