2017-12-06 20:41:58 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/WebAuthnManagerBase.h"
|
|
|
|
#include "mozilla/dom/WebAuthnTransactionChild.h"
|
|
|
|
#include "mozilla/dom/Event.h"
|
2018-03-10 08:43:20 +03:00
|
|
|
#include "nsGlobalWindowInner.h"
|
|
|
|
#include "nsPIWindowRoot.h"
|
2017-12-06 20:41:58 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2018-03-10 08:43:20 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(kDeactivateEvent, "deactivate");
|
2017-12-06 20:41:58 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(kVisibilityChange, "visibilitychange");
|
|
|
|
|
|
|
|
WebAuthnManagerBase::WebAuthnManagerBase(nsPIDOMWindowInner* aParent)
|
|
|
|
: mParent(aParent) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
WebAuthnManagerBase::~WebAuthnManagerBase() { MOZ_ASSERT(NS_IsMainThread()); }
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* IPC Protocol Implementation
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
bool WebAuthnManagerBase::MaybeCreateBackgroundActor() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
if (mChild) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
PBackgroundChild* actorChild = BackgroundChild::GetOrCreateForCurrentThread();
|
|
|
|
if (NS_WARN_IF(!actorChild)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<WebAuthnTransactionChild> mgr(new WebAuthnTransactionChild(this));
|
|
|
|
PWebAuthnTransactionChild* constructedMgr =
|
|
|
|
actorChild->SendPWebAuthnTransactionConstructor(mgr);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!constructedMgr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(constructedMgr == mgr);
|
|
|
|
mChild = mgr.forget();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAuthnManagerBase::ActorDestroyed() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
mChild = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***********************************************************************
|
|
|
|
* Event Handling
|
|
|
|
**********************************************************************/
|
|
|
|
|
|
|
|
void WebAuthnManagerBase::ListenForVisibilityEvents() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-03-10 08:43:20 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> outer = mParent->GetOuterWindow();
|
|
|
|
if (NS_WARN_IF(!outer)) {
|
2017-12-06 20:41:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:43:20 +03:00
|
|
|
nsCOMPtr<EventTarget> windowRoot = outer->GetTopWindowRoot();
|
|
|
|
if (NS_WARN_IF(!windowRoot)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv = windowRoot->AddEventListener(kDeactivateEvent, this,
|
|
|
|
/* use capture */ true,
|
|
|
|
/* wants untrusted */ false);
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
|
|
|
|
rv = windowRoot->AddEventListener(kVisibilityChange, this,
|
|
|
|
/* use capture */ true,
|
|
|
|
/* wants untrusted */ false);
|
2017-12-06 20:41:58 +03:00
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
}
|
|
|
|
|
|
|
|
void WebAuthnManagerBase::StopListeningForVisibilityEvents() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-03-10 08:43:20 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> outer = mParent->GetOuterWindow();
|
|
|
|
if (NS_WARN_IF(!outer)) {
|
2017-12-06 20:41:58 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:43:20 +03:00
|
|
|
nsCOMPtr<EventTarget> windowRoot = outer->GetTopWindowRoot();
|
|
|
|
if (NS_WARN_IF(!windowRoot)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-04-05 20:42:40 +03:00
|
|
|
windowRoot->RemoveEventListener(kDeactivateEvent, this,
|
|
|
|
/* use capture */ true);
|
|
|
|
windowRoot->RemoveEventListener(kVisibilityChange, this,
|
|
|
|
/* use capture */ true);
|
2017-12-06 20:41:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2018-04-20 07:49:29 +03:00
|
|
|
WebAuthnManagerBase::HandleEvent(Event* aEvent) {
|
2017-12-06 20:41:58 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
MOZ_ASSERT(aEvent);
|
|
|
|
|
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
2018-03-10 08:43:20 +03:00
|
|
|
if (!type.Equals(kDeactivateEvent) && !type.Equals(kVisibilityChange)) {
|
2017-12-06 20:41:58 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:43:20 +03:00
|
|
|
// The "deactivate" event on the root window has no
|
|
|
|
// "current inner window" and thus GetTarget() is always null.
|
|
|
|
if (type.Equals(kVisibilityChange)) {
|
2018-04-20 07:49:30 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aEvent->GetTarget());
|
2018-03-10 08:43:20 +03:00
|
|
|
if (NS_WARN_IF(!doc) || !doc->Hidden()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsGlobalWindowInner* win = nsGlobalWindowInner::Cast(doc->GetInnerWindow());
|
|
|
|
if (NS_WARN_IF(!win) || !win->IsTopInnerWindow()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-12-06 20:41:58 +03:00
|
|
|
}
|
|
|
|
|
2018-03-10 08:43:20 +03:00
|
|
|
CancelTransaction(NS_ERROR_DOM_ABORT_ERR);
|
2017-12-06 20:41:58 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|