OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/extensions/screenlock_private_api.h" | 5 #include "chrome/browser/chromeos/extensions/screenlock_private_api.h" |
6 | 6 |
7 #include "base/lazy_instance.h" | 7 #include "base/lazy_instance.h" |
8 #include "base/values.h" | 8 #include "base/values.h" |
9 #include "chrome/browser/chromeos/login/screen_locker.h" | 9 #include "chrome/browser/chromeos/login/screen_locker.h" |
10 #include "chrome/browser/extensions/image_loader.h" | 10 #include "chrome/browser/extensions/image_loader.h" |
11 #include "chrome/common/extensions/api/screenlock_private.h" | 11 #include "chrome/common/extensions/api/screenlock_private.h" |
12 #include "chromeos/dbus/dbus_thread_manager.h" | 12 #include "chromeos/dbus/dbus_thread_manager.h" |
13 #include "extensions/browser/event_router.h" | 13 #include "extensions/browser/event_router.h" |
14 #include "extensions/browser/extension_system.h" | 14 #include "extensions/browser/extension_system.h" |
15 | 15 |
16 namespace screenlock = extensions::api::screenlock_private; | 16 namespace screenlock = extensions::api::screenlock_private; |
17 | 17 |
18 namespace extensions { | 18 namespace extensions { |
19 | 19 |
20 namespace { | |
21 | |
22 const char kNotLockedError[] = "Screen is not currently locked."; | |
23 | |
24 chromeos::LoginDisplay::AuthType ToLoginDisplayAuthType( | |
25 screenlock::AuthType auth_type) { | |
26 switch (auth_type) { | |
27 case screenlock::AUTH_TYPE_SYSTEMPASSWORD: | |
28 return chromeos::LoginDisplay::OFFLINE_PASSWORD; | |
29 case screenlock::AUTH_TYPE_NUMERICPIN: | |
30 return chromeos::LoginDisplay::NUMERIC_PIN; | |
31 case screenlock::AUTH_TYPE_USERCLICK: | |
32 return chromeos::LoginDisplay::USER_CLICK; | |
33 default: | |
34 NOTREACHED(); | |
35 return chromeos::LoginDisplay::OFFLINE_PASSWORD; | |
36 } | |
37 } | |
38 | |
39 screenlock::AuthType ToScreenlockPrivateAuthType( | |
40 chromeos::LoginDisplay::AuthType auth_type) { | |
41 switch (auth_type) { | |
42 case chromeos::LoginDisplay::OFFLINE_PASSWORD: | |
43 return screenlock::AUTH_TYPE_SYSTEMPASSWORD; | |
44 case chromeos::LoginDisplay::NUMERIC_PIN: | |
45 return screenlock::AUTH_TYPE_NUMERICPIN; | |
46 case chromeos::LoginDisplay::USER_CLICK: | |
47 return screenlock::AUTH_TYPE_USERCLICK; | |
48 case chromeos::LoginDisplay::ONLINE_SIGN_IN: | |
49 // Apps should treat forced online sign in same as system password. | |
50 return screenlock::AUTH_TYPE_SYSTEMPASSWORD; | |
51 default: | |
52 NOTREACHED(); | |
53 return screenlock::AUTH_TYPE_SYSTEMPASSWORD; | |
54 } | |
55 } | |
56 | |
57 } // namespace | |
58 | |
20 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {} | 59 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {} |
21 | 60 |
22 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {} | 61 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {} |
23 | 62 |
24 bool ScreenlockPrivateGetLockedFunction::RunImpl() { | 63 bool ScreenlockPrivateGetLockedFunction::RunImpl() { |
25 bool locked = false; | 64 bool locked = false; |
26 chromeos::ScreenLocker* locker = | 65 chromeos::ScreenLocker* locker = |
27 chromeos::ScreenLocker::default_screen_locker(); | 66 chromeos::ScreenLocker::default_screen_locker(); |
28 if (locker) | 67 if (locker) |
29 locked = locker->locked(); | 68 locked = locker->locked(); |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 ScreenlockPrivateShowButtonFunction:: | 117 ScreenlockPrivateShowButtonFunction:: |
79 ~ScreenlockPrivateShowButtonFunction() {} | 118 ~ScreenlockPrivateShowButtonFunction() {} |
80 | 119 |
81 bool ScreenlockPrivateShowButtonFunction::RunImpl() { | 120 bool ScreenlockPrivateShowButtonFunction::RunImpl() { |
82 scoped_ptr<screenlock::ShowButton::Params> params( | 121 scoped_ptr<screenlock::ShowButton::Params> params( |
83 screenlock::ShowButton::Params::Create(*args_)); | 122 screenlock::ShowButton::Params::Create(*args_)); |
84 EXTENSION_FUNCTION_VALIDATE(params.get()); | 123 EXTENSION_FUNCTION_VALIDATE(params.get()); |
85 chromeos::ScreenLocker* locker = | 124 chromeos::ScreenLocker* locker = |
86 chromeos::ScreenLocker::default_screen_locker(); | 125 chromeos::ScreenLocker::default_screen_locker(); |
87 if (!locker) { | 126 if (!locker) { |
88 SendResponse(error_.empty()); | 127 SetError(kNotLockedError); |
128 SendResponse(false); | |
89 return true; | 129 return true; |
90 } | 130 } |
91 extensions::ImageLoader* loader = extensions::ImageLoader::Get(GetProfile()); | 131 extensions::ImageLoader* loader = extensions::ImageLoader::Get(GetProfile()); |
92 loader->LoadImageAsync( | 132 loader->LoadImageAsync( |
93 GetExtension(), GetExtension()->GetResource(params->icon), | 133 GetExtension(), GetExtension()->GetResource(params->icon), |
94 gfx::Size(kMaxButtonIconSize, kMaxButtonIconSize), | 134 gfx::Size(kMaxButtonIconSize, kMaxButtonIconSize), |
95 base::Bind(&ScreenlockPrivateShowButtonFunction::OnImageLoaded, this)); | 135 base::Bind(&ScreenlockPrivateShowButtonFunction::OnImageLoaded, this)); |
96 return true; | 136 return true; |
97 } | 137 } |
98 | 138 |
99 void ScreenlockPrivateShowButtonFunction::OnImageLoaded( | 139 void ScreenlockPrivateShowButtonFunction::OnImageLoaded( |
100 const gfx::Image& image) { | 140 const gfx::Image& image) { |
101 chromeos::ScreenLocker* locker = | 141 chromeos::ScreenLocker* locker = |
102 chromeos::ScreenLocker::default_screen_locker(); | 142 chromeos::ScreenLocker::default_screen_locker(); |
103 ScreenlockPrivateEventRouter* router = | 143 ScreenlockPrivateEventRouter* router = |
104 ScreenlockPrivateEventRouter::GetFactoryInstance()->GetForProfile( | 144 ScreenlockPrivateEventRouter::GetFactoryInstance()->GetForProfile( |
105 GetProfile()); | 145 GetProfile()); |
146 const chromeos::User* user = | |
147 chromeos::UserManager::Get()->GetUserByProfile(GetProfile()); | |
106 locker->ShowUserPodButton( | 148 locker->ShowUserPodButton( |
107 GetProfile()->GetProfileName(), image, | 149 user->email(), |
150 image, | |
108 base::Bind(&ScreenlockPrivateEventRouter::OnButtonClicked, | 151 base::Bind(&ScreenlockPrivateEventRouter::OnButtonClicked, |
109 base::Unretained(router))); | 152 base::Unretained(router))); |
110 SendResponse(error_.empty()); | 153 SendResponse(error_.empty()); |
111 } | 154 } |
112 | 155 |
156 ScreenlockPrivateHideButtonFunction::ScreenlockPrivateHideButtonFunction() {} | |
157 | |
158 ScreenlockPrivateHideButtonFunction::~ScreenlockPrivateHideButtonFunction() {} | |
159 | |
160 bool ScreenlockPrivateHideButtonFunction::RunImpl() { | |
161 chromeos::ScreenLocker* locker = | |
162 chromeos::ScreenLocker::default_screen_locker(); | |
163 if (locker) { | |
164 const chromeos::User* user = | |
165 chromeos::UserManager::Get()->GetUserByProfile(GetProfile()); | |
166 locker->HideUserPodButton(user->email()); | |
167 } else { | |
168 SetError(kNotLockedError); | |
169 } | |
170 SendResponse(error_.empty()); | |
171 return true; | |
172 } | |
173 | |
174 ScreenlockPrivateSetAuthTypeFunction::ScreenlockPrivateSetAuthTypeFunction() {} | |
175 | |
176 ScreenlockPrivateSetAuthTypeFunction::~ScreenlockPrivateSetAuthTypeFunction() {} | |
177 | |
178 bool ScreenlockPrivateSetAuthTypeFunction::RunImpl() { | |
179 scoped_ptr<screenlock::SetAuthType::Params> params( | |
180 screenlock::SetAuthType::Params::Create(*args_)); | |
181 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
182 | |
183 chromeos::ScreenLocker* locker = | |
184 chromeos::ScreenLocker::default_screen_locker(); | |
185 if (locker) { | |
186 std::string initial_value = | |
187 params->initial_value.get() ? *(params->initial_value.get()) : ""; | |
188 const chromeos::User* user = | |
189 chromeos::UserManager::Get()->GetUserByProfile(GetProfile()); | |
190 locker->SetAuthType(user->email(), | |
191 ToLoginDisplayAuthType(params->auth_type), | |
192 initial_value); | |
193 } else { | |
194 SetError(kNotLockedError); | |
195 } | |
196 SendResponse(error_.empty()); | |
197 return true; | |
198 } | |
199 | |
200 ScreenlockPrivateGetAuthTypeFunction::ScreenlockPrivateGetAuthTypeFunction() {} | |
201 | |
202 ScreenlockPrivateGetAuthTypeFunction::~ScreenlockPrivateGetAuthTypeFunction() {} | |
203 | |
204 bool ScreenlockPrivateGetAuthTypeFunction::RunImpl() { | |
205 chromeos::ScreenLocker* locker = | |
206 chromeos::ScreenLocker::default_screen_locker(); | |
207 if (locker) { | |
208 const chromeos::User* user = | |
209 chromeos::UserManager::Get()->GetUserByProfile(GetProfile()); | |
210 chromeos::LoginDisplay::AuthType auth_type = | |
211 locker->GetAuthType(user->email()); | |
212 std::string auth_type_name = | |
213 screenlock::ToString(ToScreenlockPrivateAuthType(auth_type)); | |
214 SetResult(new base::StringValue(auth_type_name)); | |
215 } else { | |
216 SetError(kNotLockedError); | |
217 } | |
218 SendResponse(error_.empty()); | |
219 return true; | |
220 } | |
221 | |
222 ScreenlockPrivateAcceptAuthAttemptFunction:: | |
223 ScreenlockPrivateAcceptAuthAttemptFunction() {} | |
224 | |
225 ScreenlockPrivateAcceptAuthAttemptFunction:: | |
226 ~ScreenlockPrivateAcceptAuthAttemptFunction() {} | |
227 | |
228 bool ScreenlockPrivateAcceptAuthAttemptFunction::RunImpl() { | |
229 scoped_ptr<screenlock::AcceptAuthAttempt::Params> params( | |
230 screenlock::AcceptAuthAttempt::Params::Create(*args_)); | |
231 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
232 | |
233 chromeos::ScreenLocker* locker = | |
234 chromeos::ScreenLocker::default_screen_locker(); | |
235 if (locker) { | |
236 if (params->accept) | |
237 locker->Hide(); | |
xiyuan
2014/02/20 17:44:44
Isn't Hide() a static method?
Tim Song
2014/02/20 21:06:08
Done.
| |
238 else | |
239 locker->EnableInput(); | |
240 } else { | |
241 SetError(kNotLockedError); | |
242 } | |
243 SendResponse(error_.empty()); | |
244 return true; | |
245 } | |
246 | |
113 ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(Profile* profile) | 247 ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(Profile* profile) |
114 : profile_(profile) { | 248 : profile_(profile) { |
115 chromeos::SessionManagerClient* session_manager = | 249 chromeos::SessionManagerClient* session_manager = |
116 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); | 250 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); |
117 if (!session_manager->HasObserver(this)) | 251 if (!session_manager->HasObserver(this)) |
118 session_manager->AddObserver(this); | 252 session_manager->AddObserver(this); |
119 } | 253 } |
120 | 254 |
121 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {} | 255 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {} |
122 | 256 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 chromeos::SessionManagerClient* session_manager = | 290 chromeos::SessionManagerClient* session_manager = |
157 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); | 291 chromeos::DBusThreadManager::Get()->GetSessionManagerClient(); |
158 if (session_manager->HasObserver(this)) | 292 if (session_manager->HasObserver(this)) |
159 session_manager->RemoveObserver(this); | 293 session_manager->RemoveObserver(this); |
160 } | 294 } |
161 | 295 |
162 void ScreenlockPrivateEventRouter::OnButtonClicked() { | 296 void ScreenlockPrivateEventRouter::OnButtonClicked() { |
163 DispatchEvent(screenlock::OnButtonClicked::kEventName, NULL); | 297 DispatchEvent(screenlock::OnButtonClicked::kEventName, NULL); |
164 } | 298 } |
165 | 299 |
300 void ScreenlockPrivateEventRouter::OnAuthAttempted( | |
301 chromeos::LoginDisplay::AuthType auth_type, | |
302 const std::string& value) { | |
303 scoped_ptr<base::ListValue> args(new base::ListValue()); | |
304 args->AppendString( | |
305 screenlock::ToString(ToScreenlockPrivateAuthType(auth_type))); | |
306 args->AppendString(value); | |
307 | |
308 scoped_ptr<extensions::Event> event(new extensions::Event( | |
309 screenlock::OnAuthAttempted::kEventName, args.Pass())); | |
310 extensions::ExtensionSystem::Get(profile_)->event_router()->BroadcastEvent( | |
311 event.Pass()); | |
312 } | |
313 | |
166 } // namespace extensions | 314 } // namespace extensions |
OLD | NEW |