Skip to content

Commit

Permalink
service worker: Queue Client.postMessage() until onmessage is set.
Browse files Browse the repository at this point in the history
This is a follow-up to
https://chromium-review.googlesource.com/c/chromium/src/+/1441657.

This enables the queue when onmessage is assigned to. It adheres to
the specification at
https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-onmessage:
"The first time the context object’s onmessage IDL attribute is set, its
client message queue must be enabled."

This makes the test cases about "setting onmessage" in
the WPT postmessage-to-client-message-queue.https.html pass, but the
test file will still time out until startMessages() is implemented.

Bug: 915685
Change-Id: I4c4930b48df68ee351676e5c3edfb28969fe3610
Reviewed-on: https://chromium-review.googlesource.com/c/1441734
Commit-Queue: Matt Falkenhagen <[email protected]>
Reviewed-by: Hiroki Nakagawa <[email protected]>
Cr-Commit-Position: refs/heads/master@{#626929}
  • Loading branch information
mfalken authored and Commit Bot committed Jan 29, 2019
1 parent 38db903 commit b9aff24
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,18 @@ const AtomicString& ServiceWorkerContainer::InterfaceName() const {
return event_target_names::kServiceWorkerContainer;
}

void ServiceWorkerContainer::setOnmessage(EventListener* listener) {
SetAttributeEventListener(event_type_names::kMessage, listener);
// https://w3c.github.io/ServiceWorker/#dom-serviceworkercontainer-onmessage:
// "The first time the context object’s onmessage IDL attribute is set, its
// client message queue must be enabled."
EnableClientMessageQueue();
}

EventListener* ServiceWorkerContainer::onmessage() {
return GetAttributeEventListener(event_type_names::kMessage);
}

ServiceWorkerRegistration*
ServiceWorkerContainer::GetOrCreateServiceWorkerRegistration(
WebServiceWorkerRegistrationObjectInfo info) {
Expand Down Expand Up @@ -600,6 +612,10 @@ ServiceWorkerContainer::CreateReadyProperty() {

void ServiceWorkerContainer::EnableClientMessageQueue() {
dom_content_loaded_observer_ = nullptr;
if (is_client_message_queue_enabled_) {
DCHECK(queued_messages_.empty());
return;
}
is_client_message_queue_enabled_ = true;
std::vector<std::unique_ptr<MessageFromServiceWorker>> messages;
messages.swap(queued_messages_);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ class MODULES_EXPORT ServiceWorkerContainer final
const AtomicString& InterfaceName() const override;

DEFINE_ATTRIBUTE_EVENT_LISTENER(controllerchange, kControllerchange);
// TODO(falken): Enable the client message queue when onmessage is assigned
// to.
DEFINE_ATTRIBUTE_EVENT_LISTENER(message, kMessage);

void setOnmessage(EventListener* listener);
EventListener* onmessage();

// Returns the ServiceWorkerRegistration object described by the given info.
// Creates a new object if needed, or else returns the existing one.
Expand Down

0 comments on commit b9aff24

Please sign in to comment.