Bug 1927515 - [FxMS Docs] Updates Spotlight UI template documentation in source doc...
[gecko.git] / dom / ipc / jsactor / nsQueryActor.h
blobcbbda7e473e1e02b0b24f4723bd604f1b69ab798
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/. */
5 #ifndef nsQueryActor_h
6 #define nsQueryActor_h
8 #include <type_traits>
10 #include "nsCOMPtr.h"
11 #include "nsPIDOMWindow.h"
12 #include "mozilla/RefPtr.h"
13 #include "mozilla/dom/Document.h"
14 #include "mozilla/dom/JSActor.h"
15 #include "mozilla/dom/JSActorManager.h"
16 #include "mozilla/dom/ScriptSettings.h"
17 #include "mozilla/dom/WindowGlobalChild.h"
19 class nsIDOMProcessChild;
20 class nsIDOMProcessParent;
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) {}
30 nsresult NS_FASTCALL operator()(const nsIID& aIID,
31 void** aResult) const override {
32 if (!mManager) {
33 return NS_ERROR_NO_INTERFACE;
36 mozilla::dom::AutoJSAPI jsapi;
37 jsapi.Init();
39 RefPtr<mozilla::dom::JSActor> actor =
40 mManager->GetActor(jsapi.cx(), mActorName, mozilla::IgnoreErrors());
41 if (!actor) {
42 return NS_ERROR_NO_INTERFACE;
45 return actor->QueryInterfaceActor(aIID, aResult);
48 private:
49 const nsLiteralCString mActorName;
50 mozilla::dom::JSActorManager* mManager;
53 // Request an XPCOM interface from a JSActor managed by `aManager`.
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);
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);
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);
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);
90 #endif // defined nsQueryActor_h