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/ipc/BlobChild.h"
|
|
|
|
#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
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace ipc;
|
|
|
|
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using namespace workers;
|
|
|
|
|
2015-07-06 21:28:41 +03:00
|
|
|
BroadcastChannelChild::BroadcastChannelChild(const nsACString& aOrigin)
|
|
|
|
: 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
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
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.
|
2015-06-17 13:44:27 +03:00
|
|
|
nsTArray<nsRefPtr<BlobImpl>> blobs;
|
2015-01-15 19:58:42 +03:00
|
|
|
if (!aData.blobsChild().IsEmpty()) {
|
2015-05-12 15:09:51 +03:00
|
|
|
blobs.SetCapacity(aData.blobsChild().Length());
|
2015-01-15 19:58:42 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0, len = aData.blobsChild().Length(); i < len; ++i) {
|
2015-05-12 15:11:03 +03:00
|
|
|
nsRefPtr<BlobImpl> impl =
|
2015-01-15 19:58:42 +03:00
|
|
|
static_cast<BlobChild*>(aData.blobsChild()[i])->GetBlobImpl();
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
blobs.AppendElement(impl);
|
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);
|
|
|
|
|
2015-01-15 19:58:42 +03: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 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
|
|
|
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.");
|
2015-01-15 19:58:40 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-09-10 23:50:58 +03:00
|
|
|
ipc::StructuredCloneData cloneData;
|
|
|
|
cloneData.BlobImpls().AppendElements(blobs);
|
2015-07-28 13:47:36 +03:00
|
|
|
|
2015-09-09 10:11:38 +03:00
|
|
|
const SerializedStructuredCloneBuffer& buffer = aData.data();
|
2015-09-10 23:50:58 +03:00
|
|
|
cloneData.UseExternalData(buffer.data, buffer.dataLength);
|
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());
|
2015-07-31 03:38:00 +03:00
|
|
|
if (buffer.dataLength) {
|
|
|
|
ErrorResult 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())) {
|
|
|
|
return true;
|
|
|
|
}
|
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;
|
|
|
|
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.");
|
2015-01-15 19:58:41 +03:00
|
|
|
return true;
|
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
|
|
|
|
|
|
|
return true;
|
2015-01-15 19:58:40 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
BroadcastChannelChild::ActorDestroy(ActorDestroyReason aWhy)
|
|
|
|
{
|
|
|
|
mActorDestroyed = true;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|