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

Unified Diff: content/common/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: Killing Nits 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/common/power_monitor_broadcast_source.cc
diff --git a/content/common/power_monitor_broadcast_source.cc b/content/common/power_monitor_broadcast_source.cc
new file mode 100644
index 0000000000000000000000000000000000000000..baaceb8428d9ac51e623dc701f4ee9ca331b93fd
--- /dev/null
+++ b/content/common/power_monitor_broadcast_source.cc
@@ -0,0 +1,86 @@
+// 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/common/power_monitor_broadcast_source.h"
+
+#include "content/common/power_monitor_messages.h"
+
+namespace content {
+
+PowerMonitorBroadcastSource::PowerMonitorBroadcastSource(
+ scoped_refptr<base::MessageLoopProxy> message_loop)
+ : last_reported_battery_power_state_(false),
+ weak_factory_(this) {
+ message_filter_ = new PowerMessageFilter(weak_factory_.GetWeakPtr(),
jar (doing other things) 2013/07/16 17:15:33 nit: undent 2
+ message_loop);
+}
+
+PowerMonitorBroadcastSource::~PowerMonitorBroadcastSource() {
+}
+
+// MessageFilters are owned by the Channel they are added to, so this class does
+// not need to retain a reference.
+IPC::ChannelProxy::MessageFilter*
+PowerMonitorBroadcastSource::MessageFilter() {
+ 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);
+}
+
+PowerMonitorBroadcastSource::PowerMessageFilter::PowerMessageFilter(
+ base::WeakPtr<PowerMonitorBroadcastSource> source,
+ scoped_refptr<base::MessageLoopProxy> message_loop)
+ : source_(source),
+ message_loop_(message_loop) {
+}
+
+PowerMonitorBroadcastSource::PowerMessageFilter::~PowerMessageFilter() {
+}
+
+bool PowerMonitorBroadcastSource::PowerMessageFilter::OnMessageReceived(
+ const IPC::Message& message) {
+ bool msg_is_ok = false;
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP_EX(PowerMessageFilter, message, msg_is_ok)
+ 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_EX()
+ return handled && msg_is_ok;
+}
+
+void PowerMonitorBroadcastSource::PowerMessageFilter::OnPowerStateChange(
+ bool on_battery_power) {
+ message_loop_->PostTask(FROM_HERE,
+ base::Bind(&PowerMonitorBroadcastSource::OnPowerStateChange, source_,
+ on_battery_power));
+}
+
+void PowerMonitorBroadcastSource::PowerMessageFilter::OnSuspend() {
+ message_loop_->PostTask(FROM_HERE,
+ base::Bind(&PowerMonitorBroadcastSource::OnSuspend, source_));
+}
+
+void PowerMonitorBroadcastSource::PowerMessageFilter::OnResume() {
+ message_loop_->PostTask(FROM_HERE,
+ base::Bind(&PowerMonitorBroadcastSource::OnResume, source_));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698