Chromium Code Reviews| Index: chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc |
| diff --git a/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc b/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..ea0337c0ccc515a66b399089dd6f50d263a89346 |
| --- /dev/null |
| +++ b/chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.cc |
| @@ -0,0 +1,185 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "chrome/browser/chromeos/extensions/quick_unlock_private/quick_unlock_private_api.h" |
| + |
| +#include "chrome/browser/chromeos/login/quick_unlock/pin_storage.h" |
| +#include "chrome/browser/chromeos/login/quick_unlock/pin_storage_factory.h" |
| +#include "chrome/browser/chromeos/profiles/profile_helper.h" |
| +#include "chrome/common/extensions/api/quick_unlock_private.h" |
| +#include "chromeos/login/auth/extended_authenticator.h" |
| +#include "chromeos/login/auth/user_context.h" |
| + |
| +namespace CheckPassword = extensions::api::quick_unlock_private::CheckPassword; |
| +namespace SetModes = extensions::api::quick_unlock_private::SetModes; |
| +namespace GetActiveModes = |
| + extensions::api::quick_unlock_private::GetActiveModes; |
| +namespace GetAvailableModes = |
| + extensions::api::quick_unlock_private::GetAvailableModes; |
| +namespace quick_unlock_private = extensions::api::quick_unlock_private; |
| + |
| +namespace extensions { |
| + |
| +namespace { |
| +const char kModesAndPasswordLengthMismatch[] = |
| + "|modes| and |passwords| must have the same number of elements"; |
| +const char kMultipleModesNotSupported[] = |
| + "Currently at most one quick unlock mode can be active."; |
| + |
| +QuickUnlockPrivateCheckPasswordFunction::CreateAuthenticator |
| + create_authenticator_ = nullptr; |
| +} |
| + |
| +//////////// quickUnlockPrivate.checkPassword |
| + |
| +/* static */ |
| +void QuickUnlockPrivateCheckPasswordFunction::SetCreateAuthenticatorForTesting( |
| + QuickUnlockPrivateCheckPasswordFunction::CreateAuthenticator allocator) { |
| + create_authenticator_ = allocator; |
| +} |
| + |
| +QuickUnlockPrivateCheckPasswordFunction:: |
| + QuickUnlockPrivateCheckPasswordFunction() |
| + : chrome_details_(this) { |
| + // Setup a fake authenticator if we are testing. |
| + if (create_authenticator_) |
| + extended_authenticator_ = create_authenticator_(this); |
| + else |
| + extended_authenticator_ = chromeos::ExtendedAuthenticator::Create(this); |
| +} |
| + |
| +QuickUnlockPrivateCheckPasswordFunction:: |
| + ~QuickUnlockPrivateCheckPasswordFunction() {} |
| + |
| +ExtensionFunction::ResponseAction |
| +QuickUnlockPrivateCheckPasswordFunction::Run() { |
| + std::unique_ptr<CheckPassword::Params> params( |
| + CheckPassword::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + user_manager::User* user = chromeos::ProfileHelper::Get()->GetUserByProfile( |
| + chrome_details_.GetProfile()); |
| + chromeos::UserContext user_context(user->GetAccountId()); |
| + user_context.SetKey(chromeos::Key(params->password)); |
| + |
| + // The extension function needs to stay alive while the authenticator is |
| + // running the password check. Add a ref before the authenticator starts, and |
| + // remove the ref after it invokes one of the OnAuth* callbacks. |
| + AddRef(); |
| + |
| + content::BrowserThread::PostTask( |
| + content::BrowserThread::UI, FROM_HERE, |
| + base::Bind(&chromeos::ExtendedAuthenticator::AuthenticateToCheck, |
| + extended_authenticator_.get(), user_context, base::Closure())); |
| + |
| + return RespondLater(); |
| +} |
| + |
| +void QuickUnlockPrivateCheckPasswordFunction::OnAuthFailure( |
| + const chromeos::AuthFailure& error) { |
| + results_ = CheckPassword::Results::Create(false); |
| + SendResponse(true); |
| + Release(); |
| +} |
| + |
| +void QuickUnlockPrivateCheckPasswordFunction::OnAuthSuccess( |
| + const chromeos::UserContext& user_context) { |
| + results_ = CheckPassword::Results::Create(true); |
| + SendResponse(true); |
| + Release(); |
| +} |
| + |
| +//////////// quickUnlockPrivate.setModes |
| + |
| +QuickUnlockPrivateSetModesFunction::QuickUnlockPrivateSetModesFunction() |
| + : chrome_details_(this) {} |
| + |
| +QuickUnlockPrivateSetModesFunction::~QuickUnlockPrivateSetModesFunction() {} |
| + |
| +ExtensionFunction::ResponseAction QuickUnlockPrivateSetModesFunction::Run() { |
| + std::unique_ptr<SetModes::Params> params(SetModes::Params::Create(*args_)); |
| + EXTENSION_FUNCTION_VALIDATE(params.get()); |
| + |
| + if (params->modes.size() != params->passwords.size()) |
| + return RespondNow(Error(kModesAndPasswordLengthMismatch)); |
| + |
| + if (params->modes.size() > 1) |
| + return RespondNow(Error(kMultipleModesNotSupported)); |
| + |
| + // This function is setup so it is easy to add another quick unlock mode while |
| + // following all of the invariants, which are: |
| + // |
| + // 1: If an unlock type is not specified, it should be deactivated. |
| + // 2: If a password for an unlock type is empty, it should not be touched. |
| + // 3: Otherwise, the password should be set to the new value. |
| + |
| + bool update_pin = true; |
| + std::string pin_password; |
| + |
| + // Compute values for update_pin, pin_password, etc. |
| + for (size_t i = 0; i < params->modes.size(); ++i) { |
| + const quick_unlock_private::QuickUnlockMode mode = params->modes[i]; |
| + const std::string password = params->passwords[i]; |
| + |
| + if (mode == quick_unlock_private::QUICK_UNLOCK_MODE_PIN) { |
| + update_pin = !password.empty(); |
| + pin_password = password; |
| + } |
| + } |
| + |
| + // Use the computed update_pin, pin_password, etc, values. |
| + if (update_pin) { |
| + chromeos::PinStorage* pin_storage = |
| + chromeos::PinStorageFactory::GetForProfile( |
| + chrome_details_.GetProfile()); |
| + if (pin_password.empty()) |
| + pin_storage->RemovePin(); |
| + else |
| + pin_storage->SetPin(pin_password); |
| + } |
| + |
| + return RespondNow(ArgumentList(SetModes::Results::Create(true))); |
| +} |
| + |
| +//////////// quickUnlockPrivate.getActiveModes |
| + |
| +QuickUnlockPrivateGetActiveModesFunction:: |
| + QuickUnlockPrivateGetActiveModesFunction() |
| + : chrome_details_(this) {} |
| + |
| +QuickUnlockPrivateGetActiveModesFunction:: |
| + ~QuickUnlockPrivateGetActiveModesFunction() {} |
| + |
| +ExtensionFunction::ResponseAction |
| +QuickUnlockPrivateGetActiveModesFunction::Run() { |
| + std::vector<quick_unlock_private::QuickUnlockMode> modes; |
| + |
| + chromeos::PinStorage* pin_storage = |
| + chromeos::PinStorageFactory::GetForProfile(chrome_details_.GetProfile()); |
| + if (pin_storage->IsPinSet()) |
| + modes.push_back(quick_unlock_private::QUICK_UNLOCK_MODE_PIN); |
| + |
| + return RespondNow(ArgumentList(GetActiveModes::Results::Create(modes))); |
| +} |
| + |
| +//////////// quickUnlockPrivate.getAvailableModes |
| + |
| +QuickUnlockPrivateGetAvailableModesFunction:: |
| + QuickUnlockPrivateGetAvailableModesFunction() |
| + : chrome_details_(this) {} |
| + |
| +QuickUnlockPrivateGetAvailableModesFunction:: |
| + ~QuickUnlockPrivateGetAvailableModesFunction() {} |
| + |
| +ExtensionFunction::ResponseAction |
| +QuickUnlockPrivateGetAvailableModesFunction::Run() { |
| + // TODO(jdufault): Check for policy and do not return PIN if policy makes it |
| + // unavailable. See crbug.com/612271. |
| + std::vector<quick_unlock_private::QuickUnlockMode> modes = { |
|
achuithb
2016/05/23 19:10:40
const?
jdufault
2016/05/27 20:00:21
Done.
|
| + quick_unlock_private::QUICK_UNLOCK_MODE_PIN}; |
| + |
| + return RespondNow(ArgumentList(GetAvailableModes::Results::Create(modes))); |
| +} |
| + |
| +} // namespace extensions |