2015-01-15 19:58:40 +03:00
|
|
|
/* -*- Mode: C++; 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 "BroadcastChannelChild.h"
|
|
|
|
#include "BroadcastChannel.h"
|
|
|
|
#include "jsapi.h"
|
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using namespace workers;
|
|
|
|
|
|
|
|
BroadcastChannelChild::BroadcastChannelChild(const nsAString& aOrigin,
|
|
|
|
const nsAString& aChannel)
|
|
|
|
: mOrigin(aOrigin)
|
|
|
|
, mChannel(aChannel)
|
|
|
|
, mActorDestroyed(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
BroadcastChannelChild::~BroadcastChannelChild()
|
|
|
|
{
|
2015-01-15 19:58:41 +03:00
|
|
|
MOZ_ASSERT(!mBC);
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
BroadcastChannelChild::RecvNotify(const nsString& aMessage)
|
|
|
|
{
|
2015-01-15 19:58:41 +03:00
|
|
|
nsCOMPtr<DOMEventTargetHelper> helper = mBC;
|
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(helper);
|
|
|
|
|
2015-01-15 19:58:40 +03:00
|
|
|
// This object is going to be deleted soon. No notify is required.
|
2015-01-15 19:58:41 +03:00
|
|
|
if (!eventTarget) {
|
2015-01-15 19:58:40 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
// CheckInnerWindowCorrectness can be used also without a window when
|
|
|
|
// BroadcastChannel is running in a worker. In this case, it's a NOP.
|
|
|
|
if (NS_FAILED(mBC->CheckInnerWindowCorrectness())) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
if (NS_IsMainThread()) {
|
2015-01-15 19:58:40 +03:00
|
|
|
AutoJSAPI autoJS;
|
2015-01-15 19:58:41 +03:00
|
|
|
if (!autoJS.Init(mBC->GetParentObject())) {
|
2015-01-15 19:58:40 +03:00
|
|
|
NS_WARNING("Dropping message");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Notify(autoJS.cx(), aMessage);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
Notify(workerPrivate->GetJSContext(), aMessage);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannelChild::Notify(JSContext* aCx, const nsString& aMessage)
|
|
|
|
{
|
|
|
|
JS::Rooted<JSString*> str(aCx,
|
|
|
|
JS_NewUCStringCopyN(aCx, aMessage.get(),
|
|
|
|
aMessage.Length()));
|
|
|
|
if (!str) {
|
|
|
|
// OOM, no exception needed.
|
|
|
|
NS_WARNING("Failed allocating a JS string. Probably OOM.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
JS::Rooted<JS::Value> value(aCx, JS::StringValue(str));
|
|
|
|
|
|
|
|
RootedDictionary<MessageEventInit> init(aCx);
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mOrigin.Construct(mOrigin);
|
|
|
|
init.mData = value;
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
nsRefPtr<MessageEvent> event =
|
2015-01-15 19:58:41 +03:00
|
|
|
MessageEvent::Constructor(mBC, NS_LITERAL_STRING("message"), init, rv);
|
2015-01-15 19:58:40 +03:00
|
|
|
if (rv.Failed()) {
|
|
|
|
NS_WARNING("Failed to create a MessageEvent object.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
bool status;
|
2015-01-15 19:58:41 +03:00
|
|
|
mBC->DispatchEvent(static_cast<Event*>(event.get()), &status);
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannelChild::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
mActorDestroyed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // dom namespace
|
|
|
|
} // mozilla namespace
|