| 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 "core/dom/DOMException.h" | 7 #include "core/dom/DOMException.h" |
| 8 #include "core/dom/Document.h" | 8 #include "core/dom/Document.h" |
| 9 #include "core/frame/LocalFrame.h" | 9 #include "core/frame/LocalFrame.h" |
| 10 #include "modules/vr/NavigatorVR.h" | 10 #include "modules/vr/NavigatorVR.h" |
| 11 #include "modules/vr/VRGetDevicesCallback.h" | 11 #include "modules/vr/VRGetDevicesCallback.h" |
| 12 #include "public/platform/InterfaceProvider.h" | 12 #include "public/platform/InterfaceProvider.h" |
| 13 | 13 |
| 14 #include "wtf/Assertions.h" | 14 #include "wtf/Assertions.h" |
| 15 | 15 |
| 16 namespace blink { | 16 namespace blink { |
| 17 | 17 |
| 18 VRController::VRController(NavigatorVR* navigatorVR) | 18 VRController::VRController(NavigatorVR* navigatorVR) |
| 19 : ContextLifecycleObserver(navigatorVR->document()), | 19 : ContextLifecycleObserver(navigatorVR->document()), |
| 20 m_navigatorVR(navigatorVR), | 20 m_navigatorVR(navigatorVR) { |
| 21 m_binding(this) { | |
| 22 navigatorVR->document()->frame()->interfaceProvider()->getInterface( | 21 navigatorVR->document()->frame()->interfaceProvider()->getInterface( |
| 23 mojo::GetProxy(&m_service)); | 22 mojo::GetProxy(&m_service)); |
| 24 m_service->SetClient(m_binding.CreateInterfacePtrAndBind()); | |
| 25 } | 23 } |
| 26 | 24 |
| 27 VRController::~VRController() {} | 25 VRController::~VRController() {} |
| 28 | 26 |
| 29 void VRController::getDisplays(ScriptPromiseResolver* resolver) { | 27 void VRController::getDisplays(ScriptPromiseResolver* resolver) { |
| 30 if (!m_service) { | 28 if (!m_service) { |
| 31 DOMException* exception = DOMException::create( | 29 DOMException* exception = DOMException::create( |
| 32 InvalidStateError, "The service is no longer active."); | 30 InvalidStateError, "The service is no longer active."); |
| 33 resolver->reject(exception); | 31 resolver->reject(exception); |
| 34 return; | 32 return; |
| 35 } | 33 } |
| 36 | 34 |
| 37 m_pendingGetDevicesCallbacks.append( | 35 m_pendingGetDevicesCallbacks.append( |
| 38 WTF::wrapUnique(new VRGetDevicesCallback(resolver))); | 36 WTF::wrapUnique(new VRGetDevicesCallback(resolver))); |
| 39 m_service->GetDisplays(convertToBaseCallback( | 37 m_service->GetDisplays(convertToBaseCallback( |
| 40 WTF::bind(&VRController::onGetDisplays, wrapPersistent(this)))); | 38 WTF::bind(&VRController::onGetDisplays, wrapPersistent(this)))); |
| 41 } | 39 } |
| 42 | 40 |
| 43 device::blink::VRPosePtr VRController::getPose(unsigned index) { | |
| 44 if (!m_service) | |
| 45 return nullptr; | |
| 46 | |
| 47 device::blink::VRPosePtr pose; | |
| 48 m_service->GetPose(index, &pose); | |
| 49 return pose; | |
| 50 } | |
| 51 | |
| 52 void VRController::resetPose(unsigned index) { | |
| 53 if (!m_service) | |
| 54 return; | |
| 55 | |
| 56 m_service->ResetPose(index); | |
| 57 } | |
| 58 | |
| 59 void VRController::requestPresent(ScriptPromiseResolver* resolver, | |
| 60 unsigned index, | |
| 61 bool secureOrigin) { | |
| 62 if (!m_service) { | |
| 63 DOMException* exception = DOMException::create( | |
| 64 InvalidStateError, "The service is no longer active."); | |
| 65 resolver->reject(exception); | |
| 66 return; | |
| 67 } | |
| 68 | |
| 69 m_service->RequestPresent( | |
| 70 index, secureOrigin, | |
| 71 convertToBaseCallback(WTF::bind(&VRController::onPresentComplete, | |
| 72 wrapPersistent(this), | |
| 73 wrapPersistent(resolver), index))); | |
| 74 } | |
| 75 | |
| 76 void VRController::exitPresent(unsigned index) { | |
| 77 if (!m_service) | |
| 78 return; | |
| 79 | |
| 80 m_service->ExitPresent(index); | |
| 81 } | |
| 82 | |
| 83 void VRController::submitFrame(unsigned index, device::blink::VRPosePtr pose) { | |
| 84 if (!m_service) | |
| 85 return; | |
| 86 | |
| 87 m_service->SubmitFrame(index, std::move(pose)); | |
| 88 } | |
| 89 | |
| 90 void VRController::updateLayerBounds( | |
| 91 unsigned index, | |
| 92 device::blink::VRLayerBoundsPtr leftBounds, | |
| 93 device::blink::VRLayerBoundsPtr rightBounds) { | |
| 94 if (!m_service) | |
| 95 return; | |
| 96 | |
| 97 m_service->UpdateLayerBounds(index, std::move(leftBounds), | |
| 98 std::move(rightBounds)); | |
| 99 } | |
| 100 | |
| 101 VRDisplay* VRController::createOrUpdateDisplay( | 41 VRDisplay* VRController::createOrUpdateDisplay( |
| 102 const device::blink::VRDisplayPtr& display) { | 42 const device::blink::VRDisplayPtr& display) { |
| 103 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | 43 VRDisplay* vrDisplay = getDisplayForIndex(display->index); |
| 104 if (!vrDisplay) { | 44 if (!vrDisplay) { |
| 105 vrDisplay = new VRDisplay(m_navigatorVR); | 45 vrDisplay = new VRDisplay(m_navigatorVR); |
| 106 m_displays.append(vrDisplay); | 46 m_displays.append(vrDisplay); |
| 107 } | 47 } |
| 108 | 48 |
| 109 vrDisplay->update(display); | 49 vrDisplay->update(display); |
| 110 return vrDisplay; | 50 return vrDisplay; |
| 111 } | 51 } |
| 112 | 52 |
| 113 VRDisplayVector VRController::updateDisplays( | 53 VRDisplayVector VRController::updateDisplays( |
| 114 mojo::WTFArray<device::blink::VRDisplayPtr> displays) { | 54 mojo::WTFArray<device::blink::VRDisplayWithServicePtr> displays) { |
| 115 VRDisplayVector vrDisplays; | 55 VRDisplayVector vrDisplays; |
| 116 | 56 |
| 117 for (const auto& display : displays.PassStorage()) { | 57 for (const auto& displayWithService : displays.PassStorage()) { |
| 118 VRDisplay* vrDisplay = createOrUpdateDisplay(display); | 58 VRDisplay* vrDisplay = |
| 59 createOrUpdateDisplay(std::move(displayWithService->display)); |
| 60 if (displayWithService->service) |
| 61 vrDisplay->setService(std::move(displayWithService->service)); |
| 119 vrDisplays.append(vrDisplay); | 62 vrDisplays.append(vrDisplay); |
| 120 } | 63 } |
| 121 | 64 |
| 122 return vrDisplays; | 65 return vrDisplays; |
| 123 } | 66 } |
| 124 | 67 |
| 125 VRDisplay* VRController::getDisplayForIndex(unsigned index) { | 68 VRDisplay* VRController::getDisplayForIndex(unsigned index) { |
| 126 VRDisplay* display; | 69 VRDisplay* display; |
| 127 for (size_t i = 0; i < m_displays.size(); ++i) { | 70 for (size_t i = 0; i < m_displays.size(); ++i) { |
| 128 display = m_displays[i]; | 71 display = m_displays[i]; |
| 129 if (display->displayId() == index) { | 72 if (display->displayId() == index) { |
| 130 return display; | 73 return display; |
| 131 } | 74 } |
| 132 } | 75 } |
| 133 | 76 |
| 134 return 0; | 77 return 0; |
| 135 } | 78 } |
| 136 | 79 |
| 137 void VRController::onGetDisplays( | 80 void VRController::onGetDisplays( |
| 138 mojo::WTFArray<device::blink::VRDisplayPtr> displays) { | 81 mojo::WTFArray<device::blink::VRDisplayWithServicePtr> displays) { |
| 139 VRDisplayVector outDisplays = updateDisplays(std::move(displays)); | 82 VRDisplayVector outDisplays = updateDisplays(std::move(displays)); |
| 83 for (const auto& VRDisplay : outDisplays) { |
| 84 VRDisplay->RegisterDisplayService(); |
| 85 } |
| 140 | 86 |
| 141 std::unique_ptr<VRGetDevicesCallback> callback = | 87 std::unique_ptr<VRGetDevicesCallback> callback = |
| 142 m_pendingGetDevicesCallbacks.takeFirst(); | 88 m_pendingGetDevicesCallbacks.takeFirst(); |
| 143 if (!callback) | 89 if (!callback) |
| 144 return; | 90 return; |
| 145 | 91 |
| 146 callback->onSuccess(outDisplays); | 92 callback->onSuccess(outDisplays); |
| 147 } | 93 } |
| 148 | 94 |
| 149 void VRController::onPresentComplete(ScriptPromiseResolver* resolver, | |
| 150 unsigned index, | |
| 151 bool success) { | |
| 152 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 153 if (!vrDisplay) { | |
| 154 DOMException* exception = | |
| 155 DOMException::create(InvalidStateError, "VRDisplay not found."); | |
| 156 resolver->reject(exception); | |
| 157 return; | |
| 158 } | |
| 159 | |
| 160 if (success) { | |
| 161 vrDisplay->beginPresent(resolver); | |
| 162 } else { | |
| 163 vrDisplay->forceExitPresent(); | |
| 164 DOMException* exception = DOMException::create( | |
| 165 NotAllowedError, "Presentation request was denied."); | |
| 166 resolver->reject(exception); | |
| 167 } | |
| 168 } | |
| 169 | |
| 170 void VRController::OnDisplayChanged(device::blink::VRDisplayPtr display) { | |
| 171 VRDisplay* vrDisplay = getDisplayForIndex(display->index); | |
| 172 if (!vrDisplay) | |
| 173 return; | |
| 174 | |
| 175 vrDisplay->update(display); | |
| 176 } | |
| 177 | |
| 178 void VRController::OnExitPresent(unsigned index) { | |
| 179 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 180 if (vrDisplay) | |
| 181 vrDisplay->forceExitPresent(); | |
| 182 } | |
| 183 | |
| 184 void VRController::OnDisplayConnected(device::blink::VRDisplayPtr display) { | |
| 185 VRDisplay* vrDisplay = createOrUpdateDisplay(display); | |
| 186 if (!vrDisplay) | |
| 187 return; | |
| 188 | |
| 189 m_navigatorVR->fireVREvent(VRDisplayEvent::create( | |
| 190 EventTypeNames::vrdisplayconnect, true, false, vrDisplay, "connect")); | |
| 191 } | |
| 192 | |
| 193 void VRController::OnDisplayDisconnected(unsigned index) { | |
| 194 VRDisplay* vrDisplay = getDisplayForIndex(index); | |
| 195 if (!vrDisplay) | |
| 196 return; | |
| 197 | |
| 198 vrDisplay->disconnected(); | |
| 199 | |
| 200 m_navigatorVR->fireVREvent( | |
| 201 VRDisplayEvent::create(EventTypeNames::vrdisplaydisconnect, true, false, | |
| 202 vrDisplay, "disconnect")); | |
| 203 } | |
| 204 | |
| 205 void VRController::contextDestroyed() { | 95 void VRController::contextDestroyed() { |
| 206 // If the document context was destroyed, shut down the client connection | 96 // If the document context was destroyed, shut down the client connection |
| 207 // and never call the mojo service again. | 97 // and never call the mojo service again. |
| 208 m_binding.Close(); | |
| 209 m_service.reset(); | 98 m_service.reset(); |
| 210 | 99 |
| 100 // Shutdown all displays' message pipe |
| 101 for (size_t i = 0; i < m_displays.size(); ++i) |
| 102 m_displays[i]->shutdownMessagePipe(); |
| 103 |
| 211 // The context is not automatically cleared, so do it manually. | 104 // The context is not automatically cleared, so do it manually. |
| 212 ContextLifecycleObserver::clearContext(); | 105 ContextLifecycleObserver::clearContext(); |
| 213 } | 106 } |
| 214 | 107 |
| 215 DEFINE_TRACE(VRController) { | 108 DEFINE_TRACE(VRController) { |
| 216 visitor->trace(m_navigatorVR); | 109 visitor->trace(m_navigatorVR); |
| 217 visitor->trace(m_displays); | 110 visitor->trace(m_displays); |
| 218 | 111 |
| 219 ContextLifecycleObserver::trace(visitor); | 112 ContextLifecycleObserver::trace(visitor); |
| 220 } | 113 } |
| 221 | 114 |
| 222 } // namespace blink | 115 } // namespace blink |
| OLD | NEW |