Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 "chrome/common/extensions/api/quick_unlock_private.h" | |
| 11 #include "chromeos/login/auth/extended_authenticator.h" | |
| 12 #include "chromeos/login/auth/user_context.h" | |
| 13 #include "extensions/browser/event_router.h" | |
| 14 | |
| 15 namespace quick_unlock_private = extensions::api::quick_unlock_private; | |
| 16 namespace SetModes = quick_unlock_private::SetModes; | |
| 17 namespace GetActiveModes = quick_unlock_private::GetActiveModes; | |
| 18 namespace GetAvailableModes = quick_unlock_private::GetAvailableModes; | |
| 19 namespace OnActiveModesChanged = quick_unlock_private::OnActiveModesChanged; | |
| 20 using QuickUnlockMode = quick_unlock_private::QuickUnlockMode; | |
| 21 using ModeList = std::vector<QuickUnlockMode>; | |
| 22 | |
| 23 namespace extensions { | |
| 24 | |
| 25 namespace { | |
| 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 QuickUnlockPrivateSetModesFunction::CreateAuthenticator create_authenticator_ = | |
| 32 nullptr; | |
| 33 | |
| 34 // Triggers a QuickUnlockMode change event. | |
| 35 void FireEvent(Profile* profile, const ModeList& modes) { | |
| 36 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); | |
| 37 std::unique_ptr<extensions::Event> event(new extensions::Event( | |
| 38 extensions::events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, | |
| 39 OnActiveModesChanged::kEventName, std::move(args))); | |
| 40 extensions::EventRouter::Get(profile)->BroadcastEvent(std::move(event)); | |
| 41 } | |
| 42 | |
| 43 // Returns the active set of quick unlock modes. | |
| 44 ModeList GetActiveModes(Profile* profile) { | |
| 45 ModeList modes; | |
| 46 | |
| 47 chromeos::PinStorage* pin_storage = | |
| 48 chromeos::PinStorageFactory::GetForProfile(profile); | |
| 49 if (pin_storage->IsPinSet()) | |
| 50 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); | |
| 51 | |
| 52 return modes; | |
| 53 } | |
| 54 | |
| 55 // Returns true if |a| and |b| contain the same elements. The elements do not | |
| 56 // need to be in the same order. | |
| 57 bool AreModesEqual(const ModeList& a, const ModeList& b) { | |
| 58 if (a.size() != b.size()) | |
| 59 return false; | |
| 60 | |
| 61 for (size_t i = 0; i < a.size(); ++i) { | |
| 62 if (std::find(b.begin(), b.end(), a[i]) == b.end()) | |
| 63 return false; | |
| 64 } | |
| 65 | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 //////////// quickUnlockPrivate.getAvailableModes | |
| 72 | |
| 73 QuickUnlockPrivateGetAvailableModesFunction:: | |
| 74 QuickUnlockPrivateGetAvailableModesFunction() | |
| 75 : chrome_details_(this) {} | |
| 76 | |
| 77 QuickUnlockPrivateGetAvailableModesFunction:: | |
| 78 ~QuickUnlockPrivateGetAvailableModesFunction() {} | |
| 79 | |
| 80 ExtensionFunction::ResponseAction | |
| 81 QuickUnlockPrivateGetAvailableModesFunction::Run() { | |
| 82 // TODO(jdufault): Check for policy and do not return PIN if policy makes it | |
| 83 // unavailable. See crbug.com/612271. | |
| 84 const ModeList modes = {quick_unlock_private::QUICK_UNLOCK_MODE_PIN}; | |
| 85 | |
| 86 return RespondNow(ArgumentList(GetAvailableModes::Results::Create(modes))); | |
| 87 } | |
| 88 | |
| 89 //////////// quickUnlockPrivate.getActiveModes | |
| 90 | |
| 91 QuickUnlockPrivateGetActiveModesFunction:: | |
| 92 QuickUnlockPrivateGetActiveModesFunction() | |
| 93 : chrome_details_(this) {} | |
|
Devlin
2016/05/31 22:58:38
If this needs chrome details, it should probably j
jdufault
2016/06/01 00:07:35
There's a comment on top of that function saying i
| |
| 94 | |
| 95 QuickUnlockPrivateGetActiveModesFunction:: | |
| 96 ~QuickUnlockPrivateGetActiveModesFunction() {} | |
| 97 | |
| 98 ExtensionFunction::ResponseAction | |
| 99 QuickUnlockPrivateGetActiveModesFunction::Run() { | |
| 100 const ModeList modes = GetActiveModes(chrome_details_.GetProfile()); | |
| 101 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); | |
| 102 } | |
| 103 | |
| 104 //////////// quickUnlockPrivate.setModes | |
| 105 | |
| 106 /* static */ | |
|
Devlin
2016/05/31 22:58:38
nit: // static
jdufault
2016/06/01 00:07:35
Done.
| |
| 107 void QuickUnlockPrivateSetModesFunction::SetCreateAuthenticatorForTesting( | |
| 108 QuickUnlockPrivateSetModesFunction::CreateAuthenticator allocator) { | |
| 109 create_authenticator_ = allocator; | |
| 110 } | |
| 111 | |
| 112 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | |
| 113 : chrome_details_(this) { | |
|
Devlin
2016/05/31 22:58:38
ditto re ChromeUIThreadExtensionFunction
jdufault
2016/06/01 00:07:35
See above.
| |
| 114 // Setup a fake authenticator if we are testing. | |
| 115 if (create_authenticator_) | |
| 116 extended_authenticator_ = create_authenticator_(this); | |
| 117 else | |
| 118 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); | |
| 119 } | |
| 120 | |
| 121 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | |
| 122 | |
| 123 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { | |
| 124 std::unique_ptr<SetModes::Params> params(SetModes::Params::Create(*args_)); | |
| 125 EXTENSION_FUNCTION_VALIDATE(params.get()); | |
| 126 | |
| 127 if (params->modes.size() != params->credentials.size()) | |
| 128 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); | |
| 129 | |
| 130 if (params->modes.size() > 1) | |
| 131 return RespondNow(Error(kMultipleModesNotSupported)); | |
| 132 | |
| 133 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | |
| 134 chrome_details_.GetProfile()); | |
| 135 chromeos::UserContext user_context(user->GetAccountId()); | |
| 136 user_context.SetKey(chromeos::Key(params->account_password)); | |
| 137 | |
| 138 // The extension function needs to stay alive while the authenticator is | |
| 139 // running the password check. Add a ref before the authenticator starts, and | |
| 140 // remove the ref after it invokes one of the OnAuth* callbacks. | |
| 141 AddRef(); | |
|
Devlin
2016/05/31 22:58:38
Do we need this even though we have the implicit r
jdufault
2016/06/01 00:07:35
Yes, because the implicit refcount is being done o
| |
| 142 | |
| 143 content::BrowserThread::PostTask( | |
| 144 content::BrowserThread::UI, FROM_HERE, | |
| 145 base::Bind(&chromeos::ExtendedAuthenticator::AuthenticateToCheck, | |
| 146 extended_authenticator_.get(), user_context, base::Closure())); | |
| 147 | |
| 148 return RespondLater(); | |
| 149 } | |
| 150 | |
| 151 void QuickUnlockPrivateSetModesFunction::OnAuthFailure( | |
| 152 const chromeos::AuthFailure& error) { | |
| 153 results_ = SetModes::Results::Create(false); | |
|
Devlin
2016/05/31 22:58:38
Use Respond(). Also, should this be an error?
jdufault
2016/06/01 00:07:35
Done.
| |
| 154 SendResponse(true); | |
| 155 Release(); | |
| 156 } | |
| 157 | |
| 158 void QuickUnlockPrivateSetModesFunction::OnAuthSuccess( | |
| 159 const chromeos::UserContext& user_context) { | |
| 160 const ModeList initial_modes = GetActiveModes(chrome_details_.GetProfile()); | |
| 161 ApplyModeChange(); | |
| 162 const ModeList updated_modes = GetActiveModes(chrome_details_.GetProfile()); | |
| 163 | |
| 164 if (!AreModesEqual(initial_modes, updated_modes)) | |
| 165 FireEvent(chrome_details_.GetProfile(), updated_modes); | |
| 166 | |
| 167 results_ = SetModes::Results::Create(true); | |
| 168 SendResponse(true); | |
|
Devlin
2016/05/31 22:58:38
use Resond()
jdufault
2016/06/01 00:07:35
Done.
| |
| 169 Release(); | |
| 170 } | |
| 171 | |
| 172 void QuickUnlockPrivateSetModesFunction::ApplyModeChange() { | |
| 173 std::unique_ptr<SetModes::Params> params(SetModes::Params::Create(*args_)); | |
| 174 | |
| 175 // This function is setup so it is easy to add another quick unlock mode while | |
| 176 // following all of the invariants, which are: | |
| 177 // | |
| 178 // 1: If an unlock type is not specified, it should be deactivated. | |
| 179 // 2: If a credential for an unlock type is empty, it should not be touched. | |
| 180 // 3: Otherwise, the credential should be set to the new value. | |
| 181 | |
| 182 bool update_pin = true; | |
| 183 std::string pin_credential; | |
| 184 | |
| 185 // Compute values for update_pin, pin_credential, etc. | |
| 186 for (size_t i = 0; i < params->modes.size(); ++i) { | |
| 187 const QuickUnlockMode mode = params->modes[i]; | |
| 188 const std::string credential = params->credentials[i]; | |
|
Devlin
2016/05/31 22:58:38
const &?
jdufault
2016/06/01 00:07:35
Done.
| |
| 189 | |
| 190 if (mode == quick_unlock_private::QUICK_UNLOCK_MODE_PIN) { | |
| 191 update_pin = !credential.empty(); | |
| 192 pin_credential = credential; | |
| 193 } | |
| 194 } | |
| 195 | |
| 196 // Use the computed update_pin, pin_credential, etc, values. | |
| 197 if (update_pin) { | |
| 198 Profile* profile = chrome_details_.GetProfile(); | |
| 199 chromeos::PinStorage* pin_storage = | |
| 200 chromeos::PinStorageFactory::GetForProfile(profile); | |
| 201 | |
| 202 if (pin_credential.empty()) | |
| 203 pin_storage->RemovePin(); | |
| 204 else | |
| 205 pin_storage->SetPin(pin_credential); | |
| 206 } | |
| 207 } | |
| 208 | |
| 209 } // namespace extensions | |
| OLD | NEW |