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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_STATUS_CONSUMER_H_
      6 #define CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_STATUS_CONSUMER_H_
      7 #pragma once
      8 
      9 #include <string>
     10 
     11 #include "base/logging.h"
     12 #include "chrome/common/net/gaia/gaia_auth_consumer.h"
     13 #include "chrome/common/net/gaia/google_service_auth_error.h"
     14 #include "net/base/net_errors.h"
     15 
     16 namespace chromeos {
     17 
     18 class LoginFailure {
     19  public:
     20   enum FailureReason {
     21     NONE,
     22     COULD_NOT_MOUNT_CRYPTOHOME,
     23     COULD_NOT_MOUNT_TMPFS,
     24     COULD_NOT_UNMOUNT_CRYPTOHOME,
     25     DATA_REMOVAL_FAILED,  // Could not destroy your old data
     26     LOGIN_TIMED_OUT,
     27     UNLOCK_FAILED,
     28     NETWORK_AUTH_FAILED,  // Could not authenticate against Google
     29     NUM_FAILURE_REASONS,  // This has to be the last item.
     30   };
     31 
     32   explicit LoginFailure(FailureReason reason)
     33       : reason_(reason),
     34         error_(GoogleServiceAuthError::NONE) {
     35     DCHECK(reason != NETWORK_AUTH_FAILED);
     36   }
     37 
     38   inline bool operator==(const LoginFailure &b) const {
     39     if (reason_ != b.reason_) {
     40       return false;
     41     }
     42     if (reason_ == NETWORK_AUTH_FAILED) {
     43       return error_ == b.error_;
     44     }
     45     return true;
     46   }
     47 
     48   static LoginFailure FromNetworkAuthFailure(
     49       const GoogleServiceAuthError& error) {
     50     return LoginFailure(NETWORK_AUTH_FAILED, error);
     51   }
     52 
     53   static LoginFailure None() {
     54     return LoginFailure(NONE);
     55   }
     56 
     57   const std::string GetErrorString() const {
     58     switch (reason_) {
     59       case DATA_REMOVAL_FAILED:
     60         return "Could not destroy your old data.";
     61       case COULD_NOT_MOUNT_CRYPTOHOME:
     62         return "Could not mount cryptohome.";
     63       case COULD_NOT_UNMOUNT_CRYPTOHOME:
     64         return "Could not mount cryptohome.";
     65       case COULD_NOT_MOUNT_TMPFS:
     66         return "Could not mount tmpfs.";
     67       case LOGIN_TIMED_OUT:
     68         return "Login timed out. Please try again.";
     69       case UNLOCK_FAILED:
     70         return "Unlock failed.";
     71       case NETWORK_AUTH_FAILED:
     72         if (error_.state() == GoogleServiceAuthError::CONNECTION_FAILED) {
     73           return net::ErrorToString(error_.network_error());
     74         }
     75         return "Google authentication failed.";
     76       default:
     77         NOTREACHED();
     78         return std::string();
     79     }
     80   }
     81 
     82   const GoogleServiceAuthError& error() const { return error_; }
     83   const FailureReason& reason() const { return reason_; }
     84 
     85  private:
     86   LoginFailure(FailureReason reason, GoogleServiceAuthError error)
     87       : reason_(reason),
     88         error_(error) {
     89   }
     90 
     91   FailureReason reason_;
     92   GoogleServiceAuthError error_;
     93 };
     94 
     95 // An interface that defines the callbacks for objects that the
     96 // Authenticator class will call to report the success/failure of
     97 // authentication for Chromium OS.
     98 class LoginStatusConsumer {
     99  public:
    100   virtual ~LoginStatusConsumer() {}
    101   // The current login attempt has ended in failure, with error |error|.
    102   virtual void OnLoginFailure(const LoginFailure& error) = 0;
    103   // The current login attempt has succeeded for
    104   // |username|/|password|, returning |credentials|.  If
    105   // |pending_requests| is false, we're totally done.  If it's true,
    106   // we will still have some more results to report later.
    107   virtual void OnLoginSuccess(
    108       const std::string& username,
    109       const std::string& password,
    110       const GaiaAuthConsumer::ClientLoginResult& credentials,
    111       bool pending_requests) = 0;
    112   // The current guest login attempt has succeeded.
    113   virtual void OnOffTheRecordLoginSuccess() {}
    114   // The same password didn't work both online and offline.
    115   virtual void OnPasswordChangeDetected(
    116       const GaiaAuthConsumer::ClientLoginResult& credentials) {
    117     NOTREACHED();
    118   };
    119 };
    120 
    121 }  // namespace chromeos
    122 
    123 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_STATUS_CONSUMER_H_
    124