| 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/TaskRunnerHelper.h" | 9 #include "core/dom/TaskRunnerHelper.h" |
| 10 #include "core/inspector/ConsoleMessage.h" | 10 #include "core/inspector/ConsoleMessage.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 | 25 |
| 26 } // namespace | 26 } // namespace |
| 27 | 27 |
| 28 Sensor::Sensor(ExecutionContext* execution_context, | 28 Sensor::Sensor(ExecutionContext* execution_context, |
| 29 const SensorOptions& sensor_options, | 29 const SensorOptions& sensor_options, |
| 30 ExceptionState& exception_state, | 30 ExceptionState& exception_state, |
| 31 SensorType type) | 31 SensorType type) |
| 32 : ContextLifecycleObserver(execution_context), | 32 : ContextLifecycleObserver(execution_context), |
| 33 sensor_options_(sensor_options), | 33 sensor_options_(sensor_options), |
| 34 type_(type), | 34 type_(type), |
| 35 state_(SensorState::kIdle), | 35 state_(SensorState::kIdle) { |
| 36 pending_reading_update_(false) { | |
| 37 // Check secure context. | 36 // Check secure context. |
| 38 String error_message; | 37 String error_message; |
| 39 if (!execution_context->IsSecureContext(error_message)) { | 38 if (!execution_context->IsSecureContext(error_message)) { |
| 40 exception_state.ThrowDOMException(kSecurityError, error_message); | 39 exception_state.ThrowDOMException(kSecurityError, error_message); |
| 41 return; | 40 return; |
| 42 } | 41 } |
| 43 | 42 |
| 44 // Check top-level browsing context. | 43 // Check top-level browsing context. |
| 45 if (!ToDocument(execution_context)->domWindow()->GetFrame() || | 44 if (!ToDocument(execution_context)->domWindow()->GetFrame() || |
| 46 !ToDocument(execution_context)->GetFrame()->IsMainFrame()) { | 45 !ToDocument(execution_context)->GetFrame()->IsMainFrame()) { |
| (...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 | 167 |
| 169 RequestAddConfiguration(); | 168 RequestAddConfiguration(); |
| 170 } | 169 } |
| 171 | 170 |
| 172 void Sensor::OnSensorReadingChanged() { | 171 void Sensor::OnSensorReadingChanged() { |
| 173 if (state_ != Sensor::SensorState::kActivated) | 172 if (state_ != Sensor::SensorState::kActivated) |
| 174 return; | 173 return; |
| 175 | 174 |
| 176 // Return if reading update is already scheduled or the cached | 175 // Return if reading update is already scheduled or the cached |
| 177 // reading is up-to-date. | 176 // reading is up-to-date. |
| 178 if (pending_reading_update_) | 177 if (pending_reading_update_.IsActive()) |
| 179 return; | 178 return; |
| 180 | 179 |
| 181 pending_reading_update_ = true; | |
| 182 | |
| 183 double elapsedTime = sensor_proxy_->reading().timestamp - reading_.timestamp; | 180 double elapsedTime = sensor_proxy_->reading().timestamp - reading_.timestamp; |
| 184 DCHECK_GT(elapsedTime, 0.0); | 181 DCHECK_GT(elapsedTime, 0.0); |
| 185 | 182 |
| 186 DCHECK_GT(configuration_->frequency, 0.0); | 183 DCHECK_GT(configuration_->frequency, 0.0); |
| 187 double waitingTime = 1 / configuration_->frequency - elapsedTime; | 184 double waitingTime = 1 / configuration_->frequency - elapsedTime; |
| 188 | 185 |
| 189 // Negative or zero 'waitingTime' means that polling period has elapsed. | 186 // Negative or zero 'waitingTime' means that polling period has elapsed. |
| 190 // We also avoid scheduling if the elapsed time is slightly behind the | 187 // We also avoid scheduling if the elapsed time is slightly behind the |
| 191 // polling period. | 188 // polling period. |
| 192 auto sensor_reading_changed = | 189 auto sensor_reading_changed = |
| 193 WTF::Bind(&Sensor::UpdateReading, WrapWeakPersistent(this)); | 190 WTF::Bind(&Sensor::UpdateReading, WrapWeakPersistent(this)); |
| 194 if (waitingTime < kMinWaitingInterval) { | 191 if (waitingTime < kMinWaitingInterval) { |
| 195 // Invoke JS callbacks in a different callchain to obviate | 192 // Invoke JS callbacks in a different callchain to obviate |
| 196 // possible modifications of SensorProxy::observers_ container | 193 // possible modifications of SensorProxy::observers_ container |
| 197 // while it is being iterated through. | 194 // while it is being iterated through. |
| 198 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) | 195 pending_reading_update_ = |
| 199 ->PostTask(BLINK_FROM_HERE, std::move(sensor_reading_changed)); | 196 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) |
| 197 ->PostCancellableTask(BLINK_FROM_HERE, |
| 198 std::move(sensor_reading_changed)); |
| 200 } else { | 199 } else { |
| 201 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) | 200 pending_reading_update_ = |
| 202 ->PostDelayedTask(BLINK_FROM_HERE, std::move(sensor_reading_changed), | 201 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) |
| 203 WTF::TimeDelta::FromSecondsD(waitingTime)); | 202 ->PostDelayedCancellableTask( |
| 203 BLINK_FROM_HERE, std::move(sensor_reading_changed), |
| 204 WTF::TimeDelta::FromSecondsD(waitingTime)); |
| 204 } | 205 } |
| 205 } | 206 } |
| 206 | 207 |
| 207 void Sensor::OnSensorError(ExceptionCode code, | 208 void Sensor::OnSensorError(ExceptionCode code, |
| 208 const String& sanitized_message, | 209 const String& sanitized_message, |
| 209 const String& unsanitized_message) { | 210 const String& unsanitized_message) { |
| 210 HandleError(code, sanitized_message, unsanitized_message); | 211 HandleError(code, sanitized_message, unsanitized_message); |
| 211 } | 212 } |
| 212 | 213 |
| 213 void Sensor::OnAddConfigurationRequestCompleted(bool result) { | 214 void Sensor::OnAddConfigurationRequestCompleted(bool result) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 sensor_proxy_->Initialize(); | 246 sensor_proxy_->Initialize(); |
| 246 | 247 |
| 247 sensor_proxy_->AddObserver(this); | 248 sensor_proxy_->AddObserver(this); |
| 248 UpdateState(SensorState::kActivating); | 249 UpdateState(SensorState::kActivating); |
| 249 } | 250 } |
| 250 | 251 |
| 251 void Sensor::StopListening() { | 252 void Sensor::StopListening() { |
| 252 if (state_ == SensorState::kIdle) | 253 if (state_ == SensorState::kIdle) |
| 253 return; | 254 return; |
| 254 | 255 |
| 256 pending_reading_update_.Cancel(); |
| 257 |
| 255 DCHECK(sensor_proxy_); | 258 DCHECK(sensor_proxy_); |
| 256 if (sensor_proxy_->IsInitialized()) { | 259 if (sensor_proxy_->IsInitialized()) { |
| 257 DCHECK(configuration_); | 260 DCHECK(configuration_); |
| 258 sensor_proxy_->RemoveConfiguration(configuration_->Clone()); | 261 sensor_proxy_->RemoveConfiguration(configuration_->Clone()); |
| 259 } | 262 } |
| 260 | 263 |
| 261 sensor_proxy_->RemoveObserver(this); | 264 sensor_proxy_->RemoveObserver(this); |
| 262 UpdateState(Sensor::SensorState::kIdle); | 265 UpdateState(Sensor::SensorState::kIdle); |
| 263 } | 266 } |
| 264 | 267 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 292 DOMException::Create(code, sanitized_message, unsanitized_message); | 295 DOMException::Create(code, sanitized_message, unsanitized_message); |
| 293 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) | 296 TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext()) |
| 294 ->PostTask(BLINK_FROM_HERE, | 297 ->PostTask(BLINK_FROM_HERE, |
| 295 WTF::Bind(&Sensor::NotifyError, WrapWeakPersistent(this), | 298 WTF::Bind(&Sensor::NotifyError, WrapWeakPersistent(this), |
| 296 WrapPersistent(error))); | 299 WrapPersistent(error))); |
| 297 } | 300 } |
| 298 } | 301 } |
| 299 | 302 |
| 300 void Sensor::UpdateReading() { | 303 void Sensor::UpdateReading() { |
| 301 reading_ = sensor_proxy_->reading(); | 304 reading_ = sensor_proxy_->reading(); |
| 302 pending_reading_update_ = false; | |
| 303 DispatchEvent(Event::Create(EventTypeNames::change)); | 305 DispatchEvent(Event::Create(EventTypeNames::change)); |
| 304 } | 306 } |
| 305 | 307 |
| 306 void Sensor::NotifyOnActivate() { | 308 void Sensor::NotifyOnActivate() { |
| 307 DispatchEvent(Event::Create(EventTypeNames::activate)); | 309 DispatchEvent(Event::Create(EventTypeNames::activate)); |
| 308 } | 310 } |
| 309 | 311 |
| 310 void Sensor::NotifyError(DOMException* error) { | 312 void Sensor::NotifyError(DOMException* error) { |
| 311 DispatchEvent( | 313 DispatchEvent( |
| 312 SensorErrorEvent::Create(EventTypeNames::error, std::move(error))); | 314 SensorErrorEvent::Create(EventTypeNames::error, std::move(error))); |
| 313 } | 315 } |
| 314 | 316 |
| 315 bool Sensor::CanReturnReadings() const { | 317 bool Sensor::CanReturnReadings() const { |
| 316 if (!IsActivated()) | 318 if (!IsActivated()) |
| 317 return false; | 319 return false; |
| 318 DCHECK(sensor_proxy_); | 320 DCHECK(sensor_proxy_); |
| 319 return sensor_proxy_->reading().timestamp != 0.0; | 321 return sensor_proxy_->reading().timestamp != 0.0; |
| 320 } | 322 } |
| 321 | 323 |
| 322 } // namespace blink | 324 } // namespace blink |
| OLD | NEW |