Changeset 288201 in webkit


Ignore:
Timestamp:
Jan 19, 2022 9:12:35 AM (6 months ago)
Author:
youenn@apple.com
Message:

Improve computation of service worker FetchEvent.resultingClientId
https://bugs.webkit.org/show_bug.cgi?id=235107

Reviewed by Darin Adler.

LayoutTests/imported/w3c:

  • web-platform-tests/service-workers/service-worker/clients-get-resultingClientId.https-expected.txt:
  • web-platform-tests/service-workers/service-worker/clients-get.https-expected.txt:
  • web-platform-tests/service-workers/service-worker/historical.https.any.serviceworker-expected.txt:

Source/WebCore:

We are now passing the reserved client ID generated by DocumentLoader to the generated document.
Previously, we would register a temporary ID that we would drop later on when creating a new document.
This allows to be closer to spec, though we need to handle the case of a document whose origin gets unique
due to CSP or sandboxing making a document origin unique.
We are also removing targetClientId, which was renamed in the spec to replacesClientId but is also not implemented in any browser yet.

Covered by existing tests.

  • dom/DOMImplementation.cpp:
  • dom/DOMImplementation.h:
  • dom/Document.cpp:
  • dom/Document.h:
  • dom/ScriptExecutionContext.cpp:
  • dom/ScriptExecutionContext.h:
  • html/FTPDirectoryDocument.cpp:
  • html/HTMLDocument.cpp:
  • html/HTMLDocument.h:
  • html/ImageDocument.cpp:
  • html/MediaDocument.cpp:
  • html/ModelDocument.cpp:
  • html/PluginDocument.cpp:
  • html/TextDocument.cpp:
  • html/TextDocument.h:
  • inspector/DOMPatchSupport.cpp:
  • loader/DocumentLoader.cpp:
  • loader/DocumentLoader.h:
  • loader/DocumentWriter.cpp:
  • loader/DocumentWriter.h:
  • loader/SinkDocument.cpp:
  • workers/service/FetchEvent.cpp:
  • workers/service/FetchEvent.h:
  • workers/service/FetchEvent.idl:
  • workers/service/server/SWServer.cpp:
  • xml/XMLHttpRequest.cpp:

Source/WebKit:

  • WebProcess/Network/WebLoaderStrategy.cpp:
  • WebProcess/Storage/WebSWClientConnection.cpp:
