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
3 // found in the LICENSE file.
5 #include "chrome/browser/chromeos/extensions/screenlock_private_api.h"
7 #include "base/lazy_instance.h"
8 #include "base/values.h"
9 #include "chrome/browser/chromeos/login/screen_locker.h"
10 #include "chrome/browser/extensions/image_loader.h"
11 #include "chrome/common/extensions/api/screenlock_private.h"
12 #include "chromeos/dbus/dbus_thread_manager.h"
13 #include "extensions/browser/event_router.h"
14 #include "extensions/browser/extension_system.h"
15 #include "ui/gfx/image/image.h"
17 namespace screenlock = extensions::api::screenlock_private;
19 namespace extensions {
23 const char kNotLockedError[] = "Screen is not currently locked.";
25 chromeos::LoginDisplay::AuthType ToLoginDisplayAuthType(
26 screenlock::AuthType auth_type) {
28 case screenlock::AUTH_TYPE_OFFLINEPASSWORD:
29 return chromeos::LoginDisplay::OFFLINE_PASSWORD;
30 case screenlock::AUTH_TYPE_NUMERICPIN:
31 return chromeos::LoginDisplay::NUMERIC_PIN;
32 case screenlock::AUTH_TYPE_USERCLICK:
33 return chromeos::LoginDisplay::USER_CLICK;
36 return chromeos::LoginDisplay::OFFLINE_PASSWORD;
40 screenlock::AuthType ToScreenlockPrivateAuthType(
41 chromeos::LoginDisplay::AuthType auth_type) {
43 case chromeos::LoginDisplay::OFFLINE_PASSWORD:
44 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
45 case chromeos::LoginDisplay::NUMERIC_PIN:
46 return screenlock::AUTH_TYPE_NUMERICPIN;
47 case chromeos::LoginDisplay::USER_CLICK:
48 return screenlock::AUTH_TYPE_USERCLICK;
49 case chromeos::LoginDisplay::ONLINE_SIGN_IN:
50 // Apps should treat forced online sign in same as system password.
51 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
54 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
60 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {}
62 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {}
64 bool ScreenlockPrivateGetLockedFunction::RunImpl() {
66 chromeos::ScreenLocker* locker =
67 chromeos::ScreenLocker::default_screen_locker();
69 locked = locker->locked();
70 SetResult(new base::FundamentalValue(locked));
71 SendResponse(error_.empty());
75 ScreenlockPrivateSetLockedFunction::ScreenlockPrivateSetLockedFunction() {}
77 ScreenlockPrivateSetLockedFunction::~ScreenlockPrivateSetLockedFunction() {}
79 bool ScreenlockPrivateSetLockedFunction::RunImpl() {
80 scoped_ptr<screenlock::SetLocked::Params> params(
81 screenlock::SetLocked::Params::Create(*args_));
82 EXTENSION_FUNCTION_VALIDATE(params.get());
84 chromeos::SessionManagerClient* session_manager =
85 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
86 session_manager->RequestLockScreen();
88 chromeos::ScreenLocker* locker =
89 chromeos::ScreenLocker::default_screen_locker();
91 chromeos::ScreenLocker::Hide();
93 SendResponse(error_.empty());
97 ScreenlockPrivateShowMessageFunction::ScreenlockPrivateShowMessageFunction() {}
99 ScreenlockPrivateShowMessageFunction::~ScreenlockPrivateShowMessageFunction() {}
101 bool ScreenlockPrivateShowMessageFunction::RunImpl() {
102 scoped_ptr<screenlock::ShowMessage::Params> params(
103 screenlock::ShowMessage::Params::Create(*args_));
104 EXTENSION_FUNCTION_VALIDATE(params.get());
105 chromeos::ScreenLocker* locker =
106 chromeos::ScreenLocker::default_screen_locker();
108 locker->ShowBannerMessage(params->message);
109 SendResponse(error_.empty());
113 static const int kMaxButtonIconSize = 40;
115 ScreenlockPrivateShowButtonFunction::
116 ScreenlockPrivateShowButtonFunction() {}
118 ScreenlockPrivateShowButtonFunction::
119 ~ScreenlockPrivateShowButtonFunction() {}
121 bool ScreenlockPrivateShowButtonFunction::RunImpl() {
122 scoped_ptr<screenlock::ShowButton::Params> params(
123 screenlock::ShowButton::Params::Create(*args_));
124 EXTENSION_FUNCTION_VALIDATE(params.get());
125 chromeos::ScreenLocker* locker =
126 chromeos::ScreenLocker::default_screen_locker();
128 SetError(kNotLockedError);
132 extensions::ImageLoader* loader = extensions::ImageLoader::Get(GetProfile());
133 loader->LoadImageAsync(
134 GetExtension(), GetExtension()->GetResource(params->icon),
135 gfx::Size(kMaxButtonIconSize, kMaxButtonIconSize),
136 base::Bind(&ScreenlockPrivateShowButtonFunction::OnImageLoaded, this));
140 void ScreenlockPrivateShowButtonFunction::OnImageLoaded(
141 const gfx::Image& image) {
142 chromeos::ScreenLocker* locker =
143 chromeos::ScreenLocker::default_screen_locker();
144 ScreenlockPrivateEventRouter* router =
145 ScreenlockPrivateEventRouter::GetFactoryInstance()->Get(GetProfile());
146 const chromeos::User* user =
147 chromeos::UserManager::Get()->GetUserByProfile(GetProfile());
148 locker->ShowUserPodButton(
151 base::Bind(&ScreenlockPrivateEventRouter::OnButtonClicked,
152 base::Unretained(router)));
153 SendResponse(error_.empty());
156 ScreenlockPrivateHideButtonFunction::ScreenlockPrivateHideButtonFunction() {}
158 ScreenlockPrivateHideButtonFunction::~ScreenlockPrivateHideButtonFunction() {}
160 bool ScreenlockPrivateHideButtonFunction::RunImpl() {
161 chromeos::ScreenLocker* locker =
162 chromeos::ScreenLocker::default_screen_locker();
164 const chromeos::User* user =
165 chromeos::UserManager::Get()->GetUserByProfile(GetProfile());
166 locker->HideUserPodButton(user->email());
168 SetError(kNotLockedError);
170 SendResponse(error_.empty());
174 ScreenlockPrivateSetAuthTypeFunction::ScreenlockPrivateSetAuthTypeFunction() {}
176 ScreenlockPrivateSetAuthTypeFunction::~ScreenlockPrivateSetAuthTypeFunction() {}
178 bool ScreenlockPrivateSetAuthTypeFunction::RunImpl() {
179 scoped_ptr<screenlock::SetAuthType::Params> params(
180 screenlock::SetAuthType::Params::Create(*args_));
181 EXTENSION_FUNCTION_VALIDATE(params.get());
183 chromeos::ScreenLocker* locker =
184 chromeos::ScreenLocker::default_screen_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),
194 SetError(kNotLockedError);
196 SendResponse(error_.empty());
200 ScreenlockPrivateGetAuthTypeFunction::ScreenlockPrivateGetAuthTypeFunction() {}
202 ScreenlockPrivateGetAuthTypeFunction::~ScreenlockPrivateGetAuthTypeFunction() {}
204 bool ScreenlockPrivateGetAuthTypeFunction::RunImpl() {
205 chromeos::ScreenLocker* locker =
206 chromeos::ScreenLocker::default_screen_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));
216 SetError(kNotLockedError);
218 SendResponse(error_.empty());
222 ScreenlockPrivateAcceptAuthAttemptFunction::
223 ScreenlockPrivateAcceptAuthAttemptFunction() {}
225 ScreenlockPrivateAcceptAuthAttemptFunction::
226 ~ScreenlockPrivateAcceptAuthAttemptFunction() {}
228 bool ScreenlockPrivateAcceptAuthAttemptFunction::RunImpl() {
229 scoped_ptr<screenlock::AcceptAuthAttempt::Params> params(
230 screenlock::AcceptAuthAttempt::Params::Create(*args_));
231 EXTENSION_FUNCTION_VALIDATE(params.get());
233 chromeos::ScreenLocker* locker =
234 chromeos::ScreenLocker::default_screen_locker();
237 chromeos::ScreenLocker::Hide();
239 locker->EnableInput();
241 SetError(kNotLockedError);
243 SendResponse(error_.empty());
247 ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(
248 content::BrowserContext* context)
249 : browser_context_(context) {
250 chromeos::SessionManagerClient* session_manager =
251 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
252 if (!session_manager->HasObserver(this))
253 session_manager->AddObserver(this);
256 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {}
258 void ScreenlockPrivateEventRouter::ScreenIsLocked() {
259 DispatchEvent(screenlock::OnChanged::kEventName,
260 new base::FundamentalValue(true));
263 void ScreenlockPrivateEventRouter::ScreenIsUnlocked() {
264 DispatchEvent(screenlock::OnChanged::kEventName,
265 new base::FundamentalValue(false));
268 void ScreenlockPrivateEventRouter::DispatchEvent(
269 const std::string& event_name,
271 scoped_ptr<base::ListValue> args(new base::ListValue());
274 scoped_ptr<extensions::Event> event(new extensions::Event(
275 event_name, args.Pass()));
276 extensions::ExtensionSystem::Get(browser_context_)
278 ->BroadcastEvent(event.Pass());
281 static base::LazyInstance<extensions::BrowserContextKeyedAPIFactory<
282 ScreenlockPrivateEventRouter> > g_factory = LAZY_INSTANCE_INITIALIZER;
285 extensions::BrowserContextKeyedAPIFactory<ScreenlockPrivateEventRouter>*
286 ScreenlockPrivateEventRouter::GetFactoryInstance() {
287 return g_factory.Pointer();
290 void ScreenlockPrivateEventRouter::Shutdown() {
291 chromeos::SessionManagerClient* session_manager =
292 chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
293 if (session_manager->HasObserver(this))
294 session_manager->RemoveObserver(this);
297 void ScreenlockPrivateEventRouter::OnButtonClicked() {
298 DispatchEvent(screenlock::OnButtonClicked::kEventName, NULL);
301 void ScreenlockPrivateEventRouter::OnAuthAttempted(
302 chromeos::LoginDisplay::AuthType auth_type,
303 const std::string& value) {
304 scoped_ptr<base::ListValue> args(new base::ListValue());
306 screenlock::ToString(ToScreenlockPrivateAuthType(auth_type)));
307 args->AppendString(value);
309 scoped_ptr<extensions::Event> event(new extensions::Event(
310 screenlock::OnAuthAttempted::kEventName, args.Pass()));
311 extensions::ExtensionSystem::Get(browser_context_)
313 ->BroadcastEvent(event.Pass());
316 } // namespace extensions