2013-06-05 18:04:23 +04: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"
|
|
|
|
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-11-05 18:16:26 +04:00
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
2014-03-05 04:37:43 +04:00
|
|
|
#include "nsIDOMEvent.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
#include "SharedWorker.h"
|
2013-11-05 18:16:26 +04:00
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 17:16:49 +04:00
|
|
|
#include "WorkerRunnable.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
using mozilla::dom::EventHandlerNonNull;
|
|
|
|
using mozilla::dom::MessagePortBase;
|
2013-06-05 18:04:23 +04:00
|
|
|
using mozilla::dom::Optional;
|
|
|
|
using mozilla::dom::Sequence;
|
2014-03-18 08:48:19 +04:00
|
|
|
using namespace mozilla;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
class DelayedEventRunnable MOZ_FINAL : public WorkerRunnable
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
nsRefPtr<MessagePort> mMessagePort;
|
2013-11-05 18:16:26 +04:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMEvent>> mEvents;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
public:
|
2013-11-05 18:16:26 +04:00
|
|
|
DelayedEventRunnable(WorkerPrivate* aWorkerPrivate,
|
2013-10-23 17:16:49 +04:00
|
|
|
TargetAndBusyBehavior aBehavior,
|
2013-11-05 18:16:26 +04:00
|
|
|
MessagePort* aMessagePort,
|
|
|
|
nsTArray<nsCOMPtr<nsIDOMEvent>>& aEvents)
|
2013-10-23 17:16:49 +04:00
|
|
|
: WorkerRunnable(aWorkerPrivate, aBehavior), mMessagePort(aMessagePort)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aMessagePort);
|
2013-11-05 18:16:26 +04:00
|
|
|
MOZ_ASSERT(aEvents.Length());
|
|
|
|
|
|
|
|
mEvents.SwapElements(aEvents);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
|
2013-06-05 18:04:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
MessagePort::MessagePort(nsPIDOMWindow* aWindow, SharedWorker* aSharedWorker,
|
|
|
|
uint64_t aSerial)
|
2013-11-05 18:16:26 +04:00
|
|
|
: MessagePortBase(aWindow), mSharedWorker(aSharedWorker),
|
|
|
|
mWorkerPrivate(nullptr), mSerial(aSerial), mStarted(false)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aSharedWorker);
|
2013-11-05 18:16:26 +04:00
|
|
|
SetIsDOMBinding();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
MessagePort::MessagePort(WorkerPrivate* aWorkerPrivate, uint64_t aSerial)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate), mSerial(aSerial), mStarted(false)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
SetIsDOMBinding();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
MessagePort::~MessagePort()
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
Close();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-11 12:04:41 +04:00
|
|
|
MessagePort::PostMessageMoz(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2013-10-08 19:51:42 +04:00
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
AssertCorrectThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
if (IsClosed()) {
|
|
|
|
aRv = NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (mSharedWorker) {
|
|
|
|
mSharedWorker->PostMessage(aCx, aMessage, aTransferable, aRv);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
mWorkerPrivate->PostMessageToParentMessagePort(aCx, Serial(), aMessage,
|
|
|
|
aTransferable, aRv);
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Start()
|
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
AssertCorrectThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
if (IsClosed()) {
|
|
|
|
NS_WARNING("Called start() after calling close()!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mStarted) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mStarted = true;
|
|
|
|
|
|
|
|
if (!mQueuedEvents.IsEmpty()) {
|
2013-10-23 17:16:49 +04:00
|
|
|
WorkerPrivate* workerPrivate;
|
|
|
|
WorkerRunnable::TargetAndBusyBehavior behavior;
|
2013-11-05 18:16:26 +04:00
|
|
|
|
2013-10-23 17:16:49 +04:00
|
|
|
if (mWorkerPrivate) {
|
|
|
|
workerPrivate = mWorkerPrivate;
|
|
|
|
behavior = WorkerRunnable::WorkerThreadModifyBusyCount;
|
|
|
|
}
|
|
|
|
else {
|
2013-11-05 18:16:26 +04:00
|
|
|
workerPrivate = mSharedWorker->GetWorkerPrivate();
|
2013-10-23 17:16:49 +04:00
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
behavior = WorkerRunnable::ParentThreadUnchangedBusyCount;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
|
|
|
|
nsRefPtr<DelayedEventRunnable> runnable =
|
2013-10-23 17:16:49 +04:00
|
|
|
new DelayedEventRunnable(workerPrivate, behavior, this, mQueuedEvents);
|
2013-11-05 18:16:26 +04:00
|
|
|
runnable->Dispatch(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::Close()
|
|
|
|
{
|
|
|
|
AssertCorrectThread();
|
|
|
|
|
|
|
|
if (!IsClosed()) {
|
|
|
|
CloseInternal();
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
MessagePort::QueueEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
AssertCorrectThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(aEvent);
|
|
|
|
MOZ_ASSERT(!IsClosed());
|
|
|
|
MOZ_ASSERT(!mStarted);
|
|
|
|
|
|
|
|
mQueuedEvents.AppendElement(aEvent);
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04: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 18:04:23 +04:00
|
|
|
void
|
|
|
|
MessagePort::CloseInternal()
|
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
AssertCorrectThread();
|
2013-06-05 18:04:23 +04: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 18:16:26 +04: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 18:04:23 +04:00
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
#endif
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(MessagePort, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(MessagePort, DOMEventTargetHelper)
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(MessagePort)
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MessagePort)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessagePort,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-06-05 18:04:23 +04:00
|
|
|
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,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-06-05 18:04:23 +04:00
|
|
|
tmp->Close();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
MessagePort::WrapObject(JSContext* aCx)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
AssertCorrectThread();
|
2013-06-05 18:04:23 +04: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-09 02:27:17 +04:00
|
|
|
return MessagePortBinding::Wrap(aCx, this);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-18 08:48:19 +04:00
|
|
|
MessagePort::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
AssertCorrectThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
nsIDOMEvent*& event = aVisitor.mDOMEvent;
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
bool preventDispatch = false;
|
|
|
|
|
|
|
|
if (IsClosed()) {
|
|
|
|
preventDispatch = true;
|
2013-11-05 18:16:26 +04:00
|
|
|
} else if (NS_IsMainThread() && mSharedWorker->IsSuspended()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
mSharedWorker->QueueEvent(event);
|
|
|
|
preventDispatch = true;
|
|
|
|
} else if (!mStarted) {
|
|
|
|
QueueEvent(event);
|
|
|
|
preventDispatch = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (preventDispatch) {
|
|
|
|
aVisitor.mCanHandle = false;
|
|
|
|
aVisitor.mParentTarget = nullptr;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
return DOMEventTargetHelper::PreHandleEvent(aVisitor);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
bool
|
|
|
|
DelayedEventRunnable::WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mMessagePort);
|
2013-11-05 18:16:26 +04:00
|
|
|
mMessagePort->AssertCorrectThread();
|
|
|
|
MOZ_ASSERT(mEvents.Length());
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
bool ignored;
|
2013-11-05 18:16:26 +04:00
|
|
|
for (uint32_t i = 0; i < mEvents.Length(); i++) {
|
|
|
|
mMessagePort->DispatchEvent(mEvents[i], &ignored);
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
return true;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|