Unreviewed, rolling out r235954.
[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/FileSystem.h>
34 #import <WebCore/SearchPopupMenuCocoa.h>
35 #import <pal/spi/cf/CFNetworkSPI.h>
36 #import <wtf/NeverDestroyed.h>
37 #import <wtf/ProcessPrivilege.h>
38
39 #if PLATFORM(IOS)
40 #import <UIKit/UIApplication.h>
41 #endif
42
43 namespace WebKit {
44
45 static id terminationObserver;
46
47 static Vector<WebsiteDataStore*>& dataStoresWithStorageManagers()
48 {
49     static NeverDestroyed<Vector<WebsiteDataStore*>> dataStoresWithStorageManagers;
50
51     return dataStoresWithStorageManagers;
52 }
53
54 WebsiteDataStoreParameters WebsiteDataStore::parameters()
55 {
56     ASSERT(hasProcessPrivilege(ProcessPrivilege::CanAccessRawCookies));
57
58     resolveDirectoriesIfNecessary();
59
60     WebsiteDataStoreParameters parameters;
61     parameters.networkSessionParameters = {
62         m_sessionID,
63         m_boundInterfaceIdentifier,
64         m_allowsCellularAccess,
65         m_proxyConfiguration,
66         m_configuration.sourceApplicationBundleIdentifier,
67         m_configuration.sourceApplicationSecondaryIdentifier,
68     };
69
70     auto cookieFile = resolvedCookieStorageFile();
71
72     if (m_uiProcessCookieStorageIdentifier.isEmpty()) {
73         auto utf8File = cookieFile.utf8();
74         auto url = adoptCF(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)utf8File.data(), (CFIndex)utf8File.length(), true));
75         m_cfCookieStorage = adoptCF(CFHTTPCookieStorageCreateFromFile(kCFAllocatorDefault, url.get(), nullptr));
76         m_uiProcessCookieStorageIdentifier = identifyingDataFromCookieStorage(m_cfCookieStorage.get());
77     }
78
79     parameters.uiProcessCookieStorageIdentifier = m_uiProcessCookieStorageIdentifier;
80     parameters.networkSessionParameters.sourceApplicationBundleIdentifier = m_configuration.sourceApplicationBundleIdentifier;
81     parameters.networkSessionParameters.sourceApplicationSecondaryIdentifier = m_configuration.sourceApplicationSecondaryIdentifier;
82
83     parameters.pendingCookies = copyToVector(m_pendingCookies);
84
85     if (!cookieFile.isEmpty())
86         SandboxExtension::createHandleForReadWriteDirectory(WebCore::FileSystem::directoryName(cookieFile), parameters.cookieStoragePathExtensionHandle);
87
88     return parameters;
89 }
90
91 void WebsiteDataStore::platformInitialize()
92 {
93     if (!m_storageManager)
94         return;
95
96     if (!terminationObserver) {
97         ASSERT(dataStoresWithStorageManagers().isEmpty());
98
99 #if PLATFORM(MAC)
100         NSString *notificationName = NSApplicationWillTerminateNotification;
101 #else
102         NSString *notificationName = UIApplicationWillTerminateNotification;
103 #endif
104         terminationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:notificationName object:nil queue:nil usingBlock:^(NSNotification *note) {
105             for (auto& dataStore : dataStoresWithStorageManagers()) {
106                 dataStore->m_storageManager->applicationWillTerminate();
107                 if (dataStore->m_resourceLoadStatistics)
108                     dataStore->m_resourceLoadStatistics->applicationWillTerminate();
109             }
110         }];
111     }
112
113     ASSERT(!dataStoresWithStorageManagers().contains(this));
114     dataStoresWithStorageManagers().append(this);
115 }
116
117 void WebsiteDataStore::platformDestroy()
118 {
119     if (m_resourceLoadStatistics)
120         m_resourceLoadStatistics->applicationWillTerminate();
121
122     if (!m_storageManager)
123         return;
124
125     // FIXME: Rename applicationWillTerminate to something that better indicates what StorageManager does (waits for pending writes to finish).
126     m_storageManager->applicationWillTerminate();
127
128     ASSERT(dataStoresWithStorageManagers().contains(this));
129     dataStoresWithStorageManagers().removeFirst(this);
130
131     if (dataStoresWithStorageManagers().isEmpty()) {
132         [[NSNotificationCenter defaultCenter] removeObserver:terminationObserver];
133         terminationObserver = nil;
134     }
135 }
136
137 void WebsiteDataStore::platformRemoveRecentSearches(WallTime oldestTimeToRemove)
138 {
139     WebCore::removeRecentlyModifiedRecentSearches(oldestTimeToRemove);
140 }
141
142 }