Bug 1136467 - ServiceWorker: client.postMessage should land in navigator.serviceWorker.onmessage. r=baku
authorCatalin Badea <catalin.badea392@gmail.com>
Wed, 25 Feb 2015 21:38:37 +0200
changeset 230783 720b1b0c423d370130ac0f69851a95798cd8155a
parent 230782 dc2c018e12c7f288781f580b06f17c2dc712319a
child 230784 136c512180899c189f32ceeaa3755d413f06cf51
push id28334
push userkwierso@gmail.com
push dateThu, 26 Feb 2015 01:18:38 +0000
treeherdermozilla-central@b11c67d6d2f5 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1136467
milestone39.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1136467 - ServiceWorker: client.postMessage should land in navigator.serviceWorker.onmessage. r=baku
dom/webidl/ServiceWorkerContainer.webidl
dom/workers/ServiceWorkerClient.cpp
dom/workers/ServiceWorkerContainer.h
dom/workers/test/serviceworkers/sw_clients/service_worker_controlled.html
--- a/dom/webidl/ServiceWorkerContainer.webidl
+++ b/dom/webidl/ServiceWorkerContainer.webidl
@@ -27,16 +27,17 @@ interface ServiceWorkerContainer : Event
   Promise<ServiceWorkerRegistration> getRegistration(optional USVString documentURL = "");
 
   [Throws]
    Promise<sequence<ServiceWorkerRegistration>> getRegistrations();
 
   attribute EventHandler oncontrollerchange;
   attribute EventHandler onreloadpage;
   attribute EventHandler onerror;
+  attribute EventHandler onmessage;
 };
 
 // Testing only.
 partial interface ServiceWorkerContainer {
   [Throws,Pref="dom.serviceWorkers.testing.enabled"]
   Promise<any> clearAllServiceWorkerData();
 
   [Throws,Pref="dom.serviceWorkers.testing.enabled"]
--- a/dom/workers/ServiceWorkerClient.cpp
+++ b/dom/workers/ServiceWorkerClient.cpp
@@ -1,15 +1,16 @@
 /* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this file,
  * You can obtain one at http://mozilla.org/MPL/2.0/.
  */
 
 #include "ServiceWorkerClient.h"
+#include "ServiceWorkerContainer.h"
 
 #include "mozilla/dom/MessageEvent.h"
 #include "nsGlobalWindow.h"
 #include "WorkerPrivate.h"
 
 #include "mozilla/dom/ClientBinding.h"
 
 using namespace mozilla;
@@ -54,59 +55,66 @@ public:
   Run()
   {
     AssertIsOnMainThread();
     nsGlobalWindow* window = nsGlobalWindow::GetInnerWindowWithId(mId);
     if (!window) {
       return NS_ERROR_FAILURE;
     }
 
+    ErrorResult result;
+    dom::Navigator* navigator = window->GetNavigator(result);
+    if (NS_WARN_IF(result.Failed())) {
+      return result.ErrorCode();
+    }
+
+    nsRefPtr<ServiceWorkerContainer> container = navigator->ServiceWorker();
     AutoJSAPI jsapi;
     jsapi.Init(window);
     JSContext* cx = jsapi.cx();
 
-    return DispatchDOMEvent(cx, window);
+    return DispatchDOMEvent(cx, container);
   }
 
 private:
   NS_IMETHOD
-  DispatchDOMEvent(JSContext* aCx, nsGlobalWindow* aTargetWindow)
+  DispatchDOMEvent(JSContext* aCx, ServiceWorkerContainer* aTargetContainer)
   {
     AssertIsOnMainThread();
 
     // Release reference to objects that were AddRef'd for
     // cloning into worker when array goes out of scope.
     nsTArray<nsCOMPtr<nsISupports>> clonedObjects;
     clonedObjects.SwapElements(mClonedObjects);
 
     JS::Rooted<JS::Value> messageData(aCx);
     if (!mBuffer.read(aCx, &messageData,
                       WorkerStructuredCloneCallbacks(true))) {
       xpc::Throw(aCx, NS_ERROR_DOM_DATA_CLONE_ERR);
       return NS_ERROR_FAILURE;
     }
 
-    nsCOMPtr<nsIDOMMessageEvent> event = new MessageEvent(aTargetWindow,
+    nsCOMPtr<nsIDOMMessageEvent> event = new MessageEvent(aTargetContainer,
                                                           nullptr, nullptr);
     nsresult rv =
       event->InitMessageEvent(NS_LITERAL_STRING("message"),
                               false /* non-bubbling */,
                               false /* not cancelable */,
                               messageData,
                               EmptyString(),
                               EmptyString(),
                               nullptr);
     if (NS_FAILED(rv)) {
       xpc::Throw(aCx, rv);
       return NS_ERROR_FAILURE;
     }
 
     event->SetTrusted(true);
     bool status = false;
-    aTargetWindow->DispatchEvent(event, &status);
+    aTargetContainer->DispatchEvent(event, &status);
 
     if (!status) {
       return NS_ERROR_FAILURE;
     }
 
     return NS_OK;
   }
 };
--- a/dom/workers/ServiceWorkerContainer.h
+++ b/dom/workers/ServiceWorkerContainer.h
@@ -26,16 +26,17 @@ class ServiceWorkerContainer MOZ_FINAL :
 {
 public:
   NS_DECL_ISUPPORTS_INHERITED
   NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ServiceWorkerContainer, DOMEventTargetHelper)
 
   IMPL_EVENT_HANDLER(controllerchange)
   IMPL_EVENT_HANDLER(reloadpage)
   IMPL_EVENT_HANDLER(error)
+  IMPL_EVENT_HANDLER(message)
 
   explicit ServiceWorkerContainer(nsPIDOMWindow* aWindow);
 
   virtual JSObject*
   WrapObject(JSContext* aCx) MOZ_OVERRIDE;
 
   already_AddRefed<Promise>
   Register(const nsAString& aScriptURL,
--- a/dom/workers/test/serviceworkers/sw_clients/service_worker_controlled.html
+++ b/dom/workers/test/serviceworkers/sw_clients/service_worker_controlled.html
@@ -21,17 +21,17 @@
   }
 
   window.onload = function() {
     navigator.serviceWorker.ready.then(function(swr) {
       parent.postMessage("READY", "*");
     });
   }
 
-  window.onmessage = function(msg) {
+  navigator.serviceWorker.onmessage = function(msg) {
     // forward message to the test page.
     parent.postMessage(msg.data, "*");
   };
 </script>
 
 </head>
 <body>
 </body>