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-12-16 22:26:15 -08:00
|
|
|
#include "mozilla/dom/BindingDeclarations.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-10-06 11:01:20 -07:00
|
|
|
#include "mozilla/dom/Fetch.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"
|
2015-03-03 15:51:53 -08:00
|
|
|
#include "mozilla/dom/WorkerDebuggerGlobalScopeBinding.h"
|
|
|
|
#include "mozilla/dom/WorkerGlobalScopeBinding.h"
|
2015-03-02 05:23:00 -08:00
|
|
|
#include "mozilla/dom/cache/CacheStorage.h"
|
2014-12-16 22:26:15 -08:00
|
|
|
#include "mozilla/dom/indexedDB/IDBFactory.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"
|
2014-10-27 04:03:00 -07:00
|
|
|
#include "ServiceWorkerClients.h"
|
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
|
|
|
|
|
2015-03-02 05:23:00 -08:00
|
|
|
using mozilla::dom::cache::CacheStorage;
|
2014-12-16 22:26:15 -08:00
|
|
|
using mozilla::dom::indexedDB::IDBFactory;
|
|
|
|
using mozilla::ipc::PrincipalInfo;
|
|
|
|
|
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)
|
2014-12-16 22:26:15 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIndexedDB)
|
2015-03-02 05:23:00 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCacheStorage)
|
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)
|
2014-12-16 22:26:15 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mIndexedDB)
|
2015-03-02 05:23:00 -08:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCacheStorage)
|
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)
|
2015-01-17 19:17:06 -08:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
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*
|
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
|
|
|
WorkerGlobalScope::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
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
|
|
|
}
|
|
|
|
|
2015-03-02 05:23:00 -08:00
|
|
|
already_AddRefed<CacheStorage>
|
|
|
|
WorkerGlobalScope::GetCaches(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!mCacheStorage) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mCacheStorage = CacheStorage::CreateOnWorker(cache::DEFAULT_NAMESPACE, this,
|
|
|
|
mWorkerPrivate, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<CacheStorage> ref = mCacheStorage;
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
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
|
2015-03-05 19:48:07 -08:00
|
|
|
WorkerGlobalScope::Close(JSContext* aCx, ErrorResult& aRv)
|
2013-11-05 06:16:26 -08:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 07:04:23 -07:00
|
|
|
|
2015-03-05 19:48:07 -08:00
|
|
|
if (mWorkerPrivate->IsServiceWorker()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
} else {
|
|
|
|
mWorkerPrivate->CloseInternal(aCx);
|
|
|
|
}
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
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();
|
2015-03-31 03:22:40 -07:00
|
|
|
scriptloader::Load(aCx, mWorkerPrivate, aScriptURLs, WorkerScript, aRv);
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
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
|
|
|
|
2014-12-03 10:57:39 -08:00
|
|
|
bool isInterval = aTimeout.WasPassed();
|
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,
|
2014-12-03 10:57:39 -08:00
|
|
|
aArguments, isInterval, aRv);
|
2013-11-05 06:16:26 -08:00
|
|
|
}
|
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
|
|
|
|
2014-12-03 10:57:39 -08:00
|
|
|
bool isInterval = aTimeout.WasPassed();
|
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,
|
2014-12-03 10:57:39 -08:00
|
|
|
aHandler, timeout, dummy, isInterval, aRv);
|
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::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>
|
2014-11-20 03:58:00 -08:00
|
|
|
WorkerGlobalScope::Fetch(const RequestOrUSVString& aInput,
|
2014-07-24 18:30:07 -07:00
|
|
|
const RequestInit& aInit, ErrorResult& aRv)
|
|
|
|
{
|
2014-10-06 11:01:20 -07:00
|
|
|
return FetchRequest(this, aInput, aInit, aRv);
|
2014-07-24 18:30:07 -07:00
|
|
|
}
|
|
|
|
|
2014-12-16 22:26:15 -08:00
|
|
|
already_AddRefed<IDBFactory>
|
|
|
|
WorkerGlobalScope::GetIndexedDB(ErrorResult& aErrorResult)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
nsRefPtr<IDBFactory> indexedDB = mIndexedDB;
|
|
|
|
|
|
|
|
if (!indexedDB) {
|
|
|
|
if (!mWorkerPrivate->IsIndexedDBAllowed()) {
|
|
|
|
NS_WARNING("IndexedDB is not allowed in this worker!");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
JSContext* cx = mWorkerPrivate->GetJSContext();
|
|
|
|
MOZ_ASSERT(cx);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> owningObject(cx, GetGlobalJSObject());
|
|
|
|
MOZ_ASSERT(owningObject);
|
|
|
|
|
|
|
|
const PrincipalInfo& principalInfo = mWorkerPrivate->GetPrincipalInfo();
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
IDBFactory::CreateForWorker(cx,
|
|
|
|
owningObject,
|
|
|
|
principalInfo,
|
|
|
|
mWorkerPrivate->WindowID(),
|
|
|
|
getter_AddRefs(indexedDB));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
aErrorResult = rv;
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIndexedDB = indexedDB;
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexedDB.forget();
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:26 -08:00
|
|
|
DedicatedWorkerGlobalScope::DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerGlobalScope(aWorkerPrivate)
|
2011-07-17 12:09:13 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-08 13:56:42 -08:00
|
|
|
bool
|
|
|
|
DedicatedWorkerGlobalScope::WrapGlobalObject(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector)
|
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(),
|
2015-01-08 13:56:42 -08:00
|
|
|
true, aReflector);
|
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
|
|
|
|
2015-01-08 13:56:42 -08:00
|
|
|
bool
|
|
|
|
SharedWorkerGlobalScope::WrapGlobalObject(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector)
|
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(),
|
2015-01-08 13:56:42 -08:00
|
|
|
true, aReflector);
|
2011-07-17 12:09:13 -07:00
|
|
|
}
|
|
|
|
|
2014-10-27 04:03:00 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerGlobalScope, WorkerGlobalScope,
|
|
|
|
mClients)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(ServiceWorkerGlobalScope)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(WorkerGlobalScope)
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(ServiceWorkerGlobalScope, WorkerGlobalScope)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(ServiceWorkerGlobalScope, WorkerGlobalScope)
|
|
|
|
|
2014-05-13 14:28:01 -07:00
|
|
|
ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aScope)
|
|
|
|
: WorkerGlobalScope(aWorkerPrivate),
|
|
|
|
mScope(NS_ConvertUTF8toUTF16(aScope))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-27 04:03:00 -07:00
|
|
|
ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-08 13:56:42 -08:00
|
|
|
bool
|
|
|
|
ServiceWorkerGlobalScope::WrapGlobalObject(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector)
|
2014-05-13 14:28:01 -07:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
|
|
|
|
JS::CompartmentOptions options;
|
|
|
|
mWorkerPrivate->CopyJSCompartmentOptions(options);
|
|
|
|
|
|
|
|
return ServiceWorkerGlobalScopeBinding_workers::Wrap(aCx, this, this, options,
|
|
|
|
GetWorkerPrincipal(),
|
2015-01-08 13:56:42 -08:00
|
|
|
true, aReflector);
|
2014-05-13 14:28:01 -07:00
|
|
|
}
|
|
|
|
|
2014-10-27 04:03:00 -07:00
|
|
|
ServiceWorkerClients*
|
|
|
|
ServiceWorkerGlobalScope::Clients()
|
|
|
|
{
|
|
|
|
if (!mClients) {
|
|
|
|
mClients = new ServiceWorkerClients(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mClients;
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:51:53 -08:00
|
|
|
WorkerDebuggerGlobalScope::WorkerDebuggerGlobalScope(
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerDebuggerGlobalScope::~WorkerDebuggerGlobalScope()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(WorkerDebuggerGlobalScope, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WorkerDebuggerGlobalScope, DOMEventTargetHelper)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WorkerDebuggerGlobalScope)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGlobalObject)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerDebuggerGlobalScope::WrapGlobalObject(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
JS::CompartmentOptions options;
|
|
|
|
mWorkerPrivate->CopyJSCompartmentOptions(options);
|
|
|
|
|
|
|
|
return WorkerDebuggerGlobalScopeBinding::Wrap(aCx, this, this, options,
|
|
|
|
GetWorkerPrincipal(), true,
|
|
|
|
aReflector);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::GetGlobal(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aGlobal)
|
|
|
|
{
|
|
|
|
aGlobal.set(mWorkerPrivate->GetOrCreateGlobalScope(aCx)->GetWrapper());
|
|
|
|
}
|
|
|
|
|
2015-03-31 03:22:40 -07:00
|
|
|
class WorkerDebuggerSandboxPrivate : public nsIGlobalObject,
|
|
|
|
public nsWrapperCache
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
explicit WorkerDebuggerSandboxPrivate(JSObject *global)
|
|
|
|
{
|
|
|
|
SetWrapper(global);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(WorkerDebuggerSandboxPrivate,
|
|
|
|
nsIGlobalObject)
|
|
|
|
|
2015-03-31 10:41:20 -07:00
|
|
|
virtual JSObject *GetGlobalJSObject() override
|
2015-03-31 03:22:40 -07:00
|
|
|
{
|
|
|
|
return GetWrapper();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual JSObject* WrapObject(JSContext* cx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) override
|
|
|
|
{
|
|
|
|
MOZ_CRASH("WorkerDebuggerSandboxPrivate doesn't use DOM bindings!");
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
virtual ~WorkerDebuggerSandboxPrivate()
|
|
|
|
{
|
|
|
|
ClearWrapper();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE_0(WorkerDebuggerSandboxPrivate)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(WorkerDebuggerSandboxPrivate)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(WorkerDebuggerSandboxPrivate)
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WorkerDebuggerSandboxPrivate)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGlobalObject)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
static bool
|
|
|
|
workerdebuggersandbox_enumerate(JSContext *cx, JS::Handle<JSObject *> obj)
|
|
|
|
{
|
|
|
|
return JS_EnumerateStandardClasses(cx, obj);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
workerdebuggersandbox_resolve(JSContext *cx, JS::Handle<JSObject *> obj,
|
|
|
|
JS::Handle<jsid> id, bool *resolvedp)
|
|
|
|
{
|
|
|
|
return JS_ResolveStandardClass(cx, obj, id, resolvedp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
workerdebuggersandbox_convert(JSContext *cx, JS::Handle<JSObject *> obj,
|
|
|
|
JSType type, JS::MutableHandle<JS::Value> vp)
|
|
|
|
{
|
|
|
|
if (type == JSTYPE_OBJECT) {
|
|
|
|
vp.set(OBJECT_TO_JSVAL(obj));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return JS::OrdinaryToPrimitive(cx, obj, type, vp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
workerdebuggersandbox_finalize(js::FreeOp *fop, JSObject *obj)
|
|
|
|
{
|
|
|
|
nsIGlobalObject *globalObject =
|
|
|
|
static_cast<nsIGlobalObject *>(JS_GetPrivate(obj));
|
|
|
|
NS_RELEASE(globalObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
workerdebuggersandbox_moved(JSObject *obj, const JSObject *old)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const js::Class workerdebuggersandbox_class = {
|
|
|
|
"workerdebuggersandbox",
|
|
|
|
JSCLASS_GLOBAL_FLAGS | JSCLASS_HAS_PRIVATE | JSCLASS_PRIVATE_IS_NSISUPPORTS,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
workerdebuggersandbox_enumerate,
|
|
|
|
workerdebuggersandbox_resolve,
|
|
|
|
workerdebuggersandbox_convert,
|
|
|
|
workerdebuggersandbox_finalize,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
JS_GlobalObjectTraceHook,
|
|
|
|
JS_NULL_CLASS_SPEC, {
|
|
|
|
nullptr,
|
|
|
|
nullptr,
|
|
|
|
false,
|
|
|
|
nullptr,
|
|
|
|
workerdebuggersandbox_moved
|
|
|
|
}, JS_NULL_OBJECT_OPS
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::CreateSandbox(JSContext* aCx, const nsAString& aName,
|
|
|
|
JS::Handle<JSObject*> aPrototype,
|
|
|
|
JS::MutableHandle<JSObject*> aResult)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
JS::CompartmentOptions options;
|
|
|
|
options.setInvisibleToDebugger(true);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> sandbox(aCx,
|
|
|
|
JS_NewGlobalObject(aCx, js::Jsvalify(&workerdebuggersandbox_class), nullptr,
|
|
|
|
JS::DontFireOnNewGlobalHook, options));
|
|
|
|
if (!sandbox) {
|
|
|
|
JS_ReportError(aCx, "Can't create sandbox!");
|
|
|
|
aResult.set(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
JSAutoCompartment ac(aCx, sandbox);
|
|
|
|
|
|
|
|
JS::Rooted<JSObject*> prototype(aCx, aPrototype);
|
|
|
|
if (!JS_WrapObject(aCx, &prototype)) {
|
|
|
|
JS_ReportError(aCx, "Can't wrap sandbox prototype!");
|
|
|
|
aResult.set(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_SetPrototype(aCx, sandbox, prototype)) {
|
|
|
|
JS_ReportError(aCx, "Can't set sandbox prototype!");
|
|
|
|
aResult.set(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIGlobalObject> globalObject =
|
|
|
|
new WorkerDebuggerSandboxPrivate(sandbox);
|
|
|
|
|
|
|
|
// Pass on ownership of globalObject to |sandbox|.
|
|
|
|
JS_SetPrivate(sandbox, globalObject.forget().take());
|
|
|
|
}
|
|
|
|
|
|
|
|
JS_FireOnNewGlobalObject(aCx, sandbox);
|
|
|
|
|
|
|
|
if (!JS_WrapObject(aCx, &sandbox)) {
|
|
|
|
JS_ReportError(aCx, "Can't wrap sandbox!");
|
|
|
|
aResult.set(nullptr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult.set(sandbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::LoadSubScript(JSContext* aCx,
|
|
|
|
const nsAString& aURL,
|
|
|
|
const Optional<JS::Handle<JSObject*>>& aSandbox,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
Maybe<JSAutoCompartment> ac;
|
|
|
|
if (aSandbox.WasPassed()) {
|
|
|
|
JS::Rooted<JSObject*> sandbox(aCx, js::CheckedUnwrap(aSandbox.Value()));
|
|
|
|
if (!IsDebuggerSandbox(sandbox)) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ac.emplace(aCx, sandbox);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsString> urls;
|
|
|
|
urls.AppendElement(aURL);
|
|
|
|
scriptloader::Load(aCx, mWorkerPrivate, urls, DebuggerScript, aRv);
|
|
|
|
}
|
|
|
|
|
2015-03-26 23:17:16 -07:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::EnterEventLoop()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->EnterDebuggerEventLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::LeaveEventLoop()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->LeaveDebuggerEventLoop();
|
|
|
|
}
|
|
|
|
|
2015-03-20 04:15:59 -07:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::PostMessage(const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->PostMessageToDebugger(aMessage);
|
|
|
|
}
|
|
|
|
|
2015-03-30 04:54:38 -07:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::SetImmediate(JSContext* aCx, Function& aHandler,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->SetDebuggerImmediate(aCx, aHandler, aRv);
|
|
|
|
}
|
|
|
|
|
2015-03-26 12:09:45 -07:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::ReportError(JSContext* aCx,
|
|
|
|
const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
JS::AutoFilename afn;
|
|
|
|
uint32_t lineno = 0;
|
|
|
|
JS::DescribeScriptedCaller(aCx, &afn, &lineno);
|
|
|
|
nsString filename(NS_ConvertUTF8toUTF16(afn.get()));
|
|
|
|
mWorkerPrivate->ReportErrorToDebugger(filename, lineno, aMessage);
|
|
|
|
}
|
|
|
|
|
2015-03-03 15:51:53 -08:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::Dump(JSContext* aCx,
|
|
|
|
const Optional<nsAString>& aString) const
|
|
|
|
{
|
|
|
|
return mWorkerPrivate->GetOrCreateGlobalScope(aCx)->Dump(aString);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIGlobalObject*
|
|
|
|
GetGlobalObjectForGlobal(JSObject* global)
|
|
|
|
{
|
|
|
|
nsIGlobalObject* globalObject = nullptr;
|
|
|
|
UNWRAP_WORKER_OBJECT(WorkerGlobalScope, global, globalObject);
|
2015-03-31 03:22:40 -07:00
|
|
|
|
2015-03-03 15:51:53 -08:00
|
|
|
if (!globalObject) {
|
|
|
|
UNWRAP_OBJECT(WorkerDebuggerGlobalScope, global, globalObject);
|
2015-03-31 03:22:40 -07:00
|
|
|
|
|
|
|
if (!globalObject) {
|
|
|
|
MOZ_ASSERT(IsDebuggerSandbox(global));
|
|
|
|
globalObject = static_cast<nsIGlobalObject *>(JS_GetPrivate(global));
|
|
|
|
|
|
|
|
MOZ_ASSERT(globalObject);
|
|
|
|
}
|
2015-03-03 15:51:53 -08:00
|
|
|
}
|
2015-03-31 03:22:40 -07:00
|
|
|
|
2015-03-03 15:51:53 -08:00
|
|
|
return globalObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsWorkerGlobal(JSObject* object)
|
|
|
|
{
|
|
|
|
nsIGlobalObject* globalObject;
|
|
|
|
return NS_SUCCEEDED(UNWRAP_WORKER_OBJECT(WorkerGlobalScope, object,
|
|
|
|
globalObject)) && !!globalObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsDebuggerGlobal(JSObject* object)
|
|
|
|
{
|
|
|
|
nsIGlobalObject* globalObject;
|
|
|
|
return NS_SUCCEEDED(UNWRAP_OBJECT(WorkerDebuggerGlobalScope, object,
|
|
|
|
globalObject)) && !!globalObject;
|
|
|
|
}
|
|
|
|
|
2015-03-31 03:22:40 -07:00
|
|
|
bool
|
|
|
|
IsDebuggerSandbox(JSObject* object)
|
|
|
|
{
|
|
|
|
return js::GetObjectClass(object) == &workerdebuggersandbox_class;
|
|
|
|
}
|
|
|
|
|
2013-11-05 06:16:24 -08:00
|
|
|
bool
|
|
|
|
GetterOnlyJSNative(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
2015-02-27 07:08:15 -08:00
|
|
|
JS_ReportErrorNumber(aCx, js::GetErrorMessage, nullptr, JSMSG_GETTER_ONLY);
|
2013-11-05 06:16:24 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:21:32 -07:00
|
|
|
namespace {
|
|
|
|
|
2014-12-19 02:00:29 -08:00
|
|
|
class WorkerScopeUnregisterRunnable;
|
2015-03-21 09:28:04 -07:00
|
|
|
class UnregisterResultRunnable final : public WorkerRunnable
|
2014-09-10 08:21:32 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
enum State { Succeeded, Failed };
|
|
|
|
|
|
|
|
UnregisterResultRunnable(WorkerPrivate* aWorkerPrivate,
|
2014-12-19 02:00:29 -08:00
|
|
|
WorkerScopeUnregisterRunnable* aRunnable,
|
2014-09-10 08:21:32 -07:00
|
|
|
State aState, bool aValue)
|
|
|
|
: WorkerRunnable(aWorkerPrivate,
|
|
|
|
WorkerThreadUnchangedBusyCount)
|
|
|
|
, mRunnable(aRunnable), mState(aState), mValue(aValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(mRunnable);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
2015-03-21 09:28:04 -07:00
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override;
|
2014-09-10 08:21:32 -07:00
|
|
|
|
|
|
|
private:
|
2014-12-19 02:00:29 -08:00
|
|
|
nsRefPtr<WorkerScopeUnregisterRunnable> mRunnable;
|
2014-09-10 08:21:32 -07:00
|
|
|
State mState;
|
|
|
|
bool mValue;
|
|
|
|
};
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class WorkerScopeUnregisterRunnable final : public nsRunnable
|
2015-03-27 11:52:19 -07:00
|
|
|
, public nsIServiceWorkerUnregisterCallback
|
|
|
|
, public WorkerFeature
|
2014-09-10 08:21:32 -07:00
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsString mScope;
|
2014-10-27 16:37:03 -07:00
|
|
|
|
|
|
|
// Worker thread only.
|
|
|
|
nsRefPtr<Promise> mWorkerPromise;
|
2014-09-10 08:21:32 -07:00
|
|
|
bool mCleanedUp;
|
|
|
|
|
2014-10-27 16:37:03 -07:00
|
|
|
~WorkerScopeUnregisterRunnable()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mCleanedUp);
|
|
|
|
}
|
|
|
|
|
2014-09-10 08:21:32 -07:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
|
|
|
|
2014-12-19 02:00:29 -08:00
|
|
|
WorkerScopeUnregisterRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
Promise* aWorkerPromise,
|
|
|
|
const nsAString& aScope)
|
2014-09-10 08:21:32 -07:00
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
, mScope(aScope)
|
2014-10-27 16:37:03 -07:00
|
|
|
, mWorkerPromise(aWorkerPromise)
|
2014-09-10 08:21:32 -07:00
|
|
|
, mCleanedUp(false)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aWorkerPromise);
|
|
|
|
|
|
|
|
if (!mWorkerPrivate->AddFeature(mWorkerPrivate->GetJSContext(), this)) {
|
|
|
|
MOZ_ASSERT(false, "cannot add the worker feature!");
|
2014-10-27 16:37:03 -07:00
|
|
|
mWorkerPromise = nullptr;
|
2014-09-10 08:21:32 -07:00
|
|
|
mCleanedUp = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise*
|
|
|
|
WorkerPromise() const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2014-10-27 16:37:03 -07:00
|
|
|
MOZ_ASSERT(!mCleanedUp);
|
2014-09-10 08:21:32 -07:00
|
|
|
return mWorkerPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-21 09:28:04 -07:00
|
|
|
UnregisterSucceeded(bool aState) override
|
2014-09-10 08:21:32 -07:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsRefPtr<UnregisterResultRunnable> runnable =
|
|
|
|
new UnregisterResultRunnable(mWorkerPrivate, this,
|
|
|
|
UnregisterResultRunnable::Succeeded, aState);
|
|
|
|
runnable->Dispatch(nullptr);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-21 09:28:04 -07:00
|
|
|
UnregisterFailed() override
|
2014-09-10 08:21:32 -07:00
|
|
|
{
|
|
|
|
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);
|
2014-10-27 16:37:03 -07:00
|
|
|
mWorkerPromise = nullptr;
|
2014-09-10 08:21:32 -07:00
|
|
|
mCleanedUp = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2015-03-21 09:28:04 -07:00
|
|
|
Run() override
|
2014-09-10 08:21:32 -07:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
|
2015-02-11 03:53:00 -08:00
|
|
|
rv = swm->Unregister(mWorkerPrivate->GetPrincipal(), this, mScope);
|
2014-09-10 08:21:32 -07:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
UnregisterFailed();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
virtual bool Notify(JSContext* aCx, workers::Status aStatus) override
|
2014-09-10 08:21:32 -07:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(aStatus > workers::Running);
|
2014-10-27 16:37:03 -07:00
|
|
|
CleanUp(aCx);
|
2014-09-10 08:21:32 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-12-19 02:00:29 -08:00
|
|
|
NS_IMPL_ISUPPORTS_INHERITED(WorkerScopeUnregisterRunnable, nsRunnable,
|
2014-09-10 08:21:32 -07:00
|
|
|
nsIServiceWorkerUnregisterCallback)
|
|
|
|
|
|
|
|
bool
|
|
|
|
UnregisterResultRunnable::WorkerRun(JSContext* aCx,
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
if (mState == Failed) {
|
|
|
|
mRunnable->WorkerPromise()->MaybeReject(aCx, JS::UndefinedHandleValue);
|
2014-10-27 16:37:03 -07:00
|
|
|
} else {
|
|
|
|
mRunnable->WorkerPromise()->MaybeResolve(mValue);
|
2014-09-10 08:21:32 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-12-19 02:00:29 -08:00
|
|
|
nsRefPtr<WorkerScopeUnregisterRunnable> runnable =
|
|
|
|
new WorkerScopeUnregisterRunnable(mWorkerPrivate, promise, mScope);
|
2014-10-27 16:37:03 -07:00
|
|
|
|
|
|
|
// Ensure the AddFeature succeeded before dispatching.
|
|
|
|
// Otherwise we let the promise remain pending since script is going to stop
|
|
|
|
// soon anyway.
|
|
|
|
if (runnable->WorkerPromise()) {
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
2014-09-10 08:21:32 -07:00
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-06 08:45:14 -07:00
|
|
|
namespace {
|
|
|
|
|
2015-03-21 09:28:04 -07:00
|
|
|
class UpdateRunnable final : public nsRunnable
|
2014-10-06 08:45:14 -07:00
|
|
|
{
|
|
|
|
nsString mScope;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit UpdateRunnable(const nsAString& aScope)
|
|
|
|
: mScope(aScope)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Run()
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIServiceWorkerManager> swm =
|
|
|
|
do_GetService(SERVICEWORKERMANAGER_CONTRACTID, &rv);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
swm->Update(mScope);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} //anonymous namespace
|
|
|
|
|
|
|
|
void
|
|
|
|
ServiceWorkerGlobalScope::Update()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
|
|
|
|
nsRefPtr<UpdateRunnable> runnable =
|
|
|
|
new UpdateRunnable(mScope);
|
|
|
|
NS_DispatchToMainThread(runnable);
|
|
|
|
}
|
|
|
|
|
2011-07-17 12:09:13 -07:00
|
|
|
END_WORKERS_NAMESPACE
|