Source code

Revision control

Other Tools

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/. */
6
7
#include "mozilla/dom/WebAuthnManagerBase.h"
8
#include "mozilla/dom/WebAuthnTransactionChild.h"
9
#include "mozilla/dom/Event.h"
10
#include "nsGlobalWindowInner.h"
11
#include "nsPIWindowRoot.h"
12
13
namespace mozilla {
14
namespace dom {
15
16
NS_NAMED_LITERAL_STRING(kDeactivateEvent, "deactivate");
17
NS_NAMED_LITERAL_STRING(kVisibilityChange, "visibilitychange");
18
19
WebAuthnManagerBase::WebAuthnManagerBase(nsPIDOMWindowInner* aParent)
20
: mParent(aParent) {
21
MOZ_ASSERT(NS_IsMainThread());
22
MOZ_ASSERT(aParent);
23
}
24
25
WebAuthnManagerBase::~WebAuthnManagerBase() { MOZ_ASSERT(NS_IsMainThread()); }
26
27
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(WebAuthnManagerBase)
28
NS_INTERFACE_MAP_ENTRY(nsISupports)
29
NS_INTERFACE_MAP_ENTRY(nsIDOMEventListener)
30
NS_INTERFACE_MAP_END
31
32
NS_IMPL_CYCLE_COLLECTION(WebAuthnManagerBase, mParent)
33
34
NS_IMPL_CYCLE_COLLECTING_ADDREF(WebAuthnManagerBase)
35
NS_IMPL_CYCLE_COLLECTING_RELEASE(WebAuthnManagerBase)
36
37
/***********************************************************************
38
* IPC Protocol Implementation
39
**********************************************************************/
40
41
bool WebAuthnManagerBase::MaybeCreateBackgroundActor() {
42
MOZ_ASSERT(NS_IsMainThread());
43
44
if (mChild) {
45
return true;
46
}
47
48
PBackgroundChild* actorChild = BackgroundChild::GetOrCreateForCurrentThread();
49
if (NS_WARN_IF(!actorChild)) {
50
return false;
51
}
52
53
RefPtr<WebAuthnTransactionChild> mgr(new WebAuthnTransactionChild(this));
54
PWebAuthnTransactionChild* constructedMgr =
55
actorChild->SendPWebAuthnTransactionConstructor(mgr);
56
57
if (NS_WARN_IF(!constructedMgr)) {
58
return false;
59
}
60
61
MOZ_ASSERT(constructedMgr == mgr);
62
mChild = mgr.forget();
63
64
return true;
65
}
66
67
void WebAuthnManagerBase::ActorDestroyed() {
68
MOZ_ASSERT(NS_IsMainThread());
69
mChild = nullptr;
70
}
71
72
/***********************************************************************
73
* Event Handling
74
**********************************************************************/
75
76
void WebAuthnManagerBase::ListenForVisibilityEvents() {
77
MOZ_ASSERT(NS_IsMainThread());
78
79
nsCOMPtr<nsPIDOMWindowOuter> outer = mParent->GetOuterWindow();
80
if (NS_WARN_IF(!outer)) {
81
return;
82
}
83
84
nsCOMPtr<EventTarget> windowRoot = outer->GetTopWindowRoot();
85
if (NS_WARN_IF(!windowRoot)) {
86
return;
87
}
88
89
nsresult rv = windowRoot->AddEventListener(kDeactivateEvent, this,
90
/* use capture */ true,
91
/* wants untrusted */ false);
92
Unused << NS_WARN_IF(NS_FAILED(rv));
93
94
rv = windowRoot->AddEventListener(kVisibilityChange, this,
95
/* use capture */ true,
96
/* wants untrusted */ false);
97
Unused << NS_WARN_IF(NS_FAILED(rv));
98
}
99
100
void WebAuthnManagerBase::StopListeningForVisibilityEvents() {
101
MOZ_ASSERT(NS_IsMainThread());
102
103
nsCOMPtr<nsPIDOMWindowOuter> outer = mParent->GetOuterWindow();
104
if (NS_WARN_IF(!outer)) {
105
return;
106
}
107
108
nsCOMPtr<EventTarget> windowRoot = outer->GetTopWindowRoot();
109
if (NS_WARN_IF(!windowRoot)) {
110
return;
111
}
112
113
windowRoot->RemoveEventListener(kDeactivateEvent, this,
114
/* use capture */ true);
115
windowRoot->RemoveEventListener(kVisibilityChange, this,
116
/* use capture */ true);
117
}
118
119
NS_IMETHODIMP
120
WebAuthnManagerBase::HandleEvent(Event* aEvent) {
121
MOZ_ASSERT(NS_IsMainThread());
122
MOZ_ASSERT(aEvent);
123
124
nsAutoString type;
125
aEvent->GetType(type);
126
if (!type.Equals(kDeactivateEvent) && !type.Equals(kVisibilityChange)) {
127
return NS_ERROR_FAILURE;
128
}
129
130
// The "deactivate" event on the root window has no
131
// "current inner window" and thus GetTarget() is always null.
132
if (type.Equals(kVisibilityChange)) {
133
nsCOMPtr<Document> doc = do_QueryInterface(aEvent->GetTarget());
134
if (NS_WARN_IF(!doc) || !doc->Hidden()) {
135
return NS_OK;
136
}
137
138
nsGlobalWindowInner* win = nsGlobalWindowInner::Cast(doc->GetInnerWindow());
139
if (NS_WARN_IF(!win) || !win->IsTopInnerWindow()) {
140
return NS_OK;
141
}
142
}
143
144
HandleVisibilityChange();
145
return NS_OK;
146
}
147
148
} // namespace dom
149
} // namespace mozilla