daf89edbdc8c169b6cb23bf03a13f074323071f7
[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 = { m_sessionID, nullptr, m_boundInterfaceIdentifier, m_allowsCellularAccess };
62
63     auto cookieFile = resolvedCookieStorageFile();
64
65 #if PLATFORM(COCOA)
66     if (m_uiProcessCookieStorageIdentifier.isEmpty()) {
67         auto utf8File = cookieFile.utf8();
68         auto url = adoptCF(CFURLCreateFromFileSystemRepresentation(kCFAllocatorDefault, (const UInt8 *)utf8File.data(), (CFIndex)utf8File.length(), true));
69         m_cfCookieStorage = adoptCF(CFHTTPCookieStorageCreateFromFile(kCFAllocatorDefault, url.get(), nullptr));
70         m_uiProcessCookieStorageIdentifier = identifyingDataFromCookieStorage(m_cfCookieStorage.get());
71     }
72
73     parameters.uiProcessCookieStorageIdentifier = m_uiProcessCookieStorageIdentifier;
74 #endif
75
76     parameters.pendingCookies = copyToVector(m_pendingCookies);
77
78     if (!cookieFile.isEmpty())
79         SandboxExtension::createHandleForReadWriteDirectory(WebCore::FileSystem::directoryName(cookieFile), parameters.cookieStoragePathExtensionHandle);
80
81     if (!m_configuration.cacheStorageDirectory.isNull()) {
82         parameters.cacheStorageDirectory = m_configuration.cacheStorageDirectory;
83         SandboxExtension::createHandleForReadWriteDirectory(parameters.cacheStorageDirectory, parameters.cacheStorageDirectoryExtensionHandle);
84     }
85
86     return parameters;
87 }
88
89 void WebsiteDataStore::platformInitialize()
90 {
91     if (!m_storageManager)
92         return;
93
94     if (!terminationObserver) {
95         ASSERT(dataStoresWithStorageManagers().isEmpty());
96
97 #if PLATFORM(MAC)
98         NSString *notificationName = NSApplicationWillTerminateNotification;
99 #else
100         NSString *notificationName = UIApplicationWillTerminateNotification;
101 #endif
102         terminationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:notificationName object:nil queue:nil usingBlock:^(NSNotification *note) {
103             for (auto& dataStore : dataStoresWithStorageManagers()) {
104                 dataStore->m_storageManager->applicationWillTerminate();
105                 if (dataStore->m_resourceLoadStatistics)
106                     dataStore->m_resourceLoadStatistics->applicationWillTerminate();
107             }
108         }];
109     }
110
111     ASSERT(!dataStoresWithStorageManagers().contains(this));
112     dataStoresWithStorageManagers().append(this);
113 }
114
115 void WebsiteDataStore::platformDestroy()
116 {
117     if (m_resourceLoadStatistics)
118         m_resourceLoadStatistics->applicationWillTerminate();
119
120     if (!m_storageManager)
121         return;
122
123     // FIXME: Rename applicationWillTerminate to something that better indicates what StorageManager does (waits for pending writes to finish).
124     m_storageManager->applicationWillTerminate();
125
126     ASSERT(dataStoresWithStorageManagers().contains(this));
127     dataStoresWithStorageManagers().removeFirst(this);
128
129     if (dataStoresWithStorageManagers().isEmpty()) {
130         [[NSNotificationCenter defaultCenter] removeObserver:terminationObserver];
131         terminationObserver = nil;
132     }
133 }
134
135 void WebsiteDataStore::platformRemoveRecentSearches(WallTime oldestTimeToRemove)
136 {
137     WebCore::removeRecentlyModifiedRecentSearches(oldestTimeToRemove);
138 }
139
140 }