2013-09-03 16:38:49 +04: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"
|
|
|
|
#include "mozilla/dom/MessageChannel.h"
|
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
2013-09-03 16:38:54 +04:00
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
2013-09-03 16:38:49 +04:00
|
|
|
#include "nsContentUtils.h"
|
2013-09-03 16:38:54 +04:00
|
|
|
#include "nsEventDispatcher.h"
|
|
|
|
#include "nsPresContext.h"
|
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMFile.h"
|
|
|
|
#include "nsIDOMFileList.h"
|
|
|
|
#include "nsIDOMMessageEvent.h"
|
|
|
|
#include "nsIPresShell.h"
|
2013-09-03 16:38:49 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2013-09-03 16:38:58 +04:00
|
|
|
class DispatchEventRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
friend class MessagePort;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DispatchEventRunnable(MessagePort* aPort)
|
|
|
|
: mPort(aPort)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
nsRefPtr<DispatchEventRunnable> mKungFuDeathGrip(this);
|
|
|
|
|
|
|
|
mPort->mDispatchRunnable = nullptr;
|
|
|
|
mPort->Dispatch();
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<MessagePort> mPort;
|
|
|
|
};
|
|
|
|
|
2013-09-03 16:38:54 +04:00
|
|
|
class PostMessageRunnable : public nsRunnable
|
|
|
|
{
|
2013-09-03 16:38:58 +04:00
|
|
|
friend class MessagePort;
|
|
|
|
|
2013-09-03 16:38:54 +04:00
|
|
|
public:
|
|
|
|
NS_DECL_NSIRUNNABLE
|
|
|
|
|
2013-09-03 16:38:56 +04:00
|
|
|
PostMessageRunnable()
|
|
|
|
: mMessage(nullptr)
|
2013-09-03 16:38:54 +04:00
|
|
|
, mMessageLen(0)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~PostMessageRunnable()
|
|
|
|
{
|
2013-09-03 16:38:56 +04:00
|
|
|
// Ensure that the buffer is freed
|
|
|
|
if (mMessage) {
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
buffer.adopt(mMessage, mMessageLen);
|
|
|
|
}
|
2013-09-03 16:38:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void SetJSData(JSAutoStructuredCloneBuffer& aBuffer)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(!mMessage && mMessageLen == 0, "Don't call twice!");
|
|
|
|
aBuffer.steal(&mMessage, &mMessageLen);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool StoreISupports(nsISupports* aSupports)
|
|
|
|
{
|
|
|
|
mSupportsArray.AppendElement(aSupports);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-09-03 16:38:56 +04:00
|
|
|
void Dispatch(MessagePort* aPort)
|
|
|
|
{
|
|
|
|
mPort = aPort;
|
|
|
|
NS_DispatchToCurrentThread(this);
|
|
|
|
}
|
|
|
|
|
2013-09-03 16:38:54 +04:00
|
|
|
private:
|
|
|
|
nsRefPtr<MessagePort> mPort;
|
|
|
|
uint64_t* mMessage;
|
|
|
|
size_t mMessageLen;
|
|
|
|
|
|
|
|
nsTArray<nsCOMPtr<nsISupports> > mSupportsArray;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
struct StructuredCloneInfo
|
|
|
|
{
|
|
|
|
PostMessageRunnable* mEvent;
|
|
|
|
MessagePort* mPort;
|
|
|
|
};
|
|
|
|
|
|
|
|
static JSObject*
|
|
|
|
PostMessageReadStructuredClone(JSContext* cx,
|
|
|
|
JSStructuredCloneReader* reader,
|
|
|
|
uint32_t tag,
|
|
|
|
uint32_t data,
|
|
|
|
void* closure)
|
|
|
|
{
|
2013-09-10 21:42:22 +04:00
|
|
|
StructuredCloneInfo* scInfo = static_cast<StructuredCloneInfo*>(closure);
|
|
|
|
NS_ASSERTION(scInfo, "Must have scInfo!");
|
2013-09-03 16:38:54 +04:00
|
|
|
|
|
|
|
if (tag == SCTAG_DOM_BLOB || tag == SCTAG_DOM_FILELIST) {
|
|
|
|
NS_ASSERTION(!data, "Data should be empty");
|
|
|
|
|
|
|
|
nsISupports* supports;
|
|
|
|
if (JS_ReadBytes(reader, &supports, sizeof(supports))) {
|
|
|
|
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
if (global) {
|
|
|
|
JS::Rooted<JS::Value> val(cx);
|
|
|
|
nsCOMPtr<nsIXPConnectJSObjectHolder> wrapper;
|
|
|
|
if (NS_SUCCEEDED(nsContentUtils::WrapNative(cx, global, supports,
|
2013-10-12 09:02:39 +04:00
|
|
|
&val,
|
2013-09-03 16:38:54 +04:00
|
|
|
getter_AddRefs(wrapper)))) {
|
|
|
|
return JSVAL_TO_OBJECT(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tag == SCTAG_DOM_MESSAGEPORT) {
|
|
|
|
NS_ASSERTION(!data, "Data should be empty");
|
|
|
|
|
|
|
|
MessagePort* port;
|
|
|
|
if (JS_ReadBytes(reader, &port, sizeof(port))) {
|
|
|
|
JS::Rooted<JSObject*> global(cx, JS::CurrentGlobalOrNull(cx));
|
|
|
|
if (global) {
|
|
|
|
JS::Rooted<JSObject*> obj(cx, port->WrapObject(cx, global));
|
2013-10-16 04:02:23 +04:00
|
|
|
if (JS_WrapObject(cx, &obj)) {
|
2013-09-10 21:42:22 +04:00
|
|
|
port->BindToOwner(scInfo->mPort->GetOwner());
|
2013-09-03 16:38:54 +04:00
|
|
|
return obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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!");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIXPConnectWrappedNative> wrappedNative;
|
|
|
|
nsContentUtils::XPConnect()->
|
|
|
|
GetWrappedNativeOfJSObject(cx, obj, getter_AddRefs(wrappedNative));
|
|
|
|
if (wrappedNative) {
|
|
|
|
uint32_t scTag = 0;
|
|
|
|
nsISupports* supports = wrappedNative->Native();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMBlob> blob = do_QueryInterface(supports);
|
|
|
|
if (blob) {
|
|
|
|
scTag = SCTAG_DOM_BLOB;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05 18:16:26 +04:00
|
|
|
MessagePortBase* port = nullptr;
|
2013-09-03 16:38:54 +04:00
|
|
|
nsresult rv = UNWRAP_OBJECT(MessagePort, cx, obj, port);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2013-11-05 18:16:26 +04:00
|
|
|
nsRefPtr<MessagePortBase> newPort = port->Clone();
|
|
|
|
|
|
|
|
if (!newPort) {
|
|
|
|
return false;
|
|
|
|
}
|
2013-09-03 16:38:54 +04:00
|
|
|
|
|
|
|
return JS_WriteUint32Pair(writer, SCTAG_DOM_MESSAGEPORT, 0) &&
|
|
|
|
JS_WriteBytes(writer, &newPort, sizeof(newPort)) &&
|
|
|
|
scInfo->mEvent->StoreISupports(newPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
const JSStructuredCloneCallbacks* runtimeCallbacks =
|
|
|
|
js::GetContextStructuredCloneCallbacks(cx);
|
|
|
|
|
|
|
|
if (runtimeCallbacks) {
|
|
|
|
return runtimeCallbacks->write(cx, writer, obj, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSStructuredCloneCallbacks kPostMessageCallbacks = {
|
|
|
|
PostMessageReadStructuredClone,
|
|
|
|
PostMessageWriteStructuredClone,
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PostMessageRunnable::Run()
|
|
|
|
{
|
2013-09-03 16:38:56 +04:00
|
|
|
MOZ_ASSERT(mPort);
|
|
|
|
|
2013-09-03 16:38:54 +04:00
|
|
|
// Ensure that the buffer is freed even if we fail to post the message
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
buffer.adopt(mMessage, mMessageLen);
|
|
|
|
mMessage = nullptr;
|
|
|
|
mMessageLen = 0;
|
|
|
|
|
|
|
|
// Get the JSContext for the target window
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> sgo = do_QueryInterface(mPort->GetOwner());
|
|
|
|
NS_ENSURE_STATE(sgo);
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext = sgo->GetContext();
|
|
|
|
AutoPushJSContext cx(scriptContext ? scriptContext->GetNativeContext()
|
|
|
|
: nsContentUtils::GetSafeJSContext());
|
|
|
|
|
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
|
|
|
|
// Deserialize the structured clone data
|
|
|
|
JS::Rooted<JS::Value> messageData(cx);
|
|
|
|
{
|
|
|
|
StructuredCloneInfo scInfo;
|
|
|
|
scInfo.mEvent = this;
|
|
|
|
scInfo.mPort = mPort;
|
|
|
|
|
2013-10-23 04:18:32 +04:00
|
|
|
if (!buffer.read(cx, &messageData, &kPostMessageCallbacks, &scInfo)) {
|
2013-09-03 16:38:54 +04:00
|
|
|
return NS_ERROR_DOM_DATA_CLONE_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the event
|
|
|
|
nsIDocument* doc = mPort->GetOwner()->GetExtantDoc();
|
|
|
|
if (!doc) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult error;
|
|
|
|
nsRefPtr<nsDOMEvent> event =
|
|
|
|
doc->CreateEvent(NS_LITERAL_STRING("MessageEvent"), error);
|
|
|
|
if (error.Failed()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMMessageEvent> message = do_QueryInterface(event);
|
|
|
|
nsresult rv = message->InitMessageEvent(NS_LITERAL_STRING("message"),
|
|
|
|
false /* non-bubbling */,
|
|
|
|
true /* cancelable */,
|
|
|
|
messageData,
|
|
|
|
EmptyString(),
|
|
|
|
EmptyString(),
|
|
|
|
mPort->GetOwner());
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
message->SetTrusted(true);
|
|
|
|
|
|
|
|
bool status;
|
|
|
|
mPort->DispatchEvent(event, &status);
|
|
|
|
return status ? NS_OK : NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
MessagePortBase::MessagePortBase(nsPIDOMWindow* aWindow)
|
|
|
|
: nsDOMEventTargetHelper(aWindow)
|
|
|
|
{
|
|
|
|
// SetIsDOMBinding() is called by nsDOMEventTargetHelper's ctor.
|
|
|
|
}
|
|
|
|
|
|
|
|
MessagePortBase::MessagePortBase()
|
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
2013-09-03 16:38:58 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MessagePort)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessagePort,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
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,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
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 16:38:49 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MessagePort)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(MessagePort, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MessagePort, nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
MessagePort::MessagePort(nsPIDOMWindow* aWindow)
|
2013-11-05 18:16:26 +04:00
|
|
|
: MessagePortBase(aWindow)
|
2013-09-03 16:38:56 +04:00
|
|
|
, mMessageQueueEnabled(false)
|
2013-09-03 16:38:49 +04:00
|
|
|
{
|
|
|
|
SetIsDOMBinding();
|
|
|
|
}
|
|
|
|
|
|
|
|
MessagePort::~MessagePort()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
MessagePort::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
|
|
|
{
|
|
|
|
return MessagePortBinding::Wrap(aCx, aScope, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-05 18:16:26 +04:00
|
|
|
MessagePort::PostMessageMoz(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
2013-09-03 16:38:49 +04:00
|
|
|
{
|
2013-09-03 16:38:56 +04:00
|
|
|
nsRefPtr<PostMessageRunnable> event = new PostMessageRunnable();
|
2013-09-03 16:38:54 +04:00
|
|
|
|
|
|
|
// We *must* clone the data here, or the JS::Value could be modified
|
|
|
|
// by script
|
|
|
|
JSAutoStructuredCloneBuffer buffer;
|
|
|
|
StructuredCloneInfo scInfo;
|
|
|
|
scInfo.mEvent = event;
|
|
|
|
scInfo.mPort = this;
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue());
|
|
|
|
if (aTransferable.WasPassed()) {
|
|
|
|
const Sequence<JS::Value>& realTransferable = aTransferable.Value();
|
|
|
|
JSObject* array =
|
|
|
|
JS_NewArrayObject(aCx, realTransferable.Length(),
|
|
|
|
const_cast<JS::Value*>(realTransferable.Elements()));
|
|
|
|
if (!array) {
|
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
transferable.setObject(*array);
|
|
|
|
}
|
2013-09-03 16:38:54 +04:00
|
|
|
|
|
|
|
if (!buffer.write(aCx, aMessage, transferable, &kPostMessageCallbacks,
|
|
|
|
&scInfo)) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_DATA_CLONE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
event->SetJSData(buffer);
|
2013-09-03 16:38:56 +04:00
|
|
|
|
|
|
|
if (!mEntangledPort) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-09-03 16:38:58 +04:00
|
|
|
mEntangledPort->mMessageQueue.AppendElement(event);
|
|
|
|
mEntangledPort->Dispatch();
|
2013-09-03 16:38:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Start()
|
|
|
|
{
|
2013-09-03 16:38:56 +04:00
|
|
|
if (mMessageQueueEnabled) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mMessageQueueEnabled = true;
|
2013-09-03 16:38:58 +04:00
|
|
|
Dispatch();
|
|
|
|
}
|
2013-09-03 16:38:56 +04:00
|
|
|
|
2013-09-03 16:38:58 +04:00
|
|
|
void
|
|
|
|
MessagePort::Dispatch()
|
|
|
|
{
|
|
|
|
if (!mMessageQueueEnabled || mMessageQueue.IsEmpty() || mDispatchRunnable) {
|
|
|
|
return;
|
2013-09-03 16:38:56 +04:00
|
|
|
}
|
2013-09-03 16:38:58 +04:00
|
|
|
|
|
|
|
nsRefPtr<PostMessageRunnable> event = mMessageQueue.ElementAt(0);
|
|
|
|
mMessageQueue.RemoveElementAt(0);
|
|
|
|
|
|
|
|
event->Dispatch(this);
|
|
|
|
|
|
|
|
mDispatchRunnable = new DispatchEventRunnable(this);
|
|
|
|
NS_DispatchToCurrentThread(mDispatchRunnable);
|
2013-09-03 16:38:49 +04: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 16:38:56 +04:00
|
|
|
EventHandlerNonNull*
|
|
|
|
MessagePort::GetOnmessage()
|
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
return GetEventHandler(nsGkAtoms::onmessage, EmptyString());
|
|
|
|
}
|
|
|
|
return GetEventHandler(nullptr, NS_LITERAL_STRING("message"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-09-17 15:01:28 +04:00
|
|
|
MessagePort::SetOnmessage(EventHandlerNonNull* aCallback)
|
2013-09-03 16:38:56 +04:00
|
|
|
{
|
|
|
|
if (NS_IsMainThread()) {
|
2013-09-17 15:01:28 +04:00
|
|
|
SetEventHandler(nsGkAtoms::onmessage, EmptyString(), aCallback);
|
2013-09-03 16:38:56 +04:00
|
|
|
} else {
|
2013-09-17 15:01:28 +04:00
|
|
|
SetEventHandler(nullptr, NS_LITERAL_STRING("message"), aCallback);
|
2013-09-03 16:38:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// When using onmessage, the call to start() is implied.
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
2013-09-03 16:38:49 +04:00
|
|
|
void
|
|
|
|
MessagePort::Entangle(MessagePort* aMessagePort)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aMessagePort);
|
|
|
|
MOZ_ASSERT(aMessagePort != this);
|
|
|
|
|
|
|
|
Close();
|
|
|
|
|
|
|
|
mEntangledPort = aMessagePort;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
already_AddRefed<MessagePortBase>
|
2013-09-10 21:42:22 +04:00
|
|
|
MessagePort::Clone()
|
2013-09-03 16:38:51 +04:00
|
|
|
{
|
2013-09-10 21:42:22 +04:00
|
|
|
nsRefPtr<MessagePort> newPort = new MessagePort(nullptr);
|
2013-09-03 16:38:51 +04:00
|
|
|
|
2013-09-03 16:38:56 +04:00
|
|
|
// Move all the events in the port message queue of original port.
|
|
|
|
newPort->mMessageQueue.SwapElements(mMessageQueue);
|
2013-09-03 16:38:51 +04:00
|
|
|
|
|
|
|
if (mEntangledPort) {
|
|
|
|
nsRefPtr<MessagePort> port = mEntangledPort;
|
|
|
|
mEntangledPort = nullptr;
|
|
|
|
|
|
|
|
newPort->Entangle(port);
|
|
|
|
port->Entangle(newPort);
|
|
|
|
}
|
|
|
|
|
|
|
|
return newPort.forget();
|
|
|
|
}
|
|
|
|
|
2013-09-03 16:38:49 +04:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|