Home | History | Annotate | Download | only in chromeos
      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.
      4 
      5 #include "tpm_password_fetcher.h"
      6 
      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"
     12 
     13 namespace chromeos {
     14 
     15 namespace {
     16 
     17 // Interval between TPM password checks.
     18 const int kTpmCheckIntervalMs = 500;
     19 
     20 }  // namespace
     21 
     22 TpmPasswordFetcher::TpmPasswordFetcher(TpmPasswordFetcherDelegate* delegate)
     23     : delegate_(delegate), weak_factory_(this) {
     24   DCHECK(delegate_);
     25 }
     26 
     27 TpmPasswordFetcher::~TpmPasswordFetcher() {
     28 }
     29 
     30 void TpmPasswordFetcher::Fetch() {
     31   // Since this method is also called directly.
     32   weak_factory_.InvalidateWeakPtrs();
     33 
     34   DBusThreadManager::Get()->GetCryptohomeClient()->TpmIsReady(base::Bind(
     35       &TpmPasswordFetcher::OnTpmIsReady, weak_factory_.GetWeakPtr()));
     36 }
     37 
     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();
     46   }
     47 }
     48 
     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.";
     57     }
     58     delegate_->OnPasswordFetched(password);
     59   } else {
     60     // Password hasn't been acquired, reschedule fetch.
     61     RescheduleFetch();
     62   }
     63 }
     64 
     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));
     70 }
     71 
     72 }  // namespace chromeos
     73