From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- dom/clients/api/Client.cpp | 205 ++++++++++++++++++++++++++++ dom/clients/api/Client.h | 89 ++++++++++++ dom/clients/api/ClientDOMUtil.h | 46 +++++++ dom/clients/api/Clients.cpp | 290 ++++++++++++++++++++++++++++++++++++++++ dom/clients/api/Clients.h | 55 ++++++++ dom/clients/api/moz.build | 25 ++++ 6 files changed, 710 insertions(+) create mode 100644 dom/clients/api/Client.cpp create mode 100644 dom/clients/api/Client.h create mode 100644 dom/clients/api/ClientDOMUtil.h create mode 100644 dom/clients/api/Clients.cpp create mode 100644 dom/clients/api/Clients.h create mode 100644 dom/clients/api/moz.build (limited to 'dom/clients/api') diff --git a/dom/clients/api/Client.cpp b/dom/clients/api/Client.cpp new file mode 100644 index 0000000000..c3dd6175de --- /dev/null +++ b/dom/clients/api/Client.cpp @@ -0,0 +1,205 @@ +/* -*- 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->SerialEventTarget()); + } +} + +Client::Client(nsIGlobalObject* aGlobal, const ClientInfoAndState& aData) + : mGlobal(aGlobal), mData(MakeUnique(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 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 aMessage, + const Sequence& aTransferable, + ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + JS::Rooted 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 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 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 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>(mGlobal); + + mHandle->Focus(aCallerType) + ->Then( + mGlobal->SerialEventTarget(), __func__, + [ipcClientInfo, holder, outerPromise](const ClientState& aResult) { + holder->Complete(); + NS_ENSURE_TRUE_VOID(holder->GetParentObject()); + RefPtr 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 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 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 self = this; + + StartClientManagerOp( + &ClientManager::Navigate, args, mGlobal, + [self, outerPromise](const ClientOpResult& aResult) { + if (aResult.type() != ClientOpResult::TClientInfoAndState) { + outerPromise->MaybeResolve(JS::NullHandleValue); + return; + } + RefPtr 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 +class Sequence; +struct StructuredSerializeOptions; + +class Client final : public nsISupports, public nsWrapperCache { + nsCOMPtr mGlobal; + UniquePtr mData; + RefPtr 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 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 Focus(CallerType aCallerType, ErrorResult& aRv); + + already_AddRefed Navigate(const nsAString& aURL, ErrorResult& aRv); + + void PostMessage(JSContext* aCx, JS::Handle aMessage, + const Sequence& aTransferrable, ErrorResult& aRv); + + void PostMessage(JSContext* aCx, JS::Handle 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..a1ad6a49ed --- /dev/null +++ b/dom/clients/api/ClientDOMUtil.h @@ -0,0 +1,46 @@ +/* -*- 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 +void StartClientManagerOp(Func aFunc, const Arg& aArg, nsIGlobalObject* aGlobal, + Resolve aResolve, Reject aReject) { + MOZ_DIAGNOSTIC_ASSERT(aGlobal); + + nsCOMPtr target = aGlobal->SerialEventTarget(); + auto holder = + MakeRefPtr>(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..b08a7fa598 --- /dev/null +++ b/dom/clients/api/Clients.cpp @@ -0,0 +1,290 @@ +/* -*- 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 aGivenProto) { + return Clients_Binding::Wrap(aCx, this, aGivenProto); +} + +nsIGlobalObject* Clients::GetParentObject() const { return mGlobal; } + +already_AddRefed 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 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 target = mGlobal->SerialEventTarget(); + RefPtr innerPromise = ClientManager::GetInfoAndState( + ClientGetInfoAndStateArgs(id, principalInfo), target); + + nsCString scope = workerPrivate->ServiceWorkerScope(); + auto holder = + MakeRefPtr>(mGlobal); + + innerPromise + ->Then( + target, __func__, + [outerPromise, holder, scope](const ClientOpResult& aResult) { + holder->Complete(); + NS_ENSURE_TRUE_VOID(holder->GetParentObject()); + RefPtr 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 r = NS_NewRunnableFunction( + "Clients::Get() storage denied", [scope] { + ServiceWorkerManager::LocalizeAndReportToAllClients( + scope, "ServiceWorkerGetClientStorageError", + nsTArray()); + }); + SchedulerGroup::Dispatch(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 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 outerPromise = Promise::Create(mGlobal, aRv); + if (aRv.Failed()) { + return outerPromise.forget(); + } + + nsCOMPtr 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> clientList; + bool storageDenied = false; + for (const ClientInfoAndState& value : + aResult.get_ClientList().values()) { + RefPtr 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 r = NS_NewRunnableFunction( + "Clients::MatchAll() storage denied", [scope] { + ServiceWorkerManager::LocalizeAndReportToAllClients( + scope, "ServiceWorkerGetClientStorageError", + nsTArray()); + }); + SchedulerGroup::Dispatch(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 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 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 global = mGlobal; + + StartClientManagerOp( + &ClientManager::OpenWindow, args, mGlobal, + [outerPromise, global](const ClientOpResult& aResult) { + if (aResult.type() != ClientOpResult::TClientInfoAndState) { + outerPromise->MaybeResolve(JS::NullHandleValue); + return; + } + RefPtr 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 Clients::Claim(ErrorResult& aRv) { + MOZ_ASSERT(!NS_IsMainThread()); + WorkerPrivate* workerPrivate = GetCurrentThreadWorkerPrivate(); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate); + MOZ_DIAGNOSTIC_ASSERT(workerPrivate->IsServiceWorker()); + workerPrivate->AssertIsOnWorkerThread(); + + RefPtr 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 mGlobal; + + ~Clients() = default; + + public: + explicit Clients(nsIGlobalObject* aGlobal); + + // nsWrapperCache interface methods + JSObject* WrapObject(JSContext* aCx, + JS::Handle aGivenProto) override; + + // DOM bindings methods + nsIGlobalObject* GetParentObject() const; + + already_AddRefed Get(const nsAString& aClientID, ErrorResult& aRv); + + already_AddRefed MatchAll(const ClientQueryOptions& aOptions, + ErrorResult& aRv); + + already_AddRefed OpenWindow(const nsAString& aURL, ErrorResult& aRv); + + already_AddRefed 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 += [] -- cgit v1.2.3