nsQueryActor.h (3306B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 #ifndef nsQueryActor_h 6 #define nsQueryActor_h 7 8 #include <type_traits> 9 10 #include "mozilla/RefPtr.h" 11 #include "mozilla/dom/Document.h" 12 #include "mozilla/dom/JSActor.h" 13 #include "mozilla/dom/JSActorManager.h" 14 #include "mozilla/dom/ScriptSettings.h" 15 #include "mozilla/dom/WindowGlobalChild.h" 16 #include "nsCOMPtr.h" 17 #include "nsPIDOMWindow.h" 18 19 class nsIDOMProcessChild; 20 class nsIDOMProcessParent; 21 22 // This type is used to get an XPCOM interface implemented by a JSActor from its 23 // native manager. 24 class MOZ_STACK_CLASS nsQueryJSActor final : public nsCOMPtr_helper { 25 public: 26 nsQueryJSActor(const nsLiteralCString aActorName, 27 mozilla::dom::JSActorManager* aManager) 28 : mActorName(aActorName), mManager(aManager) {} 29 30 nsresult NS_FASTCALL operator()(const nsIID& aIID, 31 void** aResult) const override { 32 if (!mManager) { 33 return NS_ERROR_NO_INTERFACE; 34 } 35 36 mozilla::dom::AutoJSAPI jsapi; 37 jsapi.Init(); 38 39 RefPtr<mozilla::dom::JSActor> actor = 40 mManager->GetActor(jsapi.cx(), mActorName, mozilla::IgnoreErrors()); 41 if (!actor) { 42 return NS_ERROR_NO_INTERFACE; 43 } 44 45 return actor->QueryInterfaceActor(aIID, aResult); 46 } 47 48 private: 49 const nsLiteralCString mActorName; 50 mozilla::dom::JSActorManager* mManager; 51 }; 52 53 // Request an XPCOM interface from a JSActor managed by `aManager`. 54 // 55 // These APIs will work with both JSWindowActors and JSProcessActors. 56 template <size_t length> 57 inline nsQueryJSActor do_QueryActor(const char (&aActorName)[length], 58 mozilla::dom::JSActorManager* aManager) { 59 return nsQueryJSActor(nsLiteralCString(aActorName), aManager); 60 } 61 62 // Overload for directly querying a JSWindowActorChild from an inner window. 63 template <size_t length> 64 inline nsQueryJSActor do_QueryActor(const char (&aActorName)[length], 65 nsPIDOMWindowInner* aWindow) { 66 return nsQueryJSActor(nsLiteralCString(aActorName), 67 aWindow ? aWindow->GetWindowGlobalChild() : nullptr); 68 } 69 70 // Overload for directly querying a JSWindowActorChild from a document. 71 template <size_t length> 72 inline nsQueryJSActor do_QueryActor(const char (&aActorName)[length], 73 mozilla::dom::Document* aDoc) { 74 return nsQueryJSActor(nsLiteralCString(aActorName), 75 aDoc ? aDoc->GetWindowGlobalChild() : nullptr); 76 } 77 78 // Overload for directly querying from a nsIDOMProcess{Parent,Child} without 79 // confusing overload selection for types inheriting from both 80 // nsIDOMProcess{Parent,Child} and JSActorManager. 81 template <size_t length, typename T, 82 typename = std::enable_if_t<std::is_same_v<T, nsIDOMProcessParent> || 83 std::is_same_v<T, nsIDOMProcessChild>>> 84 inline nsQueryJSActor do_QueryActor(const char (&aActorName)[length], 85 T* aManager) { 86 return nsQueryJSActor(nsLiteralCString(aActorName), 87 aManager ? aManager->AsJSActorManager() : nullptr); 88 } 89 90 #endif // defined nsQueryActor_h