2013-09-03 05:38:49 -07: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 "MessagePort.h"
|
2013-11-14 20:42:34 -08:00
|
|
|
#include "MessageEvent.h"
|
2014-10-08 09:15:22 -07:00
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2014-03-04 16:37:43 -08:00
|
|
|
#include "mozilla/dom/Event.h"
|
2014-10-08 09:15:23 -07:00
|
|
|
#include "mozilla/dom/File.h"
|
2013-09-03 05:38:49 -07:00
|
|
|
#include "mozilla/dom/MessageChannel.h"
|
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
2013-11-14 20:42:34 -08:00
|
|
|
#include "mozilla/dom/MessagePortList.h"
|
2013-09-03 05:38:54 -07:00
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
2013-09-03 05:38:49 -07:00
|
|
|
#include "nsContentUtils.h"
|
2013-11-14 20:42:34 -08:00
|
|
|
#include "nsGlobalWindow.h"
|
2013-09-03 05:38:54 -07:00
|
|
|
#include "nsPresContext.h"
|
2014-06-11 09:22:00 -07:00
|
|
|
#include "ScriptSettings.h"
|
2013-09-03 05:38:54 -07:00
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMFileList.h"
|
|
|
|
#include "nsIPresShell.h"
|
2013-09-03 05:38:49 -07:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-09-03 05:38:58 -07:00
|
|
|
class DispatchEventRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
friend class MessagePort;
|
|
|
|
|
|
|
|
public:
|
2014-08-05 06:19:51 -07:00
|
|
|
explicit DispatchEventRunnable(MessagePort* aPort)
|
2013-09-03 05:38:58 -07:00
|
|
|
: mPort(aPort)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
nsRefPtr<DispatchEventRunnable> mKungFuDeathGrip(this);
|
|
|
|
|
|
|
|
mPort->mDispatchRunnable = nullptr;
|
|
|
|
mPort->Dispatch();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<MessagePort> mPort;
|
|
|
|
};
|
|
|
|
|
2013-09-03 05:38:54 -07:00
|
|
|
class PostMessageRunnable : public nsRunnable
|
|
|
|
{
|
2013-09-03 05:38:58 -07:00
|
|
|
friend class MessagePort;
|
|
|
|
|
2013-09-03 05:38:54 -07:00
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2013-09-03 05:38:56 -07:00
|
|
|
PostMessageRunnable()
|
2013-09-03 05:38:54 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~PostMessageRunnable()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-03-04 19:28:20 -08:00
|
|
|
JSAutoStructuredCloneBuffer& Buffer()
|
2013-09-03 05:38:54 -07:00
|
|
|
{
|
2014-03-04 19:28:20 -08:00
|
|
|
return mBuffer;
|
2013-09-03 05:38:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StoreISupports(nsISupports* aSupports)
|
|
|
|
{
|
|
|
|
mSupportsArray.AppendElement(aSupports);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:56 -07:00
|
|
|
void Dispatch(MessagePort* aPort)
|
|
|
|
{
|
|
|
|
mPort = aPort;
|
|
|
|
NS_DispatchToCurrentThread(this);
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:54 -07:00
|
|
|
private:
|
|
|
|
nsRefPtr<MessagePort> mPort;
|
2014-03-04 19:28:20 -08:00
|
|
|
JSAutoStructuredCloneBuffer mBuffer;
|
2013-09-03 05:38:54 -07:00
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mSupportsArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct StructuredCloneInfo
|
|
|
|
{
|
|
|
|
PostMessageRunnable* mEvent;
|
|
|
|
MessagePort* mPort;
|
2013-11-14 20:42:34 -08:00
|
|
|
nsRefPtrHashtable<nsRefPtrHashKey<MessagePortBase>, MessagePortBase> mPorts;
|
2013-09-03 05:38:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
PostMessageReadStructuredClone(JSContext* cx,
|
|
|
|
JSStructuredCloneReader* reader,
|
|
|
|
uint32_t tag,
|
|
|
|
uint32_t data,
|
|
|
|
void* closure)
|
|
|
|
{
|
2014-10-08 09:15:22 -07:00
|
|
|
StructuredCloneInfo* scInfo = static_cast<StructuredCloneInfo*>(closure);
|
|
|
|
NS_ASSERTION(scInfo, "Must have scInfo!");
|
|
|
|
|
|
|
|
if (tag == SCTAG_DOM_BLOB) {
|
|
|
|
NS_ASSERTION(!data, "Data should be empty");
|
|
|
|
|
2014-10-08 09:15:23 -07:00
|
|
|
// What we get back from the reader is a FileImpl.
|
|
|
|
// From that we create a new File.
|
|
|
|
FileImpl* blobImpl;
|
2014-10-08 09:15:22 -07:00
|
|
|
if (JS_ReadBytes(reader, &blobImpl, sizeof(blobImpl))) {
|
|
|
|
MOZ_ASSERT(blobImpl);
|
|
|
|
|
2014-10-08 09:15:23 -07:00
|
|
|
// nsRefPtr<File> needs to go out of scope before toObjectOrNull() is
|
2014-10-08 09:15:22 -07:00
|
|
|
// called because the static analysis thinks dereferencing XPCOM objects
|
|
|
|
// can GC (because in some cases it can!), and a return statement with a
|
|
|
|
// JSObject* type means that JSObject* is on the stack as a raw pointer
|
|
|
|
// while destructors are running.
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
{
|
2014-10-08 09:15:23 -07:00
|
|
|
nsRefPtr<File> blob = new File(scInfo->mPort->GetParentObject(),
|
2014-10-08 09:15:22 -07:00
|
|
|
blobImpl);
|
|
|
|
if (!WrapNewBindingObject(cx, blob, &val)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &val.toObject();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag == SCTAG_DOM_FILELIST) {
|
2013-09-03 05:38:54 -07:00
|
|
|
NS_ASSERTION(!data, "Data should be empty");
|
|
|
|
|
|
|
|
nsISupports* supports;
|
|
|
|
if (JS_ReadBytes(reader, &supports, sizeof(supports))) {
|
2014-04-09 21:58:41 -07:00
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
if (NS_SUCCEEDED(nsContentUtils::WrapNative(cx, supports, &val))) {
|
2014-04-27 19:58:52 -07:00
|
|
|
return val.toObjectOrNull();
|
2013-09-03 05:38:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const JSStructuredCloneCallbacks* runtimeCallbacks =
|
|
|
|
js::GetContextStructuredCloneCallbacks(cx);
|
|
|
|
|
|
|
|
if (runtimeCallbacks) {
|
|
|
|
return runtimeCallbacks->read(cx, reader, tag, data, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
PostMessageWriteStructuredClone(JSContext* cx,
|
|
|
|
JSStructuredCloneWriter* writer,
|
|
|
|
JS::Handle<JSObject*> obj,
|
|
|
|
void *closure)
|
|
|
|
{
|
|
|
|
StructuredCloneInfo* scInfo = static_cast<StructuredCloneInfo*>(closure);
|
|
|
|
NS_ASSERTION(scInfo, "Must have scInfo!");
|
|
|
|
|
2014-10-08 09:15:22 -07:00
|
|
|
// See if this is a File/Blob object.
|
|
|
|
{
|
2014-10-08 09:15:23 -07:00
|
|
|
File* blob = nullptr;
|
2014-10-08 09:15:22 -07:00
|
|
|
if (NS_SUCCEEDED(UNWRAP_OBJECT(Blob, obj, blob))) {
|
2014-10-08 09:15:23 -07:00
|
|
|
FileImpl* blobImpl = blob->Impl();
|
2014-10-08 09:15:22 -07:00
|
|
|
if (JS_WriteUint32Pair(writer, SCTAG_DOM_BLOB, 0) &&
|
|
|
|
JS_WriteBytes(writer, &blobImpl, sizeof(blobImpl))) {
|
|
|
|
scInfo->mEvent->StoreISupports(blobImpl);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:54 -07:00
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrappedNative;
|
|
|
|
nsContentUtils::XPConnect()->
|
|
|
|
GetWrappedNativeOfJSObject(cx, obj, getter_AddRefs(wrappedNative));
|
|
|
|
if (wrappedNative) {
|
|
|
|
uint32_t scTag = 0;
|
|
|
|
nsISupports* supports = wrappedNative->Native();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMFileList> list = do_QueryInterface(supports);
|
|
|
|
if (list) {
|
|
|
|
scTag = SCTAG_DOM_FILELIST;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (scTag) {
|
|
|
|
return JS_WriteUint32Pair(writer, scTag, 0) &&
|
|
|
|
JS_WriteBytes(writer, &supports, sizeof(supports)) &&
|
|
|
|
scInfo->mEvent->StoreISupports(supports);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
const JSStructuredCloneCallbacks* runtimeCallbacks =
|
|
|
|
js::GetContextStructuredCloneCallbacks(cx);
|
|
|
|
|
|
|
|
if (runtimeCallbacks) {
|
|
|
|
return runtimeCallbacks->write(cx, writer, obj, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
PostMessageReadTransferStructuredClone(JSContext* aCx,
|
|
|
|
JSStructuredCloneReader* reader,
|
|
|
|
uint32_t tag, void* data,
|
|
|
|
uint64_t unused,
|
|
|
|
void* aClosure,
|
|
|
|
JS::MutableHandle<JSObject*> returnObject)
|
|
|
|
{
|
|
|
|
StructuredCloneInfo* scInfo = static_cast<StructuredCloneInfo*>(aClosure);
|
|
|
|
NS_ASSERTION(scInfo, "Must have scInfo!");
|
|
|
|
|
|
|
|
if (tag == SCTAG_DOM_MAP_MESSAGEPORT) {
|
|
|
|
MessagePort* port = static_cast<MessagePort*>(data);
|
|
|
|
port->BindToOwner(scInfo->mPort->GetOwner());
|
|
|
|
scInfo->mPorts.Put(port, nullptr);
|
|
|
|
|
2014-04-08 15:27:18 -07:00
|
|
|
JS::Rooted<JSObject*> obj(aCx, port->WrapObject(aCx));
|
2014-07-08 02:52:00 -07:00
|
|
|
if (!obj || !JS_WrapObject(aCx, &obj)) {
|
|
|
|
return false;
|
2013-11-14 20:42:34 -08:00
|
|
|
}
|
2014-07-08 02:52:00 -07:00
|
|
|
|
|
|
|
MOZ_ASSERT(port->GetOwner() == scInfo->mPort->GetOwner());
|
|
|
|
returnObject.set(obj);
|
2013-11-14 20:42:34 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-05 06:16:26 -08:00
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
static bool
|
|
|
|
PostMessageTransferStructuredClone(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aObj,
|
|
|
|
void* aClosure,
|
|
|
|
uint32_t* aTag,
|
|
|
|
JS::TransferableOwnership* aOwnership,
|
|
|
|
void** aContent,
|
|
|
|
uint64_t *aExtraData)
|
|
|
|
{
|
|
|
|
StructuredCloneInfo* scInfo = static_cast<StructuredCloneInfo*>(aClosure);
|
|
|
|
NS_ASSERTION(scInfo, "Must have scInfo!");
|
|
|
|
|
|
|
|
MessagePortBase *port = nullptr;
|
|
|
|
nsresult rv = UNWRAP_OBJECT(MessagePort, aObj, port);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
nsRefPtr<MessagePortBase> newPort;
|
|
|
|
if (scInfo->mPorts.Get(port, getter_AddRefs(newPort))) {
|
|
|
|
// No duplicate.
|
2013-11-05 06:16:26 -08:00
|
|
|
return false;
|
|
|
|
}
|
2013-09-03 05:38:54 -07:00
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
newPort = port->Clone();
|
|
|
|
scInfo->mPorts.Put(port, newPort);
|
2013-09-03 05:38:54 -07:00
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
*aTag = SCTAG_DOM_MAP_MESSAGEPORT;
|
|
|
|
*aOwnership = JS::SCTAG_TMO_CUSTOM;
|
|
|
|
*aContent = newPort;
|
|
|
|
*aExtraData = 0;
|
2013-09-03 05:38:54 -07:00
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
return true;
|
2013-09-03 05:38:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
static void
|
|
|
|
PostMessageFreeTransferStructuredClone(uint32_t aTag, JS::TransferableOwnership aOwnership,
|
|
|
|
void* aData,
|
|
|
|
uint64_t aExtraData,
|
|
|
|
void* aClosure)
|
|
|
|
{
|
|
|
|
StructuredCloneInfo* scInfo = static_cast<StructuredCloneInfo*>(aClosure);
|
|
|
|
NS_ASSERTION(scInfo, "Must have scInfo!");
|
|
|
|
|
|
|
|
if (aTag == SCTAG_DOM_MAP_MESSAGEPORT) {
|
|
|
|
MOZ_ASSERT(aOwnership == JS::SCTAG_TMO_CUSTOM);
|
|
|
|
nsRefPtr<MessagePort> port(static_cast<MessagePort*>(aData));
|
|
|
|
scInfo->mPorts.Remove(port);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:54 -07:00
|
|
|
JSStructuredCloneCallbacks kPostMessageCallbacks = {
|
|
|
|
PostMessageReadStructuredClone,
|
|
|
|
PostMessageWriteStructuredClone,
|
2013-11-14 20:42:34 -08:00
|
|
|
nullptr,
|
|
|
|
PostMessageReadTransferStructuredClone,
|
|
|
|
PostMessageTransferStructuredClone,
|
|
|
|
PostMessageFreeTransferStructuredClone
|
2013-09-03 05:38:54 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
static PLDHashOperator
|
|
|
|
PopulateMessagePortList(MessagePortBase* aKey, MessagePortBase* aValue, void* aClosure)
|
|
|
|
{
|
|
|
|
nsTArray<nsRefPtr<MessagePortBase> > *array =
|
|
|
|
static_cast<nsTArray<nsRefPtr<MessagePortBase> > *>(aClosure);
|
|
|
|
|
|
|
|
array->AppendElement(aKey);
|
|
|
|
return PL_DHASH_NEXT;
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:54 -07:00
|
|
|
NS_IMETHODIMP
|
|
|
|
PostMessageRunnable::Run()
|
|
|
|
{
|
2013-09-03 05:38:56 -07:00
|
|
|
MOZ_ASSERT(mPort);
|
|
|
|
|
2014-06-19 00:21:14 -07:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
if (NS_WARN_IF(!jsapi.Init(mPort->GetParentObject()))) {
|
2014-06-11 09:22:00 -07:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
JSContext* cx = jsapi.cx();
|
2013-09-03 05:38:54 -07:00
|
|
|
|
|
|
|
// Deserialize the structured clone data
|
|
|
|
JS::Rooted<JS::Value> messageData(cx);
|
2013-11-14 20:42:34 -08:00
|
|
|
StructuredCloneInfo scInfo;
|
|
|
|
scInfo.mEvent = this;
|
|
|
|
scInfo.mPort = mPort;
|
2013-09-03 05:38:54 -07:00
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
if (!mBuffer.read(cx, &messageData, &kPostMessageCallbacks, &scInfo)) {
|
|
|
|
return NS_ERROR_DOM_DATA_CLONE_ERR;
|
2013-09-03 05:38:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the event
|
2013-11-14 20:42:34 -08:00
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> eventTarget =
|
|
|
|
do_QueryInterface(mPort->GetOwner());
|
|
|
|
nsRefPtr<MessageEvent> event =
|
|
|
|
new MessageEvent(eventTarget, nullptr, nullptr);
|
2013-09-03 05:38:54 -07:00
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
event->InitMessageEvent(NS_LITERAL_STRING("message"), false /* non-bubbling */,
|
|
|
|
false /* cancelable */, messageData, EmptyString(),
|
|
|
|
EmptyString(), nullptr);
|
|
|
|
event->SetTrusted(true);
|
|
|
|
event->SetSource(mPort);
|
2013-09-03 05:38:54 -07:00
|
|
|
|
2013-11-14 20:42:34 -08:00
|
|
|
nsTArray<nsRefPtr<MessagePortBase> > ports;
|
|
|
|
scInfo.mPorts.EnumerateRead(PopulateMessagePortList, &ports);
|
|
|
|
event->SetPorts(new MessagePortList(static_cast<dom::Event*>(event.get()), ports));
|
2013-09-03 05:38:54 -07:00
|
|
|
|
|
|
|
bool status;
|
2013-11-14 20:42:34 -08:00
|
|
|
mPort->DispatchEvent(static_cast<dom::Event*>(event.get()), &status);
|
2013-09-03 05:38:54 -07:00
|
|
|
return status ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
MessagePortBase::MessagePortBase(nsPIDOMWindow* aWindow)
|
2014-03-31 23:13:50 -07:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
2013-11-05 06:16:26 -08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MessagePortBase::MessagePortBase()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:58 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MessagePort)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessagePort,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-09-03 05:38:58 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEntangledPort)
|
|
|
|
|
|
|
|
// Custom unlink loop because this array contains nsRunnable objects
|
|
|
|
// which are not cycle colleactable.
|
|
|
|
while (!tmp->mMessageQueue.IsEmpty()) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessageQueue[0]->mPort);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessageQueue[0]->mSupportsArray);
|
|
|
|
tmp->mMessageQueue.RemoveElementAt(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp->mDispatchRunnable) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDispatchRunnable->mPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessagePort,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-09-03 05:38:58 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEntangledPort)
|
|
|
|
|
|
|
|
// Custom unlink loop because this array contains nsRunnable objects
|
|
|
|
// which are not cycle colleactable.
|
|
|
|
for (uint32_t i = 0, len = tmp->mMessageQueue.Length(); i < len; ++i) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessageQueue[i]->mPort);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessageQueue[i]->mSupportsArray);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tmp->mDispatchRunnable) {
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDispatchRunnable->mPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-09-03 05:38:49 -07:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MessagePort)
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2013-09-03 05:38:49 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(MessagePort, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MessagePort, DOMEventTargetHelper)
|
2013-09-03 05:38:49 -07:00
|
|
|
|
|
|
|
MessagePort::MessagePort(nsPIDOMWindow* aWindow)
|
2013-11-05 06:16:26 -08:00
|
|
|
: MessagePortBase(aWindow)
|
2013-09-03 05:38:56 -07:00
|
|
|
, mMessageQueueEnabled(false)
|
2013-09-03 05:38:49 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
MessagePort::~MessagePort()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
MessagePort::WrapObject(JSContext* aCx)
|
2013-09-03 05:38:49 -07:00
|
|
|
{
|
Bug 991742 part 6. Remove the "aScope" argument of binding Wrap() methods. r=bholley
This patch was mostly generated with this command:
find . -name "*.h" -o -name "*.cpp" | xargs sed -e 's/Binding::Wrap(aCx, aScope, this/Binding::Wrap(aCx, this/' -e 's/Binding_workers::Wrap(aCx, aScope, this/Binding_workers::Wrap(aCx, this/' -e 's/Binding::Wrap(cx, scope, this/Binding::Wrap(cx, this/' -i ""
plus a few manual fixes to dom/bindings/Codegen.py, js/xpconnect/src/event_impl_gen.py, and a few C++ files that were not caught in the search-and-replace above.
2014-04-08 15:27:17 -07:00
|
|
|
return MessagePortBinding::Wrap(aCx, this);
|
2013-09-03 05:38:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-05 06:16:26 -08:00
|
|
|
MessagePort::PostMessageMoz(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
2013-09-03 05:38:49 -07:00
|
|
|
{
|
2013-09-03 05:38:56 -07:00
|
|
|
nsRefPtr<PostMessageRunnable> event = new PostMessageRunnable();
|
2013-09-03 05:38:54 -07:00
|
|
|
|
|
|
|
// We *must* clone the data here, or the JS::Value could be modified
|
|
|
|
// by script
|
|
|
|
StructuredCloneInfo scInfo;
|
|
|
|
scInfo.mEvent = event;
|
|
|
|
scInfo.mPort = this;
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
const Sequence<JS::Value>& realTransferable = aTransferable.Value();
|
2014-02-12 02:50:46 -08:00
|
|
|
|
|
|
|
// The input sequence only comes from the generated bindings code, which
|
|
|
|
// ensures it is rooted.
|
|
|
|
JS::HandleValueArray elements =
|
|
|
|
JS::HandleValueArray::fromMarkedLocation(realTransferable.Length(),
|
|
|
|
realTransferable.Elements());
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
JSObject* array =
|
2014-02-12 02:50:46 -08:00
|
|
|
JS_NewArrayObject(aCx, elements);
|
2013-11-05 06:16:26 -08:00
|
|
|
if (!array) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transferable.setObject(*array);
|
|
|
|
}
|
2013-09-03 05:38:54 -07:00
|
|
|
|
2014-03-04 19:28:20 -08:00
|
|
|
if (!event->Buffer().write(aCx, aMessage, transferable,
|
|
|
|
&kPostMessageCallbacks, &scInfo)) {
|
2013-09-03 05:38:54 -07:00
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:56 -07:00
|
|
|
if (!mEntangledPort) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:58 -07:00
|
|
|
mEntangledPort->mMessageQueue.AppendElement(event);
|
|
|
|
mEntangledPort->Dispatch();
|
2013-09-03 05:38:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Start()
|
|
|
|
{
|
2013-09-03 05:38:56 -07:00
|
|
|
if (mMessageQueueEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMessageQueueEnabled = true;
|
2013-09-03 05:38:58 -07:00
|
|
|
Dispatch();
|
|
|
|
}
|
2013-09-03 05:38:56 -07:00
|
|
|
|
2013-09-03 05:38:58 -07:00
|
|
|
void
|
|
|
|
MessagePort::Dispatch()
|
|
|
|
{
|
|
|
|
if (!mMessageQueueEnabled || mMessageQueue.IsEmpty() || mDispatchRunnable) {
|
|
|
|
return;
|
2013-09-03 05:38:56 -07:00
|
|
|
}
|
2013-09-03 05:38:58 -07:00
|
|
|
|
|
|
|
nsRefPtr<PostMessageRunnable> event = mMessageQueue.ElementAt(0);
|
|
|
|
mMessageQueue.RemoveElementAt(0);
|
|
|
|
|
|
|
|
event->Dispatch(this);
|
|
|
|
|
|
|
|
mDispatchRunnable = new DispatchEventRunnable(this);
|
|
|
|
NS_DispatchToCurrentThread(mDispatchRunnable);
|
2013-09-03 05:38:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Close()
|
|
|
|
{
|
|
|
|
if (!mEntangledPort) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This avoids loops.
|
|
|
|
nsRefPtr<MessagePort> port = mEntangledPort;
|
|
|
|
mEntangledPort = nullptr;
|
|
|
|
|
|
|
|
// Let's disentangle the 2 ports symmetrically.
|
|
|
|
port->Close();
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:56 -07:00
|
|
|
EventHandlerNonNull*
|
|
|
|
MessagePort::GetOnmessage()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return GetEventHandler(nsGkAtoms::onmessage, EmptyString());
|
|
|
|
}
|
|
|
|
return GetEventHandler(nullptr, NS_LITERAL_STRING("message"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-17 04:01:28 -07:00
|
|
|
MessagePort::SetOnmessage(EventHandlerNonNull* aCallback)
|
2013-09-03 05:38:56 -07:00
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
2013-09-17 04:01:28 -07:00
|
|
|
SetEventHandler(nsGkAtoms::onmessage, EmptyString(), aCallback);
|
2013-09-03 05:38:56 -07:00
|
|
|
} else {
|
2013-09-17 04:01:28 -07:00
|
|
|
SetEventHandler(nullptr, NS_LITERAL_STRING("message"), aCallback);
|
2013-09-03 05:38:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// When using onmessage, the call to start() is implied.
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:49 -07:00
|
|
|
void
|
|
|
|
MessagePort::Entangle(MessagePort* aMessagePort)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aMessagePort);
|
|
|
|
MOZ_ASSERT(aMessagePort != this);
|
|
|
|
|
|
|
|
Close();
|
|
|
|
|
|
|
|
mEntangledPort = aMessagePort;
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
already_AddRefed<MessagePortBase>
|
2013-09-10 10:42:22 -07:00
|
|
|
MessagePort::Clone()
|
2013-09-03 05:38:51 -07:00
|
|
|
{
|
2013-09-10 10:42:22 -07:00
|
|
|
nsRefPtr<MessagePort> newPort = new MessagePort(nullptr);
|
2013-09-03 05:38:51 -07:00
|
|
|
|
2013-09-03 05:38:56 -07:00
|
|
|
// Move all the events in the port message queue of original port.
|
|
|
|
newPort->mMessageQueue.SwapElements(mMessageQueue);
|
2013-09-03 05:38:51 -07:00
|
|
|
|
|
|
|
if (mEntangledPort) {
|
|
|
|
nsRefPtr<MessagePort> port = mEntangledPort;
|
|
|
|
mEntangledPort = nullptr;
|
|
|
|
|
|
|
|
newPort->Entangle(port);
|
|
|
|
port->Entangle(newPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPort.forget();
|
|
|
|
}
|
|
|
|
|
2013-09-03 05:38:49 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|