| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 41 } | 41 } |
| 42 | 42 |
| 43 virtual void AddObserver(Observer* observer) OVERRIDE { | 43 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 44 observers_.AddObserver(observer); | 44 observers_.AddObserver(observer); |
| 45 } | 45 } |
| 46 | 46 |
| 47 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 47 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 48 observers_.RemoveObserver(observer); | 48 observers_.RemoveObserver(observer); |
| 49 } | 49 } |
| 50 | 50 |
| 51 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | |
| 52 // TODO(sidor): Examine if it's really a good idea to use void* as a second | |
| 53 // argument. | |
| 54 chromeos::GetIdleTime(&GetIdleTimeCallback, callback); | |
| 55 } | |
| 56 | |
| 57 virtual void EnableScreenLock(bool enable) OVERRIDE { | 51 virtual void EnableScreenLock(bool enable) OVERRIDE { |
| 58 // Called when the screen preference is changed, which should always | 52 // Called when the screen preference is changed, which should always |
| 59 // run on UI thread. | 53 // run on UI thread. |
| 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 54 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 61 // Post the task to FILE thread as chromeos::EnableScreenLock | 55 // Post the task to FILE thread as chromeos::EnableScreenLock |
| 62 // would write power manager config file to disk. | 56 // would write power manager config file to disk. |
| 63 BrowserThread::PostTask( | 57 BrowserThread::PostTask( |
| 64 BrowserThread::FILE, FROM_HERE, | 58 BrowserThread::FILE, FROM_HERE, |
| 65 base::Bind(&PowerLibraryImpl::DoEnableScreenLock, enable)); | 59 base::Bind(&PowerLibraryImpl::DoEnableScreenLock, enable)); |
| 66 } | 60 } |
| 67 | 61 |
| 68 // End PowerLibrary implementation. | 62 // End PowerLibrary implementation. |
| 69 | 63 |
| 70 private: | 64 private: |
| 71 static void DoEnableScreenLock(bool enable) { | 65 static void DoEnableScreenLock(bool enable) { |
| 72 chromeos::EnableScreenLock(enable); | 66 chromeos::EnableScreenLock(enable); |
| 73 } | 67 } |
| 74 | 68 |
| 75 static void GetIdleTimeCallback(void* object, | |
| 76 int64_t time_idle_ms, | |
| 77 bool success) { | |
| 78 DCHECK(object); | |
| 79 CalculateIdleTimeCallback* notify = | |
| 80 static_cast<CalculateIdleTimeCallback*>(object); | |
| 81 if (success) { | |
| 82 notify->Run(time_idle_ms/1000); | |
| 83 } else { | |
| 84 LOG(ERROR) << "Power manager failed to calculate idle time."; | |
| 85 notify->Run(-1); | |
| 86 } | |
| 87 delete notify; | |
| 88 } | |
| 89 | |
| 90 static void SystemResumedHandler(void* object) { | 69 static void SystemResumedHandler(void* object) { |
| 91 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); | 70 PowerLibraryImpl* power = static_cast<PowerLibraryImpl*>(object); |
| 92 power->SystemResumed(); | 71 power->SystemResumed(); |
| 93 } | 72 } |
| 94 | 73 |
| 95 void SystemResumed() { | 74 void SystemResumed() { |
| 96 // Called from SystemResumedHandler, a libcros callback which should | 75 // Called from SystemResumedHandler, a libcros callback which should |
| 97 // always run on UI thread. | 76 // always run on UI thread. |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 77 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 99 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); | 78 FOR_EACH_OBSERVER(Observer, observers_, SystemResumed()); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 119 // Begin PowerLibrary implementation. | 98 // Begin PowerLibrary implementation. |
| 120 virtual void Init() OVERRIDE {} | 99 virtual void Init() OVERRIDE {} |
| 121 virtual void AddObserver(Observer* observer) OVERRIDE { | 100 virtual void AddObserver(Observer* observer) OVERRIDE { |
| 122 observers_.AddObserver(observer); | 101 observers_.AddObserver(observer); |
| 123 } | 102 } |
| 124 | 103 |
| 125 virtual void RemoveObserver(Observer* observer) OVERRIDE { | 104 virtual void RemoveObserver(Observer* observer) OVERRIDE { |
| 126 observers_.RemoveObserver(observer); | 105 observers_.RemoveObserver(observer); |
| 127 } | 106 } |
| 128 | 107 |
| 129 virtual void CalculateIdleTime(CalculateIdleTimeCallback* callback) OVERRIDE { | |
| 130 callback->Run(0); | |
| 131 delete callback; | |
| 132 } | |
| 133 | |
| 134 virtual void EnableScreenLock(bool enable) OVERRIDE {} | 108 virtual void EnableScreenLock(bool enable) OVERRIDE {} |
| 135 | 109 |
| 136 // End PowerLibrary implementation. | 110 // End PowerLibrary implementation. |
| 137 private: | 111 private: |
| 138 ObserverList<Observer> observers_; | 112 ObserverList<Observer> observers_; |
| 139 }; | 113 }; |
| 140 | 114 |
| 141 // static | 115 // static |
| 142 PowerLibrary* PowerLibrary::GetImpl(bool stub) { | 116 PowerLibrary* PowerLibrary::GetImpl(bool stub) { |
| 143 PowerLibrary* impl; | 117 PowerLibrary* impl; |
| 144 if (stub) | 118 if (stub) |
| 145 impl = new PowerLibraryStubImpl(); | 119 impl = new PowerLibraryStubImpl(); |
| 146 else | 120 else |
| 147 impl = new PowerLibraryImpl(); | 121 impl = new PowerLibraryImpl(); |
| 148 impl->Init(); | 122 impl->Init(); |
| 149 return impl; | 123 return impl; |
| 150 } | 124 } |
| 151 | 125 |
| 152 } // namespace chromeos | 126 } // namespace chromeos |
| OLD | NEW |