Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unclear how Client.postMessage looks up its destination #1042

Closed
isonmad opened this issue Dec 31, 2016 · 4 comments
Closed

unclear how Client.postMessage looks up its destination #1042

isonmad opened this issue Dec 31, 2016 · 4 comments

Comments

@isonmad
Copy link
Contributor

isonmad commented Dec 31, 2016

  1. Let |destination| be the {{ServiceWorkerContainer}} object whose [=ServiceWorkerContainer/service worker client=] is the context object's [=Client/service worker client=].
  2. If |destination| is null, throw an "{{InvalidStateError}}" exception.

It sort of sounds like looking through the set of all ServiceWorkerContainers that possibly exist, and it's only implied that it sets destination to null if it didn't find any. Is it possible to make the mapping from service worker client -> ServiceWorkerContainer more explicit?

It's not clear in what cases it wouldn't find one.

@jungkees
Copy link
Collaborator

jungkees commented Jan 2, 2017

Is it possible to make the mapping from service worker client -> ServiceWorkerContainer more explicit?

Please see the definition of the association between a ServiceWorkerContainer and a service worker client: https://w3c.github.io/ServiceWorker/#serviceworkercontainer-service-worker-client. client.postMessage() basically tries to reference the ServiceWorkerContainer object that is associated with the service worker client in that sense. Informally speaking, that's the captured client's document's ServiceWorkerContainer object.

It's not clear in what cases it wouldn't find one.

Client objects retrieved from client APIs are snapshots. So, the states are captured in the Client creation time: https://w3c.github.io/ServiceWorker/#create-client. So, by the time the steps of the postMessage() is running, the client may already have been closed. The null-check is a guard against this occasion.

Note that by introducing the "reserved client concept" lately, the message queuing mechanism for client.postMessage() needs changes since it can be possible that a ServiceWorkerContainer object hasn't been created by the time 'postMessage()` is run. The issue filed in #1035.

@isonmad
Copy link
Contributor Author

isonmad commented Jan 3, 2017

I did read that definition, it makes for an explicit ServiceWorkerContainer -> service worker client mapping, and while it describes a way to do the inverse mapping as part of the definition, that inverse isn't actually given its own definition for use elsewhere. Whereas something like the 1-to-1-to-1 relation for Realm/global/settings object defines all six mappings between the associated objects explicitly. As is, it's like saying "let x be the JavaScript realm whose [[GlobalObject]] field is window" instead of the more straightforward "window's Realm".

The null-check still makes no sense, because it didn't say e.g. "let destination be the [[SWContainer]] internal slot of the service worker client", which could be either a ServiceWorkerContainer or null, with a step elsewhere in the spec that sets it to null when the client closes. It uses much more convoluted and indirect wording, and you have to infer that it's supposed to set destination to null if the wording as-is, taken literally, is effectively impossible, because there is no Container that fits that description anymore.

@jungkees
Copy link
Collaborator

jungkees commented Jan 3, 2017

it makes for an explicit ServiceWorkerContainer -> service worker client mapping

What spec says is not a mapping between a ServiceWorkerContainer and a service worker client. ServiceWorkerContainer's associated service worker client explicitly defines a reference to the service worker client of the Window/Navigator where ServiceWorkerClient is tied. I don't see a problem in this definition.

The null-check still makes no sense

Agreed. The destination should be initialized to null and should remain null if it doesn't get a target object. I'll address that point.

@jungkees jungkees added this to the Version 1 milestone Jan 3, 2017
jungkees added a commit that referenced this issue Feb 2, 2018
This change addresses the issue described in the second paragraph of
#1042 (comment).
This change gets |destination| variable initialized to null if the
ServiceWorkerContainer object does not exist.

Fixes #1042.
jungkees added a commit to jungkees/web-platform-tests that referenced this issue Mar 23, 2018
Client.postMessage to an unloaded Client object must throw an
"InvalidStateError" DOMException. (See https://w3c.github.io/ServiceWorker/#dom-client-postmessage.)
This adds a test for that requirement.

Related spec issue: w3c/ServiceWorker#1042.
Related spec PR: w3c/ServiceWorker#1274.
jungkees added a commit that referenced this issue Mar 23, 2018
This change addresses the issue described in the second paragraph of
#1042 (comment).
This change gets |destination| variable initialized to null if the
ServiceWorkerContainer object does not exist.

Fixes #1042.
@wanderview
Copy link
Member

See #1291. I don't think browsers can reasonably look up the destination synchronously. Cross-thread and cross-process issues require the lookup to be asynchronous.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants