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: */
|
2012-05-21 15:12:37 +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/. */
|
2011-07-17 23:09:13 +04:00
|
|
|
|
|
|
|
#include "WorkerScope.h"
|
|
|
|
|
|
|
|
#include "jsapi.h"
|
2014-03-17 10:56:53 +04:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2014-12-17 09:26:15 +03:00
|
|
|
#include "mozilla/dom/BindingDeclarations.h"
|
2014-09-10 19:21:32 +04:00
|
|
|
#include "mozilla/dom/Console.h"
|
2013-11-05 18:16:26 +04:00
|
|
|
#include "mozilla/dom/DedicatedWorkerGlobalScopeBinding.h"
|
2014-10-06 22:01:20 +04:00
|
|
|
#include "mozilla/dom/Fetch.h"
|
2014-09-10 19:21:32 +04:00
|
|
|
#include "mozilla/dom/FunctionBinding.h"
|
2016-02-17 00:46:08 +03:00
|
|
|
#include "mozilla/dom/IDBFactory.h"
|
2015-07-30 21:47:00 +03:00
|
|
|
#include "mozilla/dom/ImageBitmap.h"
|
2015-11-18 11:57:38 +03:00
|
|
|
#include "mozilla/dom/ImageBitmapBinding.h"
|
2016-06-09 20:04:42 +03:00
|
|
|
#include "mozilla/dom/Performance.h"
|
2014-09-10 19:21:32 +04:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/dom/PromiseWorkerProxy.h"
|
2014-05-14 01:28:01 +04:00
|
|
|
#include "mozilla/dom/ServiceWorkerGlobalScopeBinding.h"
|
2013-11-05 18:16:26 +04:00
|
|
|
#include "mozilla/dom/SharedWorkerGlobalScopeBinding.h"
|
2016-04-04 19:29:16 +03:00
|
|
|
#include "mozilla/dom/SimpleGlobalObject.h"
|
2015-03-04 02:51:53 +03:00
|
|
|
#include "mozilla/dom/WorkerDebuggerGlobalScopeBinding.h"
|
|
|
|
#include "mozilla/dom/WorkerGlobalScopeBinding.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "mozilla/dom/WorkerLocation.h"
|
2016-03-19 00:15:46 +03:00
|
|
|
#include "mozilla/dom/WorkerNavigator.h"
|
2015-03-02 16:23:00 +03:00
|
|
|
#include "mozilla/dom/cache/CacheStorage.h"
|
2014-09-10 19:21:32 +04:00
|
|
|
#include "mozilla/Services.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIServiceWorkerManager.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2014-02-12 01:01:29 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
#include <android/log.h>
|
|
|
|
#endif
|
|
|
|
|
2015-09-21 20:29:41 +03:00
|
|
|
#include "Crypto.h"
|
2013-12-10 15:43:16 +04:00
|
|
|
#include "Principal.h"
|
|
|
|
#include "RuntimeService.h"
|
|
|
|
#include "ScriptLoader.h"
|
|
|
|
#include "WorkerPrivate.h"
|
2014-09-10 19:21:32 +04:00
|
|
|
#include "WorkerRunnable.h"
|
2014-10-27 14:03:00 +03:00
|
|
|
#include "ServiceWorkerClients.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "ServiceWorkerManager.h"
|
2015-04-04 08:46:07 +03:00
|
|
|
#include "ServiceWorkerRegistration.h"
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
#ifdef XP_WIN
|
|
|
|
#undef PostMessage
|
|
|
|
#endif
|
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
using namespace mozilla;
|
2012-11-10 19:45:52 +04:00
|
|
|
using namespace mozilla::dom;
|
2011-07-17 23:09:13 +04:00
|
|
|
USING_WORKERS_NAMESPACE
|
|
|
|
|
2015-03-02 16:23:00 +03:00
|
|
|
using mozilla::dom::cache::CacheStorage;
|
2014-12-17 09:26:15 +03:00
|
|
|
using mozilla::ipc::PrincipalInfo;
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
WorkerGlobalScope::WorkerGlobalScope(WorkerPrivate* aWorkerPrivate)
|
2015-07-23 18:30:27 +03:00
|
|
|
: mWindowInteractionsAllowed(0)
|
|
|
|
, mWorkerPrivate(aWorkerPrivate)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
2013-09-09 07:28:48 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
WorkerGlobalScope::~WorkerGlobalScope()
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2014-02-26 23:00:40 +04:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2012-04-27 00:57:33 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(WorkerGlobalScope)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WorkerGlobalScope,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-11-05 18:16:26 +04:00
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
2014-08-08 16:34:30 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConsole)
|
2015-09-21 20:29:41 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCrypto)
|
2014-08-30 03:50:06 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPerformance)
|
2014-09-03 00:02:06 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mNavigator)
|
2014-12-17 09:26:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mIndexedDB)
|
2015-03-02 16:23:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCacheStorage)
|
2015-05-07 10:05:43 +03:00
|
|
|
tmp->TraverseHostObjectURIs(cb);
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WorkerGlobalScope,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-11-05 18:16:26 +04:00
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
2014-08-08 16:34:30 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mConsole)
|
2015-09-21 20:29:41 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCrypto)
|
2014-08-30 03:50:06 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPerformance)
|
2014-09-03 00:02:06 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mLocation)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mNavigator)
|
2014-12-17 09:26:15 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mIndexedDB)
|
2015-03-02 16:23:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCacheStorage)
|
2015-05-07 10:05:43 +03:00
|
|
|
tmp->UnlinkHostObjectURIs();
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(WorkerGlobalScope,
|
2014-04-01 10:13:50 +04:00
|
|
|
DOMEventTargetHelper)
|
2013-11-05 18:16:26 +04:00
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-08-16 18:11:22 +03:00
|
|
|
|
|
|
|
tmp->mWorkerPrivate->TraceTimeouts(aCallbacks, aClosure);
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
2013-08-09 02:55:23 +04:00
|
|
|
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(WorkerGlobalScope, DOMEventTargetHelper)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(WorkerGlobalScope, DOMEventTargetHelper)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WorkerGlobalScope)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIGlobalObject)
|
2015-01-18 06:17:06 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
2014-04-01 10:13:50 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(DOMEventTargetHelper)
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04: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 17:13:33 +03:00
|
|
|
WorkerGlobalScope::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-06-05 18:04:23 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
MOZ_CRASH("We should never get here!");
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-08-08 16:34:30 +04:00
|
|
|
Console*
|
2016-03-24 00:55:07 +03:00
|
|
|
WorkerGlobalScope::GetConsole(ErrorResult& aRv)
|
2014-01-26 16:35:17 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mConsole) {
|
2016-03-24 00:55:07 +03:00
|
|
|
mConsole = Console::Create(nullptr, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-01-26 16:35:17 +04:00
|
|
|
}
|
|
|
|
|
2014-08-08 16:34:30 +04:00
|
|
|
return mConsole;
|
2014-01-26 16:35:17 +04:00
|
|
|
}
|
|
|
|
|
2015-09-21 20:29:41 +03:00
|
|
|
Crypto*
|
|
|
|
WorkerGlobalScope::GetCrypto(ErrorResult& aError)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mCrypto) {
|
|
|
|
mCrypto = new Crypto();
|
|
|
|
mCrypto->Init(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mCrypto;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:23:00 +03:00
|
|
|
already_AddRefed<CacheStorage>
|
|
|
|
WorkerGlobalScope::GetCaches(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!mCacheStorage) {
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mCacheStorage = CacheStorage::CreateOnWorker(cache::DEFAULT_NAMESPACE, this,
|
|
|
|
mWorkerPrivate, aRv);
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<CacheStorage> ref = mCacheStorage;
|
2015-03-02 16:23:00 +03:00
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
already_AddRefed<WorkerLocation>
|
|
|
|
WorkerGlobalScope::Location()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!mLocation) {
|
|
|
|
WorkerPrivate::LocationInfo& info = mWorkerPrivate->GetLocationInfo();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
mLocation = WorkerLocation::Create(info);
|
|
|
|
MOZ_ASSERT(mLocation);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerLocation> location = mLocation;
|
2013-11-05 18:16:26 +04:00
|
|
|
return location.forget();
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
already_AddRefed<WorkerNavigator>
|
|
|
|
WorkerGlobalScope::Navigator()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!mNavigator) {
|
2013-11-20 03:08:50 +04:00
|
|
|
mNavigator = WorkerNavigator::Create(mWorkerPrivate->OnLine());
|
2013-11-05 18:16:26 +04:00
|
|
|
MOZ_ASSERT(mNavigator);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerNavigator> navigator = mNavigator;
|
2013-11-05 18:16:26 +04:00
|
|
|
return navigator.forget();
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-20 03:08:50 +04:00
|
|
|
already_AddRefed<WorkerNavigator>
|
|
|
|
WorkerGlobalScope::GetExistingNavigator() const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerNavigator> navigator = mNavigator;
|
2013-11-20 03:08:50 +04:00
|
|
|
return navigator.forget();
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
2015-03-06 06:48:07 +03:00
|
|
|
WorkerGlobalScope::Close(JSContext* aCx, ErrorResult& aRv)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2015-03-06 06:48:07 +03:00
|
|
|
if (mWorkerPrivate->IsServiceWorker()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
} else {
|
|
|
|
mWorkerPrivate->CloseInternal(aCx);
|
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
OnErrorEventHandlerNonNull*
|
|
|
|
WorkerGlobalScope::GetOnerror()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* elm = GetExistingListenerManager();
|
2013-11-05 18:16:26 +04:00
|
|
|
return elm ? elm->GetOnErrorEventHandler() : nullptr;
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::SetOnerror(OnErrorEventHandlerNonNull* aHandler)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-03-17 10:56:53 +04:00
|
|
|
EventListenerManager* elm = GetOrCreateListenerManager();
|
2013-11-05 18:16:26 +04:00
|
|
|
if (elm) {
|
|
|
|
elm->SetEventHandler(aHandler);
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
2016-02-29 22:52:42 +03:00
|
|
|
WorkerGlobalScope::ImportScripts(const Sequence<nsString>& aScriptURLs,
|
2013-11-05 18:16:26 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-02-29 22:52:42 +03:00
|
|
|
scriptloader::Load(mWorkerPrivate, aScriptURLs, WorkerScript, aRv);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t
|
|
|
|
WorkerGlobalScope::SetTimeout(JSContext* aCx,
|
|
|
|
Function& aHandler,
|
|
|
|
const int32_t aTimeout,
|
|
|
|
const Sequence<JS::Value>& aArguments,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-08-16 18:11:22 +03:00
|
|
|
return mWorkerPrivate->SetTimeout(aCx, &aHandler, EmptyString(), aTimeout,
|
|
|
|
aArguments, false, aRv);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t
|
2016-04-07 00:20:30 +03:00
|
|
|
WorkerGlobalScope::SetTimeout(JSContext* aCx,
|
2014-02-19 19:13:38 +04:00
|
|
|
const nsAString& aHandler,
|
2013-11-05 18:16:26 +04:00
|
|
|
const int32_t aTimeout,
|
2014-02-19 19:13:38 +04:00
|
|
|
const Sequence<JS::Value>& /* unused */,
|
2013-11-05 18:16:26 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2016-08-16 18:11:22 +03:00
|
|
|
Sequence<JS::Value> dummy;
|
|
|
|
return mWorkerPrivate->SetTimeout(aCx, nullptr, aHandler, aTimeout, dummy,
|
|
|
|
false, aRv);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
2016-03-10 12:50:56 +03:00
|
|
|
WorkerGlobalScope::ClearTimeout(int32_t aHandle)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mWorkerPrivate->ClearTimeout(aHandle);
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04: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 18:04:23 +04:00
|
|
|
|
2014-12-03 21:57:39 +03:00
|
|
|
bool isInterval = aTimeout.WasPassed();
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t timeout = aTimeout.WasPassed() ? aTimeout.Value() : 0;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2016-08-16 18:11:22 +03:00
|
|
|
return mWorkerPrivate->SetTimeout(aCx, &aHandler, EmptyString(), timeout,
|
|
|
|
aArguments, isInterval, aRv);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t
|
2016-04-07 00:57:44 +03:00
|
|
|
WorkerGlobalScope::SetInterval(JSContext* aCx,
|
2014-02-19 19:13:38 +04:00
|
|
|
const nsAString& aHandler,
|
2013-11-05 18:16:26 +04:00
|
|
|
const Optional<int32_t>& aTimeout,
|
2014-02-19 19:13:38 +04:00
|
|
|
const Sequence<JS::Value>& /* unused */,
|
2013-11-05 18:16:26 +04:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
Sequence<JS::Value> dummy;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-12-03 21:57:39 +03:00
|
|
|
bool isInterval = aTimeout.WasPassed();
|
2013-11-05 18:16:26 +04:00
|
|
|
int32_t timeout = aTimeout.WasPassed() ? aTimeout.Value() : 0;
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2016-08-16 18:11:22 +03:00
|
|
|
return mWorkerPrivate->SetTimeout(aCx, nullptr, aHandler, timeout, dummy,
|
|
|
|
isInterval, aRv);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
2016-03-10 12:50:56 +03:00
|
|
|
WorkerGlobalScope::ClearInterval(int32_t aHandle)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
mWorkerPrivate->ClearTimeout(aHandle);
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::Atob(const nsAString& aAtob, nsAString& aOutput, ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
aRv = nsContentUtils::Atob(aAtob, aOutput);
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::Btoa(const nsAString& aBtoa, nsAString& aOutput, ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
aRv = nsContentUtils::Btoa(aBtoa, aOutput);
|
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
void
|
|
|
|
WorkerGlobalScope::Dump(const Optional<nsAString>& aString) const
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
if (!aString.WasPassed()) {
|
|
|
|
return;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
|
|
|
|
2015-10-24 19:06:57 +03:00
|
|
|
#if !(defined(DEBUG) || defined(MOZ_ENABLE_JS_DUMP))
|
2013-11-24 23:27:15 +04:00
|
|
|
if (!mWorkerPrivate->DumpEnabled()) {
|
2013-11-05 18:16:26 +04:00
|
|
|
return;
|
2013-06-05 18:04:23 +04:00
|
|
|
}
|
2015-10-24 19:06:57 +03:00
|
|
|
#endif
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-02-12 01:01:29 +04:00
|
|
|
NS_ConvertUTF16toUTF8 str(aString.Value());
|
|
|
|
|
2016-01-07 00:18:29 +03:00
|
|
|
MOZ_LOG(nsContentUtils::DOMDumpLog(), LogLevel::Debug, ("[Worker.Dump] %s", str.get()));
|
2014-02-12 01:01:29 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
__android_log_print(ANDROID_LOG_INFO, "Gecko", "%s", str.get());
|
|
|
|
#endif
|
|
|
|
fputs(str.get(), stdout);
|
|
|
|
fflush(stdout);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2013-06-05 18:04:23 +04:00
|
|
|
|
2014-08-30 03:50:06 +04:00
|
|
|
Performance*
|
|
|
|
WorkerGlobalScope::GetPerformance()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (!mPerformance) {
|
2016-06-09 20:04:42 +03:00
|
|
|
mPerformance = Performance::CreateForWorker(mWorkerPrivate);
|
2014-08-30 03:50:06 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return mPerformance;
|
|
|
|
}
|
|
|
|
|
2014-07-25 05:30:07 +04:00
|
|
|
already_AddRefed<Promise>
|
2014-11-20 14:58:00 +03:00
|
|
|
WorkerGlobalScope::Fetch(const RequestOrUSVString& aInput,
|
2014-07-25 05:30:07 +04:00
|
|
|
const RequestInit& aInit, ErrorResult& aRv)
|
|
|
|
{
|
2014-10-06 22:01:20 +04:00
|
|
|
return FetchRequest(this, aInput, aInit, aRv);
|
2014-07-25 05:30:07 +04:00
|
|
|
}
|
|
|
|
|
2014-12-17 09:26:15 +03:00
|
|
|
already_AddRefed<IDBFactory>
|
|
|
|
WorkerGlobalScope::GetIndexedDB(ErrorResult& aErrorResult)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IDBFactory> indexedDB = mIndexedDB;
|
2014-12-17 09:26:15 +03:00
|
|
|
|
|
|
|
if (!indexedDB) {
|
2015-07-16 00:01:02 +03:00
|
|
|
if (!mWorkerPrivate->IsStorageAllowed()) {
|
2014-12-17 09:26:15 +03:00
|
|
|
NS_WARNING("IndexedDB is not allowed in this worker!");
|
2015-07-16 00:01:02 +03:00
|
|
|
aErrorResult = NS_ERROR_DOM_SECURITY_ERR;
|
2014-12-17 09:26:15 +03:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:47:00 +03:00
|
|
|
already_AddRefed<Promise>
|
|
|
|
WorkerGlobalScope::CreateImageBitmap(const ImageBitmapSource& aImage,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-11-18 11:57:38 +03:00
|
|
|
if (aImage.IsArrayBuffer() || aImage.IsArrayBufferView()) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:47:00 +03:00
|
|
|
return ImageBitmap::Create(this, aImage, Nothing(), aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
WorkerGlobalScope::CreateImageBitmap(const ImageBitmapSource& aImage,
|
|
|
|
int32_t aSx, int32_t aSy, int32_t aSw, int32_t aSh,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2015-11-18 11:57:38 +03:00
|
|
|
if (aImage.IsArrayBuffer() || aImage.IsArrayBufferView()) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-30 21:47:00 +03:00
|
|
|
return ImageBitmap::Create(this, aImage, Some(gfx::IntRect(aSx, aSy, aSw, aSh)), aRv);
|
|
|
|
}
|
|
|
|
|
2016-04-27 06:47:36 +03:00
|
|
|
already_AddRefed<mozilla::dom::Promise>
|
|
|
|
WorkerGlobalScope::CreateImageBitmap(const ImageBitmapSource& aImage,
|
|
|
|
int32_t aOffset, int32_t aLength,
|
|
|
|
ImageBitmapFormat aFormat,
|
|
|
|
const Sequence<ChannelPixelLayout>& aLayout,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (aImage.IsArrayBuffer() || aImage.IsArrayBufferView()) {
|
|
|
|
return ImageBitmap::Create(this, aImage, aOffset, aLength, aFormat, aLayout,
|
|
|
|
aRv);
|
|
|
|
} else {
|
|
|
|
aRv.Throw(NS_ERROR_TYPE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
DedicatedWorkerGlobalScope::DedicatedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate)
|
|
|
|
: WorkerGlobalScope(aWorkerPrivate)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-09 00:56:42 +03:00
|
|
|
bool
|
|
|
|
DedicatedWorkerGlobalScope::WrapGlobalObject(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector)
|
2011-07-17 23:09:13 +04:00
|
|
|
{
|
2013-11-05 18:16:26 +04:00
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(!mWorkerPrivate->IsSharedWorker());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-12-10 15:43:16 +04:00
|
|
|
JS::CompartmentOptions options;
|
|
|
|
mWorkerPrivate->CopyJSCompartmentOptions(options);
|
|
|
|
|
2015-06-11 00:12:55 +03:00
|
|
|
const bool usesSystemPrincipal = mWorkerPrivate->UsesSystemPrincipal();
|
|
|
|
|
|
|
|
// Note that xpc::ShouldDiscardSystemSource() and
|
|
|
|
// xpc::ExtraWarningsForSystemJS() read prefs that are cached on the main
|
|
|
|
// thread. This is benignly racey.
|
2016-04-21 16:48:59 +03:00
|
|
|
const bool discardSource = usesSystemPrincipal &&
|
2015-06-11 00:12:55 +03:00
|
|
|
xpc::ShouldDiscardSystemSource();
|
|
|
|
const bool extraWarnings = usesSystemPrincipal &&
|
|
|
|
xpc::ExtraWarningsForSystemJS();
|
|
|
|
|
2015-12-29 01:15:52 +03:00
|
|
|
JS::CompartmentBehaviors& behaviors = options.behaviors();
|
|
|
|
behaviors.setDiscardSource(discardSource)
|
|
|
|
.extraWarningsOverride().set(extraWarnings);
|
2015-06-11 00:12:55 +03:00
|
|
|
|
2016-02-02 00:37:27 +03:00
|
|
|
const bool sharedMemoryEnabled = xpc::SharedMemoryEnabled();
|
2016-02-01 23:48:58 +03:00
|
|
|
|
2016-01-06 18:31:38 +03:00
|
|
|
JS::CompartmentCreationOptions& creationOptions = options.creationOptions();
|
2016-04-21 16:48:59 +03:00
|
|
|
creationOptions.setSharedMemoryAndAtomicsEnabled(sharedMemoryEnabled);
|
2016-01-06 18:31:38 +03:00
|
|
|
|
2016-06-28 20:47:23 +03:00
|
|
|
return DedicatedWorkerGlobalScopeBinding::Wrap(aCx, this, this,
|
|
|
|
options,
|
|
|
|
GetWorkerPrincipal(),
|
|
|
|
true, aReflector);
|
2013-11-05 18:16:26 +04:00
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04: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 23:09:13 +04:00
|
|
|
|
2013-11-05 18:16:26 +04:00
|
|
|
SharedWorkerGlobalScope::SharedWorkerGlobalScope(WorkerPrivate* aWorkerPrivate,
|
2014-02-06 18:39:10 +04:00
|
|
|
const nsCString& aName)
|
2013-11-05 18:16:26 +04:00
|
|
|
: WorkerGlobalScope(aWorkerPrivate), mName(aName)
|
|
|
|
{
|
|
|
|
}
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2015-01-09 00:56:42 +03:00
|
|
|
bool
|
|
|
|
SharedWorkerGlobalScope::WrapGlobalObject(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector)
|
2013-11-05 18:16:26 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsSharedWorker());
|
2011-07-17 23:09:13 +04:00
|
|
|
|
2013-12-10 15:43:16 +04:00
|
|
|
JS::CompartmentOptions options;
|
|
|
|
mWorkerPrivate->CopyJSCompartmentOptions(options);
|
|
|
|
|
2016-06-28 20:47:23 +03:00
|
|
|
return SharedWorkerGlobalScopeBinding::Wrap(aCx, this, this, options,
|
|
|
|
GetWorkerPrincipal(),
|
|
|
|
true, aReflector);
|
2011-07-17 23:09:13 +04:00
|
|
|
}
|
|
|
|
|
2014-10-27 14:03:00 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_INHERITED(ServiceWorkerGlobalScope, WorkerGlobalScope,
|
2015-04-04 08:46:07 +03:00
|
|
|
mClients, mRegistration)
|
2014-10-27 14:03:00 +03:00
|
|
|
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-14 01:28:01 +04:00
|
|
|
ServiceWorkerGlobalScope::ServiceWorkerGlobalScope(WorkerPrivate* aWorkerPrivate,
|
|
|
|
const nsACString& aScope)
|
|
|
|
: WorkerGlobalScope(aWorkerPrivate),
|
|
|
|
mScope(NS_ConvertUTF8toUTF16(aScope))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-10-27 14:03:00 +03:00
|
|
|
ServiceWorkerGlobalScope::~ServiceWorkerGlobalScope()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2015-01-09 00:56:42 +03:00
|
|
|
bool
|
|
|
|
ServiceWorkerGlobalScope::WrapGlobalObject(JSContext* aCx,
|
|
|
|
JS::MutableHandle<JSObject*> aReflector)
|
2014-05-14 01:28:01 +04:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
|
|
|
|
JS::CompartmentOptions options;
|
|
|
|
mWorkerPrivate->CopyJSCompartmentOptions(options);
|
|
|
|
|
2016-06-28 20:47:23 +03:00
|
|
|
return ServiceWorkerGlobalScopeBinding::Wrap(aCx, this, this, options,
|
|
|
|
GetWorkerPrincipal(),
|
|
|
|
true, aReflector);
|
2014-05-14 01:28:01 +04:00
|
|
|
}
|
|
|
|
|
2014-10-27 14:03:00 +03:00
|
|
|
ServiceWorkerClients*
|
|
|
|
ServiceWorkerGlobalScope::Clients()
|
|
|
|
{
|
|
|
|
if (!mClients) {
|
|
|
|
mClients = new ServiceWorkerClients(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mClients;
|
|
|
|
}
|
|
|
|
|
2016-06-12 17:26:51 +03:00
|
|
|
ServiceWorkerRegistration*
|
2015-04-04 08:46:07 +03:00
|
|
|
ServiceWorkerGlobalScope::Registration()
|
|
|
|
{
|
2015-04-07 21:50:08 +03:00
|
|
|
if (!mRegistration) {
|
2015-04-09 01:22:36 +03:00
|
|
|
mRegistration =
|
2016-06-12 17:26:51 +03:00
|
|
|
ServiceWorkerRegistration::CreateForWorker(mWorkerPrivate, mScope);
|
2015-04-07 21:50:08 +03:00
|
|
|
}
|
2015-04-04 08:46:07 +03:00
|
|
|
|
|
|
|
return mRegistration;
|
|
|
|
}
|
|
|
|
|
2015-06-02 14:12:00 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class SkipWaitingResultRunnable final : public WorkerRunnable
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseProxy;
|
2015-06-02 14:12:00 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
SkipWaitingResultRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
PromiseWorkerProxy* aPromiseProxy)
|
2016-06-28 20:28:13 +03:00
|
|
|
: WorkerRunnable(aWorkerPrivate)
|
2015-06-02 14:12:00 +03:00
|
|
|
, mPromiseProxy(aPromiseProxy)
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate) override
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = mPromiseProxy->WorkerPromise();
|
2016-08-09 12:15:13 +03:00
|
|
|
promise->MaybeResolveWithUndefined();
|
2015-06-02 14:12:00 +03:00
|
|
|
|
|
|
|
// Release the reference on the worker thread.
|
2016-02-29 22:52:42 +03:00
|
|
|
mPromiseProxy->CleanUp();
|
2015-06-02 14:12:00 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class WorkerScopeSkipWaitingRunnable final : public Runnable
|
2015-06-02 14:12:00 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseProxy;
|
2015-06-02 14:12:00 +03:00
|
|
|
nsCString mScope;
|
|
|
|
|
|
|
|
public:
|
|
|
|
WorkerScopeSkipWaitingRunnable(PromiseWorkerProxy* aPromiseProxy,
|
|
|
|
const nsCString& aScope)
|
|
|
|
: mPromiseProxy(aPromiseProxy)
|
|
|
|
, mScope(aScope)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aPromiseProxy);
|
|
|
|
}
|
|
|
|
|
2016-08-08 03:54:47 +03:00
|
|
|
NS_IMETHOD
|
2015-06-02 14:12:00 +03:00
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
2015-06-02 14:12:00 +03:00
|
|
|
MOZ_ASSERT(swm);
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
MutexAutoLock lock(mPromiseProxy->Lock());
|
|
|
|
if (mPromiseProxy->CleanedUp()) {
|
2015-06-02 14:12:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-09-02 20:07:26 +03:00
|
|
|
WorkerPrivate* workerPrivate = mPromiseProxy->GetWorkerPrivate();
|
2015-06-03 17:20:52 +03:00
|
|
|
swm->SetSkipWaitingFlag(workerPrivate->GetPrincipal(), mScope,
|
|
|
|
workerPrivate->ServiceWorkerID());
|
2015-06-02 14:12:00 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<SkipWaitingResultRunnable> runnable =
|
2015-06-02 14:12:00 +03:00
|
|
|
new SkipWaitingResultRunnable(workerPrivate, mPromiseProxy);
|
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
if (!runnable->Dispatch()) {
|
2015-11-26 20:03:10 +03:00
|
|
|
NS_WARNING("Failed to dispatch SkipWaitingResultRunnable to the worker.");
|
|
|
|
}
|
2015-06-02 14:12:00 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-06-02 14:12:00 +03:00
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
ServiceWorkerGlobalScope::SkipWaiting(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mWorkerPrivate->IsServiceWorker());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(this, aRv);
|
2015-06-02 14:12:00 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> promiseProxy =
|
2015-06-02 14:12:00 +03:00
|
|
|
PromiseWorkerProxy::Create(mWorkerPrivate, promise);
|
2015-09-02 20:07:26 +03:00
|
|
|
if (!promiseProxy) {
|
2016-08-09 12:15:13 +03:00
|
|
|
promise->MaybeResolveWithUndefined();
|
2015-06-02 14:12:00 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<WorkerScopeSkipWaitingRunnable> runnable =
|
2015-06-02 14:12:00 +03:00
|
|
|
new WorkerScopeSkipWaitingRunnable(promiseProxy,
|
|
|
|
NS_ConvertUTF16toUTF8(mScope));
|
|
|
|
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(runnable));
|
2015-06-02 14:12:00 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2015-10-25 23:36:27 +03:00
|
|
|
bool
|
|
|
|
ServiceWorkerGlobalScope::OpenWindowEnabled(JSContext* aCx, JSObject* aObj)
|
|
|
|
{
|
|
|
|
WorkerPrivate* worker = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(worker);
|
|
|
|
worker->AssertIsOnWorkerThread();
|
|
|
|
return worker->OpenWindowEnabled();
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
WorkerDebuggerGlobalScope::WorkerDebuggerGlobalScope(
|
|
|
|
WorkerPrivate* aWorkerPrivate)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerDebuggerGlobalScope::~WorkerDebuggerGlobalScope()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:41:17 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(WorkerDebuggerGlobalScope)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(WorkerDebuggerGlobalScope,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mConsole)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(WorkerDebuggerGlobalScope,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mConsole)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN_INHERITED(WorkerDebuggerGlobalScope,
|
|
|
|
DOMEventTargetHelper)
|
|
|
|
tmp->mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
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,
|
2016-03-18 16:04:57 +03:00
|
|
|
JS::MutableHandle<JSObject*> aGlobal,
|
|
|
|
ErrorResult& aRv)
|
2015-03-04 02:51:53 +03:00
|
|
|
{
|
2016-03-18 16:04:57 +03:00
|
|
|
WorkerGlobalScope* scope = mWorkerPrivate->GetOrCreateGlobalScope(aCx);
|
|
|
|
if (!scope) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
aGlobal.set(scope->GetWrapper());
|
2015-03-04 02:51:53 +03:00
|
|
|
}
|
|
|
|
|
2015-03-31 13:22:40 +03:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::CreateSandbox(JSContext* aCx, const nsAString& aName,
|
|
|
|
JS::Handle<JSObject*> aPrototype,
|
2016-03-28 20:28:14 +03:00
|
|
|
JS::MutableHandle<JSObject*> aResult,
|
|
|
|
ErrorResult& aRv)
|
2015-03-31 13:22:40 +03:00
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2016-03-28 20:28:14 +03:00
|
|
|
aResult.set(nullptr);
|
|
|
|
|
2016-04-04 19:29:16 +03:00
|
|
|
JS::Rooted<JS::Value> protoVal(aCx);
|
|
|
|
protoVal.setObjectOrNull(aPrototype);
|
2015-03-31 13:22:40 +03:00
|
|
|
JS::Rooted<JSObject*> sandbox(aCx,
|
2016-04-04 19:29:16 +03:00
|
|
|
SimpleGlobalObject::Create(SimpleGlobalObject::GlobalType::WorkerDebuggerSandbox,
|
|
|
|
protoVal));
|
|
|
|
|
2015-03-31 13:22:40 +03:00
|
|
|
if (!sandbox) {
|
2016-04-04 19:29:16 +03:00
|
|
|
aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
|
2015-03-31 13:22:40 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!JS_WrapObject(aCx, &sandbox)) {
|
2016-03-28 20:28:14 +03:00
|
|
|
aRv.NoteJSContextException(aCx);
|
2015-03-31 13:22:40 +03:00
|
|
|
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);
|
2016-02-29 22:52:42 +03:00
|
|
|
scriptloader::Load(mWorkerPrivate, urls, DebuggerScript, aRv);
|
2015-03-31 13:22:40 +03:00
|
|
|
}
|
|
|
|
|
2015-03-27 09:17:16 +03:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::EnterEventLoop()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->EnterDebuggerEventLoop();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::LeaveEventLoop()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->LeaveDebuggerEventLoop();
|
|
|
|
}
|
|
|
|
|
2015-03-20 14:15:59 +03:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::PostMessage(const nsAString& aMessage)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->PostMessageToDebugger(aMessage);
|
|
|
|
}
|
|
|
|
|
2015-03-30 14:54:38 +03:00
|
|
|
void
|
2016-02-26 23:23:12 +03:00
|
|
|
WorkerDebuggerGlobalScope::SetImmediate(Function& aHandler, ErrorResult& aRv)
|
2015-03-30 14:54:38 +03:00
|
|
|
{
|
2016-02-26 23:23:12 +03:00
|
|
|
mWorkerPrivate->SetDebuggerImmediate(aHandler, aRv);
|
2015-03-30 14:54:38 +03:00
|
|
|
}
|
|
|
|
|
2015-03-26 22:09:45 +03:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::ReportError(JSContext* aCx,
|
|
|
|
const nsAString& aMessage)
|
|
|
|
{
|
2016-03-09 13:20:11 +03:00
|
|
|
JS::AutoFilename chars;
|
2015-03-26 22:09:45 +03:00
|
|
|
uint32_t lineno = 0;
|
2016-01-16 03:26:20 +03:00
|
|
|
JS::DescribeScriptedCaller(aCx, &chars, &lineno);
|
|
|
|
nsString filename(NS_ConvertUTF8toUTF16(chars.get()));
|
2015-03-26 22:09:45 +03:00
|
|
|
mWorkerPrivate->ReportErrorToDebugger(filename, lineno, aMessage);
|
|
|
|
}
|
|
|
|
|
2016-03-24 00:55:07 +03:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::RetrieveConsoleEvents(JSContext* aCx,
|
|
|
|
nsTArray<JS::Value>& aEvents,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
WorkerGlobalScope* scope = mWorkerPrivate->GetOrCreateGlobalScope(aCx);
|
|
|
|
if (!scope) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Console> console = scope->GetConsole(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console->RetrieveConsoleEvents(aCx, aEvents, aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::SetConsoleEventHandler(JSContext* aCx,
|
2016-03-29 17:56:10 +03:00
|
|
|
AnyCallback* aHandler,
|
2016-03-24 00:55:07 +03:00
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
WorkerGlobalScope* scope = mWorkerPrivate->GetOrCreateGlobalScope(aCx);
|
|
|
|
if (!scope) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Console> console = scope->GetConsole(aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console->SetConsoleEventHandler(aHandler);
|
|
|
|
}
|
|
|
|
|
2016-02-11 20:41:17 +03:00
|
|
|
Console*
|
|
|
|
WorkerDebuggerGlobalScope::GetConsole(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
// Debugger console has its own console object.
|
|
|
|
if (!mConsole) {
|
2016-03-24 00:55:07 +03:00
|
|
|
mConsole = Console::Create(nullptr, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2016-02-11 20:41:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return mConsole;
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
void
|
|
|
|
WorkerDebuggerGlobalScope::Dump(JSContext* aCx,
|
|
|
|
const Optional<nsAString>& aString) const
|
|
|
|
{
|
2016-03-18 16:04:57 +03:00
|
|
|
WorkerGlobalScope* scope = mWorkerPrivate->GetOrCreateGlobalScope(aCx);
|
|
|
|
if (scope) {
|
|
|
|
scope->Dump(aString);
|
|
|
|
}
|
2015-03-04 02:51:53 +03:00
|
|
|
}
|
|
|
|
|
2016-06-28 20:47:23 +03:00
|
|
|
BEGIN_WORKERS_NAMESPACE
|
|
|
|
|
2015-03-04 02:51:53 +03:00
|
|
|
bool
|
|
|
|
IsWorkerGlobal(JSObject* object)
|
|
|
|
{
|
2015-06-03 17:20:52 +03:00
|
|
|
nsIGlobalObject* globalObject = nullptr;
|
2016-06-28 20:47:23 +03:00
|
|
|
return NS_SUCCEEDED(UNWRAP_OBJECT(WorkerGlobalScope, object,
|
|
|
|
globalObject)) && !!globalObject;
|
2015-03-04 02:51:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
IsDebuggerGlobal(JSObject* object)
|
|
|
|
{
|
2015-06-03 17:20:52 +03:00
|
|
|
nsIGlobalObject* globalObject = nullptr;
|
2015-03-04 02:51:53 +03:00
|
|
|
return NS_SUCCEEDED(UNWRAP_OBJECT(WorkerDebuggerGlobalScope, object,
|
|
|
|
globalObject)) && !!globalObject;
|
|
|
|
}
|
|
|
|
|
2015-03-31 13:22:40 +03:00
|
|
|
bool
|
|
|
|
IsDebuggerSandbox(JSObject* object)
|
|
|
|
{
|
2016-04-04 19:29:16 +03:00
|
|
|
return SimpleGlobalObject::SimpleGlobalType(object) ==
|
|
|
|
SimpleGlobalObject::GlobalType::WorkerDebuggerSandbox;
|
2015-03-31 13:22:40 +03:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:16:24 +04:00
|
|
|
bool
|
|
|
|
GetterOnlyJSNative(JSContext* aCx, unsigned aArgc, JS::Value* aVp)
|
|
|
|
{
|
2015-02-27 18:08:15 +03:00
|
|
|
JS_ReportErrorNumber(aCx, js::GetErrorMessage, nullptr, JSMSG_GETTER_ONLY);
|
2013-11-05 18:16:24 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-17 23:09:13 +04:00
|
|
|
END_WORKERS_NAMESPACE
|