Bug 1861467 - [wpt-sync] Update web-platform-tests to eedf737ce39c512d0ca3471f988972e...
[gecko.git] / dom / webauthn / WebAuthnManagerBase.cpp
blobee46d7b1b223902063fc25a73a7ce8d0bd006e21
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/WebAuthnManagerBase.h"
8 #include "mozilla/dom/Document.h"
9 #include "mozilla/dom/WebAuthnTransactionChild.h"
10 #include "mozilla/ipc/BackgroundChild.h"
11 #include "mozilla/ipc/PBackgroundChild.h"
12 #include "mozilla/dom/Event.h"
13 #include "nsGlobalWindowInner.h"
14 #include "nsPIWindowRoot.h"
16 namespace mozilla::dom {
18 constexpr auto kDeactivateEvent = u"deactivate"_ns;
19 constexpr auto kVisibilityChange = u"visibilitychange"_ns;
21 WebAuthnManagerBase::WebAuthnManagerBase(nsPIDOMWindowInner* aParent)
22 : mParent(aParent) {
23 MOZ_ASSERT(NS_IsMainThread());
24 MOZ_ASSERT(aParent);
27 WebAuthnManagerBase::~WebAuthnManagerBase() { MOZ_ASSERT(NS_IsMainThread()); }
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebAuthnManagerBase)
30 NS_INTERFACE_MAP_ENTRY(nsISupports)
31 NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
32 NS_INTERFACE_MAP_END
34 NS_IMPL_CYCLE_COLLECTION(WebAuthnManagerBase, mParent)
36 NS_IMPL_CYCLE_COLLECTING_ADDREF(WebAuthnManagerBase)
37 NS_IMPL_CYCLE_COLLECTING_RELEASE(WebAuthnManagerBase)
39 /***********************************************************************
40 * IPC Protocol Implementation
41 **********************************************************************/
43 bool WebAuthnManagerBase::MaybeCreateBackgroundActor() {
44 MOZ_ASSERT(NS_IsMainThread());
46 if (mChild) {
47 return true;
50 ::mozilla::ipc::PBackgroundChild* actorChild =
51 ::mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
52 if (NS_WARN_IF(!actorChild)) {
53 return false;
56 RefPtr<WebAuthnTransactionChild> mgr(new WebAuthnTransactionChild(this));
57 PWebAuthnTransactionChild* constructedMgr =
58 actorChild->SendPWebAuthnTransactionConstructor(mgr);
60 if (NS_WARN_IF(!constructedMgr)) {
61 return false;
64 MOZ_ASSERT(constructedMgr == mgr);
65 mChild = std::move(mgr);
67 return true;
70 void WebAuthnManagerBase::ActorDestroyed() {
71 MOZ_ASSERT(NS_IsMainThread());
72 mChild = nullptr;
75 /***********************************************************************
76 * Event Handling
77 **********************************************************************/
79 void WebAuthnManagerBase::ListenForVisibilityEvents() {
80 MOZ_ASSERT(NS_IsMainThread());
82 nsCOMPtr<nsPIDOMWindowOuter> outer = mParent->GetOuterWindow();
83 if (NS_WARN_IF(!outer)) {
84 return;
87 nsCOMPtr<EventTarget> windowRoot = outer->GetTopWindowRoot();
88 if (NS_WARN_IF(!windowRoot)) {
89 return;
92 nsresult rv = windowRoot->AddEventListener(kDeactivateEvent, this,
93 /* use capture */ true,
94 /* wants untrusted */ false);
95 Unused << NS_WARN_IF(NS_FAILED(rv));
97 rv = windowRoot->AddEventListener(kVisibilityChange, this,
98 /* use capture */ true,
99 /* wants untrusted */ false);
100 Unused << NS_WARN_IF(NS_FAILED(rv));
103 void WebAuthnManagerBase::StopListeningForVisibilityEvents() {
104 MOZ_ASSERT(NS_IsMainThread());
106 nsCOMPtr<nsPIDOMWindowOuter> outer = mParent->GetOuterWindow();
107 if (NS_WARN_IF(!outer)) {
108 return;
111 nsCOMPtr<EventTarget> windowRoot = outer->GetTopWindowRoot();
112 if (NS_WARN_IF(!windowRoot)) {
113 return;
116 windowRoot->RemoveEventListener(kDeactivateEvent, this,
117 /* use capture */ true);
118 windowRoot->RemoveEventListener(kVisibilityChange, this,
119 /* use capture */ true);
122 NS_IMETHODIMP
123 WebAuthnManagerBase::HandleEvent(Event* aEvent) {
124 MOZ_ASSERT(NS_IsMainThread());
125 MOZ_ASSERT(aEvent);
127 nsAutoString type;
128 aEvent->GetType(type);
129 if (!type.Equals(kDeactivateEvent) && !type.Equals(kVisibilityChange)) {
130 return NS_ERROR_FAILURE;
133 // The "deactivate" event on the root window has no
134 // "current inner window" and thus GetTarget() is always null.
135 if (type.Equals(kVisibilityChange)) {
136 nsCOMPtr<Document> doc = do_QueryInterface(aEvent->GetTarget());
137 if (NS_WARN_IF(!doc) || !doc->Hidden()) {
138 return NS_OK;
141 nsGlobalWindowInner* win = nsGlobalWindowInner::Cast(doc->GetInnerWindow());
142 if (NS_WARN_IF(!win) || !win->IsTopInnerWindow()) {
143 return NS_OK;
147 HandleVisibilityChange();
148 return NS_OK;
151 } // namespace mozilla::dom