2014-10-27 11:03:00 +00:00
|
|
|
/* -*- Mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; tab-width: 40 -*- */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
2015-02-20 15:16:17 +00:00
|
|
|
#include "mozilla/dom/Promise.h"
|
|
|
|
|
2014-10-27 11:03:00 +00:00
|
|
|
#include "ServiceWorkerClient.h"
|
|
|
|
#include "ServiceWorkerClients.h"
|
|
|
|
#include "ServiceWorkerManager.h"
|
2015-03-06 13:04:49 +00:00
|
|
|
#include "ServiceWorkerWindowClient.h"
|
2014-10-27 11:03:00 +00:00
|
|
|
|
|
|
|
#include "WorkerPrivate.h"
|
|
|
|
#include "WorkerRunnable.h"
|
|
|
|
#include "WorkerScope.h"
|
|
|
|
|
|
|
|
using namespace mozilla;
|
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::dom::workers;
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ServiceWorkerClients)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ServiceWorkerClients)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(ServiceWorkerClients, mWorkerScope)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ServiceWorkerClients)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
ServiceWorkerClients::ServiceWorkerClients(ServiceWorkerGlobalScope* aWorkerScope)
|
|
|
|
: mWorkerScope(aWorkerScope)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerScope);
|
|
|
|
}
|
|
|
|
|
|
|
|
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 14:13:33 +00:00
|
|
|
ServiceWorkerClients::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2014-10-27 11:03:00 +00:00
|
|
|
{
|
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 14:13:33 +00:00
|
|
|
return ClientsBinding::Wrap(aCx, this, aGivenProto);
|
2014-10-27 11:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Helper class used for passing the promise between threads while
|
|
|
|
// keeping the worker alive.
|
|
|
|
class PromiseHolder MOZ_FINAL : public WorkerFeature
|
|
|
|
{
|
2015-02-20 15:16:17 +00:00
|
|
|
friend class MatchAllRunnable;
|
2014-10-28 09:29:00 +00:00
|
|
|
|
2014-10-27 11:03:00 +00:00
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(PromiseHolder)
|
|
|
|
|
|
|
|
public:
|
|
|
|
PromiseHolder(WorkerPrivate* aWorkerPrivate,
|
|
|
|
Promise* aPromise)
|
|
|
|
: mWorkerPrivate(aWorkerPrivate),
|
|
|
|
mPromise(aPromise),
|
2014-10-28 09:29:00 +00:00
|
|
|
mCleanUpLock("promiseHolderCleanUpLock"),
|
2014-10-27 11:03:00 +00:00
|
|
|
mClean(false)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mWorkerPrivate);
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
MOZ_ASSERT(mPromise);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!mWorkerPrivate->AddFeature(mWorkerPrivate->GetJSContext(), this))) {
|
|
|
|
// Worker has been canceled and will go away.
|
|
|
|
// The ResolvePromiseWorkerRunnable won't run, so we can set mPromise to
|
|
|
|
// nullptr.
|
|
|
|
mPromise = nullptr;
|
|
|
|
mClean = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Promise*
|
2014-10-28 09:29:00 +00:00
|
|
|
GetPromise() const
|
2014-10-27 11:03:00 +00:00
|
|
|
{
|
|
|
|
return mPromise;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Clean()
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2014-10-28 09:29:00 +00:00
|
|
|
MutexAutoLock lock(mCleanUpLock);
|
2014-10-27 11:03:00 +00:00
|
|
|
if (mClean) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mPromise = nullptr;
|
|
|
|
mWorkerPrivate->RemoveFeature(mWorkerPrivate->GetJSContext(), this);
|
|
|
|
mClean = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Notify(JSContext* aCx, Status aStatus)
|
|
|
|
{
|
|
|
|
mWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
if (aStatus > Running) {
|
|
|
|
Clean();
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
~PromiseHolder()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mClean);
|
|
|
|
}
|
|
|
|
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsRefPtr<Promise> mPromise;
|
|
|
|
|
2014-10-28 09:29:00 +00:00
|
|
|
// Used to prevent race conditions on |mClean| and to ensure that either a
|
|
|
|
// Notify() call or a dispatch back to the worker thread occurs before
|
|
|
|
// this object is released.
|
|
|
|
Mutex mCleanUpLock;
|
|
|
|
|
2014-10-27 11:03:00 +00:00
|
|
|
bool mClean;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ResolvePromiseWorkerRunnable MOZ_FINAL : public WorkerRunnable
|
|
|
|
{
|
|
|
|
nsRefPtr<PromiseHolder> mPromiseHolder;
|
2015-03-06 13:04:49 +00:00
|
|
|
nsTArray<ServiceWorkerClientInfo> mValue;
|
2014-10-27 11:03:00 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
ResolvePromiseWorkerRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
PromiseHolder* aPromiseHolder,
|
2015-03-06 13:04:49 +00:00
|
|
|
nsTArray<ServiceWorkerClientInfo>& aValue)
|
2014-10-27 11:03:00 +00:00
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount),
|
2015-03-06 13:04:49 +00:00
|
|
|
mPromiseHolder(aPromiseHolder)
|
2014-10-27 11:03:00 +00:00
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-03-06 13:04:49 +00:00
|
|
|
mValue.SwapElements(aValue);
|
2014-10-27 11:03:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2014-10-28 09:29:00 +00:00
|
|
|
Promise* promise = mPromiseHolder->GetPromise();
|
2014-10-27 11:03:00 +00:00
|
|
|
MOZ_ASSERT(promise);
|
|
|
|
|
|
|
|
nsTArray<nsRefPtr<ServiceWorkerClient>> ret;
|
2015-03-06 13:04:49 +00:00
|
|
|
for (size_t i = 0; i < mValue.Length(); i++) {
|
2014-10-27 11:03:00 +00:00
|
|
|
ret.AppendElement(nsRefPtr<ServiceWorkerClient>(
|
2015-03-06 13:04:49 +00:00
|
|
|
new ServiceWorkerWindowClient(promise->GetParentObject(),
|
|
|
|
mValue.ElementAt(i))));
|
2014-10-27 11:03:00 +00:00
|
|
|
}
|
|
|
|
promise->MaybeResolve(ret);
|
|
|
|
|
|
|
|
// release the reference on the worker thread.
|
|
|
|
mPromiseHolder->Clean();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-10-30 15:13:00 +00:00
|
|
|
class ReleasePromiseRunnable MOZ_FINAL : public MainThreadWorkerControlRunnable
|
|
|
|
{
|
|
|
|
nsRefPtr<PromiseHolder> mPromiseHolder;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ReleasePromiseRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
PromiseHolder* aPromiseHolder)
|
|
|
|
: MainThreadWorkerControlRunnable(aWorkerPrivate),
|
|
|
|
mPromiseHolder(aPromiseHolder)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
private:
|
|
|
|
~ReleasePromiseRunnable()
|
|
|
|
{ }
|
|
|
|
|
|
|
|
bool
|
|
|
|
WorkerRun(JSContext* aCx, WorkerPrivate* aWorkerPrivate)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
|
|
|
mPromiseHolder->Clean();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2015-02-20 15:16:17 +00:00
|
|
|
class MatchAllRunnable MOZ_FINAL : public nsRunnable
|
2014-10-27 11:03:00 +00:00
|
|
|
{
|
|
|
|
WorkerPrivate* mWorkerPrivate;
|
|
|
|
nsRefPtr<PromiseHolder> mPromiseHolder;
|
2014-10-28 09:29:00 +00:00
|
|
|
nsCString mScope;
|
2014-10-27 11:03:00 +00:00
|
|
|
public:
|
2015-02-20 15:16:17 +00:00
|
|
|
MatchAllRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
PromiseHolder* aPromiseHolder,
|
|
|
|
const nsCString& aScope)
|
2014-10-27 11:03:00 +00:00
|
|
|
: mWorkerPrivate(aWorkerPrivate),
|
2014-10-28 09:29:00 +00:00
|
|
|
mPromiseHolder(aPromiseHolder),
|
2014-10-27 11:03:00 +00:00
|
|
|
mScope(aScope)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aWorkerPrivate);
|
|
|
|
aWorkerPrivate->AssertIsOnWorkerThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() MOZ_OVERRIDE
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
|
2014-10-28 09:29:00 +00:00
|
|
|
MutexAutoLock lock(mPromiseHolder->mCleanUpLock);
|
|
|
|
if (mPromiseHolder->mClean) {
|
|
|
|
// Don't resolve the promise if it was already released.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-27 11:03:00 +00:00
|
|
|
nsRefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
2015-03-06 13:04:49 +00:00
|
|
|
nsTArray<ServiceWorkerClientInfo> result;
|
2014-10-27 11:03:00 +00:00
|
|
|
|
2015-02-20 15:16:17 +00:00
|
|
|
swm->GetAllClients(mScope, result);
|
2014-10-27 11:03:00 +00:00
|
|
|
nsRefPtr<ResolvePromiseWorkerRunnable> r =
|
|
|
|
new ResolvePromiseWorkerRunnable(mWorkerPrivate, mPromiseHolder, result);
|
|
|
|
|
|
|
|
AutoSafeJSContext cx;
|
2014-10-30 15:13:00 +00:00
|
|
|
if (r->Dispatch(cx)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Dispatch to worker thread failed because the worker is shutting down.
|
|
|
|
// Use a control runnable to release the runnable on the worker thread.
|
|
|
|
nsRefPtr<ReleasePromiseRunnable> releaseRunnable =
|
|
|
|
new ReleasePromiseRunnable(mWorkerPrivate, mPromiseHolder);
|
|
|
|
|
|
|
|
if (!releaseRunnable->Dispatch(cx)) {
|
|
|
|
NS_RUNTIMEABORT("Failed to dispatch PromiseHolder control runnable.");
|
|
|
|
}
|
|
|
|
|
2014-10-27 11:03:00 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // anonymous namespace
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
2015-02-20 15:16:17 +00:00
|
|
|
ServiceWorkerClients::MatchAll(const ClientQueryOptions& aOptions,
|
|
|
|
ErrorResult& aRv)
|
2014-10-27 11:03:00 +00:00
|
|
|
{
|
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-02-12 23:39:26 +00:00
|
|
|
nsString scope;
|
2014-10-27 11:03:00 +00:00
|
|
|
mWorkerScope->GetScope(scope);
|
|
|
|
|
2015-02-20 15:16:17 +00:00
|
|
|
if (aOptions.mIncludeUncontrolled || aOptions.mType != ClientType::Window) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-27 11:03:00 +00:00
|
|
|
nsRefPtr<Promise> promise = Promise::Create(mWorkerScope, aRv);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-10-28 09:29:00 +00:00
|
|
|
nsRefPtr<PromiseHolder> promiseHolder = new PromiseHolder(workerPrivate,
|
|
|
|
promise);
|
|
|
|
if (!promiseHolder->GetPromise()) {
|
|
|
|
// Don't dispatch if adding the worker feature failed.
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
2015-02-20 15:16:17 +00:00
|
|
|
nsRefPtr<MatchAllRunnable> r =
|
|
|
|
new MatchAllRunnable(workerPrivate,
|
|
|
|
promiseHolder,
|
|
|
|
NS_ConvertUTF16toUTF8(scope));
|
2014-10-27 11:03:00 +00:00
|
|
|
nsresult rv = NS_DispatchToMainThread(r);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
promise->MaybeReject(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return promise.forget();
|
|
|
|
}
|
2015-03-06 13:04:49 +00:00
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
ServiceWorkerClients::OpenWindow(const nsAString& aUrl)
|
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
nsRefPtr<Promise> promise = Promise::Create(mWorkerScope, result);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise->MaybeReject(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<Promise>
|
|
|
|
ServiceWorkerClients::Claim()
|
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
nsRefPtr<Promise> promise = Promise::Create(mWorkerScope, result);
|
|
|
|
if (NS_WARN_IF(result.Failed())) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
promise->MaybeReject(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return promise.forget();
|
|
|
|
}
|