Commit d290d1cb authored by Reilly Grant's avatar Reilly Grant Committed by Commit Bot

Convert chromeos/tpm to base::Bind and base::Callback to Once/Repeating

Bug: 1007665
Change-Id: I0c22e26a575e4cb346944c6419d26ea4bc165eef
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1918648
Auto-Submit: Reilly Grant <reillyg@chromium.org>
Commit-Queue: Achuith Bhandarkar <achuith@chromium.org>
Reviewed-by: default avatarAchuith Bhandarkar <achuith@chromium.org>
Cr-Commit-Position: refs/heads/master@{#716427}
parent 5a685fac
......@@ -133,8 +133,8 @@ void InstallAttributes::Init(const base::FilePath& cache_file) {
// actually calling TriggerConsistencyCheck().
consistency_check_running_ = true;
cryptohome_client_->WaitForServiceToBeAvailable(
base::Bind(&InstallAttributes::OnCryptohomeServiceInitiallyAvailable,
weak_ptr_factory_.GetWeakPtr()));
base::BindOnce(&InstallAttributes::OnCryptohomeServiceInitiallyAvailable,
weak_ptr_factory_.GetWeakPtr()));
if (!base::PathExists(cache_file)) {
LOG_IF(WARNING, base::SysInfo::IsRunningOnChromeOS())
......@@ -171,18 +171,18 @@ void InstallAttributes::Init(const base::FilePath& cache_file) {
DecodeInstallAttributes(attr_map);
}
void InstallAttributes::ReadImmutableAttributes(const base::Closure& callback) {
void InstallAttributes::ReadImmutableAttributes(base::OnceClosure callback) {
if (device_locked_) {
callback.Run();
std::move(callback).Run();
return;
}
cryptohome_client_->InstallAttributesIsReady(
base::Bind(&InstallAttributes::ReadAttributesIfReady,
weak_ptr_factory_.GetWeakPtr(), callback));
base::BindOnce(&InstallAttributes::ReadAttributesIfReady,
weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}
void InstallAttributes::ReadAttributesIfReady(const base::Closure& callback,
void InstallAttributes::ReadAttributesIfReady(base::OnceClosure callback,
base::Optional<bool> is_ready) {
if (is_ready.value_or(false)) {
registration_mode_ = policy::DEVICE_MODE_NOT_SET;
......@@ -206,7 +206,7 @@ void InstallAttributes::ReadAttributesIfReady(const base::Closure& callback,
DecodeInstallAttributes(attr_map);
}
}
callback.Run();
std::move(callback).Run();
}
void InstallAttributes::SetBlockDevmodeInTpm(
......@@ -232,7 +232,7 @@ void InstallAttributes::LockDevice(policy::DeviceMode device_mode,
const std::string& domain,
const std::string& realm,
const std::string& device_id,
const LockResultCallback& callback) {
LockResultCallback callback) {
CHECK((device_mode == policy::DEVICE_MODE_ENTERPRISE && !domain.empty() &&
realm.empty() && !device_id.empty()) ||
(device_mode == policy::DEVICE_MODE_ENTERPRISE_AD && domain.empty() &&
......@@ -241,7 +241,7 @@ void InstallAttributes::LockDevice(policy::DeviceMode device_mode,
realm.empty() && !device_id.empty()) ||
(device_mode == policy::DEVICE_MODE_CONSUMER_KIOSK_AUTOLAUNCH &&
domain.empty() && realm.empty() && device_id.empty()));
DCHECK(!callback.is_null());
DCHECK(callback);
CHECK_EQ(device_lock_running_, false);
// Check for existing lock first.
......@@ -250,20 +250,20 @@ void InstallAttributes::LockDevice(policy::DeviceMode device_mode,
LOG(ERROR) << "Trying to re-lock with wrong mode: device_mode: "
<< device_mode
<< ", registration_mode: " << registration_mode_;
callback.Run(LOCK_WRONG_MODE);
std::move(callback).Run(LOCK_WRONG_MODE);
return;
}
if (domain != registration_domain_ || realm != registration_realm_ ||
device_id != registration_device_id_) {
LOG(ERROR) << "Trying to re-lock with non-matching parameters.";
callback.Run(LOCK_WRONG_DOMAIN);
std::move(callback).Run(LOCK_WRONG_DOMAIN);
return;
}
// Already locked in the right mode, signal success.
ReportExistingLockUma(true /* is_existing_lock */);
callback.Run(LOCK_SUCCESS);
std::move(callback).Run(LOCK_SUCCESS);
return;
}
......@@ -272,9 +272,9 @@ void InstallAttributes::LockDevice(policy::DeviceMode device_mode,
// device locking must wait for TPM initialization anyways.
if (consistency_check_running_) {
CHECK(post_check_action_.is_null());
post_check_action_ = base::Bind(&InstallAttributes::LockDevice,
weak_ptr_factory_.GetWeakPtr(), device_mode,
domain, realm, device_id, callback);
post_check_action_ = base::BindOnce(
&InstallAttributes::LockDevice, weak_ptr_factory_.GetWeakPtr(),
device_mode, domain, realm, device_id, std::move(callback));
return;
}
......@@ -282,7 +282,7 @@ void InstallAttributes::LockDevice(policy::DeviceMode device_mode,
cryptohome_client_->InstallAttributesIsReady(
base::BindOnce(&InstallAttributes::LockDeviceIfAttributesIsReady,
weak_ptr_factory_.GetWeakPtr(), device_mode, domain, realm,
device_id, callback));
device_id, std::move(callback)));
}
void InstallAttributes::LockDeviceIfAttributesIsReady(
......@@ -290,11 +290,11 @@ void InstallAttributes::LockDeviceIfAttributesIsReady(
const std::string& domain,
const std::string& realm,
const std::string& device_id,
const LockResultCallback& callback,
LockResultCallback callback,
base::Optional<bool> is_ready) {
if (!is_ready.has_value() || !is_ready.value()) {
device_lock_running_ = false;
callback.Run(LOCK_NOT_READY);
std::move(callback).Run(LOCK_NOT_READY);
return;
}
......@@ -308,14 +308,14 @@ void InstallAttributes::LockDeviceIfAttributesIsReady(
if (tpm_util::InstallAttributesIsInvalid()) {
LOG(ERROR) << "Install attributes invalid.";
device_lock_running_ = false;
callback.Run(LOCK_BACKEND_INVALID);
std::move(callback).Run(LOCK_BACKEND_INVALID);
return;
}
if (!tpm_util::InstallAttributesIsFirstInstall()) {
LOG(ERROR) << "Install attributes already installed.";
device_lock_running_ = false;
callback.Run(LOCK_ALREADY_LOCKED);
std::move(callback).Run(LOCK_ALREADY_LOCKED);
return;
}
......@@ -336,7 +336,7 @@ void InstallAttributes::LockDeviceIfAttributesIsReady(
!tpm_util::InstallAttributesSet(kAttrEnterpriseDeviceId, device_id)) {
LOG(ERROR) << "Failed writing attributes.";
device_lock_running_ = false;
callback.Run(LOCK_SET_ERROR);
std::move(callback).Run(LOCK_SET_ERROR);
return;
}
......@@ -344,33 +344,32 @@ void InstallAttributes::LockDeviceIfAttributesIsReady(
tpm_util::InstallAttributesIsFirstInstall()) {
LOG(ERROR) << "Failed locking.";
device_lock_running_ = false;
callback.Run(LOCK_FINALIZE_ERROR);
std::move(callback).Run(LOCK_FINALIZE_ERROR);
return;
}
ReadImmutableAttributes(
base::Bind(&InstallAttributes::OnReadImmutableAttributes,
weak_ptr_factory_.GetWeakPtr(), device_mode, domain, realm,
device_id, callback));
base::BindOnce(&InstallAttributes::OnReadImmutableAttributes,
weak_ptr_factory_.GetWeakPtr(), device_mode, domain, realm,
device_id, std::move(callback)));
}
void InstallAttributes::OnReadImmutableAttributes(
policy::DeviceMode mode,
const std::string& domain,
const std::string& realm,
const std::string& device_id,
const LockResultCallback& callback) {
void InstallAttributes::OnReadImmutableAttributes(policy::DeviceMode mode,
const std::string& domain,
const std::string& realm,
const std::string& device_id,
LockResultCallback callback) {
device_lock_running_ = false;
if (registration_mode_ != mode || registration_domain_ != domain ||
registration_realm_ != realm || registration_device_id_ != device_id) {
LOG(ERROR) << "Locked data doesn't match.";
callback.Run(LOCK_READBACK_ERROR);
std::move(callback).Run(LOCK_READBACK_ERROR);
return;
}
ReportExistingLockUma(false /* is_existing_lock */);
callback.Run(LOCK_SUCCESS);
std::move(callback).Run(LOCK_SUCCESS);
}
bool InstallAttributes::IsEnterpriseManaged() const {
......@@ -404,8 +403,8 @@ bool InstallAttributes::IsConsumerKioskDeviceWithAutoLaunch() {
void InstallAttributes::TriggerConsistencyCheck(int dbus_retries) {
cryptohome_client_->TpmGetPassword(
base::Bind(&InstallAttributes::OnTpmGetPasswordCompleted,
weak_ptr_factory_.GetWeakPtr(), dbus_retries));
base::BindOnce(&InstallAttributes::OnTpmGetPasswordCompleted,
weak_ptr_factory_.GetWeakPtr(), dbus_retries));
}
void InstallAttributes::OnTpmGetPasswordCompleted(
......@@ -440,8 +439,8 @@ void InstallAttributes::OnTpmGetPasswordCompleted(
// Run any action (LockDevice call) that might have queued behind the
// consistency check.
consistency_check_running_ = false;
if (!post_check_action_.is_null()) {
post_check_action_.Run();
if (post_check_action_) {
std::move(post_check_action_).Run();
post_check_action_.Reset();
}
}
......
......@@ -44,7 +44,7 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) InstallAttributes {
};
// A callback to handle responses of methods returning a LockResult value.
typedef base::Callback<void(LockResult lock_result)> LockResultCallback;
using LockResultCallback = base::OnceCallback<void(LockResult lock_result)>;
// Manage singleton instance.
static void Initialize();
......@@ -72,7 +72,7 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) InstallAttributes {
// up to date with what cryptohome has. This method checks the readiness of
// attributes and read them if ready. Actual read will be performed in
// ReadAttributesIfReady().
void ReadImmutableAttributes(const base::Closure& callback);
void ReadImmutableAttributes(base::OnceClosure callback);
// Updates the firmware management parameters from TPM, storing the devmode
// flag according to |block_devmode|. Invokes |callback| when done. Must be
......@@ -90,7 +90,7 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) InstallAttributes {
const std::string& domain,
const std::string& realm,
const std::string& device_id,
const LockResultCallback& callback);
LockResultCallback callback);
// Checks whether this devices is under any kind of enterprise management.
bool IsEnterpriseManaged() const;
......@@ -133,7 +133,7 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) InstallAttributes {
bool consistency_check_running_ = false;
// To be run after the consistency check has finished.
base::Closure post_check_action_;
base::OnceClosure post_check_action_;
// Wether the LockDevice() initiated TPM calls are running.
bool device_lock_running_ = false;
......@@ -187,7 +187,7 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) InstallAttributes {
const std::map<std::string, std::string>& attr_map);
// Helper for ReadImmutableAttributes.
void ReadAttributesIfReady(const base::Closure& callback,
void ReadAttributesIfReady(base::OnceClosure callback,
base::Optional<bool> response);
// Helper for LockDevice(). Handles the result of InstallAttributesIsReady()
......@@ -196,7 +196,7 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) InstallAttributes {
const std::string& domain,
const std::string& realm,
const std::string& device_id,
const LockResultCallback& callback,
LockResultCallback callback,
base::Optional<bool> response);
// Confirms the registered user and invoke the callback.
......@@ -204,7 +204,7 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) InstallAttributes {
const std::string& domain,
const std::string& realm,
const std::string& device_id,
const LockResultCallback& callback);
LockResultCallback callback);
// Check state of install attributes against TPM lock state and generate UMA
// for the result. Asynchronously retry |dbus_retries| times in case of DBUS
......
......@@ -84,7 +84,7 @@ class InstallAttributesTest : public testing::Test {
InstallAttributes::LockResult result;
install_attributes_->LockDevice(
device_mode, domain, realm, device_id,
base::Bind(&CopyLockResult, &loop, &result));
base::BindOnce(&CopyLockResult, &loop, &result));
loop.Run();
return result;
}
......
......@@ -21,9 +21,9 @@ namespace chromeos {
namespace {
void PostResultToTaskRunner(scoped_refptr<base::SequencedTaskRunner> runner,
const base::Callback<void(bool)>& callback,
base::OnceCallback<void(bool)> callback,
bool success) {
runner->PostTask(FROM_HERE, base::BindOnce(callback, success));
runner->PostTask(FROM_HERE, base::BindOnce(std::move(callback), success));
}
} // namespace
......@@ -97,14 +97,14 @@ TPMTokenLoader::~TPMTokenLoader() {
}
TPMTokenLoader::TPMTokenStatus TPMTokenLoader::IsTPMTokenEnabled(
const TPMReadyCallback& callback) {
TPMReadyCallback callback) {
if (tpm_token_state_ == TPM_TOKEN_INITIALIZED)
return TPM_TOKEN_STATUS_ENABLED;
if (!IsTPMLoadingEnabled() || tpm_token_state_ == TPM_DISABLED)
return TPM_TOKEN_STATUS_DISABLED;
// Status is not known yet.
if (!callback.is_null())
tpm_ready_callback_list_.push_back(callback);
if (callback)
tpm_ready_callback_list_.push_back(std::move(callback));
return TPM_TOKEN_STATUS_UNDETERMINED;
}
......@@ -173,10 +173,10 @@ void TPMTokenLoader::ContinueTokenInitialization() {
FROM_HERE,
base::BindOnce(
&crypto::InitializeTPMTokenAndSystemSlot, tpm_token_slot_id_,
base::Bind(&PostResultToTaskRunner,
base::ThreadTaskRunnerHandle::Get(),
base::Bind(&TPMTokenLoader::OnTPMTokenInitialized,
weak_factory_.GetWeakPtr()))));
base::BindOnce(
&PostResultToTaskRunner, base::ThreadTaskRunnerHandle::Get(),
base::BindOnce(&TPMTokenLoader::OnTPMTokenInitialized,
weak_factory_.GetWeakPtr()))));
return;
}
case TPM_TOKEN_INITIALIZED: {
......@@ -218,11 +218,8 @@ void TPMTokenLoader::NotifyTPMTokenReady() {
DCHECK(tpm_token_state_ == TPM_DISABLED ||
tpm_token_state_ == TPM_TOKEN_INITIALIZED);
bool tpm_status = tpm_token_state_ == TPM_TOKEN_INITIALIZED;
for (TPMReadyCallbackList::iterator i = tpm_ready_callback_list_.begin();
i != tpm_ready_callback_list_.end();
++i) {
i->Run(tpm_status);
}
for (TPMReadyCallback& callback : tpm_ready_callback_list_)
std::move(callback).Run(tpm_status);
tpm_ready_callback_list_.clear();
}
......
......@@ -43,8 +43,8 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) TPMTokenLoader
TPM_TOKEN_STATUS_DISABLED
};
typedef base::Callback<void(bool)> TPMReadyCallback;
typedef std::vector<TPMReadyCallback> TPMReadyCallbackList;
using TPMReadyCallback = base::OnceCallback<void(bool)>;
using TPMReadyCallbackList = std::vector<TPMReadyCallback>;
// Sets the global instance. Must be called before any calls to Get().
// The global instance will immediately start observing |LoginState|.
......@@ -78,7 +78,7 @@ class COMPONENT_EXPORT(CHROMEOS_TPM) TPMTokenLoader
// Checks if the TPM token is enabled. If the state is unknown, |callback|
// will be called back once the TPM state is known.
TPMTokenStatus IsTPMTokenEnabled(const TPMReadyCallback& callback);
TPMTokenStatus IsTPMTokenEnabled(TPMReadyCallback callback);
std::string tpm_user_pin() const { return tpm_user_pin_; }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment