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
)
23 MOZ_ASSERT(NS_IsMainThread());
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
)
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());
50 ::mozilla::ipc::PBackgroundChild
* actorChild
=
51 ::mozilla::ipc::BackgroundChild::GetOrCreateForCurrentThread();
52 if (NS_WARN_IF(!actorChild
)) {
56 RefPtr
<WebAuthnTransactionChild
> mgr(new WebAuthnTransactionChild(this));
57 PWebAuthnTransactionChild
* constructedMgr
=
58 actorChild
->SendPWebAuthnTransactionConstructor(mgr
);
60 if (NS_WARN_IF(!constructedMgr
)) {
64 MOZ_ASSERT(constructedMgr
== mgr
);
65 mChild
= std::move(mgr
);
70 void WebAuthnManagerBase::ActorDestroyed() {
71 MOZ_ASSERT(NS_IsMainThread());
75 /***********************************************************************
77 **********************************************************************/
79 void WebAuthnManagerBase::ListenForVisibilityEvents() {
80 MOZ_ASSERT(NS_IsMainThread());
82 nsCOMPtr
<nsPIDOMWindowOuter
> outer
= mParent
->GetOuterWindow();
83 if (NS_WARN_IF(!outer
)) {
87 nsCOMPtr
<EventTarget
> windowRoot
= outer
->GetTopWindowRoot();
88 if (NS_WARN_IF(!windowRoot
)) {
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
)) {
111 nsCOMPtr
<EventTarget
> windowRoot
= outer
->GetTopWindowRoot();
112 if (NS_WARN_IF(!windowRoot
)) {
116 windowRoot
->RemoveEventListener(kDeactivateEvent
, this,
117 /* use capture */ true);
118 windowRoot
->RemoveEventListener(kVisibilityChange
, this,
119 /* use capture */ true);
123 WebAuthnManagerBase::HandleEvent(Event
* aEvent
) {
124 MOZ_ASSERT(NS_IsMainThread());
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()) {
141 nsGlobalWindowInner
* win
= nsGlobalWindowInner::Cast(doc
->GetInnerWindow());
142 if (NS_WARN_IF(!win
) || !win
->IsTopInnerWindow()) {
147 HandleVisibilityChange();
151 } // namespace mozilla::dom