dom/webauthn/WebAuthnManagerBase.cpp
author moz-wptsync-bot <wptsync@mozilla.com>
Wed, 06 Jun 2018 17:50:35 +0000
changeset 1536247 ada68664895bc87c588d998a37aee6af1452d564
parent 1490233 491b4ebcc48a57315d0e19b31e99d8a62f911f5e
child 1723497 2aa30ed48cd8ecbab39658ade7dc17c3e5bd90be
child 1736707 07e9dc784455f2488cb86dcc6dd5a1485aa27923
child 1736737 e9ec0fa94bb7da724347bc118b4071f36235b6fd
child 1736746 43c23af36ee0b2d760ce807be7d3f683c17c89ce
child 1737258 b7358c1fe8005e728e71f34b24aa8d8d45eab568
child 1820836 879997404310931a986a2b73315881bf24126b5a
permissions -rw-r--r--
Bug 1467143 - [wpt-sync] Update web-platform-tests to 75c1b7437dfc0d505520de95df4a96cb16c9afcc, a=testonly wpt-head: 75c1b7437dfc0d505520de95df4a96cb16c9afcc wpt-type: landing

/* -*- 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"
#include "nsGlobalWindowInner.h"
#include "nsPIWindowRoot.h"

namespace mozilla {
namespace dom {

NS_NAMED_LITERAL_STRING(kDeactivateEvent, "deactivate");
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());

  nsCOMPtr<nsPIDOMWindowOuter> outer = mParent->GetOuterWindow();
  if (NS_WARN_IF(!outer)) {
    return;
  }

  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);
  Unused << NS_WARN_IF(NS_FAILED(rv));
}

void
WebAuthnManagerBase::StopListeningForVisibilityEvents()
{
  MOZ_ASSERT(NS_IsMainThread());

  nsCOMPtr<nsPIDOMWindowOuter> outer = mParent->GetOuterWindow();
  if (NS_WARN_IF(!outer)) {
    return;
  }

  nsCOMPtr<EventTarget> windowRoot = outer->GetTopWindowRoot();
  if (NS_WARN_IF(!windowRoot)) {
    return;
  }

  windowRoot->RemoveEventListener(kDeactivateEvent, this,
                                  /* use capture */ true);
  windowRoot->RemoveEventListener(kVisibilityChange, this,
                                  /* use capture */ true);
}

NS_IMETHODIMP
WebAuthnManagerBase::HandleEvent(Event* aEvent)
{
  MOZ_ASSERT(NS_IsMainThread());
  MOZ_ASSERT(aEvent);

  nsAutoString type;
  aEvent->GetType(type);
  if (!type.Equals(kDeactivateEvent) && !type.Equals(kVisibilityChange)) {
    return NS_ERROR_FAILURE;
  }

  // The "deactivate" event on the root window has no
  // "current inner window" and thus GetTarget() is always null.
  if (type.Equals(kVisibilityChange)) {
    nsCOMPtr<nsIDocument> doc = do_QueryInterface(aEvent->GetTarget());
    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;
    }
  }

  CancelTransaction(NS_ERROR_DOM_ABORT_ERR);
  return NS_OK;
}

}
}