Bug 1136467 - ServiceWorker: client.postMessage should land in navigator.serviceWorker.onmessage. r=baku

This commit is contained in:
Catalin Badea 2015-02-25 21:38:37 +02:00
parent a58e64d193
commit 553cdbd8f5
4 changed files with 15 additions and 5 deletions

View File

@ -32,6 +32,7 @@ interface ServiceWorkerContainer : EventTarget {
attribute EventHandler oncontrollerchange;
attribute EventHandler onreloadpage;
attribute EventHandler onerror;
attribute EventHandler onmessage;
};
// Testing only.

View File

@ -5,6 +5,7 @@
*/
#include "ServiceWorkerClient.h"
#include "ServiceWorkerContainer.h"
#include "mozilla/dom/MessageEvent.h"
#include "nsGlobalWindow.h"
@ -59,16 +60,23 @@ public:
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();
@ -84,7 +92,7 @@ private:
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"),
@ -101,7 +109,7 @@ private:
event->SetTrusted(true);
bool status = false;
aTargetWindow->DispatchEvent(event, &status);
aTargetContainer->DispatchEvent(event, &status);
if (!status) {
return NS_ERROR_FAILURE;

View File

@ -31,6 +31,7 @@ public:
IMPL_EVENT_HANDLER(controllerchange)
IMPL_EVENT_HANDLER(reloadpage)
IMPL_EVENT_HANDLER(error)
IMPL_EVENT_HANDLER(message)
explicit ServiceWorkerContainer(nsPIDOMWindow* aWindow);

View File

@ -26,7 +26,7 @@
});
}
window.onmessage = function(msg) {
navigator.serviceWorker.onmessage = function(msg) {
// forward message to the test page.
parent.postMessage(msg.data, "*");
};