Home | History | Annotate | Download | only in login
      1 // Copyright (c) 2012 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 
      8 #include <string>
      9 
     10 #include "base/logging.h"
     11 #include "google_apis/gaia/gaia_auth_consumer.h"
     12 #include "google_apis/gaia/google_service_auth_error.h"
     13 #include "net/base/net_errors.h"
     14 
     15 namespace chromeos {
     16 
     17 struct UserContext;
     18 
     19 class LoginFailure {
     20  public:
     21   enum FailureReason {
     22     NONE,
     23     COULD_NOT_MOUNT_CRYPTOHOME,
     24     COULD_NOT_MOUNT_TMPFS,
     25     COULD_NOT_UNMOUNT_CRYPTOHOME,
     26     DATA_REMOVAL_FAILED,    // Could not destroy your old data
     27     LOGIN_TIMED_OUT,
     28     UNLOCK_FAILED,
     29     NETWORK_AUTH_FAILED,    // Could not authenticate against Google
     30     OWNER_REQUIRED,         // Only the device owner can log-in.
     31     WHITELIST_CHECK_FAILED, // Login attempt blocked by whitelist. This value is
     32                             // synthesized by the ExistingUserController and
     33                             // passed to the login_status_consumer_ in tests
     34                             // only. It is never generated or seen by any of the
     35                             // other authenticator classes.
     36     TPM_ERROR,              // Critical TPM error encountered.
     37     NUM_FAILURE_REASONS,    // This has to be the last item.
     38   };
     39 
     40   explicit LoginFailure(FailureReason reason)
     41       : reason_(reason),
     42         error_(GoogleServiceAuthError::NONE) {
     43     DCHECK(reason != NETWORK_AUTH_FAILED);
     44   }
     45 
     46   inline bool operator==(const LoginFailure &b) const {
     47     if (reason_ != b.reason_) {
     48       return false;
     49     }
     50     if (reason_ == NETWORK_AUTH_FAILED) {
     51       return error_ == b.error_;
     52     }
     53     return true;
     54   }
     55 
     56   static LoginFailure FromNetworkAuthFailure(
     57       const GoogleServiceAuthError& error) {
     58     return LoginFailure(NETWORK_AUTH_FAILED, error);
     59   }
     60 
     61   static LoginFailure LoginFailureNone() {
     62     return LoginFailure(NONE);
     63   }
     64 
     65   const std::string GetErrorString() const {
     66     switch (reason_) {
     67       case DATA_REMOVAL_FAILED:
     68         return "Could not destroy your old data.";
     69       case COULD_NOT_MOUNT_CRYPTOHOME:
     70         return "Could not mount cryptohome.";
     71       case COULD_NOT_UNMOUNT_CRYPTOHOME:
     72         return "Could not unmount cryptohome.";
     73       case COULD_NOT_MOUNT_TMPFS:
     74         return "Could not mount tmpfs.";
     75       case LOGIN_TIMED_OUT:
     76         return "Login timed out. Please try again.";
     77       case UNLOCK_FAILED:
     78         return "Unlock failed.";
     79       case NETWORK_AUTH_FAILED:
     80         if (error_.state() == GoogleServiceAuthError::CONNECTION_FAILED) {
     81           return net::ErrorToString(error_.network_error());
     82         }
     83         return "Google authentication failed.";
     84       case OWNER_REQUIRED:
     85         return "Login is restricted to the owner's account only.";
     86       case WHITELIST_CHECK_FAILED:
     87         return "Login attempt blocked by whitelist.";
     88       default:
     89         NOTREACHED();
     90         return std::string();
     91     }
     92   }
     93 
     94   const GoogleServiceAuthError& error() const { return error_; }
     95   const FailureReason& reason() const { return reason_; }
     96 
     97  private:
     98   LoginFailure(FailureReason reason, GoogleServiceAuthError error)
     99       : reason_(reason),
    100         error_(error) {
    101   }
    102 
    103   FailureReason reason_;
    104   GoogleServiceAuthError error_;
    105 };
    106 
    107 // An interface that defines the callbacks for objects that the
    108 // Authenticator class will call to report the success/failure of
    109 // authentication for Chromium OS.
    110 class LoginStatusConsumer {
    111  public:
    112   virtual ~LoginStatusConsumer() {}
    113   // The current login attempt has ended in failure, with error |error|.
    114   virtual void OnLoginFailure(const LoginFailure& error) = 0;
    115 
    116   // The current retail mode login attempt has succeeded.
    117   // Unless overridden for special processing, this should always call
    118   // OnLoginSuccess with the magic |kRetailModeUserEMail| constant.
    119   virtual void OnRetailModeLoginSuccess(const UserContext& user_context);
    120   // The current login attempt has succeeded for |user_context|.
    121   // If |pending_requests| is false, we're totally done.
    122   // If it's true, we will still have some more results to report later.
    123   virtual void OnLoginSuccess(
    124       const UserContext& user_context,
    125       bool pending_requests,
    126       bool using_oauth) = 0;
    127   // The current guest login attempt has succeeded.
    128   virtual void OnOffTheRecordLoginSuccess() {}
    129   // The same password didn't work both online and offline.
    130   virtual void OnPasswordChangeDetected();
    131 };
    132 
    133 }  // namespace chromeos
    134 
    135 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_LOGIN_STATUS_CONSUMER_H_
    136