Chromium Code Reviews| 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/VRGetDevicesCallback.h" | |
| 8 #include "platform/RuntimeEnabledFeatures.h" | 9 #include "platform/RuntimeEnabledFeatures.h" |
| 10 #include "platform/mojo/MojoHelper.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 { |
| 14 } | 17 } |
| 15 | 18 |
| 16 void VRController::provideTo(LocalFrame& frame, WebVRClient* client) | 19 void VRController::provideTo(LocalFrame& frame, ServiceRegistry* registry) |
| 17 { | 20 { |
| 18 ASSERT(RuntimeEnabledFeatures::webVREnabled()); | 21 ASSERT(RuntimeEnabledFeatures::webVREnabled()); |
| 19 | 22 |
| 20 VRController* controller = new VRController(frame, client); | 23 VRController* controller = new VRController(frame, registry); |
| 21 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); | 24 Supplement<LocalFrame>::provideTo(frame, supplementName(), controller); |
| 22 } | 25 } |
| 23 | 26 |
| 24 VRController* VRController::from(LocalFrame& frame) | 27 VRController* VRController::from(LocalFrame& frame) |
| 25 { | 28 { |
| 26 return static_cast<VRController*>(Supplement<LocalFrame>::from(frame, supple mentName())); | 29 return static_cast<VRController*>(Supplement<LocalFrame>::from(frame, supple mentName())); |
| 27 } | 30 } |
| 28 | 31 |
| 29 VRController::VRController(LocalFrame& frame, WebVRClient* client) | 32 VRController::VRController(LocalFrame& frame, ServiceRegistry* registry) |
| 30 : LocalFrameLifecycleObserver(&frame) | 33 : LocalFrameLifecycleObserver(&frame) |
| 31 , m_client(client) | |
| 32 { | 34 { |
| 35 ASSERT(!m_service.is_bound()); | |
| 36 ASSERT(registry); | |
| 37 registry->connectToRemoteService(mojo::GetProxy(&m_service)); | |
| 33 } | 38 } |
| 34 | 39 |
| 35 const char* VRController::supplementName() | 40 const char* VRController::supplementName() |
| 36 { | 41 { |
| 37 return "VRController"; | 42 return "VRController"; |
| 38 } | 43 } |
| 39 | 44 |
| 40 void VRController::getDevices(WebVRGetDevicesCallback* callback) | 45 void VRController::getDevices(VRGetDevicesCallback* callback) |
| 41 { | 46 { |
| 42 // When detached, the client is no longer valid. | 47 if (!m_service) { |
| 43 if (!m_client) { | |
| 44 callback->onError(); | 48 callback->onError(); |
| 45 delete callback; | 49 delete callback; |
| 46 return; | 50 return; |
| 47 } | 51 } |
| 48 | 52 |
| 49 // Client is expected to take ownership of the callback | 53 VRGetDevicesRequest* deviceRequest = new VRGetDevicesRequest(callback); |
| 50 m_client->getDevices(callback); | 54 m_pendingRequests.add(adoptPtr(deviceRequest)); |
| 55 m_service->GetDevices( | |
| 56 sameThreadBindForMojoWithBoundArgs(&VRController::OnGetDevices, this, de viceRequest)); | |
| 51 } | 57 } |
| 52 | 58 |
| 53 void VRController::getSensorState(unsigned index, WebHMDSensorState& into) | 59 void VRController::getSensorState(unsigned index, WebHMDSensorState& into) |
| 54 { | 60 { |
| 55 // When detached, the client is no longer valid. | 61 if (!m_service) |
| 56 if (!m_client) | |
| 57 return; | 62 return; |
| 58 m_client->getSensorState(index, into); | 63 |
| 64 m_service->GetSensorState( | |
| 65 index, | |
| 66 sameThreadBindForMojoWithBoundArgs(&VRController::OnGetSensorState, this , &into)); | |
| 67 | |
| 68 // This call needs to return results synchronously in order to be useful and | |
| 69 // provide the lowest latency results possible. | |
| 70 m_service.WaitForIncomingResponse(); | |
| 59 } | 71 } |
| 60 | 72 |
| 61 void VRController::resetSensor(unsigned index) | 73 void VRController::resetSensor(unsigned index) |
| 62 { | 74 { |
| 63 // When detached, the client is no longer valid. | 75 if (!m_service) |
| 64 if (!m_client) | |
| 65 return; | 76 return; |
| 66 m_client->resetSensor(index); | 77 m_service->ResetSensor(index); |
| 67 } | 78 } |
| 68 | 79 |
| 69 void VRController::willDetachFrameHost() | 80 void VRController::willDetachFrameHost() |
| 70 { | 81 { |
| 71 m_client = nullptr; | 82 resetSensor(0); |
| 83 } | |
| 84 | |
| 85 void VRController::OnGetDevices(VRGetDevicesRequest* deviceRequest, const mojo:: Array<mojom::VRDeviceInfoPtr>& devices) | |
| 86 { | |
| 87 WebVector<WebVRDevice> webDevices(devices.size()); | |
| 88 | |
| 89 VRGetDevicesCallback* callback = deviceRequest->getDevicesCallback(); | |
| 90 if (!callback) | |
| 91 return; | |
| 92 | |
| 93 for (size_t i = 0; i < devices.size(); ++i) { | |
| 94 webDevices[i] = devices[i].To<WebVRDevice>(); | |
| 95 } | |
| 96 | |
| 97 callback->onSuccess(webDevices); | |
| 98 ASSERT(m_pendingRequests.contains(deviceRequest)); | |
| 99 m_pendingRequests.remove(deviceRequest); | |
|
haraken
2016/04/27 06:51:07
BTW, why do you need m_pendingRequests? If it's us
RaviKasibhatla
2016/04/27 14:33:49
I have removed this entire logic. I am now simply
| |
| 100 } | |
| 101 | |
| 102 void VRController::OnGetSensorState(WebHMDSensorState* state, const mojom::VRSen sorStatePtr& mojoState) | |
| 103 { | |
| 104 *state = mojoState.To<WebHMDSensorState>(); | |
| 72 } | 105 } |
| 73 | 106 |
| 74 DEFINE_TRACE(VRController) | 107 DEFINE_TRACE(VRController) |
| 75 { | 108 { |
| 76 Supplement<LocalFrame>::trace(visitor); | 109 Supplement<LocalFrame>::trace(visitor); |
| 77 LocalFrameLifecycleObserver::trace(visitor); | 110 LocalFrameLifecycleObserver::trace(visitor); |
| 78 } | 111 } |
| 79 | 112 |
| 113 VRGetDevicesRequest::VRGetDevicesRequest(VRGetDevicesCallback* callback) | |
| 114 { | |
| 115 m_getDevicesCallback = adoptPtr(callback); | |
|
haraken
2016/04/27 06:51:07
It's nasty to call adoptPtr for a raw pointer... V
| |
| 116 } | |
| 117 | |
| 118 VRGetDevicesRequest::~VRGetDevicesRequest() | |
| 119 { | |
| 120 m_getDevicesCallback.clear(); | |
|
haraken
2016/04/27 06:51:07
This won't be needed, since the destructor will au
| |
| 121 } | |
| 122 | |
| 123 VRGetDevicesCallback* VRGetDevicesRequest::getDevicesCallback() const | |
| 124 { | |
| 125 return m_getDevicesCallback.get(); | |
| 126 } | |
| 127 | |
| 80 } // namespace blink | 128 } // namespace blink |
| OLD | NEW |