2016-07-05 01:46:49 +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 "VREventObserver.h"
|
|
|
|
|
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "VRManagerChild.h"
|
|
|
|
|
2017-07-28 13:42:11 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
|
|
|
|
2016-07-05 01:46:49 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
using namespace gfx;
|
|
|
|
|
|
|
|
/**
|
2016-10-19 01:18:10 +03:00
|
|
|
* This class is used by nsGlobalWindow to implement window.onvrdisplayactivate,
|
|
|
|
* window.onvrdisplaydeactivate, window.onvrdisplayconnected,
|
2016-07-05 01:46:49 +03:00
|
|
|
* window.onvrdisplaydisconnected, and window.onvrdisplaypresentchange.
|
|
|
|
*/
|
|
|
|
VREventObserver::VREventObserver(nsGlobalWindow* aGlobalWindow)
|
|
|
|
: mWindow(aGlobalWindow)
|
2017-07-28 13:42:11 +03:00
|
|
|
, mIs2DView(true)
|
2016-07-05 01:46:49 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aGlobalWindow && aGlobalWindow->IsInnerWindow());
|
|
|
|
|
2017-07-28 13:42:11 +03:00
|
|
|
mSpendTimeIn2DView = TimeStamp::Now();
|
2016-07-05 01:46:49 +03:00
|
|
|
VRManagerChild* vmc = VRManagerChild::Get();
|
|
|
|
if (vmc) {
|
|
|
|
vmc->AddListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VREventObserver::~VREventObserver()
|
|
|
|
{
|
2017-02-14 22:21:09 +03:00
|
|
|
DisconnectFromOwner();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VREventObserver::DisconnectFromOwner()
|
|
|
|
{
|
|
|
|
// In the event that nsGlobalWindow is deallocated, VREventObserver may
|
|
|
|
// still be AddRef'ed elsewhere. Ensure that we don't UAF by
|
|
|
|
// dereferencing mWindow.
|
2017-07-28 13:42:11 +03:00
|
|
|
if (mWindow && mIs2DView) {
|
|
|
|
// The WebVR content is closed, and we will collect the telemetry info
|
|
|
|
// for the users who view it in 2D view only.
|
|
|
|
Telemetry::Accumulate(Telemetry::WEBVR_USERS_VIEW_IN, 0);
|
2017-08-08 13:23:43 +03:00
|
|
|
Telemetry::AccumulateTimeDelta(Telemetry::WEBVR_TIME_SPENT_VIEWING_IN_2D,
|
|
|
|
mSpendTimeIn2DView);
|
2017-07-28 13:42:11 +03:00
|
|
|
}
|
2017-02-14 22:21:09 +03:00
|
|
|
mWindow = nullptr;
|
|
|
|
|
|
|
|
// Unregister from VRManagerChild
|
2017-03-03 00:07:24 +03:00
|
|
|
if (VRManagerChild::IsCreated()) {
|
|
|
|
VRManagerChild* vmc = VRManagerChild::Get();
|
2016-07-05 01:46:49 +03:00
|
|
|
vmc->RemoveListener(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-19 01:18:10 +03:00
|
|
|
void
|
|
|
|
VREventObserver::NotifyVRDisplayMounted(uint32_t aDisplayID)
|
|
|
|
{
|
2017-02-14 22:21:09 +03:00
|
|
|
if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
|
2016-10-19 01:18:10 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
|
|
|
|
mWindow->DispatchVRDisplayActivate(aDisplayID,
|
|
|
|
VRDisplayEventReason::Mounted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VREventObserver::NotifyVRDisplayNavigation(uint32_t aDisplayID)
|
|
|
|
{
|
2017-02-14 22:21:09 +03:00
|
|
|
if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
|
2016-10-19 01:18:10 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
|
|
|
|
mWindow->DispatchVRDisplayActivate(aDisplayID,
|
|
|
|
VRDisplayEventReason::Navigation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VREventObserver::NotifyVRDisplayRequested(uint32_t aDisplayID)
|
|
|
|
{
|
2017-02-14 22:21:09 +03:00
|
|
|
if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
|
2016-10-19 01:18:10 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
|
|
|
|
mWindow->DispatchVRDisplayActivate(aDisplayID,
|
|
|
|
VRDisplayEventReason::Requested);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
VREventObserver::NotifyVRDisplayUnmounted(uint32_t aDisplayID)
|
|
|
|
{
|
2017-02-14 22:21:09 +03:00
|
|
|
if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
|
2016-10-19 01:18:10 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
|
|
|
|
mWindow->DispatchVRDisplayDeactivate(aDisplayID,
|
|
|
|
VRDisplayEventReason::Unmounted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-05 01:46:49 +03:00
|
|
|
void
|
2017-02-14 22:21:09 +03:00
|
|
|
VREventObserver::NotifyVRDisplayConnect(uint32_t aDisplayID)
|
2016-07-05 01:46:49 +03:00
|
|
|
{
|
2016-07-05 01:52:21 +03:00
|
|
|
/**
|
|
|
|
* We do not call nsGlobalWindow::NotifyActiveVRDisplaysChanged here, as we
|
|
|
|
* can assume that a newly enumerated display is not presenting WebVR
|
|
|
|
* content.
|
|
|
|
*/
|
2017-02-14 22:21:09 +03:00
|
|
|
if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
|
2016-07-05 01:46:49 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
|
2017-02-14 22:21:09 +03:00
|
|
|
mWindow->DispatchVRDisplayConnect(aDisplayID);
|
2016-07-05 01:46:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-02-14 22:21:09 +03:00
|
|
|
VREventObserver::NotifyVRDisplayDisconnect(uint32_t aDisplayID)
|
2016-07-05 01:46:49 +03:00
|
|
|
{
|
2017-02-14 22:21:09 +03:00
|
|
|
if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
|
2016-07-05 01:52:21 +03:00
|
|
|
mWindow->NotifyActiveVRDisplaysChanged();
|
2016-07-05 01:46:49 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
|
2017-02-14 22:21:09 +03:00
|
|
|
mWindow->DispatchVRDisplayDisconnect(aDisplayID);
|
2016-07-05 01:46:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-02-14 22:21:09 +03:00
|
|
|
VREventObserver::NotifyVRDisplayPresentChange(uint32_t aDisplayID)
|
2016-07-05 01:46:49 +03:00
|
|
|
{
|
2017-07-28 13:42:11 +03:00
|
|
|
// When switching to HMD present mode, it is no longer
|
|
|
|
// to be a 2D view.
|
|
|
|
mIs2DView = false;
|
|
|
|
|
2017-02-14 22:21:09 +03:00
|
|
|
if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
|
2016-07-05 01:52:21 +03:00
|
|
|
mWindow->NotifyActiveVRDisplaysChanged();
|
2016-07-05 01:46:49 +03:00
|
|
|
MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
|
2017-02-14 22:21:09 +03:00
|
|
|
mWindow->DispatchVRDisplayPresentChange(aDisplayID);
|
2016-07-05 01:46:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|