diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 01:47:29 +0000 |
commit | 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d (patch) | |
tree | a31f07c9bcca9d56ce61e9a1ffd30ef350d513aa /dom/clients/api | |
parent | Initial commit. (diff) | |
download | firefox-esr-upstream/115.8.0esr.tar.xz firefox-esr-upstream/115.8.0esr.zip |
Adding upstream version 115.8.0esr.upstream/115.8.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'dom/clients/api')
-rw-r--r-- | dom/clients/api/Client.cpp | 206 | ||||
-rw-r--r-- | dom/clients/api/Client.h | 89 | ||||
-rw-r--r-- | dom/clients/api/ClientDOMUtil.h | 48 | ||||
-rw-r--r-- | dom/clients/api/Clients.cpp | 292 | ||||
-rw-r--r-- | dom/clients/api/Clients.h | 55 | ||||
-rw-r--r-- | dom/clients/api/moz.build | 25 |
6 files changed, 715 insertions, 0 deletions
diff --git a/dom/clients/api/Client.cpp b/dom/clients/api/Client.cpp new file mode 100644 index 0000000000..1b0b4a2a59 --- /dev/null +++ b/dom/clients/api/Client.cpp @@ -0,0 +1,206 @@ +/* -*- 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 "Client.h" + +#include "ClientDOMUtil.h" +#include "mozilla/dom/ClientHandle.h" +#include "mozilla/dom/ClientIPCTypes.h" +#include "mozilla/dom/ClientManager.h" +#include "mozilla/dom/ClientState.h" +#include "mozilla/dom/DOMMozPromiseRequestHolder.h" +#include "mozilla/dom/MessagePortBinding.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/dom/WorkerScope.h" +#include "nsIDUtils.h" +#include "nsIGlobalObject.h" + +namespace mozilla::dom { + +using mozilla::dom::ipc::StructuredCloneData; + +NS_IMPL_CYCLE_COLLECTING_ADDREF(mozilla::dom::Client); +NS_IMPL_CYCLE_COLLECTING_RELEASE(mozilla::dom::Client); +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(mozilla::dom::Client, mGlobal); + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(mozilla::dom::Client) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +void Client::EnsureHandle() { + NS_ASSERT_OWNINGTHREAD(mozilla::dom::Client); + if (!mHandle) { + mHandle = ClientManager::CreateHandle( + ClientInfo(mData->info()), + mGlobal->EventTargetFor(TaskCategory::Other)); + } +} + +Client::Client(nsIGlobalObject* aGlobal, const ClientInfoAndState& aData) + : mGlobal(aGlobal), mData(MakeUnique<ClientInfoAndState>(aData)) { + MOZ_DIAGNOSTIC_ASSERT(mGlobal); +} + +TimeStamp Client::CreationTime() const { return mData->info().creationTime(); } + +TimeStamp Client::LastFocusTime() const { + if (mData->info().type() != ClientType::Window) { + return TimeStamp(); + } + return mData->state().get_IPCClientWindowState().lastFocusTime(); +} + +StorageAccess Client::GetStorageAccess() const { + ClientState state(ClientState::FromIPC(mData->state())); + return state.GetStorageAccess(); +} + +JSObject* Client::WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) { + if (mData->info().type() == ClientType::Window) { + return WindowClient_Binding::Wrap(aCx, this, aGivenProto); + } + return Client_Binding::Wrap(aCx, this, aGivenProto); +} + +nsIGlobalObject* Client::GetParentObject() const { return mGlobal; } + +void Client::GetUrl(nsAString& aUrlOut) const { + CopyUTF8toUTF16(mData->info().url(), aUrlOut); +} + +void Client::GetId(nsAString& aIdOut) const { + aIdOut = NSID_TrimBracketsUTF16(mData->info().id()); +} + +ClientType Client::Type() const { return mData->info().type(); } + +FrameType Client::GetFrameType() const { return mData->info().frameType(); } + +void Client::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, + const Sequence<JSObject*>& aTransferable, + ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + JS::Rooted<JS::Value> transferable(aCx, JS::UndefinedValue()); + aRv = nsContentUtils::CreateJSValueFromSequenceOfObject(aCx, aTransferable, + &transferable); + if (aRv.Failed()) { + return; + } + + StructuredCloneData data; + data.Write(aCx, aMessage, transferable, JS::CloneDataPolicy(), aRv); + if (aRv.Failed()) { + return; + } + + EnsureHandle(); + mHandle->PostMessage(data, workerPrivate->GetServiceWorkerDescriptor()); +} + +void Client::PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, + const StructuredSerializeOptions& aOptions, + ErrorResult& aRv) { + PostMessage(aCx, aMessage, aOptions.mTransfer, aRv); +} + +VisibilityState Client::GetVisibilityState() const { + return mData->state().get_IPCClientWindowState().visibilityState(); +} + +bool Client::Focused() const { + return mData->state().get_IPCClientWindowState().focused(); +} + +already_AddRefed<Promise> Client::Focus(CallerType aCallerType, + ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + RefPtr<Promise> outerPromise = Promise::Create(mGlobal, aRv); + if (aRv.Failed()) { + return outerPromise.forget(); + } + + if (!workerPrivate->GlobalScope()->WindowInteractionAllowed()) { + outerPromise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR); + return outerPromise.forget(); + } + + EnsureHandle(); + + IPCClientInfo ipcClientInfo(mData->info()); + auto holder = + MakeRefPtr<DOMMozPromiseRequestHolder<ClientStatePromise>>(mGlobal); + + mHandle->Focus(aCallerType) + ->Then( + mGlobal->EventTargetFor(TaskCategory::Other), __func__, + [ipcClientInfo, holder, outerPromise](const ClientState& aResult) { + holder->Complete(); + NS_ENSURE_TRUE_VOID(holder->GetParentObject()); + RefPtr<Client> newClient = + new Client(holder->GetParentObject(), + ClientInfoAndState(ipcClientInfo, aResult.ToIPC())); + outerPromise->MaybeResolve(newClient); + }, + [holder, outerPromise](const CopyableErrorResult& aResult) { + holder->Complete(); + // MaybeReject needs a non-const-ref result, so make a copy. + outerPromise->MaybeReject(CopyableErrorResult(aResult)); + }) + ->Track(*holder); + + return outerPromise.forget(); +} + +already_AddRefed<Promise> Client::Navigate(const nsAString& aURL, + ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + RefPtr<Promise> outerPromise = Promise::Create(mGlobal, aRv); + if (aRv.Failed()) { + return outerPromise.forget(); + } + + ClientNavigateArgs args(mData->info(), NS_ConvertUTF16toUTF8(aURL), + workerPrivate->GetLocationInfo().mHref, + workerPrivate->GetServiceWorkerDescriptor().ToIPC()); + RefPtr<Client> self = this; + + StartClientManagerOp( + &ClientManager::Navigate, args, mGlobal, + [self, outerPromise](const ClientOpResult& aResult) { + if (aResult.type() != ClientOpResult::TClientInfoAndState) { + outerPromise->MaybeResolve(JS::NullHandleValue); + return; + } + RefPtr<Client> newClient = + new Client(self->mGlobal, aResult.get_ClientInfoAndState()); + outerPromise->MaybeResolve(newClient); + }, + [self, outerPromise](const CopyableErrorResult& aResult) { + // MaybeReject needs a non-const-ref result, so make a copy. + outerPromise->MaybeReject(CopyableErrorResult(aResult)); + }); + + return outerPromise.forget(); +} + +} // namespace mozilla::dom diff --git a/dom/clients/api/Client.h b/dom/clients/api/Client.h new file mode 100644 index 0000000000..cd47ccd93b --- /dev/null +++ b/dom/clients/api/Client.h @@ -0,0 +1,89 @@ +/* -*- 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/. */ +#ifndef _mozilla_dom_Client_h +#define _mozilla_dom_Client_h + +#include "X11UndefineNone.h" +#include "mozilla/dom/ClientBinding.h" +#include "mozilla/StorageAccess.h" +#include "nsCOMPtr.h" +#include "nsContentUtils.h" +#include "nsISupports.h" +#include "nsWrapperCache.h" + +class nsIGlobalObject; + +namespace mozilla { + +class ErrorResult; + +namespace dom { + +class ClientHandle; +class ClientInfoAndState; +class Promise; +template <typename t> +class Sequence; +struct StructuredSerializeOptions; + +class Client final : public nsISupports, public nsWrapperCache { + nsCOMPtr<nsIGlobalObject> mGlobal; + UniquePtr<ClientInfoAndState> mData; + RefPtr<ClientHandle> mHandle; + + ~Client() = default; + + void EnsureHandle(); + + public: + Client(nsIGlobalObject* aGlobal, const ClientInfoAndState& aData); + + TimeStamp CreationTime() const; + + TimeStamp LastFocusTime() const; + + StorageAccess GetStorageAccess() const; + + // nsWrapperCache interface methods + JSObject* WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) override; + + // DOM bindings methods + nsIGlobalObject* GetParentObject() const; + + // Client Bindings + void GetUrl(nsAString& aUrlOut) const; + + void GetId(nsAString& aIdOut) const; + + ClientType Type() const; + + FrameType GetFrameType() const; + + // WindowClient bindings + VisibilityState GetVisibilityState() const; + + bool Focused() const; + + already_AddRefed<Promise> Focus(CallerType aCallerType, ErrorResult& aRv); + + already_AddRefed<Promise> Navigate(const nsAString& aURL, ErrorResult& aRv); + + void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, + const Sequence<JSObject*>& aTransferrable, ErrorResult& aRv); + + void PostMessage(JSContext* aCx, JS::Handle<JS::Value> aMessage, + const StructuredSerializeOptions& aOptions, + ErrorResult& aRv); + + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(mozilla::dom::Client) +}; + +} // namespace dom +} // namespace mozilla + +#endif // _mozilla_dom_Client_h diff --git a/dom/clients/api/ClientDOMUtil.h b/dom/clients/api/ClientDOMUtil.h new file mode 100644 index 0000000000..2798aeec53 --- /dev/null +++ b/dom/clients/api/ClientDOMUtil.h @@ -0,0 +1,48 @@ +/* -*- 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/. */ +#ifndef _mozilla_dom_ClientDOMUtil_h +#define _mozilla_dom_ClientDOMUtil_h + +#include "mozilla/dom/ClientIPCTypes.h" +#include "mozilla/dom/ClientOpPromise.h" +#include "mozilla/dom/DOMMozPromiseRequestHolder.h" +#include "mozilla/dom/WorkerPrivate.h" + +class nsIGlobalObject; + +namespace mozilla::dom { + +// Utility method to properly execute a ClientManager operation. It +// will properly hold a worker thread alive and avoid executing callbacks +// if the thread is shutting down. +template <typename Func, typename Arg, typename Resolve, typename Reject> +void StartClientManagerOp(Func aFunc, const Arg& aArg, nsIGlobalObject* aGlobal, + Resolve aResolve, Reject aReject) { + MOZ_DIAGNOSTIC_ASSERT(aGlobal); + + nsCOMPtr<nsISerialEventTarget> target = + aGlobal->EventTargetFor(TaskCategory::Other); + + auto holder = + MakeRefPtr<DOMMozPromiseRequestHolder<ClientOpPromise>>(aGlobal); + + aFunc(aArg, target) + ->Then( + target, __func__, + [aResolve, holder](const ClientOpResult& aResult) { + holder->Complete(); + aResolve(aResult); + }, + [aReject, holder](const CopyableErrorResult& aResult) { + holder->Complete(); + aReject(aResult); + }) + ->Track(*holder); +} + +} // namespace mozilla::dom + +#endif // _mozilla_dom_ClientDOMUtil_h diff --git a/dom/clients/api/Clients.cpp b/dom/clients/api/Clients.cpp new file mode 100644 index 0000000000..6dd88953b3 --- /dev/null +++ b/dom/clients/api/Clients.cpp @@ -0,0 +1,292 @@ +/* -*- 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 "Clients.h" + +#include "Client.h" +#include "ClientDOMUtil.h" +#include "mozilla/dom/ClientIPCTypes.h" +#include "mozilla/dom/ClientManager.h" +#include "mozilla/dom/ClientsBinding.h" +#include "mozilla/dom/Promise.h" +#include "mozilla/dom/ServiceWorkerDescriptor.h" +#include "mozilla/dom/ServiceWorkerManager.h" +#include "mozilla/dom/WorkerScope.h" +#include "mozilla/ipc/BackgroundUtils.h" +#include "mozilla/SchedulerGroup.h" +#include "mozilla/StaticPrefs_privacy.h" +#include "mozilla/StorageAccess.h" +#include "nsIGlobalObject.h" +#include "nsString.h" + +namespace mozilla::dom { + +using mozilla::ipc::CSPInfo; +using mozilla::ipc::PrincipalInfo; + +NS_IMPL_CYCLE_COLLECTING_ADDREF(Clients); +NS_IMPL_CYCLE_COLLECTING_RELEASE(Clients); +NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Clients, mGlobal); + +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Clients) + NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY + NS_INTERFACE_MAP_ENTRY(nsISupports) +NS_INTERFACE_MAP_END + +Clients::Clients(nsIGlobalObject* aGlobal) : mGlobal(aGlobal) { + MOZ_DIAGNOSTIC_ASSERT(mGlobal); +} + +JSObject* Clients::WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) { + return Clients_Binding::Wrap(aCx, this, aGivenProto); +} + +nsIGlobalObject* Clients::GetParentObject() const { return mGlobal; } + +already_AddRefed<Promise> Clients::Get(const nsAString& aClientID, + ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + RefPtr<Promise> outerPromise = Promise::Create(mGlobal, aRv); + if (aRv.Failed()) { + return outerPromise.forget(); + } + + nsID id; + // nsID::Parse accepts both "{...}" and "...", but we only emit the latter, so + // forbid strings that start with "{" to avoid inconsistency and bugs like + // bug 1446225. + if (aClientID.IsEmpty() || aClientID.CharAt(0) == '{' || + !id.Parse(NS_ConvertUTF16toUTF8(aClientID).get())) { + // Invalid ID means we will definitely not find a match, so just + // resolve with undefined indicating "not found". + outerPromise->MaybeResolveWithUndefined(); + return outerPromise.forget(); + } + + const PrincipalInfo& principalInfo = workerPrivate->GetPrincipalInfo(); + nsCOMPtr<nsISerialEventTarget> target = + mGlobal->EventTargetFor(TaskCategory::Other); + + RefPtr<ClientOpPromise> innerPromise = ClientManager::GetInfoAndState( + ClientGetInfoAndStateArgs(id, principalInfo), target); + + nsCString scope = workerPrivate->ServiceWorkerScope(); + auto holder = + MakeRefPtr<DOMMozPromiseRequestHolder<ClientOpPromise>>(mGlobal); + + innerPromise + ->Then( + target, __func__, + [outerPromise, holder, scope](const ClientOpResult& aResult) { + holder->Complete(); + NS_ENSURE_TRUE_VOID(holder->GetParentObject()); + RefPtr<Client> client = new Client( + holder->GetParentObject(), aResult.get_ClientInfoAndState()); + if (client->GetStorageAccess() == StorageAccess::eAllow || + (StaticPrefs::privacy_partition_serviceWorkers() && + ShouldPartitionStorage(client->GetStorageAccess()))) { + outerPromise->MaybeResolve(std::move(client)); + return; + } + nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction( + "Clients::Get() storage denied", [scope] { + ServiceWorkerManager::LocalizeAndReportToAllClients( + scope, "ServiceWorkerGetClientStorageError", + nsTArray<nsString>()); + }); + SchedulerGroup::Dispatch(TaskCategory::Other, r.forget()); + outerPromise->MaybeResolveWithUndefined(); + }, + [outerPromise, holder](const CopyableErrorResult& aResult) { + holder->Complete(); + outerPromise->MaybeResolveWithUndefined(); + }) + ->Track(*holder); + + return outerPromise.forget(); +} + +namespace { + +class MatchAllComparator final { + public: + bool LessThan(Client* aLeft, Client* aRight) const { + TimeStamp leftFocusTime = aLeft->LastFocusTime(); + TimeStamp rightFocusTime = aRight->LastFocusTime(); + // If the focus times are the same, then default to creation order. + // MatchAll should return oldest Clients first. + if (leftFocusTime == rightFocusTime) { + return aLeft->CreationTime() < aRight->CreationTime(); + } + + // Otherwise compare focus times. We reverse the logic here so + // that the most recently focused window is first in the list. + if (!leftFocusTime.IsNull() && rightFocusTime.IsNull()) { + return true; + } + if (leftFocusTime.IsNull() && !rightFocusTime.IsNull()) { + return false; + } + return leftFocusTime > rightFocusTime; + } + + bool Equals(Client* aLeft, Client* aRight) const { + return aLeft->LastFocusTime() == aRight->LastFocusTime() && + aLeft->CreationTime() == aRight->CreationTime(); + } +}; + +} // anonymous namespace + +already_AddRefed<Promise> Clients::MatchAll(const ClientQueryOptions& aOptions, + ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + RefPtr<Promise> outerPromise = Promise::Create(mGlobal, aRv); + if (aRv.Failed()) { + return outerPromise.forget(); + } + + nsCOMPtr<nsIGlobalObject> global = mGlobal; + nsCString scope = workerPrivate->ServiceWorkerScope(); + + ClientMatchAllArgs args(workerPrivate->GetServiceWorkerDescriptor().ToIPC(), + aOptions.mType, aOptions.mIncludeUncontrolled); + StartClientManagerOp( + &ClientManager::MatchAll, args, mGlobal, + [outerPromise, global, scope](const ClientOpResult& aResult) { + nsTArray<RefPtr<Client>> clientList; + bool storageDenied = false; + for (const ClientInfoAndState& value : + aResult.get_ClientList().values()) { + RefPtr<Client> client = new Client(global, value); + if (client->GetStorageAccess() != StorageAccess::eAllow && + (!StaticPrefs::privacy_partition_serviceWorkers() || + !ShouldPartitionStorage(client->GetStorageAccess()))) { + storageDenied = true; + continue; + } + clientList.AppendElement(std::move(client)); + } + if (storageDenied) { + nsCOMPtr<nsIRunnable> r = NS_NewRunnableFunction( + "Clients::MatchAll() storage denied", [scope] { + ServiceWorkerManager::LocalizeAndReportToAllClients( + scope, "ServiceWorkerGetClientStorageError", + nsTArray<nsString>()); + }); + SchedulerGroup::Dispatch(TaskCategory::Other, r.forget()); + } + clientList.Sort(MatchAllComparator()); + outerPromise->MaybeResolve(clientList); + }, + [outerPromise](const CopyableErrorResult& aResult) { + // MaybeReject needs a non-const-ref result, so make a copy. + outerPromise->MaybeReject(CopyableErrorResult(aResult)); + }); + + return outerPromise.forget(); +} + +already_AddRefed<Promise> Clients::OpenWindow(const nsAString& aURL, + ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + RefPtr<Promise> outerPromise = Promise::Create(mGlobal, aRv); + if (aRv.Failed()) { + return outerPromise.forget(); + } + + if (aURL.EqualsLiteral("about:blank")) { + CopyableErrorResult rv; + rv.ThrowTypeError( + "Passing \"about:blank\" to Clients.openWindow is not allowed"); + outerPromise->MaybeReject(std::move(rv)); + return outerPromise.forget(); + } + + if (!workerPrivate->GlobalScope()->WindowInteractionAllowed()) { + outerPromise->MaybeReject(NS_ERROR_DOM_INVALID_ACCESS_ERR); + return outerPromise.forget(); + } + + const PrincipalInfo& principalInfo = workerPrivate->GetPrincipalInfo(); + const CSPInfo& cspInfo = workerPrivate->GetCSPInfo(); + nsCString baseURL = workerPrivate->GetLocationInfo().mHref; + + ClientOpenWindowArgs args(principalInfo, Some(cspInfo), + NS_ConvertUTF16toUTF8(aURL), baseURL); + + nsCOMPtr<nsIGlobalObject> global = mGlobal; + + StartClientManagerOp( + &ClientManager::OpenWindow, args, mGlobal, + [outerPromise, global](const ClientOpResult& aResult) { + if (aResult.type() != ClientOpResult::TClientInfoAndState) { + outerPromise->MaybeResolve(JS::NullHandleValue); + return; + } + RefPtr<Client> client = + new Client(global, aResult.get_ClientInfoAndState()); + outerPromise->MaybeResolve(client); + }, + [outerPromise](const CopyableErrorResult& aResult) { + // MaybeReject needs a non-const-ref result, so make a copy. + outerPromise->MaybeReject(CopyableErrorResult(aResult)); + }); + + return outerPromise.forget(); +} + +already_AddRefed<Promise> Clients::Claim(ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + RefPtr<Promise> outerPromise = Promise::Create(mGlobal, aRv); + if (aRv.Failed()) { + return outerPromise.forget(); + } + + const ServiceWorkerDescriptor& serviceWorker = + workerPrivate->GetServiceWorkerDescriptor(); + + if (serviceWorker.State() != ServiceWorkerState::Activating && + serviceWorker.State() != ServiceWorkerState::Activated) { + aRv.ThrowInvalidStateError("Service worker is not active"); + return outerPromise.forget(); + } + + StartClientManagerOp( + &ClientManager::Claim, ClientClaimArgs(serviceWorker.ToIPC()), mGlobal, + [outerPromise](const ClientOpResult& aResult) { + outerPromise->MaybeResolveWithUndefined(); + }, + [outerPromise](const CopyableErrorResult& aResult) { + // MaybeReject needs a non-const-ref result, so make a copy. + outerPromise->MaybeReject(CopyableErrorResult(aResult)); + }); + + return outerPromise.forget(); +} + +} // namespace mozilla::dom diff --git a/dom/clients/api/Clients.h b/dom/clients/api/Clients.h new file mode 100644 index 0000000000..481b066655 --- /dev/null +++ b/dom/clients/api/Clients.h @@ -0,0 +1,55 @@ +/* -*- 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/. */ +#ifndef _mozilla_dom_Clients_h +#define _mozilla_dom_Clients_h + +#include "nsCOMPtr.h" +#include "nsISupports.h" +#include "nsWrapperCache.h" + +class nsIGlobalObject; + +namespace mozilla { + +class ErrorResult; + +namespace dom { + +struct ClientQueryOptions; +class Promise; + +class Clients final : public nsISupports, public nsWrapperCache { + nsCOMPtr<nsIGlobalObject> mGlobal; + + ~Clients() = default; + + public: + explicit Clients(nsIGlobalObject* aGlobal); + + // nsWrapperCache interface methods + JSObject* WrapObject(JSContext* aCx, + JS::Handle<JSObject*> aGivenProto) override; + + // DOM bindings methods + nsIGlobalObject* GetParentObject() const; + + already_AddRefed<Promise> Get(const nsAString& aClientID, ErrorResult& aRv); + + already_AddRefed<Promise> MatchAll(const ClientQueryOptions& aOptions, + ErrorResult& aRv); + + already_AddRefed<Promise> OpenWindow(const nsAString& aURL, ErrorResult& aRv); + + already_AddRefed<Promise> Claim(ErrorResult& aRv); + + NS_DECL_CYCLE_COLLECTING_ISUPPORTS + NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Clients) +}; + +} // namespace dom +} // namespace mozilla + +#endif // _mozilla_dom_Clients_h diff --git a/dom/clients/api/moz.build b/dom/clients/api/moz.build new file mode 100644 index 0000000000..4e864290fd --- /dev/null +++ b/dom/clients/api/moz.build @@ -0,0 +1,25 @@ +# -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*- +# vim: set filetype=python: +# 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/. + +EXPORTS.mozilla.dom += [ + "Client.h", + "Clients.h", +] + +UNIFIED_SOURCES += [ + "Client.cpp", + "Clients.cpp", +] + +include("/ipc/chromium/chromium-config.mozbuild") + +FINAL_LIBRARY = "xul" + +MOCHITEST_MANIFESTS += [] + +BROWSER_CHROME_MANIFESTS += [] + +XPCSHELL_TESTS_MANIFESTS += [] |