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/message_loop/message_loop.h"
10 #include "chromeos/dbus/cryptohome_client.h"
11 #include "chromeos/dbus/dbus_thread_manager.h"
17 // Interval between TPM password checks.
18 const int kTpmCheckIntervalMs
= 500;
22 TpmPasswordFetcher::TpmPasswordFetcher(TpmPasswordFetcherDelegate
* delegate
)
23 : delegate_(delegate
), weak_factory_(this) {
27 TpmPasswordFetcher::~TpmPasswordFetcher() {
30 void TpmPasswordFetcher::Fetch() {
31 // Since this method is also called directly.
32 weak_factory_
.InvalidateWeakPtrs();
34 DBusThreadManager::Get()->GetCryptohomeClient()->TpmIsReady(base::Bind(
35 &TpmPasswordFetcher::OnTpmIsReady
, weak_factory_
.GetWeakPtr()));
38 void TpmPasswordFetcher::OnTpmIsReady(DBusMethodCallStatus call_status
,
40 if (call_status
== DBUS_METHOD_CALL_SUCCESS
&& tpm_is_ready
) {
41 DBusThreadManager::Get()->GetCryptohomeClient()->TpmGetPassword(base::Bind(
42 &TpmPasswordFetcher::OnTpmGetPassword
, weak_factory_
.GetWeakPtr()));
44 // Password hasn't been acquired, reschedule fetch.
49 void TpmPasswordFetcher::OnTpmGetPassword(DBusMethodCallStatus call_status
,
50 const std::string
& password
) {
51 if (call_status
== DBUS_METHOD_CALL_SUCCESS
) {
52 if (password
.empty()) {
53 // For a fresh OOBE flow TPM is uninitialized,
54 // ownership process is started at the EULA screen,
55 // password is cleared after EULA is accepted.
56 LOG(ERROR
) << "TPM returned an empty password.";
58 delegate_
->OnPasswordFetched(password
);
60 // Password hasn't been acquired, reschedule fetch.
65 void TpmPasswordFetcher::RescheduleFetch() {
66 base::MessageLoop::current()->PostDelayedTask(
68 base::Bind(&TpmPasswordFetcher::Fetch
, weak_factory_
.GetWeakPtr()),
69 base::TimeDelta::FromMilliseconds(kTpmCheckIntervalMs
));
72 } // namespace chromeos