2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2013-06-05 18:04:23 +04:00
|
|
|
/* 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 "SharedWorker.h"
|
|
|
|
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
|
2014-03-18 08:48:21 +04:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2015-09-16 06:27:56 +03:00
|
|
|
#include "mozilla/dom/MessagePort.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "mozilla/dom/SharedWorkerBinding.h"
|
2017-05-17 17:48:54 +03:00
|
|
|
#include "mozilla/dom/WorkerBinding.h"
|
2016-08-18 09:00:18 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsIClassInfoImpl.h"
|
2014-03-05 04:37:43 +04:00
|
|
|
#include "nsIDOMEvent.h"
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
2018-01-30 12:14:00 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
2013-06-05 18:04:23 +04:00
|
|
|
using mozilla::dom::Optional;
|
|
|
|
using mozilla::dom::Sequence;
|
2016-03-16 21:51:11 +03:00
|
|
|
using mozilla::dom::MessagePort;
|
2014-03-18 08:48:19 +04:00
|
|
|
using namespace mozilla;
|
2018-01-31 10:19:10 +03:00
|
|
|
using namespace mozilla::dom;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
SharedWorker::SharedWorker(nsPIDOMWindowInner* aWindow,
|
2015-09-16 06:27:56 +03:00
|
|
|
WorkerPrivate* aWorkerPrivate,
|
|
|
|
MessagePort* aMessagePort)
|
2015-09-23 08:54:29 +03:00
|
|
|
: DOMEventTargetHelper(aWindow)
|
|
|
|
, mWorkerPrivate(aWorkerPrivate)
|
|
|
|
, mMessagePort(aMessagePort)
|
|
|
|
, mFrozen(false)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
2015-09-23 08:54:29 +03:00
|
|
|
MOZ_ASSERT(aMessagePort);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
SharedWorker::~SharedWorker()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<SharedWorker>
|
2017-02-03 09:46:22 +03:00
|
|
|
SharedWorker::Constructor(const GlobalObject& aGlobal,
|
2013-06-05 18:04:23 +04:00
|
|
|
const nsAString& aScriptURL,
|
2017-05-17 17:48:54 +03:00
|
|
|
const StringOrWorkerOptions& aOptions,
|
2013-06-05 18:04:23 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2018-01-31 10:24:59 +03:00
|
|
|
workerinternals::RuntimeService* rts =
|
|
|
|
workerinternals::RuntimeService::GetOrCreateService();
|
2013-06-05 18:04:23 +04:00
|
|
|
if (!rts) {
|
|
|
|
aRv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-05-17 17:49:34 +03:00
|
|
|
nsAutoString name;
|
2017-05-17 17:48:54 +03:00
|
|
|
if (aOptions.IsString()) {
|
2017-05-17 17:49:34 +03:00
|
|
|
name = aOptions.GetAsString();
|
2017-05-17 17:48:54 +03:00
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aOptions.IsWorkerOptions());
|
2017-05-17 17:49:34 +03:00
|
|
|
name = aOptions.GetAsWorkerOptions().mName;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SharedWorker> sharedWorker;
|
2013-11-05 18:16:24 +04:00
|
|
|
nsresult rv = rts->CreateSharedWorker(aGlobal, aScriptURL, name,
|
2013-06-05 18:04:23 +04:00
|
|
|
getter_AddRefs(sharedWorker));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv = rv;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sharedWorker.forget();
|
|
|
|
}
|
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
MessagePort*
|
2013-06-05 18:04:23 +04:00
|
|
|
SharedWorker::Port()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-09-16 06:27:56 +03:00
|
|
|
return mMessagePort;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-01 12:00:19 +03:00
|
|
|
SharedWorker::Freeze()
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-04-01 12:00:19 +03:00
|
|
|
MOZ_ASSERT(!IsFrozen());
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mFrozen = true;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-04-01 12:00:19 +03:00
|
|
|
SharedWorker::Thaw()
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-04-01 12:00:19 +03:00
|
|
|
MOZ_ASSERT(IsFrozen());
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mFrozen = false;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
if (!mFrozenEvents.IsEmpty()) {
|
2013-06-05 18:04:23 +04:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMEvent>> events;
|
2015-04-01 12:00:19 +03:00
|
|
|
mFrozenEvents.SwapElements(events);
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
for (uint32_t index = 0; index < events.Length(); index++) {
|
|
|
|
nsCOMPtr<nsIDOMEvent>& event = events[index];
|
|
|
|
MOZ_ASSERT(event);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
if (NS_SUCCEEDED(event->GetTarget(getter_AddRefs(target)))) {
|
|
|
|
bool ignored;
|
|
|
|
if (NS_FAILED(target->DispatchEvent(event, &ignored))) {
|
|
|
|
NS_WARNING("Failed to dispatch event!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
NS_WARNING("Failed to get target!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedWorker::QueueEvent(nsIDOMEvent* aEvent)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aEvent);
|
2015-04-01 12:00:19 +03:00
|
|
|
MOZ_ASSERT(IsFrozen());
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-04-01 12:00:19 +03:00
|
|
|
mFrozenEvents.AppendElement(aEvent);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedWorker::Close()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mMessagePort) {
|
|
|
|
mMessagePort->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-11 12:04:41 +04:00
|
|
|
SharedWorker::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2017-02-03 13:00:38 +03:00
|
|
|
const Sequence<JSObject*>& aTransferable,
|
2013-06-05 18:04:23 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(mMessagePort);
|
|
|
|
|
2015-09-16 06:27:56 +03:00
|
|
|
mMessagePort->PostMessage(aCx, aMessage, aTransferable, aRv);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(SharedWorker, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(SharedWorker, DOMEventTargetHelper)
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2017-08-30 02:02:48 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(SharedWorker)
|
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(SharedWorker)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(SharedWorker,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMessagePort)
|
2015-04-01 12:00:19 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFrozenEvents)
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(SharedWorker,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-06-05 18:04:23 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessagePort)
|
2015-04-01 12:00:19 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFrozenEvents)
|
2013-06-05 18:04:23 +04:00
|
|
|
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 17:13:33 +03:00
|
|
|
SharedWorker::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
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 17:13:33 +03:00
|
|
|
return SharedWorkerBinding::Wrap(aCx, this, aGivenProto);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2016-10-21 05:11:07 +03:00
|
|
|
SharedWorker::GetEventTargetParent(EventChainPreVisitor& aVisitor)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2016-11-11 18:23:32 +03:00
|
|
|
if (IsFrozen()) {
|
|
|
|
nsCOMPtr<nsIDOMEvent> event = aVisitor.mDOMEvent;
|
|
|
|
if (!event) {
|
|
|
|
event = EventDispatcher::CreateEvent(aVisitor.mEvent->mOriginalTarget,
|
|
|
|
aVisitor.mPresContext,
|
|
|
|
aVisitor.mEvent, EmptyString());
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
|
|
|
QueueEvent(event);
|
|
|
|
|
|
|
|
aVisitor.mCanHandle = false;
|
2017-12-18 19:07:36 +03:00
|
|
|
aVisitor.SetParentTarget(nullptr, false);
|
2013-06-05 18:04:23 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-21 05:11:07 +03:00
|
|
|
return DOMEventTargetHelper::GetEventTargetParent(aVisitor);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|