JSActorManager.h (2940B)
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 7 #ifndef mozilla_dom_JSActorManager_h 8 #define mozilla_dom_JSActorManager_h 9 10 #include "js/TypeDecls.h" 11 #include "mozilla/dom/JSActor.h" 12 #include "mozilla/dom/JSIPCValue.h" 13 #include "nsRefPtrHashtable.h" 14 #include "nsString.h" 15 16 namespace mozilla { 17 class ErrorResult; 18 19 namespace ipc { 20 class IProtocol; 21 } 22 23 namespace dom { 24 25 class JSActorProtocol; 26 class JSActorService; 27 28 class JSActorManager : public nsISupports { 29 public: 30 /** 31 * Get or create an actor by its name. 32 * 33 * Will set an error on |aRv| if the actor fails to be constructed. 34 */ 35 already_AddRefed<JSActor> GetActor(JSContext* aCx, const nsACString& aName, 36 ErrorResult& aRv); 37 38 /** 39 * Look up an existing actor by its name, returning nullptr if it doesn't 40 * already exist. Will not attempt to create the actor. 41 */ 42 already_AddRefed<JSActor> GetExistingActor(const nsACString& aName); 43 44 /** 45 * Handle receiving a raw message from the other side. 46 */ 47 void ReceiveRawMessage(const JSActorMessageMeta& aMetadata, 48 JSIPCValue&& aData, 49 UniquePtr<ipc::StructuredCloneData> aStack); 50 51 virtual const nsACString& GetRemoteType() const = 0; 52 53 protected: 54 /** 55 * The actor is about to be destroyed so prevent it from sending any 56 * more messages. 57 */ 58 void JSActorWillDestroy(); 59 60 /** 61 * Lifecycle method which will fire the `didDestroy` methods on relevant 62 * actors. 63 */ 64 void JSActorDidDestroy(); 65 66 /** 67 * Return the protocol with the given name, if it is supported by the current 68 * actor. 69 */ 70 virtual already_AddRefed<JSActorProtocol> MatchingJSActorProtocol( 71 JSActorService* aActorSvc, const nsACString& aName, ErrorResult& aRv) = 0; 72 73 /** 74 * Initialize a JSActor instance given the constructed JS object. 75 * `aMaybeActor` may be `nullptr`, which should construct the default empty 76 * actor. 77 */ 78 virtual already_AddRefed<JSActor> InitJSActor( 79 JS::Handle<JSObject*> aMaybeActor, const nsACString& aName, 80 ErrorResult& aRv) = 0; 81 82 /** 83 * Return this native actor. This should be the same object which is 84 * implementing `JSActorManager`. 85 */ 86 virtual mozilla::ipc::IProtocol* AsNativeActor() = 0; 87 88 private: 89 friend class JSActorService; 90 91 /** 92 * Note that a particular actor name has been unregistered, and fire the 93 * `didDestroy` method on the actor, if it's been initialized. 94 */ 95 void JSActorUnregister(const nsACString& aName); 96 97 nsRefPtrHashtable<nsCStringHashKey, JSActor> mJSActors; 98 }; 99 100 } // namespace dom 101 } // namespace mozilla 102 103 #endif // mozilla_dom_JSActorManager_h