OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/vr/NavigatorVR.h" | 5 #include "modules/vr/NavigatorVR.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptPromiseResolver.h" | 7 #include "bindings/core/v8/ScriptPromiseResolver.h" |
8 #include "core/dom/DOMException.h" | 8 #include "core/dom/DOMException.h" |
9 #include "core/dom/Document.h" | 9 #include "core/dom/Document.h" |
10 #include "core/dom/DocumentUserGestureToken.h" | 10 #include "core/dom/DocumentUserGestureToken.h" |
(...skipping 17 matching lines...) Expand all Loading... |
28 if (!document.frame() || !document.frame()->domWindow()) | 28 if (!document.frame() || !document.frame()->domWindow()) |
29 return 0; | 29 return 0; |
30 Navigator& navigator = *document.frame()->domWindow()->navigator(); | 30 Navigator& navigator = *document.frame()->domWindow()->navigator(); |
31 return &from(navigator); | 31 return &from(navigator); |
32 } | 32 } |
33 | 33 |
34 NavigatorVR& NavigatorVR::from(Navigator& navigator) { | 34 NavigatorVR& NavigatorVR::from(Navigator& navigator) { |
35 NavigatorVR* supplement = static_cast<NavigatorVR*>( | 35 NavigatorVR* supplement = static_cast<NavigatorVR*>( |
36 Supplement<Navigator>::from(navigator, supplementName())); | 36 Supplement<Navigator>::from(navigator, supplementName())); |
37 if (!supplement) { | 37 if (!supplement) { |
38 supplement = new NavigatorVR(navigator.frame()); | 38 supplement = new NavigatorVR(navigator); |
39 provideTo(navigator, supplementName(), supplement); | 39 provideTo(navigator, supplementName(), supplement); |
40 } | 40 } |
41 return *supplement; | 41 return *supplement; |
42 } | 42 } |
43 | 43 |
44 ScriptPromise NavigatorVR::getVRDisplays(ScriptState* scriptState, | 44 ScriptPromise NavigatorVR::getVRDisplays(ScriptState* scriptState, |
45 Navigator& navigator) { | 45 Navigator& navigator) { |
46 return NavigatorVR::from(navigator).getVRDisplays(scriptState); | 46 return NavigatorVR::from(navigator).getVRDisplays(scriptState); |
47 } | 47 } |
48 | 48 |
49 ScriptPromise NavigatorVR::getVRDisplays(ScriptState* scriptState) { | 49 ScriptPromise NavigatorVR::getVRDisplays(ScriptState* scriptState) { |
50 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); | 50 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scriptState); |
51 ScriptPromise promise = resolver->promise(); | 51 ScriptPromise promise = resolver->promise(); |
52 | 52 |
53 Document* document = frame() ? frame()->document() : 0; | 53 if (!document()) { |
54 | |
55 if (!document || !controller()) { | |
56 DOMException* exception = DOMException::create( | 54 DOMException* exception = DOMException::create( |
57 InvalidStateError, "The object is no longer associated to a document."); | 55 InvalidStateError, "The object is no longer associated to a document."); |
58 resolver->reject(exception); | 56 resolver->reject(exception); |
59 return promise; | 57 return promise; |
60 } | 58 } |
61 | 59 |
62 UseCounter::count(*document, UseCounter::VRGetDisplays); | 60 UseCounter::count(*document(), UseCounter::VRGetDisplays); |
63 ExecutionContext* executionContext = scriptState->getExecutionContext(); | 61 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
64 if (!executionContext->isSecureContext()) | 62 if (!executionContext->isSecureContext()) |
65 UseCounter::count(*document, UseCounter::VRGetDisplaysInsecureOrigin); | 63 UseCounter::count(*document(), UseCounter::VRGetDisplaysInsecureOrigin); |
66 | 64 |
67 Platform::current()->recordRapporURL("VR.WebVR.GetDisplays", document->url()); | 65 Platform::current()->recordRapporURL("VR.WebVR.GetDisplays", |
| 66 document()->url()); |
68 | 67 |
69 controller()->getDisplays(resolver); | 68 controller()->getDisplays(resolver); |
70 | 69 |
71 return promise; | 70 return promise; |
72 } | 71 } |
73 | 72 |
74 VRController* NavigatorVR::controller() { | 73 VRController* NavigatorVR::controller() { |
75 if (!frame()) | 74 if (!host()->frame()) |
76 return 0; | 75 return 0; |
77 | 76 |
78 if (!m_controller) { | 77 if (!m_controller) { |
79 m_controller = new VRController(this); | 78 m_controller = new VRController(this); |
80 } | 79 } |
81 | 80 |
82 return m_controller; | 81 return m_controller; |
83 } | 82 } |
84 | 83 |
85 Document* NavigatorVR::document() { | 84 Document* NavigatorVR::document() { |
86 return frame() ? frame()->document() : 0; | 85 return host()->frame() ? host()->frame()->document() : nullptr; |
87 } | 86 } |
88 | 87 |
89 DEFINE_TRACE(NavigatorVR) { | 88 DEFINE_TRACE(NavigatorVR) { |
90 visitor->trace(m_controller); | 89 visitor->trace(m_controller); |
91 | |
92 Supplement<Navigator>::trace(visitor); | 90 Supplement<Navigator>::trace(visitor); |
93 ContextClient::trace(visitor); | |
94 PageVisibilityObserver::trace(visitor); | 91 PageVisibilityObserver::trace(visitor); |
95 } | 92 } |
96 | 93 |
97 NavigatorVR::NavigatorVR(LocalFrame* frame) | 94 NavigatorVR::NavigatorVR(Navigator& navigator) |
98 : ContextClient(frame), PageVisibilityObserver(frame->page()) { | 95 : Supplement<Navigator>(navigator), |
99 frame->domWindow()->registerEventListenerObserver(this); | 96 PageVisibilityObserver(navigator.frame()->page()) { |
| 97 navigator.frame()->domWindow()->registerEventListenerObserver(this); |
100 } | 98 } |
101 | 99 |
102 NavigatorVR::~NavigatorVR() {} | 100 NavigatorVR::~NavigatorVR() {} |
103 | 101 |
104 const char* NavigatorVR::supplementName() { | 102 const char* NavigatorVR::supplementName() { |
105 return "NavigatorVR"; | 103 return "NavigatorVR"; |
106 } | 104 } |
107 | 105 |
108 void NavigatorVR::enqueueVREvent(VRDisplayEvent* event) { | 106 void NavigatorVR::enqueueVREvent(VRDisplayEvent* event) { |
109 // TODO(dcheng): Why does this need to check both frame and domWindow? | 107 if (host()->frame()) { |
110 if (frame() && frame()->domWindow()) { | 108 host()->frame()->domWindow()->enqueueWindowEvent(event); |
111 frame()->domWindow()->enqueueWindowEvent(event); | |
112 } | 109 } |
113 } | 110 } |
114 | 111 |
115 void NavigatorVR::dispatchVRGestureEvent(VRDisplayEvent* event) { | 112 void NavigatorVR::dispatchVRGestureEvent(VRDisplayEvent* event) { |
116 // TODO(dcheng): Why does this need to check both frame and domWindow? | 113 if (host()->frame()) |
117 if (frame() && frame()->domWindow()) { | 114 return; |
118 UserGestureIndicator gestureIndicator( | 115 UserGestureIndicator gestureIndicator( |
119 DocumentUserGestureToken::create(frame()->document())); | 116 DocumentUserGestureToken::create(document())); |
120 event->setTarget(frame()->domWindow()); | 117 LocalDOMWindow* window = host()->frame()->domWindow(); |
121 frame()->domWindow()->dispatchEvent(event); | 118 DCHECK(window); |
122 } | 119 event->setTarget(window); |
| 120 window->dispatchEvent(event); |
123 } | 121 } |
124 | 122 |
125 void NavigatorVR::pageVisibilityChanged() { | 123 void NavigatorVR::pageVisibilityChanged() { |
126 if (!page()) | 124 if (!page()) |
127 return; | 125 return; |
128 if (m_controller) { | 126 if (m_controller) { |
129 m_controller->setListeningForActivate(page()->isPageVisible() && | 127 m_controller->setListeningForActivate(page()->isPageVisible() && |
130 m_listeningForActivate); | 128 m_listeningForActivate); |
131 } | 129 } |
132 } | 130 } |
(...skipping 20 matching lines...) Expand all Loading... |
153 } | 151 } |
154 | 152 |
155 void NavigatorVR::didRemoveAllEventListeners(LocalDOMWindow* window) { | 153 void NavigatorVR::didRemoveAllEventListeners(LocalDOMWindow* window) { |
156 if (m_controller) { | 154 if (m_controller) { |
157 m_controller->setListeningForActivate(false); | 155 m_controller->setListeningForActivate(false); |
158 m_listeningForActivate = false; | 156 m_listeningForActivate = false; |
159 } | 157 } |
160 } | 158 } |
161 | 159 |
162 } // namespace blink | 160 } // namespace blink |
OLD | NEW |