Changeset 224113 in webkit


Ignore:
Timestamp:
Oct 27, 2017 9:35:45 AM (6 years ago)
Author:
Chris Dumez
Message:

Add initial support for serviceWorkerClient.postMessage()
https://bugs.webkit.org/show_bug.cgi?id=178794

Reviewed by Youenn Fablet.

Source/WebCore:

Add initial support for serviceWorkerClient.postMessage():

It is now possible to do bi-directional communication with a service worker
via postMessage().

No new tests, updated existing test.

  • WebCore.xcodeproj/project.pbxproj:
  • dom/Document.cpp:

(WebCore::generateDocumentIdentifier):
(WebCore::Document::allDocumentsMap):
(WebCore::Document::allDocuments):
(WebCore::m_identifier):
(WebCore::Document::~Document):

  • dom/Document.h:

(WebCore::Document::identifier const):

  • dom/ScriptExecutionContext.cpp:

(WebCore::ScriptExecutionContext::serviceWorkerContainer):

  • dom/ScriptExecutionContext.h:
  • workers/service/ServiceWorker.cpp:

(WebCore::ServiceWorker::postMessage):

  • workers/service/ServiceWorkerClient.cpp:

(WebCore::ServiceWorkerClient::ServiceWorkerClient):
(WebCore::ServiceWorkerClient::~ServiceWorkerClient):
(WebCore::ServiceWorkerClient::id const):
(WebCore::ServiceWorkerClient::postMessage):

  • workers/service/ServiceWorkerClient.h:

(WebCore::ServiceWorkerClient::create):

  • workers/service/ServiceWorkerClient.idl:
  • workers/service/ServiceWorkerClientIdentifier.h: Copied from Source/WebCore/workers/service/ServiceWorkerClient.idl.

(WebCore::ServiceWorkerClientIdentifier::toString const):

  • workers/service/ServiceWorkerRegistration.cpp:

(WebCore::ServiceWorkerRegistration::unregister):

  • workers/service/ServiceWorkerWindowClient.cpp:

(WebCore::ServiceWorkerWindowClient::ServiceWorkerWindowClient):

  • workers/service/ServiceWorkerWindowClient.h:
  • workers/service/context/SWContextManager.cpp:

(WebCore::SWContextManager::postMessageToServiceWorkerGlobalScope):

  • workers/service/context/SWContextManager.h:
  • workers/service/context/ServiceWorkerThread.cpp:

(WebCore::ServiceWorkerThread::postMessageToServiceWorkerGlobalScope):

  • workers/service/context/ServiceWorkerThread.h:
  • workers/service/server/SWClientConnection.cpp:

(WebCore::SWClientConnection::postMessageToServiceWorkerClient):

  • workers/service/server/SWClientConnection.h:

Source/WebKit:

Add initial support for serviceWorkerClient.postMessage():

It is now possible to do bi-directional communication with a service worker
via postMessage().

  • Shared/WebCoreArgumentCoders.cpp:

(IPC::ArgumentCoder<ServiceWorkerClientIdentifier>::encode):
(IPC::ArgumentCoder<ServiceWorkerClientIdentifier>::decode):

  • Shared/WebCoreArgumentCoders.h:
  • StorageProcess/ServiceWorker/WebSWServerConnection.cpp:

(WebKit::WebSWServerConnection::postMessageToServiceWorkerGlobalScope):
(WebKit::WebSWServerConnection::postMessageToServiceWorkerClient):

  • StorageProcess/ServiceWorker/WebSWServerConnection.h:
  • StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
  • StorageProcess/StorageProcess.cpp:

(WebKit::StorageProcess::postMessageToServiceWorkerClient):

  • StorageProcess/StorageProcess.h:
  • StorageProcess/StorageProcess.messages.in:
  • WebProcess/Storage/WebSWClientConnection.cpp:

(WebKit::WebSWClientConnection::postMessageToServiceWorkerGlobalScope):
(WebKit::WebSWClientConnection::postMessageToServiceWorkerClient):

  • WebProcess/Storage/WebSWClientConnection.h:
  • WebProcess/Storage/WebSWClientConnection.messages.in:
  • WebProcess/Storage/WebSWContextManagerConnection.cpp:

(WebKit::WebSWContextManagerConnection::postMessageToServiceWorkerGlobalScope):
(WebKit::WebSWContextManagerConnection::postMessageToServiceWorkerClient):

  • WebProcess/Storage/WebSWContextManagerConnection.h:
  • WebProcess/Storage/WebSWContextManagerConnection.messages.in:

LayoutTests:

Add layout test coverage.

  • http/tests/workers/service/basic-ServiceWorker-postMessage.https-expected.txt:
  • http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js:
  • http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js:

(then):

