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/Sensor.h" | 5 #include "modules/sensor/Sensor.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/ExceptionCode.h" | 8 #include "core/dom/ExceptionCode.h" |
9 #include "core/dom/ExecutionContextTask.h" | 9 #include "core/dom/ExecutionContextTask.h" |
10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 return; | 161 return; |
162 | 162 |
163 startListening(); | 163 startListening(); |
164 } | 164 } |
165 | 165 |
166 void Sensor::onSensorReadingChanged() { | 166 void Sensor::onSensorReadingChanged() { |
167 if (m_polling) | 167 if (m_polling) |
168 m_polling->onSensorReadingChanged(); | 168 m_polling->onSensorReadingChanged(); |
169 } | 169 } |
170 | 170 |
171 void Sensor::onSensorError() { | 171 void Sensor::onSensorError(ExceptionCode code, |
172 reportError(); | 172 const String& sanitizedMessage, |
| 173 const String& unsanitizedMessage) { |
| 174 reportError(code, sanitizedMessage, unsanitizedMessage); |
173 } | 175 } |
174 | 176 |
175 void Sensor::onStartRequestCompleted(bool result) { | 177 void Sensor::onStartRequestCompleted(bool result) { |
176 if (m_state != Sensor::SensorState::ACTIVATING) | 178 if (m_state != Sensor::SensorState::ACTIVATING) |
177 return; | 179 return; |
178 | 180 |
179 if (!result) { | 181 if (!result) { |
180 reportError(); | 182 reportError( |
| 183 OperationError, |
| 184 "start() call has failed possibly due to inappropriate options."); |
181 return; | 185 return; |
182 } | 186 } |
183 | 187 |
184 DCHECK(m_configuration); | 188 DCHECK(m_configuration); |
185 DCHECK(m_sensorProxy); | 189 DCHECK(m_sensorProxy); |
186 auto pollCallback = WTF::bind(&Sensor::pollForData, wrapWeakPersistent(this)); | 190 auto pollCallback = WTF::bind(&Sensor::pollForData, wrapWeakPersistent(this)); |
187 DCHECK_GT(m_configuration->frequency, 0); | 191 DCHECK_GT(m_configuration->frequency, 0); |
188 m_polling = SensorPollingStrategy::create(1 / m_configuration->frequency, | 192 m_polling = SensorPollingStrategy::create(1 / m_configuration->frequency, |
189 std::move(pollCallback), | 193 std::move(pollCallback), |
190 m_sensorProxy->reportingMode()); | 194 m_sensorProxy->reportingMode()); |
191 updateState(Sensor::SensorState::ACTIVE); | 195 updateState(Sensor::SensorState::ACTIVE); |
192 } | 196 } |
193 | 197 |
194 void Sensor::onStopRequestCompleted(bool result) { | 198 void Sensor::onStopRequestCompleted(bool result) { |
195 if (m_state == Sensor::SensorState::IDLE) | 199 if (m_state == Sensor::SensorState::IDLE) |
196 return; | 200 return; |
197 | 201 |
198 if (!result) | 202 if (!result) |
199 reportError(); | 203 reportError(OperationError); |
200 | 204 |
201 DCHECK(m_sensorProxy); | 205 DCHECK(m_sensorProxy); |
202 m_sensorProxy->removeObserver(this); | 206 m_sensorProxy->removeObserver(this); |
203 } | 207 } |
204 | 208 |
205 void Sensor::pageVisibilityChanged() { | 209 void Sensor::pageVisibilityChanged() { |
206 updatePollingStatus(); | 210 updatePollingStatus(); |
207 | 211 |
208 if (!m_sensorProxy || !m_sensorProxy->isInitialized()) | 212 if (!m_sensorProxy || !m_sensorProxy->isInitialized()) |
209 return; | 213 return; |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
286 m_state = newState; | 290 m_state = newState; |
287 if (getExecutionContext()) { | 291 if (getExecutionContext()) { |
288 getExecutionContext()->postTask( | 292 getExecutionContext()->postTask( |
289 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChanged, | 293 BLINK_FROM_HERE, createSameThreadTask(&Sensor::notifyStateChanged, |
290 wrapWeakPersistent(this))); | 294 wrapWeakPersistent(this))); |
291 } | 295 } |
292 | 296 |
293 updatePollingStatus(); | 297 updatePollingStatus(); |
294 } | 298 } |
295 | 299 |
296 void Sensor::reportError() { | 300 void Sensor::reportError(ExceptionCode code, |
| 301 const String& sanitizedMessage, |
| 302 const String& unsanitizedMessage) { |
297 updateState(Sensor::SensorState::ERRORED); | 303 updateState(Sensor::SensorState::ERRORED); |
298 // TODO(Mikhail) : Dispatch Sensor Error event. | 304 if (getExecutionContext()) { |
| 305 auto error = |
| 306 DOMException::create(code, sanitizedMessage, unsanitizedMessage); |
| 307 getExecutionContext()->postTask( |
| 308 BLINK_FROM_HERE, |
| 309 createSameThreadTask(&Sensor::notifyError, wrapWeakPersistent(this), |
| 310 wrapPersistent(error))); |
| 311 } |
299 } | 312 } |
300 | 313 |
301 void Sensor::updatePollingStatus() { | 314 void Sensor::updatePollingStatus() { |
302 if (!m_polling) | 315 if (!m_polling) |
303 return; | 316 return; |
304 | 317 |
305 if (m_state != Sensor::SensorState::ACTIVE || | 318 if (m_state != Sensor::SensorState::ACTIVE || |
306 page()->visibilityState() != PageVisibilityStateVisible) { | 319 page()->visibilityState() != PageVisibilityStateVisible) { |
307 m_polling->stopPolling(); | 320 m_polling->stopPolling(); |
308 } else { | 321 } else { |
309 m_polling->startPolling(); | 322 m_polling->startPolling(); |
310 } | 323 } |
311 } | 324 } |
312 | 325 |
313 void Sensor::notifySensorReadingChanged() { | 326 void Sensor::notifySensorReadingChanged() { |
314 dispatchEvent( | 327 dispatchEvent( |
315 SensorReadingEvent::create(EventTypeNames::change, m_sensorReading)); | 328 SensorReadingEvent::create(EventTypeNames::change, m_sensorReading)); |
316 } | 329 } |
317 | 330 |
318 void Sensor::notifyStateChanged() { | 331 void Sensor::notifyStateChanged() { |
319 dispatchEvent(Event::create(EventTypeNames::statechange)); | 332 dispatchEvent(Event::create(EventTypeNames::statechange)); |
320 } | 333 } |
321 | 334 |
| 335 void Sensor::notifyError(DOMException* error) { |
| 336 dispatchEvent( |
| 337 SensorErrorEvent::create(EventTypeNames::error, std::move(error))); |
| 338 } |
| 339 |
322 } // namespace blink | 340 } // namespace blink |
OLD | NEW |