Add API test to validate setting of service worker and cache storage directories
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 1 Mar 2018 23:18:28 +0000 (23:18 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 1 Mar 2018 23:18:28 +0000 (23:18 +0000)
https://bugs.webkit.org/show_bug.cgi?id=182543

Patch by Youenn Fablet <youenn@apple.com> on 2018-03-01
Reviewed by Chris Dumez.

Source/WebCore:

Covered by API tests.

Add getters to service worker registration directory.

* testing/Internals.cpp:
(WebCore::Internals::serviceWorkerRegistrationDirectory):
* testing/Internals.h:
* testing/Internals.idl:
* workers/service/SWClientConnection.h:
* workers/service/server/RegistrationDatabase.h:
(WebCore::RegistrationDatabase::serviceWorkerRegistrationDirectory const):
* workers/service/server/RegistrationStore.h:
(WebCore::RegistrationStore::serviceWorkerRegistrationDirectory const):
* workers/service/server/SWServer.h:
(WebCore::SWServer::serviceWorkerRegistrationDirectory const):

Source/WebKit:

Add cache engine directory path to its representation dump.
This is used in API tests to check that the path is correctly set.

Add a way for to know whether a service worker was registered from a WKWebsiteDataStore.
This is used in API tests to check that the path is correctly set.

* NetworkProcess/cache/CacheStorageEngine.cpp:
(WebKit::CacheStorage::Engine::representation):
* UIProcess/API/Cocoa/WKWebsiteDataStore.mm:
(-[WKWebsiteDataStore _hasRegisteredServiceWorker]):
* UIProcess/API/Cocoa/WKWebsiteDataStorePrivate.h:

Tools:

* TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm:

LayoutTests:

Updated tests according new engine representation.

* http/tests/cache-storage/cache-clearing-origin.https.html:
* http/tests/cache-storage/cache-origins.https.html:
* http/tests/cache-storage/cache-representation.https.html:

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

LayoutTests/ChangeLog
LayoutTests/http/tests/cache-storage/cache-clearing-origin.https.html
LayoutTests/http/tests/cache-storage/cache-origins.https.html
LayoutTests/http/tests/cache-storage/cache-representation.https.html
Source/WebCore/ChangeLog
Source/WebKit/ChangeLog
Source/WebKit/NetworkProcess/cache/CacheStorageEngine.cpp
Source/WebKit/UIProcess/API/Cocoa/WKWebsiteDataStore.mm
Source/WebKit/UIProcess/API/Cocoa/WKWebsiteDataStorePrivate.h
Tools/ChangeLog
Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm

index b886c7c..ac1409b 100644 (file)
@@ -1,3 +1,16 @@
+2018-03-01  Youenn Fablet  <youenn@apple.com>
+
+        Add API test to validate setting of service worker and cache storage directories
+        https://bugs.webkit.org/show_bug.cgi?id=182543
+
+        Reviewed by Chris Dumez.
+
+        Updated tests according new engine representation.
+
+        * http/tests/cache-storage/cache-clearing-origin.https.html:
+        * http/tests/cache-storage/cache-origins.https.html:
+        * http/tests/cache-storage/cache-representation.https.html:
+
 2018-03-01  Daniel Bates  <dabates@apple.com>
 
         CSS ::selection stroke-color and stroke-width are not applied to selected text in text fields
index 01f0321..92ce8d4 100644 (file)
@@ -23,7 +23,7 @@ promise_test(() => {
     return new Promise((resolve, reject) => {
         var counter = 0;
         window.addEventListener("message", async (event) => {
-            var representation = JSON.parse(await internals.cacheStorageEngineRepresentation());
+            var representation = JSON.parse(await internals.cacheStorageEngineRepresentation()).origins;
             test(() => {
                 representation.sort(compareClientOrigins);
                 var cache = representation[0].origin.clientOrigin === "https://localhost:8443"
@@ -34,7 +34,7 @@ promise_test(() => {
 
             testRunner.clearDOMCache('https://localhost:8443');
 
-            representation = JSON.parse(await internals.cacheStorageEngineRepresentation());
+            representation = JSON.parse(await internals.cacheStorageEngineRepresentation()).origins;
             test(() => {
                 representation.sort(compareClientOrigins);
                 assert_equals(representation[0].origin.topOrigin, "https://127.0.0.1:8443", "top origin of cache 1");
index 7731f1c..21f8ec3 100644 (file)
@@ -19,7 +19,7 @@
 
     async function checkCachesAfterClearingOrigins() {
         testRunner.clearDOMCache('https://127.0.0.1:8443');
-        var representation = JSON.parse(await internals.cacheStorageEngineRepresentation());
+        var representation = JSON.parse(await internals.cacheStorageEngineRepresentation()).origins;
         test(() => {
             assert_equals(representation[0].origin.topOrigin, "https://127.0.0.1:8443", "top origin of cache 1");
             assert_equals(representation[1].origin.topOrigin, "https://127.0.0.1:8443", "top origin of cache 2");
@@ -31,7 +31,7 @@
     }
 
     async function checkCacheRepresentation() {
-        var representation = JSON.parse(await internals.cacheStorageEngineRepresentation());
+        var representation = JSON.parse(await internals.cacheStorageEngineRepresentation()).origins;
         test(() => {
             assert_equals(representation[0].origin.topOrigin, "https://127.0.0.1:8443", "top origin of cache 1");
             assert_equals(representation[1].origin.topOrigin, "https://127.0.0.1:8443", "top origin of cache 2");
index 2f93028..61c0f8d 100644 (file)
@@ -14,7 +14,7 @@
 
     function checkCaches(hasPersistent, hasRemoved, name, value) {
         test(() => {
-            var results = JSON.parse(value);
+            var results = JSON.parse(value).origins;
             assert_equals(results.length, 1);
             var caches = results[0].caches;
             assert_equals(!!caches["persistent"].length, hasPersistent, "persistent");
index 19046c4..265525b 100644 (file)
@@ -1,5 +1,28 @@
 2018-03-01  Youenn Fablet  <youenn@apple.com>
 
+        Add API test to validate setting of service worker and cache storage directories
+        https://bugs.webkit.org/show_bug.cgi?id=182543
+
+        Reviewed by Chris Dumez.
+
+        Covered by API tests.
+
+        Add getters to service worker registration directory.
+
+        * testing/Internals.cpp:
+        (WebCore::Internals::serviceWorkerRegistrationDirectory):
+        * testing/Internals.h:
+        * testing/Internals.idl:
+        * workers/service/SWClientConnection.h:
+        * workers/service/server/RegistrationDatabase.h:
+        (WebCore::RegistrationDatabase::serviceWorkerRegistrationDirectory const):
+        * workers/service/server/RegistrationStore.h:
+        (WebCore::RegistrationStore::serviceWorkerRegistrationDirectory const):
+        * workers/service/server/SWServer.h:
+        (WebCore::SWServer::serviceWorkerRegistrationDirectory const):
+
+2018-03-01  Youenn Fablet  <youenn@apple.com>
+
         SWServer::removeClientServiceWorkerRegistration should not log an error if there is no registration
         https://bugs.webkit.org/show_bug.cgi?id=183248
 
index 7d440e2..84ff1ee 100644 (file)
@@ -1,3 +1,22 @@
+2018-03-01  Youenn Fablet  <youenn@apple.com>
+
+        Add API test to validate setting of service worker and cache storage directories
+        https://bugs.webkit.org/show_bug.cgi?id=182543
+
+        Reviewed by Chris Dumez.
+
+        Add cache engine directory path to its representation dump.
+        This is used in API tests to check that the path is correctly set.
+
+        Add a way for to know whether a service worker was registered from a WKWebsiteDataStore.
+        This is used in API tests to check that the path is correctly set.
+
+        * NetworkProcess/cache/CacheStorageEngine.cpp:
+        (WebKit::CacheStorage::Engine::representation):
+        * UIProcess/API/Cocoa/WKWebsiteDataStore.mm:
+        (-[WKWebsiteDataStore _hasRegisteredServiceWorker]):
+        * UIProcess/API/Cocoa/WKWebsiteDataStorePrivate.h:
+
 2018-03-01  Wenson Hsieh  <wenson_hsieh@apple.com>
 
         Fix some errors due to some mach port APIs being unavailable on watchOS
index 1a91966..2c105c4 100644 (file)
@@ -475,7 +475,9 @@ String Engine::representation()
 {
     bool isFirst = true;
     StringBuilder builder;
-    builder.append("[");
+    builder.append("{ \"path\": \"");
+    builder.append(m_rootPath);
+    builder.append("\", \"origins\": [");
     for (auto& keyValue : m_caches) {
         if (!isFirst)
             builder.append(",");
@@ -489,7 +491,7 @@ String Engine::representation()
         keyValue.value->appendRepresentation(builder);
         builder.append("}");
     }
-    builder.append("\n]");
+    builder.append("]}");
     return builder.toString();
 }
 
index 8c35b95..df96302 100644 (file)
@@ -37,6 +37,7 @@
 #import "WebsiteDataFetchOption.h"
 #import "_WKWebsiteDataStoreConfiguration.h"
 #import <WebCore/URL.h>
+#import <WebKit/ServiceWorkerProcessProxy.h>
 #import <wtf/BlockPtr.h>
 
 using namespace WebCore;
@@ -341,6 +342,11 @@ static Vector<WebKit::WebsiteDataRecord> toWebsiteDataRecords(NSArray *dataRecor
     });
 }
 
+- (bool)_hasRegisteredServiceWorker
+{
+    return WebKit::ServiceWorkerProcessProxy::hasRegisteredServiceWorkers(_websiteDataStore->websiteDataStore().serviceWorkerRegistrationDirectory());
+}
+
 @end
 
 #endif // WK_API_ENABLED
index 20d705e..9366602 100644 (file)
@@ -57,6 +57,8 @@ typedef NS_OPTIONS(NSUInteger, _WKWebsiteDataStoreFetchOptions) {
 - (void)_setResourceLoadStatisticsTestingCallback:(nullable void (^)(WKWebsiteDataStore *, NSString *))callback WK_API_AVAILABLE(macosx(10.13), ios(11.0));
 - (void)_getAllStorageAccessEntries:(void (^)(NSArray<NSString *> *domains))completionHandler WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
 + (void)_allowWebsiteDataRecordsForAllOrigins WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
+- (bool)_hasRegisteredServiceWorker WK_API_AVAILABLE(macosx(WK_MAC_TBA), ios(WK_IOS_TBA));
+
 @end
 
 NS_ASSUME_NONNULL_END
index 101fb29..0c1970c 100644 (file)
@@ -1,3 +1,12 @@
+2018-03-01  Youenn Fablet  <youenn@apple.com>
+
+        Add API test to validate setting of service worker and cache storage directories
+        https://bugs.webkit.org/show_bug.cgi?id=182543
+
+        Reviewed by Chris Dumez.
+
+        * TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm:
+
 2018-03-01  Ross Kirsling  <ross.kirsling@sony.com>
 
         [Win][DRT] Implement setSpatialNavigationEnabled.
index 60d2503..42f4426 100644 (file)
@@ -54,6 +54,7 @@ struct ResourceInfo {
 static bool done;
 
 static String expectedMessage;
+static String retrievedString;
 
 @interface SWMessageHandler : NSObject <WKScriptMessageHandler>
 @end
@@ -978,6 +979,122 @@ TEST(ServiceWorkers, HasServiceWorkerRegistrationBit)
     TestWebKitAPI::Util::run(&done);
     done = false;
 }
+
+static const char* regularPageGrabbingCacheStorageDirectory = R"SWRESOURCE(
+<script>
+async function getResult()
+{
+    var result = await window.internals.cacheStorageEngineRepresentation();
+    window.webkit.messageHandlers.sw.postMessage(result);
+}
+getResult();
+</script>
+)SWRESOURCE";
+
+@interface DirectoryPageMessageHandler : NSObject <WKScriptMessageHandler>
+@end
+
+@implementation DirectoryPageMessageHandler
+- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
+{
+    retrievedString = [message body];
+    done = true;
+}
+@end
+
+TEST(ServiceWorkers, ServiceWorkerAndCacheStorageDefaultDirectories)
+{
+    ASSERT(mainBytes);
+    ASSERT(scriptBytes);
+
+    [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins];
+
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+
+    configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    setConfigurationInjectedBundlePath(configuration.get());
+
+    RetainPtr<DirectoryPageMessageHandler> directoryPageMessageHandler = adoptNS([[DirectoryPageMessageHandler alloc] init]);
+    [[configuration userContentController] addScriptMessageHandler:directoryPageMessageHandler.get() name:@"sw"];
+
+    RetainPtr<SWSchemes> handler = adoptNS([[SWSchemes alloc] init]);
+    handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainBytes });
+    handler->resources.set("sw://host/regularPageGrabbingCacheStorageDirectory.html", ResourceInfo { @"text/html", regularPageGrabbingCacheStorageDirectory });
+    handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes });
+    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"];
+
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]];
+
+    [webView loadRequest:request];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+    EXPECT_TRUE([[configuration websiteDataStore] _hasRegisteredServiceWorker]);
+
+    webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"];
+
+    request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/regularPageGrabbingCacheStorageDirectory.html"]];
+    [webView loadRequest:request];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+    EXPECT_TRUE(retrievedString.contains("/Caches/TestWebKitAPI/WebKit/CacheStorage"));
+
+    [[configuration websiteDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+}
+
+TEST(ServiceWorkers, ServiceWorkerAndCacheStorageSpecificDirectories)
+{
+    ASSERT(mainBytes);
+    ASSERT(scriptBytes);
+
+    [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins];
+
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+
+    configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+    setConfigurationInjectedBundlePath(configuration.get());
+    auto websiteDataStore = [configuration websiteDataStore];
+    [websiteDataStore _setCacheStorageDirectory:@"/var/tmp"];
+    [websiteDataStore _setServiceWorkerRegistrationDirectory:@"/var/tmp"];
+
+    RetainPtr<DirectoryPageMessageHandler> directoryPageMessageHandler = adoptNS([[DirectoryPageMessageHandler alloc] init]);
+    [[configuration userContentController] addScriptMessageHandler:directoryPageMessageHandler.get() name:@"sw"];
+
+    RetainPtr<SWSchemes> handler = adoptNS([[SWSchemes alloc] init]);
+    handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainBytes });
+    handler->resources.set("sw://host/regularPageGrabbingCacheStorageDirectory.html", ResourceInfo { @"text/html", regularPageGrabbingCacheStorageDirectory });
+    handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes });
+    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"];
+
+    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"];
+
+    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]];
+    [webView loadRequest:request];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+    EXPECT_TRUE([websiteDataStore _hasRegisteredServiceWorker]);
+
+    webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
+    request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/regularPageGrabbingCacheStorageDirectory.html"]];
+
+    [webView loadRequest:request];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+    EXPECT_TRUE(retrievedString.contains("\"path\": \"/var/tmp\""));
+
+    [[configuration websiteDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+}
+
 #endif // WK_HAVE_C_SPI
 
 TEST(ServiceWorkers, NonDefaultSessionID)