2015-05-03 19:32:37 +00: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 16:58:40 +00: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 16:58:41 +00:00
|
|
|
#include "mozilla/dom/ipc/BlobChild.h"
|
|
|
|
#include "mozilla/dom/File.h"
|
2015-01-15 16:58:40 +00:00
|
|
|
#include "mozilla/dom/MessageEvent.h"
|
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2015-01-15 16:58:41 +00:00
|
|
|
#include "mozilla/dom/WorkerPrivate.h"
|
|
|
|
#include "mozilla/dom/WorkerScope.h"
|
2015-01-15 16:58:40 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundChild.h"
|
2015-09-10 20:50:58 +00:00
|
|
|
#include "mozilla/dom/ipc/StructuredCloneData.h"
|
2015-01-15 16:58:40 +00:00
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using namespace workers;
|
|
|
|
|
2015-07-06 18:28:41 +00:00
|
|
|
BroadcastChannelChild::BroadcastChannelChild(const nsACString& aOrigin)
|
|
|
|
: mActorDestroyed(false)
|
2015-01-15 16:58:40 +00:00
|
|
|
{
|
2015-07-06 18:28:41 +00:00
|
|
|
CopyUTF8toUTF16(aOrigin, mOrigin);
|
2015-01-15 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BroadcastChannelChild::~BroadcastChannelChild()
|
|
|
|
{
|
2015-01-15 16:58:41 +00:00
|
|
|
MOZ_ASSERT(!mBC);
|
2015-01-15 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-15 16:58:41 +00:00
|
|
|
BroadcastChannelChild::RecvNotify(const ClonedMessageData& aData)
|
2015-01-15 16:58:40 +00:00
|
|
|
{
|
2015-01-15 16:58:42 +00:00
|
|
|
// Make sure to retrieve all blobs from the message before returning to avoid
|
|
|
|
// leaking their actors.
|
2015-10-18 05:24:48 +00:00
|
|
|
nsTArray<RefPtr<BlobImpl>> blobs;
|
2015-01-15 16:58:42 +00:00
|
|
|
if (!aData.blobsChild().IsEmpty()) {
|
2015-05-12 12:09:51 +00:00
|
|
|
blobs.SetCapacity(aData.blobsChild().Length());
|
2015-01-15 16:58:42 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0, len = aData.blobsChild().Length(); i < len; ++i) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<BlobImpl> impl =
|
2015-01-15 16:58:42 +00:00
|
|
|
static_cast<BlobChild*>(aData.blobsChild()[i])->GetBlobImpl();
|
|
|
|
|
2015-06-17 10:44:27 +00:00
|
|
|
blobs.AppendElement(impl);
|
2015-01-15 16:58:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 16:58:41 +00:00
|
|
|
nsCOMPtr<DOMEventTargetHelper> helper = mBC;
|
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(helper);
|
|
|
|
|
2015-01-15 16:58:42 +00:00
|
|
|
// This object has been already closed by content or is going to be deleted
|
|
|
|
// soon. No notify is required.
|
|
|
|
if (!eventTarget || mBC->IsClosed()) {
|
2015-01-15 16:58:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-15 16:58:41 +00: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 16:58:40 +00:00
|
|
|
|
2015-10-13 11:04:17 +00:00
|
|
|
mBC->RemoveDocFromBFCache();
|
|
|
|
|
2015-01-15 16:58:41 +00:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject;
|
|
|
|
|
2015-01-15 16:58:41 +00:00
|
|
|
if (NS_IsMainThread()) {
|
2015-01-15 16:58:41 +00:00
|
|
|
globalObject = do_QueryInterface(mBC->GetParentObject());
|
|
|
|
} else {
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
globalObject = workerPrivate->GlobalScope();
|
|
|
|
}
|
2015-01-15 16:58:40 +00:00
|
|
|
|
2015-01-15 16:58:41 +00:00
|
|
|
if (!globalObject || !jsapi.Init(globalObject)) {
|
|
|
|
NS_WARNING("Failed to initialize AutoJSAPI object.");
|
2015-01-15 16:58:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-10 20:50:58 +00:00
|
|
|
ipc::StructuredCloneData cloneData;
|
|
|
|
cloneData.BlobImpls().AppendElements(blobs);
|
2015-07-28 10:47:36 +00:00
|
|
|
|
2015-09-09 07:11:38 +00:00
|
|
|
const SerializedStructuredCloneBuffer& buffer = aData.data();
|
2015-09-10 20:50:58 +00:00
|
|
|
cloneData.UseExternalData(buffer.data, buffer.dataLength);
|
2015-09-09 07:11:38 +00:00
|
|
|
|
|
|
|
JSContext* cx = jsapi.cx();
|
2015-01-15 16:58:41 +00:00
|
|
|
JS::Rooted<JS::Value> value(cx, JS::NullValue());
|
2015-07-31 00:38:00 +00:00
|
|
|
if (buffer.dataLength) {
|
|
|
|
ErrorResult rv;
|
2015-09-10 20:50:58 +00:00
|
|
|
cloneData.Read(cx, &value, rv);
|
2015-07-31 00:38:00 +00:00
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-15 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 16:58:41 +00:00
|
|
|
RootedDictionary<MessageEventInit> init(cx);
|
2015-01-15 16:58:40 +00:00
|
|
|
init.mBubbles = false;
|
|
|
|
init.mCancelable = false;
|
|
|
|
init.mOrigin.Construct(mOrigin);
|
|
|
|
init.mData = value;
|
|
|
|
|
|
|
|
ErrorResult rv;
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<MessageEvent> event =
|
2015-01-15 16:58:41 +00:00
|
|
|
MessageEvent::Constructor(mBC, NS_LITERAL_STRING("message"), init, rv);
|
2015-01-15 16:58:40 +00:00
|
|
|
if (rv.Failed()) {
|
|
|
|
NS_WARNING("Failed to create a MessageEvent object.");
|
2015-01-15 16:58:41 +00:00
|
|
|
return true;
|
2015-01-15 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
event->SetTrusted(true);
|
|
|
|
|
|
|
|
bool status;
|
2015-01-15 16:58:41 +00:00
|
|
|
mBC->DispatchEvent(static_cast<Event*>(event.get()), &status);
|
2015-01-15 16:58:41 +00:00
|
|
|
|
|
|
|
return true;
|
2015-01-15 16:58:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannelChild::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
mActorDestroyed = true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 15:25:42 +00:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|