diff options
author | pneubeck <pneubeck@chromium.org> | 2014-09-11 13:13:58 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2014-09-11 20:21:05 +0000 |
commit | a52b2d041ac1f2e2905b0b9e5d222cee5a9ff116 (patch) | |
tree | 7e0a465520f5b618dc8816648cd399bb890b3670 /chromeos | |
parent | cca2bcd58f7fe1ef6100265c0d2f40e9c5e23f8c (diff) | |
download | chromium_src-a52b2d041ac1f2e2905b0b9e5d222cee5a9ff116.zip chromium_src-a52b2d041ac1f2e2905b0b9e5d222cee5a9ff116.tar.gz chromium_src-a52b2d041ac1f2e2905b0b9e5d222cee5a9ff116.tar.bz2 |
TPMTokenLoader: Report both the disabled and enabled state of the TPM.
Also change from the observer pattern to the simpler callback pattern, as there is only a single notification type (TPM is enabled/disabled).
This simplifies ownership of the observer/receiver especially if multiple threads (typically UI and IO) are involved: A temporary callback with a WeakPtr can be used instead of an observer object that has to be owned by someone.
BUG=413219
Review URL: https://codereview.chromium.org/567553002
Cr-Commit-Position: refs/heads/master@{#294454}
Diffstat (limited to 'chromeos')
-rw-r--r-- | chromeos/tpm_token_loader.cc | 43 | ||||
-rw-r--r-- | chromeos/tpm_token_loader.h | 27 |
2 files changed, 40 insertions, 30 deletions
diff --git a/chromeos/tpm_token_loader.cc b/chromeos/tpm_token_loader.cc index a469b84..55e0883 100644 --- a/chromeos/tpm_token_loader.cc +++ b/chromeos/tpm_token_loader.cc @@ -106,17 +106,24 @@ TPMTokenLoader::~TPMTokenLoader() { LoginState::Get()->RemoveObserver(this); } -void TPMTokenLoader::AddObserver(TPMTokenLoader::Observer* observer) { - observers_.AddObserver(observer); +TPMTokenLoader::TPMTokenStatus TPMTokenLoader::IsTPMTokenEnabled( + const 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); + return TPM_TOKEN_STATUS_UNDETERMINED; } -void TPMTokenLoader::RemoveObserver(TPMTokenLoader::Observer* observer) { - observers_.RemoveObserver(observer); -} - -bool TPMTokenLoader::IsTPMTokenReady() const { - return tpm_token_state_ == TPM_DISABLED || - tpm_token_state_ == TPM_TOKEN_INITIALIZED; +bool TPMTokenLoader::IsTPMLoadingEnabled() const { + // TPM loading is enabled on non-ChromeOS environments, e.g. when running + // tests on Linux. + // Treat TPM as disabled for guest users since they do not store certs. + return initialized_for_test_ || (base::SysInfo::IsRunningOnChromeOS() && + LoginState::Get()->IsGuestSessionUser()); } void TPMTokenLoader::MaybeStartTokenInitialization() { @@ -136,11 +143,7 @@ void TPMTokenLoader::MaybeStartTokenInitialization() { if (!start_initialization) return; - if (!base::SysInfo::IsRunningOnChromeOS()) - tpm_token_state_ = TPM_DISABLED; - - // Treat TPM as disabled for guest users since they do not store certs. - if (LoginState::Get()->IsGuestSessionUser()) + if (!IsTPMLoadingEnabled()) tpm_token_state_ = TPM_DISABLED; ContinueTokenInitialization(); @@ -240,7 +243,7 @@ void TPMTokenLoader::OnTpmIsEnabled(DBusMethodCallStatus call_status, } void TPMTokenLoader::OnPkcs11IsTpmTokenReady(DBusMethodCallStatus call_status, - bool is_tpm_token_ready) { + bool is_tpm_token_ready) { VLOG(1) << "OnPkcs11IsTpmTokenReady: " << is_tpm_token_ready; if (call_status == DBUS_METHOD_CALL_FAILURE || !is_tpm_token_ready) { @@ -281,7 +284,15 @@ void TPMTokenLoader::OnTPMTokenInitialized(bool success) { } void TPMTokenLoader::NotifyTPMTokenReady() { - FOR_EACH_OBSERVER(Observer, observers_, OnTPMTokenReady()); + 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); + } + tpm_ready_callback_list_.clear(); } void TPMTokenLoader::LoggedInStateChanged() { diff --git a/chromeos/tpm_token_loader.h b/chromeos/tpm_token_loader.h index 16e20ef..33fffa2 100644 --- a/chromeos/tpm_token_loader.h +++ b/chromeos/tpm_token_loader.h @@ -32,16 +32,15 @@ namespace chromeos { // See CryptohomeClient::OnPkcs11GetTpmTokenInfo. class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { public: - class Observer { - public: - // Called when the TPM token initialization is done or the case where TPM - // should stay disabled is detected (e.g. on guest login). - virtual void OnTPMTokenReady() = 0; - - protected: - virtual ~Observer() {} + enum TPMTokenStatus { + TPM_TOKEN_STATUS_UNDETERMINED, + TPM_TOKEN_STATUS_ENABLED, + TPM_TOKEN_STATUS_DISABLED }; + typedef base::Callback<void(bool)> TPMReadyCallback; + typedef std::vector<TPMReadyCallback> TPMReadyCallbackList; + // Sets the global instance. Must be called before any calls to Get(). // The global instance will immediately start observing |LoginState|. static void Initialize(); @@ -65,11 +64,9 @@ class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { void SetCryptoTaskRunner( const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner); - void AddObserver(TPMTokenLoader::Observer* observer); - void RemoveObserver(TPMTokenLoader::Observer* observer); - - // Checks if the TPM token in ready to be used. - bool IsTPMTokenReady() const; + // 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); std::string tpm_user_pin() const { return tpm_user_pin_; } @@ -77,6 +74,8 @@ class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { explicit TPMTokenLoader(bool for_test); virtual ~TPMTokenLoader(); + bool IsTPMLoadingEnabled() const; + // Starts tpm token initialization if the user is logged in and the crypto // task runner is set. void MaybeStartTokenInitialization(); @@ -106,7 +105,7 @@ class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer { bool initialized_for_test_; - ObserverList<Observer> observers_; + TPMReadyCallbackList tpm_ready_callback_list_; // The states are traversed in this order but some might get omitted or never // be left. |