Move NetworkCache ownership from NetworkProcess to NetworkSession
authorachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 Jul 2019 18:41:31 +0000 (18:41 +0000)
committerachristensen@apple.com <achristensen@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 18 Jul 2019 18:41:31 +0000 (18:41 +0000)
https://bugs.webkit.org/show_bug.cgi?id=199817

Reviewed by Geoff Garen.

Source/WebKit:

I also added SPI to set the path of the NetworkCache with an API test.
If this new SPI is not used, it falls back to getting the cache location from the NetworkProcess to maintain compatibility.
We still get options, cache size, and cache model from the NetworkProcess.  Those should be migrated with new SPI later.
Clearing the cache and other operations now iterate the NetworkSessions instead of assuming there is only one cache.

* NetworkProcess/NetworkProcess.cpp:
(WebKit::NetworkProcess::fetchWebsiteData):
(WebKit::NetworkProcess::deleteWebsiteDataForOrigins):
(WebKit::NetworkProcess::deleteWebsiteDataForRegistrableDomains):
(WebKit::NetworkProcess::registrableDomainsWithWebsiteData):
(WebKit::NetworkProcess::setCacheModel):
* NetworkProcess/NetworkProcess.h:
(WebKit::NetworkProcess::diskCacheDirectory const):
(WebKit::NetworkProcess::cacheOptions const):
(WebKit::NetworkProcess::cache): Deleted.
* NetworkProcess/NetworkResourceLoader.cpp:
(WebKit::m_shouldCaptureExtraNetworkLoadMetrics):
* NetworkProcess/NetworkSession.cpp:
(WebKit::NetworkSession::NetworkSession):
* NetworkProcess/NetworkSession.h:
(WebKit::NetworkSession::cache):
* NetworkProcess/NetworkSessionCreationParameters.cpp:
(WebKit::NetworkSessionCreationParameters::encode const):
(WebKit::NetworkSessionCreationParameters::decode):
* NetworkProcess/NetworkSessionCreationParameters.h:
* NetworkProcess/cache/NetworkCache.cpp:
(WebKit::NetworkCache::Cache::open):
(WebKit::NetworkCache::Cache::Cache):
* NetworkProcess/cache/NetworkCache.h:
* NetworkProcess/cocoa/NetworkProcessCocoa.mm:
(WebKit::NetworkProcess::platformInitializeNetworkProcessCocoa):
(WebKit::NetworkProcess::clearDiskCache):
* NetworkProcess/cocoa/NetworkSessionCocoa.mm:
(WebKit::NetworkSessionCocoa::NetworkSessionCocoa):
* NetworkProcess/curl/NetworkSessionCurl.cpp:
(WebKit::NetworkSessionCurl::NetworkSessionCurl):
* NetworkProcess/soup/NetworkSessionSoup.cpp:
(WebKit::NetworkSessionSoup::NetworkSessionSoup):
* UIProcess/API/Cocoa/WKWebsiteDataStore.mm:
(-[WKWebsiteDataStore _initWithConfiguration:]):
* UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.h:
* UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.mm:
(-[_WKWebsiteDataStoreConfiguration networkCacheDirectory]):
(-[_WKWebsiteDataStoreConfiguration setNetworkCacheDirectory:]):
* UIProcess/WebsiteData/Cocoa/WebsiteDataStoreCocoa.mm:
(WebKit::WebsiteDataStore::parameters):
* UIProcess/WebsiteData/WebsiteDataStore.cpp:
(WebKit::WebsiteDataStore::resolveDirectoriesIfNecessary):
* UIProcess/WebsiteData/WebsiteDataStore.h:
(WebKit::WebsiteDataStore::resolvedNetworkCacheDirectory const):

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm:
(TEST):
* TestWebKitAPI/cocoa/TestWKWebView.h:
* TestWebKitAPI/cocoa/TestWKWebView.mm:
(-[WKWebView synchronouslyLoadRequest:]):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247567 268f45cc-cd09-0410-ab3c-d52691b4dbfc

26 files changed:
Source/WebKit/ChangeLog
Source/WebKit/NetworkProcess/NetworkProcess.cpp
Source/WebKit/NetworkProcess/NetworkProcess.h
Source/WebKit/NetworkProcess/NetworkResourceLoader.cpp
Source/WebKit/NetworkProcess/NetworkSession.cpp
Source/WebKit/NetworkProcess/NetworkSession.h
Source/WebKit/NetworkProcess/NetworkSessionCreationParameters.cpp
Source/WebKit/NetworkProcess/NetworkSessionCreationParameters.h
Source/WebKit/NetworkProcess/cache/NetworkCache.cpp
Source/WebKit/NetworkProcess/cache/NetworkCache.h
Source/WebKit/NetworkProcess/cocoa/NetworkProcessCocoa.mm
Source/WebKit/NetworkProcess/cocoa/NetworkSessionCocoa.mm
Source/WebKit/NetworkProcess/curl/NetworkSessionCurl.cpp
Source/WebKit/NetworkProcess/ios/NetworkProcessIOS.mm
Source/WebKit/NetworkProcess/soup/NetworkProcessSoup.cpp
Source/WebKit/NetworkProcess/soup/NetworkSessionSoup.cpp
Source/WebKit/UIProcess/API/Cocoa/WKWebsiteDataStore.mm
Source/WebKit/UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.h
Source/WebKit/UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.mm
Source/WebKit/UIProcess/WebsiteData/Cocoa/WebsiteDataStoreCocoa.mm
Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.cpp
Source/WebKit/UIProcess/WebsiteData/WebsiteDataStore.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm
Tools/TestWebKitAPI/cocoa/TestWKWebView.h
Tools/TestWebKitAPI/cocoa/TestWKWebView.mm

