Skip to content

Commit

Permalink
Add Battery Status API support in content/renderer and IPC messages.
Browse files Browse the repository at this point in the history
This patch defines the async IPCs needed for the Battery Status API.
It also implements the platform-side part with a
setBatteryStatusListener() method and a dispatcher class for
propagating messages to blink. Unit tests for the dispatcher
class are also included.

BUG=122593,360068

Review URL: https://codereview.chromium.org/252113006

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270504 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
timvolodine@chromium.org committed May 14, 2014
1 parent 649e4fb commit 5981d40
Show file tree
Hide file tree
Showing 13 changed files with 281 additions and 1 deletion.
1 change: 1 addition & 0 deletions content/common/DEPS
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
32 changes: 32 additions & 0 deletions content/common/battery_status_messages.h
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions content/common/content_message_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions content/content_common.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 2 additions & 0 deletions content/content_renderer.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions content/content_tests.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
1 change: 1 addition & 0 deletions content/renderer/battery_status/OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
timvolodine@chromium.org
55 changes: 55 additions & 0 deletions content/renderer/battery_status/battery_status_dispatcher.cc
Original file line number Diff line number Diff line change
@@ -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
44 changes: 44 additions & 0 deletions content/renderer/battery_status/battery_status_dispatcher.h
Original file line number Diff line number Diff line change
@@ -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_
123 changes: 123 additions & 0 deletions content/renderer/battery_status/battery_status_dispatcher_unittest.cc
Original file line number Diff line number Diff line change
@@ -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
13 changes: 13 additions & 0 deletions content/renderer/renderer_webkitplatformsupport_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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
7 changes: 6 additions & 1 deletion content/renderer/renderer_webkitplatformsupport_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class WebScreenOrientationListener;
}

namespace content {
class BatteryStatusDispatcher;
class DeviceMotionEventPump;
class DeviceOrientationEventPump;
class QuotaMessageFilter;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
};

Expand Down
1 change: 1 addition & 0 deletions ipc/ipc_message_start.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ enum IPCMessageStart {
TranslateMsgStart,
PushMessagingMsgStart,
GinJavaBridgeMsgStart,
BatteryStatusMsgStart,
LastIPCMsgStart // Must come last.
};

Expand Down

0 comments on commit 5981d40

Please sign in to comment.