Upstream version 10.38.208.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / extensions / api / screenlock_private / 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/extensions/api/screenlock_private/screenlock_private_api.h"
6
7 #include <vector>
8
9 #include "base/lazy_instance.h"
10 #include "base/strings/utf_string_conversions.h"
11 #include "base/values.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/common/extensions/api/screenlock_private.h"
14 #include "extensions/browser/event_router.h"
15 #include "extensions/browser/image_loader.h"
16 #include "ui/gfx/image/image.h"
17
18 namespace screenlock = extensions::api::screenlock_private;
19
20 namespace extensions {
21
22 namespace {
23
24 const char kNotLockedError[] = "Screen is not currently locked.";
25 const char kInvalidIconError[] = "Invalid custom icon data.";
26
27 ScreenlockBridge::LockHandler::AuthType ToLockHandlerAuthType(
28     screenlock::AuthType auth_type) {
29   switch (auth_type) {
30     case screenlock::AUTH_TYPE_OFFLINEPASSWORD:
31       return ScreenlockBridge::LockHandler::OFFLINE_PASSWORD;
32     case screenlock::AUTH_TYPE_NUMERICPIN:
33       return ScreenlockBridge::LockHandler::NUMERIC_PIN;
34     case screenlock::AUTH_TYPE_USERCLICK:
35       return ScreenlockBridge::LockHandler::USER_CLICK;
36     case screenlock::AUTH_TYPE_NONE:
37       break;
38   }
39   NOTREACHED();
40   return ScreenlockBridge::LockHandler::OFFLINE_PASSWORD;
41 }
42
43 screenlock::AuthType FromLockHandlerAuthType(
44     ScreenlockBridge::LockHandler::AuthType auth_type) {
45   switch (auth_type) {
46     case ScreenlockBridge::LockHandler::OFFLINE_PASSWORD:
47       return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
48     case ScreenlockBridge::LockHandler::NUMERIC_PIN:
49       return screenlock::AUTH_TYPE_NUMERICPIN;
50     case ScreenlockBridge::LockHandler::USER_CLICK:
51       return screenlock::AUTH_TYPE_USERCLICK;
52     case ScreenlockBridge::LockHandler::ONLINE_SIGN_IN:
53       // Apps should treat forced online sign in same as system password.
54       return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
55     case ScreenlockBridge::LockHandler::EXPAND_THEN_USER_CLICK:
56       // This type is used for public sessions, which do not support screen
57       // locking.
58       NOTREACHED();
59       return screenlock::AUTH_TYPE_NONE;
60     case ScreenlockBridge::LockHandler::FORCE_OFFLINE_PASSWORD:
61       return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
62   }
63   NOTREACHED();
64   return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
65 }
66
67 }  // namespace
68
69 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {}
70
71 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {}
72
73 bool ScreenlockPrivateGetLockedFunction::RunAsync() {
74   SetResult(new base::FundamentalValue(ScreenlockBridge::Get()->IsLocked()));
75   SendResponse(error_.empty());
76   return true;
77 }
78
79 ScreenlockPrivateSetLockedFunction::ScreenlockPrivateSetLockedFunction() {}
80
81 ScreenlockPrivateSetLockedFunction::~ScreenlockPrivateSetLockedFunction() {}
82
83 bool ScreenlockPrivateSetLockedFunction::RunAsync() {
84   scoped_ptr<screenlock::SetLocked::Params> params(
85       screenlock::SetLocked::Params::Create(*args_));
86   EXTENSION_FUNCTION_VALIDATE(params.get());
87   if (params->locked)
88     ScreenlockBridge::Get()->Lock(GetProfile());
89   else
90     ScreenlockBridge::Get()->Unlock(GetProfile());
91   SendResponse(error_.empty());
92   return true;
93 }
94
95 ScreenlockPrivateShowMessageFunction::ScreenlockPrivateShowMessageFunction() {}
96
97 ScreenlockPrivateShowMessageFunction::~ScreenlockPrivateShowMessageFunction() {}
98
99 bool ScreenlockPrivateShowMessageFunction::RunAsync() {
100   scoped_ptr<screenlock::ShowMessage::Params> params(
101       screenlock::ShowMessage::Params::Create(*args_));
102   EXTENSION_FUNCTION_VALIDATE(params.get());
103   ScreenlockBridge::LockHandler* locker =
104       ScreenlockBridge::Get()->lock_handler();
105   if (locker)
106     locker->ShowBannerMessage(base::UTF8ToUTF16(params->message));
107   SendResponse(error_.empty());
108   return true;
109 }
110
111 ScreenlockPrivateShowCustomIconFunction::
112   ScreenlockPrivateShowCustomIconFunction() {}
113
114 ScreenlockPrivateShowCustomIconFunction::
115   ~ScreenlockPrivateShowCustomIconFunction() {}
116
117 bool ScreenlockPrivateShowCustomIconFunction::RunAsync() {
118   scoped_ptr<screenlock::ShowCustomIcon::Params> params(
119       screenlock::ShowCustomIcon::Params::Create(*args_));
120   EXTENSION_FUNCTION_VALIDATE(params.get());
121   ScreenlockBridge::LockHandler* locker =
122       ScreenlockBridge::Get()->lock_handler();
123   if (!locker) {
124     SetError(kNotLockedError);
125     return false;
126   }
127
128   const int kMaxButtonIconSize = 40;
129   bool has_scale_100P = false;
130   std::vector<extensions::ImageLoader::ImageRepresentation> icon_info;
131   for (size_t i = 0; i < params->icon.size(); ++i) {
132     ui::ScaleFactor scale_factor;
133     if (params->icon[i]->scale_factor == 1.) {
134       scale_factor = ui::SCALE_FACTOR_100P;
135     } else if (params->icon[i]->scale_factor == 2.) {
136       scale_factor = ui::SCALE_FACTOR_200P;
137     } else {
138       continue;
139     }
140
141     ExtensionResource resource = extension()->GetResource(params->icon[i]->url);
142     if (resource.empty())
143       continue;
144
145     icon_info.push_back(
146         ImageLoader::ImageRepresentation(
147             resource,
148             ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER,
149             gfx::Size(kMaxButtonIconSize * params->icon[i]->scale_factor,
150                       kMaxButtonIconSize * params->icon[i]->scale_factor),
151             scale_factor));
152     if (scale_factor == ui::SCALE_FACTOR_100P)
153       has_scale_100P = true;
154   }
155
156   if (!has_scale_100P) {
157     SetError(kInvalidIconError);
158     return false;
159   }
160
161   extensions::ImageLoader* loader = extensions::ImageLoader::Get(GetProfile());
162   loader->LoadImagesAsync(
163       extension(),
164       icon_info,
165       base::Bind(&ScreenlockPrivateShowCustomIconFunction::OnImageLoaded,
166                  this));
167   return true;
168 }
169
170 void ScreenlockPrivateShowCustomIconFunction::OnImageLoaded(
171     const gfx::Image& image) {
172   ScreenlockBridge::LockHandler* locker =
173       ScreenlockBridge::Get()->lock_handler();
174   if (!locker) {
175     SetError(kNotLockedError);
176     SendResponse(false);
177     return;
178   }
179
180   ScreenlockBridge::UserPodCustomIconOptions icon;
181   icon.SetIconAsImage(image);
182   locker->ShowUserPodCustomIcon(
183       ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()),
184       icon);
185   SendResponse(error_.empty());
186 }
187
188 ScreenlockPrivateHideCustomIconFunction::
189     ScreenlockPrivateHideCustomIconFunction() {
190 }
191
192 ScreenlockPrivateHideCustomIconFunction::
193     ~ScreenlockPrivateHideCustomIconFunction() {
194 }
195
196 bool ScreenlockPrivateHideCustomIconFunction::RunAsync() {
197   ScreenlockBridge::LockHandler* locker =
198       ScreenlockBridge::Get()->lock_handler();
199   if (locker) {
200     locker->HideUserPodCustomIcon(
201         ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
202   } else {
203     SetError(kNotLockedError);
204   }
205   SendResponse(error_.empty());
206   return true;
207 }
208
209 ScreenlockPrivateSetAuthTypeFunction::ScreenlockPrivateSetAuthTypeFunction() {}
210
211 ScreenlockPrivateSetAuthTypeFunction::~ScreenlockPrivateSetAuthTypeFunction() {}
212
213 bool ScreenlockPrivateSetAuthTypeFunction::RunAsync() {
214   scoped_ptr<screenlock::SetAuthType::Params> params(
215       screenlock::SetAuthType::Params::Create(*args_));
216   EXTENSION_FUNCTION_VALIDATE(params.get());
217
218   ScreenlockBridge::LockHandler* locker =
219       ScreenlockBridge::Get()->lock_handler();
220   if (locker) {
221     std::string initial_value =
222         params->initial_value.get() ? *(params->initial_value.get()) : "";
223     locker->SetAuthType(
224         ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()),
225         ToLockHandlerAuthType(params->auth_type),
226         base::UTF8ToUTF16(initial_value));
227   } else {
228     SetError(kNotLockedError);
229   }
230   SendResponse(error_.empty());
231   return true;
232 }
233
234 ScreenlockPrivateGetAuthTypeFunction::ScreenlockPrivateGetAuthTypeFunction() {}
235
236 ScreenlockPrivateGetAuthTypeFunction::~ScreenlockPrivateGetAuthTypeFunction() {}
237
238 bool ScreenlockPrivateGetAuthTypeFunction::RunAsync() {
239   ScreenlockBridge::LockHandler* locker =
240       ScreenlockBridge::Get()->lock_handler();
241   if (locker) {
242     ScreenlockBridge::LockHandler::AuthType auth_type = locker->GetAuthType(
243         ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
244     std::string auth_type_name =
245         screenlock::ToString(FromLockHandlerAuthType(auth_type));
246     SetResult(new base::StringValue(auth_type_name));
247   } else {
248     SetError(kNotLockedError);
249   }
250   SendResponse(error_.empty());
251   return true;
252 }
253
254 ScreenlockPrivateAcceptAuthAttemptFunction::
255     ScreenlockPrivateAcceptAuthAttemptFunction() {}
256
257 ScreenlockPrivateAcceptAuthAttemptFunction::
258     ~ScreenlockPrivateAcceptAuthAttemptFunction() {}
259
260 bool ScreenlockPrivateAcceptAuthAttemptFunction::RunAsync() {
261   scoped_ptr<screenlock::AcceptAuthAttempt::Params> params(
262       screenlock::AcceptAuthAttempt::Params::Create(*args_));
263   EXTENSION_FUNCTION_VALIDATE(params.get());
264
265   ScreenlockBridge::LockHandler* locker =
266       ScreenlockBridge::Get()->lock_handler();
267   if (locker) {
268     if (params->accept) {
269       locker->Unlock(ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
270     } else {
271       locker->EnableInput();
272     }
273   } else {
274     SetError(kNotLockedError);
275   }
276   SendResponse(error_.empty());
277   return true;
278 }
279
280 ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(
281     content::BrowserContext* context)
282     : browser_context_(context) {
283   ScreenlockBridge::Get()->AddObserver(this);
284 }
285
286 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {}
287
288 void ScreenlockPrivateEventRouter::OnScreenDidLock() {
289   DispatchEvent(screenlock::OnChanged::kEventName,
290       new base::FundamentalValue(true));
291 }
292
293 void ScreenlockPrivateEventRouter::OnScreenDidUnlock() {
294   DispatchEvent(screenlock::OnChanged::kEventName,
295       new base::FundamentalValue(false));
296 }
297
298 void ScreenlockPrivateEventRouter::DispatchEvent(
299     const std::string& event_name,
300     base::Value* arg) {
301   scoped_ptr<base::ListValue> args(new base::ListValue());
302   if (arg)
303     args->Append(arg);
304   scoped_ptr<extensions::Event> event(new extensions::Event(
305       event_name, args.Pass()));
306   extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
307 }
308
309 static base::LazyInstance<extensions::BrowserContextKeyedAPIFactory<
310     ScreenlockPrivateEventRouter> > g_factory = LAZY_INSTANCE_INITIALIZER;
311
312 // static
313 extensions::BrowserContextKeyedAPIFactory<ScreenlockPrivateEventRouter>*
314 ScreenlockPrivateEventRouter::GetFactoryInstance() {
315   return g_factory.Pointer();
316 }
317
318 void ScreenlockPrivateEventRouter::Shutdown() {
319   ScreenlockBridge::Get()->RemoveObserver(this);
320 }
321
322 void ScreenlockPrivateEventRouter::OnAuthAttempted(
323     ScreenlockBridge::LockHandler::AuthType auth_type,
324     const std::string& value) {
325   scoped_ptr<base::ListValue> args(new base::ListValue());
326   args->AppendString(screenlock::ToString(FromLockHandlerAuthType(auth_type)));
327   args->AppendString(value);
328
329   scoped_ptr<extensions::Event> event(new extensions::Event(
330       screenlock::OnAuthAttempted::kEventName, args.Pass()));
331   extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
332 }
333
334 }  // namespace extensions