OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 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/sensor/SensorProxy.h" | 5 #include "modules/sensor/SensorProxy.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/frame/LocalFrame.h" | 8 #include "core/frame/LocalFrame.h" |
9 #include "modules/sensor/SensorProviderProxy.h" | 9 #include "modules/sensor/SensorProviderProxy.h" |
10 #include "modules/sensor/SensorReading.h" | |
11 #include "modules/sensor/SensorReadingUpdater.h" | 10 #include "modules/sensor/SensorReadingUpdater.h" |
12 #include "platform/mojo/MojoHelper.h" | 11 #include "platform/mojo/MojoHelper.h" |
13 #include "public/platform/Platform.h" | 12 #include "public/platform/Platform.h" |
14 | 13 |
15 using namespace device::mojom::blink; | 14 using namespace device::mojom::blink; |
16 | 15 |
17 namespace blink { | 16 namespace blink { |
18 | 17 |
19 SensorProxy::SensorProxy(SensorType sensorType, | 18 SensorProxy::SensorProxy(SensorType sensorType, |
20 SensorProviderProxy* provider, | 19 SensorProviderProxy* provider, |
21 Page* page, | 20 Page* page) |
22 std::unique_ptr<SensorReadingFactory> readingFactory) | |
23 : PageVisibilityObserver(page), | 21 : PageVisibilityObserver(page), |
24 m_type(sensorType), | 22 m_type(sensorType), |
25 m_mode(ReportingMode::CONTINUOUS), | 23 m_mode(ReportingMode::CONTINUOUS), |
26 m_provider(provider), | 24 m_provider(provider), |
27 m_clientBinding(this), | 25 m_clientBinding(this), |
28 m_state(SensorProxy::Uninitialized), | 26 m_state(SensorProxy::Uninitialized), |
29 m_suspended(false), | 27 m_suspended(false), |
30 m_readingFactory(std::move(readingFactory)), | |
31 m_maximumFrequency(0.0) {} | 28 m_maximumFrequency(0.0) {} |
32 | 29 |
33 SensorProxy::~SensorProxy() {} | 30 SensorProxy::~SensorProxy() {} |
34 | 31 |
35 void SensorProxy::dispose() { | 32 void SensorProxy::dispose() { |
36 m_clientBinding.Close(); | 33 m_clientBinding.Close(); |
37 } | 34 } |
38 | 35 |
39 DEFINE_TRACE(SensorProxy) { | 36 DEFINE_TRACE(SensorProxy) { |
40 visitor->trace(m_readingUpdater); | 37 visitor->trace(m_readingUpdater); |
41 visitor->trace(m_reading); | |
42 visitor->trace(m_observers); | 38 visitor->trace(m_observers); |
43 visitor->trace(m_provider); | 39 visitor->trace(m_provider); |
44 PageVisibilityObserver::trace(visitor); | 40 PageVisibilityObserver::trace(visitor); |
45 } | 41 } |
46 | 42 |
47 void SensorProxy::addObserver(Observer* observer) { | 43 void SensorProxy::addObserver(Observer* observer) { |
48 if (!m_observers.contains(observer)) | 44 if (!m_observers.contains(observer)) |
49 m_observers.insert(observer); | 45 m_observers.insert(observer); |
50 } | 46 } |
51 | 47 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 DCHECK(isInitialized()); | 113 DCHECK(isInitialized()); |
118 return m_defaultConfig.get(); | 114 return m_defaultConfig.get(); |
119 } | 115 } |
120 | 116 |
121 Document* SensorProxy::document() const { | 117 Document* SensorProxy::document() const { |
122 return m_provider->supplementable()->document(); | 118 return m_provider->supplementable()->document(); |
123 } | 119 } |
124 | 120 |
125 void SensorProxy::updateSensorReading() { | 121 void SensorProxy::updateSensorReading() { |
126 DCHECK(isInitialized()); | 122 DCHECK(isInitialized()); |
127 DCHECK(m_readingFactory); | |
128 int readAttempts = 0; | 123 int readAttempts = 0; |
129 const int kMaxReadAttemptsCount = 10; | 124 const int kMaxReadAttemptsCount = 10; |
130 device::SensorReading readingData; | 125 device::SensorReading readingData; |
131 while (!tryReadFromBuffer(readingData)) { | 126 while (!tryReadFromBuffer(readingData)) { |
132 if (++readAttempts == kMaxReadAttemptsCount) { | 127 if (++readAttempts == kMaxReadAttemptsCount) { |
133 handleSensorError(); | 128 handleSensorError(); |
134 return; | 129 return; |
135 } | 130 } |
136 } | 131 } |
137 | 132 |
138 m_reading = m_readingFactory->createSensorReading(readingData); | 133 m_reading = readingData; |
139 } | 134 } |
140 | 135 |
141 void SensorProxy::notifySensorChanged(double timestamp) { | 136 void SensorProxy::notifySensorChanged(double timestamp) { |
142 // This notification leads to sync 'onchange' event sending, so | 137 // This notification leads to sync 'onchange' event sending, so |
143 // we must cache m_observers as it can be modified within event handlers. | 138 // we must cache m_observers as it can be modified within event handlers. |
144 auto copy = m_observers; | 139 auto copy = m_observers; |
145 for (Observer* observer : copy) | 140 for (Observer* observer : copy) |
146 observer->onSensorReadingChanged(timestamp); | 141 observer->onSensorReadingChanged(timestamp); |
147 } | 142 } |
148 | 143 |
(...skipping 21 matching lines...) Expand all Loading... |
170 void SensorProxy::handleSensorError(ExceptionCode code, | 165 void SensorProxy::handleSensorError(ExceptionCode code, |
171 String sanitizedMessage, | 166 String sanitizedMessage, |
172 String unsanitizedMessage) { | 167 String unsanitizedMessage) { |
173 if (!Platform::current()) { | 168 if (!Platform::current()) { |
174 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. | 169 // TODO(rockot): Remove this hack once renderer shutdown sequence is fixed. |
175 return; | 170 return; |
176 } | 171 } |
177 | 172 |
178 m_state = Uninitialized; | 173 m_state = Uninitialized; |
179 m_frequenciesUsed.clear(); | 174 m_frequenciesUsed.clear(); |
| 175 m_reading = device::SensorReading(); |
180 | 176 |
181 // The m_sensor.reset() will release all callbacks and its bound parameters, | 177 // The m_sensor.reset() will release all callbacks and its bound parameters, |
182 // therefore, handleSensorError accepts messages by value. | 178 // therefore, handleSensorError accepts messages by value. |
183 m_sensor.reset(); | 179 m_sensor.reset(); |
184 m_sharedBuffer.reset(); | 180 m_sharedBuffer.reset(); |
185 m_sharedBufferHandle.reset(); | 181 m_sharedBufferHandle.reset(); |
186 m_defaultConfig.reset(); | 182 m_defaultConfig.reset(); |
187 m_clientBinding.Close(); | 183 m_clientBinding.Close(); |
188 m_reading = nullptr; | |
189 | 184 |
190 for (Observer* observer : m_observers) | 185 for (Observer* observer : m_observers) |
191 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); | 186 observer->onSensorError(code, sanitizedMessage, unsanitizedMessage); |
192 } | 187 } |
193 | 188 |
194 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, | 189 void SensorProxy::onSensorCreated(SensorInitParamsPtr params, |
195 SensorClientRequest clientRequest) { | 190 SensorClientRequest clientRequest) { |
196 DCHECK_EQ(Initializing, m_state); | 191 DCHECK_EQ(Initializing, m_state); |
197 if (!params) { | 192 if (!params) { |
198 handleSensorError(NotFoundError, "Sensor is not present on the platform."); | 193 handleSensorError(NotFoundError, "Sensor is not present on the platform."); |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
274 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); | 269 const device::OneWriterSeqLock& seqlock = buffer->seqlock.value(); |
275 auto version = seqlock.ReadBegin(); | 270 auto version = seqlock.ReadBegin(); |
276 auto readingData = buffer->reading; | 271 auto readingData = buffer->reading; |
277 if (seqlock.ReadRetry(version)) | 272 if (seqlock.ReadRetry(version)) |
278 return false; | 273 return false; |
279 result = readingData; | 274 result = readingData; |
280 return true; | 275 return true; |
281 } | 276 } |
282 | 277 |
283 } // namespace blink | 278 } // namespace blink |
OLD | NEW |