OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "chrome/browser/chromeos/cros/screen_lock_library.h" | |
6 | |
7 #include "base/basictypes.h" | |
8 #include "base/compiler_specific.h" | |
9 #include "base/logging.h" | |
10 #include "base/message_loop.h" | |
11 #include "base/observer_list.h" | |
12 #include "chrome/browser/chromeos/cros/cros_library.h" | |
13 #include "content/public/browser/browser_thread.h" | |
14 #include "third_party/cros/chromeos_screen_lock.h" | |
15 | |
16 using content::BrowserThread; | |
17 | |
18 namespace chromeos { | |
19 | |
20 class ScreenLockLibraryImpl : public ScreenLockLibrary { | |
21 public: | |
22 ScreenLockLibraryImpl() : screen_lock_connection_(NULL) {} | |
23 | |
24 virtual ~ScreenLockLibraryImpl() { | |
25 if (screen_lock_connection_) { | |
26 chromeos::DisconnectScreenLock(screen_lock_connection_); | |
27 screen_lock_connection_ = NULL; | |
28 } | |
29 } | |
30 | |
31 // Begin ScreenLockLibrary implementation. | |
32 virtual void Init() OVERRIDE { | |
33 DCHECK(CrosLibrary::Get()->libcros_loaded()); | |
34 screen_lock_connection_ = | |
35 chromeos::MonitorScreenLock(&ScreenLockedHandler, this); | |
36 } | |
37 | |
38 virtual void AddObserver(Observer* observer) OVERRIDE { | |
39 observers_.AddObserver(observer); | |
40 } | |
41 | |
42 virtual void RemoveObserver(Observer* observer) OVERRIDE { | |
43 observers_.RemoveObserver(observer); | |
44 } | |
45 | |
46 virtual void NotifyScreenLockRequested() OVERRIDE { | |
47 chromeos::NotifyScreenLockRequested(); | |
48 } | |
49 | |
50 virtual void NotifyScreenLockCompleted() OVERRIDE { | |
51 chromeos::NotifyScreenLockCompleted(); | |
52 } | |
53 | |
54 virtual void NotifyScreenUnlockRequested() OVERRIDE { | |
55 chromeos::NotifyScreenUnlockRequested(); | |
56 } | |
57 | |
58 virtual void NotifyScreenUnlockCompleted() OVERRIDE { | |
59 chromeos::NotifyScreenUnlockCompleted(); | |
60 } | |
61 // End ScreenLockLibrary implementation. | |
62 | |
63 private: | |
64 static void ScreenLockedHandler(void* object, ScreenLockEvent event) { | |
65 ScreenLockLibraryImpl* self = static_cast<ScreenLockLibraryImpl*>(object); | |
66 switch (event) { | |
67 case chromeos::LockScreen: | |
68 self->LockScreen(); | |
69 break; | |
70 case chromeos::UnlockScreen: | |
71 self->UnlockScreen(); | |
72 break; | |
73 case chromeos::UnlockScreenFailed: | |
74 self->UnlockScreenFailed(); | |
75 break; | |
76 default: | |
77 NOTREACHED(); | |
78 break; | |
79 } | |
80 } | |
81 | |
82 void LockScreen() { | |
83 // Called from ScreenLockedHandler, a libcros callback which should | |
84 // always run on UI thread. | |
85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
86 FOR_EACH_OBSERVER(Observer, observers_, LockScreen(this)); | |
87 } | |
88 | |
89 void UnlockScreen() { | |
90 // Called from ScreenLockedHandler, a libcros callback which should | |
91 // always run on UI thread. | |
92 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
93 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreen(this)); | |
94 } | |
95 | |
96 void UnlockScreenFailed() { | |
97 // Called from ScreenLockedHandler, a libcros callback which should | |
98 // always run on UI thread. | |
99 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
100 FOR_EACH_OBSERVER(Observer, observers_, UnlockScreenFailed(this)); | |
101 } | |
102 | |
103 chromeos::ScreenLockConnection screen_lock_connection_; | |
104 | |
105 ObserverList<Observer> observers_; | |
106 | |
107 DISALLOW_COPY_AND_ASSIGN(ScreenLockLibraryImpl); | |
108 }; | |
109 | |
110 class ScreenLockLibraryStubImpl : public ScreenLockLibrary { | |
111 public: | |
112 ScreenLockLibraryStubImpl() {} | |
113 virtual ~ScreenLockLibraryStubImpl() {} | |
114 virtual void Init() OVERRIDE {} | |
115 virtual void AddObserver(Observer* observer) OVERRIDE {} | |
116 virtual void RemoveObserver(Observer* observer) OVERRIDE {} | |
117 virtual void NotifyScreenLockRequested() OVERRIDE {} | |
118 virtual void NotifyScreenLockCompleted() OVERRIDE {} | |
119 virtual void NotifyScreenUnlockRequested() OVERRIDE {} | |
120 virtual void NotifyScreenUnlockCompleted() OVERRIDE {} | |
121 }; | |
122 | |
123 // static | |
124 ScreenLockLibrary* ScreenLockLibrary::GetImpl(bool stub) { | |
125 ScreenLockLibrary* impl; | |
126 if (stub) | |
127 impl = new ScreenLockLibraryStubImpl(); | |
128 else | |
129 impl = new ScreenLockLibraryImpl(); | |
130 impl->Init(); | |
131 return impl; | |
132 } | |
133 | |
134 } // namespace chromeos | |
OLD | NEW |