| 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/frame/LocalFrame.h" | 7 #include "core/frame/LocalFrame.h" |
| 8 #include "modules/vr/VRTypeConverters.h" |
| 9 #include "platform/MojoHelper.h" |
| 8 #include "platform/RuntimeEnabledFeatures.h" | 10 #include "platform/RuntimeEnabledFeatures.h" |
| 11 #include "public/platform/ServiceRegistry.h" |
| 9 | 12 |
| 10 namespace blink { | 13 namespace blink { |
| 11 | 14 |
| 12 VRController::~VRController() | 15 VRController::~VRController() |
| 13 { | 16 { |
| 17 m_pendingDeviceRequests.clear(); |
| 14 } | 18 } |
| 15 | 19 |
| 16 void VRController::provideTo(LocalFrame& frame, WebVRClient* client) | 20 void VRController::provideTo(LocalFrame& frame, ServiceRegistry* registry) |
| 17 { | 21 { |
| 18 ASSERT(RuntimeEnabledFeatures::webVREnabled()); | 22 ASSERT(RuntimeEnabledFeatures::webVREnabled()); |
| 19 | 23 |
| 20 VRController* controller = new VRController(frame, client); | 24 VRController* controller = new VRController(frame, registry); |
| 21 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); | 25 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); |
| 22 } | 26 } |
| 23 | 27 |
| 24 VRController* VRController::from(LocalFrame& frame) | 28 VRController* VRController::from(LocalFrame& frame) |
| 25 { | 29 { |
| 26 return static_cast<VRController*>(Supplement<LocalFrame>::from(frame, supple
mentName())); | 30 return static_cast<VRController*>(Supplement<LocalFrame>::from(frame, supple
mentName())); |
| 27 } | 31 } |
| 28 | 32 |
| 29 VRController::VRController(LocalFrame& frame, WebVRClient* client) | 33 VRController::VRController(LocalFrame& frame, ServiceRegistry* registry) |
| 30 : LocalFrameLifecycleObserver(&frame) | 34 : LocalFrameLifecycleObserver(&frame) |
| 31 , m_client(client) | 35 , m_registry(registry) |
| 32 { | 36 { |
| 37 DCHECK(!m_service.is_bound()); |
| 38 DCHECK(registry); |
| 33 } | 39 } |
| 34 | 40 |
| 35 const char* VRController::supplementName() | 41 const char* VRController::supplementName() |
| 36 { | 42 { |
| 37 return "VRController"; | 43 return "VRController"; |
| 38 } | 44 } |
| 39 | 45 |
| 40 void VRController::getDevices(WebVRGetDevicesCallback* callback) | 46 void VRController::getDevices(WebVRGetDevicesCallback* callback) |
| 41 { | 47 { |
| 42 // When detached, the client is no longer valid. | 48 // When detached, the service is no longer valid. |
| 43 if (!m_client) { | 49 if (!service()) { |
| 44 callback->onError(); | 50 callback->onError(); |
| 45 delete callback; | 51 delete callback; |
| 46 return; | 52 return; |
| 47 } | 53 } |
| 48 | 54 |
| 49 // Client is expected to take ownership of the callback | 55 bool requestPending = m_pendingDeviceRequests.size() > 0; |
| 50 m_client->getDevices(callback); | 56 |
| 57 // VRController takes ownership of the callback |
| 58 m_pendingDeviceRequests.append(adoptPtr(callback)); |
| 59 |
| 60 // We don't need to fire off another request if the previous one hasn't |
| 61 // returned yet. Just add the callback to the pending request list in |
| 62 // this case. |
| 63 if (!requestPending) |
| 64 service()->GetDevices(sameThreadBindForMojo(&VRController::OnGetDevices,
this)); |
| 51 } | 65 } |
| 52 | 66 |
| 53 void VRController::getSensorState(unsigned index, WebHMDSensorState& into) | 67 void VRController::getSensorState(unsigned index, WebHMDSensorState* into) |
| 54 { | 68 { |
| 55 // When detached, the client is no longer valid. | 69 // When detached, the service is no longer valid. |
| 56 if (!m_client) | 70 if (!service()) |
| 57 return; | 71 return; |
| 58 m_client->getSensorState(index, into); | 72 |
| 73 // Should never have overlapping getSensorState requests |
| 74 DCHECK(!m_pendingState); |
| 75 |
| 76 m_pendingState = into; |
| 77 |
| 78 service()->GetSensorState(index, sameThreadBindForMojo(&VRController::OnGetS
ensorState, this)); |
| 79 |
| 80 // This call needs to return results synchronously in order to be useful and |
| 81 // provide the lowest latency results possible. |
| 82 service().WaitForIncomingResponse(); |
| 59 } | 83 } |
| 60 | 84 |
| 61 void VRController::resetSensor(unsigned index) | 85 void VRController::resetSensor(unsigned index) |
| 62 { | 86 { |
| 63 // When detached, the client is no longer valid. | 87 // When detached, the service is no longer valid. |
| 64 if (!m_client) | 88 if (!service()) |
| 65 return; | 89 return; |
| 66 m_client->resetSensor(index); | 90 service()->ResetSensor(index); |
| 91 } |
| 92 |
| 93 void VRController::OnGetDevices(const mojo::WTFArray<mojom::wtf::VRDeviceInfoPtr
>& devices) |
| 94 { |
| 95 WebVector<WebVRDevice> webDevices(devices.size()); |
| 96 |
| 97 if (m_pendingDeviceRequests.size()) { |
| 98 for (size_t i = 0; i < devices.size(); ++i) { |
| 99 webDevices[i] = devices[i].To<WebVRDevice>(); |
| 100 } |
| 101 |
| 102 for (size_t i = 0; i < m_pendingDeviceRequests.size(); ++i) { |
| 103 WebVRGetDevicesCallback* callback = m_pendingDeviceRequests[i].get()
; |
| 104 callback->onSuccess(webDevices); |
| 105 } |
| 106 |
| 107 m_pendingDeviceRequests.clear(); |
| 108 } |
| 109 } |
| 110 |
| 111 void VRController::OnGetSensorState(const mojom::wtf::VRSensorStatePtr& mojoStat
e) |
| 112 { |
| 113 if (m_pendingState) { |
| 114 *m_pendingState = mojoState.To<WebHMDSensorState>(); |
| 115 m_pendingState = nullptr; |
| 116 } |
| 67 } | 117 } |
| 68 | 118 |
| 69 void VRController::willDetachFrameHost() | 119 void VRController::willDetachFrameHost() |
| 70 { | 120 { |
| 71 m_client = nullptr; | 121 m_registry = nullptr; |
| 122 m_service = nullptr; |
| 123 } |
| 124 |
| 125 mojom::wtf::VRServicePtr& VRController::service() |
| 126 { |
| 127 // Lazily initialize the VRService the first time it's used. |
| 128 if (!m_service && m_registry) |
| 129 m_registry->connectToRemoteService(mojo::GetProxy(&m_service)); |
| 130 return m_service; |
| 72 } | 131 } |
| 73 | 132 |
| 74 DEFINE_TRACE(VRController) | 133 DEFINE_TRACE(VRController) |
| 75 { | 134 { |
| 76 Supplement<LocalFrame>::trace(visitor); | 135 Supplement<LocalFrame>::trace(visitor); |
| 77 LocalFrameLifecycleObserver::trace(visitor); | 136 LocalFrameLifecycleObserver::trace(visitor); |
| 78 } | 137 } |
| 79 | 138 |
| 80 } // namespace blink | 139 } // namespace blink |
| OLD | NEW |