File: session_manager.h

package info (click to toggle)
chromium 138.0.7204.157-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,864 kB
  • sloc: cpp: 34,936,859; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,967; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (180 lines) | stat: -rw-r--r-- 7,147 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2014 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SESSION_MANAGER_CORE_SESSION_MANAGER_H_
#define COMPONENTS_SESSION_MANAGER_CORE_SESSION_MANAGER_H_

#include <string>
#include <vector>

#include "base/observer_list.h"
#include "base/scoped_observation.h"
#include "components/account_id/account_id.h"
#include "components/session_manager/session_manager_export.h"
#include "components/session_manager/session_manager_types.h"
#include "components/user_manager/user_manager.h"
#include "components/user_manager/user_type.h"

namespace session_manager {

class Session;
class SessionManagerObserver;

class SESSION_EXPORT SessionManager
    : public user_manager::UserManager::Observer {
 public:
  SessionManager();

  SessionManager(const SessionManager&) = delete;
  SessionManager& operator=(const SessionManager&) = delete;

  ~SessionManager() override;

  // Returns current SessionManager instance and NULL if it hasn't been
  // initialized yet.
  static SessionManager* Get();

  void SetSessionState(SessionState state);

  // Creates a session for the given user, hash and the type.
  // This is used for common session starts, and recovery from crash
  // for the secondary+ users. For the latter case, `has_active_session`
  // is set true.
  void CreateSession(const AccountId& user_account_id,
                     const std::string& username_hash,
                     bool new_user,
                     bool has_active_session);

  // Similar to above, creates a session for the given user and hash,
  // but for the primary user session on restarting chrome for crash recovering.
  // (Note: for non primary user sessions, CreateSession() is called with
  // `has_active_session == true`).
  // For this case, we expect there already is a registered User, so in general
  // the user type should be derived from the one. Though, there are edge
  // cases. Please find UserManager::CalculateUserType() for details.
  void CreateSessionForRestart(const AccountId& user_account_id,
                               const std::string& user_id_hash,
                               bool new_user);

  // Switches the active user session to the one specified by `account_id`.
  // The User has to be logged in already (i.e. CreateSession* needs to be
  // called in advance).
  void SwitchActiveSession(const AccountId& account_id);

  // Returns true if we're logged in and browser has been started i.e.
  // browser_creator.LaunchBrowser(...) was called after sign in
  // or restart after crash.
  bool IsSessionStarted() const;

  // Returns true if user session start up tasks are completed.
  bool IsUserSessionStartUpTaskCompleted() const;

  // Currently, UserManager is created after SessionManager.
  // However, UserManager is destroyed after SessionManager in the production.
  // Tests need to follow the same lifetime management.
  // TODO(b:332481586): Move this to the constructor by fixing initialization
  // order.
  virtual void OnUserManagerCreated(user_manager::UserManager* user_manager);

  // Called when browser session is started i.e. after
  // browser_creator.LaunchBrowser(...) was called after user sign in.
  // When user is at the image screen IsUserLoggedIn() will return true
  // but IsSessionStarted() will return false. During the kiosk splash screen,
  // we perform additional initialization after the user is logged in but
  // before the session has been started.
  virtual void SessionStarted();

  // Returns true if the session for the given user was started.
  bool HasSessionForAccountId(const AccountId& user_account_id) const;

  // Convenience wrapps of session state.
  bool IsInSecondaryLoginScreen() const;
  bool IsScreenLocked() const;
  bool IsUserSessionBlocked() const;

  void AddObserver(SessionManagerObserver* observer);
  void RemoveObserver(SessionManagerObserver* observer);

  void HandleUserSessionStartUpTaskCompleted();

  // user_manager::UserManager::Observer:
  void OnUserProfileCreated(const user_manager::User& user) override;

  // Various helpers to notify observers.
  void NotifyUserProfileLoaded(const AccountId& account_id);
  void NotifyNetworkErrorScreenShown();
  void NotifyLoginOrLockScreenVisible();
  void NotifyUnlockAttempt(const bool success, const UnlockType unlock_type);

  SessionState session_state() const { return session_state_; }
  const std::vector<std::unique_ptr<Session>>& sessions() const {
    return sessions_;
  }

  bool login_or_lock_screen_shown_for_test() const {
    return login_or_lock_screen_shown_for_test_;
  }

 protected:
  user_manager::UserManager* user_manager() { return user_manager_.get(); }

  // Called when a session is created. Make it possible for subclasses to inject
  // their more specific behavior at the timing.
  // TODO(crbug.com/278643115): Consolidate the subclass behaviors to this class
  // or extract into one of SessionManagerObserver's implementation.
  virtual void OnSessionCreated(bool browser_restart) {}

  // Sets SessionManager instance.
  static void SetInstance(SessionManager* session_manager);

 private:
  void CreateSessionInternal(const AccountId& user_account_id,
                             const std::string& username_hash,
                             bool new_user,
                             bool browser_restart);

  // Pointer to the existing SessionManager instance (if any).
  // Set in ctor, reset in dtor. Not owned since specific implementation of
  // SessionManager should decide on its own appropriate owner of SessionManager
  // instance. For src/chrome implementation such place is
  // g_browser_process->platform_part().
  static SessionManager* instance;

  raw_ptr<user_manager::UserManager> user_manager_ = nullptr;
  base::ScopedObservation<user_manager::UserManager,
                          user_manager::UserManager::Observer>
      user_manager_observation_{this};

  SessionState session_state_ = SessionState::UNKNOWN;

  // ID of the user just added to the session that needs to be activated
  // as soon as user's profile is loaded.
  AccountId pending_active_account_id_ = EmptyAccountId();

  // True if SessionStarted() has been called.
  bool session_started_ = false;

  // True if HandleUserSessionStartUpTaskCompleted() has been called.
  bool user_session_start_up_task_completed_ = false;

  // True if `NotifyLoginOrLockScreenVisible()` has been called. Used by test
  // classes to determine whether they should observe the session manager, as
  // the session manager may not be available when the test object is created.
  bool login_or_lock_screen_shown_for_test_ = false;

  // Id of the primary session, i.e. the first user session.
  static constexpr SessionId kPrimarySessionId = 1;

  // ID assigned to the next session.
  SessionId next_id_ = kPrimarySessionId;

  // Keeps track of user sessions.
  std::vector<std::unique_ptr<Session>> sessions_;

  base::ObserverList<SessionManagerObserver> observers_;
};

}  // namespace session_manager

#endif  // COMPONENTS_SESSION_MANAGER_CORE_SESSION_MANAGER_H_