1// Copyright (c) 2013 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 CHROMEOS_LOGIN_LOGIN_STATE_H_
6#define CHROMEOS_LOGIN_LOGIN_STATE_H_
7
8#include "base/basictypes.h"
9#include "base/observer_list.h"
10#include "chromeos/chromeos_export.h"
11
12namespace chromeos {
13
14// Tracks the login state of chrome, accessible to Ash and other chromeos code.
15class CHROMEOS_EXPORT LoginState {
16 public:
17  enum LoggedInState {
18    LOGGED_IN_OOBE,       // Out of box experience not completed
19    LOGGED_IN_NONE,       // Not logged in
20    LOGGED_IN_SAFE_MODE,  // Not logged in and login not allowed for non-owners
21    LOGGED_IN_ACTIVE      // A user has logged in
22  };
23
24  enum LoggedInUserType {
25    LOGGED_IN_USER_NONE,             // User is not logged in
26    LOGGED_IN_USER_REGULAR,          // A regular user is logged in
27    LOGGED_IN_USER_OWNER,            // The owner of the device is logged in
28    LOGGED_IN_USER_GUEST,            // A guest is logged in (i.e. incognito)
29    LOGGED_IN_USER_RETAIL_MODE,      // Is in retail mode
30    LOGGED_IN_USER_PUBLIC_ACCOUNT,   // A public account is logged in
31    LOGGED_IN_USER_LOCALLY_MANAGED,  // A locally managed user is logged in
32    LOGGED_IN_USER_KIOSK_APP         // Is in kiosk app mode
33  };
34
35  class Observer {
36   public:
37    // Called when either the login state or the logged in user type changes.
38    virtual void LoggedInStateChanged() = 0;
39
40   protected:
41    virtual ~Observer() {}
42  };
43
44  // Manage singleton instance.
45  static void Initialize();
46  static void Shutdown();
47  static LoginState* Get();
48  static bool IsInitialized();
49
50  // Add/remove observers.
51  void AddObserver(Observer* observer);
52  void RemoveObserver(Observer* observer);
53
54  // Sets the logged in state, user type, and primary user hash when the
55  // primary user initialy logs in. Also notifies observers.
56  void SetLoggedInStateAndPrimaryUser(
57      LoggedInState state,
58      LoggedInUserType type,
59      const std::string& primary_user_hash);
60
61  // Sets the logged in state and user type. Also notifies observers. Used
62  // in tests or situations where there is no primary user (e.g. from the
63  // login screen).
64  void SetLoggedInState(LoggedInState state, LoggedInUserType type);
65
66  // Gets the logged in user type.
67  LoggedInUserType GetLoggedInUserType() const;
68
69  // Returns true if a user is considered to be logged in.
70  bool IsUserLoggedIn() const;
71
72  // Returns true if |logged_in_state_| is safe mode (i.e. the user is not yet
73  // logged in, and only the owner will be allowed to log in).
74  bool IsInSafeMode() const;
75
76  // Returns true if logged in and is a guest, retail, or public user.
77  bool IsGuestUser() const;
78
79  // Returns true if logged in as a kiosk app.
80  bool IsKioskApp() const;
81
82  // Whether a network profile is created for the user.
83  bool UserHasNetworkProfile() const;
84
85  // Returns true if the user is an authenticated user (i.e. non public account)
86  bool IsUserAuthenticated() const;
87
88  // Returns true if the user is authenticated by logging into Google account
89  // (i.e., non public nor locally managed account).
90  bool IsUserGaiaAuthenticated() const;
91
92  void set_always_logged_in(bool always_logged_in) {
93    always_logged_in_ = always_logged_in;
94  }
95
96  const std::string& primary_user_hash() const { return primary_user_hash_; }
97
98 private:
99  LoginState();
100  virtual ~LoginState();
101
102  void NotifyObservers();
103
104  LoggedInState logged_in_state_;
105  LoggedInUserType logged_in_user_type_;
106  std::string primary_user_hash_;
107  ObserverList<Observer> observer_list_;
108
109  // If true, it always thinks the current status as logged in. Set to true by
110  // default running on a Linux desktop without flags and test cases. To test
111  // behaviors with a specific login state, call set_always_logged_in(false).
112  bool always_logged_in_;
113
114  DISALLOW_COPY_AND_ASSIGN(LoginState);
115};
116
117}  // namespace chromeos
118
119#endif  // CHROMEOS_LOGIN_LOGIN_STATE_H_
120