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_
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"
22 class CryptohomeClient
;
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.
31 // Default constructor creates token info for disabled TPM.
36 std::string token_name
;
41 // Class for getting a user or the system TPM token info from cryptohome during
43 class CHROMEOS_EXPORT TPMTokenInfoGetter
{
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
65 void Start(const TPMTokenInfoCallback
& callback
);
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.
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.
94 // Cryptohome methods callbacks.
95 void OnTpmIsEnabled(DBusMethodCallStatus call_status
,
97 void OnPkcs11GetTpmTokenInfo(DBusMethodCallStatus call_status
,
98 const std::string
& token_name
,
99 const std::string
& user_pin
,
102 // The task runner used to run delayed tasks when retrying failed Cryptohome
104 scoped_refptr
<base::TaskRunner
> delayed_task_runner_
;
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_