Upstream version 7.35.144.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / chromeos / extensions / screenlock_private_api.cc
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.
4
5 #include "chrome/browser/chromeos/extensions/screenlock_private_api.h"
6
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"
16
17 namespace screenlock = extensions::api::screenlock_private;
18
19 namespace extensions {
20
21 namespace {
22
23 const char kNotLockedError[] = "Screen is not currently locked.";
24
25 chromeos::LoginDisplay::AuthType ToLoginDisplayAuthType(
26     screenlock::AuthType auth_type) {
27   switch (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;
34     default:
35       NOTREACHED();
36       return chromeos::LoginDisplay::OFFLINE_PASSWORD;
37   }
38 }
39
40 screenlock::AuthType ToScreenlockPrivateAuthType(
41     chromeos::LoginDisplay::AuthType auth_type) {
42   switch (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;
52     default:
53       NOTREACHED();
54       return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
55   }
56 }
57
58 }  // namespace
59
60 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {}
61
62 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {}
63
64 bool ScreenlockPrivateGetLockedFunction::RunImpl() {
65   bool locked = false;
66   chromeos::ScreenLocker* locker =
67       chromeos::ScreenLocker::default_screen_locker();
68   if (locker)
69     locked = locker->locked();
70   SetResult(new base::FundamentalValue(locked));
71   SendResponse(error_.empty());
72   return true;
73 }
74
75 ScreenlockPrivateSetLockedFunction::ScreenlockPrivateSetLockedFunction() {}
76
77 ScreenlockPrivateSetLockedFunction::~ScreenlockPrivateSetLockedFunction() {}
78
79 bool ScreenlockPrivateSetLockedFunction::RunImpl() {
80   scoped_ptr<screenlock::SetLocked::Params> params(
81       screenlock::SetLocked::Params::Create(*args_));
82   EXTENSION_FUNCTION_VALIDATE(params.get());
83   if (params->locked) {
84     chromeos::SessionManagerClient* session_manager =
85         chromeos::DBusThreadManager::Get()->GetSessionManagerClient();
86     session_manager->RequestLockScreen();
87   } else {
88     chromeos::ScreenLocker* locker =
89         chromeos::ScreenLocker::default_screen_locker();
90     if (locker)
91       chromeos::ScreenLocker::Hide();
92   }
93   SendResponse(error_.empty());
94   return true;
95 }
96
97 ScreenlockPrivateShowMessageFunction::ScreenlockPrivateShowMessageFunction() {}
98
99 ScreenlockPrivateShowMessageFunction::~ScreenlockPrivateShowMessageFunction() {}
100
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();
107   if (locker)
108     locker->ShowBannerMessage(params->message);
109   SendResponse(error_.empty());
110   return true;
111 }
112
113 static const int kMaxButtonIconSize = 40;
114
115 ScreenlockPrivateShowButtonFunction::
116   ScreenlockPrivateShowButtonFunction() {}
117
118 ScreenlockPrivateShowButtonFunction::
119   ~ScreenlockPrivateShowButtonFunction() {}
120
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();
127   if (!locker) {
128     SetError(kNotLockedError);
129     SendResponse(false);
130     return true;
131   }
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));
137   return true;
138 }
139
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(
149       user->email(),
150       image,
151       base::Bind(&ScreenlockPrivateEventRouter::OnButtonClicked,
152                  base::Unretained(router)));
153   SendResponse(error_.empty());
154 }
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       chromeos::ScreenLocker::Hide();
238     else
239       locker->EnableInput();
240   } else {
241     SetError(kNotLockedError);
242   }
243   SendResponse(error_.empty());
244   return true;
245 }
246
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);
254 }
255
256 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {}
257
258 void ScreenlockPrivateEventRouter::ScreenIsLocked() {
259   DispatchEvent(screenlock::OnChanged::kEventName,
260       new base::FundamentalValue(true));
261 }
262
263 void ScreenlockPrivateEventRouter::ScreenIsUnlocked() {
264   DispatchEvent(screenlock::OnChanged::kEventName,
265       new base::FundamentalValue(false));
266 }
267
268 void ScreenlockPrivateEventRouter::DispatchEvent(
269     const std::string& event_name,
270     base::Value* arg) {
271   scoped_ptr<base::ListValue> args(new base::ListValue());
272   if (arg)
273     args->Append(arg);
274   scoped_ptr<extensions::Event> event(new extensions::Event(
275       event_name, args.Pass()));
276   extensions::ExtensionSystem::Get(browser_context_)
277       ->event_router()
278       ->BroadcastEvent(event.Pass());
279 }
280
281 static base::LazyInstance<extensions::BrowserContextKeyedAPIFactory<
282     ScreenlockPrivateEventRouter> > g_factory = LAZY_INSTANCE_INITIALIZER;
283
284 // static
285 extensions::BrowserContextKeyedAPIFactory<ScreenlockPrivateEventRouter>*
286 ScreenlockPrivateEventRouter::GetFactoryInstance() {
287   return g_factory.Pointer();
288 }
289
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);
295 }
296
297 void ScreenlockPrivateEventRouter::OnButtonClicked() {
298   DispatchEvent(screenlock::OnButtonClicked::kEventName, NULL);
299 }
300
301 void ScreenlockPrivateEventRouter::OnAuthAttempted(
302     chromeos::LoginDisplay::AuthType auth_type,
303     const std::string& value) {
304   scoped_ptr<base::ListValue> args(new base::ListValue());
305   args->AppendString(
306       screenlock::ToString(ToScreenlockPrivateAuthType(auth_type)));
307   args->AppendString(value);
308
309   scoped_ptr<extensions::Event> event(new extensions::Event(
310       screenlock::OnAuthAttempted::kEventName, args.Pass()));
311   extensions::ExtensionSystem::Get(browser_context_)
312       ->event_router()
313       ->BroadcastEvent(event.Pass());
314 }
315
316 }  // namespace extensions