ClientManager.h (5869B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #ifndef _mozilla_dom_ClientManager_h 7 #define _mozilla_dom_ClientManager_h 8 9 #include "mozilla/dom/ClientOpPromise.h" 10 #include "mozilla/dom/ClientThing.h" 11 #include "mozilla/dom/PClientManagerChild.h" 12 13 class nsIPrincipal; 14 15 namespace mozilla { 16 namespace ipc { 17 class PBackgroundChild; 18 class PrincipalInfo; 19 } // namespace ipc 20 namespace dom { 21 22 class ClientClaimArgs; 23 class ClientGetInfoAndStateArgs; 24 class ClientHandle; 25 class ClientInfo; 26 class ClientManagerChild; 27 class ClientMatchAllArgs; 28 class ClientNavigateArgs; 29 class ClientOpConstructorArgs; 30 class ClientOpenWindowArgs; 31 class ClientSource; 32 enum class ClientType : uint8_t; 33 class WorkerPrivate; 34 35 // The ClientManager provides a per-thread singleton interface workering 36 // with the client subsystem. It allows globals to create ClientSource 37 // objects. It allows other parts of the system to attach to this globals 38 // by creating ClientHandle objects. The ClientManager also provides 39 // methods for querying the list of clients active in the system. 40 class ClientManager final : public ClientThing<ClientManagerChild> { 41 friend class ClientManagerChild; 42 friend class ClientSource; 43 44 ClientManager(); 45 ~ClientManager(); 46 47 // Utility method to trigger a shutdown of the ClientManager. This 48 // is called in various error conditions or when the last reference 49 // is dropped. 50 void Shutdown(); 51 52 UniquePtr<ClientSource> CreateSourceInternal( 53 ClientType aType, nsISerialEventTarget* aEventTarget, 54 const mozilla::ipc::PrincipalInfo& aPrincipal); 55 56 UniquePtr<ClientSource> CreateSourceInternal( 57 const ClientInfo& aClientInfo, nsISerialEventTarget* aEventTarget); 58 59 already_AddRefed<ClientHandle> CreateHandleInternal( 60 const ClientInfo& aClientInfo, nsISerialEventTarget* aSerialEventTarget); 61 62 // Utility method to perform an IPC operation. This will create a 63 // PClientManagerOp actor tied to a MozPromise. The promise will 64 // resolve or reject with the result of the remote operation. 65 [[nodiscard]] RefPtr<ClientOpPromise> StartOp( 66 const ClientOpConstructorArgs& aArgs, 67 nsISerialEventTarget* aSerialEventTarget); 68 69 // Get or create the TLS singleton. Currently this is only used 70 // internally and external code indirectly calls it by invoking 71 // static methods. 72 static already_AddRefed<ClientManager> GetOrCreateForCurrentThread(); 73 74 // Private methods called by ClientSource 75 mozilla::dom::WorkerPrivate* GetWorkerPrivate() const; 76 77 // Don't use - use {Expect,Forget}FutureSource instead. 78 static bool ExpectOrForgetFutureSource( 79 const ClientInfo& aClientInfo, 80 bool (PClientManagerChild::*aMethod)(const IPCClientInfo&)); 81 82 public: 83 // Asynchronously declare that a ClientSource will possibly be constructed 84 // from an equivalent ClientInfo in the future. This must be called before any 85 // any ClientHandles are created with the ClientInfo to avoid race conditions 86 // when ClientHandles query the ClientManagerService. 87 // 88 // This method exists so that the ClientManagerService can determine if a 89 // particular ClientSource can be expected to exist in the future or has 90 // already existed and been destroyed. 91 // 92 // If it's later known that the expected ClientSource will not be 93 // constructed, ForgetFutureSource must be called. 94 static bool ExpectFutureSource(const ClientInfo& aClientInfo); 95 96 // May also be called even when the "future" source has become a "real" 97 // source, in which case this is a no-op. 98 static bool ForgetFutureSource(const ClientInfo& aClientInfo); 99 100 // Initialize the ClientManager at process start. This 101 // does book-keeping like creating a TLS identifier, etc. 102 // This should only be called by process startup code. 103 static void Startup(); 104 105 static UniquePtr<ClientSource> CreateSource( 106 ClientType aType, nsISerialEventTarget* aEventTarget, 107 nsIPrincipal* aPrincipal); 108 109 static UniquePtr<ClientSource> CreateSource( 110 ClientType aType, nsISerialEventTarget* aEventTarget, 111 const mozilla::ipc::PrincipalInfo& aPrincipal); 112 113 // Construct a new ClientSource from an existing ClientInfo (and id) rather 114 // than allocating a new id. 115 static UniquePtr<ClientSource> CreateSourceFromInfo( 116 const ClientInfo& aClientInfo, nsISerialEventTarget* aSerialEventTarget); 117 118 // Allocate a new ClientInfo and id without creating a ClientSource. Used 119 // when we have a redirect that isn't exposed to the process that owns 120 // the global/ClientSource. 121 static Maybe<ClientInfo> CreateInfo(ClientType aType, 122 nsIPrincipal* aPrincipal); 123 124 static already_AddRefed<ClientHandle> CreateHandle( 125 const ClientInfo& aClientInfo, nsISerialEventTarget* aSerialEventTarget); 126 127 static RefPtr<ClientOpPromise> MatchAll(const ClientMatchAllArgs& aArgs, 128 nsISerialEventTarget* aTarget); 129 130 static RefPtr<ClientOpPromise> Claim( 131 const ClientClaimArgs& aArgs, nsISerialEventTarget* aSerialEventTarget); 132 133 static RefPtr<ClientOpPromise> GetInfoAndState( 134 const ClientGetInfoAndStateArgs& aArgs, 135 nsISerialEventTarget* aSerialEventTarget); 136 137 static RefPtr<ClientOpPromise> Navigate( 138 const ClientNavigateArgs& aArgs, 139 nsISerialEventTarget* aSerialEventTarget); 140 141 static RefPtr<ClientOpPromise> OpenWindow( 142 const ClientOpenWindowArgs& aArgs, 143 nsISerialEventTarget* aSerialEventTarget); 144 145 NS_INLINE_DECL_REFCOUNTING(mozilla::dom::ClientManager) 146 }; 147 148 } // namespace dom 149 } // namespace mozilla 150 151 #endif // _mozilla_dom_ClientManager_h