Bug 178534

Summary: Add initial implementation for serviceWorker.postMessage()
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: WebCore Misc.Assignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Normal CC: beidson, buildbot, commit-queue, dbates, esprehn+autocc, ggaren, kangil.han, webkit-bug-importer, youennf
Priority: P2 Keywords: InRadar, WebExposed
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
URL: https://w3c.github.io/ServiceWorker/#service-worker-postmessage
See Also: https://bugs.webkit.org/show_bug.cgi?id=174541
Bug Depends on: 178475, 178491, 178552    
Bug Blocks: 178794    
Attachments:
Description Flags
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch
none
Patch none

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>