[MD settings] moving attached() code
[chromium-blink-merge.git] / chromeos / tpm / tpm_token_loader.h
blob6add4a600b25320bf9156b95935fcef38df0e623
1 // Copyright 2014 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.
5 #ifndef CHROMEOS_TPM_TPM_TOKEN_LOADER_H_
6 #define CHROMEOS_TPM_TPM_TOKEN_LOADER_H_
8 #include <string>
9 #include <vector>
11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/observer_list.h"
15 #include "base/threading/thread_checker.h"
16 #include "chromeos/chromeos_export.h"
17 #include "chromeos/login/login_state.h"
19 namespace base {
20 class SequencedTaskRunner;
23 namespace chromeos {
25 struct TPMTokenInfo;
26 class TPMTokenInfoGetter;
28 // This class is responsible for loading the TPM backed token for the system
29 // slot when the user logs in. It is expected to be constructed on the UI thread
30 // and public methods should all be called from the UI thread.
31 // When the TPM token is loaded, or if the TPM should stay disabled for the
32 // session, the observers are notified using |OnTPMTokenReady|.
33 // Note: This currently initializes the token with the hard coded default id 0.
34 // See CryptohomeClient::OnPkcs11GetTpmTokenInfo.
35 class CHROMEOS_EXPORT TPMTokenLoader : public LoginState::Observer {
36 public:
37 enum TPMTokenStatus {
38 TPM_TOKEN_STATUS_UNDETERMINED,
39 TPM_TOKEN_STATUS_ENABLED,
40 TPM_TOKEN_STATUS_DISABLED
43 typedef base::Callback<void(bool)> TPMReadyCallback;
44 typedef std::vector<TPMReadyCallback> TPMReadyCallbackList;
46 // Sets the global instance. Must be called before any calls to Get().
47 // The global instance will immediately start observing |LoginState|.
48 static void Initialize();
50 // Sets the global. stubbed out, instance. To be used in tests.
51 static void InitializeForTest();
53 // Destroys the global instance.
54 static void Shutdown();
56 // Gets the global instance. Initialize() must be called before this.
57 static TPMTokenLoader* Get();
59 // Returns true if the global instance has been initialized.
60 static bool IsInitialized();
62 // |crypto_task_runner| is the task runner that any synchronous crypto calls
63 // should be made from, e.g. in Chrome this is the IO thread. Must be called
64 // after the thread is started. When called, this will attempt to start TPM
65 // token loading.
66 void SetCryptoTaskRunner(
67 const scoped_refptr<base::SequencedTaskRunner>& crypto_task_runner);
69 // Starts loading TPM system token, if not yet started. It should be called
70 // if the system token has to be loaded before a user logs in. By default (if
71 // |EnsureStarted| is not called) system token loading will start when the
72 // login state changes to LOGGED_IN_ACTIVE.
73 void EnsureStarted();
75 // Checks if the TPM token is enabled. If the state is unknown, |callback|
76 // will be called back once the TPM state is known.
77 TPMTokenStatus IsTPMTokenEnabled(const TPMReadyCallback& callback);
79 std::string tpm_user_pin() const { return tpm_user_pin_; }
81 private:
82 explicit TPMTokenLoader(bool for_test);
83 ~TPMTokenLoader() override;
85 bool IsTPMLoadingEnabled() const;
87 // Starts tpm token initialization if the user is logged in and the crypto
88 // task runner is set.
89 void MaybeStartTokenInitialization();
91 // This is the cyclic chain of callbacks to initialize the TPM token.
92 void ContinueTokenInitialization();
93 void OnTPMTokenEnabledForNSS();
94 void OnGotTpmTokenInfo(const TPMTokenInfo& token_info);
95 void OnTPMTokenInitialized(bool success);
97 // Notifies observers that the TPM token is ready.
98 void NotifyTPMTokenReady();
100 // LoginState::Observer
101 void LoggedInStateChanged() override;
103 bool initialized_for_test_;
105 TPMReadyCallbackList tpm_ready_callback_list_;
107 // The states are traversed in this order but some might get omitted or never
108 // be left.
109 enum TPMTokenState {
110 TPM_STATE_UNKNOWN,
111 TPM_INITIALIZATION_STARTED,
112 TPM_TOKEN_ENABLED_FOR_NSS,
113 TPM_DISABLED,
114 TPM_TOKEN_INFO_RECEIVED,
115 TPM_TOKEN_INITIALIZED,
117 TPMTokenState tpm_token_state_;
119 scoped_ptr<TPMTokenInfoGetter> tpm_token_info_getter_;
121 // Cached TPM token info.
122 int tpm_token_slot_id_;
123 std::string tpm_user_pin_;
125 // Whether TPM system token loading may be started before user log in.
126 // This will be true iff |EnsureStarted| was called.
127 bool can_start_before_login_;
129 base::ThreadChecker thread_checker_;
131 // TaskRunner for crypto calls.
132 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_;
134 base::WeakPtrFactory<TPMTokenLoader> weak_factory_;
136 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader);
139 } // namespace chromeos
141 #endif // CHROMEOS_TPM_TPM_TOKEN_LOADER_H_