forked from chromium/chromium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Battery Status API support in content/renderer and IPC messages.
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
Showing
13 changed files
with
281 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
timvolodine@chromium.org |
55 changes: 55 additions & 0 deletions
55
content/renderer/battery_status/battery_status_dispatcher.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
content/renderer/battery_status/battery_status_dispatcher.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
123
content/renderer/battery_status/battery_status_dispatcher_unittest.cc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters