2017-11-09 08:20:00 +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: */
|
|
|
|
/* 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 "ClientHandle.h"
|
|
|
|
|
|
|
|
#include "ClientHandleChild.h"
|
|
|
|
#include "ClientHandleOpChild.h"
|
|
|
|
#include "ClientManager.h"
|
2018-05-17 00:18:36 +03:00
|
|
|
#include "ClientPrincipalUtils.h"
|
2017-12-08 22:46:43 +03:00
|
|
|
#include "ClientState.h"
|
2017-11-09 08:20:00 +03:00
|
|
|
#include "mozilla/dom/PClientManagerChild.h"
|
2017-12-05 22:54:10 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerDescriptor.h"
|
2017-12-08 22:46:43 +03:00
|
|
|
#include "mozilla/dom/ipc/StructuredCloneData.h"
|
2017-11-09 08:20:00 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2017-12-08 22:46:43 +03:00
|
|
|
using mozilla::dom::ipc::StructuredCloneData;
|
|
|
|
|
2017-11-09 08:20:00 +03:00
|
|
|
ClientHandle::~ClientHandle()
|
|
|
|
{
|
|
|
|
Shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClientHandle::Shutdown()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(ClientSource);
|
|
|
|
if (IsShutdown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ShutdownThing();
|
|
|
|
|
|
|
|
mManager = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-05-02 16:29:27 +03:00
|
|
|
void
|
|
|
|
ClientHandle::StartOp(const ClientOpConstructorArgs& aArgs,
|
|
|
|
const ClientOpCallback&& aResolveCallback,
|
|
|
|
const ClientOpCallback&& aRejectCallback)
|
2017-11-09 08:20:00 +03:00
|
|
|
{
|
|
|
|
// Hold a ref to the client until the remote operation completes. Otherwise
|
|
|
|
// the ClientHandle might get de-refed and teardown the actor before we
|
|
|
|
// get an answer.
|
|
|
|
RefPtr<ClientHandle> kungFuGrip = this;
|
|
|
|
|
2018-05-02 16:29:27 +03:00
|
|
|
MaybeExecute([aArgs, kungFuGrip, aRejectCallback,
|
2018-05-30 22:15:35 +03:00
|
|
|
resolve = std::move(aResolveCallback)] (ClientHandleChild* aActor) {
|
2018-06-01 23:36:34 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aActor);
|
2018-05-02 16:29:27 +03:00
|
|
|
ClientHandleOpChild* actor =
|
2018-05-30 22:15:35 +03:00
|
|
|
new ClientHandleOpChild(kungFuGrip, aArgs, std::move(resolve),
|
|
|
|
std::move(aRejectCallback));
|
2017-11-09 08:20:00 +03:00
|
|
|
if (!aActor->SendPClientHandleOpConstructor(actor, aArgs)) {
|
2018-05-02 16:29:27 +03:00
|
|
|
// Constructor failure will call reject callback via ActorDestroy()
|
2017-11-09 08:20:00 +03:00
|
|
|
return;
|
|
|
|
}
|
2018-05-08 18:11:58 +03:00
|
|
|
}, [aRejectCallback] {
|
2018-06-01 23:36:34 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aRejectCallback);
|
2018-05-02 16:29:27 +03:00
|
|
|
aRejectCallback(NS_ERROR_DOM_INVALID_STATE_ERR);
|
2017-11-09 08:20:00 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-05 20:10:20 +03:00
|
|
|
void
|
|
|
|
ClientHandle::OnShutdownThing()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(ClientHandle);
|
|
|
|
if (!mDetachPromise) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mDetachPromise->Resolve(true, __func__);
|
|
|
|
}
|
|
|
|
|
2017-11-09 08:20:00 +03:00
|
|
|
ClientHandle::ClientHandle(ClientManager* aManager,
|
|
|
|
nsISerialEventTarget* aSerialEventTarget,
|
|
|
|
const ClientInfo& aClientInfo)
|
|
|
|
: mManager(aManager)
|
|
|
|
, mSerialEventTarget(aSerialEventTarget)
|
|
|
|
, mClientInfo(aClientInfo)
|
|
|
|
{
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mManager);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mSerialEventTarget);
|
|
|
|
MOZ_ASSERT(mSerialEventTarget->IsOnCurrentThread());
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ClientHandle::Activate(PClientManagerChild* aActor)
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(ClientHandle);
|
|
|
|
|
|
|
|
if (IsShutdown()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
PClientHandleChild* actor =
|
|
|
|
aActor->SendPClientHandleConstructor(mClientInfo.ToIPC());
|
|
|
|
if (!actor) {
|
|
|
|
Shutdown();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ActivateThing(static_cast<ClientHandleChild*>(actor));
|
|
|
|
}
|
|
|
|
|
2017-11-14 22:36:45 +03:00
|
|
|
void
|
|
|
|
ClientHandle::ExecutionReady(const ClientInfo& aClientInfo)
|
|
|
|
{
|
|
|
|
mClientInfo = aClientInfo;
|
|
|
|
}
|
|
|
|
|
2017-11-09 08:20:00 +03:00
|
|
|
const ClientInfo&
|
|
|
|
ClientHandle::Info() const
|
|
|
|
{
|
|
|
|
return mClientInfo;
|
|
|
|
}
|
|
|
|
|
2017-12-05 22:54:10 +03:00
|
|
|
RefPtr<GenericPromise>
|
|
|
|
ClientHandle::Control(const ServiceWorkerDescriptor& aServiceWorker)
|
|
|
|
{
|
|
|
|
RefPtr<GenericPromise::Private> outerPromise =
|
|
|
|
new GenericPromise::Private(__func__);
|
|
|
|
|
2018-05-17 00:18:36 +03:00
|
|
|
// We should never have a cross-origin controller. Since this would be
|
|
|
|
// same-origin policy violation we do a full release assertion here.
|
|
|
|
MOZ_RELEASE_ASSERT(ClientMatchPrincipalInfo(mClientInfo.PrincipalInfo(),
|
|
|
|
aServiceWorker.PrincipalInfo()));
|
|
|
|
|
2018-05-02 16:29:27 +03:00
|
|
|
StartOp(ClientControlledArgs(aServiceWorker.ToIPC()),
|
2017-12-05 22:54:10 +03:00
|
|
|
[outerPromise](const ClientOpResult& aResult) {
|
|
|
|
outerPromise->Resolve(true, __func__);
|
|
|
|
},
|
|
|
|
[outerPromise](const ClientOpResult& aResult) {
|
|
|
|
outerPromise->Reject(aResult.get_nsresult(), __func__);
|
|
|
|
});
|
|
|
|
|
|
|
|
return outerPromise.forget();
|
|
|
|
}
|
|
|
|
|
2017-12-08 22:46:43 +03:00
|
|
|
RefPtr<ClientStatePromise>
|
|
|
|
ClientHandle::Focus()
|
|
|
|
{
|
|
|
|
RefPtr<ClientStatePromise::Private> outerPromise =
|
|
|
|
new ClientStatePromise::Private(__func__);
|
|
|
|
|
2018-05-02 16:29:27 +03:00
|
|
|
StartOp(ClientFocusArgs(),
|
2017-12-08 22:46:43 +03:00
|
|
|
[outerPromise](const ClientOpResult& aResult) {
|
|
|
|
outerPromise->Resolve(ClientState::FromIPC(aResult.get_IPCClientState()), __func__);
|
|
|
|
}, [outerPromise](const ClientOpResult& aResult) {
|
|
|
|
outerPromise->Reject(aResult.get_nsresult(), __func__);
|
|
|
|
});
|
|
|
|
|
|
|
|
RefPtr<ClientStatePromise> ref = outerPromise.get();
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2017-12-08 22:46:43 +03:00
|
|
|
RefPtr<GenericPromise>
|
|
|
|
ClientHandle::PostMessage(StructuredCloneData& aData,
|
|
|
|
const ServiceWorkerDescriptor& aSource)
|
|
|
|
{
|
|
|
|
RefPtr<GenericPromise> ref;
|
|
|
|
|
|
|
|
if (IsShutdown()) {
|
|
|
|
ref = GenericPromise::CreateAndReject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
ClientPostMessageArgs args;
|
|
|
|
args.serviceWorker() = aSource.ToIPC();
|
|
|
|
|
|
|
|
if (!aData.BuildClonedMessageDataForBackgroundChild(GetActor()->Manager()->Manager(),
|
|
|
|
args.clonedData())) {
|
|
|
|
ref = GenericPromise::CreateAndReject(NS_ERROR_DOM_INVALID_STATE_ERR, __func__);
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GenericPromise::Private> outerPromise =
|
|
|
|
new GenericPromise::Private(__func__);
|
|
|
|
|
2018-05-02 16:29:27 +03:00
|
|
|
StartOp(args,
|
2017-12-08 22:46:43 +03:00
|
|
|
[outerPromise](const ClientOpResult& aResult) {
|
|
|
|
outerPromise->Resolve(true, __func__);
|
|
|
|
}, [outerPromise](const ClientOpResult& aResult) {
|
|
|
|
outerPromise->Reject(aResult.get_nsresult(), __func__);
|
|
|
|
});
|
|
|
|
|
|
|
|
ref = outerPromise.get();
|
|
|
|
return ref.forget();
|
|
|
|
}
|
|
|
|
|
2018-01-05 20:10:20 +03:00
|
|
|
RefPtr<GenericPromise>
|
|
|
|
ClientHandle::OnDetach()
|
|
|
|
{
|
|
|
|
NS_ASSERT_OWNINGTHREAD(ClientSource);
|
|
|
|
|
|
|
|
if (!mDetachPromise) {
|
|
|
|
mDetachPromise = new GenericPromise::Private(__func__);
|
|
|
|
if (IsShutdown()) {
|
|
|
|
mDetachPromise->Resolve(true, __func__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<GenericPromise> ref(mDetachPromise);
|
2018-06-01 18:59:07 +03:00
|
|
|
return ref;
|
2018-01-05 20:10:20 +03:00
|
|
|
}
|
|
|
|
|
2017-11-09 08:20:00 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|