Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(679)

Side by Side Diff: chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc

Issue 1968083004: Implement the private API for quick unlock. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lkgr
Patch Set: Move to base::Callback, adjust api_test_utils api, address comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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/chromeos/extensions/quick_unlock_private/quick_unlock_p rivate_api.h"
6
7 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h"
8 #include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h"
9 #include "chrome/browser/chromeos/profiles/profile_helper.h"
10 #include "chromeos/login/auth/extended_authenticator.h"
11 #include "chromeos/login/auth/user_context.h"
12 #include "extensions/browser/event_router.h"
13
14 namespace quick_unlock_private = extensions::api::quick_unlock_private;
15 namespace SetModes = quick_unlock_private::SetModes;
16 namespace GetActiveModes = quick_unlock_private::GetActiveModes;
17 namespace GetAvailableModes = quick_unlock_private::GetAvailableModes;
18 namespace OnActiveModesChanged = quick_unlock_private::OnActiveModesChanged;
19 using QuickUnlockMode = quick_unlock_private::QuickUnlockMode;
20 using ModeList = std::vector<QuickUnlockMode>;
21
22 namespace extensions {
23
24 namespace {
25
26 const char kModesAndCredentialsLengthMismatch[] =
27 "|modes| and |credentials| must have the same number of elements";
28 const char kMultipleModesNotSupported[] =
29 "Currently at most one quick unlock mode can be active.";
30
31 // Returns the active set of quick unlock modes.
32 ModeList GetActiveModes(Profile* profile) {
33 ModeList modes;
34
35 chromeos::PinStorage* pin_storage =
36 chromeos::PinStorageFactory::GetForProfile(profile);
37 if (pin_storage->IsPinSet())
38 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN);
39
40 return modes;
41 }
42
43 // Returns true if |a| and |b| contain the same elements. The elements do not
44 // need to be in the same order.
45 bool AreModesEqual(const ModeList& a, const ModeList& b) {
46 if (a.size() != b.size())
47 return false;
48
49 // This is a slow comparison algorithm, but the number of entries in |a| and
50 // |b| will always be very low (0-3 items) so it doesn't matter.
51 for (size_t i = 0; i < a.size(); ++i) {
52 if (std::find(b.begin(), b.end(), a[i]) == b.end())
53 return false;
54 }
55
56 return true;
57 }
58
59 } // namespace
60
61 //////////// quickUnlockPrivate.getAvailableModes
62
63 QuickUnlockPrivateGetAvailableModesFunction::
64 QuickUnlockPrivateGetAvailableModesFunction()
65 : chrome_details_(this) {}
66
67 QuickUnlockPrivateGetAvailableModesFunction::
68 ~QuickUnlockPrivateGetAvailableModesFunction() {}
69
70 ExtensionFunction::ResponseAction
71 QuickUnlockPrivateGetAvailableModesFunction::Run() {
72 // TODO(jdufault): Check for policy and do not return PIN if policy makes it
73 // unavailable. See crbug.com/612271.
74 const ModeList modes = {quick_unlock_private::QUICK_UNLOCK_MODE_PIN};
75
76 return RespondNow(ArgumentList(GetAvailableModes::Results::Create(modes)));
77 }
78
79 //////////// quickUnlockPrivate.getActiveModes
80
81 QuickUnlockPrivateGetActiveModesFunction::
82 QuickUnlockPrivateGetActiveModesFunction()
83 : chrome_details_(this) {}
84
85 QuickUnlockPrivateGetActiveModesFunction::
86 ~QuickUnlockPrivateGetActiveModesFunction() {}
87
88 ExtensionFunction::ResponseAction
89 QuickUnlockPrivateGetActiveModesFunction::Run() {
90 const ModeList modes = GetActiveModes(chrome_details_.GetProfile());
91 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes)));
92 }
93
94 //////////// quickUnlockPrivate.setModes
95
96 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction()
97 : chrome_details_(this) {}
98
99 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {}
100
101 void QuickUnlockPrivateSetModesFunction::SetAuthenticatorAllocatorForTesting(
102 QuickUnlockPrivateSetModesFunction::AuthenticatorAllocator allocator) {
103 authenticator_allocator_ = allocator;
104 }
105
106 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting(
107 ModesChangedEventHandler handler) {
108 modes_changed_handler_ = handler;
109 }
110
111 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() {
112 params_ = SetModes::Params::Create(*args_);
113 EXTENSION_FUNCTION_VALIDATE(params_.get());
114
115 if (params_->modes.size() != params_->credentials.size())
116 return RespondNow(Error(kModesAndCredentialsLengthMismatch));
117
118 if (params_->modes.size() > 1)
119 return RespondNow(Error(kMultipleModesNotSupported));
120
121 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile(
122 chrome_details_.GetProfile());
123 chromeos::UserContext user_context(user->GetAccountId());
124 user_context.SetKey(chromeos::Key(params_->account_password));
125
126 // The extension function needs to stay alive while the authenticator is
127 // running the password check. Add a ref before the authenticator starts, and
128 // remove the ref after it invokes one of the OnAuth* callbacks.
129 AddRef();
130
131 // Lazily allocate the authenticator. We do this here, instead of in the ctor,
132 // so that tests can install a fake.
133 if (authenticator_allocator_.is_null())
134 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this);
135 else
136 extended_authenticator_ = authenticator_allocator_.Run(this);
137
138 content::BrowserThread::PostTask(
139 content::BrowserThread::UI, FROM_HERE,
140 base::Bind(&chromeos::ExtendedAuthenticator::AuthenticateToCheck,
141 extended_authenticator_.get(), user_context, base::Closure()));
142
143 return RespondLater();
144 }
145
146 void QuickUnlockPrivateSetModesFunction::OnAuthFailure(
147 const chromeos::AuthFailure& error) {
148 Respond(ArgumentList(SetModes::Results::Create(false)));
149 Release(); // Balanced in Run().
150 }
151
152 void QuickUnlockPrivateSetModesFunction::OnAuthSuccess(
153 const chromeos::UserContext& user_context) {
154 const ModeList initial_modes = GetActiveModes(chrome_details_.GetProfile());
155 ApplyModeChange();
156 const ModeList updated_modes = GetActiveModes(chrome_details_.GetProfile());
157
158 if (!AreModesEqual(initial_modes, updated_modes))
159 FireEvent(updated_modes);
160
161 Respond(ArgumentList(SetModes::Results::Create(true)));
162 Release(); // Balanced in Run().
163 }
164
165 void QuickUnlockPrivateSetModesFunction::ApplyModeChange() {
166 // This function is setup so it is easy to add another quick unlock mode while
167 // following all of the invariants, which are:
168 //
169 // 1: If an unlock type is not specified, it should be deactivated.
170 // 2: If a credential for an unlock type is empty, it should not be touched.
171 // 3: Otherwise, the credential should be set to the new value.
172
173 bool update_pin = true;
174 std::string pin_credential;
175
176 // Compute needed changes.
177 for (size_t i = 0; i < params_->modes.size(); ++i) {
178 const QuickUnlockMode mode = params_->modes[i];
179 const std::string& credential = params_->credentials[i];
180
181 if (mode == quick_unlock_private::QUICK_UNLOCK_MODE_PIN) {
182 update_pin = !credential.empty();
183 pin_credential = credential;
184 }
185 }
186
187 // Apply changes.
188 if (update_pin) {
189 Profile* profile = chrome_details_.GetProfile();
190 chromeos::PinStorage* pin_storage =
191 chromeos::PinStorageFactory::GetForProfile(profile);
192
193 if (pin_credential.empty())
194 pin_storage->RemovePin();
195 else
196 pin_storage->SetPin(pin_credential);
197 }
198 }
199
200 // Triggers a QuickUnlockMode change event.
201 void QuickUnlockPrivateSetModesFunction::FireEvent(const ModeList& modes) {
202 // Allow unit tests to override how events are raised/handled.
203 if (!modes_changed_handler_.is_null()) {
204 modes_changed_handler_.Run(modes);
205 return;
206 }
207
208 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes);
209 std::unique_ptr<Event> event(
210 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED,
211 OnActiveModesChanged::kEventName, std::move(args)));
212 Profile* profile = chrome_details_.GetProfile();
213 EventRouter::Get(profile)->BroadcastEvent(std::move(event));
Devlin 2016/06/21 00:16:45 nit: just use browser_context() here.
jdufault 2016/06/21 18:46:39 Done.
214 }
215
216 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698