| 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_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<CrosPowerLibrary> { |
| 16 void RetainCallee(CrosPowerLibrary* obj) {} | 16 void RetainCallee(CrosPowerLibrary* obj) {} |
| 17 void ReleaseCallee(CrosPowerLibrary* obj) {} | 17 void ReleaseCallee(CrosPowerLibrary* obj) {} |
| 18 }; | 18 }; |
| 19 | 19 |
| 20 CrosPowerLibrary::CrosPowerLibrary() : status_(chromeos::PowerStatus()) { | 20 CrosPowerLibrary::CrosPowerLibrary() : status_(chromeos::PowerStatus()) { |
| 21 if (CrosLibrary::loaded()) { | 21 if (CrosLibrary::loaded()) { |
| 22 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::FILE); | 22 Init(); |
| 23 if (loop) | |
| 24 loop->PostTask(FROM_HERE, NewRunnableMethod(this, | |
| 25 &CrosPowerLibrary::InitOnBackgroundThread)); | |
| 26 } | 23 } |
| 27 } | 24 } |
| 28 | 25 |
| 29 CrosPowerLibrary::~CrosPowerLibrary() { | 26 CrosPowerLibrary::~CrosPowerLibrary() { |
| 30 if (CrosLibrary::loaded()) { | 27 if (CrosLibrary::loaded()) { |
| 31 // FILE thread is already gone by the time we get to this destructor. | |
| 32 // So it's ok to just make the disconnect call on the main thread. | |
| 33 chromeos::DisconnectPowerStatus(power_status_connection_); | 28 chromeos::DisconnectPowerStatus(power_status_connection_); |
| 34 } | 29 } |
| 35 } | 30 } |
| 36 | 31 |
| 37 // static | 32 // static |
| 38 CrosPowerLibrary* CrosPowerLibrary::Get() { | 33 CrosPowerLibrary* CrosPowerLibrary::Get() { |
| 39 return Singleton<CrosPowerLibrary>::get(); | 34 return Singleton<CrosPowerLibrary>::get(); |
| 40 } | 35 } |
| 41 | 36 |
| 42 // static | 37 // static |
| (...skipping 30 matching lines...) Expand all Loading... |
| 73 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); | 68 return base::TimeDelta::FromSeconds(status_.battery_time_to_full); |
| 74 } | 69 } |
| 75 | 70 |
| 76 // static | 71 // static |
| 77 void CrosPowerLibrary::PowerStatusChangedHandler(void* object, | 72 void CrosPowerLibrary::PowerStatusChangedHandler(void* object, |
| 78 const chromeos::PowerStatus& status) { | 73 const chromeos::PowerStatus& status) { |
| 79 CrosPowerLibrary* power = static_cast<CrosPowerLibrary*>(object); | 74 CrosPowerLibrary* power = static_cast<CrosPowerLibrary*>(object); |
| 80 power->UpdatePowerStatus(status); | 75 power->UpdatePowerStatus(status); |
| 81 } | 76 } |
| 82 | 77 |
| 83 void CrosPowerLibrary::InitOnBackgroundThread() { | 78 void CrosPowerLibrary::Init() { |
| 84 power_status_connection_ = chromeos::MonitorPowerStatus( | 79 power_status_connection_ = chromeos::MonitorPowerStatus( |
| 85 &PowerStatusChangedHandler, this); | 80 &PowerStatusChangedHandler, this); |
| 86 } | 81 } |
| 87 | 82 |
| 88 void CrosPowerLibrary::UpdatePowerStatus(const chromeos::PowerStatus& status) { | 83 void CrosPowerLibrary::UpdatePowerStatus(const chromeos::PowerStatus& status) { |
| 89 // Make sure we run on UI thread. | 84 // Make sure we run on UI thread. |
| 90 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { | 85 if (!ChromeThread::CurrentlyOn(ChromeThread::UI)) { |
| 91 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::UI); | 86 MessageLoop* loop = ChromeThread::GetMessageLoop(ChromeThread::UI); |
| 92 if (loop) | 87 if (loop) |
| 93 loop->PostTask(FROM_HERE, NewRunnableMethod(this, | 88 loop->PostTask(FROM_HERE, NewRunnableMethod(this, |
| 94 &CrosPowerLibrary::UpdatePowerStatus, status)); | 89 &CrosPowerLibrary::UpdatePowerStatus, status)); |
| 95 return; | 90 return; |
| 96 } | 91 } |
| 97 | 92 |
| 98 DLOG(INFO) << "Power" << | 93 DLOG(INFO) << "Power" << |
| 99 " lpo=" << status.line_power_on << | 94 " lpo=" << status.line_power_on << |
| 100 " sta=" << status.battery_state << | 95 " sta=" << status.battery_state << |
| 101 " per=" << status.battery_percentage << | 96 " per=" << status.battery_percentage << |
| 102 " tte=" << status.battery_time_to_empty << | 97 " tte=" << status.battery_time_to_empty << |
| 103 " ttf=" << status.battery_time_to_full; | 98 " ttf=" << status.battery_time_to_full; |
| 104 status_ = status; | 99 status_ = status; |
| 105 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); | 100 FOR_EACH_OBSERVER(Observer, observers_, PowerChanged(this)); |
| 106 } | 101 } |
| OLD | NEW |