• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef CHROME_BROWSER_CHROMEOS_LOGIN_AUTH_LOGIN_STATUS_CONSUMER_H_
6 #define CHROME_BROWSER_CHROMEOS_LOGIN_AUTH_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 class 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     USERNAME_HASH_FAILED,   // Could not get username hash.
38     NUM_FAILURE_REASONS,    // This has to be the last item.
39   };
40 
LoginFailure(FailureReason reason)41   explicit LoginFailure(FailureReason reason)
42       : reason_(reason),
43         error_(GoogleServiceAuthError::NONE) {
44     DCHECK(reason != NETWORK_AUTH_FAILED);
45   }
46 
47   inline bool operator==(const LoginFailure &b) const {
48     if (reason_ != b.reason_) {
49       return false;
50     }
51     if (reason_ == NETWORK_AUTH_FAILED) {
52       return error_ == b.error_;
53     }
54     return true;
55   }
56 
FromNetworkAuthFailure(const GoogleServiceAuthError & error)57   static LoginFailure FromNetworkAuthFailure(
58       const GoogleServiceAuthError& error) {
59     return LoginFailure(NETWORK_AUTH_FAILED, error);
60   }
61 
LoginFailureNone()62   static LoginFailure LoginFailureNone() {
63     return LoginFailure(NONE);
64   }
65 
GetErrorString()66   const std::string GetErrorString() const {
67     switch (reason_) {
68       case DATA_REMOVAL_FAILED:
69         return "Could not destroy your old data.";
70       case COULD_NOT_MOUNT_CRYPTOHOME:
71         return "Could not mount cryptohome.";
72       case COULD_NOT_UNMOUNT_CRYPTOHOME:
73         return "Could not unmount cryptohome.";
74       case COULD_NOT_MOUNT_TMPFS:
75         return "Could not mount tmpfs.";
76       case LOGIN_TIMED_OUT:
77         return "Login timed out. Please try again.";
78       case UNLOCK_FAILED:
79         return "Unlock failed.";
80       case NETWORK_AUTH_FAILED:
81         if (error_.state() == GoogleServiceAuthError::CONNECTION_FAILED) {
82           return net::ErrorToString(error_.network_error());
83         }
84         return "Google authentication failed.";
85       case OWNER_REQUIRED:
86         return "Login is restricted to the owner's account only.";
87       case WHITELIST_CHECK_FAILED:
88         return "Login attempt blocked by whitelist.";
89       default:
90         NOTREACHED();
91         return std::string();
92     }
93   }
94 
error()95   const GoogleServiceAuthError& error() const { return error_; }
reason()96   const FailureReason& reason() const { return reason_; }
97 
98  private:
LoginFailure(FailureReason reason,GoogleServiceAuthError error)99   LoginFailure(FailureReason reason, GoogleServiceAuthError error)
100       : reason_(reason),
101         error_(error) {
102   }
103 
104   FailureReason reason_;
105   GoogleServiceAuthError error_;
106 };
107 
108 // An interface that defines the callbacks for objects that the
109 // Authenticator class will call to report the success/failure of
110 // authentication for Chromium OS.
111 class LoginStatusConsumer {
112  public:
~LoginStatusConsumer()113   virtual ~LoginStatusConsumer() {}
114   // The current login attempt has ended in failure, with error |error|.
115   virtual void OnLoginFailure(const LoginFailure& error) = 0;
116 
117   // The current retail mode login attempt has succeeded.
118   // Unless overridden for special processing, this should always call
119   // OnLoginSuccess with the magic |kRetailModeUserEMail| constant.
120   virtual void OnRetailModeLoginSuccess(const UserContext& user_context);
121   // The current login attempt has succeeded for |user_context|.
122   virtual void OnLoginSuccess(const UserContext& user_context) = 0;
123   // The current guest login attempt has succeeded.
OnOffTheRecordLoginSuccess()124   virtual void OnOffTheRecordLoginSuccess() {}
125   // The same password didn't work both online and offline.
126   virtual void OnPasswordChangeDetected();
127 };
128 
129 }  // namespace chromeos
130 
131 #endif  // CHROME_BROWSER_CHROMEOS_LOGIN_AUTH_LOGIN_STATUS_CONSUMER_H_
132