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