Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/browser_thread.h" | 9 #include "chrome/browser/browser_thread.h" |
| 10 #include "chrome/browser/chromeos/cros/cros_library.h" | 10 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 11 #include "third_party/cros/chromeos_resume.h" | |
| 11 | 12 |
| 12 namespace chromeos { | 13 namespace chromeos { |
| 13 | 14 |
| 14 class PowerLibraryImpl : public PowerLibrary { | 15 class PowerLibraryImpl : public PowerLibrary { |
| 15 public: | 16 public: |
| 16 PowerLibraryImpl() | 17 PowerLibraryImpl() |
| 17 : power_status_connection_(NULL), | 18 : power_status_connection_(NULL), |
| 18 status_(chromeos::PowerStatus()) { | 19 status_(chromeos::PowerStatus()) { |
| 19 if (CrosLibrary::Get()->EnsureLoaded()) { | 20 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 20 Init(); | 21 Init(); |
| 21 } | 22 } |
| 22 } | 23 } |
| 23 | 24 |
| 24 ~PowerLibraryImpl() { | 25 ~PowerLibraryImpl() { |
| 25 if (power_status_connection_) { | 26 if (power_status_connection_) { |
| 26 chromeos::DisconnectPowerStatus(power_status_connection_); | 27 chromeos::DisconnectPowerStatus(power_status_connection_); |
|
Nikita (slow)
2011/02/15 15:23:20
set power_status_connection_ to NULL?
glotov
2011/02/15 15:35:49
Done.
| |
| 27 } | 28 } |
| 29 if (resume_status_connection_) { | |
| 30 chromeos::DisconnectResume(resume_status_connection_); | |
| 31 resume_status_connection_ = NULL; | |
| 32 } | |
| 28 } | 33 } |
| 29 | 34 |
| 30 void AddObserver(Observer* observer) { | 35 void AddObserver(Observer* observer) { |
| 31 observers_.AddObserver(observer); | 36 observers_.AddObserver(observer); |
| 32 } | 37 } |
| 33 | 38 |
| 34 void RemoveObserver(Observer* observer) { | 39 void RemoveObserver(Observer* observer) { |
| 35 observers_.RemoveObserver(observer); | 40 observers_.RemoveObserver(observer); |
| 36 } | 41 } |
| 37 | 42 |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 87 chromeos::RequestShutdown(); | 92 chromeos::RequestShutdown(); |
| 88 } | 93 } |
| 89 | 94 |
| 90 private: | 95 private: |
| 91 static void PowerStatusChangedHandler(void* object, | 96 static void PowerStatusChangedHandler(void* object, |
| 92 const chromeos::PowerStatus& status) { | 97 const chromeos::PowerStatus& status) { |
| 93 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 98 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 94 power->UpdatePowerStatus(status); | 99 power->UpdatePowerStatus(status); |
| 95 } | 100 } |
| 96 | 101 |
| 102 static void SystemResumedHandler(void* object) { | |
| 103 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | |
| 104 power->SystemResumed(); | |
| 105 } | |
| 106 | |
| 97 void Init() { | 107 void Init() { |
| 98 power_status_connection_ = chromeos::MonitorPowerStatus( | 108 power_status_connection_ = chromeos::MonitorPowerStatus( |
| 99 &PowerStatusChangedHandler, this); | 109 &PowerStatusChangedHandler, this); |
| 110 resume_status_connection_ = | |
| 111 chromeos::MonitorResume(&SystemResumedHandler, this); | |
| 100 } | 112 } |
| 101 | 113 |
| 102 void UpdatePowerStatus(const chromeos::PowerStatus& status) { | 114 void UpdatePowerStatus(const chromeos::PowerStatus& status) { |
| 103 // Make sure we run on UI thread. | 115 // Make sure we run on UI thread. |
| 104 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 116 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 105 BrowserThread::PostTask( | 117 BrowserThread::PostTask( |
| 106 BrowserThread::UI, FROM_HERE, | 118 BrowserThread::UI, FROM_HERE, |
| 107 NewRunnableMethod( | 119 NewRunnableMethod( |
| 108 this, &PowerLibraryImpl::UpdatePowerStatus, status)); | 120 this, &PowerLibraryImpl::UpdatePowerStatus, status)); |
| 109 return; | 121 return; |
| 110 } | 122 } |
| 111 | 123 |
| 112 DVLOG(1) << "Power lpo=" << status.line_power_on | 124 DVLOG(1) << "Power lpo=" << status.line_power_on |
| 113 << " sta=" << status.battery_state | 125 << " sta=" << status.battery_state |
| 114 << " per=" << status.battery_percentage | 126 << " per=" << status.battery_percentage |
| 115 << " tte=" << status.battery_time_to_empty | 127 << " tte=" << status.battery_time_to_empty |
| 116 << " ttf=" << status.battery_time_to_full; | 128 << " ttf=" << status.battery_time_to_full; |
| 117 status_ = status; | 129 status_ = status; |
| 118 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | 130 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); |
| 119 } | 131 } |
| 120 | 132 |
| 133 void SystemResumed() { | |
| 134 // Make sure we run on the UI thread. | |
| 135 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 136 BrowserThread::PostTask( | |
| 137 BrowserThread::UI, FROM_HERE, | |
| 138 NewRunnableMethod(this, &PowerLibraryImpl::SystemResumed)); | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); | |
| 143 } | |
| 144 | |
| 121 ObserverList<Observer> observers_; | 145 ObserverList<Observer> observers_; |
| 122 | 146 |
| 123 // A reference to the battery power api, to allow callbacks when the battery | 147 // A reference to the battery power api, to allow callbacks when the battery |
| 124 // status changes. | 148 // status changes. |
| 125 chromeos::PowerStatusConnection power_status_connection_; | 149 chromeos::PowerStatusConnection power_status_connection_; |
| 126 | 150 |
| 151 // A reference to the resume alerts. | |
| 152 chromeos::ResumeConnection resume_status_connection_; | |
| 153 | |
| 127 // The latest power status. | 154 // The latest power status. |
| 128 chromeos::PowerStatus status_; | 155 chromeos::PowerStatus status_; |
| 129 | 156 |
| 130 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); | 157 DISALLOW_COPY_AND_ASSIGN(PowerLibraryImpl); |
| 131 }; | 158 }; |
| 132 | 159 |
| 133 class PowerLibraryStubImpl : public PowerLibrary { | 160 class PowerLibraryStubImpl : public PowerLibrary { |
| 134 public: | 161 public: |
| 135 PowerLibraryStubImpl() {} | 162 PowerLibraryStubImpl() {} |
| 136 ~PowerLibraryStubImpl() {} | 163 ~PowerLibraryStubImpl() {} |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 157 return new PowerLibraryStubImpl(); | 184 return new PowerLibraryStubImpl(); |
| 158 else | 185 else |
| 159 return new PowerLibraryImpl(); | 186 return new PowerLibraryImpl(); |
| 160 } | 187 } |
| 161 | 188 |
| 162 } // namespace chromeos | 189 } // namespace chromeos |
| 163 | 190 |
| 164 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 191 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 165 // won't be deleted until it's last InvokeLater is run. | 192 // won't be deleted until it's last InvokeLater is run. |
| 166 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); | 193 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::PowerLibraryImpl); |
| OLD | NEW |