Title: [232769] branches/safari-606.1.20.40-branch
Revision
232769
Author
kocsen_ch...@apple.com
Date
2018-06-12 12:28:38 -0700 (Tue, 12 Jun 2018)

Log Message

Cherry-pick r232580. rdar://problem/38881568

    Don't start service worker fetch when there is substitute data
    https://bugs.webkit.org/show_bug.cgi?id=186349
    <rdar://problem/38881568>

    Reviewed by Youenn Fablet.

    Loading content via WKWebView.loadData may also end up starting a main resource service worker fetch.
    This breaks DocumentWriter assumptions.

    * loader/DocumentLoader.cpp:
    (WebCore::DocumentLoader::tryLoadingRequestFromApplicationCache):
    (WebCore::DocumentLoader::tryLoadingSubstituteData):

    Factor substitute resource loading out from tryLoadingRequestFromApplicationCache.

    (WebCore::DocumentLoader::startLoadingMainResource):

    If we have substitute data already (typically from WKWebView.loadData), allow service worker registration
    but load the main resource using the substitute data.

    (WebCore::DocumentLoader::handleSubstituteDataLoadSoon): Deleted.

    Merge to tryLoadingSubstituteData.

    * loader/DocumentLoader.h:

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

Modified Paths

Diff

Modified: branches/safari-606.1.20.40-branch/Source/WebCore/ChangeLog (232768 => 232769)


--- branches/safari-606.1.20.40-branch/Source/WebCore/ChangeLog	2018-06-12 19:28:30 UTC (rev 232768)
+++ branches/safari-606.1.20.40-branch/Source/WebCore/ChangeLog	2018-06-12 19:28:38 UTC (rev 232769)
@@ -1,3 +1,64 @@
+2018-06-12  Kocsen Chung  <kocsen_ch...@apple.com>
+
+        Cherry-pick r232580. rdar://problem/38881568
+
+    Don't start service worker fetch when there is substitute data
+    https://bugs.webkit.org/show_bug.cgi?id=186349
+    <rdar://problem/38881568>
+    
+    Reviewed by Youenn Fablet.
+    
+    Loading content via WKWebView.loadData may also end up starting a main resource service worker fetch.
+    This breaks DocumentWriter assumptions.
+    
+    * loader/DocumentLoader.cpp:
+    (WebCore::DocumentLoader::tryLoadingRequestFromApplicationCache):
+    (WebCore::DocumentLoader::tryLoadingSubstituteData):
+    
+    Factor substitute resource loading out from tryLoadingRequestFromApplicationCache.
+    
+    (WebCore::DocumentLoader::startLoadingMainResource):
+    
+    If we have substitute data already (typically from WKWebView.loadData), allow service worker registration
+    but load the main resource using the substitute data.
+    
+    (WebCore::DocumentLoader::handleSubstituteDataLoadSoon): Deleted.
+    
+    Merge to tryLoadingSubstituteData.
+    
+    * loader/DocumentLoader.h:
+    
+    
+    git-svn-id: https://svn.webkit.org/repository/webkit/trunk@232580 268f45cc-cd09-0410-ab3c-d52691b4dbfc
+
+    2018-06-07  Antti Koivisto  <an...@apple.com>
+
+            Don't start service worker fetch when there is substitute data
+            https://bugs.webkit.org/show_bug.cgi?id=186349
+            <rdar://problem/38881568>
+
+            Reviewed by Youenn Fablet.
+
+            Loading content via WKWebView.loadData may also end up starting a main resource service worker fetch.
+            This breaks DocumentWriter assumptions.
+
+            * loader/DocumentLoader.cpp:
+            (WebCore::DocumentLoader::tryLoadingRequestFromApplicationCache):
+            (WebCore::DocumentLoader::tryLoadingSubstituteData):
+
+            Factor substitute resource loading out from tryLoadingRequestFromApplicationCache.
+
+            (WebCore::DocumentLoader::startLoadingMainResource):
+
+            If we have substitute data already (typically from WKWebView.loadData), allow service worker registration
+            but load the main resource using the substitute data.
+
+            (WebCore::DocumentLoader::handleSubstituteDataLoadSoon): Deleted.
+
+            Merge to tryLoadingSubstituteData.
+
+            * loader/DocumentLoader.h:
+
 2018-06-07  Kocsen Chung  <kocsen_ch...@apple.com>
 
         Cherry-pick r232563. rdar://problem/40439109

Modified: branches/safari-606.1.20.40-branch/Source/WebCore/loader/DocumentLoader.cpp (232768 => 232769)


--- branches/safari-606.1.20.40-branch/Source/WebCore/loader/DocumentLoader.cpp	2018-06-12 19:28:30 UTC (rev 232768)
+++ branches/safari-606.1.20.40-branch/Source/WebCore/loader/DocumentLoader.cpp	2018-06-12 19:28:38 UTC (rev 232769)
@@ -478,14 +478,6 @@
 #endif
 }
 
