Move IndexedDB to Network Process
[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 #if ENABLE(INDEXED_DATABASE)
89     parameters.indexedDatabaseDirectory = resolvedIndexedDatabaseDirectory();
90     if (!parameters.indexedDatabaseDirectory.isEmpty())
91         SandboxExtension::createHandleForReadWriteDirectory(parameters.indexedDatabaseDirectory, parameters.indexedDatabaseDirectoryExtensionHandle);
92 #endif
93
94     return parameters;
95 }
96
97 void WebsiteDataStore::platformInitialize()
98 {
99     if (!m_storageManager)
100         return;
101
102     if (!terminationObserver) {
103         ASSERT(dataStoresWithStorageManagers().isEmpty());
104
105 #if PLATFORM(MAC)
106         NSString *notificationName = NSApplicationWillTerminateNotification;
107 #else
108         NSString *notificationName = UIApplicationWillTerminateNotification;
109 #endif
110         terminationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:notificationName object:nil queue:nil usingBlock:^(NSNotification *note) {
111             for (auto& dataStore : dataStoresWithStorageManagers()) {
112                 dataStore->m_storageManager->applicationWillTerminate();
113                 if (dataStore->m_resourceLoadStatistics)
114                     dataStore->m_resourceLoadStatistics->applicationWillTerminate();
115             }
116         }];
117     }
118
119     ASSERT(!dataStoresWithStorageManagers().contains(this));
120     dataStoresWithStorageManagers().append(this);
121 }
122
123 void WebsiteDataStore::platformDestroy()
124 {
125     if (m_resourceLoadStatistics)
126         m_resourceLoadStatistics->applicationWillTerminate();
127
128     if (!m_storageManager)
129         return;
130
131     // FIXME: Rename applicationWillTerminate to something that better indicates what StorageManager does (waits for pending writes to finish).
132     m_storageManager->applicationWillTerminate();
133
134     ASSERT(dataStoresWithStorageManagers().contains(this));
135     dataStoresWithStorageManagers().removeFirst(this);
136
137     if (dataStoresWithStorageManagers().isEmpty()) {
138         [[NSNotificationCenter defaultCenter] removeObserver:terminationObserver];
139         terminationObserver = nil;
140     }
141 }
142
143 void WebsiteDataStore::platformRemoveRecentSearches(WallTime oldestTimeToRemove)
144 {
145     WebCore::removeRecentlyModifiedRecentSearches(oldestTimeToRemove);
146 }
147
148 }