session_controller_impl.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // Copyright 2016 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. #ifndef ASH_SESSION_SESSION_CONTROLLER_IMPL_H_
  5. #define ASH_SESSION_SESSION_CONTROLLER_IMPL_H_
  6. #include <stdint.h>
  7. #include <memory>
  8. #include <vector>
  9. #include "ash/ash_export.h"
  10. #include "ash/login_status.h"
  11. #include "ash/public/cpp/session/session_controller.h"
  12. #include "ash/public/cpp/session/session_types.h"
  13. #include "ash/session/session_activation_observer_holder.h"
  14. #include "base/callback.h"
  15. #include "base/memory/weak_ptr.h"
  16. #include "base/observer_list.h"
  17. #include "base/time/time.h"
  18. class AccountId;
  19. class PrefService;
  20. namespace ash {
  21. class FullscreenController;
  22. class SessionControllerClient;
  23. class SessionObserver;
  24. class TestSessionControllerClient;
  25. // Implements mojom::SessionController to cache session related info such as
  26. // session state, meta data about user sessions to support synchronous
  27. // queries for ash.
  28. class ASH_EXPORT SessionControllerImpl : public SessionController {
  29. public:
  30. using UserSessions = std::vector<std::unique_ptr<UserSession>>;
  31. SessionControllerImpl();
  32. SessionControllerImpl(const SessionControllerImpl&) = delete;
  33. SessionControllerImpl& operator=(const SessionControllerImpl&) = delete;
  34. ~SessionControllerImpl() override;
  35. base::TimeDelta session_length_limit() const { return session_length_limit_; }
  36. base::Time session_start_time() const { return session_start_time_; }
  37. bool session_state_change_in_progress() const {
  38. return session_state_change_in_progress_;
  39. }
  40. // Returns the number of signed in users. If 0 is returned, there is either
  41. // no session in progress or no active user.
  42. int NumberOfLoggedInUsers() const;
  43. // Returns the active account. If no users are logged in this returns an empty
  44. // AccountId.
  45. AccountId GetActiveAccountId() const;
  46. // Gets the policy of adding a user session to ash.
  47. AddUserSessionPolicy GetAddUserPolicy() const;
  48. // Returns |true| if the session has been fully started for the active user.
  49. // When a user becomes active, the profile and browser UI are not immediately
  50. // available. Only once this method starts returning |true| is the browser
  51. // startup complete and both profile and UI are fully available.
  52. bool IsActiveUserSessionStarted() const;
  53. // Returns true if the screen can be locked.
  54. bool CanLockScreen() const;
  55. // Returns true if the screen should be locked automatically when the screen
  56. // is turned off or the system is suspended.
  57. bool ShouldLockScreenAutomatically() const;
  58. // Returns true if the session is in a kiosk-like mode running a single app.
  59. bool IsRunningInAppMode() const;
  60. // Returns true if the current session is a demo session for Demo Mode.
  61. bool IsDemoSession() const;
  62. // Returns true if user session blocked by some overlying UI. It can be
  63. // login screen, lock screen or screen for adding users into multi-profile
  64. // session.
  65. bool IsUserSessionBlocked() const;
  66. // Convenience function that returns true if session state is LOGIN_SECONDARY.
  67. bool IsInSecondaryLoginScreen() const;
  68. // Returns true if the settings icon should be enabled in the system tray.
  69. bool ShouldEnableSettings() const;
  70. // Returns true if the notification tray should appear.
  71. bool ShouldShowNotificationTray() const;
  72. // Gets the ash session state.
  73. session_manager::SessionState GetSessionState() const;
  74. // Gets the user sessions in LRU order with the active session being first.
  75. const UserSessions& GetUserSessions() const;
  76. // Convenience helper to get the user session at a given index. Returns
  77. // nullptr if no user session is found for the index.
  78. const UserSession* GetUserSession(UserIndex index) const;
  79. // Convenience helper to get the user session with the given account id.
  80. // Returns nullptr if no user session is found for the account id.
  81. const UserSession* GetUserSessionByAccountId(
  82. const AccountId& account_id) const;
  83. // Gets the primary user session.
  84. const UserSession* GetPrimaryUserSession() const;
  85. // Returns true if the current user is a child account.
  86. bool IsUserChild() const;
  87. // Returns true if the current user is a public account.
  88. bool IsUserPublicAccount() const;
  89. // Returns the type of the current user, or empty if there is no current user
  90. // logged in.
  91. absl::optional<user_manager::UserType> GetUserType() const;
  92. // Returns true if the current user is the primary user in a multi-profile
  93. // scenario. This always return true if there is only one user logged in.
  94. bool IsUserPrimary() const;
  95. // Returns true if the current user has the profile newly created on the
  96. // device (i.e. first time login on the device).
  97. bool IsUserFirstLogin() const;
  98. // Returns true if the device is enterprise managed.
  99. bool IsEnterpriseManaged() const;
  100. // Returns true if should display managed icon for current session,
  101. // and false otherwise.
  102. bool ShouldDisplayManagedUI() const;
  103. // Locks the screen. The locking happens asynchronously.
  104. void LockScreen();
  105. // Hides the lock screen.
  106. void HideLockScreen();
  107. // Requests signing out all users, ending the current session.
  108. // NOTE: This should only be called from LockStateController, other callers
  109. // should use LockStateController::RequestSignOut() instead.
  110. void RequestSignOut();
  111. // Attempts to restart the chrome browser.
  112. void AttemptRestartChrome();
  113. // Switches to another active user with |account_id| (if that user has
  114. // already signed in).
  115. void SwitchActiveUser(const AccountId& account_id);
  116. // Switches the active user to the next or previous user, with the same
  117. // ordering as user sessions are created.
  118. void CycleActiveUser(CycleUserDirection direction);
  119. // Show the multi-profile login UI to add another user to this session.
  120. void ShowMultiProfileLogin();
  121. // Returns the PrefService used at the signin screen, which is tied to an
  122. // incognito profile in chrome and is valid until the browser exits.
  123. PrefService* GetSigninScreenPrefService() const;
  124. // Returns the PrefService for |account_id| or null if one does not exist.
  125. PrefService* GetUserPrefServiceForUser(const AccountId& account_id) const;
  126. // Returns the PrefService for the primary user or null if no user is signed
  127. // in or the PrefService connection hasn't been established.
  128. PrefService* GetPrimaryUserPrefService() const;
  129. // Returns the PrefService for the last active user that had one or null if no
  130. // PrefService connection has been successfully established.
  131. PrefService* GetLastActiveUserPrefService() const;
  132. // Before login returns the signin screen profile prefs. After login returns
  133. // the active user profile prefs. Returns null early during startup.
  134. PrefService* GetActivePrefService() const;
  135. // Returns the ash notion of login status.
  136. // NOTE: Prefer GetSessionState() in new code because the concept of
  137. // SessionState more closes matches the state in chrome.
  138. LoginStatus login_status() const { return login_status_; }
  139. // SessionController
  140. void SetClient(SessionControllerClient* client) override;
  141. void SetSessionInfo(const SessionInfo& info) override;
  142. void UpdateUserSession(const UserSession& user_session) override;
  143. void SetUserSessionOrder(
  144. const std::vector<uint32_t>& user_session_order) override;
  145. void PrepareForLock(PrepareForLockCallback callback) override;
  146. void StartLock(StartLockCallback callback) override;
  147. void NotifyChromeLockAnimationsComplete() override;
  148. void RunUnlockAnimation(RunUnlockAnimationCallback callback) override;
  149. void NotifyChromeTerminating() override;
  150. void SetSessionLengthLimit(base::TimeDelta length_limit,
  151. base::Time start_time) override;
  152. void CanSwitchActiveUser(CanSwitchActiveUserCallback callback) override;
  153. void ShowMultiprofilesIntroDialog(
  154. ShowMultiprofilesIntroDialogCallback callback) override;
  155. void ShowTeleportWarningDialog(
  156. ShowTeleportWarningDialogCallback callback) override;
  157. void ShowMultiprofilesSessionAbortedDialog(
  158. const std::string& user_email) override;
  159. void AddSessionActivationObserverForAccountId(
  160. const AccountId& account_id,
  161. SessionActivationObserver* observer) override;
  162. void RemoveSessionActivationObserverForAccountId(
  163. const AccountId& account_id,
  164. SessionActivationObserver* observer) override;
  165. void AddObserver(SessionObserver* observer) override;
  166. void RemoveObserver(SessionObserver* observer) override;
  167. bool IsScreenLocked() const override;
  168. // Test helpers.
  169. void ClearUserSessionsForTest();
  170. private:
  171. friend class TestSessionControllerClient;
  172. // Marks the session as a demo session for Demo Mode.
  173. void SetIsDemoSession();
  174. void SetSessionState(session_manager::SessionState state);
  175. void AddUserSession(const UserSession& user_session);
  176. // Calculate login status based on session state and active user session.
  177. LoginStatus CalculateLoginStatus() const;
  178. // Helper that returns login status when the session state is ACTIVE.
  179. LoginStatus CalculateLoginStatusForActiveSession() const;
  180. // Update the |login_status_| and notify observers.
  181. void UpdateLoginStatus();
  182. // Used as lock screen displayed callback of LockStateController and invoked
  183. // when post lock animation finishes and ash is fully locked. It would then
  184. // run |start_lock_callback_| to indicate ash is locked successfully.
  185. void OnLockAnimationFinished();
  186. // Ensure that the sign-in screen PrefService is obtained.
  187. void EnsureSigninScreenPrefService();
  188. void OnSigninScreenPrefServiceInitialized(PrefService* pref_service);
  189. void OnProfilePrefServiceInitialized(const AccountId& account_id,
  190. PrefService* pref_service);
  191. // Notifies observers that the active user pref service changed only if the
  192. // signin profile pref service has been connected and observers were notified
  193. // via OnSigninScreenPrefServiceInitialized(). Otherwise, defer the
  194. // notification until that happens.
  195. void MaybeNotifyOnActiveUserPrefServiceChanged();
  196. // Called when IsUserSessionBlocked() becomes true. If there isn't an active
  197. // window, tries to activate one.
  198. void EnsureActiveWindowAfterUnblockingUserSession();
  199. // Client interface to session manager code (chrome).
  200. SessionControllerClient* client_ = nullptr;
  201. // Cached session info.
  202. bool can_lock_ = false;
  203. bool should_lock_screen_automatically_ = false;
  204. bool is_running_in_app_mode_ = false;
  205. bool is_demo_session_ = false;
  206. AddUserSessionPolicy add_user_session_policy_ = AddUserSessionPolicy::ALLOWED;
  207. session_manager::SessionState state_ = session_manager::SessionState::UNKNOWN;
  208. // Cached user session info sorted by the order from SetUserSessionOrder.
  209. // Currently the session manager code (chrome) sets a LRU order with the
  210. // active session being the first.
  211. UserSessions user_sessions_;
  212. // The user session id of the current active user session. User session id
  213. // is managed by session manager code, starting at 1. 0u is an invalid id
  214. // to detect first active user session.
  215. uint32_t active_session_id_ = 0u;
  216. // The user session id of the primary user session. The primary user session
  217. // is the very first user session of the current ash session.
  218. uint32_t primary_session_id_ = 0u;
  219. // Last known login status. Used to track login status changes.
  220. LoginStatus login_status_ = LoginStatus::NOT_LOGGED_IN;
  221. // Whether unlocking is in progress. The flag is set when the pre-unlock
  222. // animation starts and reset when session state is no longer LOCKED.
  223. bool is_unlocking_ = false;
  224. // Pending callback for the StartLock request.
  225. base::OnceCallback<void(bool)> start_lock_callback_;
  226. // The session length limit; set to zero if there is no limit.
  227. base::TimeDelta session_length_limit_;
  228. // The session start time, set at login or on the first user activity; set to
  229. // null if there is no session length limit. This value is also stored in a
  230. // pref in case of a crash during the session.
  231. base::Time session_start_time_;
  232. // Set to true if the active user's pref is received before the signin prefs.
  233. // This is so that we can guarantee that observers are notified with
  234. // OnActiveUserPrefServiceChanged() after
  235. // OnSigninScreenPrefServiceInitialized().
  236. bool on_active_user_prefs_changed_notify_deferred_ = false;
  237. base::ObserverList<SessionObserver> observers_;
  238. SessionActivationObserverHolder session_activation_observer_holder_;
  239. bool signin_screen_prefs_obtained_ = false;
  240. PrefService* last_active_user_prefs_ = nullptr;
  241. std::unique_ptr<FullscreenController> fullscreen_controller_;
  242. // Indicate if the session state is being changed.
  243. bool session_state_change_in_progress_ = false;
  244. base::WeakPtrFactory<SessionControllerImpl> weak_ptr_factory_{this};
  245. };
  246. } // namespace ash
  247. #endif // ASH_SESSION_SESSION_CONTROLLER_IMPL_H_