Commit 5a177141 authored by Johann's avatar Johann Committed by Commit Bot

DeviceOrientationEventPump: simplify fallback logic

Change-Id: Ie9b1c8d4b928e7328ecd8efd2139c0ba3f543261
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2217752Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Commit-Queue: Johann Koenig <johannkoenig@google.com>
Cr-Commit-Position: refs/heads/master@{#773575}
parent 2aef8221
...@@ -47,9 +47,7 @@ const double DeviceOrientationEventPump::kOrientationThreshold = 0.1; ...@@ -47,9 +47,7 @@ const double DeviceOrientationEventPump::kOrientationThreshold = 0.1;
DeviceOrientationEventPump::DeviceOrientationEventPump( DeviceOrientationEventPump::DeviceOrientationEventPump(
scoped_refptr<base::SingleThreadTaskRunner> task_runner, scoped_refptr<base::SingleThreadTaskRunner> task_runner,
bool absolute) bool absolute)
: DeviceSensorEventPump(std::move(task_runner)), : DeviceSensorEventPump(std::move(task_runner)), absolute_(absolute) {
absolute_(absolute),
fall_back_to_absolute_orientation_sensor_(!absolute) {
relative_orientation_sensor_ = MakeGarbageCollected<DeviceSensorEntry>( relative_orientation_sensor_ = MakeGarbageCollected<DeviceSensorEntry>(
this, device::mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES); this, device::mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES);
absolute_orientation_sensor_ = MakeGarbageCollected<DeviceSensorEntry>( absolute_orientation_sensor_ = MakeGarbageCollected<DeviceSensorEntry>(
...@@ -97,8 +95,10 @@ void DeviceOrientationEventPump::SendStartMessage(LocalFrame& frame) { ...@@ -97,8 +95,10 @@ void DeviceOrientationEventPump::SendStartMessage(LocalFrame& frame) {
if (absolute_) { if (absolute_) {
absolute_orientation_sensor_->Start(sensor_provider_.get()); absolute_orientation_sensor_->Start(sensor_provider_.get());
} else { } else {
fall_back_to_absolute_orientation_sensor_ = true; // Start() is asynchronous. Therefore IsConnected() can not be checked right
should_suspend_absolute_orientation_sensor_ = false; // away to determine if we should attempt to fall back to
// absolute_orientation_sensor_.
attempted_to_fall_back_to_absolute_orientation_sensor_ = false;
relative_orientation_sensor_->Start(sensor_provider_.get()); relative_orientation_sensor_->Start(sensor_provider_.get());
} }
} }
...@@ -109,22 +109,8 @@ void DeviceOrientationEventPump::SendStopMessage() { ...@@ -109,22 +109,8 @@ void DeviceOrientationEventPump::SendStopMessage() {
// the event listener is more rare than the page visibility changing, // the event listener is more rare than the page visibility changing,
// Sensor::Suspend() is used to optimize this case for not doing extra work. // Sensor::Suspend() is used to optimize this case for not doing extra work.
relative_orientation_sensor_->Stop();
// This is needed in case we fallback to using the absolute orientation
// sensor. In this case, the relative orientation sensor is marked as
// SensorState::SHOULD_SUSPEND, and if the relative orientation sensor
// is not available, the absolute orientation sensor should also be marked as
// SensorState::SHOULD_SUSPEND, but only after the
// absolute_orientation_sensor_.Start() is called for initializing
// the absolute orientation sensor in
// DeviceOrientationEventPump::DidStartIfPossible().
if (relative_orientation_sensor_->state() ==
DeviceSensorEntry::State::SHOULD_SUSPEND &&
fall_back_to_absolute_orientation_sensor_) {
should_suspend_absolute_orientation_sensor_ = true;
}
absolute_orientation_sensor_->Stop(); absolute_orientation_sensor_->Stop();
relative_orientation_sensor_->Stop();
// Reset the cached data because DeviceOrientationDispatcher resets its // Reset the cached data because DeviceOrientationDispatcher resets its
// data when stopping. If we don't reset here as well, then when starting back // data when stopping. If we don't reset here as well, then when starting back
...@@ -148,18 +134,23 @@ void DeviceOrientationEventPump::FireEvent(TimerBase*) { ...@@ -148,18 +134,23 @@ void DeviceOrientationEventPump::FireEvent(TimerBase*) {
} }
void DeviceOrientationEventPump::DidStartIfPossible() { void DeviceOrientationEventPump::DidStartIfPossible() {
if (!absolute_ && !relative_orientation_sensor_->IsConnected() && if (!absolute_ && sensor_provider_ &&
fall_back_to_absolute_orientation_sensor_ && sensor_provider_) { !relative_orientation_sensor_->IsConnected() &&
// When relative orientation sensor is not available fall back to using !attempted_to_fall_back_to_absolute_orientation_sensor_) {
// the absolute orientation sensor but only on the first failure. // If relative_orientation_sensor_ was requested but was not able to connect
fall_back_to_absolute_orientation_sensor_ = false; // then fall back to using absolute_orientation_sensor_.
attempted_to_fall_back_to_absolute_orientation_sensor_ = true;
absolute_orientation_sensor_->Start(sensor_provider_.get()); absolute_orientation_sensor_->Start(sensor_provider_.get());
if (should_suspend_absolute_orientation_sensor_) { if (relative_orientation_sensor_->state() ==
// The absolute orientation sensor needs to be marked as DeviceSensorEntry::State::SHOULD_SUSPEND) {
// SensorState::SUSPENDED when it is successfully initialized. // If SendStopMessage() was called before the OnSensorCreated() callback
// registered that relative_orientation_sensor_ was not able to connect
// then absolute_orientation_sensor_ needs to be Stop()'d so that it
// matches the relative_orientation_sensor_ state.
absolute_orientation_sensor_->Stop(); absolute_orientation_sensor_->Stop();
should_suspend_absolute_orientation_sensor_ = false;
} }
// Start() is asynchronous. Give the OnSensorCreated() callback time to fire
// before calling DeviceSensorEventPump::DidStartIfPossible().
return; return;
} }
DeviceSensorEventPump::DidStartIfPossible(); DeviceSensorEventPump::DidStartIfPossible();
......
...@@ -65,8 +65,9 @@ class MODULES_EXPORT DeviceOrientationEventPump ...@@ -65,8 +65,9 @@ class MODULES_EXPORT DeviceOrientationEventPump
bool ShouldFireEvent(const DeviceOrientationData* data) const; bool ShouldFireEvent(const DeviceOrientationData* data) const;
bool absolute_; bool absolute_;
bool fall_back_to_absolute_orientation_sensor_; // If relative_orientation_sensor_ is requested but fails to initialize then
bool should_suspend_absolute_orientation_sensor_ = false; // attempt to fall back to absolute_orientation_sensor_ once.
bool attempted_to_fall_back_to_absolute_orientation_sensor_;
Member<DeviceOrientationData> data_; Member<DeviceOrientationData> data_;
Member<PlatformEventController> controller_; Member<PlatformEventController> controller_;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment