Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(679)

Unified Diff: content/child/power_monitor_broadcast_source.cc

Issue 17074009: Created multi-process-friendly PowerMonitor interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixing windows unit test errors Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: content/child/power_monitor_broadcast_source.cc
diff --git a/content/child/power_monitor_broadcast_source.cc b/content/child/power_monitor_broadcast_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b3f72ed485b6e3a1cc7fe486b35db4e626cb118c
--- /dev/null
+++ b/content/child/power_monitor_broadcast_source.cc
@@ -0,0 +1,108 @@
+// Copyright (c) 2013 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/child/power_monitor_broadcast_source.h"
+
+#include "base/message_loop/message_loop.h"
+#include "content/common/power_monitor_messages.h"
+
+namespace content {
+
+class PowerMessageFilter : public IPC::ChannelProxy::MessageFilter {
+ public:
+ PowerMessageFilter(
+ PowerMonitorBroadcastSource* source,
+ scoped_refptr<base::MessageLoopProxy> message_loop)
+ : source_(source),
+ message_loop_(message_loop) {
+ }
+
+ virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP(PowerMessageFilter, message)
+ IPC_MESSAGE_HANDLER(PowerMonitorMsg_PowerStateChange, OnPowerStateChange)
+ IPC_MESSAGE_HANDLER(PowerMonitorMsg_Suspend, OnSuspend)
+ IPC_MESSAGE_HANDLER(PowerMonitorMsg_Resume, OnResume)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP()
+ return handled;
+ }
+
+ void RemoveSource() {
+ source_ = NULL;
+ }
+
+ private:
+ friend class base::RefCounted<PowerMessageFilter>;
+
+ virtual ~PowerMessageFilter() {};
+
+ void OnPowerStateChange(bool on_battery_power) {
+ message_loop_->PostTask(FROM_HERE,
+ base::Bind(&PowerMessageFilter::NotifySourcePowerStateChange, this,
+ on_battery_power));
+ }
+ void OnSuspend() {
+ message_loop_->PostTask(FROM_HERE,
+ base::Bind(&PowerMessageFilter::NotifySourceSuspend, this));
+ }
+ void OnResume() {
+ message_loop_->PostTask(FROM_HERE,
+ base::Bind(&PowerMessageFilter::NotifySourceResume, this));
+ }
+
+ void NotifySourcePowerStateChange(bool on_battery_power) {
+ if (source_)
+ source_->OnPowerStateChange(on_battery_power);
+ }
+ void NotifySourceSuspend() {
+ if (source_)
+ source_->OnSuspend();
+ }
+ void NotifySourceResume() {
+ if (source_)
+ source_->OnResume();
+ }
+
+ // source_ should only be accessed on the thread associated with
+ // message_loop_.
+ PowerMonitorBroadcastSource* source_;
+ scoped_refptr<base::MessageLoopProxy> message_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(PowerMessageFilter);
+};
+
+PowerMonitorBroadcastSource::PowerMonitorBroadcastSource()
+ : last_reported_battery_power_state_(false) {
+ message_filter_ = new PowerMessageFilter(this,
+ base::MessageLoopProxy::current());
+}
+
+PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
+ message_filter_->RemoveSource();
+}
+
+IPC::ChannelProxy::MessageFilter*
+PowerMonitorBroadcastSource::GetMessageFilter() {
+ return message_filter_.get();
+}
+
+bool PowerMonitorBroadcastSource::IsOnBatteryPowerImpl() {
+ return last_reported_battery_power_state_;
+}
+
+void PowerMonitorBroadcastSource::OnPowerStateChange(bool on_battery_power) {
+ last_reported_battery_power_state_ = on_battery_power;
+ ProcessPowerEvent(PowerMonitorSource::POWER_STATE_EVENT);
+}
+
+void PowerMonitorBroadcastSource::OnSuspend() {
+ ProcessPowerEvent(PowerMonitorSource::SUSPEND_EVENT);
+}
+
+void PowerMonitorBroadcastSource::OnResume() {
+ ProcessPowerEvent(PowerMonitorSource::RESUME_EVENT);
+}
+
+} // namespace content
« no previous file with comments | « content/child/power_monitor_broadcast_source.h ('k') | content/child/power_monitor_broadcast_source_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698