Revert 285173 "Removed InProcessBrowserTest::CleanUpOnMainThread()"
[chromium-blink-merge.git] / chrome / browser / extensions / api / screenlock_private / screenlock_private_api.cc
blob04216eb5a54589e89be3e10f18d88294c69bba76
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"
7 #include <vector>
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 {
21 namespace {
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) {
28 switch (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:
36 break;
38 NOTREACHED();
39 return ScreenlockBridge::LockHandler::OFFLINE_PASSWORD;
42 screenlock::AuthType FromLockHandlerAuthType(
43 ScreenlockBridge::LockHandler::AuthType auth_type) {
44 switch (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
56 // locking.
57 NOTREACHED();
58 return screenlock::AUTH_TYPE_NONE;
60 NOTREACHED();
61 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
64 } // namespace
66 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {}
68 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {}
70 bool ScreenlockPrivateGetLockedFunction::RunAsync() {
71 SetResult(new base::FundamentalValue(ScreenlockBridge::Get()->IsLocked()));
72 SendResponse(error_.empty());
73 return true;
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());
84 if (params->locked)
85 ScreenlockBridge::Get()->Lock(GetProfile());
86 else
87 ScreenlockBridge::Get()->Unlock(GetProfile());
88 SendResponse(error_.empty());
89 return true;
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();
102 if (locker)
103 locker->ShowBannerMessage(params->message);
104 SendResponse(error_.empty());
105 return true;
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();
120 if (!locker) {
121 SetError(kNotLockedError);
122 return false;
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;
134 } else {
135 continue;
138 ExtensionResource resource =
139 GetExtension()->GetResource(params->icon[i]->url);
140 if (resource.empty())
141 continue;
143 icon_info.push_back(
144 ImageLoader::ImageRepresentation(
145 resource,
146 ImageLoader::ImageRepresentation::RESIZE_WHEN_LARGER,
147 gfx::Size(kMaxButtonIconSize * params->icon[i]->scale_factor,
148 kMaxButtonIconSize * params->icon[i]->scale_factor),
149 scale_factor));
150 if (scale_factor == ui::SCALE_FACTOR_100P)
151 has_scale_100P = true;
154 if (!has_scale_100P) {
155 SetError(kInvalidIconError);
156 return false;
159 extensions::ImageLoader* loader = extensions::ImageLoader::Get(GetProfile());
160 loader->LoadImagesAsync(
161 GetExtension(),
162 icon_info,
163 base::Bind(&ScreenlockPrivateShowCustomIconFunction::OnImageLoaded,
164 this));
165 return true;
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()),
174 image);
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();
189 if (locker) {
190 locker->HideUserPodCustomIcon(
191 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
192 } else {
193 SetError(kNotLockedError);
195 SendResponse(error_.empty());
196 return true;
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();
210 if (locker) {
211 std::string initial_value =
212 params->initial_value.get() ? *(params->initial_value.get()) : "";
213 locker->SetAuthType(
214 ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()),
215 ToLockHandlerAuthType(params->auth_type),
216 initial_value);
217 } else {
218 SetError(kNotLockedError);
220 SendResponse(error_.empty());
221 return true;
224 ScreenlockPrivateGetAuthTypeFunction::ScreenlockPrivateGetAuthTypeFunction() {}
226 ScreenlockPrivateGetAuthTypeFunction::~ScreenlockPrivateGetAuthTypeFunction() {}
228 bool ScreenlockPrivateGetAuthTypeFunction::RunAsync() {
229 ScreenlockBridge::LockHandler* locker =
230 ScreenlockBridge::Get()->lock_handler();
231 if (locker) {
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));
237 } else {
238 SetError(kNotLockedError);
240 SendResponse(error_.empty());
241 return true;
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();
257 if (locker) {
258 if (params->accept) {
259 locker->Unlock(ScreenlockBridge::GetAuthenticatedUserEmail(GetProfile()));
260 } else {
261 locker->EnableInput();
263 } else {
264 SetError(kNotLockedError);
266 SendResponse(error_.empty());
267 return true;
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,
290 base::Value* arg) {
291 scoped_ptr<base::ListValue> args(new base::ListValue());
292 if (arg)
293 args->Append(arg);
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;
302 // static
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