Bug 178534 - Add initial implementation for serviceWorker.postMessage()
Summary: Add initial implementation for serviceWorker.postMessage()
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebCore Misc. (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL: https://w3c.github.io/ServiceWorker/#...
Keywords: InRadar, WebExposed
Depends on: 178475 178491 178552
Blocks: 178794
  Show dependency treegraph
 
Reported: 2017-10-19 12:32 PDT by Chris Dumez
Modified: 2017-11-15 13:02 PST (History)
9 users (show)

See Also:


Attachments
WIP Patch (21.46 KB, patch)
2017-10-19 14:30 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
WIP Patch (21.75 KB, patch)
2017-10-19 15:46 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
WIP Patch (20.28 KB, patch)
2017-10-23 09:21 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
WIP Patch (20.40 KB, patch)
2017-10-23 11:31 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
WIP Patch (23.71 KB, patch)
2017-10-23 14:56 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
WIP Patch (50.46 KB, patch)
2017-10-23 16:54 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (60.19 KB, patch)
2017-10-23 20:01 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (60.40 KB, patch)
2017-10-23 20:08 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (59.50 KB, patch)
2017-10-23 21:11 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (59.48 KB, patch)
2017-10-23 21:14 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (59.54 KB, patch)
2017-10-23 21:28 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (59.52 KB, patch)
2017-10-24 12:45 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (60.23 KB, patch)
2017-10-24 13:29 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2017-10-19 12:32:24 PDT
Add initial implementation for serviceWorker.postMessage():
- https://w3c.github.io/ServiceWorker/#service-worker-postmessage
Comment 1 Chris Dumez 2017-10-19 14:30:05 PDT
Created attachment 324289 [details]
WIP Patch
Comment 2 Chris Dumez 2017-10-19 15:46:14 PDT
Created attachment 324303 [details]
WIP Patch
Comment 3 Chris Dumez 2017-10-23 09:21:21 PDT
Created attachment 324560 [details]
WIP Patch
Comment 4 Chris Dumez 2017-10-23 11:31:06 PDT
Created attachment 324573 [details]
WIP Patch
Comment 5 Chris Dumez 2017-10-23 14:56:51 PDT
Created attachment 324594 [details]
WIP Patch
Comment 6 Build Bot 2017-10-23 14:59:14 PDT
Attachment 324594 [details] did not pass style-queue:


ERROR: Source/WebCore/workers/service/ServiceWorker.h:32:  Alphabetical sorting problem.  [build/include_order] [4]
Total errors found: 1 in 22 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 7 Chris Dumez 2017-10-23 16:54:51 PDT
Created attachment 324614 [details]
WIP Patch
Comment 8 Chris Dumez 2017-10-23 20:01:35 PDT
Created attachment 324630 [details]
Patch
Comment 9 Chris Dumez 2017-10-23 20:08:26 PDT
Created attachment 324631 [details]
Patch
Comment 10 Chris Dumez 2017-10-23 21:11:43 PDT
Created attachment 324634 [details]
Patch
Comment 11 Chris Dumez 2017-10-23 21:14:33 PDT
Created attachment 324636 [details]
Patch
Comment 12 Chris Dumez 2017-10-23 21:28:46 PDT
Created attachment 324639 [details]
Patch
Comment 13 Chris Dumez 2017-10-24 12:45:33 PDT
Created attachment 324702 [details]
Patch
Comment 14 youenn fablet 2017-10-24 12:54:12 PDT
Comment on attachment 324639 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=324639&action=review

> Source/WebCore/ChangeLog:18
> +        * bindings/js/JSExtendableMessageEvent.cpp: Copied from Source/WebCore/workers/service/ServiceWorker.cpp.

Probably added, not copied.

> Source/WebCore/bindings/js/JSExtendableMessageEventCustom.cpp:54
> +    return result;

Is there a way to share some code with JSMessageEvent::data?

> Source/WebCore/dom/ScriptExecutionContext.h:114
> +    virtual String origin() const = 0;

Why not using a SecurityOrigin&/SecurityOrigin*?

> Source/WebCore/workers/service/ExtendableMessageEvent.h:57
> +        Vector<RefPtr<MessagePort>> ports;

Would it be difficult to have a Vector<Ref<MessagePort>> instead?

> Source/WebCore/workers/service/ServiceWorker.cpp:68
> +    if (!scriptExecutionContext())

I am not sure we can get both a valid ExecState and a null scriptExecutionContext.
If so, I would add it as a first check.

Can we get a ScriptExecutionContext& as first parameter and get an ExecState from it?
Maybe add an ASSERT.

> Source/WebCore/workers/service/ServiceWorkerContainer.cpp:84
> +        m_controller = ServiceWorker::create(*context, context->selectedServiceWorkerIdentifier());

Can we get a controller whose identifier is not context selectedServiceWorkerIdentifier?

> Source/WebCore/workers/service/context/ServiceWorkerThread.cpp:109
> +        serviceWorkerGlobalScope.dispatchEvent(ExtendableMessageEvent::create(WTFMove(ports), WTFMove(message), sourceOrigin));

I guess sourceOrigin might be different from the origin of the service worker, like in the case of data URL iframes.
If I am wrong, we might not even need to pass the origin.
If the origin cannot be any random origin, maybe we should assert somehow that this is in a restricted set.
I probably need to read the spec.

> Source/WebCore/workers/service/context/ServiceWorkerThread.h:43
> +using MessagePortChannelArray = Vector<std::unique_ptr<MessagePortChannel>, 1>;

Should probably be UniqueRef if possible.

> Source/WebCore/workers/service/context/ServiceWorkerThread.h:56
> +    WEBCORE_EXPORT void postMessageToServiceWorkerGlobalScope(Ref<SerializedScriptValue>&&, std::unique_ptr<MessagePortChannelArray>&&, const String& sourceOrigin);

Here as well.

> Source/WebKit/WebProcess/Storage/ServiceWorkerContextManager.cpp:76
> +    auto* workerThread = m_workerThreadMap.get(serviceWorkerIdentifier);

Should we protect for a serviceWorkerIdentifier being zero?
Even more valid for startFetch above.
Comment 15 Chris Dumez 2017-10-24 13:29:17 PDT
Comment on attachment 324639 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=324639&action=review

>> Source/WebCore/bindings/js/JSExtendableMessageEventCustom.cpp:54
>> +    return result;
> 
> Is there a way to share some code with JSMessageEvent::data?

I'll see what I can do.

>> Source/WebCore/dom/ScriptExecutionContext.h:114
>> +    virtual String origin() const = 0;
> 
> Why not using a SecurityOrigin&/SecurityOrigin*?

This is a pre-existing method on Document / WorkerGlobalScope is is used for the self.origin Web API. This is why it currently returns a String. Also note that I need the origin as a String currently since it is used to populate the messageEvent.source attribute.

>> Source/WebCore/workers/service/ExtendableMessageEvent.h:57
>> +        Vector<RefPtr<MessagePort>> ports;
> 
> Would it be difficult to have a Vector<Ref<MessagePort>> instead?

Yes. This is a long-standing issue with our bindings converters.

>> Source/WebCore/workers/service/ServiceWorker.cpp:68
>> +    if (!scriptExecutionContext())
> 
> I am not sure we can get both a valid ExecState and a null scriptExecutionContext.
> If so, I would add it as a first check.
> 
> Can we get a ScriptExecutionContext& as first parameter and get an ExecState from it?
> Maybe add an ASSERT.

Ok.

>> Source/WebCore/workers/service/ServiceWorkerContainer.cpp:84
>> +        m_controller = ServiceWorker::create(*context, context->selectedServiceWorkerIdentifier());
> 
> Can we get a controller whose identifier is not context selectedServiceWorkerIdentifier?

I think this ways:
1. Register service worker A
2. Access container.controller
3. Register service worker B
4. Access container.controller

>> Source/WebCore/workers/service/context/ServiceWorkerThread.cpp:109
>> +        serviceWorkerGlobalScope.dispatchEvent(ExtendableMessageEvent::create(WTFMove(ports), WTFMove(message), sourceOrigin));
> 
> I guess sourceOrigin might be different from the origin of the service worker, like in the case of data URL iframes.
> If I am wrong, we might not even need to pass the origin.
> If the origin cannot be any random origin, maybe we should assert somehow that this is in a restricted set.
> I probably need to read the spec.

No, the source origin is not necessarily the same as the worker's origin. It is the origin of the calling script.

>> Source/WebCore/workers/service/context/ServiceWorkerThread.h:43
>> +using MessagePortChannelArray = Vector<std::unique_ptr<MessagePortChannel>, 1>;
> 
> Should probably be UniqueRef if possible.

This is a pre-existing WebCore type. I am merely forward declaring.

>> Source/WebCore/workers/service/context/ServiceWorkerThread.h:56
>> +    WEBCORE_EXPORT void postMessageToServiceWorkerGlobalScope(Ref<SerializedScriptValue>&&, std::unique_ptr<MessagePortChannelArray>&&, const String& sourceOrigin);
> 
> Here as well.

No, as mentioned in an earlier patch, channels can be null.

>> Source/WebKit/WebProcess/Storage/ServiceWorkerContextManager.cpp:76
>> +    auto* workerThread = m_workerThreadMap.get(serviceWorkerIdentifier);
> 
> Should we protect for a serviceWorkerIdentifier being zero?
> Even more valid for startFetch above.

It would hit an assertion in HashMap.
Comment 16 Chris Dumez 2017-10-24 13:29:45 PDT
Created attachment 324713 [details]
Patch
Comment 17 Chris Dumez 2017-10-24 14:22:59 PDT
Comment on attachment 324713 [details]
Patch

Clearing flags on attachment: 324713

Committed r223922: <https://trac.webkit.org/changeset/223922>
Comment 18 Chris Dumez 2017-10-24 14:23:01 PDT
All reviewed patches have been landed.  Closing bug.
Comment 19 Radar WebKit Bug Importer 2017-11-15 13:02:03 PST
<rdar://problem/35568649>