Changeset 232580 in webkit


Ignore:
Timestamp:
Jun 7, 2018 7:59:03 AM (6 years ago)
Author:
Antti Koivisto
Message:

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:
Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r232579 r232580  
     12018-06-07  Antti Koivisto  <antti@apple.com>
     2
     3        Don't start service worker fetch when there is substitute data
     4        https://bugs.webkit.org/show_bug.cgi?id=186349
     5        <rdar://problem/38881568>
     6
     7        Reviewed by Youenn Fablet.
     8
     9        Loading content via WKWebView.loadData may also end up starting a main resource service worker fetch.
     10        This breaks DocumentWriter assumptions.
     11
     12        * loader/DocumentLoader.cpp:
     13        (WebCore::DocumentLoader::tryLoadingRequestFromApplicationCache):
     14        (WebCore::DocumentLoader::tryLoadingSubstituteData):
     15
     16        Factor substitute resource loading out from tryLoadingRequestFromApplicationCache.
     17
     18        (WebCore::DocumentLoader::startLoadingMainResource):
     19
     20        If we have substitute data already (typically from WKWebView.loadData), allow service worker registration
     21        but load the main resource using the substitute data.
     22
     23        (WebCore::DocumentLoader::handleSubstituteDataLoadSoon): Deleted.
     24
     25        Merge to tryLoadingSubstituteData.
     26
     27        * loader/DocumentLoader.h:
     28
    1292018-06-07  Thibault Saunier  <tsaunier@igalia.com>
    230
  • trunk/Source/WebCore/loader/DocumentLoader.cpp

    r232419 r232580  
    479479}
    480480
    481 void DocumentLoader::handleSubstituteDataLoadSoon()
    482 {
    483     if (!m_deferMainResourceDataLoad || frameLoader()->loadsSynchronously())
    484         handleSubstituteDataLoadNow();
    485     else
    486         startDataLoadTimer();
    487 }
    488 
    489481#if ENABLE(SERVICE_WORKER)
    490482void DocumentLoader::matchRegistration(const URL& url, SWClientConnection::RegistrationCallback&& callback)
     
    672664{
    673665    m_applicationCacheHost->maybeLoadMainResource(m_request, m_substituteData);
    674 
     666    return tryLoadingSubstituteData();
     667}
     668
     669bool DocumentLoader::tryLoadingSubstituteData()
     670{
    675671    if (!m_substituteData.isValid() || !m_frame->page())
    676672        return false;
    677673
    678     RELEASE_LOG_IF_ALLOWED("startLoadingMainResource: Returning cached main resource (frame = %p, main = %d)", m_frame, m_frame->isMainFrame());
     674    RELEASE_LOG_IF_ALLOWED("startLoadingMainResource: Returning substitute data (frame = %p, main = %d)", m_frame, m_frame->isMainFrame());
    679675    m_identifierForLoadWithoutResourceLoader = m_frame->page()->progress().createUniqueIdentifier();
    680676    frameLoader()->notifier().assignIdentifierToInitialRequest(m_identifierForLoadWithoutResourceLoader, this, m_request);
    681677    frameLoader()->notifier().dispatchWillSendRequest(this, m_identifierForLoadWithoutResourceLoader, m_request, ResourceResponse());
    682     handleSubstituteDataLoadSoon();
     678
     679    if (!m_deferMainResourceDataLoad || frameLoader()->loadsSynchronously())
     680        handleSubstituteDataLoadNow();
     681    else
     682        startDataLoadTimer();
     683
    683684    return true;
    684685}
     
    17291730
    17301731            m_serviceWorkerRegistrationData = WTFMove(registrationData);
     1732
     1733            // Prefer existing substitute data (from WKWebView.loadData etc) over service worker fetch.
     1734            if (this->tryLoadingSubstituteData())
     1735                return;
     1736            // Try app cache only if there is no service worker.
    17311737            if (!m_serviceWorkerRegistrationData && this->tryLoadingRequestFromApplicationCache())
    17321738                return;
  • trunk/Source/WebCore/loader/DocumentLoader.h

    r232419 r232580  
    378378
    379379    bool tryLoadingRequestFromApplicationCache();
     380    bool tryLoadingSubstituteData();
    380381    bool tryLoadingRedirectRequestFromApplicationCache(const ResourceRequest&);
    381382#if ENABLE(SERVICE_WORKER)
     
    392393    typedef Timer DocumentLoaderTimer;
    393394#endif
    394     void handleSubstituteDataLoadSoon();
    395395    void handleSubstituteDataLoadNow();
    396396    void startDataLoadTimer();
  • trunk/Tools/TestWebKitAPI/Tests/WebKitCocoa/ServiceWorkerBasic.mm

    r229927 r232580  
    13711371}
    13721372
     1373TEST(ServiceWorkers, LoadData)
     1374{
     1375    ASSERT(mainBytes);
     1376    ASSERT(scriptBytes);
     1377
     1378    [WKWebsiteDataStore _allowWebsiteDataRecordsForAllOrigins];
     1379
     1380    // Start with a clean slate data store
     1381    [[WKWebsiteDataStore defaultDataStore] removeDataOfTypes:[WKWebsiteDataStore allWebsiteDataTypes] modifiedSince:[NSDate distantPast] completionHandler:^() {
     1382        done = true;
     1383    }];
     1384    TestWebKitAPI::Util::run(&done);
     1385    done = false;
     1386
     1387    RetainPtr<WKWebViewConfiguration> configuration = adoptNS([[WKWebViewConfiguration alloc] init]);
     1388
     1389    RetainPtr<SWMessageHandler> messageHandler = adoptNS([[SWMessageHandler alloc] init]);
     1390    [[configuration userContentController] addScriptMessageHandler:messageHandler.get() name:@"sw"];
     1391
     1392    RetainPtr<SWSchemes> handler = adoptNS([[SWSchemes alloc] init]);
     1393    handler->resources.set("sw://host/main.html", ResourceInfo { @"text/html", mainBytes });
     1394    handler->resources.set("sw://host/sw.js", ResourceInfo { @"application/javascript", scriptBytes });
     1395    [configuration setURLSchemeHandler:handler.get() forURLScheme:@"SW"];
     1396
     1397    RetainPtr<WKWebView> webView = adoptNS([[WKWebView alloc] initWithFrame:NSMakeRect(0, 0, 800, 600) configuration:configuration.get()]);
     1398    [webView.get().configuration.processPool _registerURLSchemeServiceWorkersCanHandle:@"sw"];
     1399
     1400    auto delegate = adoptNS([[TestSWAsyncNavigationDelegate alloc] init]);
     1401    [webView setNavigationDelegate:delegate.get()];
     1402    [webView setUIDelegate:delegate.get()];
     1403
     1404    done = false;
     1405
     1406    // Normal load to get SW registered.
     1407    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"sw://host/main.html"]];
     1408    [webView loadRequest:request];
     1409
     1410    TestWebKitAPI::Util::run(&done);
     1411    done = false;
     1412
     1413    // Now try a data load.
     1414    NSData *data = [NSData dataWithBytes:mainBytes length:strlen(mainBytes)];
     1415    [webView loadData:data MIMEType:@"text/html" characterEncodingName:@"UTF-8" baseURL:[NSURL URLWithString:@"sw://host/main.html"]];
     1416
     1417    TestWebKitAPI::Util::run(&done);
     1418    done = false;
     1419}
     1420
    13731421#endif // WK_API_ENABLED
Note: See TracChangeset for help on using the changeset viewer.