Update V8 to version 4.7.3.
[chromium-blink-merge.git] / chromeos / tpm / tpm_password_fetcher.cc
blobdac9c2a45d5fb144307d1c04e55dce811e26e732
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/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"
15 namespace chromeos {
17 namespace {
19 // Interval between TPM password checks.
20 const int kTpmCheckIntervalMs = 500;
22 } // namespace
24 TpmPasswordFetcher::TpmPasswordFetcher(TpmPasswordFetcherDelegate* delegate)
25 : delegate_(delegate), weak_factory_(this) {
26 DCHECK(delegate_);
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,
41 bool tpm_is_ready) {
42 if (call_status == DBUS_METHOD_CALL_SUCCESS && tpm_is_ready) {
43 DBusThreadManager::Get()->GetCryptohomeClient()->TpmGetPassword(base::Bind(
44 &TpmPasswordFetcher::OnTpmGetPassword, weak_factory_.GetWeakPtr()));
45 } else {
46 // Password hasn't been acquired, reschedule fetch.
47 RescheduleFetch();
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);
61 } else {
62 // Password hasn't been acquired, reschedule fetch.
63 RescheduleFetch();
67 void TpmPasswordFetcher::RescheduleFetch() {
68 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
69 FROM_HERE,
70 base::Bind(&TpmPasswordFetcher::Fetch, weak_factory_.GetWeakPtr()),
71 base::TimeDelta::FromMilliseconds(kTpmCheckIntervalMs));
74 } // namespace chromeos