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 "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 const char kModesAndCredentialsLengthMismatch[] = | |
| 26 "|modes| and |credentials| must have the same number of elements"; | |
| 27 const char kMultipleModesNotSupported[] = | |
| 28 "Currently at most one quick unlock mode can be active."; | |
| 29 | |
| 30 QuickUnlockPrivateSetModesFunction::CreateAuthenticator create_authenticator_ = | |
|
Devlin
2016/06/08 23:26:42
nit: this should be something like 'g_create_authe
jdufault
2016/06/09 02:41:48
Done.
| |
| 31 nullptr; | |
| 32 QuickUnlockPrivateSetModesFunction::ModesChangedEventHandler | |
| 33 modes_changed_handler_ = nullptr; | |
|
Devlin
2016/06/08 23:26:41
ditto
jdufault
2016/06/09 02:41:49
Done.
| |
| 34 | |
| 35 // Triggers a QuickUnlockMode change event. | |
| 36 void FireEvent(Profile* profile, const ModeList& modes) { | |
| 37 // Allow unit tests to override how events are raised/handled. | |
| 38 if (modes_changed_handler_) { | |
| 39 modes_changed_handler_(modes); | |
| 40 return; | |
| 41 } | |
| 42 | |
| 43 std::unique_ptr<base::ListValue> args = OnActiveModesChanged::Create(modes); | |
| 44 std::unique_ptr<Event> event( | |
| 45 new Event(events::QUICK_UNLOCK_PRIVATE_ON_ACTIVE_MODES_CHANGED, | |
| 46 OnActiveModesChanged::kEventName, std::move(args))); | |
| 47 EventRouter::Get(profile)->BroadcastEvent(std::move(event)); | |
| 48 } | |
| 49 | |
| 50 // Returns the active set of quick unlock modes. | |
| 51 ModeList GetActiveModes(Profile* profile) { | |
| 52 ModeList modes; | |
| 53 | |
| 54 chromeos::PinStorage* pin_storage = | |
| 55 chromeos::PinStorageFactory::GetForProfile(profile); | |
| 56 if (pin_storage->IsPinSet()) | |
| 57 modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); | |
| 58 | |
| 59 return modes; | |
| 60 } | |
| 61 | |
| 62 // Returns true if |a| and |b| contain the same elements. The elements do not | |
| 63 // need to be in the same order. | |
| 64 bool AreModesEqual(const ModeList& a, const ModeList& b) { | |
| 65 if (a.size() != b.size()) | |
| 66 return false; | |
| 67 | |
| 68 // This is a slow comparison algorithm, but the number of entries in |a| and | |
| 69 // |b| will always be very low (0-3 items) so it doesn't matter. | |
| 70 for (size_t i = 0; i < a.size(); ++i) { | |
| 71 if (std::find(b.begin(), b.end(), a[i]) == b.end()) | |
| 72 return false; | |
| 73 } | |
| 74 | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 } // namespace | |
| 79 | |
| 80 //////////// quickUnlockPrivate.getAvailableModes | |
| 81 | |
| 82 QuickUnlockPrivateGetAvailableModesFunction:: | |
| 83 QuickUnlockPrivateGetAvailableModesFunction() | |
| 84 : chrome_details_(this) {} | |
| 85 | |
| 86 QuickUnlockPrivateGetAvailableModesFunction:: | |
| 87 ~QuickUnlockPrivateGetAvailableModesFunction() {} | |
| 88 | |
| 89 ExtensionFunction::ResponseAction | |
| 90 QuickUnlockPrivateGetAvailableModesFunction::Run() { | |
| 91 // TODO(jdufault): Check for policy and do not return PIN if policy makes it | |
| 92 // unavailable. See crbug.com/612271. | |
| 93 const ModeList modes = {quick_unlock_private::QUICK_UNLOCK_MODE_PIN}; | |
| 94 | |
| 95 return RespondNow(ArgumentList(GetAvailableModes::Results::Create(modes))); | |
| 96 } | |
| 97 | |
| 98 //////////// quickUnlockPrivate.getActiveModes | |
| 99 | |
| 100 QuickUnlockPrivateGetActiveModesFunction:: | |
| 101 QuickUnlockPrivateGetActiveModesFunction() | |
| 102 : chrome_details_(this) {} | |
| 103 | |
| 104 QuickUnlockPrivateGetActiveModesFunction:: | |
| 105 ~QuickUnlockPrivateGetActiveModesFunction() {} | |
| 106 | |
| 107 ExtensionFunction::ResponseAction | |
| 108 QuickUnlockPrivateGetActiveModesFunction::Run() { | |
| 109 const ModeList modes = GetActiveModes(chrome_details_.GetProfile()); | |
| 110 return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); | |
| 111 } | |
| 112 | |
| 113 //////////// quickUnlockPrivate.setModes | |
| 114 | |
| 115 // static | |
| 116 void QuickUnlockPrivateSetModesFunction::SetCreateAuthenticatorForTesting( | |
| 117 QuickUnlockPrivateSetModesFunction::CreateAuthenticator allocator) { | |
| 118 create_authenticator_ = allocator; | |
| 119 } | |
| 120 | |
| 121 // static | |
| 122 void QuickUnlockPrivateSetModesFunction::SetModesChangedEventHandlerForTesting( | |
| 123 ModesChangedEventHandler handler) { | |
| 124 modes_changed_handler_ = handler; | |
| 125 } | |
| 126 | |
| 127 QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() | |
| 128 : chrome_details_(this) { | |
| 129 // Setup a fake authenticator if we are testing. | |
| 130 if (create_authenticator_) | |
| 131 extended_authenticator_ = create_authenticator_(this); | |
| 132 else | |
| 133 extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); | |
| 134 } | |
| 135 | |
| 136 QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} | |
| 137 | |
| 138 ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { | |
| 139 params_ = SetModes::Params::Create(*args_); | |
| 140 EXTENSION_FUNCTION_VALIDATE(params_.get()); | |
| 141 | |
| 142 if (params_->modes.size() != params_->credentials.size()) | |
| 143 return RespondNow(Error(kModesAndCredentialsLengthMismatch)); | |
| 144 | |
| 145 if (params_->modes.size() > 1) | |
| 146 return RespondNow(Error(kMultipleModesNotSupported)); | |
| 147 | |
| 148 user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( | |
| 149 chrome_details_.GetProfile()); | |
| 150 chromeos::UserContext user_context(user->GetAccountId()); | |
| 151 user_context.SetKey(chromeos::Key(params_->account_password)); | |
| 152 | |
| 153 // The extension function needs to stay alive while the authenticator is | |
| 154 // running the password check. Add a ref before the authenticator starts, and | |
| 155 // remove the ref after it invokes one of the OnAuth* callbacks. | |
| 156 AddRef(); | |
| 157 | |
| 158 content::BrowserThread::PostTask( | |
| 159 content::BrowserThread::UI, FROM_HERE, | |
| 160 base::Bind(&chromeos::ExtendedAuthenticator::AuthenticateToCheck, | |
| 161 extended_authenticator_.get(), user_context, base::Closure())); | |
| 162 | |
| 163 return RespondLater(); | |
| 164 } | |
| 165 | |
| 166 void QuickUnlockPrivateSetModesFunction::OnAuthFailure( | |
| 167 const chromeos::AuthFailure& error) { | |
| 168 Respond(ArgumentList(SetModes::Results::Create(false))); | |
| 169 Release(); // Balanced in Run(). | |
| 170 } | |
| 171 | |
| 172 void QuickUnlockPrivateSetModesFunction::OnAuthSuccess( | |
| 173 const chromeos::UserContext& user_context) { | |
| 174 const ModeList initial_modes = GetActiveModes(chrome_details_.GetProfile()); | |
| 175 ApplyModeChange(); | |
| 176 const ModeList updated_modes = GetActiveModes(chrome_details_.GetProfile()); | |
| 177 | |
| 178 if (!AreModesEqual(initial_modes, updated_modes)) | |
| 179 FireEvent(chrome_details_.GetProfile(), updated_modes); | |
| 180 | |
| 181 Respond(ArgumentList(SetModes::Results::Create(true))); | |
| 182 Release(); // Balanced in Run(). | |
| 183 } | |
| 184 | |
| 185 void QuickUnlockPrivateSetModesFunction::ApplyModeChange() { | |
| 186 // This function is setup so it is easy to add another quick unlock mode while | |
| 187 // following all of the invariants, which are: | |
| 188 // | |
| 189 // 1: If an unlock type is not specified, it should be deactivated. | |
| 190 // 2: If a credential for an unlock type is empty, it should not be touched. | |
| 191 // 3: Otherwise, the credential should be set to the new value. | |
| 192 | |
| 193 bool update_pin = true; | |
| 194 std::string pin_credential; | |
| 195 | |
| 196 // Compute needed changes. | |
| 197 for (size_t i = 0; i < params_->modes.size(); ++i) { | |
| 198 const QuickUnlockMode mode = params_->modes[i]; | |
| 199 const std::string& credential = params_->credentials[i]; | |
| 200 | |
| 201 if (mode == quick_unlock_private::QUICK_UNLOCK_MODE_PIN) { | |
| 202 update_pin = !credential.empty(); | |
| 203 pin_credential = credential; | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 // Apply changes. | |
| 208 if (update_pin) { | |
| 209 Profile* profile = chrome_details_.GetProfile(); | |
| 210 chromeos::PinStorage* pin_storage = | |
| 211 chromeos::PinStorageFactory::GetForProfile(profile); | |
| 212 | |
| 213 if (pin_credential.empty()) | |
| 214 pin_storage->RemovePin(); | |
| 215 else | |
| 216 pin_storage->SetPin(pin_credential); | |
| 217 } | |
| 218 } | |
| 219 | |
| 220 } // namespace extensions | |
| OLD | NEW |