Location:
trunk
Files:
38 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r224112 r224113  
     12017-10-27  Chris Dumez  <cdumez@apple.com>
     2
     3        Add initial support for serviceWorkerClient.postMessage()
     4        https://bugs.webkit.org/show_bug.cgi?id=178794
     5
     6        Reviewed by Youenn Fablet.
     7
     8        Add layout test coverage.
     9
     10        * http/tests/workers/service/basic-ServiceWorker-postMessage.https-expected.txt:
     11        * http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js:
     12        * http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js:
     13        (then):
     14
    1152017-10-27  Ryan Haddad  <ryanhaddad@apple.com>
    216
  • trunk/LayoutTests/http/tests/workers/service/basic-ServiceWorker-postMessage.https-expected.txt

    r223922 r224113  
    1 PASS: Status code is 200
    2 PASS: Status text is Worker received the message event. messageEvent.origin was https://127.0.0.1:8443
    3 PASS: Source is Service worker
     1PASS: Client received message from service worker, origin: https://127.0.0.1:8443
     2PASS: Service worker received message 'Message 1' from origin 'https://127.0.0.1:8443'
     3PASS: Client received message from service worker, origin: https://127.0.0.1:8443
     4PASS: Service worker received message 'Message 2' from origin 'https://127.0.0.1:8443'
    45
  • trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage-worker.js

    r223922 r224113  
    1 let responseToSend = { status: 404, statusText: "Not Found" };
    2 
    31self.addEventListener("message", (event) => {
    4     responseToSend = event.data;
    5     responseToSend.statusText += " messageEvent.origin was " + event.origin;
     2    event.source.postMessage("Service worker received message '" + event.data + "' from origin '" + event.origin + "'");
    63});
    74
    8 self.addEventListener("fetch", (event) => {
    9     if (event.request.url.indexOf("test1") !== -1) {
    10         event.respondWith(new Response(null, responseToSend));
    11         return;
    12     }
    13     event.respondWith(Response.error());
    14 });
  • trunk/LayoutTests/http/tests/workers/service/resources/basic-ServiceWorker-postMessage.js

    r223922 r224113  
    1 function done()
    2 {
    3     finishSWTest();
    4 }
     1var messageNumber = 1;
     2navigator.serviceWorker.addEventListener("message", function(event) {
     3    log("PASS: Client received message from service worker, origin: " + event.origin);
     4    log("PASS: " + event.data);
     5    if (messageNumber == 1) {
     6        event.source.postMessage("Message 2");
     7        messageNumber++;
     8    } else
     9        finishSWTest();
     10});
    511
    6 async function test()
    7 {
    8     try {
    9         await navigator.serviceWorker.register("resources/basic-ServiceWorker-postMessage-worker.js", { });
    10 
    11         navigator.serviceWorker.controller.postMessage({ status: 200, statusText: "Worker received the message event." });
    12 
    13         let response = await fetch("test1");
    14         if (response.status == 200)
    15             log("PASS: Status code is " + response.status);
    16         else
    17             log("FAIL: Status code is " + response.status);
    18 
    19         if (response.statusText.startsWith("Worker received the message"))
    20             log("PASS: Status text is " + response.statusText);
    21         else
    22             log("FAIL: Status text is " + response.statusText);
    23 
    24         if (window.internals) {
    25             let source = internals.fetchResponseSource(response);
    26             if (source === "Service worker")
    27                 log("PASS: Source is " + source);
    28             else
    29                 log("FAIL: Source is " + source);
    30         }
    31     } catch(e) {
    32         log("Got exception: " + e);
    33     }
    34     finishSWTest();
    35 }
    36 
    37 test();
     12navigator.serviceWorker.register("resources/basic-ServiceWorker-postMessage-worker.js", { }).then(function() {
     13    navigator.serviceWorker.controller.postMessage("Message 1");
     14});
  • trunk/Source/WebCore/ChangeLog

    r224110 r224113  
     12017-10-27  Chris Dumez  <cdumez@apple.com>
     2
     3        Add initial support for serviceWorkerClient.postMessage()
     4        https://bugs.webkit.org/show_bug.cgi?id=178794
     5
     6        Reviewed by Youenn Fablet.
     7
     8        Add initial support for serviceWorkerClient.postMessage():
     9        - https://w3c.github.io/ServiceWorker/#client-postmessage
     10
     11        It is now possible to do bi-directional communication with a service worker
     12        via postMessage().
     13
     14        No new tests, updated existing test.
     15
     16        * WebCore.xcodeproj/project.pbxproj:
     17        * dom/Document.cpp:
     18        (WebCore::generateDocumentIdentifier):
     19        (WebCore::Document::allDocumentsMap):
     20        (WebCore::Document::allDocuments):
     21        (WebCore::m_identifier):
     22        (WebCore::Document::~Document):
     23        * dom/Document.h:
     24        (WebCore::Document::identifier const):
     25        * dom/ScriptExecutionContext.cpp:
     26        (WebCore::ScriptExecutionContext::serviceWorkerContainer):
     27        * dom/ScriptExecutionContext.h:
     28        * workers/service/ServiceWorker.cpp:
     29        (WebCore::ServiceWorker::postMessage):
     30        * workers/service/ServiceWorkerClient.cpp:
     31        (WebCore::ServiceWorkerClient::ServiceWorkerClient):
     32        (WebCore::ServiceWorkerClient::~ServiceWorkerClient):
     33        (WebCore::ServiceWorkerClient::id const):
     34        (WebCore::ServiceWorkerClient::postMessage):
     35        * workers/service/ServiceWorkerClient.h:
     36        (WebCore::ServiceWorkerClient::create):
     37        * workers/service/ServiceWorkerClient.idl:
     38        * workers/service/ServiceWorkerClientIdentifier.h: Copied from Source/WebCore/workers/service/ServiceWorkerClient.idl.
     39        (WebCore::ServiceWorkerClientIdentifier::toString const):
     40        * workers/service/ServiceWorkerRegistration.cpp:
     41        (WebCore::ServiceWorkerRegistration::unregister):
     42        * workers/service/ServiceWorkerWindowClient.cpp:
     43        (WebCore::ServiceWorkerWindowClient::ServiceWorkerWindowClient):
     44        * workers/service/ServiceWorkerWindowClient.h:
     45        * workers/service/context/SWContextManager.cpp:
     46        (WebCore::SWContextManager::postMessageToServiceWorkerGlobalScope):
     47        * workers/service/context/SWContextManager.h:
     48        * workers/service/context/ServiceWorkerThread.cpp:
     49        (WebCore::ServiceWorkerThread::postMessageToServiceWorkerGlobalScope):
     50        * workers/service/context/ServiceWorkerThread.h:
     51        * workers/service/server/SWClientConnection.cpp:
     52        (WebCore::SWClientConnection::postMessageToServiceWorkerClient):
     53        * workers/service/server/SWClientConnection.h:
     54
    1552017-10-27  Frederic Wang  <fwang@igalia.com>
    256
  • trunk/Source/WebCore/WebCore.xcodeproj/project.pbxproj

    r224061 r224113  
    22442244                837A80131E1E127300026B9F /* Localizable.stringsdict in Resources */ = {isa = PBXBuildFile; fileRef = 837A80111E1E127300026B9F /* Localizable.stringsdict */; };
    22452245                837B7D201DC3F55000D051FC /* ValidationBubbleIOS.mm in Sources */ = {isa = PBXBuildFile; fileRef = 837B7D1F1DC3F54C00D051FC /* ValidationBubbleIOS.mm */; };
     2246                837D46271FA2A8CE0054E1FA /* ServiceWorkerClientIdentifier.h in Headers */ = {isa = PBXBuildFile; fileRef = 837D46251FA2A8C50054E1FA /* ServiceWorkerClientIdentifier.h */; settings = {ATTRIBUTES = (Private, ); }; };
    22462247                837FB3451F9EA06D00D0FC31 /* ExtendableMessageEvent.h in Headers */ = {isa = PBXBuildFile; fileRef = 837FB3401F9EA06600D0FC31 /* ExtendableMessageEvent.h */; };
    22472248                8386A96D19F61B2E00E1EC4A /* StyleBuilder.h in Headers */ = {isa = PBXBuildFile; fileRef = 8386A96C19F61B2E00E1EC4A /* StyleBuilder.h */; };
     
    95229523                837A80121E1E127300026B9F /* English */ = {isa = PBXFileReference; lastKnownFileType = text.plist.stringsdict; name = English; path = English.lproj/Localizable.stringsdict; sourceTree = SOURCE_ROOT; };
    95239524                837B7D1F1DC3F54C00D051FC /* ValidationBubbleIOS.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = ValidationBubbleIOS.mm; sourceTree = "<group>"; };
     9525                837D46251FA2A8C50054E1FA /* ServiceWorkerClientIdentifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ServiceWorkerClientIdentifier.h; sourceTree = "<group>"; };
    95249526                837DFB341EBFEA7000601385 /* ElementCSSInlineStyle.idl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = ElementCSSInlineStyle.idl; sourceTree = "<group>"; };
    95259527                837FB3401F9EA06600D0FC31 /* ExtendableMessageEvent.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ExtendableMessageEvent.h; sourceTree = "<group>"; };
     
    1731417316                                46EF14241F97B7BA00C2A524 /* ServiceWorkerClient.h */,
    1731517317                                46EF14281F97B7BA00C2A524 /* ServiceWorkerClient.idl */,
     17318                                837D46251FA2A8C50054E1FA /* ServiceWorkerClientIdentifier.h */,
    1731617319                                46EF14211F97B7BA00C2A524 /* ServiceWorkerClients.cpp */,
    1731717320                                46EF14221F97B7BA00C2A524 /* ServiceWorkerClients.h */,
     
    2888128884                                51F1755D1F3EBC8300C74950 /* ServiceWorker.h in Headers */,
    2888228885                                46EF142D1F97B7D800C2A524 /* ServiceWorkerClient.h in Headers */,
     28886                                837D46271FA2A8CE0054E1FA /* ServiceWorkerClientIdentifier.h in Headers */,
    2888328887                                46EF142C1F97B7D800C2A524 /* ServiceWorkerClients.h in Headers */,
    2888428888                                8369FDFC1FA102E300C1FF1F /* ServiceWorkerClientType.h in Headers */,
  • trunk/Source/WebCore/dom/Document.cpp

    r224053 r224113  
    435435uint64_t Document::s_globalTreeVersion = 0;
    436436
    437 HashSet<Document*>& Document::allDocuments()
    438 {
    439     static NeverDestroyed<HashSet<Document*>> documents;
     437static uint64_t generateDocumentIdentifier()
     438{
     439    static uint64_t identifier = 0;
     440    return ++identifier;
     441}
     442
     443auto Document::allDocumentsMap() -> DocumentsMap&
     444{
     445    static NeverDestroyed<DocumentsMap> documents;
    440446    return documents;
     447}
     448
     449auto Document::allDocuments() -> DocumentsMap::ValuesIteratorRange
     450{
     451    return allDocumentsMap().values();
    441452}
    442453
     
    497508    , m_isNonRenderedPlaceholder(constructionFlags & NonRenderedPlaceholder)
    498509    , m_orientationNotifier(currentOrientation(frame))
    499 {
    500     allDocuments().add(this);
     510    , m_identifier(generateDocumentIdentifier())
     511{
     512    auto addResult = allDocumentsMap().add(m_identifier, this);
     513    ASSERT_UNUSED(addResult, addResult.isNewEntry);
    501514
    502515    // We depend on the url getting immediately set in subframes, but we
     
    548561Document::~Document()
    549562{
    550     allDocuments().remove(this);
     563    bool wasRemoved = allDocumentsMap().remove(m_identifier);
     564    ASSERT_UNUSED(wasRemoved, wasRemoved);
    551565
    552566    ASSERT(!renderView());
  • trunk/Source/WebCore/dom/Document.h

    r224053 r224113  
    348348    void removedLastRef();
    349349
    350     WEBCORE_EXPORT static HashSet<Document*>& allDocuments();
     350    uint64_t identifier() const { return m_identifier; }
     351
     352    using DocumentsMap = HashMap<uint64_t, Document*>;
     353    WEBCORE_EXPORT static DocumentsMap::ValuesIteratorRange allDocuments();
     354    WEBCORE_EXPORT static DocumentsMap& allDocumentsMap();
    351355
    352356    MediaQueryMatcher& mediaQueryMatcher();
     
    18361840
    18371841    RefPtr<DocumentTimeline> m_timeline;
     1842    uint64_t m_identifier;
    18381843};
    18391844
  • trunk/Source/WebCore/dom/ScriptExecutionContext.cpp

    r223476 r224113  
    3939#include "JSDOMWindow.h"
    4040#include "MessagePort.h"
     41#include "Navigator.h"
    4142#include "NoEventDispatchAssertion.h"
    4243#include "PublicURLManager.h"
     
    4647#include "Settings.h"
    4748#include "WorkerGlobalScope.h"
     49#include "WorkerNavigator.h"
    4850#include "WorkerThread.h"
    4951#include <heap/StrongInlines.h>
     
    526528}
    527529
     530#if ENABLE(SERVICE_WORKER)
     531ServiceWorkerContainer* ScriptExecutionContext::serviceWorkerContainer()
     532{
     533    NavigatorBase* navigator = nullptr;
     534    if (is<Document>(*this)) {
     535        if (auto* window = downcast<Document>(*this).domWindow())
     536            navigator = window->navigator();
     537    } else
     538        navigator = &downcast<WorkerGlobalScope>(*this).navigator();
     539
     540    return navigator ? navigator->serviceWorker() : nullptr;
     541}
     542#endif
     543
    528544} // namespace WebCore
  • trunk/Source/WebCore/dom/ScriptExecutionContext.h

    r223922 r224113  
    6969class URL;
    7070
     71#if ENABLE(SERVICE_WORKER)
     72class ServiceWorkerContainer;
     73#endif
     74
    7175namespace IDBClient {
    7276class IDBConnectionProxy;
     
    235239    uint64_t selectedServiceWorkerIdentifier() const { return m_serviceWorkerIdentifier; }
    236240    void setSelectedServiceWorkerIdentifier(uint64_t identifier) { m_serviceWorkerIdentifier = identifier; }
     241
     242    ServiceWorkerContainer* serviceWorkerContainer();
    237243#endif
    238244
  • trunk/Source/WebCore/workers/service/ServiceWorker.cpp

    r223922 r224113  
    7070
    7171    auto& swConnection = ServiceWorkerProvider::singleton().serviceWorkerConnectionForSession(context.sessionID());
    72     swConnection.postMessageToServiceWorkerGlobalScope(m_identifier, message.releaseReturnValue(), context.origin());
     72    swConnection.postMessageToServiceWorkerGlobalScope(m_identifier, message.releaseReturnValue(), context);
    7373
    7474    return { };
  • trunk/Source/WebCore/workers/service/ServiceWorkerClient.cpp

    r223985 r224113  
    2929#include "ServiceWorkerClient.h"
    3030
     31#include "MessagePort.h"
     32#include "SWContextManager.h"
     33#include "ScriptExecutionContext.h"
     34#include "SerializedScriptValue.h"
     35#include "ServiceWorkerGlobalScope.h"
     36#include "ServiceWorkerThread.h"
     37
    3138namespace WebCore {
    3239
    33 ServiceWorkerClient::ServiceWorkerClient(ScriptExecutionContext& context, Type type)
     40ServiceWorkerClient::ServiceWorkerClient(ScriptExecutionContext& context, const Identifier& identifier, Type type)
    3441    : ContextDestructionObserver(&context)
     42    , m_identifier(identifier)
    3543    , m_type(type)
     44{
     45}
     46
     47ServiceWorkerClient::~ServiceWorkerClient()
    3648{
    3749}
     
    4961String ServiceWorkerClient::id() const
    5062{
    51     return { };
     63    return m_identifier.toString();
    5264}
    5365
    54 ExceptionOr<void> ServiceWorkerClient::postMessage(JSC::ExecState&, JSC::JSValue message, Vector<JSC::Strong<JSC::JSObject>>&& transfer)
     66ExceptionOr<void> ServiceWorkerClient::postMessage(ScriptExecutionContext& context, JSC::JSValue messageValue, Vector<JSC::Strong<JSC::JSObject>>&& transfer)
    5567{
    56     UNUSED_PARAM(message);
    57     UNUSED_PARAM(transfer);
    58     return Exception { NotSupportedError, ASCIILiteral("client.postMessage() is not yet supported") };
     68    auto* execState = context.execState();
     69    ASSERT(execState);
     70
     71    Vector<RefPtr<MessagePort>> ports;
     72    auto message = SerializedScriptValue::create(*execState, messageValue, WTFMove(transfer), ports, SerializationContext::WorkerPostMessage);
     73    if (message.hasException())
     74        return message.releaseException();
     75
     76    // Disentangle the port in preparation for sending it to the remote context.
     77    auto channelsOrException = MessagePort::disentanglePorts(WTFMove(ports));
     78    if (channelsOrException.hasException())
     79        return channelsOrException.releaseException();
     80
     81    // FIXME: Support sending the channels.
     82    auto channels = channelsOrException.releaseReturnValue();
     83    if (channels && !channels->isEmpty())
     84        return Exception { NotSupportedError, ASCIILiteral("Passing MessagePort objects to postMessage is not yet supported") };
     85
     86    uint64_t sourceIdentifier = downcast<ServiceWorkerGlobalScope>(context).thread().identifier();
     87    callOnMainThread([message = message.releaseReturnValue(), destinationIdentifier = m_identifier, sourceIdentifier, sourceOrigin = context.origin().isolatedCopy()] () mutable {
     88        if (auto* connection = SWContextManager::singleton().connection())
     89            connection->postMessageToServiceWorkerClient(destinationIdentifier, WTFMove(message), sourceIdentifier, sourceOrigin);
     90    });
     91
     92    return { };
    5993}
    6094
  • trunk/Source/WebCore/workers/service/ServiceWorkerClient.h

    r223985 r224113  
    3030#include "ContextDestructionObserver.h"
    3131#include "ExceptionOr.h"
     32#include "ServiceWorkerClientIdentifier.h"
    3233#include "ServiceWorkerClientType.h"
    3334#include <heap/Strong.h>
     
    3536
    3637namespace JSC {
    37 class ExecState;
    3838class JSValue;
    3939}
     
    4343class ServiceWorkerClient : public RefCounted<ServiceWorkerClient>, public ContextDestructionObserver {
    4444public:
     45    using Identifier = ServiceWorkerClientIdentifier;
     46
    4547    using Type = ServiceWorkerClientType;
    4648    enum class FrameType {
     
    5153    };
    5254
    53     static Ref<ServiceWorkerClient> create(ScriptExecutionContext& context, Type type)
     55    static Ref<ServiceWorkerClient> create(ScriptExecutionContext& context, const Identifier& identifier, Type type)
    5456    {
    55         return adoptRef(*new ServiceWorkerClient(context, type));
     57        return adoptRef(*new ServiceWorkerClient(context, identifier, type));
    5658    }
     59
     60    ~ServiceWorkerClient();
    5761
    5862    String url() const;
     
    6165    String id() const;
    6266
    63     ExceptionOr<void> postMessage(JSC::ExecState&, JSC::JSValue message, Vector<JSC::Strong<JSC::JSObject>>&& transfer);
     67    ExceptionOr<void> postMessage(ScriptExecutionContext&, JSC::JSValue message, Vector<JSC::Strong<JSC::JSObject>>&& transfer);
    6468
    6569protected:
    66     ServiceWorkerClient(ScriptExecutionContext&, Type);
     70    ServiceWorkerClient(ScriptExecutionContext&, const Identifier&, Type);
    6771
     72    Identifier m_identifier;
    6873    Type m_type;
    6974};
  • trunk/Source/WebCore/workers/service/ServiceWorkerClient.idl

    r223985 r224113  
    3636    readonly attribute DOMString id;
    3737
    38     [CallWith=ScriptState, MayThrowException] void postMessage(any message, optional sequence<object> transfer = []);
     38    [CallWith=ScriptExecutionContext, MayThrowException] void postMessage(any message, optional sequence<object> transfer = []);
    3939};
    4040
  • trunk/Source/WebCore/workers/service/ServiceWorkerClientIdentifier.h

    r224112 r224113  
    2424 */
    2525
    26 [
    27     Conditional=SERVICE_WORKER,
    28     CustomToJSObject,
    29     EnabledAtRuntime=ServiceWorker,
    30     Exposed=ServiceWorker,
    31     InterfaceName=Client,
    32 ] interface ServiceWorkerClient {
    33     readonly attribute USVString url;
    34     readonly attribute FrameType frameType;
    35     readonly attribute ServiceWorkerClientType type;
    36     readonly attribute DOMString id;
     26#pragma once
    3727
    38     [CallWith=ScriptState, MayThrowException] void postMessage(any message, optional sequence<object> transfer = []);
     28#include <wtf/text/WTFString.h>
     29
     30namespace WebCore {
     31
     32struct ServiceWorkerClientIdentifier {
     33    uint64_t serverConnectionIdentifier;
     34    uint64_t scriptExecutionContextIdentifier;
     35
     36    String toString() const { return String::number(serverConnectionIdentifier) + "-" +  String::number(scriptExecutionContextIdentifier); }
    3937};
    4038
    41 enum FrameType {
    42   "auxiliary",
    43   "top-level",
    44   "nested",
    45   "none"
    46 };
     39}
  • trunk/Source/WebCore/workers/service/ServiceWorkerRegistration.cpp

    r223964 r224113  
    3030#include "DOMWindow.h"
    3131#include "Document.h"
    32 #include "Navigator.h"
    3332#include "ServiceWorkerContainer.h"
    3433#include "WorkerGlobalScope.h"
    35 #include "WorkerNavigator.h"
    3634
    3735namespace WebCore {
    38 
    39 static ServiceWorkerContainer* containerForScriptExecutionContext(ScriptExecutionContext& context)
    40 {
    41     NavigatorBase* navigator = nullptr;
    42     if (is<Document>(context)) {
    43         if (auto* window = downcast<Document>(context).domWindow())
    44             navigator = window->navigator();
    45     } else
    46         navigator = &downcast<WorkerGlobalScope>(context).navigator();
    47 
    48     return navigator ? navigator->serviceWorker() : nullptr;
    49 }
    5036
    5137ServiceWorkerRegistration::ServiceWorkerRegistration(ScriptExecutionContext& context, ServiceWorkerRegistrationData&& registrationData)
     
    9581    }
    9682
    97     auto* container = containerForScriptExecutionContext(*context);
     83    auto* container = context->serviceWorkerContainer();
    9884    if (!container) {
    9985        promise->reject(Exception(InvalidStateError));
  • trunk/Source/WebCore/workers/service/ServiceWorkerWindowClient.cpp

    r223985 r224113  
    3333namespace WebCore {
    3434
    35 ServiceWorkerWindowClient::ServiceWorkerWindowClient(ScriptExecutionContext& context)
    36     : ServiceWorkerClient(context, Type::Window)
     35ServiceWorkerWindowClient::ServiceWorkerWindowClient(ScriptExecutionContext& context, const Identifier& identifier)
     36    : ServiceWorkerClient(context, identifier, Type::Window)
    3737{
    3838}
  • trunk/Source/WebCore/workers/service/ServiceWorkerWindowClient.h

    r223985 r224113  
    3737class ServiceWorkerWindowClient final : public ServiceWorkerClient {
    3838public:
    39     static Ref<ServiceWorkerWindowClient> create(ScriptExecutionContext& context)
     39    static Ref<ServiceWorkerWindowClient> create(ScriptExecutionContext& context, const Identifier& identifier)
    4040    {
    41         return adoptRef(*new ServiceWorkerWindowClient(context));
     41        return adoptRef(*new ServiceWorkerWindowClient(context, identifier));
    4242    }
    4343
     
    4949
    5050private:
    51     explicit ServiceWorkerWindowClient(ScriptExecutionContext&);
     51    ServiceWorkerWindowClient(ScriptExecutionContext&, const Identifier&);
    5252};
    5353
  • trunk/Source/WebCore/workers/service/context/SWContextManager.cpp

    r224041 r224113  
    6060}
    6161
    62 void SWContextManager::postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, Ref<SerializedScriptValue>&& message, const String& sourceOrigin)
     62void SWContextManager::postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, Ref<SerializedScriptValue>&& message, const ServiceWorkerClientIdentifier& sourceIdentifier, const String& sourceOrigin)
    6363{
    64     auto* serviceWorker = m_workerMap.get(serviceWorkerIdentifier);
     64    auto* serviceWorker = m_workerMap.get(destinationServiceWorkerIdentifier);
    6565    if (!serviceWorker)
    6666        return;
    6767
    6868    // FIXME: We should pass valid MessagePortChannels.
    69     serviceWorker->thread().postMessageToServiceWorkerGlobalScope(WTFMove(message), nullptr, sourceOrigin);
     69    serviceWorker->thread().postMessageToServiceWorkerGlobalScope(WTFMove(message), nullptr, sourceIdentifier, sourceOrigin);
    7070}
    7171
  • trunk/Source/WebCore/workers/service/context/SWContextManager.h

    r224041 r224113  
    3535
    3636class SerializedScriptValue;
     37struct ServiceWorkerClientIdentifier;
    3738
    3839class SWContextManager {
     
    4344    public:
    4445        virtual ~Connection() { }
     46
     47        virtual void postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, Ref<SerializedScriptValue>&& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin) = 0;
    4548    };
    4649
     
    5053    WEBCORE_EXPORT void registerServiceWorkerThread(Ref<ServiceWorkerThreadProxy>&&);
    5154    WEBCORE_EXPORT ServiceWorkerThreadProxy* serviceWorkerThreadProxy(uint64_t) const;
    52     WEBCORE_EXPORT void postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, Ref<SerializedScriptValue>&& message, const String& sourceOrigin);
     55    WEBCORE_EXPORT void postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, Ref<SerializedScriptValue>&& message, const ServiceWorkerClientIdentifier& sourceIdentifier, const String& sourceOrigin);
    5356
    5457private:
  • trunk/Source/WebCore/workers/service/context/ServiceWorkerThread.cpp

    r223981 r224113  
    3535#include "ServiceWorkerFetch.h"
    3636#include "ServiceWorkerGlobalScope.h"
     37#include "ServiceWorkerWindowClient.h"
    3738#include "WorkerLoaderProxy.h"
    3839#include "WorkerObjectProxy.h"
     
    100101}
    101102
    102 void ServiceWorkerThread::postMessageToServiceWorkerGlobalScope(Ref<SerializedScriptValue>&& message, std::unique_ptr<MessagePortChannelArray>&& channels, const String& sourceOrigin)
     103void ServiceWorkerThread::postMessageToServiceWorkerGlobalScope(Ref<SerializedScriptValue>&& message, std::unique_ptr<MessagePortChannelArray>&& channels, const ServiceWorkerClientIdentifier& sourceIdentifier, const String& sourceOrigin)
    103104{
    104     ScriptExecutionContext::Task task([channels = WTFMove(channels), message = WTFMove(message), sourceOrigin = sourceOrigin.isolatedCopy()] (ScriptExecutionContext& context) mutable {
     105    ScriptExecutionContext::Task task([channels = WTFMove(channels), message = WTFMove(message), sourceIdentifier, sourceOrigin = sourceOrigin.isolatedCopy()] (ScriptExecutionContext& context) mutable {
    105106        auto& serviceWorkerGlobalScope = downcast<ServiceWorkerGlobalScope>(context);
    106107        auto ports = MessagePort::entanglePorts(serviceWorkerGlobalScope, WTFMove(channels));
    107         serviceWorkerGlobalScope.dispatchEvent(ExtendableMessageEvent::create(WTFMove(ports), WTFMove(message), sourceOrigin));
     108        ExtendableMessageEventSource source = RefPtr<ServiceWorkerClient> { ServiceWorkerWindowClient::create(context, sourceIdentifier) };
     109        serviceWorkerGlobalScope.dispatchEvent(ExtendableMessageEvent::create(WTFMove(ports), WTFMove(message), sourceOrigin, { }, WTFMove(source)));
    108110        serviceWorkerGlobalScope.thread().workerObjectProxy().confirmMessageFromWorkerObject(serviceWorkerGlobalScope.hasPendingActivity());
    109111    });
  • trunk/Source/WebCore/workers/service/context/ServiceWorkerThread.h

    r223981 r224113  
    4141class WorkerLoaderProxy;
    4242class WorkerObjectProxy;
     43struct ServiceWorkerClientIdentifier;
    4344struct ServiceWorkerContextData;
    4445
     
    5657
    5758    WEBCORE_EXPORT void postFetchTask(Ref<ServiceWorkerFetch::Client>&&, ResourceRequest&&, FetchOptions&&);
    58     WEBCORE_EXPORT void postMessageToServiceWorkerGlobalScope(Ref<SerializedScriptValue>&&, std::unique_ptr<MessagePortChannelArray>&&, const String& sourceOrigin);
     59    WEBCORE_EXPORT void postMessageToServiceWorkerGlobalScope(Ref<SerializedScriptValue>&&, std::unique_ptr<MessagePortChannelArray>&&, const ServiceWorkerClientIdentifier& sourceIdentifier, const String& sourceOrigin);
    5960
    6061protected:
  • trunk/Source/WebCore/workers/service/server/SWClientConnection.cpp

    r223964 r224113  
    2929#if ENABLE(SERVICE_WORKER)
    3030
     31#include "Document.h"
    3132#include "ExceptionData.h"
     33#include "MessageEvent.h"
     34#include "ServiceWorkerContainer.h"
    3235#include "ServiceWorkerFetchResult.h"
    3336#include "ServiceWorkerJobData.h"
     
    110113}
    111114
     115void SWClientConnection::postMessageToServiceWorkerClient(uint64_t destinationScriptExecutionContextIdentifier, Ref<SerializedScriptValue>&& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin)
     116{
     117    // FIXME: destinationScriptExecutionContextIdentifier can only identify a Document at the moment.
     118    auto* destinationDocument = Document::allDocumentsMap().get(destinationScriptExecutionContextIdentifier);
     119    if (!destinationDocument)
     120        return;
     121
     122    auto* container = destinationDocument->serviceWorkerContainer();
     123    if (!container)
     124        return;
     125
     126    std::optional<MessageEventSource> source;
     127    if (destinationDocument->selectedServiceWorkerIdentifier() == sourceServiceWorkerIdentifier) {
     128        ASSERT(container->controller()->identifier() == sourceServiceWorkerIdentifier);
     129        source = MessageEventSource { RefPtr<ServiceWorker> { container->controller() } };
     130    } else
     131        source = MessageEventSource { RefPtr<ServiceWorker> { ServiceWorker::create(*destinationDocument, sourceServiceWorkerIdentifier) } };
     132
     133    // FIXME: We should pass in ports.
     134    auto messageEvent = MessageEvent::create({ }, WTFMove(message), sourceOrigin, { }, WTFMove(source));
     135    container->dispatchEvent(messageEvent);
     136}
     137
    112138} // namespace WebCore
    113139
  • trunk/Source/WebCore/workers/service/server/SWClientConnection.h

    r223964 r224113  
    5050    void finishedFetchingScript(ServiceWorkerJob&, const String&);
    5151    void failedFetchingScript(ServiceWorkerJob&, const ResourceError&);
    52     virtual void postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, Ref<SerializedScriptValue>&&, const String& sourceOrigin) = 0;
     52    virtual void postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, Ref<SerializedScriptValue>&&, ScriptExecutionContext& source) = 0;
    5353
    5454    virtual uint64_t identifier() const = 0;
     
    6060    WEBCORE_EXPORT void unregistrationJobResolvedInServer(uint64_t jobIdentifier, bool unregistrationResult);
    6161    WEBCORE_EXPORT void startScriptFetchForServer(uint64_t jobIdentifier);
     62    WEBCORE_EXPORT void postMessageToServiceWorkerClient(uint64_t destinationScriptExecutionContextIdentifier, Ref<SerializedScriptValue>&& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin);
    6263
    6364private:
  • trunk/Source/WebKit/ChangeLog

    r224095 r224113  
     12017-10-27  Chris Dumez  <cdumez@apple.com>
     2
     3        Add initial support for serviceWorkerClient.postMessage()
     4        https://bugs.webkit.org/show_bug.cgi?id=178794
     5
     6        Reviewed by Youenn Fablet.
     7
     8        Add initial support for serviceWorkerClient.postMessage():
     9        - https://w3c.github.io/ServiceWorker/#client-postmessage
     10
     11        It is now possible to do bi-directional communication with a service worker
     12        via postMessage().
     13
     14        * Shared/WebCoreArgumentCoders.cpp:
     15        (IPC::ArgumentCoder<ServiceWorkerClientIdentifier>::encode):
     16        (IPC::ArgumentCoder<ServiceWorkerClientIdentifier>::decode):
     17        * Shared/WebCoreArgumentCoders.h:
     18        * StorageProcess/ServiceWorker/WebSWServerConnection.cpp:
     19        (WebKit::WebSWServerConnection::postMessageToServiceWorkerGlobalScope):
     20        (WebKit::WebSWServerConnection::postMessageToServiceWorkerClient):
     21        * StorageProcess/ServiceWorker/WebSWServerConnection.h:
     22        * StorageProcess/ServiceWorker/WebSWServerConnection.messages.in:
     23        * StorageProcess/StorageProcess.cpp:
     24        (WebKit::StorageProcess::postMessageToServiceWorkerClient):
     25        * StorageProcess/StorageProcess.h:
     26        * StorageProcess/StorageProcess.messages.in:
     27        * WebProcess/Storage/WebSWClientConnection.cpp:
     28        (WebKit::WebSWClientConnection::postMessageToServiceWorkerGlobalScope):
     29        (WebKit::WebSWClientConnection::postMessageToServiceWorkerClient):
     30        * WebProcess/Storage/WebSWClientConnection.h:
     31        * WebProcess/Storage/WebSWClientConnection.messages.in:
     32        * WebProcess/Storage/WebSWContextManagerConnection.cpp:
     33        (WebKit::WebSWContextManagerConnection::postMessageToServiceWorkerGlobalScope):
     34        (WebKit::WebSWContextManagerConnection::postMessageToServiceWorkerClient):
     35        * WebProcess/Storage/WebSWContextManagerConnection.h:
     36        * WebProcess/Storage/WebSWContextManagerConnection.messages.in:
     37
    1382017-10-27  David Kilzer  <ddkilzer@apple.com>
    239
  • trunk/Source/WebKit/Shared/WebCoreArgumentCoders.cpp

    r223678 r224113  
    6666#include <WebCore/ScrollingCoordinator.h>
    6767#include <WebCore/SearchPopupMenu.h>
     68#include <WebCore/ServiceWorkerClientIdentifier.h>
    6869#include <WebCore/TextCheckerClient.h>
    6970#include <WebCore/TextIndicator.h>
     
    19011902    return true;
    19021903}
     1904
     1905#if ENABLE(SERVICE_WORKER)
     1906void ArgumentCoder<ServiceWorkerClientIdentifier>::encode(Encoder& encoder, const ServiceWorkerClientIdentifier& identifier)
     1907{
     1908    encoder << identifier.serverConnectionIdentifier << identifier.scriptExecutionContextIdentifier;
     1909}
     1910
     1911bool ArgumentCoder<ServiceWorkerClientIdentifier>::decode(Decoder& decoder, ServiceWorkerClientIdentifier& identifier)
     1912{
     1913    uint64_t serverConnectionIdentifier;
     1914    if (!decoder.decode(serverConnectionIdentifier))
     1915        return false;
     1916
     1917    uint64_t scriptExecutionContextIdentifier;
     1918    if (!decoder.decode(scriptExecutionContextIdentifier))
     1919        return false;
     1920
     1921    identifier = { serverConnectionIdentifier, scriptExecutionContextIdentifier };
     1922    return true;
     1923}
     1924#endif
    19031925
    19041926void ArgumentCoder<TextCheckingResult>::encode(Encoder& encoder, const TextCheckingResult& result)
  • trunk/Source/WebKit/Shared/WebCoreArgumentCoders.h

    r223652 r224113  
    158158using IDBKeyPath = Variant<String, Vector<String>>;
    159159#endif
     160
     161#if ENABLE(SERVICE_WORKER)
     162struct ServiceWorkerClientIdentifier;
     163#endif
    160164}
    161165
     
    477481    static bool decode(Decoder&, WebCore::TextCheckingRequestData&);
    478482};
     483
     484#if ENABLE(SERVICE_WORKER)
     485template<> struct ArgumentCoder<WebCore::ServiceWorkerClientIdentifier> {
     486    static void encode(Encoder&, const WebCore::ServiceWorkerClientIdentifier&);
     487    static bool decode(Decoder&, WebCore::ServiceWorkerClientIdentifier&);
     488};
     489#endif
    479490
    480491template<> struct ArgumentCoder<WebCore::TextCheckingResult> {
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.cpp

    r224041 r224113  
    4545#include <WebCore/NotImplemented.h>
    4646#include <WebCore/SecurityOrigin.h>
     47#include <WebCore/ServiceWorkerClientIdentifier.h>
    4748#include <WebCore/ServiceWorkerContextData.h>
    4849#include <WebCore/ServiceWorkerJobData.h>
     
    109110}
    110111
    111 void WebSWServerConnection::postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, const IPC::DataReference& message, const String& sourceOrigin)
     112void WebSWServerConnection::postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, const IPC::DataReference& message, uint64_t sourceScriptExecutionContextIdentifier, const String& sourceOrigin)
    112113{
    113     sendToContextProcess(Messages::WebSWContextManagerConnection::PostMessageToServiceWorkerGlobalScope(serviceWorkerIdentifier, message, sourceOrigin));
     114    ServiceWorkerClientIdentifier sourceIdentifier { identifier(), sourceScriptExecutionContextIdentifier };
     115    sendToContextProcess(Messages::WebSWContextManagerConnection::PostMessageToServiceWorkerGlobalScope { destinationServiceWorkerIdentifier, message, sourceIdentifier, sourceOrigin });
    114116}
    115117
     
    139141}
    140142
     143void WebSWServerConnection::postMessageToServiceWorkerClient(uint64_t destinationScriptExecutionContextIdentifier, const IPC::DataReference& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin)
     144{
     145    send(Messages::WebSWClientConnection::PostMessageToServiceWorkerClient { destinationScriptExecutionContextIdentifier, message, sourceServiceWorkerIdentifier, sourceOrigin });
     146}
     147
    141148template<typename U> bool WebSWServerConnection::sendToContextProcess(U&& message)
    142149{
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.h

    r223964 r224113  
    5858    void didNotHandleFetch(uint64_t fetchIdentifier);
    5959
     60    void postMessageToServiceWorkerClient(uint64_t destinationScriptExecutionContextIdentifier, const IPC::DataReference& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin);
     61
    6062private:
    6163    // Implement SWServer::Connection (Messages to the client WebProcess)
     
    6769    void startFetch(uint64_t fetchIdentifier, uint64_t serviceWorkerIdentifier, const WebCore::ResourceRequest&, const WebCore::FetchOptions&);
    6870
    69     void postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, const IPC::DataReference& message, const String& sourceOrigin);
     71    void postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, const IPC::DataReference& message, uint64_t sourceScriptExecutionContextIdentifier, const String& sourceOrigin);
    7072
    7173    // Messages to the SW context WebProcess
  • trunk/Source/WebKit/StorageProcess/ServiceWorker/WebSWServerConnection.messages.in

    r223922 r224113  
    2929
    3030    StartFetch(uint64_t identifier, uint64_t serviceWorkerIdentifier, WebCore::ResourceRequest request, struct WebCore::FetchOptions options)
    31     PostMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, IPC::DataReference message, String sourceOrigin)
     31    PostMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, IPC::DataReference message, uint64_t sourceScriptExecutionContextIdentifier, String sourceOrigin)
    3232}
    3333
  • trunk/Source/WebKit/StorageProcess/StorageProcess.cpp

    r223951 r224113  
    3939#include <WebCore/NotImplemented.h>
    4040#include <WebCore/SecurityOrigin.h>
     41#include <WebCore/ServiceWorkerClientIdentifier.h>
    4142#include <WebCore/TextEncoding.h>
    4243#include <pal/SessionID.h>
     
    436437}
    437438
     439void StorageProcess::postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, const IPC::DataReference& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin)
     440{
     441    if (auto* connection = m_swServerConnections.get(destinationIdentifier.serverConnectionIdentifier))
     442        connection->postMessageToServiceWorkerClient(destinationIdentifier.scriptExecutionContextIdentifier, message, sourceServiceWorkerIdentifier, sourceOrigin);
     443}
     444
    438445void StorageProcess::registerSWServerConnection(WebSWServerConnection& connection)
    439446{
  • trunk/Source/WebKit/StorageProcess/StorageProcess.h

    r223951 r224113  
    3838class SWServer;
    3939struct SecurityOriginData;
     40struct ServiceWorkerClientIdentifier;
    4041struct ServiceWorkerRegistrationKey;
    4142}
     
    126127    void didFailFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier);
    127128    void didNotHandleFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier);
     129
     130    void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, const IPC::DataReference& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin);
    128131#endif
    129132#if ENABLE(INDEXED_DATABASE)
  • trunk/Source/WebKit/StorageProcess/StorageProcess.messages.in

    r223839 r224113  
    4747    DidReceiveFetchData(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier, IPC::DataReference data, int64_t encodedDataLength)
    4848    DidFinishFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier)
     49    PostMessageToServiceWorkerClient(struct WebCore::ServiceWorkerClientIdentifier destinationIdentifier, IPC::DataReference message, uint64_t sourceServiceWorkerIdentifier, String sourceOrigin)
    4950#endif
    5051}
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.cpp

    r223922 r224113  
    3636#include "WebSWOriginTable.h"
    3737#include "WebSWServerConnectionMessages.h"
     38#include <WebCore/Document.h>
    3839#include <WebCore/SerializedScriptValue.h>
    3940#include <WebCore/ServiceWorkerFetchResult.h>
     
    6970}
    7071
    71 void WebSWClientConnection::postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, Ref<SerializedScriptValue>&& scriptValue, const String& sourceOrigin)
     72void WebSWClientConnection::postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, Ref<SerializedScriptValue>&& scriptValue, ScriptExecutionContext& source)
    7273{
    73     send(Messages::WebSWServerConnection::PostMessageToServiceWorkerGlobalScope(serviceWorkerIdentifier, IPC::DataReference { scriptValue->data() }, sourceOrigin));
     74    // FIXME: Add support for posting messages from workers.
     75    if (!is<Document>(source))
     76        return;
     77
     78    send(Messages::WebSWServerConnection::PostMessageToServiceWorkerGlobalScope(destinationServiceWorkerIdentifier, IPC::DataReference { scriptValue->data() }, downcast<Document>(source).identifier(), source.origin()));
    7479}
    7580
     
    9398}
    9499
     100void WebSWClientConnection::postMessageToServiceWorkerClient(uint64_t destinationScriptExecutionContextIdentifier, const IPC::DataReference& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin)
     101{
     102    SWClientConnection::postMessageToServiceWorkerClient(destinationScriptExecutionContextIdentifier, SerializedScriptValue::adopt(message.vector()), sourceServiceWorkerIdentifier, sourceOrigin);
     103}
     104
    95105} // namespace WebKit
    96106
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.h

    r223922 r224113  
    5757    void scheduleJobInServer(const WebCore::ServiceWorkerJobData&) final;
    5858    void finishFetchingScriptInServer(const WebCore::ServiceWorkerFetchResult&) final;
    59     void postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, Ref<WebCore::SerializedScriptValue>&&, const String& sourceOrigin) final;
     59    void postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, Ref<WebCore::SerializedScriptValue>&&, WebCore::ScriptExecutionContext& source) final;
    6060
    6161    void disconnectedFromWebProcess();
     
    6464    bool hasServiceWorkerRegisteredForOrigin(const WebCore::SecurityOrigin&) const final;
    6565    Ref<ServiceWorkerClientFetch> startFetch(WebServiceWorkerProvider&, Ref<WebCore::ResourceLoader>&&, uint64_t identifier, ServiceWorkerClientFetch::Callback&&);
     66
     67    void postMessageToServiceWorkerClient(uint64_t destinationScriptExecutionContextIdentifier, const IPC::DataReference& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin);
    6668
    6769private:
  • trunk/Source/WebKit/WebProcess/Storage/WebSWClientConnection.messages.in

    r223964 r224113  
    3131
    3232    SetSWOriginTableSharedMemory(WebKit::SharedMemory::Handle handle)
     33    PostMessageToServiceWorkerClient(uint64_t destinationScriptExecutionContextIdentifier, IPC::DataReference message, uint64_t sourceServiceWorkerIdentifier, String sourceOrigin)
    3334}
    3435
  • trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.cpp

    r224041 r224113  
    131131}
    132132
    133 void WebSWContextManagerConnection::postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, const IPC::DataReference& message, const String& sourceOrigin)
     133void WebSWContextManagerConnection::postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, const IPC::DataReference& message, const ServiceWorkerClientIdentifier& sourceIdentifier, const String& sourceOrigin)
    134134{
    135     SWContextManager::singleton().postMessageToServiceWorkerGlobalScope(serviceWorkerIdentifier, SerializedScriptValue::adopt(message.vector()), sourceOrigin);
     135    SWContextManager::singleton().postMessageToServiceWorkerGlobalScope(destinationServiceWorkerIdentifier, SerializedScriptValue::adopt(message.vector()), sourceIdentifier, sourceOrigin);
     136}
     137
     138void WebSWContextManagerConnection::postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, Ref<SerializedScriptValue>&& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin)
     139{
     140    m_connectionToStorageProcess->send(Messages::StorageProcess::PostMessageToServiceWorkerClient(destinationIdentifier, IPC::DataReference { message->data() }, sourceServiceWorkerIdentifier, sourceOrigin), 0);
    136141}
    137142
  • trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.h

    r224041 r224113  
    4242struct WebPreferencesStore;
    4343
    44 class WebSWContextManagerConnection : public WebCore::SWContextManager::Connection, public IPC::MessageReceiver {
     44class WebSWContextManagerConnection final : public WebCore::SWContextManager::Connection, public IPC::MessageReceiver {
    4545public:
    4646    WebSWContextManagerConnection(Ref<IPC::Connection>&&, uint64_t pageID, const WebPreferencesStore&);
     
    4848    void didReceiveMessage(IPC::Connection&, IPC::Decoder&) final;
    4949
    50     void postMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, const IPC::DataReference& message, const String& sourceOrigin);
    51 
    5250private:
    5351    void updatePreferences(const WebPreferencesStore&);
    5452
     53    // WebCore::SWContextManager::Connection.
     54    void postMessageToServiceWorkerClient(const WebCore::ServiceWorkerClientIdentifier& destinationIdentifier, Ref<WebCore::SerializedScriptValue>&& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin) final;
     55
     56    // IPC messages.
    5557    void startServiceWorker(uint64_t serverConnectionIdentifier, const WebCore::ServiceWorkerContextData&);
    5658    void startFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier, uint64_t serviceWorkerIdentifier, WebCore::ResourceRequest&&, WebCore::FetchOptions&&);
     59    void postMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, const IPC::DataReference& message, const WebCore::ServiceWorkerClientIdentifier& sourceIdentifier, const String& sourceOrigin);
    5760
    5861    Ref<IPC::Connection> m_connectionToStorageProcess;
  • trunk/Source/WebKit/WebProcess/Storage/WebSWContextManagerConnection.messages.in

    r224041 r224113  
    2626    StartServiceWorker(uint64_t serverConnectionIdentifier, struct WebCore::ServiceWorkerContextData contextData)
    2727    StartFetch(uint64_t serverConnectionIdentifier, uint64_t fetchIdentifier, uint64_t serviceWorkerIdentifier, WebCore::ResourceRequest request, struct WebCore::FetchOptions options)
    28     PostMessageToServiceWorkerGlobalScope(uint64_t serviceWorkerIdentifier, IPC::DataReference message, String sourceOrigin)
     28    PostMessageToServiceWorkerGlobalScope(uint64_t destinationServiceWorkerIdentifier, IPC::DataReference message, struct WebCore::ServiceWorkerClientIdentifier sourceIdentifier, String sourceOrigin)
    2929}
    3030
Note: See TracChangeset for help on using the changeset viewer.