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/values.h"
11 #include "chrome/browser/profiles/profile.h"
12 #include "chrome/common/extensions/api/screenlock_private.h"
13 #include "extensions/browser/event_router.h"
14 #include "extensions/browser/image_loader.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.";
24 const char kInvalidIconError
[] = "Invalid custom icon data.";
26 ScreenlockBridge::LockHandler::AuthType
ToLockHandlerAuthType(
27 screenlock::AuthType auth_type
) {
29 case screenlock::AUTH_TYPE_OFFLINEPASSWORD
:
30 return ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
;
31 case screenlock::AUTH_TYPE_NUMERICPIN
:
32 return ScreenlockBridge::LockHandler::NUMERIC_PIN
;
33 case screenlock::AUTH_TYPE_USERCLICK
:
34 return ScreenlockBridge::LockHandler::USER_CLICK
;
35 case screenlock::AUTH_TYPE_NONE
:
39 return ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
;
42 screenlock::AuthType
FromLockHandlerAuthType(
43 ScreenlockBridge::LockHandler::AuthType auth_type
) {
45 case ScreenlockBridge::LockHandler::OFFLINE_PASSWORD
:
46 return screenlock::AUTH_TYPE_OFFLINEPASSWORD
;
47 case ScreenlockBridge::LockHandler::NUMERIC_PIN
:
48 return screenlock::AUTH_TYPE_NUMERICPIN
;
49 case ScreenlockBridge::LockHandler::USER_CLICK
:
50 return screenlock::AUTH_TYPE_USERCLICK
;
51 case ScreenlockBridge::LockHandler::ONLINE_SIGN_IN
:
52 // Apps should treat forced online sign in same as system password.
53 return screenlock::AUTH_TYPE_OFFLINEPASSWORD
;
54 case ScreenlockBridge::LockHandler::EXPAND_THEN_USER_CLICK
:
55 // This type is used for public sessions, which do not support screen
58 return screenlock::AUTH_TYPE_NONE
;
61 return screenlock::AUTH_TYPE_OFFLINEPASSWORD
;
66 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {}
68 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {}
70 bool ScreenlockPrivateGetLockedFunction::RunAsync() {
71 SetResult(new base::FundamentalValue(ScreenlockBridge::Get()->IsLocked()));
72 SendResponse(error_
.empty());
76 ScreenlockPrivateSetLockedFunction::ScreenlockPrivateSetLockedFunction() {}
78 ScreenlockPrivateSetLockedFunction::~ScreenlockPrivateSetLockedFunction() {}
80 bool ScreenlockPrivateSetLockedFunction::RunAsync() {
81 scoped_ptr
<screenlock::SetLocked::Params
> params(
82 screenlock::SetLocked::Params::Create(*args_
));
83 EXTENSION_FUNCTION_VALIDATE(params
.get());
85 ScreenlockBridge::Get()->Lock(GetProfile());
87 ScreenlockBridge::Get()->Unlock(GetProfile());
88 SendResponse(error_
.empty());
92 ScreenlockPrivateShowMessageFunction::ScreenlockPrivateShowMessageFunction() {}
94 ScreenlockPrivateShowMessageFunction::~ScreenlockPrivateShowMessageFunction() {}
96 bool ScreenlockPrivateShowMessageFunction::RunAsync() {
97 scoped_ptr
<screenlock::ShowMessage::Params
> params(
98 screenlock::ShowMessage::Params::Create(*args_
));
99 EXTENSION_FUNCTION_VALIDATE(params
.get());
100 ScreenlockBridge::LockHandler
* locker
=
101 ScreenlockBridge::Get()->lock_handler();
103 locker
->ShowBannerMessage(params
->message
);
104 SendResponse(error_
.empty());
108 ScreenlockPrivateShowCustomIconFunction::
109 ScreenlockPrivateShowCustomIconFunction() {}
111 ScreenlockPrivateShowCustomIconFunction::
112 ~ScreenlockPrivateShowCustomIconFunction() {}
114 bool ScreenlockPrivateShowCustomIconFunction::RunAsync() {
115 scoped_ptr
<screenlock::ShowCustomIcon::Params
> params(
116 screenlock::ShowCustomIcon::Params::Create(*args_
));
117 EXTENSION_FUNCTION_VALIDATE(params
.get());
118 ScreenlockBridge::LockHandler
* locker
=
119 ScreenlockBridge::Get()->lock_handler();
121 SetError(kNotLockedError
);
125 const int kMaxButtonIconSize
= 40;
126 bool has_scale_100P
= false;
127 std::vector
<extensions::ImageLoader::ImageRepresentation
> icon_info
;
128 for (size_t i
= 0; i
< params
->icon
.size(); ++i
) {
129 ui::ScaleFactor scale_factor
;
130 if (params
->icon
[i
]->scale_factor
== 1.) {
131 scale_factor
= ui::SCALE_FACTOR_100P
;
132 } else if (params
->icon
[i
]->scale_factor
== 2.) {
133 scale_factor
= ui::SCALE_FACTOR_200P
;
138 ExtensionResource resource
=
139 GetExtension()->GetResource(params
->icon
[i
]->url
);
140 if (resource
.empty())
144 ImageLoader::ImageRepresentation(
146 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER
,
147 gfx::Size(kMaxButtonIconSize
* params
->icon
[i
]->scale_factor
,
148 kMaxButtonIconSize
* params
->icon
[i
]->scale_factor
),
150 if (scale_factor
== ui::SCALE_FACTOR_100P
)
151 has_scale_100P
= true;
154 if (!has_scale_100P
) {
155 SetError(kInvalidIconError
);
159 extensions::ImageLoader
* loader
= extensions::ImageLoader::Get(GetProfile());
160 loader
->LoadImagesAsync(
163 base::Bind(&ScreenlockPrivateShowCustomIconFunction::OnImageLoaded
,
168 void ScreenlockPrivateShowCustomIconFunction::OnImageLoaded(
169 const gfx::Image
& image
) {
170 ScreenlockBridge::LockHandler
* locker
=
171 ScreenlockBridge::Get()->lock_handler();
172 locker
->ShowUserPodCustomIcon(
173 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()),
175 SendResponse(error_
.empty());
178 ScreenlockPrivateHideCustomIconFunction::
179 ScreenlockPrivateHideCustomIconFunction() {
182 ScreenlockPrivateHideCustomIconFunction::
183 ~ScreenlockPrivateHideCustomIconFunction() {
186 bool ScreenlockPrivateHideCustomIconFunction::RunAsync() {
187 ScreenlockBridge::LockHandler
* locker
=
188 ScreenlockBridge::Get()->lock_handler();
190 locker
->HideUserPodCustomIcon(
191 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
193 SetError(kNotLockedError
);
195 SendResponse(error_
.empty());
199 ScreenlockPrivateSetAuthTypeFunction::ScreenlockPrivateSetAuthTypeFunction() {}
201 ScreenlockPrivateSetAuthTypeFunction::~ScreenlockPrivateSetAuthTypeFunction() {}
203 bool ScreenlockPrivateSetAuthTypeFunction::RunAsync() {
204 scoped_ptr
<screenlock::SetAuthType::Params
> params(
205 screenlock::SetAuthType::Params::Create(*args_
));
206 EXTENSION_FUNCTION_VALIDATE(params
.get());
208 ScreenlockBridge::LockHandler
* locker
=
209 ScreenlockBridge::Get()->lock_handler();
211 std::string initial_value
=
212 params
->initial_value
.get() ? *(params
->initial_value
.get()) : "";
214 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()),
215 ToLockHandlerAuthType(params
->auth_type
),
218 SetError(kNotLockedError
);
220 SendResponse(error_
.empty());
224 ScreenlockPrivateGetAuthTypeFunction::ScreenlockPrivateGetAuthTypeFunction() {}
226 ScreenlockPrivateGetAuthTypeFunction::~ScreenlockPrivateGetAuthTypeFunction() {}
228 bool ScreenlockPrivateGetAuthTypeFunction::RunAsync() {
229 ScreenlockBridge::LockHandler
* locker
=
230 ScreenlockBridge::Get()->lock_handler();
232 ScreenlockBridge::LockHandler::AuthType auth_type
= locker
->GetAuthType(
233 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
234 std::string auth_type_name
=
235 screenlock::ToString(FromLockHandlerAuthType(auth_type
));
236 SetResult(new base::StringValue(auth_type_name
));
238 SetError(kNotLockedError
);
240 SendResponse(error_
.empty());
244 ScreenlockPrivateAcceptAuthAttemptFunction::
245 ScreenlockPrivateAcceptAuthAttemptFunction() {}
247 ScreenlockPrivateAcceptAuthAttemptFunction::
248 ~ScreenlockPrivateAcceptAuthAttemptFunction() {}
250 bool ScreenlockPrivateAcceptAuthAttemptFunction::RunAsync() {
251 scoped_ptr
<screenlock::AcceptAuthAttempt::Params
> params(
252 screenlock::AcceptAuthAttempt::Params::Create(*args_
));
253 EXTENSION_FUNCTION_VALIDATE(params
.get());
255 ScreenlockBridge::LockHandler
* locker
=
256 ScreenlockBridge::Get()->lock_handler();
258 if (params
->accept
) {
259 locker
->Unlock(ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
261 locker
->EnableInput();
264 SetError(kNotLockedError
);
266 SendResponse(error_
.empty());
270 ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(
271 content::BrowserContext
* context
)
272 : browser_context_(context
) {
273 ScreenlockBridge::Get()->AddObserver(this);
276 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {}
278 void ScreenlockPrivateEventRouter::OnScreenDidLock() {
279 DispatchEvent(screenlock::OnChanged::kEventName
,
280 new base::FundamentalValue(true));
283 void ScreenlockPrivateEventRouter::OnScreenDidUnlock() {
284 DispatchEvent(screenlock::OnChanged::kEventName
,
285 new base::FundamentalValue(false));
288 void ScreenlockPrivateEventRouter::DispatchEvent(
289 const std::string
& event_name
,
291 scoped_ptr
<base::ListValue
> args(new base::ListValue());
294 scoped_ptr
<extensions::Event
> event(new extensions::Event(
295 event_name
, args
.Pass()));
296 extensions::EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
299 static base::LazyInstance
<extensions::BrowserContextKeyedAPIFactory
<
300 ScreenlockPrivateEventRouter
> > g_factory
= LAZY_INSTANCE_INITIALIZER
;
303 extensions::BrowserContextKeyedAPIFactory
<ScreenlockPrivateEventRouter
>*
304 ScreenlockPrivateEventRouter::GetFactoryInstance() {
305 return g_factory
.Pointer();
308 void ScreenlockPrivateEventRouter::Shutdown() {
309 ScreenlockBridge::Get()->RemoveObserver(this);
312 void ScreenlockPrivateEventRouter::OnAuthAttempted(
313 ScreenlockBridge::LockHandler::AuthType auth_type
,
314 const std::string
& value
) {
315 scoped_ptr
<base::ListValue
> args(new base::ListValue());
316 args
->AppendString(screenlock::ToString(FromLockHandlerAuthType(auth_type
)));
317 args
->AppendString(value
);
319 scoped_ptr
<extensions::Event
> event(new extensions::Event(
320 screenlock::OnAuthAttempted::kEventName
, args
.Pass()));
321 extensions::EventRouter::Get(browser_context_
)->BroadcastEvent(event
.Pass());
324 } // namespace extensions