Location:
trunk
Files:
36 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/imported/w3c/ChangeLog

    r288197 r288201  
     12022-01-19  Youenn Fablet  <youenn@apple.com>
     2
     3        Improve computation of service worker FetchEvent.resultingClientId
     4        https://bugs.webkit.org/show_bug.cgi?id=235107
     5
     6        Reviewed by Darin Adler.
     7
     8        * web-platform-tests/service-workers/service-worker/clients-get-resultingClientId.https-expected.txt:
     9        * web-platform-tests/service-workers/service-worker/clients-get.https-expected.txt:
     10        * web-platform-tests/service-workers/service-worker/historical.https.any.serviceworker-expected.txt:
     11
    1122022-01-19  Alexey Shvayka  <ashvayka@apple.com>
    213
  • trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/clients-get-resultingClientId.https-expected.txt

    r267647 r288201  
    11
    22PASS global setup
    3 FAIL get(resultingClientId) for same-origin document assert_equals: promiseValue expected "client" but got "undefinedValue"
     3PASS get(resultingClientId) for same-origin document
    44PASS get(resultingClientId) on cross-origin redirect
    5 PASS get(resultingClientId) for document sandboxed by CSP header
    6 FAIL get(resultingClientId) for document sandboxed by CSP header with allow-same-origin assert_equals: promiseValue expected "client" but got "undefinedValue"
     5FAIL get(resultingClientId) for document sandboxed by CSP header assert_equals: promiseValue expected "undefinedValue" but got "client"
     6PASS get(resultingClientId) for document sandboxed by CSP header with allow-same-origin
    77PASS global cleanup
    88
  • trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/clients-get.https-expected.txt

    r267647 r288201  
    11
    22FAIL Test Clients.get() assert_array_equals: expected property 1 to be false but got true (expected array ["visible", false, "https://localhost:9443/service-workers/service-worker/resources/clients-get-frame.html#2", "window", "nested"] got ["visible", true, "https://localhost:9443/service-workers/service-worker/resources/clients-get-frame.html#2", "window", "nested"])
    3 FAIL Test successful Clients.get(FetchEvent.resultingClientId) assert_false: Clients.get(FetchEvent.resultingClientId) resolved with a Client expected false got true
     3PASS Test successful Clients.get(FetchEvent.resultingClientId)
    44PASS Test unsuccessful Clients.get(FetchEvent.resultingClientId)
    55
  • trunk/LayoutTests/imported/w3c/web-platform-tests/service-workers/service-worker/historical.https.any.serviceworker-expected.txt

    r279389 r288201  
    11
    2 FAIL targetClientId should not be on FetchEvent assert_false: expected false got true
     2PASS targetClientId should not be on FetchEvent
    33
  • trunk/Source/WTF/wtf/UUID.h

    r288119 r288201  
    8686    WTF_EXPORT_PRIVATE String toString() const;
    8787
     88    operator bool() const { return !!m_data; }
     89
    8890private:
    8991    WTF_EXPORT_PRIVATE UUID();
  • trunk/Source/WebCore/ChangeLog

    r288200 r288201  
     12022-01-19  Youenn Fablet  <youenn@apple.com>
     2
     3        Improve computation of service worker FetchEvent.resultingClientId
     4        https://bugs.webkit.org/show_bug.cgi?id=235107
     5
     6        Reviewed by Darin Adler.
     7
     8        We are now passing the reserved client ID generated by DocumentLoader to the generated document.
     9        Previously, we would register a temporary ID that we would drop later on when creating a new document.
     10        This allows to be closer to spec, though we need to handle the case of a document whose origin gets unique
     11        due to CSP or sandboxing making a document origin unique.
     12        We are also removing targetClientId, which was renamed in the spec to replacesClientId but is also not implemented in any browser yet.
     13
     14        Covered by existing tests.
     15
     16        * dom/DOMImplementation.cpp:
     17        * dom/DOMImplementation.h:
     18        * dom/Document.cpp:
     19        * dom/Document.h:
     20        * dom/ScriptExecutionContext.cpp:
     21        * dom/ScriptExecutionContext.h:
     22        * html/FTPDirectoryDocument.cpp:
     23        * html/HTMLDocument.cpp:
     24        * html/HTMLDocument.h:
     25        * html/ImageDocument.cpp:
     26        * html/MediaDocument.cpp:
     27        * html/ModelDocument.cpp:
     28        * html/PluginDocument.cpp:
     29        * html/TextDocument.cpp:
     30        * html/TextDocument.h:
     31        * inspector/DOMPatchSupport.cpp:
     32        * loader/DocumentLoader.cpp:
     33        * loader/DocumentLoader.h:
     34        * loader/DocumentWriter.cpp:
     35        * loader/DocumentWriter.h:
     36        * loader/SinkDocument.cpp:
     37        * workers/service/FetchEvent.cpp:
     38        * workers/service/FetchEvent.h:
     39        * workers/service/FetchEvent.idl:
     40        * workers/service/server/SWServer.cpp:
     41        * xml/XMLHttpRequest.cpp:
     42
    1432022-01-19  Michael Catanzaro  <mcatanzaro@gnome.org>
    244
  • trunk/Source/WebCore/dom/DOMImplementation.cpp

    r285936 r288201  
    125125Ref<HTMLDocument> DOMImplementation::createHTMLDocument(const String& title)
    126126{
    127     auto document = HTMLDocument::create(nullptr, m_document.settings(), URL());
     127    auto document = HTMLDocument::create(nullptr, m_document.settings(), URL(), { });
    128128    document->open();
    129129    document->write(nullptr, { "<!doctype html><html><head></head><body></body></html>"_s });
     
    139139}
    140140
    141 Ref<Document> DOMImplementation::createDocument(const String& contentType, Frame* frame, const Settings& settings, const URL& url)
     141Ref<Document> DOMImplementation::createDocument(const String& contentType, Frame* frame, const Settings& settings, const URL& url, ScriptExecutionContextIdentifier documentIdentifier)
    142142{
    143143    // FIXME: Inelegant to have this here just because this is the home of DOM APIs for creating documents.
     
    147147    // Plug-ins cannot take over for HTML, XHTML, plain text, or non-PDF images.
    148148    if (equalLettersIgnoringASCIICase(contentType, "text/html"))
    149         return HTMLDocument::create(frame, settings, url);
     149        return HTMLDocument::create(frame, settings, url, documentIdentifier);
    150150    if (equalLettersIgnoringASCIICase(contentType, "application/xhtml+xml"))
    151151        return XMLDocument::createXHTML(frame, settings, url);
    152152    if (equalLettersIgnoringASCIICase(contentType, "text/plain"))
    153         return TextDocument::create(frame, settings, url);
     153        return TextDocument::create(frame, settings, url, documentIdentifier);
    154154    bool isImage = MIMETypeRegistry::isSupportedImageMIMEType(contentType);
    155155    if (frame && isImage && !MIMETypeRegistry::isPDFOrPostScriptMIMEType(contentType))
     
    194194        return ImageDocument::create(*frame, url);
    195195    if (MIMETypeRegistry::isTextMIMEType(contentType))
    196         return TextDocument::create(frame, settings, url);
     196        return TextDocument::create(frame, settings, url, documentIdentifier);
    197197    if (equalLettersIgnoringASCIICase(contentType, "image/svg+xml"))
    198198        return SVGDocument::create(frame, settings, url);
     
    202202        return document;
    203203    }
    204     return HTMLDocument::create(frame, settings, url);
    205 }
    206 
    207 }
     204
     205    return HTMLDocument::create(frame, settings, url, documentIdentifier);
     206}
     207
     208}
  • trunk/Source/WebCore/dom/DOMImplementation.h

    r268114 r288201  
    2525
    2626#include "ExceptionOr.h"
     27#include "ScriptExecutionContextIdentifier.h"
    2728#include "XMLDocument.h"
    2829
     
    4445    WEBCORE_EXPORT static Ref<CSSStyleSheet> createCSSStyleSheet(const String& title, const String& media);
    4546
    46     static Ref<Document> createDocument(const String& contentType, Frame*, const Settings&, const URL&);
     47    static Ref<Document> createDocument(const String& contentType, Frame*, const Settings&, const URL&, ScriptExecutionContextIdentifier = { });
    4748
    4849private:
  • trunk/Source/WebCore/dom/Document.cpp

    r288162 r288201  
    606606}
    607607
    608 Document::Document(Frame* frame, const Settings& settings, const URL& url, DocumentClasses documentClasses, unsigned constructionFlags)
     608Document::Document(Frame* frame, const Settings& settings, const URL& url, DocumentClasses documentClasses, unsigned constructionFlags, ScriptExecutionContextIdentifier identifier)
    609609    : ContainerNode(*this, CreateDocument)
    610610    , TreeScope(*this)
     611    , ScriptExecutionContext(identifier)
    611612    , FrameDestructionObserver(frame)
    612613    , m_settings(settings)
     
    75137514
    75147515    if (isHTMLDocument())
    7515         m_templateDocument = HTMLDocument::create(nullptr, m_settings, aboutBlankURL());
     7516        m_templateDocument = HTMLDocument::create(nullptr, m_settings, aboutBlankURL(), { });
    75167517    else
    75177518        m_templateDocument = create(m_settings, aboutBlankURL());
  • trunk/Source/WebCore/dom/Document.h

    r288087 r288201  
    16771677protected:
    16781678    enum ConstructionFlags { Synthesized = 1, NonRenderedPlaceholder = 1 << 1 };
    1679     WEBCORE_EXPORT Document(Frame*, const Settings&, const URL&, DocumentClasses = { }, unsigned constructionFlags = 0);
     1679    WEBCORE_EXPORT Document(Frame*, const Settings&, const URL&, DocumentClasses = { }, unsigned constructionFlags = 0, ScriptExecutionContextIdentifier = { });
    16801680
    16811681    void clearXMLVersion() { m_xmlVersion = String(); }
  • trunk/Source/WebCore/dom/ScriptExecutionContext.cpp

    r288093 r288201  
    112112};
    113113
    114 ScriptExecutionContext::ScriptExecutionContext()
    115     : m_identifier(ScriptExecutionContextIdentifier::generate())
     114ScriptExecutionContext::ScriptExecutionContext(ScriptExecutionContextIdentifier contextIdentifier)
     115    : m_identifier(contextIdentifier ? contextIdentifier : ScriptExecutionContextIdentifier::generate())
    116116{
    117117    Locker locker { allScriptExecutionContextsMapLock };
     118    ASSERT(!allScriptExecutionContextsMap().contains(m_identifier));
    118119    allScriptExecutionContextsMap().add(m_identifier, this);
    119120}
  • trunk/Source/WebCore/dom/ScriptExecutionContext.h

    r286625 r288201  
    9090class ScriptExecutionContext : public SecurityContext, public CanMakeWeakPtr<ScriptExecutionContext> {
    9191public:
    92     ScriptExecutionContext();
     92    explicit ScriptExecutionContext(ScriptExecutionContextIdentifier = { });
    9393    virtual ~ScriptExecutionContext();
    9494
  • trunk/Source/WebCore/html/FTPDirectoryDocument.cpp

    r287021 r288201  
    419419
    420420FTPDirectoryDocument::FTPDirectoryDocument(Frame* frame, const Settings& settings, const URL& url)
    421     : HTMLDocument(frame, settings, url)
     421    : HTMLDocument(frame, settings, url, { })
    422422{
    423423#if !LOG_DISABLED
  • trunk/Source/WebCore/html/HTMLDocument.cpp

    r287232 r288201  
    9090Ref<HTMLDocument> HTMLDocument::createSynthesizedDocument(Frame& frame, const URL& url)
    9191{
    92     return adoptRef(*new HTMLDocument(&frame, frame.settings(), url, { DocumentClass::HTML }, Synthesized));
    93 }
    94 
    95 HTMLDocument::HTMLDocument(Frame* frame, const Settings& settings, const URL& url, DocumentClasses documentClasses, unsigned constructionFlags)
    96     : Document(frame, settings, url, documentClasses | DocumentClasses(DocumentClass::HTML), constructionFlags)
     92    return adoptRef(*new HTMLDocument(&frame, frame.settings(), url, { }, { DocumentClass::HTML }, Synthesized));
     93}
     94
     95HTMLDocument::HTMLDocument(Frame* frame, const Settings& settings, const URL& url, ScriptExecutionContextIdentifier documentIdentifier, DocumentClasses documentClasses, unsigned constructionFlags)
     96    : Document(frame, settings, url, documentClasses | DocumentClasses(DocumentClass::HTML), constructionFlags, documentIdentifier)
    9797{
    9898    clearXMLVersion();
  • trunk/Source/WebCore/html/HTMLDocument.h

    r286151 r288201  
    3030    WTF_MAKE_ISO_ALLOCATED(HTMLDocument);
    3131public:
    32     static Ref<HTMLDocument> create(Frame*, const Settings&, const URL&);
     32    static Ref<HTMLDocument> create(Frame*, const Settings&, const URL&, ScriptExecutionContextIdentifier = { });
    3333    static Ref<HTMLDocument> createSynthesizedDocument(Frame&, const URL&);
    3434    virtual ~HTMLDocument();
     
    5555
    5656protected:
    57     HTMLDocument(Frame*, const Settings&, const URL&, DocumentClasses = { }, unsigned constructionFlags = 0);
     57    HTMLDocument(Frame*, const Settings&, const URL&, ScriptExecutionContextIdentifier, DocumentClasses = { }, unsigned constructionFlags = 0);
    5858
    5959private:
     
    6666};
    6767
    68 inline Ref<HTMLDocument> HTMLDocument::create(Frame* frame, const Settings& settings, const URL& url)
     68inline Ref<HTMLDocument> HTMLDocument::create(Frame* frame, const Settings& settings, const URL& url, ScriptExecutionContextIdentifier identifier)
    6969{
    70     return adoptRef(*new HTMLDocument(frame, settings, url, { DocumentClass::HTML }));
     70    return adoptRef(*new HTMLDocument(frame, settings, url, identifier, { DocumentClass::HTML }, 0));
    7171}
    7272
  • trunk/Source/WebCore/html/ImageDocument.cpp

    r287021 r288201  
    199199
    200200ImageDocument::ImageDocument(Frame& frame, const URL& url)
    201     : HTMLDocument(&frame, frame.settings(), url, { DocumentClass::Image })
     201    : HTMLDocument(&frame, frame.settings(), url, { }, { DocumentClass::Image })
    202202    , m_imageElement(nullptr)
    203203    , m_imageSizeIsKnown(false)
  • trunk/Source/WebCore/html/MediaDocument.cpp

    r286151 r288201  
    141141   
    142142MediaDocument::MediaDocument(Frame* frame, const Settings& settings, const URL& url)
    143     : HTMLDocument(frame, settings, url, { DocumentClass::Media })
     143    : HTMLDocument(frame, settings, url, { }, { DocumentClass::Media })
    144144{
    145145    setCompatibilityMode(DocumentCompatibilityMode::QuirksMode);
  • trunk/Source/WebCore/html/ModelDocument.cpp

    r286151 r288201  
    139139
    140140ModelDocument::ModelDocument(Frame* frame, const Settings& settings, const URL& url)
    141     : HTMLDocument(frame, settings, url, { DocumentClass::Model })
     141    : HTMLDocument(frame, settings, url, { }, { DocumentClass::Model })
    142142{
    143143    if (frame)
  • trunk/Source/WebCore/html/PluginDocument.cpp

    r286151 r288201  
    145145
    146146PluginDocument::PluginDocument(Frame& frame, const URL& url)
    147     : HTMLDocument(&frame, frame.settings(), url, { DocumentClass::Plugin })
     147    : HTMLDocument(&frame, frame.settings(), url, { }, { DocumentClass::Plugin })
    148148{
    149149    setCompatibilityMode(DocumentCompatibilityMode::QuirksMode);
  • trunk/Source/WebCore/html/TextDocument.cpp

    r286151 r288201  
    3333WTF_MAKE_ISO_ALLOCATED_IMPL(TextDocument);
    3434
    35 TextDocument::TextDocument(Frame* frame, const Settings& settings, const URL& url)
    36     : HTMLDocument(frame, settings, url, { DocumentClass::Text })
     35TextDocument::TextDocument(Frame* frame, const Settings& settings, const URL& url, ScriptExecutionContextIdentifier identifier)
     36    : HTMLDocument(frame, settings, url, identifier, { DocumentClass::Text })
    3737{
    3838    setCompatibilityMode(DocumentCompatibilityMode::QuirksMode);
  • trunk/Source/WebCore/html/TextDocument.h

    r268114 r288201  
    3232    WTF_MAKE_ISO_ALLOCATED(TextDocument);
    3333public:
    34     static Ref<TextDocument> create(Frame* frame, const Settings& settings, const URL& url)
     34    static Ref<TextDocument> create(Frame* frame, const Settings& settings, const URL& url, ScriptExecutionContextIdentifier identifier)
    3535    {
    36         return adoptRef(*new TextDocument(frame, settings, url));
     36        return adoptRef(*new TextDocument(frame, settings, url, identifier));
    3737    }
    3838
    3939private:
    40     TextDocument(Frame*, const Settings&, const URL&);
     40    TextDocument(Frame*, const Settings&, const URL&, ScriptExecutionContextIdentifier);
    4141   
    4242    Ref<DocumentParser> createParser() override;
  • trunk/Source/WebCore/inspector/DOMPatchSupport.cpp

    r285195 r288201  
    7878    RefPtr<Document> newDocument;
    7979    if (m_document.isHTMLDocument())
    80         newDocument = HTMLDocument::create(nullptr, m_document.settings(), URL());
     80        newDocument = HTMLDocument::create(nullptr, m_document.settings(), URL(), { });
    8181    else if (m_document.isXHTMLDocument())
    8282        newDocument = XMLDocument::createXHTML(nullptr, m_document.settings(), URL());
  • trunk/Source/WebCore/loader/DocumentLoader.cpp

    r288093 r288201  
    151151}
    152152
    153 static HashMap<ScriptExecutionContextIdentifier, DocumentLoader*>& temporaryIdentifierToLoaderMap()
     153static HashMap<ScriptExecutionContextIdentifier, DocumentLoader*>& scriptExecutionContextIdentifierToLoaderMap()
    154154{
    155155    static NeverDestroyed<HashMap<ScriptExecutionContextIdentifier, DocumentLoader*>> map;
     
    157157}
    158158
    159 DocumentLoader* DocumentLoader::fromTemporaryDocumentIdentifier(ScriptExecutionContextIdentifier identifier)
    160 {
    161     return temporaryIdentifierToLoaderMap().get(identifier);
     159DocumentLoader* DocumentLoader::fromScriptExecutionContextIdentifier(ScriptExecutionContextIdentifier identifier)
     160{
     161    return scriptExecutionContextIdentifierToLoaderMap().get(identifier);
    162162}
    163163
     
    201201
    202202#if ENABLE(SERVICE_WORKER)
    203     if (m_temporaryServiceWorkerClient) {
    204         ASSERT(temporaryIdentifierToLoaderMap().contains(*m_temporaryServiceWorkerClient));
    205         temporaryIdentifierToLoaderMap().remove(*m_temporaryServiceWorkerClient);
     203    if (m_resultingClientId) {
     204        ASSERT(scriptExecutionContextIdentifierToLoaderMap().contains(m_resultingClientId));
     205        scriptExecutionContextIdentifierToLoaderMap().remove(m_resultingClientId);
    206206    }
    207207#endif
     
    556556}
    557557
     558ScriptExecutionContextIdentifier DocumentLoader::resultingClientId() const
     559{
     560    return m_resultingClientId;
     561}
     562
    558563void DocumentLoader::matchRegistration(const URL& url, SWClientConnection::RegistrationCallback&& callback)
    559564{
     
    590595    if (m_serviceWorkerRegistrationData) {
    591596        m_serviceWorkerRegistrationData = { };
    592         unregisterTemporaryServiceWorkerClient();
    593     }
     597        unregisterReservedServiceWorkerClient();
     598    }
     599
    594600    willSendRequest(WTFMove(request), redirectResponse, [completionHandler = WTFMove(completionHandler), protectedThis = Ref { *this }, this] (auto&& request) mutable {
    595601        ASSERT(!m_substituteData.isValid());
     
    12031209    if (!m_gotFirstByte) {
    12041210        m_gotFirstByte = true;
    1205         bool hasBegun = m_writer.begin(documentURL(), false);
     1211        bool hasBegun = m_writer.begin(documentURL(), false, nullptr, m_resultingClientId);
    12061212        if (!hasBegun)
    12071213            return;
     
    12311237#if ENABLE(SERVICE_WORKER)
    12321238        if (RuntimeEnabledFeatures::sharedFeatures().serviceWorkerEnabled()) {
    1233             if (m_serviceWorkerRegistrationData && m_serviceWorkerRegistrationData->activeWorker) {
    1234                 document.setActiveServiceWorker(ServiceWorker::getOrCreate(document, WTFMove(m_serviceWorkerRegistrationData->activeWorker.value())));
    1235                 m_serviceWorkerRegistrationData = { };
    1236             } else if (auto* parent = document.parentDocument()) {
    1237                 if (shouldUseActiveServiceWorkerFromParent(document, *parent))
    1238                     document.setActiveServiceWorker(parent->activeServiceWorker());
     1239            if (!document.securityOrigin().isUnique()) {
     1240                if (m_serviceWorkerRegistrationData && m_serviceWorkerRegistrationData->activeWorker) {
     1241                    document.setActiveServiceWorker(ServiceWorker::getOrCreate(document, WTFMove(m_serviceWorkerRegistrationData->activeWorker.value())));
     1242                    m_serviceWorkerRegistrationData = { };
     1243                } else if (auto* parent = document.parentDocument()) {
     1244                    if (shouldUseActiveServiceWorkerFromParent(document, *parent))
     1245                        document.setActiveServiceWorker(parent->activeServiceWorker());
     1246                }
    12391247            }
    12401248
     
    12421250                document.setServiceWorkerConnection(&ServiceWorkerProvider::singleton().serviceWorkerConnection());
    12431251
    1244             // We currently unregister the temporary service worker client since we now registered the real document.
    1245             // FIXME: We should make the real document use the temporary client identifier.
    1246             unregisterTemporaryServiceWorkerClient();
     1252            if (m_resultingClientId) {
     1253                if (m_resultingClientId != document.identifier())
     1254                    unregisterReservedServiceWorkerClient();
     1255                scriptExecutionContextIdentifierToLoaderMap().remove(m_resultingClientId);
     1256                m_resultingClientId = { };
     1257            }
    12471258        }
    12481259#endif
     
    20672078}
    20682079
    2069 void DocumentLoader::unregisterTemporaryServiceWorkerClient()
     2080void DocumentLoader::unregisterReservedServiceWorkerClient()
    20702081{
    20712082#if ENABLE(SERVICE_WORKER)
    2072     if (!m_temporaryServiceWorkerClient || !RuntimeEnabledFeatures::sharedFeatures().serviceWorkerEnabled())
     2083    if (!m_resultingClientId || !RuntimeEnabledFeatures::sharedFeatures().serviceWorkerEnabled())
    20732084        return;
    20742085
    20752086    auto& serviceWorkerConnection = ServiceWorkerProvider::singleton().serviceWorkerConnection();
    2076     serviceWorkerConnection.unregisterServiceWorkerClient(*m_temporaryServiceWorkerClient);
     2087    serviceWorkerConnection.unregisterServiceWorkerClient(m_resultingClientId);
    20772088#endif
    20782089}
     
    20932104        DefersLoadingPolicy::AllowDefersLoading,
    20942105        CachingPolicy::AllowCaching);
     2106
    20952107#if ENABLE(SERVICE_WORKER)
    2096     if (!m_temporaryServiceWorkerClient) {
    2097         // The main navigation load will trigger the registration of the temp client.
    2098         m_temporaryServiceWorkerClient = ScriptExecutionContextIdentifier::generate();
    2099         ASSERT(!temporaryIdentifierToLoaderMap().contains(*m_temporaryServiceWorkerClient));
    2100         temporaryIdentifierToLoaderMap().add(*m_temporaryServiceWorkerClient, this);
    2101     }
    2102     mainResourceLoadOptions.clientIdentifier = m_temporaryServiceWorkerClient;
    2103 #endif
     2108    // The main navigation load will trigger the registration of the client.
     2109    if (m_resultingClientId)
     2110        scriptExecutionContextIdentifierToLoaderMap().remove(m_resultingClientId);
     2111    m_resultingClientId = ScriptExecutionContextIdentifier::generate();
     2112    ASSERT(!scriptExecutionContextIdentifierToLoaderMap().contains(m_resultingClientId));
     2113    scriptExecutionContextIdentifierToLoaderMap().add(m_resultingClientId, this);
     2114#endif
     2115
    21042116    CachedResourceRequest mainResourceRequest(WTFMove(request), mainResourceLoadOptions);
    21052117    if (!m_frame->isMainFrame() && m_frame->document()) {
     
    22132225    m_isContinuingLoadAfterProvisionalLoadStarted = false;
    22142226
    2215     unregisterTemporaryServiceWorkerClient();
     2227    unregisterReservedServiceWorkerClient();
    22162228}
    22172229
  • trunk/Source/WebCore/loader/DocumentLoader.h

    r287684 r288201  
    172172    }
    173173
    174     WEBCORE_EXPORT static DocumentLoader* fromTemporaryDocumentIdentifier(ScriptExecutionContextIdentifier);
     174    WEBCORE_EXPORT static DocumentLoader* fromScriptExecutionContextIdentifier(ScriptExecutionContextIdentifier);
    175175
    176176    WEBCORE_EXPORT virtual ~DocumentLoader();
     
    440440#if ENABLE(SERVICE_WORKER)
    441441    WEBCORE_EXPORT bool setControllingServiceWorkerRegistration(ServiceWorkerRegistrationData&&);
     442    WEBCORE_EXPORT ScriptExecutionContextIdentifier resultingClientId() const;
    442443#endif
    443444
     
    469470    void matchRegistration(const URL&, CompletionHandler<void(std::optional<ServiceWorkerRegistrationData>&&)>&&);
    470471#endif
    471     void unregisterTemporaryServiceWorkerClient();
     472    void unregisterReservedServiceWorkerClient();
    472473
    473474    std::optional<CrossOriginOpenerPolicyEnforcementResult> doCrossOriginOpenerHandlingOfResponse(const ResourceResponse&);
     
    689690#if ENABLE(SERVICE_WORKER)
    690691    std::optional<ServiceWorkerRegistrationData> m_serviceWorkerRegistrationData;
    691     std::optional<ScriptExecutionContextIdentifier> m_temporaryServiceWorkerClient;
    692 #endif
     692#endif
     693    ScriptExecutionContextIdentifier m_resultingClientId;
    693694
    694695#if ASSERT_ENABLED
  • trunk/Source/WebCore/loader/DocumentWriter.cpp

    r287684 r288201  
    113113}
    114114
    115 Ref<Document> DocumentWriter::createDocument(const URL& url)
     115Ref<Document> DocumentWriter::createDocument(const URL& url, ScriptExecutionContextIdentifier documentIdentifier)
    116116{
    117117    if (!m_frame->loader().stateMachine().isDisplayingInitialEmptyDocument() && m_frame->loader().client().shouldAlwaysUsePluginDocument(m_mimeType))
     
    123123    if (!m_frame->loader().client().hasHTMLView())
    124124        return Document::createNonRenderedPlaceholder(*m_frame, url);
    125     return DOMImplementation::createDocument(m_mimeType, m_frame.get(), m_frame->settings(), url);
    126 }
    127 
    128 bool DocumentWriter::begin(const URL& urlReference, bool dispatch, Document* ownerDocument)
     125    return DOMImplementation::createDocument(m_mimeType, m_frame.get(), m_frame->settings(), url, documentIdentifier);
     126}
     127
     128bool DocumentWriter::begin(const URL& urlReference, bool dispatch, Document* ownerDocument, ScriptExecutionContextIdentifier documentIdentifier)
    129129{
    130130    // We grab a local copy of the URL because it's easy for callers to supply
     
    135135    // Create a new document before clearing the frame, because it may need to
    136136    // inherit an aliased security context.
    137     Ref<Document> document = createDocument(url);
     137    Ref<Document> document = createDocument(url, documentIdentifier);
    138138   
    139139    // If the new document is for a Plugin but we're supposed to be sandboxed from Plugins,
  • trunk/Source/WebCore/loader/DocumentWriter.h

    r287684 r288201  
    2929#pragma once
    3030
     31#include "ScriptExecutionContextIdentifier.h"
    3132#include <wtf/WeakPtr.h>
    3233#include <wtf/text/WTFString.h>
     
    4849
    4950    bool begin();
    50     bool begin(const URL&, bool dispatchWindowObjectAvailable = true, Document* ownerDocument = nullptr);
     51    bool begin(const URL&, bool dispatchWindowObjectAvailable = true, Document* ownerDocument = nullptr, ScriptExecutionContextIdentifier = { });
    5152    void addData(const SharedBuffer&);
    5253    void insertDataSynchronously(const String&); // For an internal use only to prevent the parser from yielding.
     
    6869
    6970private:
    70     Ref<Document> createDocument(const URL&);
     71    Ref<Document> createDocument(const URL&, ScriptExecutionContextIdentifier);
    7172    void clear();
    7273
  • trunk/Source/WebCore/loader/SinkDocument.cpp

    r278532 r288201  
    5555
    5656SinkDocument::SinkDocument(Frame& frame, const URL& url)
    57     : HTMLDocument(&frame, frame.settings(), url)
     57    : HTMLDocument(&frame, frame.settings(), url, { })
    5858{
    5959    setCompatibilityMode(DocumentCompatibilityMode::QuirksMode);
  • trunk/Source/WebCore/platform/ScriptExecutionContextIdentifier.h

    r288116 r288201  
    5454    }
    5555
    56     operator bool() const { return !!m_processIdentifier; }
     56    operator bool() const { return !!m_object; }
    5757
    5858    const UUID& object() const { return m_object; }
  • trunk/Source/WebCore/workers/service/FetchEvent.cpp

    r288093 r288201  
    6363    , m_clientId(WTFMove(initializer.clientId))
    6464    , m_resultingClientId(WTFMove(initializer.resultingClientId))
    65     , m_targetClientId(WTFMove(initializer.targetClientId))
    6665    , m_handled(retrieveHandledPromise(globalObject, WTFMove(initializer.handled)))
    6766{
  • trunk/Source/WebCore/workers/service/FetchEvent.h

    r288093 r288201  
    5151        String clientId;
    5252        String resultingClientId;
    53         String targetClientId;
    5453        RefPtr<DOMPromise> handled;
    5554    };
     
    7372    const String& clientId() const { return m_clientId; }
    7473    const String& resultingClientId() const { return m_resultingClientId; }
    75     const String& targetClientId() const { return m_targetClientId; }
    7674    DOMPromise& handled() const { return m_handled.get(); }
    7775
     
    9593    String m_clientId;
    9694    String m_resultingClientId;
    97     String m_targetClientId;
    9895
    9996    bool m_respondWithEntered { false };
  • trunk/Source/WebCore/workers/service/FetchEvent.idl

    r288093 r288201  
    3737    readonly attribute DOMString clientId;
    3838    readonly attribute DOMString resultingClientId;
    39     readonly attribute DOMString targetClientId;
    4039    readonly attribute Promise<undefined> handled;
    4140
     
    4746    DOMString clientId = "";
    4847    DOMString resultingClientId = "";
    49     DOMString targetClientId = "";
    5048    Promise<undefined> handled;
    5149};
  • trunk/Source/WebCore/workers/service/server/SWServer.cpp

    r288093 r288201  
    943943    }
    944944
    945     m_visibleClientIdToInternalClientIdMap.add(data.identifier.object().toString(), clientIdentifier);
     945    auto addResult = m_visibleClientIdToInternalClientIdMap.add(data.identifier.object().toString(), clientIdentifier);
     946    if (!addResult.isNewEntry) {
     947        ASSERT(m_visibleClientIdToInternalClientIdMap.get(data.identifier.object().toString()) == clientIdentifier);
     948        ASSERT(m_clientsById.contains(clientIdentifier));
     949        m_clientsById.set(clientIdentifier, WTFMove(data));
     950        return;
     951    }
    946952
    947953    ASSERT(!m_clientsById.contains(clientIdentifier));
  • trunk/Source/WebCore/xml/XMLHttpRequest.cpp

    r288088 r288201  
    182182        } else {
    183183            if (isHTML)
    184                 m_responseDocument = HTMLDocument::create(nullptr, context.settings(), m_response.url());
     184                m_responseDocument = HTMLDocument::create(nullptr, context.settings(), m_response.url(), { });
    185185            else
    186186                m_responseDocument = XMLDocument::create(nullptr, context.settings(), m_response.url());
  • trunk/Source/WebKit/ChangeLog

    r288185 r288201  
     12022-01-19  Youenn Fablet  <youenn@apple.com>
     2
     3        Improve computation of service worker FetchEvent.resultingClientId
     4        https://bugs.webkit.org/show_bug.cgi?id=235107
     5
     6        Reviewed by Darin Adler.
     7
     8        * WebProcess/Network/WebLoaderStrategy.cpp:
     9        * WebProcess/Storage/WebSWClientConnection.cpp:
     10
    1112022-01-19  Youenn Fablet  <youenn@apple.com>
    212
  • trunk/Source/WebKit/WebProcess/Network/WebLoaderStrategy.cpp

    r287731 r288201  
    442442            frameAncestorOrigins.append(&frame->document()->securityOrigin());
    443443        loadParameters.frameAncestorOrigins = WTFMove(frameAncestorOrigins);
     444
     445#if ENABLE(SERVICE_WORKER)
     446        if (auto* documentLoader = resourceLoader.documentLoader()) {
     447            if (auto resultingClientId = static_cast<WebDocumentLoader&>(*documentLoader).resultingClientId())
     448                loadParameters.options.clientIdentifier = resultingClientId;
     449        }
     450#endif
    444451    }
    445452
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp

    r286419 r288201  
    179179void WebSWClientConnection::setDocumentIsControlled(ScriptExecutionContextIdentifier documentIdentifier, ServiceWorkerRegistrationData&& data, CompletionHandler<void(bool)>&& completionHandler)
    180180{
    181     auto* documentLoader = DocumentLoader::fromTemporaryDocumentIdentifier(documentIdentifier);
     181    auto* documentLoader = DocumentLoader::fromScriptExecutionContextIdentifier(documentIdentifier);
    182182    bool result = documentLoader ? documentLoader->setControllingServiceWorkerRegistration(WTFMove(data)) : false;
    183183    completionHandler(result);
Note: See TracChangeset for help on using the changeset viewer.