| OLD | NEW |
| 1 // Copyright (c) 2010 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/screen_lock_library.h" | 5 #include "chrome/browser/chromeos/cros/screen_lock_library.h" |
| 6 | 6 |
| 7 #include "base/basictypes.h" |
| 8 #include "base/logging.h" |
| 7 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 8 #include "base/string_util.h" | 10 #include "base/observer_list.h" |
| 9 #include "chrome/browser/chromeos/cros/cros_library.h" | 11 #include "chrome/browser/chromeos/cros/cros_library.h" |
| 10 #include "content/browser/browser_thread.h" | 12 #include "content/browser/browser_thread.h" |
| 13 #include "third_party/cros/chromeos_screen_lock.h" |
| 11 | 14 |
| 12 namespace chromeos { | 15 namespace chromeos { |
| 13 | 16 |
| 14 // This class handles the interaction with the ChromeOS screen lock APIs. | |
| 15 class ScreenLockLibraryImpl : public ScreenLockLibrary { | 17 class ScreenLockLibraryImpl : public ScreenLockLibrary { |
| 16 public: | 18 public: |
| 17 ScreenLockLibraryImpl() { | 19 ScreenLockLibraryImpl() {} |
| 20 |
| 21 ~ScreenLockLibraryImpl() { |
| 22 if (screen_lock_connection_) |
| 23 chromeos::DisconnectScreenLock(screen_lock_connection_); |
| 24 } |
| 25 |
| 26 void Init() { |
| 18 if (CrosLibrary::Get()->EnsureLoaded()) { | 27 if (CrosLibrary::Get()->EnsureLoaded()) { |
| 19 Init(); | 28 screen_lock_connection_ = |
| 29 chromeos::MonitorScreenLock(&ScreenLockedHandler, this); |
| 20 } | 30 } |
| 21 } | 31 } |
| 22 | 32 |
| 23 ~ScreenLockLibraryImpl() { | |
| 24 if (screen_lock_connection_) { | |
| 25 chromeos::DisconnectScreenLock(screen_lock_connection_); | |
| 26 } | |
| 27 } | |
| 28 | |
| 29 void AddObserver(Observer* observer) { | 33 void AddObserver(Observer* observer) { |
| 30 observers_.AddObserver(observer); | 34 observers_.AddObserver(observer); |
| 31 } | 35 } |
| 32 | 36 |
| 33 void RemoveObserver(Observer* observer) { | 37 void RemoveObserver(Observer* observer) { |
| 34 observers_.RemoveObserver(observer); | 38 observers_.RemoveObserver(observer); |
| 35 } | 39 } |
| 36 | 40 |
| 37 void NotifyScreenLockRequested() { | 41 void NotifyScreenLockRequested() { |
| 38 chromeos::NotifyScreenLockRequested(); | 42 chromeos::NotifyScreenLockRequested(); |
| 39 } | 43 } |
| 40 | 44 |
| 41 void NotifyScreenLockCompleted() { | 45 void NotifyScreenLockCompleted() { |
| 42 chromeos::NotifyScreenLockCompleted(); | 46 chromeos::NotifyScreenLockCompleted(); |
| 43 } | 47 } |
| 44 | 48 |
| 45 void NotifyScreenUnlockRequested() { | 49 void NotifyScreenUnlockRequested() { |
| 46 chromeos::NotifyScreenUnlockRequested(); | 50 chromeos::NotifyScreenUnlockRequested(); |
| 47 } | 51 } |
| 48 | 52 |
| 49 void NotifyScreenUnlockCompleted() { | 53 void NotifyScreenUnlockCompleted() { |
| 50 chromeos::NotifyScreenUnlockCompleted(); | 54 chromeos::NotifyScreenUnlockCompleted(); |
| 51 } | 55 } |
| 52 | 56 |
| 53 private: | 57 private: |
| 54 void Init() { | 58 static void ScreenLockedHandler(void* object, ScreenLockEvent event) { |
| 55 screen_lock_connection_ = chromeos::MonitorScreenLock( | 59 ScreenLockLibraryImpl* self = static_cast<ScreenLockLibraryImpl*>(object); |
| 56 &ScreenLockedHandler, this); | 60 switch (event) { |
| 61 case chromeos::LockScreen: |
| 62 self->LockScreen(); |
| 63 break; |
| 64 case chromeos::UnlockScreen: |
| 65 self->UnlockScreen(); |
| 66 break; |
| 67 case chromeos::UnlockScreenFailed: |
| 68 self->UnlockScreenFailed(); |
| 69 break; |
| 70 default: |
| 71 NOTREACHED(); |
| 72 break; |
| 73 } |
| 57 } | 74 } |
| 58 | 75 |
| 59 void LockScreen() { | 76 void LockScreen() { |
| 60 // Make sure we run on UI thread. | 77 // Make sure we run on UI thread. |
| 61 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 78 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 62 BrowserThread::PostTask( | 79 BrowserThread::PostTask( |
| 63 BrowserThread::UI, FROM_HERE, | 80 BrowserThread::UI, FROM_HERE, |
| 64 NewRunnableMethod(this, &ScreenLockLibraryImpl::LockScreen)); | 81 NewRunnableMethod(this, &ScreenLockLibraryImpl::LockScreen)); |
| 65 return; | 82 return; |
| 66 } | 83 } |
| (...skipping 15 matching lines...) Expand all Loading... |
| 82 // Make sure we run on UI thread. | 99 // Make sure we run on UI thread. |
| 83 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 100 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 84 BrowserThread::PostTask( | 101 BrowserThread::PostTask( |
| 85 BrowserThread::UI, FROM_HERE, | 102 BrowserThread::UI, FROM_HERE, |
| 86 NewRunnableMethod(this, &ScreenLockLibraryImpl::UnlockScreenFailed)); | 103 NewRunnableMethod(this, &ScreenLockLibraryImpl::UnlockScreenFailed)); |
| 87 return; | 104 return; |
| 88 } | 105 } |
| 89 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreenFailed(this)); | 106 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreenFailed(this)); |
| 90 } | 107 } |
| 91 | 108 |
| 92 static void ScreenLockedHandler(void* object, ScreenLockEvent event) { | |
| 93 ScreenLockLibraryImpl* self = static_cast<ScreenLockLibraryImpl*>(object); | |
| 94 switch (event) { | |
| 95 case chromeos::LockScreen: | |
| 96 self->LockScreen(); | |
| 97 break; | |
| 98 case chromeos::UnlockScreen: | |
| 99 self->UnlockScreen(); | |
| 100 break; | |
| 101 case chromeos::UnlockScreenFailed: | |
| 102 self->UnlockScreenFailed(); | |
| 103 break; | |
| 104 default: | |
| 105 NOTREACHED(); | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 ObserverList<Observer> observers_; | 109 ObserverList<Observer> observers_; |
| 110 | 110 |
| 111 // A reference to the screen lock api | 111 // A reference to the screen lock API. |
| 112 chromeos::ScreenLockConnection screen_lock_connection_; | 112 chromeos::ScreenLockConnection screen_lock_connection_; |
| 113 | 113 |
| 114 DISALLOW_COPY_AND_ASSIGN(ScreenLockLibraryImpl); | 114 DISALLOW_COPY_AND_ASSIGN(ScreenLockLibraryImpl); |
| 115 }; | 115 }; |
| 116 | 116 |
| 117 class ScreenLockLibraryStubImpl : public ScreenLockLibrary { | 117 class ScreenLockLibraryStubImpl : public ScreenLockLibrary { |
| 118 public: | 118 public: |
| 119 ScreenLockLibraryStubImpl() {} | 119 ScreenLockLibraryStubImpl() {} |
| 120 ~ScreenLockLibraryStubImpl() {} | 120 ~ScreenLockLibraryStubImpl() {} |
| 121 void Init() {} |
| 121 void AddObserver(Observer* observer) {} | 122 void AddObserver(Observer* observer) {} |
| 122 void RemoveObserver(Observer* observer) {} | 123 void RemoveObserver(Observer* observer) {} |
| 123 void NotifyScreenLockRequested() {} | 124 void NotifyScreenLockRequested() {} |
| 124 void NotifyScreenLockCompleted() {} | 125 void NotifyScreenLockCompleted() {} |
| 125 void NotifyScreenUnlockRequested() {} | 126 void NotifyScreenUnlockRequested() {} |
| 126 void NotifyScreenUnlockCompleted() {} | 127 void NotifyScreenUnlockCompleted() {} |
| 127 }; | 128 }; |
| 128 | 129 |
| 129 // static | 130 // static |
| 130 ScreenLockLibrary* ScreenLockLibrary::GetImpl(bool stub) { | 131 ScreenLockLibrary* ScreenLockLibrary::GetImpl(bool stub) { |
| 132 ScreenLockLibrary* impl; |
| 131 if (stub) | 133 if (stub) |
| 132 return new ScreenLockLibraryStubImpl(); | 134 impl = new ScreenLockLibraryStubImpl(); |
| 133 else | 135 else |
| 134 return new ScreenLockLibraryImpl(); | 136 impl = new ScreenLockLibraryImpl(); |
| 137 impl->Init(); |
| 138 return impl; |
| 135 } | 139 } |
| 136 | 140 |
| 137 } // namespace chromeos | 141 } // namespace chromeos |
| 138 | 142 |
| 139 // Allows InvokeLater without adding refcounting. This class is a Singleton and | 143 // Allows InvokeLater without adding refcounting. This class is a Singleton and |
| 140 // won't be deleted until it's last InvokeLater is run. | 144 // won't be deleted until it's last InvokeLater is run. |
| 141 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::ScreenLockLibraryImpl); | 145 DISABLE_RUNNABLE_METHOD_REFCOUNT(chromeos::ScreenLockLibraryImpl); |
| OLD | NEW |