From 13a878290c1475ad9f99b9fb7c87ace39e0ad6e5 Mon Sep 17 00:00:00 2001 From: "rijubrata.bhaumik@intel.com" Date: Fri, 27 Jun 2014 11:51:20 +0000 Subject: [DeviceLight] Add renderer+common parts Adding the Renderer and Common part in the content side for the DeviceLight API. Add Unit tests for deviceLight pump Add DeviceLight support in renderer_webkitplatformsupport_impl.h|cc The default polling time interval of DeviceLightEventPump is right now set as of 200 milliseconds. (device motion/orientation = 50ms) In Firefox (Android) implementation, DeviceLight's frequency of update is set as SensorManager.SENSOR_DELAY_NORMAL which in Android parlance translates to 200 milliseconds delay which is 5 times a second. Ofcourse the delay is only a suggested delay, system typically uses a smaller delay. BUG=336424 Review URL: https://codereview.chromium.org/286793002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@280306 0039d316-1c4b-4281-b951-d872f2087c98 --- .../device_sensors/device_light_event_pump.cc | 74 +++++++++++ .../device_sensors/device_light_event_pump.h | 51 ++++++++ .../device_light_event_pump_unittest.cc | 142 +++++++++++++++++++++ .../renderer_webkitplatformsupport_impl.cc | 31 ++++- .../renderer/renderer_webkitplatformsupport_impl.h | 5 + 5 files changed, 302 insertions(+), 1 deletion(-) create mode 100644 content/renderer/device_sensors/device_light_event_pump.cc create mode 100644 content/renderer/device_sensors/device_light_event_pump.h create mode 100644 content/renderer/device_sensors/device_light_event_pump_unittest.cc (limited to 'content/renderer') diff --git a/content/renderer/device_sensors/device_light_event_pump.cc b/content/renderer/device_sensors/device_light_event_pump.cc new file mode 100644 index 0000000..31b0970 --- /dev/null +++ b/content/renderer/device_sensors/device_light_event_pump.cc @@ -0,0 +1,74 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "content/renderer/device_sensors/device_light_event_pump.h" + +#include "content/common/device_sensors/device_light_messages.h" +#include "content/public/renderer/render_thread.h" +#include "third_party/WebKit/public/platform/WebDeviceLightListener.h" + +namespace { +// Default delay between subsequent firing of DeviceLight events. +const int kDefaultLightPumpDelayMillis = 200; +} // namespace + +namespace content { + +DeviceLightEventPump::DeviceLightEventPump() + : DeviceSensorEventPump(kDefaultLightPumpDelayMillis), + listener_(NULL), + last_seen_data_(-1) { +} + +DeviceLightEventPump::DeviceLightEventPump(int pump_delay_millis) + : DeviceSensorEventPump(pump_delay_millis), + listener_(NULL), + last_seen_data_(-1) { +} + +DeviceLightEventPump::~DeviceLightEventPump() { +} + +bool DeviceLightEventPump::SetListener( + blink::WebDeviceLightListener* listener) { + listener_ = listener; + return listener_ ? RequestStart() : Stop(); +} + +bool DeviceLightEventPump::OnControlMessageReceived( + const IPC::Message& message) { + bool handled = true; + IPC_BEGIN_MESSAGE_MAP(DeviceLightEventPump, message) + IPC_MESSAGE_HANDLER(DeviceLightMsg_DidStartPolling, OnDidStart) + IPC_MESSAGE_UNHANDLED(handled = false) + IPC_END_MESSAGE_MAP() + return handled; +} + +void DeviceLightEventPump::FireEvent() { + DCHECK(listener_); + DeviceLightData data; + bool did_return_light_data = reader_->GetLatestData(&data); + if (did_return_light_data && data.value != last_seen_data_) { + last_seen_data_ = data.value; + listener_->didChangeDeviceLight(data.value); + } +} + +bool DeviceLightEventPump::InitializeReader(base::SharedMemoryHandle handle) { + if (!reader_) + reader_.reset(new DeviceLightSharedMemoryReader()); + return reader_->Initialize(handle); +} + +bool DeviceLightEventPump::SendStartMessage() { + return RenderThread::Get()->Send(new DeviceLightHostMsg_StartPolling()); +} + +bool DeviceLightEventPump::SendStopMessage() { + last_seen_data_ = -1; + return RenderThread::Get()->Send(new DeviceLightHostMsg_StopPolling()); +} + +} // namespace content diff --git a/content/renderer/device_sensors/device_light_event_pump.h b/content/renderer/device_sensors/device_light_event_pump.h new file mode 100644 index 0000000..a998adf --- /dev/null +++ b/content/renderer/device_sensors/device_light_event_pump.h @@ -0,0 +1,51 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_LIGHT_EVENT_PUMP_H_ +#define CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_LIGHT_EVENT_PUMP_H_ + +#include "base/memory/scoped_ptr.h" +#include "content/common/device_sensors/device_light_data.h" +#include "content/renderer/device_sensors/device_sensor_event_pump.h" +#include "content/renderer/shared_memory_seqlock_reader.h" + +namespace blink { +class WebDeviceLightListener; +} + +namespace content { + +typedef SharedMemorySeqLockReader + DeviceLightSharedMemoryReader; + +class CONTENT_EXPORT DeviceLightEventPump : public DeviceSensorEventPump { + public: + DeviceLightEventPump(); + explicit DeviceLightEventPump(int pump_delay_millis); + virtual ~DeviceLightEventPump(); + + // Sets the listener to receive updates for device light data at + // regular intervals. Returns true if the registration was successful. + bool SetListener(blink::WebDeviceLightListener* listener); + + // RenderProcessObserver implementation. + virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE; + + protected: + // Methods overriden from base class DeviceSensorEventPump + virtual void FireEvent() OVERRIDE; + virtual bool InitializeReader(base::SharedMemoryHandle handle) OVERRIDE; + virtual bool SendStartMessage() OVERRIDE; + virtual bool SendStopMessage() OVERRIDE; + + blink::WebDeviceLightListener* listener_; + scoped_ptr reader_; + double last_seen_data_; + + DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPump); +}; + +} // namespace content + +#endif // CONTENT_RENDERER_DEVICE_SENSORS_DEVICE_LIGHT_EVENT_PUMP_H_ diff --git a/content/renderer/device_sensors/device_light_event_pump_unittest.cc b/content/renderer/device_sensors/device_light_event_pump_unittest.cc new file mode 100644 index 0000000..08182f7 --- /dev/null +++ b/content/renderer/device_sensors/device_light_event_pump_unittest.cc @@ -0,0 +1,142 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "device_light_event_pump.h" + +#include "base/message_loop/message_loop.h" +#include "content/common/device_sensors/device_light_hardware_buffer.h" +#include "content/public/test/test_utils.h" +#include "testing/gtest/include/gtest/gtest.h" +#include "third_party/WebKit/public/platform/WebDeviceLightListener.h" + +namespace content { + +class MockDeviceLightListener : public blink::WebDeviceLightListener { + public: + MockDeviceLightListener() : did_change_device_light_(false) {} + virtual ~MockDeviceLightListener() {} + + virtual void didChangeDeviceLight(double value) OVERRIDE { + data_.value = value; + did_change_device_light_ = true; + } + + bool did_change_device_light() const { return did_change_device_light_; } + + void set_did_change_device_light(bool value) { + did_change_device_light_ = value; + } + + const DeviceLightData& data() const { return data_; } + + private: + bool did_change_device_light_; + DeviceLightData data_; + + DISALLOW_COPY_AND_ASSIGN(MockDeviceLightListener); +}; + +class DeviceLightEventPumpForTesting : public DeviceLightEventPump { + public: + DeviceLightEventPumpForTesting() {} + virtual ~DeviceLightEventPumpForTesting() {} + + void OnDidStart(base::SharedMemoryHandle renderer_handle) { + DeviceLightEventPump::OnDidStart(renderer_handle); + } + virtual bool SendStartMessage() OVERRIDE { return true; } + virtual bool SendStopMessage() OVERRIDE { return true; } + virtual void FireEvent() OVERRIDE { + DeviceLightEventPump::FireEvent(); + Stop(); + base::MessageLoop::current()->QuitWhenIdle(); + } + + private: + DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpForTesting); +}; + +class DeviceLightEventPumpTest : public testing::Test { + public: + DeviceLightEventPumpTest() { + EXPECT_TRUE(shared_memory_.CreateAndMapAnonymous( + sizeof(DeviceLightHardwareBuffer))); + } + + protected: + virtual void SetUp() OVERRIDE { + const DeviceLightHardwareBuffer* null_buffer = NULL; + listener_.reset(new MockDeviceLightListener); + light_pump_.reset(new DeviceLightEventPumpForTesting); + buffer_ = static_cast(shared_memory_.memory()); + ASSERT_NE(null_buffer, buffer_); + ASSERT_TRUE(shared_memory_.ShareToProcess(base::GetCurrentProcessHandle(), + &handle_)); + } + + void InitBuffer() { + DeviceLightData& data = buffer_->data; + data.value = 1.0; + } + + MockDeviceLightListener* listener() { return listener_.get(); } + DeviceLightEventPumpForTesting* light_pump() { return light_pump_.get(); } + base::SharedMemoryHandle handle() { return handle_; } + DeviceLightHardwareBuffer* buffer() { return buffer_; } + + private: + scoped_ptr listener_; + scoped_ptr light_pump_; + base::SharedMemoryHandle handle_; + base::SharedMemory shared_memory_; + DeviceLightHardwareBuffer* buffer_; + + DISALLOW_COPY_AND_ASSIGN(DeviceLightEventPumpTest); +}; + +TEST_F(DeviceLightEventPumpTest, DidStartPolling) { + base::MessageLoopForUI loop; + + InitBuffer(); + + light_pump()->SetListener(listener()); + light_pump()->OnDidStart(handle()); + + base::MessageLoop::current()->Run(); + + const DeviceLightData& received_data = listener()->data(); + EXPECT_TRUE(listener()->did_change_device_light()); + EXPECT_EQ(1, static_cast(received_data.value)); +} + +TEST_F(DeviceLightEventPumpTest, DidStartPollingValuesEqual) { + base::MessageLoopForUI loop; + + InitBuffer(); + + light_pump()->SetListener(listener()); + light_pump()->OnDidStart(handle()); + + base::MessageLoop::current()->Run(); + + const DeviceLightData& received_data = listener()->data(); + EXPECT_TRUE(listener()->did_change_device_light()); + EXPECT_EQ(1, static_cast(received_data.value)); + + double last_seen_data = received_data.value; + // Set next value to be same as previous value. + buffer()->data.value = 1.0; + listener()->set_did_change_device_light(false); + base::MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(&DeviceLightEventPumpForTesting::FireEvent, + base::Unretained(light_pump()))); + base::MessageLoop::current()->Run(); + + // No change in device light as present value is same as previous value. + EXPECT_FALSE(listener()->did_change_device_light()); + EXPECT_EQ(last_seen_data, static_cast(received_data.value)); +} + +} // namespace content diff --git a/content/renderer/renderer_webkitplatformsupport_impl.cc b/content/renderer/renderer_webkitplatformsupport_impl.cc index 02fb2e7..1add621 100644 --- a/content/renderer/renderer_webkitplatformsupport_impl.cc +++ b/content/renderer/renderer_webkitplatformsupport_impl.cc @@ -39,6 +39,7 @@ #include "content/public/renderer/content_renderer_client.h" #include "content/renderer/battery_status/battery_status_dispatcher.h" #include "content/renderer/battery_status/fake_battery_status_dispatcher.h" +#include "content/renderer/device_sensors/device_light_event_pump.h" #include "content/renderer/device_sensors/device_motion_event_pump.h" #include "content/renderer/device_sensors/device_orientation_event_pump.h" #include "content/renderer/dom_storage/webstoragenamespace_impl.h" @@ -63,6 +64,7 @@ #include "net/base/net_util.h" #include "third_party/WebKit/public/platform/WebBatteryStatusListener.h" #include "third_party/WebKit/public/platform/WebBlobRegistry.h" +#include "third_party/WebKit/public/platform/WebDeviceLightListener.h" #include "third_party/WebKit/public/platform/WebDeviceMotionListener.h" #include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h" #include "third_party/WebKit/public/platform/WebFileInfo.h" @@ -139,7 +141,8 @@ namespace content { namespace { -static bool g_sandbox_enabled = true; +bool g_sandbox_enabled = true; +double g_test_device_light_data = -1; base::LazyInstance::Leaky g_test_device_motion_data = LAZY_INSTANCE_INITIALIZER; base::LazyInstance::Leaky @@ -1018,6 +1021,32 @@ blink::WebString RendererWebKitPlatformSupportImpl::convertIDNToUnicode( //------------------------------------------------------------------------------ +void RendererWebKitPlatformSupportImpl::setDeviceLightListener( + blink::WebDeviceLightListener* listener) { + if (g_test_device_light_data < 0) { + if (!device_light_event_pump_) { + device_light_event_pump_.reset(new DeviceLightEventPump); + device_light_event_pump_->Attach(RenderThreadImpl::current()); + } + device_light_event_pump_->SetListener(listener); + } else if (listener) { + // Testing mode: just echo the test data to the listener. + base::MessageLoopProxy::current()->PostTask( + FROM_HERE, + base::Bind(&blink::WebDeviceLightListener::didChangeDeviceLight, + base::Unretained(listener), + g_test_device_light_data)); + } +} + +// static +void RendererWebKitPlatformSupportImpl::SetMockDeviceLightDataForTesting( + double data) { + g_test_device_light_data = data; +} + +//------------------------------------------------------------------------------ + void RendererWebKitPlatformSupportImpl::setDeviceMotionListener( blink::WebDeviceMotionListener* listener) { if (g_test_device_motion_data == 0) { diff --git a/content/renderer/renderer_webkitplatformsupport_impl.h b/content/renderer/renderer_webkitplatformsupport_impl.h index 849eeb6..f575ab4 100644 --- a/content/renderer/renderer_webkitplatformsupport_impl.h +++ b/content/renderer/renderer_webkitplatformsupport_impl.h @@ -36,6 +36,7 @@ class WebGraphicsContext3DProvider; namespace content { class BatteryStatusDispatcher; +class DeviceLightEventPump; class DeviceMotionEventPump; class DeviceOrientationEventPump; class QuotaMessageFilter; @@ -136,6 +137,7 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl virtual blink::WebCompositorSupport* compositorSupport(); virtual blink::WebString convertIDNToUnicode( const blink::WebString& host, const blink::WebString& languages); + virtual void setDeviceLightListener(blink::WebDeviceLightListener* listener); virtual void setDeviceMotionListener( blink::WebDeviceMotionListener* listener); virtual void setDeviceOrientationListener( @@ -162,6 +164,8 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl // Returns the previous |enable| value. static bool SetSandboxEnabledForTesting(bool enable); + // Set a double to return when setDeviceLightListener is invoked. + static void SetMockDeviceLightDataForTesting(double data); // Set WebDeviceMotionData to return when setDeviceMotionListener is invoked. static void SetMockDeviceMotionDataForTesting( const blink::WebDeviceMotionData& data); @@ -208,6 +212,7 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl WebPublicSuffixListImpl public_suffix_list_; + scoped_ptr device_light_event_pump_; scoped_ptr device_motion_event_pump_; scoped_ptr device_orientation_event_pump_; -- cgit v1.1