summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--content/common/DEPS1
-rw-r--r--content/common/battery_status_messages.h32
-rw-r--r--content/common/content_message_generator.h1
-rw-r--r--content/content_common.gypi1
-rw-r--r--content/content_renderer.gypi2
-rw-r--r--content/content_tests.gypi1
-rw-r--r--content/renderer/battery_status/OWNERS1
-rw-r--r--content/renderer/battery_status/battery_status_dispatcher.cc55
-rw-r--r--content/renderer/battery_status/battery_status_dispatcher.h44
-rw-r--r--content/renderer/battery_status/battery_status_dispatcher_unittest.cc123
-rw-r--r--content/renderer/renderer_webkitplatformsupport_impl.cc13
-rw-r--r--content/renderer/renderer_webkitplatformsupport_impl.h7
-rw-r--r--ipc/ipc_message_start.h1
13 files changed, 281 insertions, 1 deletions
diff --git a/content/common/DEPS b/content/common/DEPS
index 645304d..afbcc16 100644
--- a/content/common/DEPS
+++ b/content/common/DEPS
@@ -8,6 +8,7 @@ include_rules = [
# No inclusion of WebKit from the browser, other than strictly enum/POD,
# header-only types, and some selected common code.
"-third_party/WebKit",
+ "+third_party/WebKit/public/platform/WebBatteryStatus.h",
"+third_party/WebKit/public/platform/WebCString.h",
"+third_party/WebKit/public/platform/WebDeviceMotionData.h",
"+third_party/WebKit/public/platform/WebDeviceOrientationData.h",
diff --git a/content/common/battery_status_messages.h b/content/common/battery_status_messages.h
new file mode 100644
index 0000000..dd09ed0
--- /dev/null
+++ b/content/common/battery_status_messages.h
@@ -0,0 +1,32 @@
+// 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.
+
+// IPC messages for Battery Status API.
+// Multiply-included message file, hence no include guard.
+
+#include "ipc/ipc_message_macros.h"
+#include "third_party/WebKit/public/platform/WebBatteryStatus.h"
+
+#undef IPC_MESSAGE_EXPORT
+#define IPC_MESSAGE_EXPORT CONTENT_EXPORT
+#define IPC_MESSAGE_START BatteryStatusMsgStart
+
+IPC_STRUCT_TRAITS_BEGIN(blink::WebBatteryStatus)
+ IPC_STRUCT_TRAITS_MEMBER(charging)
+ IPC_STRUCT_TRAITS_MEMBER(chargingTime)
+ IPC_STRUCT_TRAITS_MEMBER(dischargingTime)
+ IPC_STRUCT_TRAITS_MEMBER(level)
+IPC_STRUCT_TRAITS_END()
+
+// Notifies the browser process that the renderer process wants
+// to listen to battery status updates.
+IPC_MESSAGE_CONTROL0(BatteryStatusHostMsg_Start)
+
+// Notifies the render process with new battery status data.
+IPC_MESSAGE_CONTROL1(BatteryStatusMsg_DidChange,
+ blink::WebBatteryStatus /* new status */)
+
+// Notifies the browser process that the renderer process is not using the
+// battery status data anymore.
+IPC_MESSAGE_CONTROL0(BatteryStatusHostMsg_Stop)
diff --git a/content/common/content_message_generator.h b/content/common/content_message_generator.h
index 07943e8..f9dfef1 100644
--- a/content/common/content_message_generator.h
+++ b/content/common/content_message_generator.h
@@ -8,6 +8,7 @@
#include "content/common/accessibility_messages.h"
#include "content/common/appcache_messages.h"
+#include "content/common/battery_status_messages.h"
#include "content/common/browser_plugin/browser_plugin_messages.h"
#include "content/common/cc_messages.h"
#include "content/common/clipboard_messages.h"
diff --git a/content/content_common.gypi b/content/content_common.gypi
index 61cc59b..e28b1cb 100644
--- a/content/content_common.gypi
+++ b/content/content_common.gypi
@@ -140,6 +140,7 @@
'common/android/surface_texture_peer.cc',
'common/android/surface_texture_peer.h',
'common/appcache_messages.h',
+ 'common/battery_status_messages.h',
'common/browser_plugin/browser_plugin_constants.cc',
'common/browser_plugin/browser_plugin_constants.h',
'common/browser_plugin/browser_plugin_messages.h',
diff --git a/content/content_renderer.gypi b/content/content_renderer.gypi
index 80fd4ec..dc3871e 100644
--- a/content/content_renderer.gypi
+++ b/content/content_renderer.gypi
@@ -91,6 +91,8 @@
'renderer/android/phone_number_detector.h',
'renderer/android/synchronous_compositor_factory.cc',
'renderer/android/synchronous_compositor_factory.h',
+ 'renderer/battery_status/battery_status_dispatcher.cc',
+ 'renderer/battery_status/battery_status_dispatcher.h',
'renderer/browser_plugin/browser_plugin.cc',
'renderer/browser_plugin/browser_plugin.h',
'renderer/browser_plugin/browser_plugin_bindings.cc',
diff --git a/content/content_tests.gypi b/content/content_tests.gypi
index 8f367de..1b0f9a8 100644
--- a/content/content_tests.gypi
+++ b/content/content_tests.gypi
@@ -637,6 +637,7 @@
'renderer/active_notification_tracker_unittest.cc',
'renderer/android/email_detector_unittest.cc',
'renderer/android/phone_number_detector_unittest.cc',
+ 'renderer/battery_status/battery_status_dispatcher_unittest.cc',
'renderer/bmp_image_decoder_unittest.cc',
'renderer/device_sensors/device_motion_event_pump_unittest.cc',
'renderer/device_sensors/device_orientation_event_pump_unittest.cc',
diff --git a/content/renderer/battery_status/OWNERS b/content/renderer/battery_status/OWNERS
new file mode 100644
index 0000000..1fd89e0
--- /dev/null
+++ b/content/renderer/battery_status/OWNERS
@@ -0,0 +1 @@
+timvolodine@chromium.org
diff --git a/content/renderer/battery_status/battery_status_dispatcher.cc b/content/renderer/battery_status/battery_status_dispatcher.cc
new file mode 100644
index 0000000..e6ab56e
--- /dev/null
+++ b/content/renderer/battery_status/battery_status_dispatcher.cc
@@ -0,0 +1,55 @@
+// 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 "battery_status_dispatcher.h"
+
+#include "base/logging.h"
+#include "content/common/battery_status_messages.h"
+#include "content/renderer/render_thread_impl.h"
+#include "third_party/WebKit/public/platform/WebBatteryStatusListener.h"
+
+namespace content {
+
+BatteryStatusDispatcher::BatteryStatusDispatcher(RenderThread* thread)
+ : listener_(0) {
+ if (thread)
+ thread->AddObserver(this);
+}
+
+BatteryStatusDispatcher::~BatteryStatusDispatcher() {
+ if (listener_)
+ Stop();
+}
+
+bool BatteryStatusDispatcher::SetListener(
+ blink::WebBatteryStatusListener* listener) {
+ listener_ = listener;
+ return listener ? Start() : Stop();
+}
+
+bool BatteryStatusDispatcher::OnControlMessageReceived(
+ const IPC::Message& message) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP(BatteryStatusDispatcher, message)
+ IPC_MESSAGE_HANDLER(BatteryStatusMsg_DidChange, OnDidChange)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ return handled;
+}
+
+bool BatteryStatusDispatcher::Start() {
+ return RenderThread::Get()->Send(new BatteryStatusHostMsg_Start());
+}
+
+bool BatteryStatusDispatcher::Stop() {
+ return RenderThread::Get()->Send(new BatteryStatusHostMsg_Stop());
+}
+
+void BatteryStatusDispatcher::OnDidChange(
+ const blink::WebBatteryStatus& status) {
+ if (listener_)
+ listener_->updateBatteryStatus(status);
+}
+
+} // namespace content
diff --git a/content/renderer/battery_status/battery_status_dispatcher.h b/content/renderer/battery_status/battery_status_dispatcher.h
new file mode 100644
index 0000000..0358fa7
--- /dev/null
+++ b/content/renderer/battery_status/battery_status_dispatcher.h
@@ -0,0 +1,44 @@
+// 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_BATTERY_STATUS_BATTERY_STATUS_DISPATCHER_H_
+#define CONTENT_RENDERER_BATTERY_STATUS_BATTERY_STATUS_DISPATCHER_H_
+
+#include "content/public/renderer/render_process_observer.h"
+
+namespace blink {
+class WebBatteryStatus;
+class WebBatteryStatusListener;
+}
+
+namespace content {
+class RenderThread;
+
+class CONTENT_EXPORT BatteryStatusDispatcher : public RenderProcessObserver {
+ public:
+ explicit BatteryStatusDispatcher(RenderThread* thread);
+ virtual ~BatteryStatusDispatcher();
+
+ // RenderProcessObserver method.
+ virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE;
+
+ // Sets the listener to receive battery status updates. Returns true if the
+ // registration was successful.
+ bool SetListener(blink::WebBatteryStatusListener* listener);
+
+ protected:
+ virtual bool Start();
+ virtual bool Stop();
+
+ private:
+ void OnDidChange(const blink::WebBatteryStatus& status);
+
+ blink::WebBatteryStatusListener* listener_;
+
+ DISALLOW_COPY_AND_ASSIGN(BatteryStatusDispatcher);
+};
+
+} // namespace content
+
+#endif // CONTENT_RENDERER_BATTERY_STATUS_BATTERY_STATUS_DISPATCHER_H_
diff --git a/content/renderer/battery_status/battery_status_dispatcher_unittest.cc b/content/renderer/battery_status/battery_status_dispatcher_unittest.cc
new file mode 100644
index 0000000..ecbcf393
--- /dev/null
+++ b/content/renderer/battery_status/battery_status_dispatcher_unittest.cc
@@ -0,0 +1,123 @@
+// 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 "battery_status_dispatcher.h"
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "content/common/battery_status_messages.h"
+#include "content/public/test/test_utils.h"
+#include "testing/gtest/include/gtest/gtest.h"
+#include "third_party/WebKit/public/platform/WebBatteryStatusListener.h"
+
+namespace content {
+
+class MockBatteryStatusListener : public blink::WebBatteryStatusListener {
+ public:
+ MockBatteryStatusListener() : did_change_battery_status_(false) { }
+ virtual ~MockBatteryStatusListener() { }
+
+ // blink::WebBatteryStatusListener method.
+ virtual void updateBatteryStatus(
+ const blink::WebBatteryStatus& status) OVERRIDE {
+ status_ = status;
+ did_change_battery_status_ = true;
+ }
+
+ const blink::WebBatteryStatus& status() const { return status_; }
+ bool did_change_battery_status() const { return did_change_battery_status_; }
+
+ private:
+ bool did_change_battery_status_;
+ blink::WebBatteryStatus status_;
+
+ DISALLOW_COPY_AND_ASSIGN(MockBatteryStatusListener);
+};
+
+class BatteryStatusDispatcherForTesting : public BatteryStatusDispatcher {
+ public:
+ BatteryStatusDispatcherForTesting()
+ : BatteryStatusDispatcher(0),
+ start_invoked_(false),
+ stop_invoked_(false) { }
+
+ virtual ~BatteryStatusDispatcherForTesting() { }
+
+ bool start_invoked() const { return start_invoked_; }
+ bool stop_invoked() const { return stop_invoked_; }
+
+ protected:
+ virtual bool Start() OVERRIDE {
+ start_invoked_ = true;
+ return true;
+ }
+
+ virtual bool Stop() OVERRIDE {
+ stop_invoked_ = true;
+ return true;
+ }
+
+ private:
+ bool start_invoked_;
+ bool stop_invoked_;
+
+ DISALLOW_COPY_AND_ASSIGN(BatteryStatusDispatcherForTesting);
+};
+
+TEST(BatteryStatusDispatcherTest, Start) {
+ MockBatteryStatusListener listener;
+ BatteryStatusDispatcherForTesting dispatcher;
+
+ EXPECT_FALSE(dispatcher.start_invoked());
+ EXPECT_FALSE(dispatcher.stop_invoked());
+
+ dispatcher.SetListener(&listener);
+ EXPECT_TRUE(dispatcher.start_invoked());
+
+ dispatcher.SetListener(0);
+ EXPECT_TRUE(dispatcher.stop_invoked());
+}
+
+TEST(BatteryStatusDispatcherTest, UpdateListener) {
+ MockBatteryStatusListener listener;
+ BatteryStatusDispatcherForTesting dispatcher;
+
+ blink::WebBatteryStatus status;
+ status.charging = true;
+ status.chargingTime = 100;
+ status.dischargingTime = 200;
+ status.level = 0.5;
+
+ dispatcher.SetListener(&listener);
+ EXPECT_TRUE(dispatcher.start_invoked());
+
+ BatteryStatusMsg_DidChange message(status);
+ dispatcher.OnControlMessageReceived(message);
+
+ const blink::WebBatteryStatus& received_status = listener.status();
+ EXPECT_TRUE(listener.did_change_battery_status());
+ EXPECT_EQ(status.charging, received_status.charging);
+ EXPECT_EQ(status.chargingTime, received_status.chargingTime);
+ EXPECT_EQ(status.dischargingTime, received_status.dischargingTime);
+ EXPECT_EQ(status.level, received_status.level);
+
+ dispatcher.SetListener(0);
+ EXPECT_TRUE(dispatcher.stop_invoked());
+}
+
+TEST(BatteryStatusDispatcherTest, NoUpdateWhenNullListener) {
+ MockBatteryStatusListener listener;
+ BatteryStatusDispatcherForTesting dispatcher;
+
+ dispatcher.SetListener(0);
+ EXPECT_FALSE(dispatcher.start_invoked());
+ EXPECT_TRUE(dispatcher.stop_invoked());
+
+ blink::WebBatteryStatus status;
+ BatteryStatusMsg_DidChange message(status);
+ dispatcher.OnControlMessageReceived(message);
+ EXPECT_FALSE(listener.did_change_battery_status());
+}
+
+} // namespace content
diff --git a/content/renderer/renderer_webkitplatformsupport_impl.cc b/content/renderer/renderer_webkitplatformsupport_impl.cc
index a2d212b..74f201b 100644
--- a/content/renderer/renderer_webkitplatformsupport_impl.cc
+++ b/content/renderer/renderer_webkitplatformsupport_impl.cc
@@ -37,6 +37,7 @@
#include "content/public/common/content_switches.h"
#include "content/public/common/webplugininfo.h"
#include "content/public/renderer/content_renderer_client.h"
+#include "content/renderer/battery_status/battery_status_dispatcher.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"
@@ -60,6 +61,7 @@
#include "media/filters/stream_parser_factory.h"
#include "net/base/mime_util.h"
#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/WebDeviceMotionListener.h"
#include "third_party/WebKit/public/platform/WebDeviceOrientationListener.h"
@@ -1181,4 +1183,15 @@ void RendererWebKitPlatformSupportImpl::queryStorageUsageAndQuota(
QuotaDispatcher::CreateWebStorageQuotaCallbacksWrapper(callbacks));
}
+//------------------------------------------------------------------------------
+
+void RendererWebKitPlatformSupportImpl::setBatteryStatusListener(
+ blink::WebBatteryStatusListener* listener) {
+ if (!battery_status_dispatcher_) {
+ battery_status_dispatcher_.reset(
+ new BatteryStatusDispatcher(RenderThreadImpl::current()));
+ }
+ battery_status_dispatcher_->SetListener(listener);
+}
+
} // namespace content
diff --git a/content/renderer/renderer_webkitplatformsupport_impl.h b/content/renderer/renderer_webkitplatformsupport_impl.h
index 6ef5bbb..b56f3e2 100644
--- a/content/renderer/renderer_webkitplatformsupport_impl.h
+++ b/content/renderer/renderer_webkitplatformsupport_impl.h
@@ -35,6 +35,7 @@ class WebScreenOrientationListener;
}
namespace content {
+class BatteryStatusDispatcher;
class DeviceMotionEventPump;
class DeviceOrientationEventPump;
class QuotaMessageFilter;
@@ -145,9 +146,11 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl
virtual void vibrate(unsigned int milliseconds);
virtual void cancelVibration();
virtual void setScreenOrientationListener(
- blink::WebScreenOrientationListener*) OVERRIDE;
+ blink::WebScreenOrientationListener*) OVERRIDE;
virtual void lockOrientation(blink::WebScreenOrientationLockType) OVERRIDE;
virtual void unlockOrientation() OVERRIDE;
+ virtual void setBatteryStatusListener(
+ blink::WebBatteryStatusListener* listener) OVERRIDE;
// Disables the WebSandboxSupport implementation for testing.
// Tests that do not set up a full sandbox environment should call
@@ -230,6 +233,8 @@ class CONTENT_EXPORT RendererWebKitPlatformSupportImpl
scoped_ptr<blink::WebScrollbarBehavior> web_scrollbar_behavior_;
+ scoped_ptr<BatteryStatusDispatcher> battery_status_dispatcher_;
+
DISALLOW_COPY_AND_ASSIGN(RendererWebKitPlatformSupportImpl);
};
diff --git a/ipc/ipc_message_start.h b/ipc/ipc_message_start.h
index 0d8193c..337b1aa 100644
--- a/ipc/ipc_message_start.h
+++ b/ipc/ipc_message_start.h
@@ -103,6 +103,7 @@ enum IPCMessageStart {
TranslateMsgStart,
PushMessagingMsgStart,
GinJavaBridgeMsgStart,
+ BatteryStatusMsgStart,
LastIPCMsgStart // Must come last.
};