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) |
haraken
2016/04/27 15:52:12
getDevices() should take PassOwnPtr. See my below
RaviKasibhatla
2016/04/28 14:34:15
Done.
| |
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 m_pendingGetDevicesCallbacks.append(callback); |
50 m_client->getDevices(callback); | 54 m_service->GetDevices(sameThreadBindForMojo(&VRController::OnGetDevices, thi s)); |
bajones
2016/04/27 18:09:28
If multiple getDevices requests are made while one
RaviKasibhatla
2016/04/28 06:05:13
Isn't the current code doing the same? We are queu
| |
51 } | 55 } |
52 | 56 |
53 void VRController::getSensorState(unsigned index, WebHMDSensorState& into) | 57 void VRController::getSensorState(unsigned index, WebHMDSensorState& into) |
54 { | 58 { |
55 // When detached, the client is no longer valid. | 59 if (!m_service) |
56 if (!m_client) | |
57 return; | 60 return; |
58 m_client->getSensorState(index, into); | 61 |
62 m_pendingSensorStateRequest = &into; | |
63 m_service->GetSensorState(index, sameThreadBindForMojo(&VRController::OnGetS ensorState, this)); | |
64 | |
65 // This call needs to return results synchronously in order to be useful and | |
66 // provide the lowest latency results possible. | |
67 m_service.WaitForIncomingResponse(); | |
59 } | 68 } |
60 | 69 |
61 void VRController::resetSensor(unsigned index) | 70 void VRController::resetSensor(unsigned index) |
62 { | 71 { |
63 // When detached, the client is no longer valid. | 72 if (!m_service) |
64 if (!m_client) | |
65 return; | 73 return; |
66 m_client->resetSensor(index); | 74 m_service->ResetSensor(index); |
67 } | 75 } |
68 | 76 |
69 void VRController::willDetachFrameHost() | 77 void VRController::willDetachFrameHost() |
70 { | 78 { |
71 m_client = nullptr; | 79 resetSensor(0); |
80 } | |
81 | |
82 void VRController::OnGetDevices(const mojo::Array<mojom::VRDeviceInfoPtr>& devic es) | |
83 { | |
84 WebVector<WebVRDevice> webDevices(devices.size()); | |
85 | |
86 VRGetDevicesCallback* callback = m_pendingGetDevicesCallbacks.takeFirst(); | |
87 if (!callback) | |
88 return; | |
89 | |
90 for (size_t i = 0; i < devices.size(); ++i) { | |
91 webDevices[i] = devices[i].To<WebVRDevice>(); | |
92 } | |
bajones
2016/04/27 18:09:28
Non-critical idea: Seems like this could benefit f
RaviKasibhatla
2016/04/28 06:05:13
Yes, currently it uses the converter added in VRTy
| |
93 | |
94 callback->onSuccess(webDevices); | |
95 delete callback; | |
haraken
2016/04/27 15:52:12
We should avoid calling manual delete.
Can we mak
RaviKasibhatla
2016/04/28 06:05:13
Since we were already calling manual delete in VRC
RaviKasibhatla
2016/04/28 14:34:15
Done.
| |
96 } | |
97 | |
98 void VRController::OnGetSensorState(const mojom::VRSensorStatePtr& mojoState) | |
99 { | |
100 *m_pendingSensorStateRequest = mojoState.To<WebHMDSensorState>(); | |
72 } | 101 } |
73 | 102 |
74 DEFINE_TRACE(VRController) | 103 DEFINE_TRACE(VRController) |
75 { | 104 { |
76 Supplement<LocalFrame>::trace(visitor); | 105 Supplement<LocalFrame>::trace(visitor); |
77 LocalFrameLifecycleObserver::trace(visitor); | 106 LocalFrameLifecycleObserver::trace(visitor); |
78 } | 107 } |
79 | 108 |
80 } // namespace blink | 109 } // namespace blink |
OLD | NEW |