2011-07-17 12:09:13 -07:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
2011-08-15 20:40:38 -07:00
|
|
|
/* vim: set ts=2 et sw=2 tw=80: */
|
2012-05-21 04:12:37 -07: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/. */
|
2011-07-17 12:09:13 -07:00
|
|
|
|
|
|
|
#include "WorkerScope.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2014-03-16 23:56:53 -07:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2014-09-10 08:21:32 -07:00
|
|
|
#include "mozilla/dom/Console.h"
|
2013-11-05 06:16:26 -08:00
|
|
|
#include "mozilla/dom/DedicatedWorkerGlobalScopeBinding.h"
|
2014-09-10 08:21:32 -07:00
|
|
|
#include "mozilla/dom/FunctionBinding.h"
|
|
|
|
#include "mozilla/dom/Promise.h"
|
2014-05-13 14:28:01 -07:00
|
|
|
#include "mozilla/dom/ServiceWorkerGlobalScopeBinding.h"
|
2013-11-05 06:16:26 -08:00
|
|
|
#include "mozilla/dom/SharedWorkerGlobalScopeBinding.h"
|
2014-09-10 08:21:32 -07:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIServiceWorkerManager.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-02-11 13:01:29 -08:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2013-12-10 03:43:16 -08:00
|
|
|
#include "Location.h"
|
|
|
|
#include "Navigator.h"
|
|
|
|
#include "Principal.h"
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
#include "WorkerPrivate.h"
|
2014-09-10 08:21:32 -07:00
|
|
|
#include "WorkerRunnable.h"
|
2014-08-29 16:50:06 -07:00
|
|
|
#include "Performance.h"
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-21 04:51:16 -08:00
|
|
|
#define UNWRAP_WORKER_OBJECT(Interface, obj, value) \
|
2013-11-05 06:16:26 -08:00
|
|
|
UnwrapObject<prototypes::id::Interface##_workers, \
|
2013-11-21 04:51:15 -08:00
|
|
|
mozilla::dom::Interface##Binding_workers::NativeType>(obj, value)
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
using namespace mozilla;
|
2012-11-10 07:45:52 -08:00
|
|
|
using namespace mozilla::dom;
|
2011-07-17 12:09:13 -07:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
WorkerGlobalScope::WorkerGlobalScope(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
2013-09-08 20:28:48 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
WorkerGlobalScope::~WorkerGlobalScope()
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2014-02-26 11:00:40 -08:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
2012-04-26 13:57:33 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(WorkerGlobalScope)
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WorkerGlobalScope,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-11-05 06:16:26 -08:00
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
2014-08-08 05:34:30 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConsole)
|
2014-08-29 16:50:06 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPerformance)
|
2014-09-02 13:02:06 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNavigator)
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WorkerGlobalScope,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-11-05 06:16:26 -08:00
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
2014-08-08 05:34:30 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mConsole)
|
2014-08-29 16:50:06 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPerformance)
|
2014-09-02 13:02:06 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mLocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNavigator)
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(WorkerGlobalScope,
|
2014-03-31 23:13:50 -07:00
|
|
|
DOMEventTargetHelper)
|
2013-11-05 06:16:26 -08:00
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
tmp->mWorkerPrivate->TraceTimeouts(aCallbacks, aClosure);
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2013-08-08 15:55:23 -07:00
|
|
|
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_IMPL_ADDREF_INHERITED(WorkerGlobalScope, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WorkerGlobalScope, DOMEventTargetHelper)
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WorkerGlobalScope)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGlobalObject)
|
2014-03-31 23:13:50 -07:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
JSObject*
|
2014-04-08 15:27:18 -07:00
|
|
|
WorkerGlobalScope::WrapObject(JSContext* aCx)
|
2013-06-05 07:04:23 -07:00
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
MOZ_CRASH("We should never get here!");
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2014-08-08 05:34:30 -07:00
|
|
|
Console*
|
2014-02-27 15:39:30 -08:00
|
|
|
WorkerGlobalScope::GetConsole()
|
2014-01-26 04:35:17 -08:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mConsole) {
|
2014-02-27 15:39:30 -08:00
|
|
|
mConsole = new Console(nullptr);
|
2014-01-26 04:35:17 -08:00
|
|
|
}
|
|
|
|
|
2014-08-08 05:34:30 -07:00
|
|
|
return mConsole;
|
2014-01-26 04:35:17 -08:00
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
already_AddRefed<WorkerLocation>
|
|
|
|
WorkerGlobalScope::Location()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
if (!mLocation) {
|
|
|
|
WorkerPrivate::LocationInfo& info = mWorkerPrivate->GetLocationInfo();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
mLocation = WorkerLocation::Create(info);
|
|
|
|
MOZ_ASSERT(mLocation);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
nsRefPtr<WorkerLocation> location = mLocation;
|
|
|
|
return location.forget();
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
already_AddRefed<WorkerNavigator>
|
|
|
|
WorkerGlobalScope::Navigator()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
if (!mNavigator) {
|
2013-11-19 15:08:50 -08:00
|
|
|
mNavigator = WorkerNavigator::Create(mWorkerPrivate->OnLine());
|
2013-11-05 06:16:26 -08:00
|
|
|
MOZ_ASSERT(mNavigator);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
nsRefPtr<WorkerNavigator> navigator = mNavigator;
|
|
|
|
return navigator.forget();
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-19 15:08:50 -08:00
|
|
|
already_AddRefed<WorkerNavigator>
|
|
|
|
WorkerGlobalScope::GetExistingNavigator() const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsRefPtr<WorkerNavigator> navigator = mNavigator;
|
|
|
|
return navigator.forget();
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::Close(JSContext* aCx)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
mWorkerPrivate->CloseInternal(aCx);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
OnErrorEventHandlerNonNull*
|
|
|
|
WorkerGlobalScope::GetOnerror()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
2013-11-05 06:16:26 -08:00
|
|
|
return elm ? elm->GetOnErrorEventHandler() : nullptr;
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::SetOnerror(OnErrorEventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2014-03-16 23:56:53 -07:00
|
|
|
EventListenerManager* elm = GetOrCreateListenerManager();
|
2013-11-05 06:16:26 -08:00
|
|
|
if (elm) {
|
|
|
|
elm->SetEventHandler(aHandler);
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::ImportScripts(JSContext* aCx,
|
|
|
|
const Sequence<nsString>& aScriptURLs,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
scriptloader::Load(aCx, mWorkerPrivate, aScriptURLs, aRv);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
int32_t
|
|
|
|
WorkerGlobalScope::SetTimeout(JSContext* aCx,
|
|
|
|
Function& aHandler,
|
|
|
|
const int32_t aTimeout,
|
|
|
|
const Sequence<JS::Value>& aArguments,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
return mWorkerPrivate->SetTimeout(aCx, &aHandler, EmptyString(), aTimeout,
|
|
|
|
aArguments, false, aRv);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
int32_t
|
2014-02-19 07:13:38 -08:00
|
|
|
WorkerGlobalScope::SetTimeout(JSContext* /* unused */,
|
|
|
|
const nsAString& aHandler,
|
2013-11-05 06:16:26 -08:00
|
|
|
const int32_t aTimeout,
|
2014-02-19 07:13:38 -08:00
|
|
|
const Sequence<JS::Value>& /* unused */,
|
2013-11-05 06:16:26 -08:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
Sequence<JS::Value> dummy;
|
|
|
|
return mWorkerPrivate->SetTimeout(GetCurrentThreadJSContext(), nullptr,
|
|
|
|
aHandler, aTimeout, dummy, false, aRv);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::ClearTimeout(int32_t aHandle, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mWorkerPrivate->ClearTimeout(aHandle);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
int32_t
|
|
|
|
WorkerGlobalScope::SetInterval(JSContext* aCx,
|
|
|
|
Function& aHandler,
|
|
|
|
const Optional<int32_t>& aTimeout,
|
|
|
|
const Sequence<JS::Value>& aArguments,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
int32_t timeout = aTimeout.WasPassed() ? aTimeout.Value() : 0;
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
return mWorkerPrivate->SetTimeout(aCx, &aHandler, EmptyString(), timeout,
|
|
|
|
aArguments, !!timeout, aRv);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
int32_t
|
2014-02-19 07:13:38 -08:00
|
|
|
WorkerGlobalScope::SetInterval(JSContext* /* unused */,
|
|
|
|
const nsAString& aHandler,
|
2013-11-05 06:16:26 -08:00
|
|
|
const Optional<int32_t>& aTimeout,
|
2014-02-19 07:13:38 -08:00
|
|
|
const Sequence<JS::Value>& /* unused */,
|
2013-11-05 06:16:26 -08:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
Sequence<JS::Value> dummy;
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
int32_t timeout = aTimeout.WasPassed() ? aTimeout.Value() : 0;
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
return mWorkerPrivate->SetTimeout(GetCurrentThreadJSContext(), nullptr,
|
|
|
|
aHandler, timeout, dummy, !!timeout, aRv);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::ClearInterval(int32_t aHandle, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mWorkerPrivate->ClearTimeout(aHandle);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::Atob(const nsAString& aAtob, nsAString& aOutput, ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
aRv = nsContentUtils::Atob(aAtob, aOutput);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::Btoa(const nsAString& aBtoa, nsAString& aOutput, ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
aRv = nsContentUtils::Btoa(aBtoa, aOutput);
|
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::Dump(const Optional<nsAString>& aString) const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
if (!aString.WasPassed()) {
|
|
|
|
return;
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2013-11-24 11:27:15 -08:00
|
|
|
if (!mWorkerPrivate->DumpEnabled()) {
|
2013-11-05 06:16:26 -08:00
|
|
|
return;
|
2013-06-05 07:04:23 -07:00
|
|
|
}
|
|
|
|
|
2014-02-11 13:01:29 -08:00
|
|
|
NS_ConvertUTF16toUTF8 str(aString.Value());
|
|
|
|
|
|
|
|
#ifdef ANDROID
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko", "%s", str.get());
|
|
|
|
#endif
|
|
|
|
fputs(str.get(), stdout);
|
|
|
|
fflush(stdout);
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2014-08-29 16:50:06 -07:00
|
|
|
Performance*
|
|
|
|
WorkerGlobalScope::GetPerformance()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mPerformance) {
|
|
|
|
mPerformance = new Performance(mWorkerPrivate);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mPerformance;
|
|
|
|
}
|
|
|
|
|
2014-07-24 18:30:07 -07:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
WorkerGlobalScope::Fetch(const RequestOrScalarValueString& aInput,
|
|
|
|
const RequestInit& aInit, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
DedicatedWorkerGlobalScope::DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerGlobalScope(aWorkerPrivate)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
2013-12-10 03:43:16 -08:00
|
|
|
DedicatedWorkerGlobalScope::WrapGlobalObject(JSContext* aCx)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
2013-11-05 06:16:26 -08:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(!mWorkerPrivate->IsSharedWorker());
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-12-10 03:43:16 -08:00
|
|
|
JS::CompartmentOptions options;
|
|
|
|
mWorkerPrivate->CopyJSCompartmentOptions(options);
|
|
|
|
|
2014-02-05 13:09:18 -08:00
|
|
|
return DedicatedWorkerGlobalScopeBinding_workers::Wrap(aCx, this, this,
|
|
|
|
options,
|
2014-04-08 11:48:37 -07:00
|
|
|
GetWorkerPrincipal(),
|
|
|
|
true);
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
void
|
|
|
|
DedicatedWorkerGlobalScope::PostMessage(JSContext* aCx,
|
|
|
|
JS::Handle<JS::Value> aMessage,
|
|
|
|
const Optional<Sequence<JS::Value>>& aTransferable,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mWorkerPrivate->PostMessageToParent(aCx, aMessage, aTransferable, aRv);
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
SharedWorkerGlobalScope::SharedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate,
|
2014-02-06 06:39:10 -08:00
|
|
|
const nsCString& aName)
|
2013-11-05 06:16:26 -08:00
|
|
|
: WorkerGlobalScope(aWorkerPrivate), mName(aName)
|
|
|
|
{
|
|
|
|
}
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
JSObject*
|
2013-12-10 03:43:16 -08:00
|
|
|
SharedWorkerGlobalScope::WrapGlobalObject(JSContext* aCx)
|
2013-11-05 06:16:26 -08:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsSharedWorker());
|
2011-07-17 12:09:13 -07:00
|
|
|
|
2013-12-10 03:43:16 -08:00
|
|
|
JS::CompartmentOptions options;
|
|
|
|
mWorkerPrivate->CopyJSCompartmentOptions(options);
|
|
|
|
|
2014-02-05 13:09:18 -08:00
|
|
|
return SharedWorkerGlobalScopeBinding_workers::Wrap(aCx, this, this, options,
|
2014-04-08 11:48:37 -07:00
|
|
|
GetWorkerPrincipal(),
|
|
|
|
true);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2014-05-13 14:28:01 -07:00
|
|
|
ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aScope)
|
|
|
|
: WorkerGlobalScope(aWorkerPrivate),
|
|
|
|
mScope(NS_ConvertUTF8toUTF16(aScope))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
JSObject*
|
|
|
|
ServiceWorkerGlobalScope::WrapGlobalObject(JSContext* aCx)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
|
|
|
|
JS::CompartmentOptions options;
|
|
|
|
mWorkerPrivate->CopyJSCompartmentOptions(options);
|
|
|
|
|
|
|
|
return ServiceWorkerGlobalScopeBinding_workers::Wrap(aCx, this, this, options,
|
|
|
|
GetWorkerPrincipal(),
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:24 -08:00
|
|
|
bool
|
|
|
|
GetterOnlyJSNative(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
|
|
|
JS_ReportErrorNumber(aCx, js_GetErrorMessage, nullptr, JSMSG_GETTER_ONLY);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:21:32 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class UnregisterRunnable;
|
|
|
|
class UnregisterResultRunnable MOZ_FINAL : public WorkerRunnable
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum State { Succeeded, Failed };
|
|
|
|
|
|
|
|
UnregisterResultRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
UnregisterRunnable* aRunnable,
|
|
|
|
State aState, bool aValue)
|
|
|
|
: WorkerRunnable(aWorkerPrivate,
|
|
|
|
WorkerThreadUnchangedBusyCount)
|
|
|
|
, mRunnable(aRunnable), mState(aState), mValue(aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) MOZ_OVERRIDE;
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsRefPtr<UnregisterRunnable> mRunnable;
|
|
|
|
State mState;
|
|
|
|
bool mValue;
|
|
|
|
};
|
|
|
|
|
|
|
|
class UnregisterRunnable MOZ_FINAL : public nsRunnable
|
|
|
|
, public nsIServiceWorkerUnregisterCallback
|
|
|
|
, public WorkerFeature
|
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsRefPtr<Promise> mWorkerPromise;
|
|
|
|
nsString mScope;
|
|
|
|
bool mCleanedUp;
|
|
|
|
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
|
|
|
UnregisterRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
Promise* aWorkerPromise,
|
|
|
|
const nsAString& aScope)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
, mWorkerPromise(aWorkerPromise)
|
|
|
|
, mScope(aScope)
|
|
|
|
, mCleanedUp(false)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aWorkerPromise);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate->AddFeature(mWorkerPrivate->GetJSContext(), this)) {
|
|
|
|
MOZ_ASSERT(false, "cannot add the worker feature!");
|
|
|
|
mCleanedUp = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise*
|
|
|
|
WorkerPromise() const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
return mWorkerPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UnregisterSucceeded(bool aState) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsRefPtr<UnregisterResultRunnable> runnable =
|
|
|
|
new UnregisterResultRunnable(mWorkerPrivate, this,
|
|
|
|
UnregisterResultRunnable::Succeeded, aState);
|
|
|
|
runnable->Dispatch(nullptr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
UnregisterFailed() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsRefPtr<UnregisterResultRunnable> runnable =
|
|
|
|
new UnregisterResultRunnable(mWorkerPrivate, this,
|
|
|
|
UnregisterResultRunnable::Failed, false);
|
|
|
|
runnable->Dispatch(nullptr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CleanUp(JSContext* aCx)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (mCleanedUp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mWorkerPrivate->RemoveFeature(aCx, this);
|
|
|
|
mCleanedUp = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~UnregisterRunnable()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCleanedUp);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIServiceWorkerManager> swm =
|
|
|
|
do_GetService(SERVICEWORKERMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
UnregisterFailed();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We don't need to check if the principal can load this mScope because a
|
|
|
|
// ServiceWorkerGlobalScope can always unregister itself.
|
|
|
|
|
|
|
|
rv = swm->Unregister(this, mScope);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
UnregisterFailed();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Notify(JSContext* aCx, workers::Status aStatus) MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aStatus > workers::Running);
|
|
|
|
|
|
|
|
mCleanedUp = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(UnregisterRunnable, nsRunnable,
|
|
|
|
nsIServiceWorkerUnregisterCallback)
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnregisterResultRunnable::WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
if (mState == Failed) {
|
|
|
|
mRunnable->WorkerPromise()->MaybeReject(aCx, JS::UndefinedHandleValue);
|
|
|
|
mRunnable->CleanUp(aCx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
mRunnable->WorkerPromise()->MaybeResolve(mValue);
|
|
|
|
mRunnable->CleanUp(aCx);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
ServiceWorkerGlobalScope::Unregister(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
|
|
|
|
nsRefPtr<Promise> promise = Promise::Create(this, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<UnregisterRunnable> runnable =
|
|
|
|
new UnregisterRunnable(mWorkerPrivate, promise, mScope);
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
END_WORKERS_NAMESPACE
|