ClientHandle.h (4315B)
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_ClientHandle_h 7 #define _mozilla_dom_ClientHandle_h 8 9 #include "mozilla/MozPromise.h" 10 #include "mozilla/dom/ClientInfo.h" 11 #include "mozilla/dom/ClientOpPromise.h" 12 #include "mozilla/dom/ClientThing.h" 13 14 #ifdef XP_WIN 15 # undef PostMessage 16 #endif 17 18 namespace mozilla::dom { 19 20 class ClientManager; 21 class ClientHandleChild; 22 class ClientOpConstructorArgs; 23 class PClientManagerChild; 24 class ServiceWorkerDescriptor; 25 enum class CallerType : uint32_t; 26 27 namespace ipc { 28 class StructuredCloneData; 29 } 30 31 // The ClientHandle allows code to take a simple ClientInfo struct and 32 // convert it into a live actor-backed object attached to a particular 33 // ClientSource somewhere in the browser. If the ClientSource is 34 // destroyed then the ClientHandle will simply begin to reject operations. 35 // We do not currently provide a way to be notified when the ClientSource 36 // is destroyed, but this could be added in the future. 37 class ClientHandle final : public ClientThing<ClientHandleChild> { 38 friend class ClientManager; 39 friend class ClientHandleChild; 40 41 RefPtr<ClientManager> mManager; 42 nsCOMPtr<nsISerialEventTarget> mSerialEventTarget; 43 RefPtr<GenericPromise::Private> mDetachPromise; 44 ClientInfo mClientInfo; 45 46 ~ClientHandle(); 47 48 void Shutdown(); 49 50 void StartOp(const ClientOpConstructorArgs& aArgs, 51 const ClientOpCallback&& aResolveCallback, 52 const ClientOpCallback&& aRejectCallback); 53 54 // ClientThing interface 55 void OnShutdownThing() override; 56 57 // Private methods called by ClientHandleChild 58 void ExecutionReady(const ClientInfo& aClientInfo); 59 60 // Private methods called by ClientManager 61 ClientHandle(ClientManager* aManager, 62 nsISerialEventTarget* aSerialEventTarget, 63 const ClientInfo& aClientInfo); 64 65 void Activate(PClientManagerChild* aActor); 66 67 public: 68 const ClientInfo& Info() const; 69 70 // Mark the ClientSource attached to this handle as controlled by the 71 // given service worker. The promise will resolve true if the ClientSource 72 // is successfully marked or reject if the operation could not be completed. 73 RefPtr<GenericErrorResultPromise> Control( 74 const ServiceWorkerDescriptor& aServiceWorker); 75 76 // Focus the Client if possible. If successful the promise will resolve with 77 // a new ClientState snapshot after focus has completed. If focusing fails 78 // for any reason then the promise will reject. 79 RefPtr<ClientStatePromise> Focus(CallerType aCallerType); 80 81 // Send a postMessage() call to the target Client. Currently this only 82 // supports sending from a ServiceWorker source and the MessageEvent is 83 // dispatched to the Client's navigator.serviceWorker event target. The 84 // returned promise will resolve if the MessageEvent is dispatched or if 85 // it triggers an error handled in the Client's context. Other errors 86 // will result in the promise rejecting. 87 RefPtr<GenericErrorResultPromise> PostMessage( 88 ipc::StructuredCloneData& aData, const ServiceWorkerDescriptor& aSource); 89 90 // Return a Promise that resolves when the ClientHandle object is detached 91 // from its remote actors. This will happen if the ClientSource is destroyed 92 // and triggers the cleanup of the handle actors. It will also naturally 93 // happen when the ClientHandle is de-referenced and tears down its own 94 // actors. 95 // 96 // Note: This method can only be called on the ClientHandle owning thread, 97 // but the MozPromise lets you Then() to another thread. 98 RefPtr<GenericPromise> OnDetach(); 99 100 // This is intended to allow the ServiceWorkerManager to evict controlled 101 // clients when their controlling registration changes. This should not be 102 // used by other holders of ClientHandles. This method can probably be removed 103 // when ServiceWorkerManager and ClientManagerService both live on the same 104 // thread. 105 void EvictFromBFCache(); 106 107 NS_INLINE_DECL_REFCOUNTING(ClientHandle); 108 }; 109 110 } // namespace mozilla::dom 111 112 #endif // _mozilla_dom_ClientHandle_h