Make NetworkProcess get cache storage parameters at creation of the CacheStorage...
[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, m_boundInterfaceIdentifier, m_allowsCellularAccess, m_proxyConfiguration };
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     return parameters;
82 }
83
84 void WebsiteDataStore::platformInitialize()
85 {
86     if (!m_storageManager)
87         return;
88
89     if (!terminationObserver) {
90         ASSERT(dataStoresWithStorageManagers().isEmpty());
91
92 #if PLATFORM(MAC)
93         NSString *notificationName = NSApplicationWillTerminateNotification;
94 #else
95         NSString *notificationName = UIApplicationWillTerminateNotification;
96 #endif
97         terminationObserver = [[NSNotificationCenter defaultCenter] addObserverForName:notificationName object:nil queue:nil usingBlock:^(NSNotification *note) {
98             for (auto& dataStore : dataStoresWithStorageManagers()) {
99                 dataStore->m_storageManager->applicationWillTerminate();
100                 if (dataStore->m_resourceLoadStatistics)
101                     dataStore->m_resourceLoadStatistics->applicationWillTerminate();
102             }
103         }];
104     }
105
106     ASSERT(!dataStoresWithStorageManagers().contains(this));
107     dataStoresWithStorageManagers().append(this);
108 }
109
110 void WebsiteDataStore::platformDestroy()
111 {
112     if (m_resourceLoadStatistics)
113         m_resourceLoadStatistics->applicationWillTerminate();
114
115     if (!m_storageManager)
116         return;
117
118     // FIXME: Rename applicationWillTerminate to something that better indicates what StorageManager does (waits for pending writes to finish).
119     m_storageManager->applicationWillTerminate();
120
121     ASSERT(dataStoresWithStorageManagers().contains(this));
122     dataStoresWithStorageManagers().removeFirst(this);
123
124     if (dataStoresWithStorageManagers().isEmpty()) {
125         [[NSNotificationCenter defaultCenter] removeObserver:terminationObserver];
126         terminationObserver = nil;
127     }
128 }
129
130 void WebsiteDataStore::platformRemoveRecentSearches(WallTime oldestTimeToRemove)
131 {
132     WebCore::removeRecentlyModifiedRecentSearches(oldestTimeToRemove);
133 }
134
135 }