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: */
|
2015-03-06 16:04:49 +03: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/.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ServiceWorkerWindowClient.h"
|
|
|
|
|
2015-06-11 01:35:18 +03:00
|
|
|
#include "mozilla/Mutex.h"
|
2015-03-06 16:04:49 +03:00
|
|
|
#include "mozilla/dom/ClientBinding.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/dom/Promise.h"
|
2015-04-07 16:25:08 +03:00
|
|
|
#include "mozilla/dom/PromiseWorkerProxy.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "mozilla/UniquePtr.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-07-02 15:54:00 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "WorkerPrivate.h"
|
2016-03-16 21:51:11 +03:00
|
|
|
#include "WorkerScope.h"
|
2015-03-06 16:04:49 +03:00
|
|
|
|
2016-03-16 21:51:11 +03:00
|
|
|
using namespace mozilla;
|
2015-03-06 16:04:49 +03:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
using namespace mozilla::dom::workers;
|
|
|
|
|
2015-07-02 15:54:00 +03:00
|
|
|
using mozilla::UniquePtr;
|
|
|
|
|
2015-03-06 16:04:49 +03: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
|
|
|
ServiceWorkerWindowClient::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2015-03-06 16:04:49 +03: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 17:13:33 +03:00
|
|
|
return WindowClientBinding::Wrap(aCx, this, aGivenProto);
|
2015-03-06 16:04:49 +03:00
|
|
|
}
|
|
|
|
|
2015-04-07 16:25:08 +03:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Passing a null clientInfo will reject the promise with InvalidAccessError.
|
|
|
|
class ResolveOrRejectPromiseRunnable final : public WorkerRunnable
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseProxy;
|
2015-04-07 16:25:08 +03:00
|
|
|
UniquePtr<ServiceWorkerClientInfo> mClientInfo;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ResolveOrRejectPromiseRunnable(WorkerPrivate* aWorkerPrivate,
|
|
|
|
PromiseWorkerProxy* aPromiseProxy,
|
|
|
|
UniquePtr<ServiceWorkerClientInfo>&& aClientInfo)
|
|
|
|
: WorkerRunnable(aWorkerPrivate, WorkerThreadModifyBusyCount)
|
|
|
|
, mPromiseProxy(aPromiseProxy)
|
|
|
|
, mClientInfo(Move(aClientInfo))
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2015-04-07 16:25:08 +03:00
|
|
|
MOZ_ASSERT(promise);
|
|
|
|
|
|
|
|
if (mClientInfo) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ServiceWorkerWindowClient> client =
|
2015-04-07 16:25:08 +03:00
|
|
|
new ServiceWorkerWindowClient(promise->GetParentObject(), *mClientInfo);
|
|
|
|
promise->MaybeResolve(client);
|
|
|
|
} else {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release the reference on the worker thread.
|
2016-02-29 22:52:42 +03:00
|
|
|
mPromiseProxy->CleanUp();
|
2015-04-07 16:25:08 +03:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-04-26 03:23:21 +03:00
|
|
|
class ClientFocusRunnable final : public Runnable
|
2015-04-07 16:25:08 +03:00
|
|
|
{
|
|
|
|
uint64_t mWindowId;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> mPromiseProxy;
|
2015-04-07 16:25:08 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
ClientFocusRunnable(uint64_t aWindowId, PromiseWorkerProxy* aPromiseProxy)
|
|
|
|
: mWindowId(aWindowId)
|
|
|
|
, mPromiseProxy(aPromiseProxy)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mPromiseProxy);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD
|
|
|
|
Run() override
|
|
|
|
{
|
|
|
|
AssertIsOnMainThread();
|
2015-05-20 23:14:49 +03:00
|
|
|
nsGlobalWindow* window = nsGlobalWindow::GetInnerWindowWithId(mWindowId);
|
2015-04-07 16:25:08 +03:00
|
|
|
UniquePtr<ServiceWorkerClientInfo> clientInfo;
|
|
|
|
|
|
|
|
if (window) {
|
2015-08-17 20:38:34 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = window->GetDocument();
|
|
|
|
if (doc) {
|
2016-03-31 17:06:00 +03:00
|
|
|
nsContentUtils::DispatchFocusChromeEvent(window->GetOuterWindow());
|
2015-08-17 20:38:34 +03:00
|
|
|
clientInfo.reset(new ServiceWorkerClientInfo(doc));
|
|
|
|
}
|
2015-04-07 16:25:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
DispatchResult(Move(clientInfo));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void
|
|
|
|
DispatchResult(UniquePtr<ServiceWorkerClientInfo>&& aClientInfo)
|
|
|
|
{
|
2015-06-11 01:35:18 +03:00
|
|
|
AssertIsOnMainThread();
|
2015-09-02 20:07:26 +03:00
|
|
|
MutexAutoLock lock(mPromiseProxy->Lock());
|
|
|
|
if (mPromiseProxy->CleanedUp()) {
|
2015-06-11 01:35:18 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ResolveOrRejectPromiseRunnable> resolveRunnable =
|
2015-09-02 20:07:26 +03:00
|
|
|
new ResolveOrRejectPromiseRunnable(mPromiseProxy->GetWorkerPrivate(),
|
|
|
|
mPromiseProxy, Move(aClientInfo));
|
2015-04-07 16:25:08 +03:00
|
|
|
|
2016-02-26 23:23:12 +03:00
|
|
|
resolveRunnable->Dispatch();
|
2015-04-07 16:25:08 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2015-04-07 16:25:08 +03:00
|
|
|
|
2015-03-06 16:04:49 +03:00
|
|
|
already_AddRefed<Promise>
|
2015-04-07 16:25:08 +03:00
|
|
|
ServiceWorkerWindowClient::Focus(ErrorResult& aRv) const
|
2015-03-06 16:04:49 +03:00
|
|
|
{
|
2015-04-07 16:25:08 +03:00
|
|
|
WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate();
|
|
|
|
MOZ_ASSERT(workerPrivate);
|
|
|
|
workerPrivate->AssertIsOnWorkerThread();
|
|
|
|
|
2015-03-06 16:04:49 +03:00
|
|
|
nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
|
|
|
|
MOZ_ASSERT(global);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Promise> promise = Promise::Create(global, aRv);
|
2015-04-07 16:25:08 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2015-03-06 16:04:49 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2015-07-23 18:30:27 +03:00
|
|
|
if (workerPrivate->GlobalScope()->WindowInteractionAllowed()) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PromiseWorkerProxy> promiseProxy =
|
2015-07-23 18:30:27 +03:00
|
|
|
PromiseWorkerProxy::Create(workerPrivate, promise);
|
2015-09-02 20:07:26 +03:00
|
|
|
if (promiseProxy) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ClientFocusRunnable> r = new ClientFocusRunnable(mWindowId,
|
2015-09-02 20:07:26 +03:00
|
|
|
promiseProxy);
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(r));
|
2015-09-02 20:07:26 +03:00
|
|
|
} else {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_ABORT_ERR);
|
2015-07-23 18:30:27 +03:00
|
|
|
}
|
2015-04-07 16:25:08 +03:00
|
|
|
|
2015-07-23 18:30:27 +03:00
|
|
|
} else {
|
|
|
|
promise->MaybeReject(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2015-04-07 16:25:08 +03:00
|
|
|
}
|
|
|
|
|
2015-03-06 16:04:49 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|