OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 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 "chrome/browser/chromeos/cros_power_library.h" | |
6 | |
7 #include "base/message_loop.h" | |
8 #include "base/string_util.h" | |
9 #include "chrome/browser/chrome_thread.h" | |
10 #include "chrome/browser/chromeos/cros_library.h" | |
11 | |
12 // Allows InvokeLater without adding refcounting. This class is a Singleton and | |
13 // won't be deleted until it's last InvokeLater is run. | |
14 template <> | |
15 struct RunnableMethodTraits<CrosPowerLibrary> { | |
16 void RetainCallee(CrosPowerLibrary* obj) {} | |
17 void ReleaseCallee(CrosPowerLibrary* obj) {} | |
18 }; | |
19 | |
20 CrosPowerLibrary::CrosPowerLibrary() : status_(chromeos::PowerStatus()) { | |
21 if (CrosLibrary::loaded()) { | |
22 Init(); | |
23 } | |
24 } | |
25 | |
26 CrosPowerLibrary::~CrosPowerLibrary() { | |
27 if (CrosLibrary::loaded()) { | |
28 chromeos::DisconnectPowerStatus(power_status_connection_); | |
29 } | |
30 } | |
31 | |
32 // static | |
33 CrosPowerLibrary* CrosPowerLibrary::Get() { | |
34 return Singleton<CrosPowerLibrary>::get(); | |
35 } | |
36 | |
37 // static | |
38 bool CrosPowerLibrary::loaded() { | |
39 return CrosLibrary::loaded(); | |
40 } | |
41 | |
42 void CrosPowerLibrary::AddObserver(Observer* observer) { | |
43 observers_.AddObserver(observer); | |
44 } | |
45 | |
46 void CrosPowerLibrary::RemoveObserver(Observer* observer) { | |
47 observers_.RemoveObserver(observer); | |
48 } | |
49 | |
50 bool CrosPowerLibrary::line_power_on() const { | |
51 return status_.line_power_on; | |
52 } | |
53 | |
54 bool CrosPowerLibrary::battery_is_present() const { | |
55 return status_.battery_is_present; | |
56 } | |
57 | |
58 bool CrosPowerLibrary::battery_fully_charged() const { | |
59 return status_.battery_state == chromeos::BATTERY_STATE_FULLY_CHARGED; | |
60 } | |
61 | |
62 double CrosPowerLibrary::battery_percentage() const { | |
63 return status_.battery_percentage; | |
64 } | |
65 | |
66 base::TimeDelta CrosPowerLibrary::battery_time_to_empty() const { | |
67 return base::TimeDelta::FromSeconds(status_.battery_time_to_empty); | |
68 } | |
69 | |
70 base::TimeDelta CrosPowerLibrary::battery_time_to_full() const { | |
71 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); | |
72 } | |
73 | |
74 // static | |
75 void CrosPowerLibrary::PowerStatusChangedHandler(void* object, | |
76 const chromeos::PowerStatus& status) { | |
77 CrosPowerLibrary* power = static_cast<CrosPowerLibrary*>(object); | |
78 power->UpdatePowerStatus(status); | |
79 } | |
80 | |
81 void CrosPowerLibrary::Init() { | |
82 power_status_connection_ = chromeos::MonitorPowerStatus( | |
83 &PowerStatusChangedHandler, this); | |
84 } | |
85 | |
86 void CrosPowerLibrary::UpdatePowerStatus(const chromeos::PowerStatus& status) { | |
87 // Make sure we run on UI thread. | |
88 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | |
89 ChromeThread::PostTask( | |
90 ChromeThread::UI, FROM_HERE, | |
91 NewRunnableMethod(this, &CrosPowerLibrary::UpdatePowerStatus, status)); | |
92 return; | |
93 } | |
94 | |
95 DLOG(INFO) << "Power" << | |
96 " lpo=" << status.line_power_on << | |
97 " sta=" << status.battery_state << | |
98 " per=" << status.battery_percentage << | |
99 " tte=" << status.battery_time_to_empty << | |
100 " ttf=" << status.battery_time_to_full; | |
101 status_ = status; | |
102 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | |
103 } | |
OLD | NEW |