index 51b75f6..314f6dc 100644 (file)
@@ -1,3 +1,61 @@
+2019-07-18  Alex Christensen  <achristensen@webkit.org>
+
+        Move NetworkCache ownership from NetworkProcess to NetworkSession
+        https://bugs.webkit.org/show_bug.cgi?id=199817
+
+        Reviewed by Geoff Garen.
+
+        I also added SPI to set the path of the NetworkCache with an API test.
+        If this new SPI is not used, it falls back to getting the cache location from the NetworkProcess to maintain compatibility.
+        We still get options, cache size, and cache model from the NetworkProcess.  Those should be migrated with new SPI later.
+        Clearing the cache and other operations now iterate the NetworkSessions instead of assuming there is only one cache.
+
+        * NetworkProcess/NetworkProcess.cpp:
+        (WebKit::NetworkProcess::fetchWebsiteData):
+        (WebKit::NetworkProcess::deleteWebsiteDataForOrigins):
+        (WebKit::NetworkProcess::deleteWebsiteDataForRegistrableDomains):
+        (WebKit::NetworkProcess::registrableDomainsWithWebsiteData):
+        (WebKit::NetworkProcess::setCacheModel):
+        * NetworkProcess/NetworkProcess.h:
+        (WebKit::NetworkProcess::diskCacheDirectory const):
+        (WebKit::NetworkProcess::cacheOptions const):
+        (WebKit::NetworkProcess::cache): Deleted.
+        * NetworkProcess/NetworkResourceLoader.cpp:
+        (WebKit::m_shouldCaptureExtraNetworkLoadMetrics):
+        * NetworkProcess/NetworkSession.cpp:
+        (WebKit::NetworkSession::NetworkSession):
+        * NetworkProcess/NetworkSession.h:
+        (WebKit::NetworkSession::cache):
+        * NetworkProcess/NetworkSessionCreationParameters.cpp:
+        (WebKit::NetworkSessionCreationParameters::encode const):
+        (WebKit::NetworkSessionCreationParameters::decode):
+        * NetworkProcess/NetworkSessionCreationParameters.h:
+        * NetworkProcess/cache/NetworkCache.cpp:
+        (WebKit::NetworkCache::Cache::open):
+        (WebKit::NetworkCache::Cache::Cache):
+        * NetworkProcess/cache/NetworkCache.h:
+        * NetworkProcess/cocoa/NetworkProcessCocoa.mm:
+        (WebKit::NetworkProcess::platformInitializeNetworkProcessCocoa):
+        (WebKit::NetworkProcess::clearDiskCache):
+        * NetworkProcess/cocoa/NetworkSessionCocoa.mm:
+        (WebKit::NetworkSessionCocoa::NetworkSessionCocoa):
+        * NetworkProcess/curl/NetworkSessionCurl.cpp:
+        (WebKit::NetworkSessionCurl::NetworkSessionCurl):
+        * NetworkProcess/soup/NetworkSessionSoup.cpp:
+        (WebKit::NetworkSessionSoup::NetworkSessionSoup):
+        * UIProcess/API/Cocoa/WKWebsiteDataStore.mm:
+        (-[WKWebsiteDataStore _initWithConfiguration:]):
+        * UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.h:
+        * UIProcess/API/Cocoa/_WKWebsiteDataStoreConfiguration.mm:
+        (-[_WKWebsiteDataStoreConfiguration networkCacheDirectory]):
+        (-[_WKWebsiteDataStoreConfiguration setNetworkCacheDirectory:]):
+        * UIProcess/WebsiteData/Cocoa/WebsiteDataStoreCocoa.mm:
+        (WebKit::WebsiteDataStore::parameters):
+        * UIProcess/WebsiteData/WebsiteDataStore.cpp:
+        (WebKit::WebsiteDataStore::resolveDirectoriesIfNecessary):
+        * UIProcess/WebsiteData/WebsiteDataStore.h:
+        (WebKit::WebsiteDataStore::resolvedNetworkCacheDirectory const):
+
 2019-07-18  Chris Dumez  <cdumez@apple.com>
 
         Regression(r247486) Multiple Layout Tests in http/tests/cache/* are crashing on iOS Debug WK2
index ef9e0c9..24bd716 100644 (file)
@@ -1357,11 +1357,12 @@ void NetworkProcess::fetchWebsiteData(PAL::SessionID sessionID, OptionSet<Websit
         });
     }
 #endif
-
     if (websiteDataTypes.contains(WebsiteDataType::DiskCache)) {
-        fetchDiskCacheEntries(cache(), sessionID, fetchOptions, [callbackAggregator = WTFMove(callbackAggregator)](auto entries) mutable {
-            callbackAggregator->m_websiteData.entries.appendVector(entries);
-        });
+        for (auto& session : networkSessions().values()) {
+            fetchDiskCacheEntries(session->cache(), sessionID, fetchOptions, [callbackAggregator = WTFMove(callbackAggregator)](auto entries) mutable {
+                callbackAggregator->m_websiteData.entries.appendVector(entries);
+            });
+        }
     }
 }
 
@@ -1516,8 +1517,10 @@ void NetworkProcess::deleteWebsiteDataForOrigins(PAL::SessionID sessionID, Optio
     }
 #endif
 
-    if (websiteDataTypes.contains(WebsiteDataType::DiskCache) && !sessionID.isEphemeral())
-        clearDiskCacheEntries(cache(), originDatas, [clearTasksHandler = WTFMove(clearTasksHandler)] { });
+    if (websiteDataTypes.contains(WebsiteDataType::DiskCache) && !sessionID.isEphemeral()) {
+        for (auto& session : networkSessions().values())
+            clearDiskCacheEntries(session->cache(), originDatas, [clearTasksHandler = WTFMove(clearTasksHandler)] { });
+    }
 
     if (websiteDataTypes.contains(WebsiteDataType::Credentials)) {
         if (auto* session = storageSession(sessionID)) {
@@ -1748,19 +1751,21 @@ void NetworkProcess::deleteWebsiteDataForRegistrableDomains(PAL::SessionID sessi
         });
     }
 #endif
-    
+
     if (websiteDataTypes.contains(WebsiteDataType::DiskCache)) {
-        fetchDiskCacheEntries(cache(), sessionID, fetchOptions, [this, domainsToDeleteAllButCookiesFor, callbackAggregator = callbackAggregator.copyRef()](auto entries) mutable {
+        for (auto& session : networkSessions().values()) {
+            fetchDiskCacheEntries(session->cache(), sessionID, fetchOptions, [domainsToDeleteAllButCookiesFor, callbackAggregator = callbackAggregator.copyRef(), session = session.copyRef()](auto entries) mutable {
 
-            Vector<SecurityOriginData> entriesToDelete;
-            for (auto& entry : entries) {
-                if (!domainsToDeleteAllButCookiesFor.contains(RegistrableDomain::uncheckedCreateFromHost(entry.origin.host)))
-                    continue;
-                entriesToDelete.append(entry.origin);
-                callbackAggregator->m_domains.add(RegistrableDomain::uncheckedCreateFromHost(entry.origin.host));
-            }
-            clearDiskCacheEntries(cache(), entriesToDelete, [callbackAggregator = callbackAggregator.copyRef()] { });
-        });
+                Vector<SecurityOriginData> entriesToDelete;
+                for (auto& entry : entries) {
+                    if (!domainsToDeleteAllButCookiesFor.contains(RegistrableDomain::uncheckedCreateFromHost(entry.origin.host)))
+                        continue;
+                    entriesToDelete.append(entry.origin);
+                    callbackAggregator->m_domains.add(RegistrableDomain::uncheckedCreateFromHost(entry.origin.host));
+                }
+                clearDiskCacheEntries(session->cache(), entriesToDelete, [callbackAggregator = callbackAggregator.copyRef()] { });
+            });
+        }
     }
 
     auto dataTypesForUIProcess = WebsiteData::filter(websiteDataTypes, WebsiteDataProcessType::UI);
@@ -1882,9 +1887,11 @@ void NetworkProcess::registrableDomainsWithWebsiteData(PAL::SessionID sessionID,
 #endif
     
     if (websiteDataTypes.contains(WebsiteDataType::DiskCache)) {
-        fetchDiskCacheEntries(cache(), sessionID, fetchOptions, [callbackAggregator = callbackAggregator.copyRef()](auto entries) mutable {
-            callbackAggregator->m_websiteData.entries.appendVector(entries);
-        });
+        for (auto& session : networkSessions().values()) {
+            fetchDiskCacheEntries(session->cache(), sessionID, fetchOptions, [callbackAggregator = callbackAggregator.copyRef()](auto entries) mutable {
+                callbackAggregator->m_websiteData.entries.appendVector(entries);
+            });
+        }
     }
 }
 #endif // ENABLE(RESOURCE_LOAD_STATISTICS)
@@ -1976,9 +1983,11 @@ void NetworkProcess::setCacheModel(CacheModel cacheModel)
         diskFreeSize /= KB * 1000;
         calculateURLCacheSizes(cacheModel, diskFreeSize, urlCacheMemoryCapacity, urlCacheDiskCapacity);
     }
-
-    if (m_cache)
-        m_cache->setCapacity(urlCacheDiskCapacity);
+    
+    for (auto& session : networkSessions().values()) {
+        if (auto* cache = session->cache())
+            cache->setCapacity(urlCacheDiskCapacity);
+    }
 }
 
 void NetworkProcess::setCanHandleHTTPSServerTrustEvaluation(bool value)
index db86f24..69f3d4f 100644 (file)
@@ -111,12 +111,12 @@ struct WebsiteDataStoreParameters;
 class WebSWOriginStore;
 #endif
 
-namespace CacheStorage {
-class Engine;
+namespace NetworkCache {
+enum class CacheOption : uint8_t;
 }
 
-namespace NetworkCache {
-class Cache;
+namespace CacheStorage {
+class Engine;
 }
 
 class NetworkProcess : public AuxiliaryProcess, private DownloadManager::Client, public ThreadSafeRefCounted<NetworkProcess>
@@ -161,8 +161,6 @@ public:
     AuthenticationManager& authenticationManager();
     DownloadManager& downloadManager();
 
-    NetworkCache::Cache* cache() { return m_cache.get(); }
-
     void setSession(const PAL::SessionID&, Ref<NetworkSession>&&);
     NetworkSession* networkSession(const PAL::SessionID&) const final;
     NetworkSession* networkSessionByConnection(IPC::Connection&) const;
@@ -343,6 +341,9 @@ public:
     void addKeptAliveLoad(Ref<NetworkResourceLoader>&&);
     void removeKeptAliveLoad(NetworkResourceLoader&);
 
+    const String& diskCacheDirectory() const { return m_diskCacheDirectory; }
+    const OptionSet<NetworkCache::CacheOption>& cacheOptions() const { return m_cacheOptions; }
+    
 private:
     void platformInitializeNetworkProcess(const NetworkProcessCreationParameters&);
     std::unique_ptr<WebCore::NetworkStorageSession> platformCreateDefaultStorageSession() const;
@@ -489,8 +490,6 @@ private:
 
     HashMap<PAL::SessionID, Ref<CacheStorage::Engine>> m_cacheEngines;
 
-    RefPtr<NetworkCache::Cache> m_cache;
-
     typedef HashMap<const char*, std::unique_ptr<NetworkProcessSupplement>, PtrHash<const char*>> NetworkProcessSupplementMap;
     NetworkProcessSupplementMap m_supplements;
 
@@ -566,6 +565,8 @@ private:
     uint32_t m_downloadMonitorSpeedMultiplier { 1 };
 
     HashMap<IPC::Connection::UniqueID, PAL::SessionID> m_sessionByConnection;
+
+    OptionSet<NetworkCache::CacheOption> m_cacheOptions;
 };
 
 } // namespace WebKit
index ea51d53..5ca46df 100644 (file)
@@ -98,10 +98,13 @@ NetworkResourceLoader::NetworkResourceLoader(NetworkResourceLoadParameters&& par
     , m_fileReferences(connection.resolveBlobReferences(m_parameters))
     , m_isAllowedToAskUserForCredentials { m_parameters.clientCredentialPolicy == ClientCredentialPolicy::MayAskClientForCredentials }
     , m_bufferingTimer { *this, &NetworkResourceLoader::bufferingTimerFired }
-    , m_cache { sessionID().isEphemeral() ? nullptr : connection.networkProcess().cache() }
     , m_shouldCaptureExtraNetworkLoadMetrics(m_connection->captureExtraNetworkLoadMetricsEnabled())
 {
     ASSERT(RunLoop::isMain());
+
+    if (auto* session = connection.networkProcess().networkSession(sessionID()))
+        m_cache = session->cache();
+
     // FIXME: This is necessary because of the existence of EmptyFrameLoaderClient in WebCore.
     //        Once bug 116233 is resolved, this ASSERT can just be "m_webPageID && m_webFrameID"
     ASSERT((m_parameters.webPageID && m_parameters.webFrameID) || m_parameters.clientCredentialPolicy == ClientCredentialPolicy::CannotAskClientForCredentials);
index c7faf04..2a1162d 100644 (file)
@@ -27,6 +27,7 @@
 #include "NetworkSession.h"
 
 #include "AdClickAttributionManager.h"
+#include "Logging.h"
 #include "NetworkProcess.h"
 #include "NetworkProcessProxyMessages.h"
 #include "NetworkResourceLoadParameters.h"
@@ -77,13 +78,25 @@ NetworkStorageSession* NetworkSession::networkStorageSession() const
     return storageSession;
 }
 
-NetworkSession::NetworkSession(NetworkProcess& networkProcess, PAL::SessionID sessionID, String&& localStorageDirectory, SandboxExtension::Handle& handle)
-    : m_sessionID(sessionID)
+NetworkSession::NetworkSession(NetworkProcess& networkProcess, const NetworkSessionCreationParameters& parameters)
+    : m_sessionID(parameters.sessionID)
     , m_networkProcess(networkProcess)
-    , m_adClickAttribution(makeUniqueRef<AdClickAttributionManager>(sessionID))
-    , m_storageManager(StorageManager::create(WTFMove(localStorageDirectory)))
-{
-    SandboxExtension::consumePermanently(handle);
+    , m_adClickAttribution(makeUniqueRef<AdClickAttributionManager>(parameters.sessionID))
+    , m_storageManager(StorageManager::create(String(parameters.localStorageDirectory)))
+{
+    if (!m_sessionID.isEphemeral()) {
+        String networkCacheDirectory = parameters.networkCacheDirectory;
+        if (networkCacheDirectory.isNull())
+            networkCacheDirectory = networkProcess.diskCacheDirectory();
+        else
+            SandboxExtension::consumePermanently(parameters.networkCacheDirectoryExtensionHandle);
+
+        m_cache = NetworkCache::Cache::open(networkProcess, networkCacheDirectory, networkProcess.cacheOptions());
+        if (!m_cache)
+            RELEASE_LOG_ERROR(NetworkCache, "Failed to initialize the WebKit network disk cache");
+    }
+
+    SandboxExtension::consumePermanently(parameters.localStorageDirectoryExtensionHandle);
     m_adClickAttribution->setPingLoadFunction([this, weakThis = makeWeakPtr(this)](NetworkResourceLoadParameters&& loadParameters, CompletionHandler<void(const WebCore::ResourceError&, const WebCore::ResourceResponse&)>&& completionHandler) {
         if (!weakThis)
             return;
index 4d48732..b3e412e 100644 (file)
@@ -59,7 +59,11 @@ class WebSocketTask;
 struct NetworkSessionCreationParameters;
 
 enum class WebsiteDataType;
-    
+
+namespace NetworkCache {
+class Cache;
+}
+
 class NetworkSession : public RefCounted<NetworkSession>, public CanMakeWeakPtr<NetworkSession> {
 public:
     static Ref<NetworkSession> create(NetworkProcess&, NetworkSessionCreationParameters&&);
@@ -100,6 +104,8 @@ public:
     void addKeptAliveLoad(Ref<NetworkResourceLoader>&&);
     void removeKeptAliveLoad(NetworkResourceLoader&);
 
+    NetworkCache::Cache* cache() { return m_cache.get(); }
+
     PrefetchCache& prefetchCache() { return m_prefetchCache; }
     void clearPrefetchCache() { m_prefetchCache.clear(); }
 
@@ -108,7 +114,7 @@ public:
     virtual void addWebSocketTask(WebSocketTask&) { }
 
 protected:
-    NetworkSession(NetworkProcess&, PAL::SessionID, String&& localStorageDirectory, SandboxExtension::Handle&);
+    NetworkSession(NetworkProcess&, const NetworkSessionCreationParameters&);
 
     PAL::SessionID m_sessionID;
     Ref<NetworkProcess> m_networkProcess;
@@ -130,6 +136,7 @@ protected:
 #if !ASSERT_DISABLED
     bool m_isInvalidated { false };
 #endif
+    RefPtr<NetworkCache::Cache> m_cache;
 };
 
 } // namespace WebKit
index 90deda4..4efa939 100644 (file)
@@ -50,7 +50,7 @@ NetworkSessionCreationParameters NetworkSessionCreationParameters::privateSessio
 #if USE(CURL)
         , { }, { }
 #endif
-        , { }, { }, false, { }, { }, { }, { }, { }, { }, { }
+        , { }, { }, false, { }, { }, { }, { }, { }, { }, { }, { }, { }
     };
 }
 
@@ -85,6 +85,7 @@ void NetworkSessionCreationParameters::encode(IPC::Encoder& encoder) const
     encoder << resourceLoadStatisticsManualPrevalentResource;
 
     encoder << localStorageDirectory << localStorageDirectoryExtensionHandle;
+    encoder << networkCacheDirectory << networkCacheDirectoryExtensionHandle;
 
     encoder << deviceManagementRestrictionsEnabled;
     encoder << allLoadsBlockedByDeviceManagementRestrictionsForTesting;
@@ -211,6 +212,16 @@ Optional<NetworkSessionCreationParameters> NetworkSessionCreationParameters::dec
     if (!localStorageDirectoryExtensionHandle)
         return WTF::nullopt;
 
+    Optional<String> networkCacheDirectory;
+    decoder >> networkCacheDirectory;
+    if (!networkCacheDirectory)
+        return WTF::nullopt;
+    
+    Optional<SandboxExtension::Handle> networkCacheDirectoryExtensionHandle;
+    decoder >> networkCacheDirectoryExtensionHandle;
+    if (!networkCacheDirectoryExtensionHandle)
+        return WTF::nullopt;
+
     Optional<bool> deviceManagementRestrictionsEnabled;
     decoder >> deviceManagementRestrictionsEnabled;
     if (!deviceManagementRestrictionsEnabled)
@@ -253,6 +264,8 @@ Optional<NetworkSessionCreationParameters> NetworkSessionCreationParameters::dec
         , WTFMove(*resourceLoadStatisticsManualPrevalentResource)
         , WTFMove(*localStorageDirectory)
         , WTFMove(*localStorageDirectoryExtensionHandle)
+        , WTFMove(*networkCacheDirectory)
+        , WTFMove(*networkCacheDirectoryExtensionHandle)
     }};
 }
 
index 6559947..0582ea4 100644 (file)
@@ -92,6 +92,8 @@ struct NetworkSessionCreationParameters {
 
     String localStorageDirectory;
     SandboxExtension::Handle localStorageDirectoryExtensionHandle;
+    String networkCacheDirectory;
+    SandboxExtension::Handle networkCacheDirectoryExtensionHandle;
 };
 
 } // namespace WebKit
index 20e0758..5477114 100644 (file)
@@ -59,9 +59,9 @@ static const AtomString& resourceType()
     return resource;
 }
 
-RefPtr<Cache> Cache::open(NetworkProcess& networkProcess, const String& cachePath, OptionSet<Option> options)
+RefPtr<Cache> Cache::open(NetworkProcess& networkProcess, const String& cachePath, OptionSet<CacheOption> options)
 {
-    auto storage = Storage::open(cachePath, options.contains(Option::TestingMode) ? Storage::Mode::AvoidRandomness : Storage::Mode::Normal);
+    auto storage = Storage::open(cachePath, options.contains(CacheOption::TestingMode) ? Storage::Mode::AvoidRandomness : Storage::Mode::Normal);
 
     LOG(NetworkCache, "(NetworkProcess) opened cache storage, success %d", !!storage);
 
@@ -78,12 +78,12 @@ static void dumpFileChanged(Cache* cache)
 }
 #endif
 
-Cache::Cache(NetworkProcess& networkProcess, Ref<Storage>&& storage, OptionSet<Option> options)
+Cache::Cache(NetworkProcess& networkProcess, Ref<Storage>&& storage, OptionSet<CacheOption> options)
     : m_storage(WTFMove(storage))
     , m_networkProcess(networkProcess)
 {
 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
-    if (options.contains(Option::SpeculativeRevalidation)) {
+    if (options.contains(CacheOption::SpeculativeRevalidation)) {
         m_lowPowerModeNotifier = std::make_unique<WebCore::LowPowerModeNotifier>([this](bool isLowPowerModeEnabled) {
             ASSERT(WTF::RunLoop::isMain());
             if (isLowPowerModeEnabled)
@@ -98,7 +98,7 @@ Cache::Cache(NetworkProcess& networkProcess, Ref<Storage>&& storage, OptionSet<O
     }
 #endif
 
-    if (options.contains(Option::RegisterNotify)) {
+    if (options.contains(CacheOption::RegisterNotify)) {
 #if PLATFORM(COCOA)
         // Triggers with "notifyutil -p com.apple.WebKit.Cache.dump".
         int token;
index 8bbc169..d94fc73 100644 (file)
@@ -87,17 +87,18 @@ enum class UseDecision {
 
 using GlobalFrameID = std::pair<WebCore::PageIdentifier, uint64_t /*webFrameID*/>;
 
