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