Bug 178794

Summary: Add initial support for serviceWorkerClient.postMessage()
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: WebCore Misc.Assignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Normal CC: beidson, buildbot, dbates, esprehn+autocc, ggaren, kangil.han, kondapallykalyan, webkit-bug-importer, youennf
Priority: P2 Keywords: InRadar, WebExposed
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=174541
Bug Depends on: 178534, 178812, 178816, 178839, 178876    
Bug Blocks:    
Attachments:
Description Flags
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
WIP Patch
none
Patch
none
Patch none

Chris Dumez
Reported 2017-10-25 08:03:29 PDT
Add initial support for serviceWorkerClient.postMessage(): - https://w3c.github.io/ServiceWorker/#client-postmessage
Attachments
WIP Patch (48.64 KB, patch)
2017-10-25 09:12 PDT, Chris Dumez
no flags
WIP Patch (61.18 KB, patch)
2017-10-25 09:39 PDT, Chris Dumez
no flags
WIP Patch (65.06 KB, patch)
2017-10-25 13:56 PDT, Chris Dumez
no flags
WIP Patch (56.14 KB, patch)
2017-10-25 14:51 PDT, Chris Dumez
no flags
WIP Patch (57.66 KB, patch)
2017-10-25 16:23 PDT, Chris Dumez
no flags
WIP Patch (61.12 KB, patch)
2017-10-25 17:03 PDT, Chris Dumez
no flags
WIP Patch (65.80 KB, patch)
2017-10-25 17:40 PDT, Chris Dumez
no flags
WIP Patch (62.14 KB, patch)
2017-10-25 20:14 PDT, Chris Dumez
no flags
WIP Patch (63.04 KB, patch)
2017-10-26 09:32 PDT, Chris Dumez
no flags
WIP Patch (61.42 KB, patch)
2017-10-26 09:33 PDT, Chris Dumez
no flags
WIP Patch (54.72 KB, patch)
2017-10-26 16:25 PDT, Chris Dumez
no flags
WIP Patch (54.71 KB, patch)
2017-10-26 19:03 PDT, Chris Dumez
no flags
Patch (62.63 KB, patch)
2017-10-26 19:18 PDT, Chris Dumez
no flags
Patch (62.54 KB, patch)
2017-10-27 09:19 PDT, Chris Dumez
no flags
Chris Dumez
Comment 1 2017-10-25 09:12:44 PDT
Created attachment 324830 [details] WIP Patch
Chris Dumez
Comment 2 2017-10-25 09:39:29 PDT
Created attachment 324833 [details] WIP Patch
Build Bot
Comment 3 2017-10-25 09:42:24 PDT
Attachment 324833 [details] did not pass style-queue: ERROR: Source/WebCore/dom/Document.cpp:444: This { should be at the end of the previous line [whitespace/braces] [4] Total errors found: 1 in 35 files If any of these errors are false positives, please file a bug against check-webkit-style.
Chris Dumez
Comment 4 2017-10-25 13:56:27 PDT
Created attachment 324889 [details] WIP Patch
Chris Dumez
Comment 5 2017-10-25 14:51:59 PDT
Created attachment 324899 [details] WIP Patch
Build Bot
Comment 6 2017-10-25 14:54:50 PDT
Attachment 324899 [details] did not pass style-queue: ERROR: Source/WebCore/workers/service/ServiceWorkerClient.h:50: Weird number of spaces at line-start. Are you using a 4-space indent? [whitespace/indent] [3] ERROR: Source/WebCore/dom/Document.cpp:444: This { should be at the end of the previous line [whitespace/braces] [4] Total errors found: 2 in 35 files If any of these errors are false positives, please file a bug against check-webkit-style.
Chris Dumez
Comment 7 2017-10-25 16:23:01 PDT
Created attachment 324917 [details] WIP Patch
Build Bot
Comment 8 2017-10-25 16:24:13 PDT
Attachment 324917 [details] did not pass style-queue: ERROR: Source/WebCore/dom/Document.cpp:444: This { should be at the end of the previous line [whitespace/braces] [4] Total errors found: 1 in 36 files If any of these errors are false positives, please file a bug against check-webkit-style.
Chris Dumez
Comment 9 2017-10-25 17:03:19 PDT
Created attachment 324926 [details] WIP Patch
Build Bot
Comment 10 2017-10-25 17:05:18 PDT
Attachment 324926 [details] did not pass style-queue: ERROR: Source/WebCore/dom/Document.cpp:444: This { should be at the end of the previous line [whitespace/braces] [4] Total errors found: 1 in 39 files If any of these errors are false positives, please file a bug against check-webkit-style.
Chris Dumez
Comment 11 2017-10-25 17:40:30 PDT
Created attachment 324929 [details] WIP Patch Ok. Will work on cleaning this up but communication works in both directions now.
Build Bot
Comment 12 2017-10-25 17:41:57 PDT
Attachment 324929 [details] did not pass style-queue: ERROR: Source/WebCore/dom/Document.cpp:444: This { should be at the end of the previous line [whitespace/braces] [4] Total errors found: 1 in 42 files If any of these errors are false positives, please file a bug against check-webkit-style.
Chris Dumez
Comment 13 2017-10-25 20:14:02 PDT
Created attachment 324947 [details] WIP Patch
Build Bot
Comment 14 2017-10-25 20:16:51 PDT
Attachment 324947 [details] did not pass style-queue: ERROR: Source/WebCore/dom/Document.cpp:444: This { should be at the end of the previous line [whitespace/braces] [4] Total errors found: 1 in 39 files If any of these errors are false positives, please file a bug against check-webkit-style.
Chris Dumez
Comment 15 2017-10-26 09:32:24 PDT
Created attachment 325017 [details] WIP Patch
Chris Dumez
Comment 16 2017-10-26 09:33:47 PDT
Created attachment 325019 [details] WIP Patch
Build Bot
Comment 17 2017-10-26 09:36:12 PDT
Attachment 325019 [details] did not pass style-queue: ERROR: Source/WebCore/dom/Document.cpp:444: This { should be at the end of the previous line [whitespace/braces] [4] Total errors found: 1 in 39 files If any of these errors are false positives, please file a bug against check-webkit-style.
Chris Dumez
Comment 18 2017-10-26 16:25:38 PDT
Created attachment 325077 [details] WIP Patch
Build Bot
Comment 19 2017-10-26 16:28:01 PDT
Attachment 325077 [details] did not pass style-queue: ERROR: Source/WebCore/dom/Document.cpp:444: This { should be at the end of the previous line [whitespace/braces] [4] Total errors found: 1 in 39 files If any of these errors are false positives, please file a bug against check-webkit-style.
Chris Dumez
Comment 20 2017-10-26 19:03:49 PDT
Created attachment 325097 [details] WIP Patch
Chris Dumez
Comment 21 2017-10-26 19:18:06 PDT
youenn fablet
Comment 22 2017-10-26 22:36:20 PDT
Comment on attachment 325099 [details] Patch View in context: https://bugs.webkit.org/attachment.cgi?id=325099&action=review > Source/WebCore/ChangeLog:12 > + via postMessage(). Cool! > Source/WebCore/dom/Document.cpp:445 > + static NeverDestroyed<HashMap<Identifier, Document*>> documents; We should be able to do HashMap<Identifier, reference_wrapper> at some point. > Source/WebCore/dom/Document.h:350 > + using Identifier = uint64_t; Does Identifier really help? If it helps, should we try to ensure that no implicit casting happens between uint64_t and Identifier? > Source/WebCore/workers/service/ServiceWorkerClient.cpp:71 > + Vector<RefPtr<MessagePort>> ports; Might want Vector<Ref> at some point. > Source/WebCore/workers/service/ServiceWorkerClient.cpp:87 > + callOnMainThread([message = message.releaseReturnValue(), destinationIdentifier = m_identifier, sourceIdentifier, sourceOrigin = context.origin().isolatedCopy()] () mutable { Probably fine or even better using callOnMainThread here. I think the expected abstraction is postMessageToWorkerObject though. Maybe we should do some refactoring. > Source/WebCore/workers/service/ServiceWorkerClientIdentifier.h:34 > + uint64_t scriptExecutionContextIdentifier; We really have a process id and a per-process environment identifier. It might be good to have something more generic, so that we can reuse this environment identifier in other places like network loads too. This can probably be improved later on when we have more concrete plans. > Source/WebCore/workers/service/context/SWContextManager.h:47 > + virtual void postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, Ref<SerializedScriptValue>&& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin) = 0; No need for destinationIdentifier name. > Source/WebCore/workers/service/server/SWClientConnection.cpp:123 > + if (!container) Can this be a real regular case? > Source/WebCore/workers/service/server/SWClientConnection.cpp:131 > + source = MessageEventSource { RefPtr<ServiceWorker> { ServiceWorker::create(*destinationDocument, sourceServiceWorkerIdentifier) } }; Every time we receive a message, we will create a new ServiceWorker. We should reuse them according their ids and if we create one, it should be based on a full ServiceWorkerRegistration. Can we fix that now? > Source/WebKit/Shared/WebCoreArgumentCoders.cpp:1906 > +void ArgumentCoder<ServiceWorkerClientIdentifier>::encode(Encoder& encoder, const ServiceWorkerClientIdentifier& identifier) The current style is to move this code closer to the definition, i.e. ServiceWorkerClientIdentifier.
Chris Dumez
Comment 23 2017-10-27 09:19:54 PDT
Chris Dumez
Comment 24 2017-10-27 09:26:37 PDT
(In reply to youenn fablet from comment #22) > Comment on attachment 325099 [details] > Patch > > View in context: > https://bugs.webkit.org/attachment.cgi?id=325099&action=review > > > Source/WebCore/ChangeLog:12 > > + via postMessage(). > > Cool! > > > Source/WebCore/dom/Document.cpp:445 > > + static NeverDestroyed<HashMap<Identifier, Document*>> documents; > > We should be able to do HashMap<Identifier, reference_wrapper> at some point. > > > Source/WebCore/dom/Document.h:350 > > + using Identifier = uint64_t; > > Does Identifier really help? > If it helps, should we try to ensure that no implicit casting happens > between uint64_t and Identifier? Ok, will drop Identifier alias. > > Source/WebCore/workers/service/ServiceWorkerClient.cpp:71 > > + Vector<RefPtr<MessagePort>> ports; > > Might want Vector<Ref> at some point. > > > Source/WebCore/workers/service/ServiceWorkerClient.cpp:87 > > + callOnMainThread([message = message.releaseReturnValue(), destinationIdentifier = m_identifier, sourceIdentifier, sourceOrigin = context.origin().isolatedCopy()] () mutable { > > Probably fine or even better using callOnMainThread here. > I think the expected abstraction is postMessageToWorkerObject though. > Maybe we should do some refactoring. > > > Source/WebCore/workers/service/ServiceWorkerClientIdentifier.h:34 > > + uint64_t scriptExecutionContextIdentifier; > > We really have a process id and a per-process environment identifier. > It might be good to have something more generic, so that we can reuse this > environment identifier in other places like network loads too. > This can probably be improved later on when we have more concrete plans. > > > Source/WebCore/workers/service/context/SWContextManager.h:47 > > + virtual void postMessageToServiceWorkerClient(const ServiceWorkerClientIdentifier& destinationIdentifier, Ref<SerializedScriptValue>&& message, uint64_t sourceServiceWorkerIdentifier, const String& sourceOrigin) = 0; > > No need for destinationIdentifier name. > > > Source/WebCore/workers/service/server/SWClientConnection.cpp:123 > > + if (!container) > > Can this be a real regular case? > > > Source/WebCore/workers/service/server/SWClientConnection.cpp:131 > > + source = MessageEventSource { RefPtr<ServiceWorker> { ServiceWorker::create(*destinationDocument, sourceServiceWorkerIdentifier) } }; > > Every time we receive a message, we will create a new ServiceWorker. > We should reuse them according their ids and if we create one, it should be > based on a full ServiceWorkerRegistration. > Can we fix that now? This is not an issue yet because we currently always reuse the controller. At some point, it'd be good to keep a list of ServiceWorkers somewhere I guess. > > > Source/WebKit/Shared/WebCoreArgumentCoders.cpp:1906 > > +void ArgumentCoder<ServiceWorkerClientIdentifier>::encode(Encoder& encoder, const ServiceWorkerClientIdentifier& identifier) > > The current style is to move this code closer to the definition, i.e. > ServiceWorkerClientIdentifier. Can you please clarify this?
Chris Dumez
Comment 25 2017-10-27 09:35:50 PDT
Comment on attachment 325166 [details] Patch Clearing flags on attachment: 325166 Committed r224113: <https://trac.webkit.org/changeset/224113>
Chris Dumez
Comment 26 2017-10-27 09:35:53 PDT
All reviewed patches have been landed. Closing bug.
youenn fablet
Comment 27 2017-10-27 10:08:36 PDT
> > Every time we receive a message, we will create a new ServiceWorker. > > We should reuse them according their ids and if we create one, it should be > > based on a full ServiceWorkerRegistration. > > Can we fix that now? > > This is not an issue yet because we currently always reuse the controller. > At some point, it'd be good to keep a list of ServiceWorkers somewhere I > guess. Maybe we should add A FIXME and not implement this code path then. > > > > > Source/WebKit/Shared/WebCoreArgumentCoders.cpp:1906 > > > +void ArgumentCoder<ServiceWorkerClientIdentifier>::encode(Encoder& encoder, const ServiceWorkerClientIdentifier& identifier) > > > > The current style is to move this code closer to the definition, i.e. > > ServiceWorkerClientIdentifier. > > Can you please clarify this? Add the encoder/decoder in ServiceWorkerClientIdentifier.h. See for instance FetchOptions.h
Chris Dumez
Comment 28 2017-10-27 10:13:56 PDT
(In reply to youenn fablet from comment #27) > > > Every time we receive a message, we will create a new ServiceWorker. > > > We should reuse them according their ids and if we create one, it should be > > > based on a full ServiceWorkerRegistration. > > > Can we fix that now? > > > > This is not an issue yet because we currently always reuse the controller. > > At some point, it'd be good to keep a list of ServiceWorkers somewhere I > > guess. > > Maybe we should add A FIXME and not implement this code path then. I do not agree. It is best to support the feature, even if not as efficient as it could be. Also note that a ServiceWorker is basically just a wrapper around an id and should be cheap to construct. > > > > > > > > Source/WebKit/Shared/WebCoreArgumentCoders.cpp:1906 > > > > +void ArgumentCoder<ServiceWorkerClientIdentifier>::encode(Encoder& encoder, const ServiceWorkerClientIdentifier& identifier) > > > > > > The current style is to move this code closer to the definition, i.e. > > > ServiceWorkerClientIdentifier. > > > > Can you please clarify this? > > Add the encoder/decoder in ServiceWorkerClientIdentifier.h. > See for instance FetchOptions.h Oh OK, I can do this in a follow-up. I did not know this policy.
youenn fablet
Comment 29 2017-10-27 10:23:38 PDT
> > Maybe we should add A FIXME and not implement this code path then. > > I do not agree. It is best to support the feature, even if not as efficient > as it could be. Also note that a ServiceWorker is basically just a wrapper > around an id and should be cheap to construct. IINM, for every message in that situation, we will have a different JS source object which will be observable from JS. If so, we are not very compliant, right? Why not simply returning a null source? If we are testing this code path, it might be good to add test showing the behavior and whether this is expected.
Chris Dumez
Comment 30 2017-10-27 10:24:53 PDT
(In reply to youenn fablet from comment #29) > > > Maybe we should add A FIXME and not implement this code path then. > > > > I do not agree. It is best to support the feature, even if not as efficient > > as it could be. Also note that a ServiceWorker is basically just a wrapper > > around an id and should be cheap to construct. > > IINM, for every message in that situation, we will have a different JS > source object which will be observable from JS. If so, we are not very > compliant, right? > Why not simply returning a null source? > > If we are testing this code path, it might be good to add test showing the > behavior and whether this is expected. If the source is null, then you cannot message the source back, which is much worse than getting a different JS wrapper every time.
Chris Dumez
Comment 31 2017-10-27 10:25:44 PDT
(In reply to Chris Dumez from comment #30) > (In reply to youenn fablet from comment #29) > > > > Maybe we should add A FIXME and not implement this code path then. > > > > > > I do not agree. It is best to support the feature, even if not as efficient > > > as it could be. Also note that a ServiceWorker is basically just a wrapper > > > around an id and should be cheap to construct. > > > > IINM, for every message in that situation, we will have a different JS > > source object which will be observable from JS. If so, we are not very > > compliant, right? > > Why not simply returning a null source? > > > > If we are testing this code path, it might be good to add test showing the > > behavior and whether this is expected. > > If the source is null, then you cannot message the source back, which is > much worse than getting a different JS wrapper every time. Also, if you look at the spec, you'll see that it says to create a new ServiceWorker object. Therefore, it may just be compliant.
Radar WebKit Bug Importer
Comment 32 2017-11-15 12:46:49 PST
Note You need to log in before you can comment on or make changes to this bug.