Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / extensions / api / screenlock_private / screenlock_private_api.cc
blobf93b1ef613988ad55c5b0d263d0613e172cdba75
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 "base/lazy_instance.h"
8 #include "base/values.h"
9 #include "chrome/browser/profiles/profile.h"
10 #include "chrome/browser/signin/chrome_proximity_auth_client.h"
11 #include "chrome/browser/signin/easy_unlock_service.h"
12 #include "chrome/common/extensions/api/screenlock_private.h"
13 #include "chrome/common/extensions/extension_constants.h"
14 #include "components/proximity_auth/screenlock_bridge.h"
15 #include "extensions/browser/app_window/app_window_registry.h"
16 #include "extensions/browser/event_router.h"
18 namespace extensions {
20 namespace screenlock = api::screenlock_private;
22 namespace {
24 screenlock::AuthType FromLockHandlerAuthType(
25 proximity_auth::ScreenlockBridge::LockHandler::AuthType auth_type) {
26 switch (auth_type) {
27 case proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD:
28 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
29 case proximity_auth::ScreenlockBridge::LockHandler::NUMERIC_PIN:
30 return screenlock::AUTH_TYPE_NUMERICPIN;
31 case proximity_auth::ScreenlockBridge::LockHandler::USER_CLICK:
32 return screenlock::AUTH_TYPE_USERCLICK;
33 case proximity_auth::ScreenlockBridge::LockHandler::ONLINE_SIGN_IN:
34 // Apps should treat forced online sign in same as system password.
35 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
36 case proximity_auth::ScreenlockBridge::LockHandler::EXPAND_THEN_USER_CLICK:
37 // This type is used for public sessions, which do not support screen
38 // locking.
39 NOTREACHED();
40 return screenlock::AUTH_TYPE_NONE;
41 case proximity_auth::ScreenlockBridge::LockHandler::FORCE_OFFLINE_PASSWORD:
42 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
44 NOTREACHED();
45 return screenlock::AUTH_TYPE_OFFLINEPASSWORD;
48 } // namespace
50 ScreenlockPrivateGetLockedFunction::ScreenlockPrivateGetLockedFunction() {}
52 ScreenlockPrivateGetLockedFunction::~ScreenlockPrivateGetLockedFunction() {}
54 bool ScreenlockPrivateGetLockedFunction::RunAsync() {
55 SetResult(new base::FundamentalValue(
56 proximity_auth::ScreenlockBridge::Get()->IsLocked()));
57 SendResponse(error_.empty());
58 return true;
61 ScreenlockPrivateSetLockedFunction::ScreenlockPrivateSetLockedFunction() {}
63 ScreenlockPrivateSetLockedFunction::~ScreenlockPrivateSetLockedFunction() {}
65 bool ScreenlockPrivateSetLockedFunction::RunAsync() {
66 scoped_ptr<screenlock::SetLocked::Params> params(
67 screenlock::SetLocked::Params::Create(*args_));
68 EXTENSION_FUNCTION_VALIDATE(params.get());
69 EasyUnlockService* service = EasyUnlockService::Get(GetProfile());
70 if (params->locked) {
71 if (extension()->id() == extension_misc::kEasyUnlockAppId &&
72 AppWindowRegistry::Get(browser_context())
73 ->GetAppWindowForAppAndKey(extension()->id(),
74 "easy_unlock_pairing")) {
75 // Mark the Easy Unlock behaviour on the lock screen as the one initiated
76 // by the Easy Unlock setup app as a trial one.
77 // TODO(tbarzic): Move this logic to a new easyUnlockPrivate function.
78 service->SetTrialRun();
80 proximity_auth::ScreenlockBridge::Get()->Lock();
81 } else {
82 proximity_auth::ScreenlockBridge::Get()->Unlock(
83 service->proximity_auth_client()->GetAuthenticatedUsername());
85 SendResponse(error_.empty());
86 return true;
89 ScreenlockPrivateAcceptAuthAttemptFunction::
90 ScreenlockPrivateAcceptAuthAttemptFunction() {}
92 ScreenlockPrivateAcceptAuthAttemptFunction::
93 ~ScreenlockPrivateAcceptAuthAttemptFunction() {}
95 bool ScreenlockPrivateAcceptAuthAttemptFunction::RunSync() {
96 scoped_ptr<screenlock::AcceptAuthAttempt::Params> params(
97 screenlock::AcceptAuthAttempt::Params::Create(*args_));
98 EXTENSION_FUNCTION_VALIDATE(params.get());
100 Profile* profile = Profile::FromBrowserContext(browser_context());
101 EasyUnlockService* service = EasyUnlockService::Get(profile);
102 if (service)
103 service->FinalizeUnlock(params->accept);
104 return true;
107 ScreenlockPrivateEventRouter::ScreenlockPrivateEventRouter(
108 content::BrowserContext* context)
109 : browser_context_(context) {
110 proximity_auth::ScreenlockBridge::Get()->AddObserver(this);
113 ScreenlockPrivateEventRouter::~ScreenlockPrivateEventRouter() {}
115 void ScreenlockPrivateEventRouter::OnScreenDidLock(
116 proximity_auth::ScreenlockBridge::LockHandler::ScreenType screen_type) {
117 DispatchEvent(events::SCREENLOCK_PRIVATE_ON_CHANGED,
118 screenlock::OnChanged::kEventName,
119 new base::FundamentalValue(true));
122 void ScreenlockPrivateEventRouter::OnScreenDidUnlock(
123 proximity_auth::ScreenlockBridge::LockHandler::ScreenType screen_type) {
124 DispatchEvent(events::SCREENLOCK_PRIVATE_ON_CHANGED,
125 screenlock::OnChanged::kEventName,
126 new base::FundamentalValue(false));
129 void ScreenlockPrivateEventRouter::OnFocusedUserChanged(
130 const std::string& user_id) {
133 void ScreenlockPrivateEventRouter::DispatchEvent(
134 events::HistogramValue histogram_value,
135 const std::string& event_name,
136 base::Value* arg) {
137 scoped_ptr<base::ListValue> args(new base::ListValue());
138 if (arg)
139 args->Append(arg);
140 scoped_ptr<Event> event(new Event(histogram_value, event_name, args.Pass()));
141 EventRouter::Get(browser_context_)->BroadcastEvent(event.Pass());
144 static base::LazyInstance<
145 BrowserContextKeyedAPIFactory<ScreenlockPrivateEventRouter>> g_factory =
146 LAZY_INSTANCE_INITIALIZER;
148 // static
149 BrowserContextKeyedAPIFactory<ScreenlockPrivateEventRouter>*
150 ScreenlockPrivateEventRouter::GetFactoryInstance() {
151 return g_factory.Pointer();
154 void ScreenlockPrivateEventRouter::Shutdown() {
155 proximity_auth::ScreenlockBridge::Get()->RemoveObserver(this);
158 bool ScreenlockPrivateEventRouter::OnAuthAttempted(
159 proximity_auth::ScreenlockBridge::LockHandler::AuthType auth_type,
160 const std::string& value) {
161 EventRouter* router = EventRouter::Get(browser_context_);
162 if (!router->HasEventListener(screenlock::OnAuthAttempted::kEventName))
163 return false;
165 scoped_ptr<base::ListValue> args(new base::ListValue());
166 args->AppendString(screenlock::ToString(FromLockHandlerAuthType(auth_type)));
167 args->AppendString(value);
169 scoped_ptr<Event> event(
170 new Event(events::SCREENLOCK_PRIVATE_ON_AUTH_ATTEMPTED,
171 screenlock::OnAuthAttempted::kEventName, args.Pass()));
172 router->BroadcastEvent(event.Pass());
173 return true;
176 } // namespace extensions