2013-06-05 07:04:23 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* 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-05 06:16:26 -08:00
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
#include "nsDOMEvent.h"
|
|
|
|
#include "nsEventDispatcher.h"
|
|
|
|
|
|
|
|
#include "SharedWorker.h"
|
2013-11-05 06:16:26 -08:00
|
|
|
#include "WorkerPrivate.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
using mozilla::dom::EventHandlerNonNull;
|
|
|
|
using mozilla::dom::MessagePortBase;
|
2013-06-05 07:04:23 -07:00
|
|
|
using mozilla::dom::Optional;
|
|
|
|
using mozilla::dom::Sequence;
|
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
class DelayedEventRunnable MOZ_FINAL : public WorkerRunnable
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
|
|
|
nsRefPtr<MessagePort> mMessagePort;
|
2013-11-05 06:16:26 -08:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMEvent>> mEvents;
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
public:
|
2013-11-05 06:16:26 -08:00
|
|
|
DelayedEventRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
Target aTarget,
|
|
|
|
MessagePort* aMessagePort,
|
|
|
|
nsTArray<nsCOMPtr<nsIDOMEvent>>& aEvents)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, aTarget,
|
|
|
|
aTarget == WorkerThread ? ModifyBusyCount : UnchangedBusyCount,
|
|
|
|
SkipWhenClearing),
|
|
|
|
mMessagePort(aMessagePort)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aMessagePort);
|
2013-11-05 06:16:26 -08:00
|
|
|
MOZ_ASSERT(aEvents.Length());
|
|
|
|
|
|
|
|
mEvents.SwapElements(aEvents);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
|
2013-06-05 07:04:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
MessagePort::MessagePort(nsPIDOMWindow* aWindow, SharedWorker* aSharedWorker,
|
|
|
|
uint64_t aSerial)
|
2013-11-05 06:16:26 -08:00
|
|
|
: MessagePortBase(aWindow), mSharedWorker(aSharedWorker),
|
|
|
|
mWorkerPrivate(nullptr), mSerial(aSerial), mStarted(false)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
2013-11-05 06:16:26 -08:00
|
|
|
SetIsDOMBinding();
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
MessagePort::MessagePort(WorkerPrivate* aWorkerPrivate, uint64_t aSerial)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mSerial(aSerial), mStarted(false)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
SetIsDOMBinding();
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
MessagePort::~MessagePort()
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
Close();
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-11 00:04:41 -08:00
|
|
|
MessagePort::PostMessageMoz(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2013-10-08 08:51:42 -07:00
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
AssertCorrectThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
if (IsClosed()) {
|
|
|
|
aRv = NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
if (mSharedWorker) {
|
|
|
|
mSharedWorker->PostMessage(aCx, aMessage, aTransferable, aRv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mWorkerPrivate->PostMessageToParentMessagePort(aCx, Serial(), aMessage,
|
|
|
|
aTransferable, aRv);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Start()
|
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
AssertCorrectThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
if (IsClosed()) {
|
|
|
|
NS_WARNING("Called start() after calling close()!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStarted = true;
|
|
|
|
|
|
|
|
if (!mQueuedEvents.IsEmpty()) {
|
2013-11-05 06:16:26 -08:00
|
|
|
WorkerRunnable::Target target = WorkerRunnable::WorkerThread;
|
|
|
|
WorkerPrivate* workerPrivate = mWorkerPrivate;
|
|
|
|
|
|
|
|
if (!workerPrivate) {
|
|
|
|
target = WorkerRunnable::ParentThread;
|
|
|
|
workerPrivate = mSharedWorker->GetWorkerPrivate();
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
2013-11-05 06:16:26 -08:00
|
|
|
|
|
|
|
nsRefPtr<DelayedEventRunnable> runnable =
|
|
|
|
new DelayedEventRunnable(workerPrivate, target, this, mQueuedEvents);
|
|
|
|
runnable->Dispatch(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Close()
|
|
|
|
{
|
|
|
|
AssertCorrectThread();
|
|
|
|
|
|
|
|
if (!IsClosed()) {
|
|
|
|
CloseInternal();
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::QueueEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
AssertCorrectThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
MOZ_ASSERT(aEvent);
|
|
|
|
MOZ_ASSERT(!IsClosed());
|
|
|
|
MOZ_ASSERT(!mStarted);
|
|
|
|
|
|
|
|
mQueuedEvents.AppendElement(aEvent);
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
EventHandlerNonNull*
|
|
|
|
MessagePort::GetOnmessage()
|
|
|
|
{
|
|
|
|
AssertCorrectThread();
|
|
|
|
|
|
|
|
return NS_IsMainThread() ? GetEventHandler(nsGkAtoms::onmessage, EmptyString())
|
|
|
|
: GetEventHandler(nullptr, NS_LITERAL_STRING("message"));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::SetOnmessage(EventHandlerNonNull* aCallback)
|
|
|
|
{
|
|
|
|
AssertCorrectThread();
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
SetEventHandler(nsGkAtoms::onmessage, EmptyString(), aCallback);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
SetEventHandler(nullptr, NS_LITERAL_STRING("message"), aCallback);
|
|
|
|
}
|
|
|
|
|
|
|
|
Start();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<MessagePortBase>
|
|
|
|
MessagePort::Clone()
|
|
|
|
{
|
|
|
|
NS_WARNING("Haven't implemented structured clone for these ports yet!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
void
|
|
|
|
MessagePort::CloseInternal()
|
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
AssertCorrectThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
MOZ_ASSERT(!IsClosed());
|
|
|
|
MOZ_ASSERT_IF(mStarted, mQueuedEvents.IsEmpty());
|
|
|
|
|
|
|
|
NS_WARN_IF_FALSE(mStarted, "Called close() before start()!");
|
|
|
|
|
|
|
|
if (!mStarted) {
|
|
|
|
mQueuedEvents.Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
mSharedWorker = nullptr;
|
2013-11-05 06:16:26 -08:00
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
void
|
|
|
|
MessagePort::AssertCorrectThread() const
|
|
|
|
{
|
|
|
|
if (IsClosed()) {
|
|
|
|
return; // Can't assert anything if we nulled out our pointers.
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT((mSharedWorker || mWorkerPrivate) &&
|
|
|
|
!(mSharedWorker && mWorkerPrivate));
|
|
|
|
|
|
|
|
if (mSharedWorker) {
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
2013-11-05 06:16:26 -08:00
|
|
|
#endif
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(MessagePort, nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MessagePort, nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MessagePort)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsDOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MessagePort)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessagePort,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSharedWorker)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mQueuedEvents)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(MessagePort,
|
|
|
|
nsDOMEventTargetHelper)
|
|
|
|
tmp->Close();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
JSObject*
|
2013-11-11 00:04:41 -08:00
|
|
|
MessagePort::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aScope)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
AssertCorrectThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
return MessagePortBinding::Wrap(aCx, aScope, this);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
MessagePort::PreHandleEvent(nsEventChainPreVisitor& aVisitor)
|
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
AssertCorrectThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
nsIDOMEvent*& event = aVisitor.mDOMEvent;
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
bool preventDispatch = false;
|
|
|
|
|
|
|
|
if (IsClosed()) {
|
|
|
|
preventDispatch = true;
|
2013-11-05 06:16:26 -08:00
|
|
|
} else if (NS_IsMainThread() && mSharedWorker->IsSuspended()) {
|
2013-06-05 07:04:23 -07:00
|
|
|
mSharedWorker->QueueEvent(event);
|
|
|
|
preventDispatch = true;
|
|
|
|
} else if (!mStarted) {
|
|
|
|
QueueEvent(event);
|
|
|
|
preventDispatch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preventDispatch) {
|
|
|
|
aVisitor.mCanHandle = false;
|
|
|
|
aVisitor.mParentTarget = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsDOMEventTargetHelper::PreHandleEvent(aVisitor);
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
bool
|
|
|
|
DelayedEventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMessagePort);
|
2013-11-05 06:16:26 -08:00
|
|
|
mMessagePort->AssertCorrectThread();
|
|
|
|
MOZ_ASSERT(mEvents.Length());
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
bool ignored;
|
2013-11-05 06:16:26 -08:00
|
|
|
for (uint32_t i = 0; i < mEvents.Length(); i++) {
|
|
|
|
mMessagePort->DispatchEvent(mEvents[i], &ignored);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
return true;
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|