-void DocumentLoader::handleSubstituteDataLoadSoon()
-{
-    if (!m_deferMainResourceDataLoad || frameLoader()->loadsSynchronously())
-        handleSubstituteDataLoadNow();
-    else
-        startDataLoadTimer();
-}
-
 #if ENABLE(SERVICE_WORKER)
 void DocumentLoader::matchRegistration(const URL& url, SWClientConnection::RegistrationCallback&& callback)
 {
@@ -671,15 +663,24 @@
 bool DocumentLoader::tryLoadingRequestFromApplicationCache()
 {
     m_applicationCacheHost->maybeLoadMainResource(m_request, m_substituteData);
+    return tryLoadingSubstituteData();
+}
 
+bool DocumentLoader::tryLoadingSubstituteData()
+{
     if (!m_substituteData.isValid() || !m_frame->page())
         return false;
 
-    RELEASE_LOG_IF_ALLOWED("startLoadingMainResource: Returning cached main resource (frame = %p, main = %d)", m_frame, m_frame->isMainFrame());
+    RELEASE_LOG_IF_ALLOWED("startLoadingMainResource: Returning substitute data (frame = %p, main = %d)", m_frame, m_frame->isMainFrame());
     m_identifierForLoadWithoutResourceLoader = m_frame->page()->progress().createUniqueIdentifier();
     frameLoader()->notifier().assignIdentifierToInitialRequest(m_identifierForLoadWithoutResourceLoader, this, m_request);
     frameLoader()->notifier().dispatchWillSendRequest(this, m_identifierForLoadWithoutResourceLoader, m_request, ResourceResponse());
-    handleSubstituteDataLoadSoon();
+
+    if (!m_deferMainResourceDataLoad || frameLoader()->loadsSynchronously())
+        handleSubstituteDataLoadNow();
+    else
+        startDataLoadTimer();
+
     return true;
 }
 
@@ -1728,6 +1729,11 @@
                 return;
 
             m_serviceWorkerRegistrationData = WTFMove(registrationData);
+
+            // Prefer existing substitute data (from WKWebView.loadData etc) over service worker fetch.
+            if (this->tryLoadingSubstituteData())
+                return;
+            // Try app cache only if there is no service worker.
             if (!m_serviceWorkerRegistrationData && this->tryLoadingRequestFromApplicationCache())
                 return;
             this->loadMainResource(WTFMove(request));

Modified: branches/safari-606.1.20.40-branch/Source/WebCore/loader/DocumentLoader.h (232768 => 232769)


--- branches/safari-606.1.20.40-branch/Source/WebCore/loader/DocumentLoader.h	2018-06-12 19:28:30 UTC (rev 232768)
+++ branches/safari-606.1.20.40-branch/Source/WebCore/loader/DocumentLoader.h	2018-06-12 19:28:38 UTC (rev 232769)
@@ -377,6 +377,7 @@
     bool isPostOrRedirectAfterPost(const ResourceRequest&, const ResourceResponse&);
 
     bool tryLoadingRequestFromApplicationCache();
+    bool tryLoadingSubstituteData();
     bool tryLoadingRedirectRequestFromApplicationCache(const ResourceRequest&);
 #if ENABLE(SERVICE_WORKER)
     void restartLoadingDueToServiceWorkerRegistrationChange(ResourceRequest&&, std::optional<ServiceWorkerRegistrationData>&&);
@@ -391,7 +392,6 @@
 #else
     typedef Timer DocumentLoaderTimer;
 #endif
-    void handleSubstituteDataLoadSoon();
     void handleSubstituteDataLoadNow();
     void startDataLoadTimer();
 

Modified: branches/safari-606.1.20.40-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm (232768 => 232769)


--- branches/safari-606.1.20.40-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm	2018-06-12 19:28:30 UTC (rev 232768)
+++ branches/safari-606.1.20.40-branch/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm	2018-06-12 19:28:38 UTC (rev 232769)
@@ -1370,4 +1370,52 @@
     EXPECT_EQ(0U, processPool._serviceWorkerProcessCount);
 }
 
+TEST(ServiceWorkers, LoadData)
+{
+    ASSERT(mainBytes);
+    ASSERT(scriptBytes);
+
+    [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins];
+
+    // Start with a clean slate data store
+    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
+        done = true;
+    }];
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+
+    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
+
+    RetainPtr<SWMessageHandler> messageHandler = adoptNS([[SWMessageHandler alloc] init]);
+    [[configuration userContentController] addScriptMessageHandler:messageHandler.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/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"];
+
+    auto delegate = adoptNS([[TestSWAsyncNavigationDelegate alloc] init]);
+    [webView setNavigationDelegate:delegate.get()];
+    [webView setUIDelegate:delegate.get()];
+
+    done = false;
+
+    // Normal load to get SW registered.
+    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]];
+    [webView loadRequest:request];
+
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+
+    // Now try a data load.
+    NSData *data = "" dataWithBytes:mainBytes length:strlen(mainBytes)];
+    [webView loadData:data MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"sw://host/main.html"]];
+
+    TestWebKitAPI::Util::run(&done);
+    done = false;
+}
+
 #endif // WK_API_ENABLED
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to