Resource Load Statistics: Switch NSURLSession on top navigation to prevalent resource...
[WebKit.git] / Source / WebKit / UIProcess / WebsiteData / Cocoa / WebsiteDataStoreCocoa.mm
1 /*
2  * Copyright (C) 2015-2018 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 "WebsiteDataStore.h"
28
29 #import "CookieStorageUtilsCF.h"
30 #import "StorageManager.h"
31 #import "WebResourceLoadStatisticsStore.h"
32 #import "WebsiteDataStoreParameters.h"
33 #import <WebCore/RegistrableDomain.h>
34 #import <WebCore/RuntimeApplicationChecks.h>
35 #import <WebCore/RuntimeEnabledFeatures.h>
36 #import <WebCore/SearchPopupMenuCocoa.h>
37 #import <pal/spi/cf/CFNetworkSPI.h>
38 #import <wtf/FileSystem.h>
39 #import <wtf/NeverDestroyed.h>
40 #import <wtf/ProcessPrivilege.h>
41 #import <wtf/URL.h>
42 #import <wtf/text/StringBuilder.h>
43
44 #if PLATFORM(IOS_FAMILY)
45 #import <UIKit/UIApplication.h>
46 #endif
47
48 namespace WebKit {
49
50 static id terminationObserver;
51
52 static HashSet<WebsiteDataStore*>& dataStores()
53 {
54     static NeverDestroyed<HashSet<WebsiteDataStore*>> dataStores;
55
56     return dataStores;
57 }
58
59 static NSString * const WebKitNetworkLoadThrottleLatencyMillisecondsDefaultsKey = @"WebKitNetworkLoadThrottleLatencyMilliseconds";
60
61 WebsiteDataStoreParameters WebsiteDataStore::parameters()
62 {
63     ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessRawCookies));
64
65     resolveDirectoriesIfNecessary();
66
67     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
68     bool shouldLogCookieInformation = false;
69     bool enableResourceLoadStatisticsDebugMode = false;
70     bool enableResourceLoadStatisticsNSURLSessionSwitching = WebCore::RuntimeEnabledFeatures::sharedFeatures().isITPSessionSwitchingEnabled();
71     WebCore::RegistrableDomain resourceLoadStatisticsManualPrevalentResource { };
72 #if ENABLE(RESOURCE_LOAD_STATISTICS)
73     enableResourceLoadStatisticsDebugMode = [defaults boolForKey:@"ITPDebugMode"];
74     auto* manualPrevalentResource = [defaults stringForKey:@"ITPManualPrevalentResource"];
75     if (manualPrevalentResource) {
76         URL url { URL(), manualPrevalentResource };
77         if (!url.isValid()) {
78             StringBuilder builder;
79             builder.appendLiteral("http://");
80             builder.append(manualPrevalentResource);
81             url = { URL(), builder.toString() };
82         }
83         if (url.isValid())
84             resourceLoadStatisticsManualPrevalentResource = WebCore::RegistrableDomain { url };
85     }
86 #if !RELEASE_LOG_DISABLED
87     static NSString * const WebKitLogCookieInformationDefaultsKey = @"WebKitLogCookieInformation";
88     shouldLogCookieInformation = [defaults boolForKey:WebKitLogCookieInformationDefaultsKey];
89 #endif
90 #endif // ENABLE(RESOURCE_LOAD_STATISTICS)
91
92     URL httpProxy = m_configuration->httpProxy();
93     URL httpsProxy = m_configuration->httpsProxy();
94     
95     bool isSafari = false;
96 #if PLATFORM(IOS_FAMILY)
97     isSafari = WebCore::IOSApplication::isMobileSafari();
98 #elif PLATFORM(MAC)
99     isSafari = WebCore::MacApplication::isSafari();
100 #endif
101     // FIXME: Remove these once Safari adopts _WKWebsiteDataStoreConfiguration.httpProxy and .httpsProxy.
102     if (!httpProxy.isValid() && isSafari)
103         httpProxy = URL(URL(), [defaults stringForKey:(NSString *)WebKit2HTTPProxyDefaultsKey]);
104     if (!httpsProxy.isValid() && isSafari)
105         httpsProxy = URL(URL(), [defaults stringForKey:(NSString *)WebKit2HTTPSProxyDefaultsKey]);
106
107     auto resourceLoadStatisticsDirectory = m_configuration->resourceLoadStatisticsDirectory();
108     SandboxExtension::Handle resourceLoadStatisticsDirectoryHandle;
109     if (!resourceLoadStatisticsDirectory.isEmpty())
110         SandboxExtension::createHandleForReadWriteDirectory(resourceLoadStatisticsDirectory, resourceLoadStatisticsDirectoryHandle);
111
112     auto localStorageDirectory = resolvedLocalStorageDirectory();
113     SandboxExtension::Handle localStorageDirectoryExtensionHandle;
114     if (!localStorageDirectory.isEmpty())
115         SandboxExtension::createHandleForReadWriteDirectory(localStorageDirectory, localStorageDirectoryExtensionHandle);
116
117     auto networkCacheDirectory = resolvedNetworkCacheDirectory();
118     SandboxExtension::Handle networkCacheDirectoryExtensionHandle;
119     if (!networkCacheDirectory.isEmpty())
120         SandboxExtension::createHandleForReadWriteDirectory(networkCacheDirectory, networkCacheDirectoryExtensionHandle);
121
122     bool shouldIncludeLocalhostInResourceLoadStatistics = isSafari;
123     WebsiteDataStoreParameters parameters;
124     parameters.networkSessionParameters = {
125         m_sessionID,
126         m_boundInterfaceIdentifier,
127         m_allowsCellularAccess,
128         m_proxyConfiguration,
129         m_sourceApplicationBundleIdentifier,
130         m_sourceApplicationSecondaryIdentifier,
131         m_allowsTLSFallback ? AllowsTLSFallback::Yes : AllowsTLSFallback::No,
132         shouldLogCookieInformation,
133         Seconds { [defaults integerForKey:WebKitNetworkLoadThrottleLatencyMillisecondsDefaultsKey] / 1000. },
134         WTFMove(httpProxy),
135         WTFMove(httpsProxy),
136         WTFMove(resourceLoadStatisticsDirectory),
137         WTFMove(resourceLoadStatisticsDirectoryHandle),
138         false,
139         false,
140         shouldIncludeLocalhostInResourceLoadStatistics,
141         enableResourceLoadStatisticsDebugMode,
142         enableResourceLoadStatisticsNSURLSessionSwitching,
143         m_configuration->deviceManagementRestrictionsEnabled(),
144         m_configuration->allLoadsBlockedByDeviceManagementRestrictionsForTesting(),
145         WTFMove(resourceLoadStatisticsManualPrevalentResource),
146         WTFMove(localStorageDirectory),
147         WTFMove(localStorageDirectoryExtensionHandle),
148         WTFMove(networkCacheDirectory),
149         WTFMove(networkCacheDirectoryExtensionHandle),
150     };
151     networkingHasBegun();
152
153     auto cookieFile = resolvedCookieStorageFile();
154
155     if (m_uiProcessCookieStorageIdentifier.isEmpty()) {
156         auto utf8File = cookieFile.utf8();
157         auto url = adoptCF(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)utf8File.data(), (CFIndex)utf8File.length(), true));
158         m_cfCookieStorage = adoptCF(CFHTTPCookieStorageCreateFromFile(kCFAllocatorDefault, url.get(), nullptr));
159         m_uiProcessCookieStorageIdentifier = identifyingDataFromCookieStorage(m_cfCookieStorage.get());
160     }
161
162     parameters.uiProcessCookieStorageIdentifier = m_uiProcessCookieStorageIdentifier;
163     parameters.networkSessionParameters.sourceApplicationBundleIdentifier = m_sourceApplicationBundleIdentifier;
164     parameters.networkSessionParameters.sourceApplicationSecondaryIdentifier = m_sourceApplicationSecondaryIdentifier;
165
166     parameters.pendingCookies = copyToVector(m_pendingCookies);
167
168     if (!cookieFile.isEmpty())
169         SandboxExtension::createHandleForReadWriteDirectory(FileSystem::directoryName(cookieFile), parameters.cookieStoragePathExtensionHandle);
170
171 #if ENABLE(INDEXED_DATABASE)
172     parameters.indexedDatabaseDirectory = resolvedIndexedDatabaseDirectory();
173     if (!parameters.indexedDatabaseDirectory.isEmpty())
174         SandboxExtension::createHandleForReadWriteDirectory(parameters.indexedDatabaseDirectory, parameters.indexedDatabaseDirectoryExtensionHandle);
175 #endif
176
177 #if ENABLE(SERVICE_WORKER)
178     parameters.serviceWorkerRegistrationDirectory = resolvedServiceWorkerRegistrationDirectory();
179     if (!parameters.serviceWorkerRegistrationDirectory.isEmpty())
180         SandboxExtension::createHandleForReadWriteDirectory(parameters.serviceWorkerRegistrationDirectory, parameters.serviceWorkerRegistrationDirectoryExtensionHandle);
181 #endif
182
183     parameters.perOriginStorageQuota = perOriginStorageQuota();
184     parameters.perThirdPartyOriginStorageQuota = perThirdPartyOriginStorageQuota();
185
186     return parameters;
187 }
188
189 void WebsiteDataStore::platformInitialize()
190 {
191     if (!terminationObserver) {
192         ASSERT(dataStores().isEmpty());
193
194 #if PLATFORM(MAC)
195         NSString *notificationName = NSApplicationWillTerminateNotification;
196 #else
197         NSString *notificationName = UIApplicationWillTerminateNotification;
198 #endif
199         terminationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:notificationName object:nil queue:nil usingBlock:^(NSNotification *note) {
200             for (auto& dataStore : dataStores()) {
201 #if ENABLE(RESOURCE_LOAD_STATISTICS)
202                 if (dataStore->m_resourceLoadStatistics)
203                     dataStore->m_resourceLoadStatistics->applicationWillTerminate();
204 #endif
205             }
206         }];
207     }
208
209     ASSERT(!dataStores().contains(this));
210     dataStores().add(this);
211 }
212
213 void WebsiteDataStore::platformDestroy()
214 {
215 #if ENABLE(RESOURCE_LOAD_STATISTICS)
216     if (m_resourceLoadStatistics)
217         m_resourceLoadStatistics->applicationWillTerminate();
218 #endif
219
220     ASSERT(dataStores().contains(this));
221     dataStores().remove(this);
222
223     if (dataStores().isEmpty()) {
224         [[NSNotificationCenter defaultCenter] removeObserver:terminationObserver];
225         terminationObserver = nil;
226     }
227 }
228
229 void WebsiteDataStore::platformRemoveRecentSearches(WallTime oldestTimeToRemove)
230 {
231     WebCore::removeRecentlyModifiedRecentSearches(oldestTimeToRemove);
232 }
233
234 }