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/extensions/api/screenlock_private/screenlock_private_api.h"
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"
18 namespace screenlock = extensions::api::screenlock_private;
20 namespace extensions {
24 const char kNotLockedError[] = "Screen is not currently locked.";
25 const char kInvalidIconError[] = "Invalid custom icon data.";
27 ScreenlockBridge::LockHandler::AuthType ToLockHandlerAuthType(
28 screenlock::AuthType 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:
40 return ScreenlockBridge::LockHandler::OFFLINE_PASSWORD;
43 screenlock::AuthType FromLockHandlerAuthType(
44 ScreenlockBridge::LockHandler::AuthType 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
59 return screenlock::AUTH_TYPE_NONE;
60 case ScreenlockBridge::LockHandler::FORCE_OFFLINE_PASSWORD:
61 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
64 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
69 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {}
71 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {}
73 bool ScreenlockPrivateGetLockedFunction::RunAsync() {
74 SetResult(new base::FundamentalValue(ScreenlockBridge::Get()->IsLocked()));
75 SendResponse(error_.empty());
79 ScreenlockPrivateSetLockedFunction::ScreenlockPrivateSetLockedFunction() {}
81 ScreenlockPrivateSetLockedFunction::~ScreenlockPrivateSetLockedFunction() {}
83 bool ScreenlockPrivateSetLockedFunction::RunAsync() {
84 scoped_ptr<screenlock::SetLocked::Params> params(
85 screenlock::SetLocked::Params::Create(*args_));
86 EXTENSION_FUNCTION_VALIDATE(params.get());
88 ScreenlockBridge::Get()->Lock(GetProfile());
90 ScreenlockBridge::Get()->Unlock(GetProfile());
91 SendResponse(error_.empty());
95 ScreenlockPrivateShowMessageFunction::ScreenlockPrivateShowMessageFunction() {}
97 ScreenlockPrivateShowMessageFunction::~ScreenlockPrivateShowMessageFunction() {}
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();
106 locker->ShowBannerMessage(base::UTF8ToUTF16(params->message));
107 SendResponse(error_.empty());
111 ScreenlockPrivateShowCustomIconFunction::
112 ScreenlockPrivateShowCustomIconFunction() {}
114 ScreenlockPrivateShowCustomIconFunction::
115 ~ScreenlockPrivateShowCustomIconFunction() {}
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();
124 SetError(kNotLockedError);
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;
141 ExtensionResource resource = extension()->GetResource(params->icon[i]->url);
142 if (resource.empty())
146 ImageLoader::ImageRepresentation(
148 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER,
149 gfx::Size(kMaxButtonIconSize * params->icon[i]->scale_factor,
150 kMaxButtonIconSize * params->icon[i]->scale_factor),
152 if (scale_factor == ui::SCALE_FACTOR_100P)
153 has_scale_100P = true;
156 if (!has_scale_100P) {
157 SetError(kInvalidIconError);
161 extensions::ImageLoader* loader = extensions::ImageLoader::Get(GetProfile());
162 loader->LoadImagesAsync(
165 base::Bind(&ScreenlockPrivateShowCustomIconFunction::OnImageLoaded,
170 void ScreenlockPrivateShowCustomIconFunction::OnImageLoaded(
171 const gfx::Image& image) {
172 ScreenlockBridge::LockHandler* locker =
173 ScreenlockBridge::Get()->lock_handler();
175 SetError(kNotLockedError);
180 ScreenlockBridge::UserPodCustomIconOptions icon;
181 icon.SetIconAsImage(image);
182 locker->ShowUserPodCustomIcon(
183 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()),
185 SendResponse(error_.empty());
188 ScreenlockPrivateHideCustomIconFunction::
189 ScreenlockPrivateHideCustomIconFunction() {
192 ScreenlockPrivateHideCustomIconFunction::
193 ~ScreenlockPrivateHideCustomIconFunction() {
196 bool ScreenlockPrivateHideCustomIconFunction::RunAsync() {
197 ScreenlockBridge::LockHandler* locker =
198 ScreenlockBridge::Get()->lock_handler();
200 locker->HideUserPodCustomIcon(
201 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
203 SetError(kNotLockedError);
205 SendResponse(error_.empty());
209 ScreenlockPrivateSetAuthTypeFunction::ScreenlockPrivateSetAuthTypeFunction() {}
211 ScreenlockPrivateSetAuthTypeFunction::~ScreenlockPrivateSetAuthTypeFunction() {}
213 bool ScreenlockPrivateSetAuthTypeFunction::RunAsync() {
214 scoped_ptr<screenlock::SetAuthType::Params> params(
215 screenlock::SetAuthType::Params::Create(*args_));
216 EXTENSION_FUNCTION_VALIDATE(params.get());
218 ScreenlockBridge::LockHandler* locker =
219 ScreenlockBridge::Get()->lock_handler();
221 std::string initial_value =
222 params->initial_value.get() ? *(params->initial_value.get()) : "";
224 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()),
225 ToLockHandlerAuthType(params->auth_type),
226 base::UTF8ToUTF16(initial_value));
228 SetError(kNotLockedError);
230 SendResponse(error_.empty());
234 ScreenlockPrivateGetAuthTypeFunction::ScreenlockPrivateGetAuthTypeFunction() {}
236 ScreenlockPrivateGetAuthTypeFunction::~ScreenlockPrivateGetAuthTypeFunction() {}
238 bool ScreenlockPrivateGetAuthTypeFunction::RunAsync() {
239 ScreenlockBridge::LockHandler* locker =
240 ScreenlockBridge::Get()->lock_handler();
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));
248 SetError(kNotLockedError);
250 SendResponse(error_.empty());
254 ScreenlockPrivateAcceptAuthAttemptFunction::
255 ScreenlockPrivateAcceptAuthAttemptFunction() {}
257 ScreenlockPrivateAcceptAuthAttemptFunction::
258 ~ScreenlockPrivateAcceptAuthAttemptFunction() {}
260 bool ScreenlockPrivateAcceptAuthAttemptFunction::RunAsync() {
261 scoped_ptr<screenlock::AcceptAuthAttempt::Params> params(
262 screenlock::AcceptAuthAttempt::Params::Create(*args_));
263 EXTENSION_FUNCTION_VALIDATE(params.get());
265 ScreenlockBridge::LockHandler* locker =
266 ScreenlockBridge::Get()->lock_handler();
268 if (params->accept) {
269 locker->Unlock(ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
271 locker->EnableInput();
274 SetError(kNotLockedError);
276 SendResponse(error_.empty());
280 ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(
281 content::BrowserContext* context)
282 : browser_context_(context) {
283 ScreenlockBridge::Get()->AddObserver(this);
286 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {}
288 void ScreenlockPrivateEventRouter::OnScreenDidLock() {
289 DispatchEvent(screenlock::OnChanged::kEventName,
290 new base::FundamentalValue(true));
293 void ScreenlockPrivateEventRouter::OnScreenDidUnlock() {
294 DispatchEvent(screenlock::OnChanged::kEventName,
295 new base::FundamentalValue(false));
298 void ScreenlockPrivateEventRouter::DispatchEvent(
299 const std::string& event_name,
301 scoped_ptr<base::ListValue> args(new base::ListValue());
304 scoped_ptr<extensions::Event> event(new extensions::Event(
305 event_name, args.Pass()));
306 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
309 static base::LazyInstance<extensions::BrowserContextKeyedAPIFactory<
310 ScreenlockPrivateEventRouter> > g_factory = LAZY_INSTANCE_INITIALIZER;
313 extensions::BrowserContextKeyedAPIFactory<ScreenlockPrivateEventRouter>*
314 ScreenlockPrivateEventRouter::GetFactoryInstance() {
315 return g_factory.Pointer();
318 void ScreenlockPrivateEventRouter::Shutdown() {
319 ScreenlockBridge::Get()->RemoveObserver(this);
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);
329 scoped_ptr<extensions::Event> event(new extensions::Event(
330 screenlock::OnAuthAttempted::kEventName, args.Pass()));
331 extensions::EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
334 } // namespace extensions