From 2aa4a82499d4becd2284cdb482213d541b8804dd Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 28 Apr 2024 16:29:10 +0200 Subject: Adding upstream version 86.0.1. Signed-off-by: Daniel Baumann --- dom/workers/sharedworkers/SharedWorkerManager.h | 151 ++++++++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 dom/workers/sharedworkers/SharedWorkerManager.h (limited to 'dom/workers/sharedworkers/SharedWorkerManager.h') diff --git a/dom/workers/sharedworkers/SharedWorkerManager.h b/dom/workers/sharedworkers/SharedWorkerManager.h new file mode 100644 index 0000000000..fab4f3d90d --- /dev/null +++ b/dom/workers/sharedworkers/SharedWorkerManager.h @@ -0,0 +1,151 @@ +/* -*- 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_SharedWorkerManager_h +#define mozilla_dom_SharedWorkerManager_h + +#include "SharedWorkerParent.h" +#include "mozilla/dom/RemoteWorkerController.h" +#include "mozilla/dom/quota/CheckedUnsafePtr.h" +#include "nsISupportsImpl.h" +#include "nsTArray.h" + +class nsIPrincipal; + +namespace mozilla { +namespace dom { + +class UniqueMessagePortId; +class RemoteWorkerData; +class SharedWorkerManager; +class SharedWorkerService; + +// Main-thread only object that keeps a manager and the service alive. +// When the last SharedWorkerManagerHolder is released, the corresponding +// manager unregisters itself from the service and terminates the worker. +class SharedWorkerManagerHolder final + : public SupportsCheckedUnsafePtr> { + public: + NS_INLINE_DECL_REFCOUNTING(SharedWorkerManagerHolder); + + SharedWorkerManagerHolder(SharedWorkerManager* aManager, + SharedWorkerService* aService); + + SharedWorkerManager* Manager() const { return mManager; } + + SharedWorkerService* Service() const { return mService; } + + private: + ~SharedWorkerManagerHolder(); + + const RefPtr mManager; + const RefPtr mService; +}; + +// Thread-safe wrapper for SharedWorkerManagerHolder. +class SharedWorkerManagerWrapper final { + public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedWorkerManagerWrapper); + + explicit SharedWorkerManagerWrapper( + already_AddRefed aHolder); + + SharedWorkerManager* Manager() const { return mHolder->Manager(); } + + private: + ~SharedWorkerManagerWrapper(); + + RefPtr mHolder; +}; + +class SharedWorkerManager final : public RemoteWorkerObserver { + public: + NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SharedWorkerManager, override); + + // Called on main-thread thread methods + + static already_AddRefed Create( + SharedWorkerService* aService, nsIEventTarget* aPBackgroundEventTarget, + const RemoteWorkerData& aData, nsIPrincipal* aLoadingPrincipal, + const OriginAttributes& aEffectiveStoragePrincipalAttrs); + + // Returns a holder if this manager matches. The holder blocks the shutdown of + // the manager. + already_AddRefed MatchOnMainThread( + SharedWorkerService* aService, const nsACString& aDomain, + nsIURI* aScriptURL, const nsAString& aName, + nsIPrincipal* aLoadingPrincipal, + const OriginAttributes& aEffectiveStoragePrincipalAttrs); + + // RemoteWorkerObserver + + void CreationFailed() override; + + void CreationSucceeded() override; + + void ErrorReceived(const ErrorValue& aValue) override; + + void Terminated() override; + + // Called on PBackground thread methods + + bool MaybeCreateRemoteWorker(const RemoteWorkerData& aData, + uint64_t aWindowID, + UniqueMessagePortId& aPortIdentifier, + base::ProcessId aProcessId); + + void AddActor(SharedWorkerParent* aParent); + + void RemoveActor(SharedWorkerParent* aParent); + + void UpdateSuspend(); + + void UpdateFrozen(); + + bool IsSecureContext() const; + + void Terminate(); + + // Called on main-thread only. + + void RegisterHolder(SharedWorkerManagerHolder* aHolder); + + void UnregisterHolder(SharedWorkerManagerHolder* aHolder); + + private: + SharedWorkerManager(nsIEventTarget* aPBackgroundEventTarget, + const RemoteWorkerData& aData, + nsIPrincipal* aLoadingPrincipal, + const OriginAttributes& aEffectiveStoragePrincipalAttrs); + + ~SharedWorkerManager(); + + nsCOMPtr mPBackgroundEventTarget; + + nsCOMPtr mLoadingPrincipal; + const nsCString mDomain; + const OriginAttributes mEffectiveStoragePrincipalAttrs; + const nsCOMPtr mResolvedScriptURL; + const nsString mName; + const bool mIsSecureContext; + bool mSuspended; + bool mFrozen; + + // Raw pointers because SharedWorkerParent unregisters itself in + // ActorDestroy(). + nsTArray> mActors; + + RefPtr mRemoteWorkerController; + + // Main-thread only. Raw Pointers because holders keep the manager alive and + // they unregister themselves in their DTOR. + nsTArray> mHolders; +}; + +} // namespace dom +} // namespace mozilla + +#endif // mozilla_dom_SharedWorkerManager_h -- cgit v1.2.3