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