Add UMA to identify where a hotword trigger comes from.
[chromium-blink-merge.git] / chromeos / tpm_token_loader.h
blobbf6b3e366d16b5f38138b110faa40754b881c9f6
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_TOKEN_LOADER_H_
6 #define CHROMEOS_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 // Checks if the TPM token is enabled. If the state is unknown, |callback|
70 // will be called back once the TPM state is known.
71 TPMTokenStatus IsTPMTokenEnabled(const TPMReadyCallback& callback);
73 std::string tpm_user_pin() const { return tpm_user_pin_; }
75 private:
76 explicit TPMTokenLoader(bool for_test);
77 ~TPMTokenLoader() override;
79 bool IsTPMLoadingEnabled() const;
81 // Starts tpm token initialization if the user is logged in and the crypto
82 // task runner is set.
83 void MaybeStartTokenInitialization();
85 // This is the cyclic chain of callbacks to initialize the TPM token.
86 void ContinueTokenInitialization();
87 void OnTPMTokenEnabledForNSS();
88 void OnGotTpmTokenInfo(const TPMTokenInfo& token_info);
89 void OnTPMTokenInitialized(bool success);
91 // Notifies observers that the TPM token is ready.
92 void NotifyTPMTokenReady();
94 // LoginState::Observer
95 void LoggedInStateChanged() override;
97 bool initialized_for_test_;
99 TPMReadyCallbackList tpm_ready_callback_list_;
101 // The states are traversed in this order but some might get omitted or never
102 // be left.
103 enum TPMTokenState {
104 TPM_STATE_UNKNOWN,
105 TPM_INITIALIZATION_STARTED,
106 TPM_TOKEN_ENABLED_FOR_NSS,
107 TPM_DISABLED,
108 TPM_TOKEN_INFO_RECEIVED,
109 TPM_TOKEN_INITIALIZED,
111 TPMTokenState tpm_token_state_;
113 scoped_ptr<TPMTokenInfoGetter> tpm_token_info_getter_;
115 // Cached TPM token info.
116 int tpm_token_slot_id_;
117 std::string tpm_user_pin_;
119 base::ThreadChecker thread_checker_;
121 // TaskRunner for crypto calls.
122 scoped_refptr<base::SequencedTaskRunner> crypto_task_runner_;
124 base::WeakPtrFactory<TPMTokenLoader> weak_factory_;
126 DISALLOW_COPY_AND_ASSIGN(TPMTokenLoader);
129 } // namespace chromeos
131 #endif // CHROMEOS_TPM_TOKEN_LOADER_H_