Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2010 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 "chrome/browser/chromeos/login/cryptohome_op.h"
      6 
      7 #include <string>
      8 
      9 #include "chrome/browser/chromeos/cros/cros_library.h"
     10 #include "chrome/browser/chromeos/cros/cryptohome_library.h"
     11 #include "chrome/browser/chromeos/login/auth_attempt_state.h"
     12 #include "chrome/browser/chromeos/login/auth_attempt_state_resolver.h"
     13 #include "content/browser/browser_thread.h"
     14 
     15 namespace chromeos {
     16 
     17 CryptohomeOp::CryptohomeOp(AuthAttemptState* current_attempt,
     18                            AuthAttemptStateResolver* callback)
     19     : attempt_(current_attempt),
     20       resolver_(callback) {
     21   CHECK(chromeos::CrosLibrary::Get()->EnsureLoaded());
     22 }
     23 
     24 CryptohomeOp::~CryptohomeOp() {}
     25 
     26 void CryptohomeOp::OnComplete(bool success, int return_code) {
     27   BrowserThread::PostTask(
     28       BrowserThread::IO, FROM_HERE,
     29       NewRunnableMethod(this,
     30                         &CryptohomeOp::TriggerResolve,
     31                         success, return_code));
     32 }
     33 
     34 void CryptohomeOp::TriggerResolve(bool success, int return_code) {
     35   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
     36   attempt_->RecordCryptohomeStatus(success, return_code);
     37   resolver_->Resolve();
     38 }
     39 
     40 class MountAttempt : public CryptohomeOp {
     41  public:
     42   MountAttempt(AuthAttemptState* current_attempt,
     43                AuthAttemptStateResolver* callback,
     44                bool create_if_missing)
     45       : CryptohomeOp(current_attempt, callback),
     46         create_if_missing_(create_if_missing) {
     47   }
     48 
     49   virtual ~MountAttempt() {}
     50 
     51   bool Initiate() {
     52     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     53     CryptohomeLibrary* lib = CrosLibrary::Get()->GetCryptohomeLibrary();
     54     return lib->AsyncMount(attempt_->username,
     55                            attempt_->ascii_hash,
     56                            create_if_missing_,
     57                            this);
     58   }
     59 
     60  private:
     61   const bool create_if_missing_;
     62   DISALLOW_COPY_AND_ASSIGN(MountAttempt);
     63 };
     64 
     65 class MountGuestAttempt : public CryptohomeOp {
     66  public:
     67   MountGuestAttempt(AuthAttemptState* current_attempt,
     68                     AuthAttemptStateResolver* callback)
     69       : CryptohomeOp(current_attempt, callback) {
     70   }
     71 
     72   virtual ~MountGuestAttempt() {}
     73 
     74   bool Initiate() {
     75     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     76     CryptohomeLibrary* lib = CrosLibrary::Get()->GetCryptohomeLibrary();
     77     return lib->AsyncMountForBwsi(this);
     78   }
     79 
     80  private:
     81   DISALLOW_COPY_AND_ASSIGN(MountGuestAttempt);
     82 };
     83 
     84 class MigrateAttempt : public CryptohomeOp {
     85  public:
     86   // TODO(cmasone): get rid of passing_old_hash arg, as it's always true.
     87   MigrateAttempt(AuthAttemptState* current_attempt,
     88                  AuthAttemptStateResolver* callback,
     89                  bool passing_old_hash,
     90                  const std::string& hash)
     91       : CryptohomeOp(current_attempt, callback),
     92         is_old_hash_(passing_old_hash),
     93         hash_(hash) {
     94   }
     95 
     96   virtual ~MigrateAttempt() {}
     97 
     98   bool Initiate() {
     99     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    100     CryptohomeLibrary* lib = CrosLibrary::Get()->GetCryptohomeLibrary();
    101     if (is_old_hash_) {
    102       return lib->AsyncMigrateKey(attempt_->username,
    103                                   hash_,
    104                                   attempt_->ascii_hash,
    105                                   this);
    106     } else {
    107       return lib->AsyncMigrateKey(attempt_->username,
    108                                   attempt_->ascii_hash,
    109                                   hash_,
    110                                   this);
    111     }
    112   }
    113 
    114  private:
    115   const bool is_old_hash_;
    116   const std::string hash_;
    117 
    118   DISALLOW_COPY_AND_ASSIGN(MigrateAttempt);
    119 };
    120 
    121 class RemoveAttempt : public CryptohomeOp {
    122  public:
    123   RemoveAttempt(AuthAttemptState* current_attempt,
    124                 AuthAttemptStateResolver* callback)
    125       : CryptohomeOp(current_attempt, callback) {
    126   }
    127 
    128   virtual ~RemoveAttempt() {}
    129 
    130   bool Initiate() {
    131     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    132     CryptohomeLibrary* lib = CrosLibrary::Get()->GetCryptohomeLibrary();
    133     return lib->AsyncRemove(attempt_->username, this);
    134   }
    135 
    136  private:
    137   DISALLOW_COPY_AND_ASSIGN(RemoveAttempt);
    138 };
    139 
    140 class CheckKeyAttempt : public CryptohomeOp {
    141  public:
    142   CheckKeyAttempt(AuthAttemptState* current_attempt,
    143                   AuthAttemptStateResolver* callback)
    144       : CryptohomeOp(current_attempt, callback) {
    145   }
    146 
    147   virtual ~CheckKeyAttempt() {}
    148 
    149   bool Initiate() {
    150     DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
    151     CryptohomeLibrary* lib = CrosLibrary::Get()->GetCryptohomeLibrary();
    152     return lib->AsyncCheckKey(attempt_->username, attempt_->ascii_hash, this);
    153   }
    154 
    155  private:
    156   DISALLOW_COPY_AND_ASSIGN(CheckKeyAttempt);
    157 };
    158 
    159 // static
    160 CryptohomeOp* CryptohomeOp::CreateMountAttempt(
    161     AuthAttemptState* current_attempt,
    162     AuthAttemptStateResolver* callback,
    163     bool create_if_missing) {
    164   return new MountAttempt(current_attempt, callback, create_if_missing);
    165 }
    166 
    167 // static
    168 CryptohomeOp* CryptohomeOp::CreateMountGuestAttempt(
    169       AuthAttemptState* current_attempt,
    170       AuthAttemptStateResolver* callback) {
    171   return new MountGuestAttempt(current_attempt, callback);
    172 }
    173 
    174 // static
    175 CryptohomeOp* CryptohomeOp::CreateMigrateAttempt(
    176     AuthAttemptState* current_attempt,
    177     AuthAttemptStateResolver* callback,
    178     bool passing_old_hash,
    179     const std::string& hash) {
    180   return new MigrateAttempt(current_attempt, callback, passing_old_hash, hash);
    181 }
    182 
    183 // static
    184 CryptohomeOp* CryptohomeOp::CreateRemoveAttempt(
    185     AuthAttemptState* current_attempt,
    186     AuthAttemptStateResolver* callback) {
    187   return new RemoveAttempt(current_attempt, callback);
    188 }
    189 
    190 // static
    191 CryptohomeOp* CryptohomeOp::CreateCheckKeyAttempt(
    192     AuthAttemptState* current_attempt,
    193     AuthAttemptStateResolver* callback) {
    194 
    195   return new CheckKeyAttempt(current_attempt, callback);
    196 }
    197 
    198 }  // namespace chromeos
    199