Resource Load Statistics: Make it possible exclude localhost from classification
[WebKit-https.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/RuntimeApplicationChecks.h>
34 #import <WebCore/SearchPopupMenuCocoa.h>
35 #import <pal/spi/cf/CFNetworkSPI.h>
36 #import <wtf/FileSystem.h>
37 #import <wtf/NeverDestroyed.h>
38 #import <wtf/ProcessPrivilege.h>
39
40 #if PLATFORM(IOS_FAMILY)
41 #import <UIKit/UIApplication.h>
42 #endif
43
44 namespace WebKit {
45
46 static id terminationObserver;
47
48 static Vector<WebsiteDataStore*>& dataStoresWithStorageManagers()
49 {
50     static NeverDestroyed<Vector<WebsiteDataStore*>> dataStoresWithStorageManagers;
51
52     return dataStoresWithStorageManagers;
53 }
54
55 static NSString * const WebKitNetworkLoadThrottleLatencyMillisecondsDefaultsKey = @"WebKitNetworkLoadThrottleLatencyMilliseconds";
56
57 WebsiteDataStoreParameters WebsiteDataStore::parameters()
58 {
59     ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessRawCookies));
60
61     resolveDirectoriesIfNecessary();
62
63     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
64 #if ENABLE(RESOURCE_LOAD_STATISTICS) && !RELEASE_LOG_DISABLED
65     static NSString * const WebKitLogCookieInformationDefaultsKey = @"WebKitLogCookieInformation";
66     bool shouldLogCookieInformation = [defaults boolForKey:WebKitLogCookieInformationDefaultsKey];
67 #else
68     bool shouldLogCookieInformation = false;
69 #endif
70
71     URL httpProxy = m_configuration->httpProxy();
72     URL httpsProxy = m_configuration->httpsProxy();
73     
74     bool isSafari = false;
75 #if PLATFORM(IOS_FAMILY)
76     isSafari = WebCore::IOSApplication::isMobileSafari();
77 #elif PLATFORM(MAC)
78     isSafari = WebCore::MacApplication::isSafari();
79 #endif
80     // FIXME: Remove these once Safari adopts _WKWebsiteDataStoreConfiguration.httpProxy and .httpsProxy.
81     if (!httpProxy.isValid() && isSafari)
82         httpProxy = URL(URL(), [defaults stringForKey:(NSString *)WebKit2HTTPProxyDefaultsKey]);
83     if (!httpsProxy.isValid() && isSafari)
84         httpsProxy = URL(URL(), [defaults stringForKey:(NSString *)WebKit2HTTPSProxyDefaultsKey]);
85
86     auto resourceLoadStatisticsDirectory = m_configuration->resourceLoadStatisticsDirectory();
87     SandboxExtension::Handle resourceLoadStatisticsDirectoryHandle;
88     if (!resourceLoadStatisticsDirectory.isEmpty())
89         SandboxExtension::createHandleForReadWriteDirectory(resourceLoadStatisticsDirectory, resourceLoadStatisticsDirectoryHandle);
90
91     WebsiteDataStoreParameters parameters;
92     parameters.networkSessionParameters = {
93         m_sessionID,
94         m_boundInterfaceIdentifier,
95         m_allowsCellularAccess,
96         m_proxyConfiguration,
97         m_sourceApplicationBundleIdentifier,
98         m_sourceApplicationSecondaryIdentifier,
99         shouldLogCookieInformation,
100         Seconds { [defaults integerForKey:WebKitNetworkLoadThrottleLatencyMillisecondsDefaultsKey] / 1000. },
101         WTFMove(httpProxy),
102         WTFMove(httpsProxy),
103         WTFMove(resourceLoadStatisticsDirectory),
104         WTFMove(resourceLoadStatisticsDirectoryHandle),
105         false,
106         isSafari
107     };
108     finalizeApplicationIdentifiers();
109
110     auto cookieFile = resolvedCookieStorageFile();
111
112     if (m_uiProcessCookieStorageIdentifier.isEmpty()) {
113         auto utf8File = cookieFile.utf8();
114         auto url = adoptCF(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)utf8File.data(), (CFIndex)utf8File.length(), true));
115         m_cfCookieStorage = adoptCF(CFHTTPCookieStorageCreateFromFile(kCFAllocatorDefault, url.get(), nullptr));
116         m_uiProcessCookieStorageIdentifier = identifyingDataFromCookieStorage(m_cfCookieStorage.get());
117     }
118
119     parameters.uiProcessCookieStorageIdentifier = m_uiProcessCookieStorageIdentifier;
120     parameters.networkSessionParameters.sourceApplicationBundleIdentifier = m_sourceApplicationBundleIdentifier;
121     parameters.networkSessionParameters.sourceApplicationSecondaryIdentifier = m_sourceApplicationSecondaryIdentifier;
122
123     parameters.pendingCookies = copyToVector(m_pendingCookies);
124
125     if (!cookieFile.isEmpty())
126         SandboxExtension::createHandleForReadWriteDirectory(FileSystem::directoryName(cookieFile), parameters.cookieStoragePathExtensionHandle);
127
128 #if ENABLE(INDEXED_DATABASE)
129     parameters.indexedDatabaseDirectory = resolvedIndexedDatabaseDirectory();
130     if (!parameters.indexedDatabaseDirectory.isEmpty())
131         SandboxExtension::createHandleForReadWriteDirectory(parameters.indexedDatabaseDirectory, parameters.indexedDatabaseDirectoryExtensionHandle);
132 #endif
133
134 #if ENABLE(SERVICE_WORKER)
135     parameters.serviceWorkerRegistrationDirectory = resolvedServiceWorkerRegistrationDirectory();
136     if (!parameters.serviceWorkerRegistrationDirectory.isEmpty())
137         SandboxExtension::createHandleForReadWriteDirectory(parameters.serviceWorkerRegistrationDirectory, parameters.serviceWorkerRegistrationDirectoryExtensionHandle);
138 #endif
139
140     return parameters;
141 }
142
143 void WebsiteDataStore::platformInitialize()
144 {
145     if (!m_storageManager)
146         return;
147
148     if (!terminationObserver) {
149         ASSERT(dataStoresWithStorageManagers().isEmpty());
150
151 #if PLATFORM(MAC)
152         NSString *notificationName = NSApplicationWillTerminateNotification;
153 #else
154         NSString *notificationName = UIApplicationWillTerminateNotification;
155 #endif
156         terminationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:notificationName object:nil queue:nil usingBlock:^(NSNotification *note) {
157             for (auto& dataStore : dataStoresWithStorageManagers()) {
158                 dataStore->m_storageManager->applicationWillTerminate();
159 #if ENABLE(RESOURCE_LOAD_STATISTICS)
160                 if (dataStore->m_resourceLoadStatistics)
161                     dataStore->m_resourceLoadStatistics->applicationWillTerminate();
162 #endif
163             }
164         }];
165     }
166
167     ASSERT(!dataStoresWithStorageManagers().contains(this));
168     dataStoresWithStorageManagers().append(this);
169 }
170
171 void WebsiteDataStore::platformDestroy()
172 {
173 #if ENABLE(RESOURCE_LOAD_STATISTICS)
174     if (m_resourceLoadStatistics)
175         m_resourceLoadStatistics->applicationWillTerminate();
176 #endif
177
178     if (!m_storageManager)
179         return;
180
181     // FIXME: Rename applicationWillTerminate to something that better indicates what StorageManager does (waits for pending writes to finish).
182     m_storageManager->applicationWillTerminate();
183
184     ASSERT(dataStoresWithStorageManagers().contains(this));
185     dataStoresWithStorageManagers().removeFirst(this);
186
187     if (dataStoresWithStorageManagers().isEmpty()) {
188         [[NSNotificationCenter defaultCenter] removeObserver:terminationObserver];
189         terminationObserver = nil;
190     }
191 }
192
193 void WebsiteDataStore::platformRemoveRecentSearches(WallTime oldestTimeToRemove)
194 {
195     WebCore::removeRecentlyModifiedRecentSearches(oldestTimeToRemove);
196 }
197
198 }