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"
|
|
|
|
|
2014-03-17 21:48:21 -07:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-11-05 06:16:26 -08:00
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
2014-06-27 10:04:45 -07:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-03-04 16:37:43 -08:00
|
|
|
#include "nsIDOMEvent.h"
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
#include "SharedWorker.h"
|
2013-11-05 06:16:26 -08:00
|
|
|
#include "WorkerPrivate.h"
|
2013-10-23 06:16:49 -07:00
|
|
|
#include "WorkerRunnable.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;
|
2014-06-27 10:04:45 -07:00
|
|
|
using mozilla::dom::AutoNoJSAPI;
|
2014-03-17 21:48:19 -07:00
|
|
|
using namespace mozilla;
|
2013-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class DelayedEventRunnable final : public WorkerRunnable
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
2014-09-25 07:12:06 -07:00
|
|
|
nsRefPtr<mozilla::dom::workers::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,
|
2013-10-23 06:16:49 -07:00
|
|
|
TargetAndBusyBehavior aBehavior,
|
2014-09-25 07:12:06 -07:00
|
|
|
mozilla::dom::workers::MessagePort* aMessagePort,
|
2013-11-05 06:16:26 -08:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMEvent>>& aEvents)
|
2013-10-23 06:16:49 -07:00
|
|
|
: WorkerRunnable(aWorkerPrivate, aBehavior), 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
|
|
|
}
|
|
|
|
|
2014-06-27 10:04:45 -07:00
|
|
|
bool PreDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
if (mBehavior == WorkerThreadModifyBusyCount) {
|
|
|
|
return aWorkerPrivate->ModifyBusyCount(aCx, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PostDispatch(JSContext* aCx, WorkerPrivate* aWorkerPrivate,
|
|
|
|
bool aDispatchResult)
|
|
|
|
{
|
|
|
|
if (!aDispatchResult) {
|
|
|
|
if (mBehavior == WorkerThreadModifyBusyCount) {
|
|
|
|
aWorkerPrivate->ModifyBusyCount(aCx, false);
|
|
|
|
}
|
|
|
|
if (aCx) {
|
|
|
|
JS_ReportPendingException(aCx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate);
|
2013-06-05 07:04:23 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2014-09-25 07:12:06 -07:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
2013-06-05 07:04:23 -07:00
|
|
|
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
|
|
|
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();
|
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-10-23 06:16:49 -07:00
|
|
|
WorkerPrivate* workerPrivate;
|
|
|
|
WorkerRunnable::TargetAndBusyBehavior behavior;
|
2013-11-05 06:16:26 -08:00
|
|
|
|
2013-10-23 06:16:49 -07:00
|
|
|
if (mWorkerPrivate) {
|
|
|
|
workerPrivate = mWorkerPrivate;
|
|
|
|
behavior = WorkerRunnable::WorkerThreadModifyBusyCount;
|
|
|
|
}
|
|
|
|
else {
|
2013-11-05 06:16:26 -08:00
|
|
|
workerPrivate = mSharedWorker->GetWorkerPrivate();
|
2013-10-23 06:16:49 -07:00
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
|
|
|
|
behavior = WorkerRunnable::ParentThreadUnchangedBusyCount;
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
2013-11-05 06:16:26 -08:00
|
|
|
|
|
|
|
nsRefPtr<DelayedEventRunnable> runnable =
|
2013-10-23 06:16:49 -07:00
|
|
|
new DelayedEventRunnable(workerPrivate, behavior, this, mQueuedEvents);
|
2013-11-05 06:16:26 -08:00
|
|
|
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
|
|
|
|
2014-04-21 06:34:36 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(mozilla::dom::workers::MessagePort, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(mozilla::dom::workers::MessagePort, DOMEventTargetHelper)
|
2013-06-05 07:04:23 -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-06-05 07:04:23 -07:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(MessagePort)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(MessagePort,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-06-05 07:04:23 -07: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-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-06-05 07:04:23 -07:00
|
|
|
tmp->Close();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
MessagePort::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
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
|
|
|
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 07:13:33 -07:00
|
|
|
return MessagePortBinding::Wrap(aCx, this, aGivenProto);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-17 21:48:19 -07:00
|
|
|
MessagePort::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
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
|
|
|
|
|
|
|
nsIDOMEvent*& event = aVisitor.mDOMEvent;
|
|
|
|
|
|
|
|
if (event) {
|
|
|
|
bool preventDispatch = false;
|
|
|
|
|
|
|
|
if (IsClosed()) {
|
|
|
|
preventDispatch = true;
|
2015-04-01 02:00:19 -07:00
|
|
|
} else if (NS_IsMainThread() && mSharedWorker->IsFrozen()) {
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
return DOMEventTargetHelper::PreHandleEvent(aVisitor);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2014-09-25 07:12:06 -07:00
|
|
|
END_WORKERS_NAMESPACE
|
|
|
|
|
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
|
|
|
|
2014-06-27 10:04:45 -07:00
|
|
|
AutoNoJSAPI nojsapi;
|
|
|
|
|
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
|
|
|
}
|