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

Side by Side Diff: content/browser/battery_status/battery_status_message_filter.cc

Issue 10024013: chromeos: Add support for the battery status API on chromeos. Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: . Created 8 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "content/browser/battery_status/battery_status_message_filter.h"
6
7 #include "base/bind.h"
8 #include "base/bind_helpers.h"
9 #include "content/browser/battery_status/battery_status_provider_impl.h"
10 #include "content/common/battery_status_messages.h"
11 #include "content/public/browser/power_supply_status.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebBatteryStatus.h"
13
14 namespace content {
15
16 class BatteryStatusMessageFilter::PowerManagerObserver :
17 public BatteryStatusProviderImpl::Observer {
18 public:
19 explicit PowerManagerObserver(BatteryStatusMessageFilter* filter)
20 : filter_(filter) {
21 BatteryStatusProviderImpl::GetInstance()->AddObserver(this);
22 }
23
24 virtual ~PowerManagerObserver() {
25 BatteryStatusProviderImpl::GetInstance()->RemoveObserver(this);
26 }
27
28 void DestroyOnUI() {
29 filter_ = NULL;
30 BrowserThread::DeleteOnUIThread::Destruct(this);
31 }
32
33 private:
34 virtual void PowerChanged(
35 const PowerSupplyStatus& power_status) OVERRIDE {
36 if (filter_)
37 filter_->SendUpdates(power_status);
38 }
39
40 BatteryStatusMessageFilter* filter_;
41
42 DISALLOW_COPY_AND_ASSIGN(PowerManagerObserver);
43 };
44
45 BatteryStatusMessageFilter::BatteryStatusMessageFilter() {
46 }
47
48 void BatteryStatusMessageFilter::SendUpdates(
49 const PowerSupplyStatus& power_status) {
50 WebKit::WebBatteryStatus web_status;
51 web_status.charging = power_status.line_power_on;
52 web_status.chargingTime = power_status.battery_seconds_to_full;
53 web_status.dischargingTime = power_status.battery_seconds_to_empty;
54 web_status.level = power_status.battery_percentage / 100.;
55
56 Send(new BatteryStatusMsg_Updated(web_status));
57 }
58
59 BatteryStatusMessageFilter::~BatteryStatusMessageFilter() {
60 if (observer_.get()) {
61 // This destructor is in the IO thread. But we need to destroy the observer
62 // in the UI thread.
63 observer_.release()->DestroyOnUI();
64 }
65 }
66
67 void BatteryStatusMessageFilter::OnStartUpdating() {
68 if (!observer_.get())
69 observer_.reset(new PowerManagerObserver(this));
70 }
71
72 void BatteryStatusMessageFilter::OnStopUpdating() {
73 observer_.reset();
74 }
75
76 void BatteryStatusMessageFilter::OverrideThreadForMessage(
77 const IPC::Message& message,
78 BrowserThread::ID* thread) {
79 if (IPC_MESSAGE_CLASS(message) == BatteryStatusMsgStart)
80 *thread = BrowserThread::UI;
81 }
82
83 bool BatteryStatusMessageFilter::OnMessageReceived(const IPC::Message& message,
84 bool* message_was_ok) {
85 bool handled = true;
86 IPC_BEGIN_MESSAGE_MAP_EX(BatteryStatusMessageFilter, message, *message_was_ok)
87 IPC_MESSAGE_HANDLER(BatteryStatusHostMsg_StartUpdating, OnStartUpdating)
88 IPC_MESSAGE_HANDLER(BatteryStatusHostMsg_StopUpdating, OnStopUpdating)
89 IPC_MESSAGE_UNHANDLED(handled = false)
90 IPC_END_MESSAGE_MAP_EX()
91 return handled;
92 }
93
94 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698