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 #include "tpm_password_fetcher.h"
8 #include "base/compiler_specific.h"
9 #include "base/location.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "chromeos/dbus/cryptohome_client.h"
13 #include "chromeos/dbus/dbus_thread_manager.h"
19 // Interval between TPM password checks.
20 const int kTpmCheckIntervalMs
= 500;
24 TpmPasswordFetcher::TpmPasswordFetcher(TpmPasswordFetcherDelegate
* delegate
)
25 : delegate_(delegate
), weak_factory_(this) {
29 TpmPasswordFetcher::~TpmPasswordFetcher() {
32 void TpmPasswordFetcher::Fetch() {
33 // Since this method is also called directly.
34 weak_factory_
.InvalidateWeakPtrs();
36 DBusThreadManager::Get()->GetCryptohomeClient()->TpmIsReady(base::Bind(
37 &TpmPasswordFetcher::OnTpmIsReady
, weak_factory_
.GetWeakPtr()));
40 void TpmPasswordFetcher::OnTpmIsReady(DBusMethodCallStatus call_status
,
42 if (call_status
== DBUS_METHOD_CALL_SUCCESS
&& tpm_is_ready
) {
43 DBusThreadManager::Get()->GetCryptohomeClient()->TpmGetPassword(base::Bind(
44 &TpmPasswordFetcher::OnTpmGetPassword
, weak_factory_
.GetWeakPtr()));
46 // Password hasn't been acquired, reschedule fetch.
51 void TpmPasswordFetcher::OnTpmGetPassword(DBusMethodCallStatus call_status
,
52 const std::string
& password
) {
53 if (call_status
== DBUS_METHOD_CALL_SUCCESS
) {
54 if (password
.empty()) {
55 // For a fresh OOBE flow TPM is uninitialized,
56 // ownership process is started at the EULA screen,
57 // password is cleared after EULA is accepted.
58 LOG(ERROR
) << "TPM returned an empty password.";
60 delegate_
->OnPasswordFetched(password
);
62 // Password hasn't been acquired, reschedule fetch.
67 void TpmPasswordFetcher::RescheduleFetch() {
68 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
70 base::Bind(&TpmPasswordFetcher::Fetch
, weak_factory_
.GetWeakPtr()),
71 base::TimeDelta::FromMilliseconds(kTpmCheckIntervalMs
));
74 } // namespace chromeos