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