OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "modules/sensor/SensorProxy.h" | |
6 | |
7 #include "core/frame/LocalFrame.h" | |
8 #include "modules/sensor/SensorProviderProxy.h" | |
9 #include "platform/mojo/MojoHelper.h" | |
10 | |
11 using namespace device::mojom::blink; | |
12 | |
13 namespace blink { | |
14 | |
15 SensorProxy::SensorProxy(SensorType sensorType, SensorProviderProxy* provider) | |
16 : m_type(sensorType) | |
17 , m_mode(ReportingMode::CONTINUOUS) | |
18 , m_provider(provider) | |
19 , m_clientBinding(this) | |
20 , m_state(SensorProxy::Uninitialized) | |
21 , m_reading() | |
22 { | |
23 } | |
24 | |
25 SensorProxy::~SensorProxy() | |
26 { | |
27 } | |
28 | |
29 void SensorProxy::dispose() | |
30 { | |
31 m_clientBinding.Close(); | |
32 } | |
33 | |
34 DEFINE_TRACE(SensorProxy) | |
35 { | |
36 visitor->trace(m_observers); | |
37 visitor->trace(m_provider); | |
38 } | |
39 | |
40 void SensorProxy::addObserver(Observer* observer) | |
41 { | |
42 if (!m_observers.contains(observer)) | |
43 m_observers.add(observer); | |
44 } | |
45 | |
46 void SensorProxy::removeObserver(Observer* observer) | |
47 { | |
48 if (m_observers.contains(observer)) | |
49 m_observers.remove(observer); | |
50 | |
51 if (m_observers.isEmpty()) | |
52 m_provider->removeSensor(this); | |
53 } | |
54 | |
55 void SensorProxy::initialize() | |
56 { | |
57 if (m_state != Uninitialized) | |
58 return; | |
59 m_state = Initializing; | |
60 auto callback = convertToBaseCallback(WTF::bind(&SensorProxy::onSensorCreate d, wrapWeakPersistent(this))); | |
61 m_provider->sensorProvider()->GetSensor(m_type, mojo::GetProxy(&m_sensor), c allback); | |
62 } | |
63 | |
64 void SensorProxy::addConfiguration(SensorConfigurationPtr configuration, std::un ique_ptr<Function<void(bool)>> callback) | |
65 { | |
66 DCHECK(isInitialized()); | |
67 m_sensor->AddConfiguration(std::move(configuration), convertToBaseCallback(s td::move(callback))); | |
68 } | |
69 | |
70 void SensorProxy::removeConfiguration(SensorConfigurationPtr configuration, std: :unique_ptr<Function<void(bool)>> callback) | |
71 { | |
72 DCHECK(isInitialized()); | |
73 m_sensor->RemoveConfiguration(std::move(configuration), convertToBaseCallbac k(std::move(callback))); | |
74 } | |
75 | |
76 void SensorProxy::suspend() | |
77 { | |
78 DCHECK(isInitialized()); | |
79 m_sensor->Suspend(); | |
80 } | |
81 | |
82 void SensorProxy::resume() | |
83 { | |
84 DCHECK(isInitialized()); | |
85 m_sensor->Resume(); | |
86 } | |
87 | |
88 void SensorProxy::updateReading() | |
89 { | |
90 DCHECK(isInitialized()); | |
91 Reading* reading = static_cast<Reading*>(m_sharedBuffer.get()); | |
92 m_reading = *reading; | |
93 } | |
94 | |
95 void SensorProxy::RaiseError() | |
96 { | |
97 handleSensorError(); | |
98 } | |
99 | |
100 void SensorProxy::SensorReadingChanged() | |
101 { | |
102 for (Observer* observer : m_observers) | |
103 observer->onSensorReadingChanged(); | |
104 } | |
105 | |
106 void SensorProxy::handleSensorError() | |
107 { | |
108 m_state = Uninitialized; | |
109 | |
110 ObserversSet observersCopy(m_observers); | |
111 for (Observer* observer : observersCopy) | |
112 observer->onSensorError(); | |
113 | |
114 m_observers.clear(); | |
115 m_provider->removeSensor(this); | |
116 } | |
117 | |
118 void SensorProxy::onSensorCreated(SensorReadBufferPtr buffer, SensorClientReques t clientRequest) | |
119 { | |
120 DCHECK_EQ(Initializing, m_state); | |
121 if (!buffer) { | |
122 handleSensorError(); | |
123 return; | |
124 } | |
125 | |
126 if (buffer->offset % SensorReadBuffer::kReadBufferSize != 0) { | |
127 NOTREACHED(); | |
128 handleSensorError(); | |
129 return; | |
130 } | |
131 | |
132 m_mode = buffer->mode; | |
133 | |
134 m_clientBinding.Bind(std::move(clientRequest)); | |
135 m_sensor.set_connection_error_handler(convertToBaseCallback(WTF::bind(&Senso rProxy::handleSensorError, wrapWeakPersistent(this)))); | |
136 | |
137 m_sharedBufferHandle = std::move(buffer->memory); | |
138 DCHECK(!m_sharedBuffer); | |
139 m_sharedBuffer = m_sharedBufferHandle->MapAtOffset(buffer->offset, SensorRea dBuffer::kReadBufferSize); | |
140 | |
141 if (!m_sharedBuffer) { | |
142 handleSensorError(); | |
143 return; | |
144 } | |
145 | |
146 auto callback = convertToBaseCallback(WTF::bind(&SensorProxy::onDefaultConfi guration, wrapWeakPersistent(this))); | |
147 m_sensor->GetDefaultConfiguration(callback); | |
timvolodine
2016/09/01 19:02:06
why is this needed? i.e. wouldn't a sensor always
Mikhail
2016/09/02 08:23:43
It is better to know the default config beforehand
timvolodine
2016/09/02 19:41:28
In that case I think my question still stands: why
Mikhail
2016/09/05 10:26:27
Oh sorry, I probably misunderstood your comment. I
| |
148 } | |
149 | |
150 void SensorProxy::onDefaultConfiguration(device::mojom::blink::SensorConfigurati onPtr config) | |
151 { | |
152 DCHECK_EQ(Initializing, m_state); | |
153 if (!config) { | |
154 handleSensorError(); | |
155 return; | |
156 } | |
157 m_defaultConfiguration = std::move(config); | |
timvolodine
2016/09/02 19:41:28
Is m_defaultConfiguration used anywhere? looks lik
| |
158 | |
159 m_state = Initialized; | |
160 for (Observer* observer : m_observers) | |
161 observer->onSensorInitialized(); | |
162 } | |
163 | |
164 } // namespace blink | |
OLD | NEW |