Line data Source code
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
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "VREventObserver.h"
8 :
9 : #include "nsContentUtils.h"
10 : #include "nsGlobalWindow.h"
11 : #include "VRManagerChild.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : using namespace gfx;
17 :
18 : /**
19 : * This class is used by nsGlobalWindow to implement window.onvrdisplayactivate,
20 : * window.onvrdisplaydeactivate, window.onvrdisplayconnected,
21 : * window.onvrdisplaydisconnected, and window.onvrdisplaypresentchange.
22 : */
23 0 : VREventObserver::VREventObserver(nsGlobalWindow* aGlobalWindow)
24 0 : : mWindow(aGlobalWindow)
25 : {
26 0 : MOZ_ASSERT(aGlobalWindow && aGlobalWindow->IsInnerWindow());
27 :
28 0 : VRManagerChild* vmc = VRManagerChild::Get();
29 0 : if (vmc) {
30 0 : vmc->AddListener(this);
31 : }
32 0 : }
33 :
34 0 : VREventObserver::~VREventObserver()
35 : {
36 0 : DisconnectFromOwner();
37 0 : }
38 :
39 : void
40 0 : VREventObserver::DisconnectFromOwner()
41 : {
42 : // In the event that nsGlobalWindow is deallocated, VREventObserver may
43 : // still be AddRef'ed elsewhere. Ensure that we don't UAF by
44 : // dereferencing mWindow.
45 0 : mWindow = nullptr;
46 :
47 : // Unregister from VRManagerChild
48 0 : if (VRManagerChild::IsCreated()) {
49 0 : VRManagerChild* vmc = VRManagerChild::Get();
50 0 : vmc->RemoveListener(this);
51 : }
52 0 : }
53 :
54 : void
55 0 : VREventObserver::NotifyVRDisplayMounted(uint32_t aDisplayID)
56 : {
57 0 : if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
58 0 : MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
59 0 : mWindow->DispatchVRDisplayActivate(aDisplayID,
60 0 : VRDisplayEventReason::Mounted);
61 : }
62 0 : }
63 :
64 : void
65 0 : VREventObserver::NotifyVRDisplayNavigation(uint32_t aDisplayID)
66 : {
67 0 : if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
68 0 : MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
69 0 : mWindow->DispatchVRDisplayActivate(aDisplayID,
70 0 : VRDisplayEventReason::Navigation);
71 : }
72 0 : }
73 :
74 : void
75 0 : VREventObserver::NotifyVRDisplayRequested(uint32_t aDisplayID)
76 : {
77 0 : if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
78 0 : MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
79 0 : mWindow->DispatchVRDisplayActivate(aDisplayID,
80 0 : VRDisplayEventReason::Requested);
81 : }
82 0 : }
83 :
84 : void
85 0 : VREventObserver::NotifyVRDisplayUnmounted(uint32_t aDisplayID)
86 : {
87 0 : if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
88 0 : MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
89 0 : mWindow->DispatchVRDisplayDeactivate(aDisplayID,
90 0 : VRDisplayEventReason::Unmounted);
91 : }
92 0 : }
93 :
94 : void
95 0 : VREventObserver::NotifyVRDisplayConnect(uint32_t aDisplayID)
96 : {
97 : /**
98 : * We do not call nsGlobalWindow::NotifyActiveVRDisplaysChanged here, as we
99 : * can assume that a newly enumerated display is not presenting WebVR
100 : * content.
101 : */
102 0 : if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
103 0 : MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
104 0 : mWindow->DispatchVRDisplayConnect(aDisplayID);
105 : }
106 0 : }
107 :
108 : void
109 0 : VREventObserver::NotifyVRDisplayDisconnect(uint32_t aDisplayID)
110 : {
111 0 : if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
112 0 : mWindow->NotifyActiveVRDisplaysChanged();
113 0 : MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
114 0 : mWindow->DispatchVRDisplayDisconnect(aDisplayID);
115 : }
116 0 : }
117 :
118 : void
119 0 : VREventObserver::NotifyVRDisplayPresentChange(uint32_t aDisplayID)
120 : {
121 0 : if (mWindow && mWindow->AsInner()->IsCurrentInnerWindow()) {
122 0 : mWindow->NotifyActiveVRDisplaysChanged();
123 0 : MOZ_ASSERT(nsContentUtils::IsSafeToRunScript());
124 0 : mWindow->DispatchVRDisplayPresentChange(aDisplayID);
125 : }
126 0 : }
127 :
128 : } // namespace dom
129 : } // namespace mozilla
|