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 "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"
|
|
|
|
#include "mozilla/dom/SharedWorkerBinding.h"
|
|
|
|
#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 "MessagePort.h"
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
SharedWorker::SharedWorker(nsPIDOMWindow* aWindow,
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
2014-04-01 10:13:50 +04:00
|
|
|
: DOMEventTargetHelper(aWindow), mWorkerPrivate(aWorkerPrivate),
|
2013-06-05 18:04:23 +04:00
|
|
|
mSuspended(false)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
|
|
|
|
mSerial = aWorkerPrivate->NextMessagePortSerial();
|
|
|
|
|
|
|
|
mMessagePort = new MessagePort(aWindow, this, mSerial);
|
|
|
|
}
|
|
|
|
|
|
|
|
SharedWorker::~SharedWorker()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2014-06-27 21:24:56 +04:00
|
|
|
Close();
|
2013-06-05 18:04:23 +04:00
|
|
|
MOZ_ASSERT(!mWorkerPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<SharedWorker>
|
|
|
|
SharedWorker::Constructor(const GlobalObject& aGlobal, JSContext* aCx,
|
|
|
|
const nsAString& aScriptURL,
|
|
|
|
const mozilla::dom::Optional<nsAString>& aName,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
RuntimeService* rts = RuntimeService::GetOrCreateService();
|
|
|
|
if (!rts) {
|
|
|
|
aRv = NS_ERROR_NOT_AVAILABLE;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-02-06 18:39:10 +04:00
|
|
|
nsCString name;
|
2013-06-05 18:04:23 +04:00
|
|
|
if (aName.WasPassed()) {
|
2014-02-06 18:39:10 +04:00
|
|
|
name = NS_ConvertUTF16toUTF8(aName.Value());
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<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();
|
|
|
|
}
|
|
|
|
|
2014-09-25 18:12:06 +04:00
|
|
|
already_AddRefed<mozilla::dom::workers::MessagePort>
|
2013-06-05 18:04:23 +04:00
|
|
|
SharedWorker::Port()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsRefPtr<MessagePort> messagePort = mMessagePort;
|
|
|
|
return messagePort.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedWorker::Suspend()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(!IsSuspended());
|
|
|
|
|
|
|
|
mSuspended = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedWorker::Resume()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(IsSuspended());
|
|
|
|
|
|
|
|
mSuspended = false;
|
|
|
|
|
|
|
|
if (!mSuspendedEvents.IsEmpty()) {
|
|
|
|
nsTArray<nsCOMPtr<nsIDOMEvent>> events;
|
|
|
|
mSuspendedEvents.SwapElements(events);
|
|
|
|
|
|
|
|
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);
|
|
|
|
MOZ_ASSERT(IsSuspended());
|
|
|
|
|
|
|
|
mSuspendedEvents.AppendElement(aEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedWorker::Close()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
if (mMessagePort) {
|
|
|
|
mMessagePort->Close();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mWorkerPrivate) {
|
|
|
|
AutoSafeJSContext cx;
|
|
|
|
NoteDeadWorker(cx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2013-11-11 12:04:41 +04:00
|
|
|
SharedWorker::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage,
|
2013-06-05 18:04:23 +04:00
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
MOZ_ASSERT(mMessagePort);
|
|
|
|
|
|
|
|
mWorkerPrivate->PostMessageToMessagePort(aCx, mMessagePort->Serial(),
|
|
|
|
aMessage, aTransferable, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SharedWorker::NoteDeadWorker(JSContext* aCx)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
|
|
|
|
mWorkerPrivate->UnregisterSharedWorker(aCx, this);
|
|
|
|
mWorkerPrivate = nullptr;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(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)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSuspendedEvents)
|
|
|
|
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
|
|
|
tmp->Close();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mMessagePort)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSuspendedEvents)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
JSObject*
|
2014-04-09 02:27:18 +04:00
|
|
|
SharedWorker::WrapObject(JSContext* aCx)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
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 SharedWorkerBinding::Wrap(aCx, this);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-03-18 08:48:19 +04:00
|
|
|
SharedWorker::PreHandleEvent(EventChainPreVisitor& aVisitor)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsIDOMEvent*& event = aVisitor.mDOMEvent;
|
|
|
|
|
|
|
|
if (IsSuspended() && event) {
|
|
|
|
QueueEvent(event);
|
|
|
|
|
|
|
|
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
|
|
|
}
|