Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chromeos / tpm / tpm_token_info_getter.h
blobf1f39a6e91ba33246b70e8073ca3b15865539f0f
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_INFO_GETTER_H_
6 #define CHROMEOS_TPM_TPM_TOKEN_INFO_GETTER_H_
8 #include <string>
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/memory/weak_ptr.h"
13 #include "base/time/time.h"
14 #include "chromeos/chromeos_export.h"
15 #include "chromeos/dbus/dbus_method_call_status.h"
17 namespace base {
18 class TaskRunner;
21 namespace chromeos {
22 class CryptohomeClient;
25 namespace chromeos {
27 // Information retrieved from cryptohome by TPMTokenInfoGetter.
28 // For invalid token |token_name| and |user_pin| will be empty, while
29 // |token_slot_id| will be set to -1.
30 struct TPMTokenInfo {
31 // Default constructor creates token info for disabled TPM.
32 TPMTokenInfo();
33 ~TPMTokenInfo();
35 bool tpm_is_enabled;
36 std::string token_name;
37 std::string user_pin;
38 int token_slot_id;
41 // Class for getting a user or the system TPM token info from cryptohome during
42 // TPM token loading.
43 class CHROMEOS_EXPORT TPMTokenInfoGetter {
44 public:
45 using TPMTokenInfoCallback = base::Callback<void(const TPMTokenInfo& info)>;
47 // Factory method for TPMTokenInfoGetter for a user token.
48 static scoped_ptr<TPMTokenInfoGetter> CreateForUserToken(
49 const std::string& user_id,
50 CryptohomeClient* cryptohome_client,
51 const scoped_refptr<base::TaskRunner>& delayed_task_runner);
53 // Factory method for TPMTokenGetter for the system token.
54 static scoped_ptr<TPMTokenInfoGetter> CreateForSystemToken(
55 CryptohomeClient* cryptohome_client,
56 const scoped_refptr<base::TaskRunner>& delayed_task_runner);
58 ~TPMTokenInfoGetter();
60 // Starts getting TPM token info. Should be called at most once.
61 // |callback| will be called when all the info is fetched.
62 // The object may get deleted before |callback| is called, which is equivalent
63 // to cancelling the info getting (in which case |callback| will never get
64 // called).
65 void Start(const TPMTokenInfoCallback& callback);
67 private:
68 enum Type {
69 TYPE_SYSTEM,
70 TYPE_USER
73 enum State {
74 STATE_INITIAL,
75 STATE_STARTED,
76 STATE_TPM_ENABLED,
77 STATE_DONE
80 TPMTokenInfoGetter(
81 Type type,
82 const std::string& user_id,
83 CryptohomeClient* cryptohome_client,
84 const scoped_refptr<base::TaskRunner>& delayed_task_runner);
86 // Continues TPM token info getting procedure by starting the task associated
87 // with the current TPMTokenInfoGetter state.
88 void Continue();
90 // If token initialization step fails (e.g. if tpm token is not yet ready)
91 // schedules the initialization step retry attempt after a timeout.
92 void RetryLater();
94 // Cryptohome methods callbacks.
95 void OnTpmIsEnabled(DBusMethodCallStatus call_status,
96 bool tpm_is_enabled);
97 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status,
98 const std::string& token_name,
99 const std::string& user_pin,
100 int token_slot_id);
102 // The task runner used to run delayed tasks when retrying failed Cryptohome
103 // calls.
104 scoped_refptr<base::TaskRunner> delayed_task_runner_;
106 Type type_;
107 State state_;
109 // The user id associated with the TPMTokenInfoGetter. Empty for system token.
110 std::string user_id_;
112 TPMTokenInfoCallback callback_;
114 // The current request delay before the next attempt to initialize the
115 // TPM. Will be adapted after each attempt.
116 base::TimeDelta tpm_request_delay_;
118 CryptohomeClient* cryptohome_client_;
120 base::WeakPtrFactory<TPMTokenInfoGetter> weak_factory_;
122 DISALLOW_COPY_AND_ASSIGN(TPMTokenInfoGetter);
125 } // namespace chromeos
127 #endif // CHROMEOS_TPM_TPM_TOKEN_INFO_GETTER_H_