gn vim: apply to *.gni also
[chromium-blink-merge.git] / chromeos / tpm_password_fetcher.cc
blob02c4abc084f32a73e2a359405972e6fcb088b9aa
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"
7 #include "base/bind.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"
13 namespace chromeos {
15 namespace {
17 // Interval between TPM password checks.
18 const int kTpmCheckIntervalMs = 500;
20 } // namespace
22 TpmPasswordFetcher::TpmPasswordFetcher(TpmPasswordFetcherDelegate* delegate)
23 : delegate_(delegate), weak_factory_(this) {
24 DCHECK(delegate_);
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,
39 bool tpm_is_ready) {
40 if (call_status == DBUS_METHOD_CALL_SUCCESS && tpm_is_ready) {
41 DBusThreadManager::Get()->GetCryptohomeClient()->TpmGetPassword(base::Bind(
42 &TpmPasswordFetcher::OnTpmGetPassword, weak_factory_.GetWeakPtr()));
43 } else {
44 // Password hasn't been acquired, reschedule fetch.
45 RescheduleFetch();
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);
59 } else {
60 // Password hasn't been acquired, reschedule fetch.
61 RescheduleFetch();
65 void TpmPasswordFetcher::RescheduleFetch() {
66 base::MessageLoop::current()->PostDelayedTask(
67 FROM_HERE,
68 base::Bind(&TpmPasswordFetcher::Fetch, weak_factory_.GetWeakPtr()),
69 base::TimeDelta::FromMilliseconds(kTpmCheckIntervalMs));
72 } // namespace chromeos