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

Unified Diff: content/browser/battery_status/battery_status_message_filter.cc

Issue 258173004: [NO SUBMIT] Battery Status API skeleton implementation ASYNC Base URL: https://chromium.googlesource.com/chromium/src.git@April-17-battery-status-renderer-and-IPC
Patch Set: Created 6 years, 8 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/browser/battery_status/battery_status_message_filter.cc
diff --git a/content/browser/battery_status/battery_status_message_filter.cc b/content/browser/battery_status/battery_status_message_filter.cc
new file mode 100644
index 0000000000000000000000000000000000000000..57e8487ef4dca1ccecd7b6abd30d9d1947af0f6d
--- /dev/null
+++ b/content/browser/battery_status/battery_status_message_filter.cc
@@ -0,0 +1,58 @@
+// 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 "content/browser/battery_status/battery_status_message_filter.h"
+
+#include "content/common/battery_status_messages.h"
+
+namespace content {
+
+BatteryStatusMessageFilter::BatteryStatusMessageFilter()
+ : BrowserMessageFilter(BatteryStatusMsgStart),
+ is_started_(false) {
+ callback_ = base::Bind(&BatteryStatusMessageFilter::SendBatteryChange,
+ base::Unretained(this));
+}
+
+BatteryStatusMessageFilter::~BatteryStatusMessageFilter() {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
+ if (is_started_)
+ BatteryStatusService::GetInstance()->RemoveConsumer(callback_);
+}
+
+bool BatteryStatusMessageFilter::OnMessageReceived(
+ const IPC::Message& message,
+ bool* message_was_ok) {
+ bool handled = true;
+ IPC_BEGIN_MESSAGE_MAP_EX(BatteryStatusMessageFilter,
+ message,
+ *message_was_ok)
+ IPC_MESSAGE_HANDLER(BatteryStatusHostMsg_Start, OnBatteryStatusStart)
+ IPC_MESSAGE_HANDLER(BatteryStatusHostMsg_Stop, OnBatteryStatusStop)
+ IPC_MESSAGE_UNHANDLED(handled = false)
+ IPC_END_MESSAGE_MAP_EX()
+ return handled;
+}
+
+void BatteryStatusMessageFilter::OnBatteryStatusStart() {
+ DCHECK(!is_started_);
+ if (is_started_)
+ return;
+ is_started_ = true;
+ BatteryStatusService::GetInstance()->AddConsumer(callback_);
+}
+
+void BatteryStatusMessageFilter::OnBatteryStatusStop() {
+ DCHECK(is_started_);
+ if (!is_started_)
+ return;
+ is_started_ = false;
+ BatteryStatusService::GetInstance()->RemoveConsumer(callback_);
+}
+
+void BatteryStatusMessageFilter::SendBatteryChange(const blink::WebBatteryStatus& status) {
+ Send(new BatteryStatusMsg_DidChange(status));
+}
+
+} // namespace content

Powered by Google App Engine
This is Rietveld 408576698