-class Cache : public RefCounted<Cache> {
-public:
-    enum class Option {
-        // In testing mode we try to eliminate sources of randomness. Cache does not shrink and there are no read timeouts.
-        TestingMode = 1 << 0,
-        RegisterNotify = 1 << 1,
+enum class CacheOption : uint8_t {
+    // In testing mode we try to eliminate sources of randomness. Cache does not shrink and there are no read timeouts.
+    TestingMode = 1 << 0,
+    RegisterNotify = 1 << 1,
 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
-        SpeculativeRevalidation = 1 << 2,
+    SpeculativeRevalidation = 1 << 2,
 #endif
-    };
-    static RefPtr<Cache> open(NetworkProcess&, const String& cachePath, OptionSet<Option>);
+};
+
+class Cache : public RefCounted<Cache> {
+public:
+    static RefPtr<Cache> open(NetworkProcess&, const String& cachePath, OptionSet<CacheOption>);
 
     void setCapacity(size_t);
 
@@ -148,7 +149,7 @@ public:
     ~Cache();
 
 private:
-    Cache(NetworkProcess&, Ref<Storage>&&, OptionSet<Option> options);
+    Cache(NetworkProcess&, Ref<Storage>&&, OptionSet<CacheOption>);
 
     Key makeCacheKey(const WebCore::ResourceRequest&);
 
index 5b52cab..07b9633 100644 (file)
@@ -117,18 +117,14 @@ void NetworkProcess::platformInitializeNetworkProcessCocoa(const NetworkProcessC
         return;
 
     SandboxExtension::consumePermanently(parameters.diskCacheDirectoryExtensionHandle);
-    OptionSet<NetworkCache::Cache::Option> cacheOptions { NetworkCache::Cache::Option::RegisterNotify };
+    m_cacheOptions = { NetworkCache::CacheOption::RegisterNotify };
     if (parameters.shouldUseTestingNetworkSession)
-        cacheOptions.add(NetworkCache::Cache::Option::TestingMode);
+        m_cacheOptions.add(NetworkCache::CacheOption::TestingMode);
 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
     if (parameters.shouldEnableNetworkCacheSpeculativeRevalidation)
-        cacheOptions.add(NetworkCache::Cache::Option::SpeculativeRevalidation);
+        m_cacheOptions.add(NetworkCache::CacheOption::SpeculativeRevalidation);
 #endif
 
-    m_cache = NetworkCache::Cache::open(*this, m_diskCacheDirectory, cacheOptions);
-    if (!m_cache)
-        RELEASE_LOG_ERROR(NetworkCache, "Failed to initialize the WebKit network disk cache");
-
     // Disable NSURLCache.
     auto urlCache(adoptNS([[NSURLCache alloc] initWithMemoryCapacity:0 diskCapacity:0 diskPath:nil]));
     [NSURLCache setSharedURLCache:urlCache.get()];
@@ -200,15 +196,13 @@ void NetworkProcess::clearDiskCache(WallTime modifiedSince, CompletionHandler<vo
     if (!m_clearCacheDispatchGroup)
         m_clearCacheDispatchGroup = dispatch_group_create();
 
-    auto* cache = this->cache();
-    if (!cache) {
-        completionHandler();
-        return;
-    }
-
     auto group = m_clearCacheDispatchGroup;
-    dispatch_group_async(group, dispatch_get_main_queue(), makeBlockPtr([cache, modifiedSince, completionHandler = WTFMove(completionHandler)] () mutable {
-        cache->clear(modifiedSince, WTFMove(completionHandler));
+    dispatch_group_async(group, dispatch_get_main_queue(), makeBlockPtr([this, protectedThis = makeRef(*this), modifiedSince, completionHandler = WTFMove(completionHandler)] () mutable {
+        auto aggregator = CallbackAggregator::create(WTFMove(completionHandler));
+        for (auto& session : networkSessions().values()) {
+            if (auto* cache = session->cache())
+                cache->clear(modifiedSince, [aggregator = aggregator.copyRef()] () { });
+        }
     }).get());
 }
 
index 018889a..4dc4f22 100644 (file)
@@ -924,7 +924,7 @@ static NSDictionary *proxyDictionary(const URL& httpProxy, const URL& httpsProxy
 }
 
 NetworkSessionCocoa::NetworkSessionCocoa(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
-    : NetworkSession(networkProcess, parameters.sessionID, WTFMove(parameters.localStorageDirectory), parameters.localStorageDirectoryExtensionHandle)
+    : NetworkSession(networkProcess, parameters)
     , m_boundInterfaceIdentifier(parameters.boundInterfaceIdentifier)
     , m_sourceApplicationBundleIdentifier(parameters.sourceApplicationBundleIdentifier)
     , m_sourceApplicationSecondaryIdentifier(parameters.sourceApplicationSecondaryIdentifier)
index 18348a8..bc2883a 100644 (file)
@@ -38,7 +38,7 @@ namespace WebKit {
 using namespace WebCore;
 
 NetworkSessionCurl::NetworkSessionCurl(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
-    : NetworkSession(networkProcess, parameters.sessionID, WTFMove(parameters.localStorageDirectory), parameters.localStorageDirectoryExtensionHandle)
+    : NetworkSession(networkProcess, parameters)
 {
     if (!parameters.cookiePersistentStorageFile.isEmpty())
         networkStorageSession()->setCookieDatabase(makeUniqueRef<CookieJarDB>(parameters.cookiePersistentStorageFile));
index 5f82aed..29dd087 100644 (file)
@@ -78,8 +78,10 @@ void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
     ResourceCachesToClear resourceCachesToClear = static_cast<ResourceCachesToClear>(cachesToClear);
     if (resourceCachesToClear == InMemoryResourceCachesOnly)
         return;
-    if (m_cache)
-        m_cache->clear();
+    for (auto& session : networkSessions().values()) {
+        if (auto* cache = session->cache())
+            cache->clear();
+    }
 }
 
 void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreationParameters& parameters)
index dca7c41..7c78b0b 100644 (file)
@@ -29,6 +29,7 @@
 
 #include "NetworkCache.h"
 #include "NetworkProcessCreationParameters.h"
+#include "NetworkSession.h"
 #include "ResourceCachesToClear.h"
 #include "WebCookieManager.h"
 #include "WebKitCachedResolver.h"
@@ -38,6 +39,7 @@
 #include <WebCore/ResourceHandle.h>
 #include <WebCore/SoupNetworkSession.h>
 #include <libsoup/soup.h>
+#include <wtf/CallbackAggregator.h>
 #include <wtf/FileSystem.h>
 #include <wtf/RAMSize.h>
 #include <wtf/glib/GRefPtr.h>
@@ -115,14 +117,12 @@ void NetworkProcess::platformInitializeNetworkProcess(const NetworkProcessCreati
 
     SoupNetworkSession::clearOldSoupCache(FileSystem::directoryName(m_diskCacheDirectory));
 
-    OptionSet<NetworkCache::Cache::Option> cacheOptions { NetworkCache::Cache::Option::RegisterNotify };
+    OptionSet<NetworkCache::CacheOption> cacheOptions { NetworkCache::CacheOption::RegisterNotify };
 #if ENABLE(NETWORK_CACHE_SPECULATIVE_REVALIDATION)
     if (parameters.shouldEnableNetworkCacheSpeculativeRevalidation)
-        cacheOptions.add(NetworkCache::Cache::Option::SpeculativeRevalidation);
+        cacheOptions.add(NetworkCache::CacheOption::SpeculativeRevalidation);
 #endif
 
-    m_cache = NetworkCache::Cache::open(*this, m_diskCacheDirectory, cacheOptions);
-
     supplement<WebCookieManager>()->setHTTPCookieAcceptPolicy(parameters.cookieAcceptPolicy, OptionalCallbackID());
 
     if (!parameters.languages.isEmpty())
@@ -156,11 +156,11 @@ void NetworkProcess::clearCacheForAllOrigins(uint32_t cachesToClear)
 
 void NetworkProcess::clearDiskCache(WallTime modifiedSince, CompletionHandler<void()>&& completionHandler)
 {
-    if (!m_cache) {
-        completionHandler();
-        return;
+    auto aggregator = CallbackAggregator::create(WTFMove(completionHandler));
+    for (auto& session : networkSessions().values()) {
+        if (auto* cache = session->cache())
+            cache->clear(modifiedSince, [aggregator = aggregator.copyRef()] () { });
     }
-    m_cache->clear(modifiedSince, WTFMove(completionHandler));
 }
 
 void NetworkProcess::platformTerminate()
index f7bbfa5..f7dec70 100644 (file)
@@ -40,7 +40,7 @@ namespace WebKit {
 using namespace WebCore;
 
 NetworkSessionSoup::NetworkSessionSoup(NetworkProcess& networkProcess, NetworkSessionCreationParameters&& parameters)
-    : NetworkSession(networkProcess, parameters.sessionID, WTFMove(parameters.localStorageDirectory), parameters.localStorageDirectoryExtensionHandle)
+    : NetworkSession(networkProcess, parameters)
 {
     networkStorageSession()->setCookieObserverHandler([this] {
         this->networkProcess().supplement<WebCookieManager>()->notifyCookiesDidChange(m_sessionID);
index e178dcd..1c91881 100644 (file)
@@ -248,6 +248,8 @@ static Vector<WebKit::WebsiteDataRecord> toWebsiteDataRecords(NSArray *dataRecor
             config->setCacheStorageDirectory(configuration._cacheStorageDirectory.path);
         if (configuration._serviceWorkerRegistrationDirectory)
             config->setServiceWorkerRegistrationDirectory(configuration._serviceWorkerRegistrationDirectory.path);
+        if (configuration.networkCacheDirectory)
+            config->setNetworkCacheDirectory(configuration.networkCacheDirectory.path);
     } else {
         RELEASE_ASSERT(!configuration._webStorageDirectory);
         RELEASE_ASSERT(!configuration._webSQLDatabaseDirectory);
@@ -256,6 +258,7 @@ static Vector<WebKit::WebsiteDataRecord> toWebsiteDataRecords(NSArray *dataRecor
         RELEASE_ASSERT(!configuration._resourceLoadStatisticsDirectory);
         RELEASE_ASSERT(!configuration._cacheStorageDirectory);
         RELEASE_ASSERT(!configuration._serviceWorkerRegistrationDirectory);
+        RELEASE_ASSERT(!configuration.networkCacheDirectory);
     }
 
     if (configuration.sourceApplicationBundleIdentifier)
index 06a70ee..b99ffe3 100644 (file)
@@ -53,6 +53,7 @@ WK_CLASS_AVAILABLE(macos(10.13), ios(11.0))
 @property (nonatomic, copy, setter=_setResourceLoadStatisticsDirectory:) NSURL *_resourceLoadStatisticsDirectory WK_API_AVAILABLE(macos(10.13.4), ios(11.3));
 @property (nonatomic, copy, setter=_setCacheStorageDirectory:) NSURL *_cacheStorageDirectory WK_API_AVAILABLE(macos(10.13.4), ios(11.3));
 @property (nonatomic, copy, setter=_setServiceWorkerRegistrationDirectory:) NSURL *_serviceWorkerRegistrationDirectory WK_API_AVAILABLE(macos(10.13.4), ios(11.3));
+@property (nonatomic, nullable, copy) NSURL *networkCacheDirectory WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
 
 // Testing only.
 @property (nonatomic) BOOL allLoadsBlockedByDeviceManagementRestrictionsForTesting WK_API_AVAILABLE(macos(WK_MAC_TBA), ios(WK_IOS_TBA));
index 59a3074..dfc93b1 100644 (file)
@@ -89,6 +89,19 @@ static void checkURLArgument(NSURL *url)
     _configuration->setIndexedDBDatabaseDirectory(url.path);
 }
 
+- (NSURL *)networkCacheDirectory
+{
+    return [NSURL fileURLWithPath:_configuration->networkCacheDirectory() isDirectory:YES];
+}
+
+- (void)setNetworkCacheDirectory:(NSURL *)url
+{
+    if (!_configuration->isPersistent())
+        [NSException raise:NSInvalidArgumentException format:@"Cannot set networkCacheDirectory on a non-persistent _WKWebsiteDataStoreConfiguration."];
+    checkURLArgument(url);
+    _configuration->setNetworkCacheDirectory(url.path);
+}
+
 - (NSURL *)_webSQLDatabaseDirectory
 {
     return [NSURL fileURLWithPath:_configuration->webSQLDatabaseDirectory() isDirectory:YES];
index 2a56a74..d7d22d9 100644 (file)
@@ -112,6 +112,11 @@ WebsiteDataStoreParameters WebsiteDataStore::parameters()
     if (!localStorageDirectory.isEmpty())
         SandboxExtension::createHandleForReadWriteDirectory(localStorageDirectory, localStorageDirectoryExtensionHandle);
 
+    auto networkCacheDirectory = resolvedNetworkCacheDirectory();
+    SandboxExtension::Handle networkCacheDirectoryExtensionHandle;
+    if (!networkCacheDirectory.isEmpty())
+        SandboxExtension::createHandleForReadWriteDirectory(networkCacheDirectory, networkCacheDirectoryExtensionHandle);
+
     bool shouldIncludeLocalhostInResourceLoadStatistics = isSafari;
     WebsiteDataStoreParameters parameters;
     parameters.networkSessionParameters = {
@@ -135,7 +140,9 @@ WebsiteDataStoreParameters WebsiteDataStore::parameters()
         m_configuration->allLoadsBlockedByDeviceManagementRestrictionsForTesting(),
         WTFMove(resourceLoadStatisticsManualPrevalentResource),
         WTFMove(localStorageDirectory),
-        WTFMove(localStorageDirectoryExtensionHandle)
+        WTFMove(localStorageDirectoryExtensionHandle),
+        WTFMove(networkCacheDirectory),
+        WTFMove(networkCacheDirectoryExtensionHandle),
     };
     networkingHasBegun();
 
index 5ab53da..00c7feb 100644 (file)
@@ -206,6 +206,8 @@ void WebsiteDataStore::resolveDirectoriesIfNecessary()
         m_resolvedConfiguration->setIndexedDBDatabaseDirectory(resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->indexedDBDatabaseDirectory()));
     if (!m_configuration->deviceIdHashSaltsStorageDirectory().isEmpty())
         m_resolvedConfiguration->setDeviceIdHashSaltsStorageDirectory(resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->deviceIdHashSaltsStorageDirectory()));
+    if (!m_configuration->networkCacheDirectory().isEmpty())
+        m_resolvedConfiguration->setNetworkCacheDirectory(resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->networkCacheDirectory()));
     if (!m_configuration->resourceLoadStatisticsDirectory().isEmpty())
         m_resolvedConfiguration->setResourceLoadStatisticsDirectory(resolveAndCreateReadWriteDirectoryForSandboxExtension(m_configuration->resourceLoadStatisticsDirectory()));
     if (!m_configuration->serviceWorkerRegistrationDirectory().isEmpty() && m_resolvedConfiguration->serviceWorkerRegistrationDirectory().isEmpty())
index f07cc61..cf760fc 100644 (file)
@@ -184,6 +184,7 @@ public:
     void resolveDirectoriesIfNecessary();
     const String& resolvedApplicationCacheDirectory() const { return m_resolvedConfiguration->applicationCacheDirectory(); }
     const String& resolvedLocalStorageDirectory() const { return m_resolvedConfiguration->localStorageDirectory(); }
+    const String& resolvedNetworkCacheDirectory() const { return m_resolvedConfiguration->networkCacheDirectory(); }
     const String& resolvedMediaCacheDirectory() const { return m_resolvedConfiguration->mediaCacheDirectory(); }
     const String& resolvedMediaKeysDirectory() const { return m_resolvedConfiguration->mediaKeysStorageDirectory(); }
     const String& resolvedDatabaseDirectory() const { return m_resolvedConfiguration->webSQLDatabaseDirectory(); }
index 52a8fa3..fcd57e2 100644 (file)
@@ -1,3 +1,16 @@
+2019-07-18  Alex Christensen  <achristensen@webkit.org>
+
+        Move NetworkCache ownership from NetworkProcess to NetworkSession
+        https://bugs.webkit.org/show_bug.cgi?id=199817
+
+        Reviewed by Geoff Garen.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/WebsiteDataStoreCustomPaths.mm:
+        (TEST):
+        * TestWebKitAPI/cocoa/TestWKWebView.h:
+        * TestWebKitAPI/cocoa/TestWKWebView.mm:
+        (-[WKWebView synchronouslyLoadRequest:]):
+
 2019-07-18  Youenn Fablet  <youenn@apple.com>
 
         Make sure to set kCTFontFallbackOptionAttribute to kCTFontFallbackOptionSystem for system fonts
index e4ee020..5f5ca5e 100644 (file)
@@ -26,6 +26,7 @@
 #import "config.h"
 
 #import "PlatformUtilities.h"
+#import "TCPServer.h"
 #import "Test.h"
 #import "TestNavigationDelegate.h"
 #import "TestWKWebView.h"
@@ -521,3 +522,35 @@ TEST(WebKit, ApplicationIdentifiers)
     EXPECT_TRUE([websiteDataStore._sourceApplicationBundleIdentifier isEqualToString:@"otheridentifier"]);
     EXPECT_TRUE([[websiteDataStoreConfiguration sourceApplicationBundleIdentifier] isEqualToString:@"testidentifier"]);
 }
+
+TEST(WebKit, NetworkCacheDirectory)
+{
+    using namespace TestWebKitAPI;
+    TCPServer server([] (int socket) {
+        TCPServer::read(socket);
+        const char* response =
+        "HTTP/1.1 200 OK\r\n"
+        "Cache-Control: max-age=1000000\r\n"
+        "Content-Length: 6\r\n\r\n"
+        "Hello!";
+        TCPServer::write(socket, response, strlen(response));
+    });
+    
+    NSURL *tempDir = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:@"CustomPathsTest"] isDirectory:YES];
+    
+    auto websiteDataStoreConfiguration = adoptNS([[_WKWebsiteDataStoreConfiguration alloc] init]);
+    [websiteDataStoreConfiguration setNetworkCacheDirectory:tempDir];
+
+    auto webViewConfiguration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    [webViewConfiguration setWebsiteDataStore:[[[WKWebsiteDataStore alloc] _initWithConfiguration:websiteDataStoreConfiguration.get()] autorelease]];
+
+    auto webView = adoptNS([[TestWKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:webViewConfiguration.get()]);
+    [webView synchronouslyLoadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://127.0.0.1:%d/", server.port()]]]];
+    NSString *path = tempDir.path;
+    NSFileManager *fileManager = [NSFileManager defaultManager];
+    while (![fileManager fileExistsAtPath:path])
+        Util::spinRunLoop();
+    NSError *error = nil;
+    [fileManager removeItemAtPath:path error:&error];
+    EXPECT_FALSE(error);
+}
index 99f0c76..4fd1fd5 100644 (file)
@@ -52,6 +52,7 @@
 - (void)loadTestPageNamed:(NSString *)pageName;
 - (void)synchronouslyLoadHTMLString:(NSString *)html;
 - (void)synchronouslyLoadHTMLString:(NSString *)html baseURL:(NSURL *)url;
+- (void)synchronouslyLoadRequest:(NSURLRequest *)request;
 - (void)synchronouslyLoadTestPageNamed:(NSString *)pageName;
 - (BOOL)_synchronouslyExecuteEditCommand:(NSString *)command argument:(NSString *)argument;
 - (void)expectElementTagsInOrder:(NSArray<NSString *> *)tagNames;
index 34b3112..3da8172 100644 (file)
@@ -71,6 +71,12 @@ SOFT_LINK_CLASS(UIKit, UIWindow)
     [self loadRequest:request];
 }
 
+- (void)synchronouslyLoadRequest:(NSURLRequest *)request
+{
+    [self loadRequest:request];
+    [self _test_waitForDidFinishNavigation];
+}
+
 - (void)synchronouslyLoadHTMLString:(NSString *)html baseURL:(NSURL *)url
 {
     [self loadHTMLString:html baseURL:url];