2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2015-01-15 19:58:40 +03:00
|
|
|
/* 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"
|
2015-01-15 19:58:41 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2015-01-15 19:58:40 +03:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2015-01-15 19:58:41 +03:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2015-01-15 19:58:40 +03:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2015-09-10 23:50:58 +03:00
|
|
|
#include "mozilla/dom/ipc/StructuredCloneData.h"
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
2015-07-06 21:28:41 +03:00
|
|
|
BroadcastChannelChild::BroadcastChannelChild(const nsACString& aOrigin)
|
2016-01-12 21:16:59 +03:00
|
|
|
: mBC(nullptr)
|
|
|
|
, mActorDestroyed(false)
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
2015-07-06 21:28:41 +03:00
|
|
|
CopyUTF8toUTF16(aOrigin, mOrigin);
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
BroadcastChannelChild::~BroadcastChannelChild()
|
|
|
|
{
|
2015-01-15 19:58:41 +03:00
|
|
|
MOZ_ASSERT(!mBC);
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
mozilla::ipc::IPCResult
|
2015-01-15 19:58:41 +03:00
|
|
|
BroadcastChannelChild::RecvNotify(const ClonedMessageData& aData)
|
2015-01-15 19:58:40 +03:00
|
|
|
{
|
2015-01-15 19:58:42 +03:00
|
|
|
// Make sure to retrieve all blobs from the message before returning to avoid
|
|
|
|
// leaking their actors.
|
2016-11-08 11:09:31 +03:00
|
|
|
ipc::StructuredCloneDataNoTransfers cloneData;
|
|
|
|
cloneData.BorrowFromClonedMessageDataForBackgroundChild(aData);
|
2015-01-15 19:58:42 +03:00
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
nsCOMPtr<DOMEventTargetHelper> helper = mBC;
|
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(helper);
|
|
|
|
|
2016-06-07 01:37:47 +03:00
|
|
|
// The object is going to be deleted soon. No notify is required.
|
|
|
|
if (!eventTarget) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
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())) {
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-01-15 19:58:41 +03:00
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
|
2015-10-13 14:04:17 +03:00
|
|
|
mBC->RemoveDocFromBFCache();
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject;
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
if (NS_IsMainThread()) {
|
2015-01-15 19:58:41 +03:00
|
|
|
globalObject = do_QueryInterface(mBC->GetParentObject());
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
globalObject = workerPrivate->GlobalScope();
|
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
if (!globalObject || !jsapi.Init(globalObject)) {
|
|
|
|
NS_WARNING("Failed to initialize AutoJSAPI object.");
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
JSContext* cx = jsapi.cx();
|
2015-01-15 19:58:41 +03:00
|
|
|
JS::Rooted<JS::Value> value(cx, JS::NullValue());
|
2016-11-08 11:09:31 +03:00
|
|
|
if (cloneData.DataLength()) {
|
2017-09-13 06:59:06 +03:00
|
|
|
IgnoredErrorResult rv;
|
2015-09-10 23:50:58 +03:00
|
|
|
cloneData.Read(cx, &value, rv);
|
2015-07-31 03:38:00 +03:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
2017-09-13 06:59:06 +03:00
|
|
|
DispatchError(cx);
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-07-31 03:38:00 +03:00
|
|
|
}
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
2015-01-15 19:58:41 +03:00
|
|
|
RootedDictionary<MessageEventInit> init(cx);
|
2015-01-15 19:58:40 +03:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
2016-10-25 08:48:05 +03:00
|
|
|
init.mOrigin = mOrigin;
|
2015-01-15 19:58:40 +03:00
|
|
|
init.mData = value;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessageEvent> event =
|
2017-03-10 04:50:13 +03:00
|
|
|
MessageEvent::Constructor(mBC, NS_LITERAL_STRING("message"), init);
|
2015-01-15 19:58:40 +03:00
|
|
|
|
|
|
|
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:41 +03:00
|
|
|
|
2016-11-15 06:26:00 +03:00
|
|
|
return IPC_OK();
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannelChild::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
mActorDestroyed = true;
|
|
|
|
}
|
|
|
|
|
2017-09-13 06:59:06 +03:00
|
|
|
void
|
|
|
|
BroadcastChannelChild::DispatchError(JSContext* aCx)
|
|
|
|
{
|
|
|
|
RootedDictionary<MessageEventInit> init(aCx);
|
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mOrigin = mOrigin;
|
|
|
|
|
|
|
|
RefPtr<Event> event =
|
|
|
|
MessageEvent::Constructor(mBC, NS_LITERAL_STRING("messageerror"), init);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
bool dummy;
|
|
|
|
mBC->DispatchEvent(event, &dummy);
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|