| Index: third_party/WebKit/Source/modules/sensor/Sensor.cpp
|
| diff --git a/third_party/WebKit/Source/modules/sensor/Sensor.cpp b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
|
| index 9f3de1ce7116d82559c6c0eec76461218cb750b4..7581a9ea08c7715856e5d5ca6e19d2d35f460ec9 100644
|
| --- a/third_party/WebKit/Source/modules/sensor/Sensor.cpp
|
| +++ b/third_party/WebKit/Source/modules/sensor/Sensor.cpp
|
| @@ -58,24 +58,10 @@ Sensor::Sensor(ExecutionContext* execution_context,
|
| Sensor::~Sensor() = default;
|
|
|
| void Sensor::start() {
|
| - if (state_ != Sensor::SensorState::kIdle)
|
| - return;
|
| -
|
| - InitSensorProxyIfNeeded();
|
| - if (!sensor_proxy_) {
|
| - ReportError(kInvalidStateError,
|
| - "The Sensor is no longer associated to a frame.");
|
| - return;
|
| - }
|
| -
|
| - last_update_timestamp_ = WTF::MonotonicallyIncreasingTime();
|
| StartListening();
|
| }
|
|
|
| void Sensor::stop() {
|
| - if (state_ == Sensor::SensorState::kIdle)
|
| - return;
|
| -
|
| StopListening();
|
| }
|
|
|
| @@ -166,16 +152,14 @@ void Sensor::InitSensorProxyIfNeeded() {
|
| }
|
|
|
| void Sensor::ContextDestroyed(ExecutionContext*) {
|
| - if (state_ == Sensor::SensorState::kActivated ||
|
| - state_ == Sensor::SensorState::kActivating)
|
| - StopListening();
|
| + StopListening();
|
| }
|
|
|
| void Sensor::OnSensorInitialized() {
|
| if (state_ != Sensor::SensorState::kActivating)
|
| return;
|
|
|
| - StartListening();
|
| + RequestAddConfiguration();
|
| }
|
|
|
| void Sensor::NotifySensorChanged(double timestamp) {
|
| @@ -194,78 +178,91 @@ void Sensor::NotifySensorChanged(double timestamp) {
|
| void Sensor::OnSensorError(ExceptionCode code,
|
| const String& sanitized_message,
|
| const String& unsanitized_message) {
|
| - ReportError(code, sanitized_message, unsanitized_message);
|
| + HandleError(code, sanitized_message, unsanitized_message);
|
| }
|
|
|
| -void Sensor::OnStartRequestCompleted(bool result) {
|
| +void Sensor::OnAddConfigurationRequestCompleted(bool result) {
|
| if (state_ != SensorState::kActivating)
|
| return;
|
|
|
| if (!result) {
|
| - ReportError(kNotReadableError, "start() call has failed.");
|
| + HandleError(kNotReadableError, "start() call has failed.");
|
| return;
|
| }
|
|
|
| + // The initial value for m_lastUpdateTimestamp is set to current time,
|
| + // so that the first reading update will be notified considering the given
|
| + // frequency hint.
|
| + last_update_timestamp_ = WTF::MonotonicallyIncreasingTime();
|
| +
|
| UpdateState(Sensor::SensorState::kActivated);
|
| +
|
| + if (GetExecutionContext()) {
|
| + TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext())
|
| + ->PostTask(BLINK_FROM_HERE, WTF::Bind(&Sensor::NotifyOnActivate,
|
| + WrapWeakPersistent(this)));
|
| + }
|
| }
|
|
|
| void Sensor::StartListening() {
|
| - DCHECK(sensor_proxy_);
|
| - UpdateState(Sensor::SensorState::kActivating);
|
| + if (state_ != SensorState::kIdle)
|
| + return;
|
|
|
| - sensor_proxy_->AddObserver(this);
|
| - if (!sensor_proxy_->IsInitialized()) {
|
| - sensor_proxy_->Initialize();
|
| + InitSensorProxyIfNeeded();
|
| + if (!sensor_proxy_) {
|
| + HandleError(kInvalidStateError,
|
| + "The Sensor is no longer associated to a frame.");
|
| return;
|
| }
|
|
|
| - if (!configuration_) {
|
| - configuration_ = CreateSensorConfig();
|
| - DCHECK(configuration_);
|
| - DCHECK(configuration_->frequency > 0 &&
|
| - configuration_->frequency <=
|
| - SensorConfiguration::kMaxAllowedFrequency);
|
| - }
|
| + if (sensor_proxy_->IsInitialized())
|
| + RequestAddConfiguration();
|
| + else
|
| + sensor_proxy_->Initialize();
|
|
|
| - auto start_callback =
|
| - WTF::Bind(&Sensor::OnStartRequestCompleted, WrapWeakPersistent(this));
|
| - sensor_proxy_->AddConfiguration(configuration_->Clone(),
|
| - std::move(start_callback));
|
| + sensor_proxy_->AddObserver(this);
|
| + UpdateState(SensorState::kActivating);
|
| }
|
|
|
| void Sensor::StopListening() {
|
| - DCHECK(sensor_proxy_);
|
| - UpdateState(Sensor::SensorState::kIdle);
|
| + if (state_ == SensorState::kIdle)
|
| + return;
|
|
|
| + DCHECK(sensor_proxy_);
|
| if (sensor_proxy_->IsInitialized()) {
|
| DCHECK(configuration_);
|
| sensor_proxy_->RemoveConfiguration(configuration_->Clone());
|
| }
|
| +
|
| sensor_proxy_->RemoveObserver(this);
|
| + UpdateState(Sensor::SensorState::kIdle);
|
| }
|
|
|
| -void Sensor::UpdateState(Sensor::SensorState new_state) {
|
| - if (new_state == state_)
|
| - return;
|
| -
|
| - if (new_state == SensorState::kActivated && GetExecutionContext()) {
|
| - DCHECK_EQ(SensorState::kActivating, state_);
|
| - // The initial value for m_lastUpdateTimestamp is set to current time,
|
| - // so that the first reading update will be notified considering the given
|
| - // frequency hint.
|
| - last_update_timestamp_ = WTF::MonotonicallyIncreasingTime();
|
| - TaskRunnerHelper::Get(TaskType::kSensor, GetExecutionContext())
|
| - ->PostTask(BLINK_FROM_HERE, WTF::Bind(&Sensor::NotifyOnActivate,
|
| - WrapWeakPersistent(this)));
|
| +void Sensor::RequestAddConfiguration() {
|
| + if (!configuration_) {
|
| + configuration_ = CreateSensorConfig();
|
| + DCHECK(configuration_);
|
| + DCHECK(configuration_->frequency > 0 &&
|
| + configuration_->frequency <=
|
| + SensorConfiguration::kMaxAllowedFrequency);
|
| }
|
|
|
| + DCHECK(sensor_proxy_);
|
| + sensor_proxy_->AddConfiguration(
|
| + configuration_->Clone(),
|
| + WTF::Bind(&Sensor::OnAddConfigurationRequestCompleted,
|
| + WrapWeakPersistent(this)));
|
| +}
|
| +
|
| +void Sensor::UpdateState(Sensor::SensorState new_state) {
|
| state_ = new_state;
|
| }
|
|
|
| -void Sensor::ReportError(ExceptionCode code,
|
| +void Sensor::HandleError(ExceptionCode code,
|
| const String& sanitized_message,
|
| const String& unsanitized_message) {
|
| - UpdateState(SensorState::kIdle);
|
| + StopListening();
|
| +
|
| if (GetExecutionContext()) {
|
| auto error =
|
| DOMException::Create(code, sanitized_message, unsanitized_message);
|
|
|