diff options
Diffstat (limited to 'chromium/third_party/blink/renderer/modules/device_orientation')
23 files changed, 653 insertions, 915 deletions
diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/BUILD.gn b/chromium/third_party/blink/renderer/modules/device_orientation/BUILD.gn index 399ac3e87e6..89112176e96 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/BUILD.gn +++ b/chromium/third_party/blink/renderer/modules/device_orientation/BUILD.gn @@ -12,8 +12,6 @@ blink_modules_sources("device_orientation") { "device_motion_controller.h", "device_motion_data.cc", "device_motion_data.h", - "device_motion_dispatcher.cc", - "device_motion_dispatcher.h", "device_motion_event.cc", "device_motion_event.h", "device_motion_event_pump.cc", @@ -24,8 +22,6 @@ blink_modules_sources("device_orientation") { "device_orientation_controller.h", "device_orientation_data.cc", "device_orientation_data.h", - "device_orientation_dispatcher.cc", - "device_orientation_dispatcher.h", "device_orientation_event.cc", "device_orientation_event.h", "device_orientation_event_pump.cc", @@ -38,8 +34,4 @@ blink_modules_sources("device_orientation") { "dom_window_device_motion.h", "dom_window_device_orientation.h", ] - - deps = [ - "//services/device/public/cpp/generic_sensor:shared_with_blink", - ] } diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/DEPS b/chromium/third_party/blink/renderer/modules/device_orientation/DEPS index 71a097e78ee..7c1b789abcd 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/DEPS +++ b/chromium/third_party/blink/renderer/modules/device_orientation/DEPS @@ -1,11 +1,6 @@ include_rules = [ "+base/run_loop.h", "+mojo/public/cpp/bindings/binding.h", - - # NOTE: These files are POD structs used to interpret shared memory across - # the Device Sensors implementation and the Blink client. - "+services/device/public/cpp/generic_sensor/orientation_data.h", - "+services/device/public/mojom/sensor.mojom-blink.h", "+services/device/public/mojom/sensor_provider.mojom-blink.h", "+services/device/public/cpp/test/fake_sensor_and_provider.h", diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_controller.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_controller.cc index f1f4dbf278d..01c1ade0c72 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_controller.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_controller.cc @@ -10,8 +10,8 @@ #include "third_party/blink/renderer/core/frame/hosts_using_features.h" #include "third_party/blink/renderer/core/frame/settings.h" #include "third_party/blink/renderer/modules/device_orientation/device_motion_data.h" -#include "third_party/blink/renderer/modules/device_orientation/device_motion_dispatcher.h" #include "third_party/blink/renderer/modules/device_orientation/device_motion_event.h" +#include "third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.h" #include "third_party/blink/renderer/modules/device_orientation/device_orientation_controller.h" #include "third_party/blink/renderer/modules/event_modules.h" #include "third_party/blink/renderer/platform/weborigin/security_origin.h" @@ -78,21 +78,33 @@ void DeviceMotionController::DidAddEventListener( } bool DeviceMotionController::HasLastData() { - return DeviceMotionDispatcher::Instance().LatestDeviceMotionData(); + return motion_event_pump_ + ? motion_event_pump_->LatestDeviceMotionData() != nullptr + : false; } void DeviceMotionController::RegisterWithDispatcher() { - DeviceMotionDispatcher::Instance().AddController(this); + if (!motion_event_pump_) { + LocalFrame* frame = GetDocument().GetFrame(); + if (!frame) + return; + scoped_refptr<base::SingleThreadTaskRunner> task_runner = + frame->GetTaskRunner(TaskType::kSensor); + motion_event_pump_ = new DeviceMotionEventPump(task_runner); + } + motion_event_pump_->AddController(this); } void DeviceMotionController::UnregisterWithDispatcher() { - DeviceMotionDispatcher::Instance().RemoveController(this); + if (motion_event_pump_) + motion_event_pump_->RemoveController(this); } Event* DeviceMotionController::LastEvent() const { return DeviceMotionEvent::Create( EventTypeNames::devicemotion, - DeviceMotionDispatcher::Instance().LatestDeviceMotionData()); + motion_event_pump_ ? motion_event_pump_->LatestDeviceMotionData() + : nullptr); } bool DeviceMotionController::IsNullEvent(Event* event) const { @@ -106,6 +118,7 @@ const AtomicString& DeviceMotionController::EventTypeName() const { void DeviceMotionController::Trace(blink::Visitor* visitor) { DeviceSingleWindowEventController::Trace(visitor); + visitor->Trace(motion_event_pump_); Supplement<Document>::Trace(visitor); } diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_controller.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_controller.h index 107bb0d4c2f..e8b62cca1e1 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_controller.h +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_controller.h @@ -12,6 +12,7 @@ namespace blink { class Event; +class DeviceMotionEventPump; class MODULES_EXPORT DeviceMotionController final : public DeviceSingleWindowEventController, @@ -34,7 +35,7 @@ class MODULES_EXPORT DeviceMotionController final private: explicit DeviceMotionController(Document&); - // Inherited from DeviceEventControllerBase. + // Inherited from PlatformEventController. void RegisterWithDispatcher() override; void UnregisterWithDispatcher() override; bool HasLastData() override; @@ -43,6 +44,8 @@ class MODULES_EXPORT DeviceMotionController final Event* LastEvent() const override; const AtomicString& EventTypeName() const override; bool IsNullEvent(Event*) const override; + + Member<DeviceMotionEventPump> motion_event_pump_; }; } // namespace blink diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_dispatcher.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_dispatcher.cc deleted file mode 100644 index 5b46504a08f..00000000000 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_dispatcher.cc +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright (C) 2013 Google Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "third_party/blink/renderer/modules/device_orientation/device_motion_dispatcher.h" - -#include "third_party/blink/renderer/modules/device_orientation/device_motion_controller.h" -#include "third_party/blink/renderer/modules/device_orientation/device_motion_data.h" -#include "third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.h" - -namespace blink { - -DeviceMotionDispatcher& DeviceMotionDispatcher::Instance() { - DEFINE_STATIC_LOCAL(DeviceMotionDispatcher, device_motion_dispatcher, - (new DeviceMotionDispatcher)); - return device_motion_dispatcher; -} - -DeviceMotionDispatcher::DeviceMotionDispatcher() = default; - -DeviceMotionDispatcher::~DeviceMotionDispatcher() = default; - -void DeviceMotionDispatcher::Trace(blink::Visitor* visitor) { - visitor->Trace(last_device_motion_data_); - PlatformEventDispatcher::Trace(visitor); -} - -void DeviceMotionDispatcher::StartListening(LocalFrame* frame) { - // TODO(crbug.com/850619): ensure a valid frame is passed - if (!frame) - return; - if (!event_pump_) { - event_pump_ = std::make_unique<DeviceMotionEventPump>( - frame->GetTaskRunner(TaskType::kSensor)); - } - event_pump_->Start(frame, this); -} - -void DeviceMotionDispatcher::StopListening() { - if (event_pump_) - event_pump_->Stop(); - last_device_motion_data_.Clear(); -} - -void DeviceMotionDispatcher::DidChangeDeviceMotion(DeviceMotionData* motion) { - last_device_motion_data_ = motion; - NotifyControllers(); -} - -const DeviceMotionData* DeviceMotionDispatcher::LatestDeviceMotionData() { - return last_device_motion_data_.Get(); -} - -} // namespace blink diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_dispatcher.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_dispatcher.h deleted file mode 100644 index 70e76a1c15d..00000000000 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_dispatcher.h +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright (C) 2013 Google Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_DEVICE_ORIENTATION_DEVICE_MOTION_DISPATCHER_H_ -#define THIRD_PARTY_BLINK_RENDERER_MODULES_DEVICE_ORIENTATION_DEVICE_MOTION_DISPATCHER_H_ - -#include "base/memory/scoped_refptr.h" -#include "third_party/blink/public/platform/modules/device_orientation/web_device_motion_listener.h" -#include "third_party/blink/renderer/core/frame/platform_event_dispatcher.h" -#include "third_party/blink/renderer/platform/heap/handle.h" - -namespace blink { - -class DeviceMotionData; -class DeviceMotionEventPump; - -// This class listens to device motion data and notifies all registered -// controllers. -class DeviceMotionDispatcher final - : public GarbageCollectedFinalized<DeviceMotionDispatcher>, - public PlatformEventDispatcher, - public WebDeviceMotionListener { - USING_GARBAGE_COLLECTED_MIXIN(DeviceMotionDispatcher); - - public: - static DeviceMotionDispatcher& Instance(); - ~DeviceMotionDispatcher() override; - - // Note that the returned object is owned by this class. - const DeviceMotionData* LatestDeviceMotionData(); - - // Inherited from WebDeviceMotionListener. - void DidChangeDeviceMotion(DeviceMotionData*) override; - - void Trace(blink::Visitor*) override; - - private: - DeviceMotionDispatcher(); - - // Inherited from PlatformEventDispatcher. - void StartListening(LocalFrame*) override; - void StopListening() override; - - Member<DeviceMotionData> last_device_motion_data_; - std::unique_ptr<DeviceMotionEventPump> event_pump_; -}; - -} // namespace blink - -#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_DEVICE_ORIENTATION_DEVICE_MOTION_DISPATCHER_H_ diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.cc index 06aab06b84a..0ef859cca95 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.cc @@ -20,11 +20,9 @@ constexpr double kDefaultPumpDelayMilliseconds = namespace blink { -template class DeviceSensorEventPump<blink::WebDeviceMotionListener>; - DeviceMotionEventPump::DeviceMotionEventPump( scoped_refptr<base::SingleThreadTaskRunner> task_runner) - : DeviceSensorEventPump<blink::WebDeviceMotionListener>(task_runner), + : DeviceSensorEventPump(task_runner), accelerometer_(this, device::mojom::blink::SensorType::ACCELEROMETER), linear_acceleration_sensor_( this, @@ -35,6 +33,22 @@ DeviceMotionEventPump::~DeviceMotionEventPump() { StopIfObserving(); } +DeviceMotionData* DeviceMotionEventPump::LatestDeviceMotionData() { + return data_.Get(); +} + +void DeviceMotionEventPump::Trace(blink::Visitor* visitor) { + visitor->Trace(data_); + PlatformEventDispatcher::Trace(visitor); +} + +void DeviceMotionEventPump::StartListening(LocalFrame* frame) { + // TODO(crbug.com/850619): ensure a valid frame is passed + if (!frame) + return; + Start(frame); +} + void DeviceMotionEventPump::SendStartMessage(LocalFrame* frame) { if (!sensor_provider_) { DCHECK(frame); @@ -43,7 +57,7 @@ void DeviceMotionEventPump::SendStartMessage(LocalFrame* frame) { mojo::MakeRequest(&sensor_provider_)); sensor_provider_.set_connection_error_handler( WTF::Bind(&DeviceSensorEventPump::HandleSensorProviderError, - WTF::Unretained(this))); + WrapPersistent(this))); } accelerometer_.Start(sensor_provider_.get()); @@ -51,6 +65,11 @@ void DeviceMotionEventPump::SendStartMessage(LocalFrame* frame) { gyroscope_.Start(sensor_provider_.get()); } +void DeviceMotionEventPump::StopListening() { + Stop(); + data_.Clear(); +} + void DeviceMotionEventPump::SendStopMessage() { // SendStopMessage() gets called both when the page visibility changes and if // all device motion event listeners are unregistered. Since removing the @@ -63,13 +82,13 @@ void DeviceMotionEventPump::SendStopMessage() { } void DeviceMotionEventPump::FireEvent(TimerBase*) { - DCHECK(listener()); - DeviceMotionData* data = GetDataFromSharedMemory(); // data is null if not all sensors are active - if (data) - listener()->DidChangeDeviceMotion(data); + if (data) { + data_ = data; + NotifyControllers(); + } } bool DeviceMotionEventPump::SensorsReadyOrErrored() const { diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.h index 4d616365b3d..751d5660bc3 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.h +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.h @@ -6,20 +6,30 @@ #define THIRD_PARTY_BLINK_RENDERER_MODULES_DEVICE_ORIENTATION_DEVICE_MOTION_EVENT_PUMP_H_ #include "base/macros.h" -#include "third_party/blink/public/platform/modules/device_orientation/web_device_motion_listener.h" +#include "third_party/blink/renderer/core/frame/platform_event_dispatcher.h" #include "third_party/blink/renderer/modules/device_orientation/device_sensor_event_pump.h" #include "third_party/blink/renderer/modules/modules_export.h" +#include "third_party/blink/renderer/platform/heap/handle.h" namespace blink { class DeviceMotionData; class MODULES_EXPORT DeviceMotionEventPump - : public DeviceSensorEventPump<blink::WebDeviceMotionListener> { + : public GarbageCollectedFinalized<DeviceMotionEventPump>, + public DeviceSensorEventPump, + public PlatformEventDispatcher { + USING_GARBAGE_COLLECTED_MIXIN(DeviceMotionEventPump); + public: explicit DeviceMotionEventPump(scoped_refptr<base::SingleThreadTaskRunner>); ~DeviceMotionEventPump() override; + // Note that the returned object is owned by this class. + DeviceMotionData* LatestDeviceMotionData(); + + void Trace(blink::Visitor*) override; + // DeviceSensorEventPump: void SendStartMessage(LocalFrame* frame) override; void SendStopMessage() override; @@ -35,11 +45,17 @@ class MODULES_EXPORT DeviceMotionEventPump private: friend class DeviceMotionEventPumpTest; + // Inherited from PlatformEventDispatcher. + void StartListening(LocalFrame*) override; + void StopListening() override; + // DeviceSensorEventPump: bool SensorsReadyOrErrored() const override; DeviceMotionData* GetDataFromSharedMemory(); + Member<DeviceMotionData> data_; + DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPump); }; diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump_unittest.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump_unittest.cc index 868c4f6cecf..a9d5238ec33 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump_unittest.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_motion_event_pump_unittest.cc @@ -9,8 +9,8 @@ #include "base/run_loop.h" #include "services/device/public/cpp/test/fake_sensor_and_provider.h" #include "testing/gtest/include/gtest/gtest.h" -#include "third_party/blink/public/platform/modules/device_orientation/web_device_motion_listener.h" #include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h" +#include "third_party/blink/renderer/core/frame/platform_event_controller.h" #include "third_party/blink/renderer/modules/device_orientation/device_motion_data.h" #include "third_party/blink/renderer/modules/device_orientation/device_motion_event_pump.h" #include "third_party/blink/renderer/platform/heap/persistent.h" @@ -20,16 +20,24 @@ namespace blink { using device::FakeSensorProvider; -class MockDeviceMotionListener : public blink::WebDeviceMotionListener { +class MockDeviceMotionController final + : public GarbageCollectedFinalized<MockDeviceMotionController>, + public PlatformEventController { + USING_GARBAGE_COLLECTED_MIXIN(MockDeviceMotionController); + public: - MockDeviceMotionListener() - : did_change_device_motion_(false), number_of_events_(0) { - data_ = DeviceMotionData::Create(); + explicit MockDeviceMotionController(DeviceMotionEventPump* motion_pump) + : PlatformEventController(nullptr), + did_change_device_motion_(false), + motion_pump_(motion_pump) {} + ~MockDeviceMotionController() override {} + + void Trace(Visitor* visitor) override { + PlatformEventController::Trace(visitor); + visitor->Trace(motion_pump_); } - ~MockDeviceMotionListener() override {} - void DidChangeDeviceMotion(DeviceMotionData* data) override { - data_ = data; + void DidUpdateData() override { did_change_device_motion_ = true; ++number_of_events_; } @@ -38,27 +46,26 @@ class MockDeviceMotionListener : public blink::WebDeviceMotionListener { int number_of_events() const { return number_of_events_; } - const DeviceMotionData* data() const { return data_.Get(); } + void RegisterWithDispatcher() override { motion_pump_->AddController(this); } - private: - bool did_change_device_motion_; - int number_of_events_; - Persistent<DeviceMotionData> data_; + bool HasLastData() override { return motion_pump_->LatestDeviceMotionData(); } - DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionListener); -}; + void UnregisterWithDispatcher() override { + motion_pump_->RemoveController(this); + } -class DeviceMotionEventPumpForTesting : public DeviceMotionEventPump { - public: - explicit DeviceMotionEventPumpForTesting( - scoped_refptr<base::SingleThreadTaskRunner> task_runner) - : DeviceMotionEventPump(task_runner) {} - ~DeviceMotionEventPumpForTesting() override {} + const DeviceMotionData* data() { + return motion_pump_->LatestDeviceMotionData(); + }; - int pump_delay_microseconds() const { return kDefaultPumpDelayMicroseconds; } + DeviceMotionEventPump* motion_pump() { return motion_pump_.Get(); } private: - DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpForTesting); + bool did_change_device_motion_; + int number_of_events_; + Member<DeviceMotionEventPump> motion_pump_; + + DISALLOW_COPY_AND_ASSIGN(MockDeviceMotionController); }; class DeviceMotionEventPumpTest : public testing::Test { @@ -67,40 +74,43 @@ class DeviceMotionEventPumpTest : public testing::Test { protected: void SetUp() override { - motion_pump_.reset(new DeviceMotionEventPumpForTesting( - base::ThreadTaskRunnerHandle::Get())); device::mojom::SensorProviderPtrInfo sensor_provider_ptr_info; sensor_provider_.Bind(mojo::MakeRequest(&sensor_provider_ptr_info)); - motion_pump_->SetSensorProviderForTesting( + auto* motion_pump = + new DeviceMotionEventPump(base::ThreadTaskRunnerHandle::Get()); + motion_pump->SetSensorProviderForTesting( device::mojom::blink::SensorProviderPtr( device::mojom::blink::SensorProviderPtrInfo( sensor_provider_ptr_info.PassHandle(), device::mojom::SensorProvider::Version_))); - listener_.reset(new MockDeviceMotionListener); + controller_ = new MockDeviceMotionController(motion_pump); ExpectAllThreeSensorsStateToBe( DeviceMotionEventPump::SensorState::NOT_INITIALIZED); EXPECT_EQ(DeviceMotionEventPump::PumpState::STOPPED, - motion_pump()->GetPumpStateForTesting()); + controller_->motion_pump()->GetPumpStateForTesting()); } - void FireEvent() { motion_pump_->FireEvent(nullptr); } + void FireEvent() { controller_->motion_pump()->FireEvent(nullptr); } void ExpectAccelerometerStateToBe( DeviceMotionEventPump::SensorState expected_sensor_state) { - EXPECT_EQ(expected_sensor_state, motion_pump_->accelerometer_.sensor_state); + EXPECT_EQ(expected_sensor_state, + controller_->motion_pump()->accelerometer_.sensor_state); } void ExpectLinearAccelerationSensorStateToBe( DeviceMotionEventPump::SensorState expected_sensor_state) { - EXPECT_EQ(expected_sensor_state, - motion_pump_->linear_acceleration_sensor_.sensor_state); + EXPECT_EQ( + expected_sensor_state, + controller_->motion_pump()->linear_acceleration_sensor_.sensor_state); } void ExpectGyroscopeStateToBe( DeviceMotionEventPump::SensorState expected_sensor_state) { - EXPECT_EQ(expected_sensor_state, motion_pump_->gyroscope_.sensor_state); + EXPECT_EQ(expected_sensor_state, + controller_->motion_pump()->gyroscope_.sensor_state); } void ExpectAllThreeSensorsStateToBe( @@ -110,52 +120,50 @@ class DeviceMotionEventPumpTest : public testing::Test { ExpectGyroscopeStateToBe(expected_sensor_state); } - DeviceMotionEventPumpForTesting* motion_pump() { return motion_pump_.get(); } - - MockDeviceMotionListener* listener() { return listener_.get(); } + MockDeviceMotionController* controller() { return controller_.Get(); } FakeSensorProvider* sensor_provider() { return &sensor_provider_; } private: - std::unique_ptr<DeviceMotionEventPumpForTesting> motion_pump_; - std::unique_ptr<MockDeviceMotionListener> listener_; + Persistent<MockDeviceMotionController> controller_; + FakeSensorProvider sensor_provider_; DISALLOW_COPY_AND_ASSIGN(DeviceMotionEventPumpTest); }; TEST_F(DeviceMotionEventPumpTest, MultipleStartAndStopWithWait) { - motion_pump()->Start(nullptr, listener()); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE); EXPECT_EQ(DeviceMotionEventPump::PumpState::RUNNING, - motion_pump()->GetPumpStateForTesting()); + controller()->motion_pump()->GetPumpStateForTesting()); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); EXPECT_EQ(DeviceMotionEventPump::PumpState::STOPPED, - motion_pump()->GetPumpStateForTesting()); + controller()->motion_pump()->GetPumpStateForTesting()); - motion_pump()->Start(nullptr, listener()); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE); EXPECT_EQ(DeviceMotionEventPump::PumpState::RUNNING, - motion_pump()->GetPumpStateForTesting()); + controller()->motion_pump()->GetPumpStateForTesting()); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); EXPECT_EQ(DeviceMotionEventPump::PumpState::STOPPED, - motion_pump()->GetPumpStateForTesting()); + controller()->motion_pump()->GetPumpStateForTesting()); } TEST_F(DeviceMotionEventPumpTest, CallStop) { - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe( @@ -163,26 +171,26 @@ TEST_F(DeviceMotionEventPumpTest, CallStop) { } TEST_F(DeviceMotionEventPumpTest, CallStartAndStop) { - motion_pump()->Start(nullptr, listener()); - motion_pump()->Stop(); + controller()->motion_pump()->Start(nullptr); + controller()->motion_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); } TEST_F(DeviceMotionEventPumpTest, CallStartMultipleTimes) { - motion_pump()->Start(nullptr, listener()); - motion_pump()->Start(nullptr, listener()); - motion_pump()->Stop(); + controller()->motion_pump()->Start(nullptr); + controller()->motion_pump()->Start(nullptr); + controller()->motion_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); } TEST_F(DeviceMotionEventPumpTest, CallStopMultipleTimes) { - motion_pump()->Start(nullptr, listener()); - motion_pump()->Stop(); - motion_pump()->Stop(); + controller()->motion_pump()->Start(nullptr); + controller()->motion_pump()->Stop(); + controller()->motion_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); @@ -190,20 +198,21 @@ TEST_F(DeviceMotionEventPumpTest, CallStopMultipleTimes) { // Test multiple DeviceSensorEventPump::Start() calls only bind sensor once. TEST_F(DeviceMotionEventPumpTest, SensorOnlyBindOnce) { - motion_pump()->Start(nullptr, listener()); - motion_pump()->Stop(); - motion_pump()->Start(nullptr, listener()); + controller()->motion_pump()->Start(nullptr); + controller()->motion_pump()->Stop(); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); } TEST_F(DeviceMotionEventPumpTest, AllSensorsAreActive) { - motion_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE); @@ -214,9 +223,8 @@ TEST_F(DeviceMotionEventPumpTest, AllSensorsAreActive) { FireEvent(); - EXPECT_TRUE(listener()->did_change_device_motion()); - - const DeviceMotionData* received_data = listener()->data(); + const DeviceMotionData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_motion()); EXPECT_TRUE(received_data->GetAccelerationIncludingGravity()->CanProvideX()); EXPECT_EQ(1, received_data->GetAccelerationIncludingGravity()->X()); @@ -239,7 +247,7 @@ TEST_F(DeviceMotionEventPumpTest, AllSensorsAreActive) { EXPECT_TRUE(received_data->GetRotationRate()->CanProvideGamma()); EXPECT_EQ(gfx::RadToDeg(9.0), received_data->GetRotationRate()->Gamma()); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); } @@ -247,7 +255,8 @@ TEST_F(DeviceMotionEventPumpTest, AllSensorsAreActive) { TEST_F(DeviceMotionEventPumpTest, TwoSensorsAreActive) { sensor_provider()->set_linear_acceleration_sensor_is_available(false); - motion_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAccelerometerStateToBe(DeviceMotionEventPump::SensorState::ACTIVE); @@ -260,8 +269,8 @@ TEST_F(DeviceMotionEventPumpTest, TwoSensorsAreActive) { FireEvent(); - const DeviceMotionData* received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_motion()); + const DeviceMotionData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_motion()); EXPECT_TRUE(received_data->GetAccelerationIncludingGravity()->CanProvideX()); EXPECT_EQ(1, received_data->GetAccelerationIncludingGravity()->X()); @@ -281,7 +290,7 @@ TEST_F(DeviceMotionEventPumpTest, TwoSensorsAreActive) { EXPECT_TRUE(received_data->GetRotationRate()->CanProvideGamma()); EXPECT_EQ(gfx::RadToDeg(9.0), received_data->GetRotationRate()->Gamma()); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); ExpectAccelerometerStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); ExpectLinearAccelerationSensorStateToBe( @@ -290,7 +299,8 @@ TEST_F(DeviceMotionEventPumpTest, TwoSensorsAreActive) { } TEST_F(DeviceMotionEventPumpTest, SomeSensorDataFieldsNotAvailable) { - motion_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE); @@ -301,8 +311,8 @@ TEST_F(DeviceMotionEventPumpTest, SomeSensorDataFieldsNotAvailable) { FireEvent(); - const DeviceMotionData* received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_motion()); + const DeviceMotionData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_motion()); EXPECT_FALSE(received_data->GetAccelerationIncludingGravity()->CanProvideX()); EXPECT_TRUE(received_data->GetAccelerationIncludingGravity()->CanProvideY()); @@ -322,7 +332,7 @@ TEST_F(DeviceMotionEventPumpTest, SomeSensorDataFieldsNotAvailable) { EXPECT_EQ(gfx::RadToDeg(8.0), received_data->GetRotationRate()->Beta()); EXPECT_FALSE(received_data->GetRotationRate()->CanProvideGamma()); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); } @@ -333,7 +343,8 @@ TEST_F(DeviceMotionEventPumpTest, FireAllNullEvent) { sensor_provider()->set_linear_acceleration_sensor_is_available(false); sensor_provider()->set_gyroscope_is_available(false); - motion_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe( @@ -341,8 +352,8 @@ TEST_F(DeviceMotionEventPumpTest, FireAllNullEvent) { FireEvent(); - const DeviceMotionData* received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_motion()); + const DeviceMotionData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_motion()); EXPECT_FALSE(received_data->GetAcceleration()->CanProvideX()); EXPECT_FALSE(received_data->GetAcceleration()->CanProvideY()); @@ -356,7 +367,7 @@ TEST_F(DeviceMotionEventPumpTest, FireAllNullEvent) { EXPECT_FALSE(received_data->GetRotationRate()->CanProvideBeta()); EXPECT_FALSE(received_data->GetRotationRate()->CanProvideGamma()); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); ExpectAllThreeSensorsStateToBe( DeviceMotionEventPump::SensorState::NOT_INITIALIZED); @@ -364,28 +375,29 @@ TEST_F(DeviceMotionEventPumpTest, FireAllNullEvent) { TEST_F(DeviceMotionEventPumpTest, NotFireEventWhenSensorReadingTimeStampIsZero) { - motion_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE); FireEvent(); - EXPECT_FALSE(listener()->did_change_device_motion()); + EXPECT_FALSE(controller()->did_change_device_motion()); sensor_provider()->UpdateAccelerometerData(1, 2, 3); FireEvent(); - EXPECT_FALSE(listener()->did_change_device_motion()); + EXPECT_FALSE(controller()->did_change_device_motion()); sensor_provider()->UpdateLinearAccelerationSensorData(4, 5, 6); FireEvent(); - EXPECT_FALSE(listener()->did_change_device_motion()); + EXPECT_FALSE(controller()->did_change_device_motion()); sensor_provider()->UpdateGyroscopeData(7, 8, 9); FireEvent(); // Event is fired only after all the available sensors have data. - EXPECT_TRUE(listener()->did_change_device_motion()); + EXPECT_TRUE(controller()->did_change_device_motion()); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); } @@ -396,9 +408,10 @@ TEST_F(DeviceMotionEventPumpTest, TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { // Confirm that the delay for pumping events is 60 Hz. EXPECT_GE(60, WTF::Time::kMicrosecondsPerSecond / - motion_pump()->pump_delay_microseconds()); + DeviceMotionEventPump::kDefaultPumpDelayMicroseconds); - motion_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->motion_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::ACTIVE); @@ -412,14 +425,14 @@ TEST_F(DeviceMotionEventPumpTest, PumpThrottlesEventRate) { FROM_HERE, loop.QuitWhenIdleClosure(), WTF::TimeDelta::FromMilliseconds(100)); loop.Run(); - motion_pump()->Stop(); + controller()->motion_pump()->Stop(); ExpectAllThreeSensorsStateToBe(DeviceMotionEventPump::SensorState::SUSPENDED); - // Check that the blink::WebDeviceMotionListener does not receive excess + // Check that the PlatformEventController does not receive excess // events. - EXPECT_TRUE(listener()->did_change_device_motion()); - EXPECT_GE(6, listener()->number_of_events()); + EXPECT_TRUE(controller()->did_change_device_motion()); + EXPECT_GE(6, controller()->number_of_events()); } } // namespace blink diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.cc index becd631742e..d671f10e015 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.cc @@ -6,7 +6,7 @@ #include "third_party/blink/renderer/core/dom/document.h" #include "third_party/blink/renderer/core/frame/settings.h" -#include "third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.h" +#include "third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h" namespace blink { @@ -66,11 +66,6 @@ void DeviceOrientationAbsoluteController::DidAddEventListener( DeviceSingleWindowEventController::DidAddEventListener(window, event_type); } -DeviceOrientationDispatcher& -DeviceOrientationAbsoluteController::DispatcherInstance() const { - return DeviceOrientationDispatcher::Instance(true); -} - const AtomicString& DeviceOrientationAbsoluteController::EventTypeName() const { return EventTypeNames::deviceorientationabsolute; } @@ -79,4 +74,8 @@ void DeviceOrientationAbsoluteController::Trace(blink::Visitor* visitor) { DeviceOrientationController::Trace(visitor); } +void DeviceOrientationAbsoluteController::RegisterWithDispatcher() { + RegisterWithOrientationEventPump(true /* absolute */); +} + } // namespace blink diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.h index 23227c705df..e8e2e28d0ed 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.h +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_absolute_controller.h @@ -26,10 +26,12 @@ class MODULES_EXPORT DeviceOrientationAbsoluteController final void Trace(blink::Visitor*) override; private: + // Inherited from PlatformEventController. + void RegisterWithDispatcher() override; + explicit DeviceOrientationAbsoluteController(Document&); // Inherited from DeviceOrientationController. - DeviceOrientationDispatcher& DispatcherInstance() const override; const AtomicString& EventTypeName() const override; }; diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_controller.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_controller.cc index 198eed07893..6df6db6ba2e 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_controller.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_controller.cc @@ -12,8 +12,8 @@ #include "third_party/blink/renderer/core/frame/settings.h" #include "third_party/blink/renderer/core/inspector/console_message.h" #include "third_party/blink/renderer/modules/device_orientation/device_orientation_data.h" -#include "third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.h" #include "third_party/blink/renderer/modules/device_orientation/device_orientation_event.h" +#include "third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h" #include "third_party/blink/renderer/modules/event_modules.h" #include "third_party/blink/renderer/platform/feature_policy/feature_policy.h" #include "third_party/blink/renderer/platform/weborigin/security_origin.h" @@ -94,7 +94,9 @@ void DeviceOrientationController::DidAddEventListener( DeviceOrientationData* DeviceOrientationController::LastData() const { return override_orientation_data_ ? override_orientation_data_.Get() - : DispatcherInstance().LatestDeviceOrientationData(); + : orientation_event_pump_ + ? orientation_event_pump_->LatestDeviceOrientationData() + : nullptr; } bool DeviceOrientationController::HasLastData() { @@ -102,11 +104,12 @@ bool DeviceOrientationController::HasLastData() { } void DeviceOrientationController::RegisterWithDispatcher() { - DispatcherInstance().AddController(this); + RegisterWithOrientationEventPump(false /* absolute */); } void DeviceOrientationController::UnregisterWithDispatcher() { - DispatcherInstance().RemoveController(this); + if (orientation_event_pump_) + orientation_event_pump_->RemoveController(this); } Event* DeviceOrientationController::LastEvent() const { @@ -137,17 +140,27 @@ void DeviceOrientationController::ClearOverride() { DidUpdateData(); } -DeviceOrientationDispatcher& DeviceOrientationController::DispatcherInstance() - const { - return DeviceOrientationDispatcher::Instance(false); -} - void DeviceOrientationController::Trace(blink::Visitor* visitor) { visitor->Trace(override_orientation_data_); + visitor->Trace(orientation_event_pump_); DeviceSingleWindowEventController::Trace(visitor); Supplement<Document>::Trace(visitor); } +void DeviceOrientationController::RegisterWithOrientationEventPump( + bool absolute) { + if (!orientation_event_pump_) { + LocalFrame* frame = GetDocument().GetFrame(); + if (!frame) + return; + scoped_refptr<base::SingleThreadTaskRunner> task_runner = + frame->GetTaskRunner(TaskType::kSensor); + orientation_event_pump_ = + new DeviceOrientationEventPump(task_runner, absolute); + } + orientation_event_pump_->AddController(this); +} + // static void DeviceOrientationController::LogToConsolePolicyFeaturesDisabled( LocalFrame* frame, diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_controller.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_controller.h index fdd13f6e781..3985fdec55c 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_controller.h +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_controller.h @@ -12,7 +12,7 @@ namespace blink { class DeviceOrientationData; -class DeviceOrientationDispatcher; +class DeviceOrientationEventPump; class Event; class MODULES_EXPORT DeviceOrientationController @@ -43,11 +43,12 @@ class MODULES_EXPORT DeviceOrientationController protected: explicit DeviceOrientationController(Document&); + void RegisterWithOrientationEventPump(bool absolute); - virtual DeviceOrientationDispatcher& DispatcherInstance() const; + Member<DeviceOrientationEventPump> orientation_event_pump_; private: - // Inherited from DeviceEventControllerBase. + // Inherited from PlatformEventController. void RegisterWithDispatcher() override; void UnregisterWithDispatcher() override; bool HasLastData() override; diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_data.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_data.cc index 9c22671ad25..f8c415301c4 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_data.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_data.cc @@ -25,7 +25,6 @@ #include "third_party/blink/renderer/modules/device_orientation/device_orientation_data.h" -#include "services/device/public/cpp/generic_sensor/orientation_data.h" #include "third_party/blink/renderer/modules/device_orientation/device_orientation_event_init.h" namespace blink { @@ -56,20 +55,6 @@ DeviceOrientationData* DeviceOrientationData::Create( return DeviceOrientationData::Create(alpha, beta, gamma, init.absolute()); } -DeviceOrientationData* DeviceOrientationData::Create( - const device::OrientationData& data) { - base::Optional<double> alpha; - base::Optional<double> beta; - base::Optional<double> gamma; - if (data.has_alpha) - alpha = data.alpha; - if (data.has_beta) - beta = data.beta; - if (data.has_gamma) - gamma = data.gamma; - return DeviceOrientationData::Create(alpha, beta, gamma, data.absolute); -} - DeviceOrientationData::DeviceOrientationData() : absolute_(false) {} DeviceOrientationData::DeviceOrientationData( diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_data.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_data.h index b2d771ba848..cba97f33d1c 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_data.h +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_data.h @@ -30,10 +30,6 @@ #include "third_party/blink/renderer/modules/modules_export.h" #include "third_party/blink/renderer/platform/heap/handle.h" -namespace device { -class OrientationData; -} - namespace blink { class DeviceOrientationEventInit; @@ -47,7 +43,6 @@ class MODULES_EXPORT DeviceOrientationData final const base::Optional<double>& gamma, bool absolute); static DeviceOrientationData* Create(const DeviceOrientationEventInit&); - static DeviceOrientationData* Create(const device::OrientationData&); void Trace(blink::Visitor* visitor) {} double Alpha() const; diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.cc deleted file mode 100644 index 8a15c24af20..00000000000 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.cc +++ /dev/null @@ -1,97 +0,0 @@ -/* - * Copyright (C) 2013 Google Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.h" - -#include "third_party/blink/renderer/modules/device_orientation/device_orientation_controller.h" -#include "third_party/blink/renderer/modules/device_orientation/device_orientation_data.h" -#include "third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h" - -namespace blink { - -DeviceOrientationDispatcher& DeviceOrientationDispatcher::Instance( - bool absolute) { - if (absolute) { - DEFINE_STATIC_LOCAL(DeviceOrientationDispatcher, - device_orientation_absolute_dispatcher, - (new DeviceOrientationDispatcher(absolute))); - return device_orientation_absolute_dispatcher; - } - DEFINE_STATIC_LOCAL(DeviceOrientationDispatcher, - device_orientation_dispatcher, - (new DeviceOrientationDispatcher(absolute))); - return device_orientation_dispatcher; -} - -DeviceOrientationDispatcher::DeviceOrientationDispatcher(bool absolute) - : absolute_(absolute) {} - -DeviceOrientationDispatcher::~DeviceOrientationDispatcher() = default; - -void DeviceOrientationDispatcher::Trace(blink::Visitor* visitor) { - visitor->Trace(last_device_orientation_data_); - PlatformEventDispatcher::Trace(visitor); -} - -void DeviceOrientationDispatcher::StartListening(LocalFrame* frame) { - // TODO(crbug.com/850619): ensure a valid frame is passed - if (!frame) - return; - if (!event_pump_) { - event_pump_ = std::make_unique<DeviceOrientationEventPump>( - frame->GetTaskRunner(TaskType::kSensor), absolute_); - } - event_pump_->Start(frame, this); -} - -void DeviceOrientationDispatcher::StopListening() { - if (event_pump_) - event_pump_->Stop(); - last_device_orientation_data_.Clear(); -} - -void DeviceOrientationDispatcher::DidChangeDeviceOrientation( - const device::OrientationData& motion) { - last_device_orientation_data_ = DeviceOrientationData::Create(motion); - NotifyControllers(); -} - -DeviceOrientationData* -DeviceOrientationDispatcher::LatestDeviceOrientationData() { - return last_device_orientation_data_.Get(); -} - -WebPlatformEventType DeviceOrientationDispatcher::GetWebPlatformEventType() - const { - return (absolute_) ? kWebPlatformEventTypeDeviceOrientationAbsolute - : kWebPlatformEventTypeDeviceOrientation; -} - -} // namespace blink diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.h deleted file mode 100644 index 62a9453ad32..00000000000 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_dispatcher.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - * Copyright (C) 2013 Google Inc. All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are - * met: - * - * * Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * * Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following disclaimer - * in the documentation and/or other materials provided with the - * distribution. - * * Neither the name of Google Inc. nor the names of its - * contributors may be used to endorse or promote products derived from - * this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#ifndef THIRD_PARTY_BLINK_RENDERER_MODULES_DEVICE_ORIENTATION_DEVICE_ORIENTATION_DISPATCHER_H_ -#define THIRD_PARTY_BLINK_RENDERER_MODULES_DEVICE_ORIENTATION_DEVICE_ORIENTATION_DISPATCHER_H_ - -#include "base/memory/scoped_refptr.h" -#include "third_party/blink/public/platform/modules/device_orientation/web_device_orientation_listener.h" -#include "third_party/blink/public/platform/web_platform_event_type.h" -#include "third_party/blink/renderer/core/frame/platform_event_dispatcher.h" -#include "third_party/blink/renderer/platform/heap/handle.h" - -namespace device { -class OrientationData; -} - -namespace blink { - -class DeviceOrientationData; -class DeviceOrientationEventPump; - -// This class listens to device orientation data and notifies all registered -// controllers. -class DeviceOrientationDispatcher final - : public GarbageCollectedFinalized<DeviceOrientationDispatcher>, - public PlatformEventDispatcher, - public WebDeviceOrientationListener { - USING_GARBAGE_COLLECTED_MIXIN(DeviceOrientationDispatcher); - - public: - static DeviceOrientationDispatcher& Instance(bool absolute); - ~DeviceOrientationDispatcher() override; - - // Note that the returned object is owned by this class. - // FIXME: make the return value const, see crbug.com/233174. - DeviceOrientationData* LatestDeviceOrientationData(); - - // Inherited from WebDeviceOrientationListener. - void DidChangeDeviceOrientation(const device::OrientationData&) override; - - void Trace(blink::Visitor*) override; - - private: - explicit DeviceOrientationDispatcher(bool absolute); - - // Inherited from PlatformEventDispatcher. - void StartListening(LocalFrame* frame) override; - void StopListening() override; - - WebPlatformEventType GetWebPlatformEventType() const; - - const bool absolute_; - Member<DeviceOrientationData> last_device_orientation_data_; - std::unique_ptr<DeviceOrientationEventPump> event_pump_; -}; - -} // namespace blink - -#endif // THIRD_PARTY_BLINK_RENDERER_MODULES_DEVICE_ORIENTATION_DEVICE_ORIENTATION_DISPATCHER_H_ diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.cc index 459caef8295..362f5c3f7d1 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.cc @@ -7,44 +7,40 @@ #include "services/device/public/mojom/sensor.mojom-blink.h" #include "services/service_manager/public/cpp/interface_provider.h" #include "third_party/blink/renderer/core/frame/local_frame.h" +#include "third_party/blink/renderer/modules/device_orientation/device_orientation_data.h" #include "third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h" namespace { -bool IsAngleDifferentThreshold(bool has_angle1, - double angle1, - bool has_angle2, - double angle2) { - if (has_angle1 != has_angle2) - return true; - - return (has_angle1 && - std::fabs(angle1 - angle2) >= - blink::DeviceOrientationEventPump::kOrientationThreshold); +bool IsAngleDifferentThreshold(double angle1, double angle2) { + return (std::fabs(angle1 - angle2) >= + blink::DeviceOrientationEventPump::kOrientationThreshold); } -bool IsSignificantlyDifferent(const device::OrientationData& data1, - const device::OrientationData& data2) { - return IsAngleDifferentThreshold(data1.has_alpha, data1.alpha, - data2.has_alpha, data2.alpha) || - IsAngleDifferentThreshold(data1.has_beta, data1.beta, data2.has_beta, - data2.beta) || - IsAngleDifferentThreshold(data1.has_gamma, data1.gamma, - data2.has_gamma, data2.gamma); +bool IsSignificantlyDifferent(const blink::DeviceOrientationData* data1, + const blink::DeviceOrientationData* data2) { + if (data1->CanProvideAlpha() != data2->CanProvideAlpha() || + data1->CanProvideBeta() != data2->CanProvideBeta() || + data1->CanProvideGamma() != data2->CanProvideGamma()) + return true; + return (data1->CanProvideAlpha() && + IsAngleDifferentThreshold(data1->Alpha(), data2->Alpha())) || + (data1->CanProvideBeta() && + IsAngleDifferentThreshold(data1->Beta(), data2->Beta())) || + (data1->CanProvideGamma() && + IsAngleDifferentThreshold(data1->Gamma(), data2->Gamma())); } } // namespace namespace blink { -template class DeviceSensorEventPump<blink::WebDeviceOrientationListener>; - const double DeviceOrientationEventPump::kOrientationThreshold = 0.1; DeviceOrientationEventPump::DeviceOrientationEventPump( scoped_refptr<base::SingleThreadTaskRunner> task_runner, bool absolute) - : DeviceSensorEventPump<blink::WebDeviceOrientationListener>(task_runner), + : DeviceSensorEventPump(task_runner), relative_orientation_sensor_( this, device::mojom::SensorType::RELATIVE_ORIENTATION_EULER_ANGLES), @@ -58,6 +54,23 @@ DeviceOrientationEventPump::~DeviceOrientationEventPump() { StopIfObserving(); } +DeviceOrientationData* +DeviceOrientationEventPump::LatestDeviceOrientationData() { + return data_.Get(); +} + +void DeviceOrientationEventPump::Trace(blink::Visitor* visitor) { + visitor->Trace(data_); + PlatformEventDispatcher::Trace(visitor); +} + +void DeviceOrientationEventPump::StartListening(LocalFrame* frame) { + // TODO(crbug.com/850619): ensure a valid frame is passed + if (!frame) + return; + Start(frame); +} + void DeviceOrientationEventPump::SendStartMessage(LocalFrame* frame) { if (!sensor_provider_) { DCHECK(frame); @@ -66,7 +79,7 @@ void DeviceOrientationEventPump::SendStartMessage(LocalFrame* frame) { mojo::MakeRequest(&sensor_provider_)); sensor_provider_.set_connection_error_handler( WTF::Bind(&DeviceSensorEventPump::HandleSensorProviderError, - WTF::Unretained(this))); + WrapPersistent(this))); } if (absolute_) { @@ -78,6 +91,11 @@ void DeviceOrientationEventPump::SendStartMessage(LocalFrame* frame) { } } +void DeviceOrientationEventPump::StopListening() { + Stop(); + data_.Clear(); +} + void DeviceOrientationEventPump::SendStopMessage() { // SendStopMessage() gets called both when the page visibility changes and if // all device orientation event listeners are unregistered. Since removing @@ -105,19 +123,15 @@ void DeviceOrientationEventPump::SendStopMessage() { // data when stopping. If we don't reset here as well, then when starting back // up we won't notify DeviceOrientationDispatcher of the orientation, since // we think it hasn't changed. - data_ = device::OrientationData(); + data_ = nullptr; } void DeviceOrientationEventPump::FireEvent(TimerBase*) { - device::OrientationData data; - - DCHECK(listener()); - - GetDataFromSharedMemory(&data); + DeviceOrientationData* data = GetDataFromSharedMemory(); if (ShouldFireEvent(data)) { data_ = data; - listener()->DidChangeDeviceOrientation(data); + NotifyControllers(); } } @@ -152,60 +166,67 @@ bool DeviceOrientationEventPump::SensorsReadyOrErrored() const { return true; } -void DeviceOrientationEventPump::GetDataFromSharedMemory( - device::OrientationData* data) { - data->all_available_sensors_are_active = true; +DeviceOrientationData* DeviceOrientationEventPump::GetDataFromSharedMemory() { + base::Optional<double> alpha; + base::Optional<double> beta; + base::Optional<double> gamma; + bool absolute = false; if (!absolute_ && relative_orientation_sensor_.SensorReadingCouldBeRead()) { // For DeviceOrientation Event, this provides relative orientation data. - data->all_available_sensors_are_active = - relative_orientation_sensor_.reading.timestamp() != 0.0; - if (!data->all_available_sensors_are_active) - return; - data->alpha = relative_orientation_sensor_.reading.orientation_euler.z; - data->beta = relative_orientation_sensor_.reading.orientation_euler.x; - data->gamma = relative_orientation_sensor_.reading.orientation_euler.y; - data->has_alpha = !std::isnan( - relative_orientation_sensor_.reading.orientation_euler.z.value()); - data->has_beta = !std::isnan( - relative_orientation_sensor_.reading.orientation_euler.x.value()); - data->has_gamma = !std::isnan( - relative_orientation_sensor_.reading.orientation_euler.y.value()); - data->absolute = false; + if (relative_orientation_sensor_.reading.timestamp() == 0.0) + return nullptr; + + if (!std::isnan( + relative_orientation_sensor_.reading.orientation_euler.z.value())) + alpha = relative_orientation_sensor_.reading.orientation_euler.z; + + if (!std::isnan( + relative_orientation_sensor_.reading.orientation_euler.x.value())) + beta = relative_orientation_sensor_.reading.orientation_euler.x; + + if (!std::isnan( + relative_orientation_sensor_.reading.orientation_euler.y.value())) + gamma = relative_orientation_sensor_.reading.orientation_euler.y; } else if (absolute_orientation_sensor_.SensorReadingCouldBeRead()) { // For DeviceOrientationAbsolute Event, this provides absolute orientation // data. // // For DeviceOrientation Event, this provides absolute orientation data if // relative orientation data is not available. - data->all_available_sensors_are_active = - absolute_orientation_sensor_.reading.timestamp() != 0.0; - if (!data->all_available_sensors_are_active) - return; - data->alpha = absolute_orientation_sensor_.reading.orientation_euler.z; - data->beta = absolute_orientation_sensor_.reading.orientation_euler.x; - data->gamma = absolute_orientation_sensor_.reading.orientation_euler.y; - data->has_alpha = !std::isnan( - absolute_orientation_sensor_.reading.orientation_euler.z.value()); - data->has_beta = !std::isnan( - absolute_orientation_sensor_.reading.orientation_euler.x.value()); - data->has_gamma = !std::isnan( - absolute_orientation_sensor_.reading.orientation_euler.y.value()); - data->absolute = true; + if (absolute_orientation_sensor_.reading.timestamp() == 0.0) + return nullptr; + + if (!std::isnan( + absolute_orientation_sensor_.reading.orientation_euler.z.value())) + alpha = absolute_orientation_sensor_.reading.orientation_euler.z; + + if (!std::isnan( + absolute_orientation_sensor_.reading.orientation_euler.x.value())) + beta = absolute_orientation_sensor_.reading.orientation_euler.x; + + if (!std::isnan( + absolute_orientation_sensor_.reading.orientation_euler.y.value())) + gamma = absolute_orientation_sensor_.reading.orientation_euler.y; + + absolute = true; } else { - data->absolute = absolute_; + absolute = absolute_; } + + return DeviceOrientationData::Create(alpha, beta, gamma, absolute); } bool DeviceOrientationEventPump::ShouldFireEvent( - const device::OrientationData& data) const { - if (!data.all_available_sensors_are_active) + const DeviceOrientationData* data) const { + // |data| is null if not all sensors are active + if (!data) return false; - if (!data.has_alpha && !data.has_beta && !data.has_gamma) { - // no data can be provided, this is an all-null event. + // when the state changes from not having data to having data, + // the event should be fired + if (!data_) return true; - } return IsSignificantlyDifferent(data_, data); } diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h index cc2122b9680..fca10da6f98 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h @@ -6,15 +6,19 @@ #define THIRD_PARTY_BLINK_RENDERER_MODULES_DEVICE_ORIENTATION_DEVICE_ORIENTATION_EVENT_PUMP_H_ #include "base/macros.h" -#include "services/device/public/cpp/generic_sensor/orientation_data.h" -#include "third_party/blink/public/platform/modules/device_orientation/web_device_orientation_listener.h" +#include "third_party/blink/renderer/core/frame/platform_event_dispatcher.h" #include "third_party/blink/renderer/modules/device_orientation/device_sensor_event_pump.h" #include "third_party/blink/renderer/modules/modules_export.h" +#include "third_party/blink/renderer/platform/heap/handle.h" namespace blink { class MODULES_EXPORT DeviceOrientationEventPump - : public DeviceSensorEventPump<blink::WebDeviceOrientationListener> { + : public GarbageCollectedFinalized<DeviceOrientationEventPump>, + public DeviceSensorEventPump, + public PlatformEventDispatcher { + USING_GARBAGE_COLLECTED_MIXIN(DeviceOrientationEventPump); + public: // Angle threshold beyond which two orientation events are considered // sufficiently different. @@ -23,8 +27,14 @@ class MODULES_EXPORT DeviceOrientationEventPump explicit DeviceOrientationEventPump( scoped_refptr<base::SingleThreadTaskRunner> task_runner, bool absolute); + ~DeviceOrientationEventPump() override; + // Note that the returned object is owned by this class. + DeviceOrientationData* LatestDeviceOrientationData(); + + void Trace(blink::Visitor*) override; + // DeviceSensorEventPump: void SendStartMessage(LocalFrame* frame) override; void SendStopMessage() override; @@ -41,17 +51,21 @@ class MODULES_EXPORT DeviceOrientationEventPump friend class DeviceOrientationEventPumpTest; friend class DeviceAbsoluteOrientationEventPumpTest; + // Inherited from PlatformEventDispatcher. + void StartListening(LocalFrame*) override; + void StopListening() override; + // DeviceSensorEventPump: bool SensorsReadyOrErrored() const override; - void GetDataFromSharedMemory(device::OrientationData* data); + DeviceOrientationData* GetDataFromSharedMemory(); - bool ShouldFireEvent(const device::OrientationData& data) const; + bool ShouldFireEvent(const DeviceOrientationData* data) const; bool absolute_; bool fall_back_to_absolute_orientation_sensor_; bool should_suspend_absolute_orientation_sensor_ = false; - device::OrientationData data_; + Member<DeviceOrientationData> data_; DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPump); }; diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump_unittest.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump_unittest.cc index 95b644c61b2..64c5535d345 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump_unittest.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump_unittest.cc @@ -7,12 +7,13 @@ #include <memory> #include "base/run_loop.h" -#include "services/device/public/cpp/generic_sensor/orientation_data.h" #include "services/device/public/cpp/test/fake_sensor_and_provider.h" #include "testing/gtest/include/gtest/gtest.h" -#include "third_party/blink/public/platform/modules/device_orientation/web_device_orientation_listener.h" #include "third_party/blink/public/platform/scheduler/test/renderer_scheduler_test_support.h" +#include "third_party/blink/renderer/core/frame/platform_event_controller.h" +#include "third_party/blink/renderer/modules/device_orientation/device_orientation_data.h" #include "third_party/blink/renderer/modules/device_orientation/device_orientation_event_pump.h" +#include "third_party/blink/renderer/platform/heap/persistent.h" namespace { @@ -24,19 +25,25 @@ namespace blink { using device::FakeSensorProvider; -class MockDeviceOrientationListener - : public blink::WebDeviceOrientationListener { +class MockDeviceOrientationController final + : public GarbageCollectedFinalized<MockDeviceOrientationController>, + public PlatformEventController { + USING_GARBAGE_COLLECTED_MIXIN(MockDeviceOrientationController); + public: - MockDeviceOrientationListener() : did_change_device_orientation_(false) { - memset(&data_, 0, sizeof(data_)); + explicit MockDeviceOrientationController( + DeviceOrientationEventPump* orientation_pump) + : PlatformEventController(nullptr), + did_change_device_orientation_(false), + orientation_pump_(orientation_pump) {} + ~MockDeviceOrientationController() override {} + + void Trace(Visitor* visitor) override { + PlatformEventController::Trace(visitor); + visitor->Trace(orientation_pump_); } - ~MockDeviceOrientationListener() override {} - void DidChangeDeviceOrientation( - const device::OrientationData& data) override { - memcpy(&data_, &data, sizeof(data)); - did_change_device_orientation_ = true; - } + void DidUpdateData() override { did_change_device_orientation_ = true; } bool did_change_device_orientation() const { return did_change_device_orientation_; @@ -44,25 +51,32 @@ class MockDeviceOrientationListener void set_did_change_device_orientation(bool value) { did_change_device_orientation_ = value; } - const device::OrientationData& data() const { return data_; } - private: - bool did_change_device_orientation_; - device::OrientationData data_; + void RegisterWithDispatcher() override { + orientation_pump_->AddController(this); + } - DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationListener); -}; + bool HasLastData() override { + return orientation_pump_->LatestDeviceOrientationData(); + } -class DeviceOrientationEventPumpForTesting : public DeviceOrientationEventPump { - public: - explicit DeviceOrientationEventPumpForTesting( - scoped_refptr<base::SingleThreadTaskRunner> task_runner, - bool absolute) - : DeviceOrientationEventPump(task_runner, absolute) {} - ~DeviceOrientationEventPumpForTesting() override {} + void UnregisterWithDispatcher() override { + orientation_pump_->RemoveController(this); + } + + const DeviceOrientationData* data() { + return orientation_pump_->LatestDeviceOrientationData(); + }; + + DeviceOrientationEventPump* orientation_pump() { + return orientation_pump_.Get(); + } private: - DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpForTesting); + bool did_change_device_orientation_; + Member<DeviceOrientationEventPump> orientation_pump_; + + DISALLOW_COPY_AND_ASSIGN(MockDeviceOrientationController); }; class DeviceOrientationEventPumpTest : public testing::Test { @@ -71,95 +85,92 @@ class DeviceOrientationEventPumpTest : public testing::Test { protected: void SetUp() override { - orientation_pump_.reset(new DeviceOrientationEventPumpForTesting( - base::ThreadTaskRunnerHandle::Get(), false /* absolute */)); device::mojom::SensorProviderPtrInfo sensor_provider_ptr_info; sensor_provider_.Bind(mojo::MakeRequest(&sensor_provider_ptr_info)); - orientation_pump_->SetSensorProviderForTesting( + auto* orientation_pump = new DeviceOrientationEventPump( + base::ThreadTaskRunnerHandle::Get(), false /* absolute */); + orientation_pump->SetSensorProviderForTesting( device::mojom::blink::SensorProviderPtr( device::mojom::blink::SensorProviderPtrInfo( sensor_provider_ptr_info.PassHandle(), device::mojom::SensorProvider::Version_))); - listener_.reset(new MockDeviceOrientationListener); + controller_ = new MockDeviceOrientationController(orientation_pump); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); EXPECT_EQ(DeviceOrientationEventPump::PumpState::STOPPED, - orientation_pump()->GetPumpStateForTesting()); + controller_->orientation_pump()->GetPumpStateForTesting()); } - void FireEvent() { orientation_pump_->FireEvent(nullptr); } + void FireEvent() { controller_->orientation_pump()->FireEvent(nullptr); } void ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState expected_sensor_state) { EXPECT_EQ(expected_sensor_state, - orientation_pump_->relative_orientation_sensor_.sensor_state); + controller_->orientation_pump() + ->relative_orientation_sensor_.sensor_state); } void ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState expected_sensor_state) { EXPECT_EQ(expected_sensor_state, - orientation_pump_->absolute_orientation_sensor_.sensor_state); + controller_->orientation_pump() + ->absolute_orientation_sensor_.sensor_state); } - DeviceOrientationEventPump* orientation_pump() { - return orientation_pump_.get(); - } - - MockDeviceOrientationListener* listener() { return listener_.get(); } + MockDeviceOrientationController* controller() { return controller_.Get(); } FakeSensorProvider* sensor_provider() { return &sensor_provider_; } private: - std::unique_ptr<DeviceOrientationEventPumpForTesting> orientation_pump_; - std::unique_ptr<MockDeviceOrientationListener> listener_; + Persistent<MockDeviceOrientationController> controller_; FakeSensorProvider sensor_provider_; DISALLOW_COPY_AND_ASSIGN(DeviceOrientationEventPumpTest); }; TEST_F(DeviceOrientationEventPumpTest, MultipleStartAndStopWithWait) { - orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); EXPECT_EQ(DeviceOrientationEventPump::PumpState::RUNNING, - orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); EXPECT_EQ(DeviceOrientationEventPump::PumpState::STOPPED, - orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); EXPECT_EQ(DeviceOrientationEventPump::PumpState::RUNNING, - orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); EXPECT_EQ(DeviceOrientationEventPump::PumpState::STOPPED, - orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); } TEST_F(DeviceOrientationEventPumpTest, MultipleStartAndStopWithWaitWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -167,9 +178,9 @@ TEST_F(DeviceOrientationEventPumpTest, ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); EXPECT_EQ(DeviceOrientationEventPump::PumpState::RUNNING, - orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -177,9 +188,9 @@ TEST_F(DeviceOrientationEventPumpTest, ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); EXPECT_EQ(DeviceOrientationEventPump::PumpState::STOPPED, - orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -187,9 +198,9 @@ TEST_F(DeviceOrientationEventPumpTest, ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); EXPECT_EQ(DeviceOrientationEventPump::PumpState::RUNNING, - orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -197,11 +208,11 @@ TEST_F(DeviceOrientationEventPumpTest, ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); EXPECT_EQ(DeviceOrientationEventPump::PumpState::STOPPED, - orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); } TEST_F(DeviceOrientationEventPumpTest, CallStop) { - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -211,7 +222,7 @@ TEST_F(DeviceOrientationEventPumpTest, CallStop) { TEST_F(DeviceOrientationEventPumpTest, CallStopWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -221,8 +232,8 @@ TEST_F(DeviceOrientationEventPumpTest, CallStopWithSensorFallback) { } TEST_F(DeviceOrientationEventPumpTest, CallStartAndStop) { - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -232,8 +243,8 @@ TEST_F(DeviceOrientationEventPumpTest, CallStartAndStop) { TEST_F(DeviceOrientationEventPumpTest, CallStartAndStopWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -243,9 +254,9 @@ TEST_F(DeviceOrientationEventPumpTest, CallStartAndStopWithSensorFallback) { } TEST_F(DeviceOrientationEventPumpTest, CallStartMultipleTimes) { - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -256,9 +267,9 @@ TEST_F(DeviceOrientationEventPumpTest, CallStartMultipleTimesWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -268,9 +279,9 @@ TEST_F(DeviceOrientationEventPumpTest, } TEST_F(DeviceOrientationEventPumpTest, CallStopMultipleTimes) { - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Stop(); - orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -281,9 +292,9 @@ TEST_F(DeviceOrientationEventPumpTest, CallStopMultipleTimesWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Stop(); - orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -294,15 +305,15 @@ TEST_F(DeviceOrientationEventPumpTest, // Test a sequence of Start(), Stop(), Start() calls only bind sensor once. TEST_F(DeviceOrientationEventPumpTest, SensorOnlyBindOnce) { - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Stop(); - orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); @@ -313,9 +324,9 @@ TEST_F(DeviceOrientationEventPumpTest, SensorOnlyBindOnce) { TEST_F(DeviceOrientationEventPumpTest, SensorOnlyBindOnceWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); - orientation_pump()->Stop(); - orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -323,7 +334,7 @@ TEST_F(DeviceOrientationEventPumpTest, SensorOnlyBindOnceWithSensorFallback) { ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); @@ -332,7 +343,8 @@ TEST_F(DeviceOrientationEventPumpTest, SensorOnlyBindOnceWithSensorFallback) { } TEST_F(DeviceOrientationEventPumpTest, SensorIsActive) { - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -343,20 +355,20 @@ TEST_F(DeviceOrientationEventPumpTest, SensorIsActive) { FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); // DeviceOrientation Event provides relative orientation data when it is // available. - EXPECT_DOUBLE_EQ(1, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(2, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(3, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_FALSE(received_data.absolute); + EXPECT_DOUBLE_EQ(1, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(2, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(3, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_FALSE(received_data->Absolute()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); @@ -365,7 +377,8 @@ TEST_F(DeviceOrientationEventPumpTest, SensorIsActive) { TEST_F(DeviceOrientationEventPumpTest, SensorIsActiveWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -378,23 +391,24 @@ TEST_F(DeviceOrientationEventPumpTest, SensorIsActiveWithSensorFallback) { FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); // DeviceOrientation Event provides absolute orientation data when relative // orientation data is not available but absolute orientation data is // available. - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(5, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(5, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + // Since no relative orientation data is available, DeviceOrientationEvent // fallback to provide absolute orientation data. - EXPECT_TRUE(received_data.absolute); + EXPECT_TRUE(received_data->Absolute()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); @@ -403,7 +417,8 @@ TEST_F(DeviceOrientationEventPumpTest, SensorIsActiveWithSensorFallback) { } TEST_F(DeviceOrientationEventPumpTest, SomeSensorDataFieldsNotAvailable) { - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -414,17 +429,17 @@ TEST_F(DeviceOrientationEventPumpTest, SomeSensorDataFieldsNotAvailable) { FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); - EXPECT_FALSE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(2, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(3, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_FALSE(received_data.absolute); + EXPECT_FALSE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(2, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(3, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_FALSE(received_data->Absolute()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); @@ -434,7 +449,8 @@ TEST_F(DeviceOrientationEventPumpTest, SomeSensorDataFieldsNotAvailableWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -447,22 +463,22 @@ TEST_F(DeviceOrientationEventPumpTest, FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); // DeviceOrientation Event provides absolute orientation data when relative // orientation data is not available but absolute orientation data is // available. - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_FALSE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_FALSE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); // Since no relative orientation data is available, DeviceOrientationEvent // fallback to provide absolute orientation data. - EXPECT_TRUE(received_data.absolute); + EXPECT_TRUE(received_data->Absolute()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); @@ -475,7 +491,8 @@ TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { sensor_provider()->set_relative_orientation_sensor_is_available(false); sensor_provider()->set_absolute_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -485,15 +502,15 @@ TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); - EXPECT_FALSE(received_data.has_alpha); - EXPECT_FALSE(received_data.has_beta); - EXPECT_FALSE(received_data.has_gamma); - EXPECT_FALSE(received_data.absolute); + EXPECT_FALSE(received_data->CanProvideAlpha()); + EXPECT_FALSE(received_data->CanProvideBeta()); + EXPECT_FALSE(received_data->CanProvideGamma()); + EXPECT_FALSE(received_data->Absolute()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); @@ -503,7 +520,8 @@ TEST_F(DeviceOrientationEventPumpTest, FireAllNullEvent) { TEST_F(DeviceOrientationEventPumpTest, NotFireEventWhenSensorReadingTimeStampIsZero) { - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -511,9 +529,9 @@ TEST_F(DeviceOrientationEventPumpTest, FireEvent(); - EXPECT_FALSE(listener()->did_change_device_orientation()); + EXPECT_FALSE(controller()->did_change_device_orientation()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); @@ -523,7 +541,8 @@ TEST_F(DeviceOrientationEventPumpTest, NotFireEventWhenSensorReadingTimeStampIsZeroWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -533,9 +552,9 @@ TEST_F(DeviceOrientationEventPumpTest, FireEvent(); - EXPECT_FALSE(listener()->did_change_device_orientation()); + EXPECT_FALSE(controller()->did_change_device_orientation()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); @@ -544,7 +563,8 @@ TEST_F(DeviceOrientationEventPumpTest, } TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -555,20 +575,20 @@ TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); // DeviceOrientation Event provides relative orientation data when it is // available. - EXPECT_DOUBLE_EQ(1, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(2, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(3, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_FALSE(received_data.absolute); + EXPECT_DOUBLE_EQ(1, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(2, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(3, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_FALSE(received_data->Absolute()); - listener()->set_did_change_device_orientation(false); + controller()->set_did_change_device_orientation(false); sensor_provider()->UpdateRelativeOrientationSensorData( 1 + DeviceOrientationEventPump::kOrientationThreshold / 2.0 /* alpha */, @@ -576,18 +596,18 @@ TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { FireEvent(); - received_data = listener()->data(); - EXPECT_FALSE(listener()->did_change_device_orientation()); + received_data = controller()->data(); + EXPECT_FALSE(controller()->did_change_device_orientation()); - EXPECT_DOUBLE_EQ(1, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(2, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(3, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_FALSE(received_data.absolute); + EXPECT_DOUBLE_EQ(1, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(2, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(3, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_FALSE(received_data->Absolute()); - listener()->set_did_change_device_orientation(false); + controller()->set_did_change_device_orientation(false); sensor_provider()->UpdateRelativeOrientationSensorData( 1 + DeviceOrientationEventPump::kOrientationThreshold /* alpha */, @@ -595,19 +615,19 @@ TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { FireEvent(); - received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); EXPECT_DOUBLE_EQ(1 + DeviceOrientationEventPump::kOrientationThreshold, - received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(2, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(3, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_FALSE(received_data.absolute); + received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(2, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(3, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_FALSE(received_data->Absolute()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); @@ -617,7 +637,8 @@ TEST_F(DeviceOrientationEventPumpTest, UpdateRespectsOrientationThresholdWithSensorFallback) { sensor_provider()->set_relative_orientation_sensor_is_available(false); - orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectRelativeOrientationSensorStateToBe( @@ -630,23 +651,23 @@ TEST_F(DeviceOrientationEventPumpTest, FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); // DeviceOrientation Event provides absolute orientation data when relative // orientation data is not available but absolute orientation data is // available. - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(5, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(5, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); // Since no relative orientation data is available, DeviceOrientationEvent // fallback to provide absolute orientation data. - EXPECT_TRUE(received_data.absolute); + EXPECT_TRUE(received_data->Absolute()); - listener()->set_did_change_device_orientation(false); + controller()->set_did_change_device_orientation(false); sensor_provider()->UpdateAbsoluteOrientationSensorData( 4 /* alpha */, @@ -655,18 +676,18 @@ TEST_F(DeviceOrientationEventPumpTest, FireEvent(); - received_data = listener()->data(); - EXPECT_FALSE(listener()->did_change_device_orientation()); + received_data = controller()->data(); + EXPECT_FALSE(controller()->did_change_device_orientation()); - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(5, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_TRUE(received_data.absolute); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(5, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_TRUE(received_data->Absolute()); - listener()->set_did_change_device_orientation(false); + controller()->set_did_change_device_orientation(false); sensor_provider()->UpdateAbsoluteOrientationSensorData( 4 /* alpha */, @@ -676,20 +697,20 @@ TEST_F(DeviceOrientationEventPumpTest, FireEvent(); - received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); EXPECT_DOUBLE_EQ( 5 + DeviceOrientationEventPump::kOrientationThreshold + kEpsilon, - received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_TRUE(received_data.absolute); + received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_TRUE(received_data->Absolute()); - orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectRelativeOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); @@ -703,85 +724,80 @@ class DeviceAbsoluteOrientationEventPumpTest : public testing::Test { protected: void SetUp() override { - absolute_orientation_pump_.reset(new DeviceOrientationEventPumpForTesting( - base::ThreadTaskRunnerHandle::Get(), true /* absolute */)); device::mojom::SensorProviderPtrInfo sensor_provider_ptr_info; sensor_provider_.Bind(mojo::MakeRequest(&sensor_provider_ptr_info)); - absolute_orientation_pump_->SetSensorProviderForTesting( + auto* absolute_orientation_pump = new DeviceOrientationEventPump( + base::ThreadTaskRunnerHandle::Get(), true /* absolute */); + absolute_orientation_pump->SetSensorProviderForTesting( device::mojom::blink::SensorProviderPtr( device::mojom::blink::SensorProviderPtrInfo( sensor_provider_ptr_info.PassHandle(), device::mojom::SensorProvider::Version_))); - listener_.reset(new MockDeviceOrientationListener); + controller_ = + new MockDeviceOrientationController(absolute_orientation_pump); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); EXPECT_EQ(DeviceOrientationEventPump::PumpState::STOPPED, - absolute_orientation_pump()->GetPumpStateForTesting()); + controller_->orientation_pump()->GetPumpStateForTesting()); } - void FireEvent() { absolute_orientation_pump_->FireEvent(nullptr); } + void FireEvent() { controller_->orientation_pump()->FireEvent(nullptr); } void ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState expected_sensor_state) { - EXPECT_EQ( - expected_sensor_state, - absolute_orientation_pump_->absolute_orientation_sensor_.sensor_state); - } - - DeviceOrientationEventPump* absolute_orientation_pump() { - return absolute_orientation_pump_.get(); + EXPECT_EQ(expected_sensor_state, + controller_->orientation_pump() + ->absolute_orientation_sensor_.sensor_state); } - MockDeviceOrientationListener* listener() { return listener_.get(); } + MockDeviceOrientationController* controller() { return controller_.Get(); } FakeSensorProvider* sensor_provider() { return &sensor_provider_; } private: - std::unique_ptr<DeviceOrientationEventPumpForTesting> - absolute_orientation_pump_; - std::unique_ptr<MockDeviceOrientationListener> listener_; + Persistent<MockDeviceOrientationController> controller_; FakeSensorProvider sensor_provider_; DISALLOW_COPY_AND_ASSIGN(DeviceAbsoluteOrientationEventPumpTest); }; TEST_F(DeviceAbsoluteOrientationEventPumpTest, MultipleStartAndStopWithWait) { - absolute_orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); EXPECT_EQ(DeviceOrientationEventPump::PumpState::RUNNING, - absolute_orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); EXPECT_EQ(DeviceOrientationEventPump::PumpState::STOPPED, - absolute_orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - absolute_orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); EXPECT_EQ(DeviceOrientationEventPump::PumpState::RUNNING, - absolute_orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); EXPECT_EQ(DeviceOrientationEventPump::PumpState::STOPPED, - absolute_orientation_pump()->GetPumpStateForTesting()); + controller()->orientation_pump()->GetPumpStateForTesting()); } TEST_F(DeviceAbsoluteOrientationEventPumpTest, CallStop) { - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -789,8 +805,8 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, CallStop) { } TEST_F(DeviceAbsoluteOrientationEventPumpTest, CallStartAndStop) { - absolute_orientation_pump()->Start(nullptr, listener()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -798,9 +814,9 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, CallStartAndStop) { } TEST_F(DeviceAbsoluteOrientationEventPumpTest, CallStartMultipleTimes) { - absolute_orientation_pump()->Start(nullptr, listener()); - absolute_orientation_pump()->Start(nullptr, listener()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -808,9 +824,9 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, CallStartMultipleTimes) { } TEST_F(DeviceAbsoluteOrientationEventPumpTest, CallStopMultipleTimes) { - absolute_orientation_pump()->Start(nullptr, listener()); - absolute_orientation_pump()->Stop(); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -819,22 +835,23 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, CallStopMultipleTimes) { // Test multiple DeviceSensorEventPump::Start() calls only bind sensor once. TEST_F(DeviceAbsoluteOrientationEventPumpTest, SensorOnlyBindOnce) { - absolute_orientation_pump()->Start(nullptr, listener()); - absolute_orientation_pump()->Stop(); - absolute_orientation_pump()->Start(nullptr, listener()); + controller()->orientation_pump()->Start(nullptr); + controller()->orientation_pump()->Stop(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::ACTIVE); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); } TEST_F(DeviceAbsoluteOrientationEventPumpTest, SensorIsActive) { - absolute_orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -845,18 +862,18 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, SensorIsActive) { FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(5, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_TRUE(received_data.absolute); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(5, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_TRUE(received_data->Absolute()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); @@ -864,7 +881,8 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, SensorIsActive) { TEST_F(DeviceAbsoluteOrientationEventPumpTest, SomeSensorDataFieldsNotAvailable) { - absolute_orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -875,17 +893,17 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_FALSE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_TRUE(received_data.absolute); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_FALSE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_TRUE(received_data->Absolute()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); @@ -895,7 +913,8 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, FireAllNullEvent) { // No active sensor. sensor_provider()->set_absolute_orientation_sensor_is_available(false); - absolute_orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -903,15 +922,15 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, FireAllNullEvent) { FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); - EXPECT_FALSE(received_data.has_alpha); - EXPECT_FALSE(received_data.has_beta); - EXPECT_FALSE(received_data.has_gamma); - EXPECT_TRUE(received_data.absolute); + EXPECT_FALSE(received_data->CanProvideAlpha()); + EXPECT_FALSE(received_data->CanProvideBeta()); + EXPECT_FALSE(received_data->CanProvideGamma()); + EXPECT_TRUE(received_data->Absolute()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::NOT_INITIALIZED); @@ -919,7 +938,8 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, FireAllNullEvent) { TEST_F(DeviceAbsoluteOrientationEventPumpTest, NotFireEventWhenSensorReadingTimeStampIsZero) { - absolute_orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -927,9 +947,9 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, FireEvent(); - EXPECT_FALSE(listener()->did_change_device_orientation()); + EXPECT_FALSE(controller()->did_change_device_orientation()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); @@ -937,7 +957,8 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, TEST_F(DeviceAbsoluteOrientationEventPumpTest, UpdateRespectsOrientationThreshold) { - absolute_orientation_pump()->Start(nullptr, listener()); + controller()->RegisterWithDispatcher(); + controller()->orientation_pump()->Start(nullptr); base::RunLoop().RunUntilIdle(); ExpectAbsoluteOrientationSensorStateToBe( @@ -948,18 +969,18 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, FireEvent(); - device::OrientationData received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + const DeviceOrientationData* received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(5, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_TRUE(received_data.absolute); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(5, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_TRUE(received_data->Absolute()); - listener()->set_did_change_device_orientation(false); + controller()->set_did_change_device_orientation(false); sensor_provider()->UpdateAbsoluteOrientationSensorData( 4 /* alpha */, @@ -968,18 +989,18 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, FireEvent(); - received_data = listener()->data(); - EXPECT_FALSE(listener()->did_change_device_orientation()); + received_data = controller()->data(); + EXPECT_FALSE(controller()->did_change_device_orientation()); - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); - EXPECT_DOUBLE_EQ(5, received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_TRUE(received_data.absolute); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); + EXPECT_DOUBLE_EQ(5, received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_TRUE(received_data->Absolute()); - listener()->set_did_change_device_orientation(false); + controller()->set_did_change_device_orientation(false); sensor_provider()->UpdateAbsoluteOrientationSensorData( 4 /* alpha */, @@ -989,20 +1010,20 @@ TEST_F(DeviceAbsoluteOrientationEventPumpTest, FireEvent(); - received_data = listener()->data(); - EXPECT_TRUE(listener()->did_change_device_orientation()); + received_data = controller()->data(); + EXPECT_TRUE(controller()->did_change_device_orientation()); - EXPECT_DOUBLE_EQ(4, received_data.alpha); - EXPECT_TRUE(received_data.has_alpha); + EXPECT_DOUBLE_EQ(4, received_data->Alpha()); + EXPECT_TRUE(received_data->CanProvideAlpha()); EXPECT_DOUBLE_EQ( 5 + DeviceOrientationEventPump::kOrientationThreshold + kEpsilon, - received_data.beta); - EXPECT_TRUE(received_data.has_beta); - EXPECT_DOUBLE_EQ(6, received_data.gamma); - EXPECT_TRUE(received_data.has_gamma); - EXPECT_TRUE(received_data.absolute); + received_data->Beta()); + EXPECT_TRUE(received_data->CanProvideBeta()); + EXPECT_DOUBLE_EQ(6, received_data->Gamma()); + EXPECT_TRUE(received_data->CanProvideGamma()); + EXPECT_TRUE(received_data->Absolute()); - absolute_orientation_pump()->Stop(); + controller()->orientation_pump()->Stop(); ExpectAbsoluteOrientationSensorStateToBe( DeviceOrientationEventPump::SensorState::SUSPENDED); diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_inspector_agent.cc b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_inspector_agent.cc index c9144c4fde2..5e777aab9b4 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_inspector_agent.cc +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_inspector_agent.cc @@ -15,19 +15,16 @@ namespace blink { using protocol::Response; -namespace DeviceOrientationInspectorAgentState { -static const char kAlpha[] = "alpha"; -static const char kBeta[] = "beta"; -static const char kGamma[] = "gamma"; -static const char kOverrideEnabled[] = "overrideEnabled"; -} - DeviceOrientationInspectorAgent::~DeviceOrientationInspectorAgent() = default; DeviceOrientationInspectorAgent::DeviceOrientationInspectorAgent( InspectedFrames* inspected_frames) : inspected_frames_(inspected_frames), - sensor_agent_(new SensorInspectorAgent(inspected_frames->Root())) {} + sensor_agent_(new SensorInspectorAgent(inspected_frames->Root())), + enabled_(&agent_state_, /*default_value=*/false), + alpha_(&agent_state_, /*default_value=*/0.0), + beta_(&agent_state_, /*default_value=*/0.0), + gamma_(&agent_state_, /*default_value=*/0.0) {} void DeviceOrientationInspectorAgent::Trace(blink::Visitor* visitor) { visitor->Trace(inspected_frames_); @@ -44,11 +41,10 @@ Response DeviceOrientationInspectorAgent::setDeviceOrientationOverride( double alpha, double beta, double gamma) { - state_->setBoolean(DeviceOrientationInspectorAgentState::kOverrideEnabled, - true); - state_->setDouble(DeviceOrientationInspectorAgentState::kAlpha, alpha); - state_->setDouble(DeviceOrientationInspectorAgentState::kBeta, beta); - state_->setDouble(DeviceOrientationInspectorAgentState::kGamma, gamma); + enabled_.Set(true); + alpha_.Set(alpha); + beta_.Set(beta); + gamma_.Set(gamma); if (Controller()) { Controller()->SetOverride( DeviceOrientationData::Create(alpha, beta, gamma, false)); @@ -58,17 +54,11 @@ Response DeviceOrientationInspectorAgent::setDeviceOrientationOverride( } Response DeviceOrientationInspectorAgent::clearDeviceOrientationOverride() { - state_->setBoolean(DeviceOrientationInspectorAgentState::kOverrideEnabled, - false); - if (Controller()) - Controller()->ClearOverride(); - sensor_agent_->Disable(); - return Response::OK(); + return disable(); } Response DeviceOrientationInspectorAgent::disable() { - state_->setBoolean(DeviceOrientationInspectorAgentState::kOverrideEnabled, - false); + agent_state_.ClearAllFields(); if (Controller()) Controller()->ClearOverride(); sensor_agent_->Disable(); @@ -76,20 +66,12 @@ Response DeviceOrientationInspectorAgent::disable() { } void DeviceOrientationInspectorAgent::Restore() { - if (!Controller()) + if (!Controller() || !enabled_.Get()) return; - if (state_->booleanProperty( - DeviceOrientationInspectorAgentState::kOverrideEnabled, false)) { - double alpha = 0; - state_->getDouble(DeviceOrientationInspectorAgentState::kAlpha, &alpha); - double beta = 0; - state_->getDouble(DeviceOrientationInspectorAgentState::kBeta, &beta); - double gamma = 0; - state_->getDouble(DeviceOrientationInspectorAgentState::kGamma, &gamma); - Controller()->SetOverride( - DeviceOrientationData::Create(alpha, beta, gamma, false)); - sensor_agent_->SetOrientationSensorOverride(alpha, beta, gamma); - } + Controller()->SetOverride(DeviceOrientationData::Create( + alpha_.Get(), beta_.Get(), gamma_.Get(), false)); + sensor_agent_->SetOrientationSensorOverride(alpha_.Get(), beta_.Get(), + gamma_.Get()); } void DeviceOrientationInspectorAgent::DidCommitLoadForLocalFrame( diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_inspector_agent.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_inspector_agent.h index 67cdd984b70..e9d4bb27b00 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_inspector_agent.h +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_orientation_inspector_agent.h @@ -38,7 +38,10 @@ class MODULES_EXPORT DeviceOrientationInspectorAgent final Member<InspectedFrames> inspected_frames_; Member<SensorInspectorAgent> sensor_agent_; - + InspectorAgentState::Boolean enabled_; + InspectorAgentState::Double alpha_; + InspectorAgentState::Double beta_; + InspectorAgentState::Double gamma_; DISALLOW_COPY_AND_ASSIGN(DeviceOrientationInspectorAgent); }; diff --git a/chromium/third_party/blink/renderer/modules/device_orientation/device_sensor_event_pump.h b/chromium/third_party/blink/renderer/modules/device_orientation/device_sensor_event_pump.h index bf83aeb8c40..e64fabaa170 100644 --- a/chromium/third_party/blink/renderer/modules/device_orientation/device_sensor_event_pump.h +++ b/chromium/third_party/blink/renderer/modules/device_orientation/device_sensor_event_pump.h @@ -14,8 +14,6 @@ #include "services/device/public/cpp/generic_sensor/sensor_reading.h" #include "services/device/public/cpp/generic_sensor/sensor_reading_shared_buffer_reader.h" #include "services/device/public/mojom/sensor_provider.mojom-blink.h" -#include "third_party/blink/public/platform/modules/device_orientation/web_device_motion_listener.h" -#include "third_party/blink/public/platform/modules/device_orientation/web_device_orientation_listener.h" #include "third_party/blink/renderer/platform/timer.h" #include "third_party/blink/renderer/platform/wtf/functional.h" @@ -23,7 +21,6 @@ namespace blink { class LocalFrame; -template <typename ListenerType> class DeviceSensorEventPump { public: // Default rate for firing events. @@ -55,8 +52,7 @@ class DeviceSensorEventPump { SUSPENDED }; - virtual void Start(LocalFrame* frame, - blink::WebPlatformEventListener* listener) { + virtual void Start(LocalFrame* frame) { DVLOG(2) << "requested start"; if (state_ != PumpState::STOPPED) @@ -67,7 +63,6 @@ class DeviceSensorEventPump { state_ = PumpState::PENDING_START; DCHECK(!is_observing_); - listener_ = static_cast<ListenerType*>(listener); is_observing_ = true; SendStartMessage(frame); @@ -86,7 +81,6 @@ class DeviceSensorEventPump { timer_.Stop(); DCHECK(is_observing_); - listener_ = nullptr; is_observing_ = false; SendStopMessage(); @@ -134,8 +128,6 @@ class DeviceSensorEventPump { // TaskRunnerTimer class virtual void FireEvent(TimerBase*) = 0; - ListenerType* listener() { return listener_; } - struct SensorEntry : public device::mojom::blink::SensorClient { SensorEntry(DeviceSensorEventPump* pump, device::mojom::blink::SensorType sensor_type) @@ -334,7 +326,6 @@ class DeviceSensorEventPump { PumpState state_; bool is_observing_ = false; - ListenerType* listener_ = nullptr; TaskRunnerTimer<DeviceSensorEventPump> timer_; DISALLOW_COPY_AND_ASSIGN(DeviceSensorEventPump); |