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/VRController.h" | 5 #include "modules/vr/VRController.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/frame/LocalFrame.h" | 10 #include "core/frame/LocalFrame.h" |
11 #include "modules/vr/NavigatorVR.h" | 11 #include "modules/vr/NavigatorVR.h" |
12 #include "modules/vr/VRGetDevicesCallback.h" | 12 #include "modules/vr/VRGetDevicesCallback.h" |
13 #include "public/platform/InterfaceProvider.h" | 13 #include "public/platform/InterfaceProvider.h" |
14 | 14 |
15 #include "wtf/Assertions.h" | 15 #include "wtf/Assertions.h" |
16 | 16 |
17 namespace blink { | 17 namespace blink { |
18 | 18 |
19 VRController::VRController(NavigatorVR* navigatorVR) | 19 VRController::VRController(NavigatorVR* navigatorVR) |
20 : ContextLifecycleObserver(navigatorVR->document()), | 20 : ContextLifecycleObserver(navigatorVR->document()), |
21 m_navigatorVR(navigatorVR), | 21 m_navigatorVR(navigatorVR), |
22 m_displaySynced(false), | 22 m_displaySynced(false), |
23 m_binding(this) { | 23 m_binding(this) { |
24 navigatorVR->document()->frame()->interfaceProvider()->getInterface( | 24 navigatorVR->document()->frame()->interfaceProvider()->getInterface( |
25 mojo::MakeRequest(&m_service)); | 25 mojo::MakeRequest(&m_service)); |
| 26 m_service.set_connection_error_handler(convertToBaseCallback( |
| 27 WTF::bind(&VRController::dispose, wrapWeakPersistent(this)))); |
26 m_service->SetClient( | 28 m_service->SetClient( |
27 m_binding.CreateInterfacePtrAndBind(), | 29 m_binding.CreateInterfacePtrAndBind(), |
28 convertToBaseCallback( | 30 convertToBaseCallback( |
29 WTF::bind(&VRController::onDisplaysSynced, wrapPersistent(this)))); | 31 WTF::bind(&VRController::onDisplaysSynced, wrapPersistent(this)))); |
30 } | 32 } |
31 | 33 |
32 VRController::~VRController() {} | 34 VRController::~VRController() {} |
33 | 35 |
34 void VRController::getDisplays(ScriptPromiseResolver* resolver) { | 36 void VRController::getDisplays(ScriptPromiseResolver* resolver) { |
35 if (!m_service) { | 37 // If we've previously synced the VRDisplays or no longer have a valid service |
36 DOMException* exception = DOMException::create( | 38 // connection just return the current list. In the case of the service being |
37 InvalidStateError, "The service is no longer active."); | 39 // disconnected this will be an empty array. |
38 resolver->reject(exception); | 40 if (!m_service || m_displaySynced) { |
39 return; | |
40 } | |
41 | |
42 // If we've previously synced the VRDisplays just return the current list. | |
43 if (m_displaySynced) { | |
44 resolver->resolve(m_displays); | 41 resolver->resolve(m_displays); |
45 return; | 42 return; |
46 } | 43 } |
47 | 44 |
48 // Otherwise we're still waiting for the full list of displays to be populated | 45 // Otherwise we're still waiting for the full list of displays to be populated |
49 // so queue up the promise for resolution when onDisplaysSynced is called. | 46 // so queue up the promise for resolution when onDisplaysSynced is called. |
50 m_pendingGetDevicesCallbacks.append( | 47 m_pendingGetDevicesCallbacks.append( |
51 WTF::makeUnique<VRGetDevicesCallback>(resolver)); | 48 WTF::makeUnique<VRGetDevicesCallback>(resolver)); |
52 } | 49 } |
53 | 50 |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 | 96 |
100 void VRController::dispose() { | 97 void VRController::dispose() { |
101 // If the document context was destroyed, shut down the client connection | 98 // If the document context was destroyed, shut down the client connection |
102 // and never call the mojo service again. | 99 // and never call the mojo service again. |
103 m_service.reset(); | 100 m_service.reset(); |
104 m_binding.Close(); | 101 m_binding.Close(); |
105 | 102 |
106 // Shutdown all displays' message pipe | 103 // Shutdown all displays' message pipe |
107 for (size_t i = 0; i < m_displays.size(); ++i) | 104 for (size_t i = 0; i < m_displays.size(); ++i) |
108 m_displays[i]->dispose(); | 105 m_displays[i]->dispose(); |
| 106 |
| 107 m_displays.clear(); |
| 108 |
| 109 // Ensure that any outstanding getDisplays promises are resolved. |
| 110 onGetDisplays(); |
109 } | 111 } |
110 | 112 |
111 DEFINE_TRACE(VRController) { | 113 DEFINE_TRACE(VRController) { |
112 visitor->trace(m_navigatorVR); | 114 visitor->trace(m_navigatorVR); |
113 visitor->trace(m_displays); | 115 visitor->trace(m_displays); |
114 | 116 |
115 ContextLifecycleObserver::trace(visitor); | 117 ContextLifecycleObserver::trace(visitor); |
116 } | 118 } |
117 | 119 |
118 } // namespace blink | 120 } // namespace blink |
OLD | NEW |