session_manager.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifndef COMPONENTS_SESSION_MANAGER_CORE_SESSION_MANAGER_H_
  5. #define COMPONENTS_SESSION_MANAGER_CORE_SESSION_MANAGER_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/observer_list.h"
  9. #include "components/session_manager/session_manager_export.h"
  10. #include "components/session_manager/session_manager_types.h"
  11. class AccountId;
  12. namespace session_manager {
  13. class SessionManagerObserver;
  14. class SESSION_EXPORT SessionManager {
  15. public:
  16. SessionManager();
  17. SessionManager(const SessionManager&) = delete;
  18. SessionManager& operator=(const SessionManager&) = delete;
  19. virtual ~SessionManager();
  20. // Returns current SessionManager instance and NULL if it hasn't been
  21. // initialized yet.
  22. static SessionManager* Get();
  23. void SetSessionState(SessionState state);
  24. // Creates a session for the given user. The first one is for regular cases
  25. // and the 2nd one is for the crash-and-restart case.
  26. void CreateSession(const AccountId& user_account_id,
  27. const std::string& user_id_hash,
  28. bool is_child);
  29. void CreateSessionForRestart(const AccountId& user_account_id,
  30. const std::string& user_id_hash);
  31. // Returns true if we're logged in and browser has been started i.e.
  32. // browser_creator.LaunchBrowser(...) was called after sign in
  33. // or restart after crash.
  34. bool IsSessionStarted() const;
  35. // Called when browser session is started i.e. after
  36. // browser_creator.LaunchBrowser(...) was called after user sign in.
  37. // When user is at the image screen IsUserLoggedIn() will return true
  38. // but IsSessionStarted() will return false. During the kiosk splash screen,
  39. // we perform additional initialization after the user is logged in but
  40. // before the session has been started.
  41. virtual void SessionStarted();
  42. // Returns true if the session for the given user was started.
  43. bool HasSessionForAccountId(const AccountId& user_account_id) const;
  44. // Convenience wrapps of session state.
  45. bool IsInSecondaryLoginScreen() const;
  46. bool IsScreenLocked() const;
  47. bool IsUserSessionBlocked() const;
  48. void AddObserver(SessionManagerObserver* observer);
  49. void RemoveObserver(SessionManagerObserver* observer);
  50. // Various helpers to notify observers.
  51. void NotifyUserProfileLoaded(const AccountId& account_id);
  52. void NotifyNetworkErrorScreenShown();
  53. void NotifyLoginOrLockScreenVisible();
  54. SessionState session_state() const { return session_state_; }
  55. const std::vector<Session>& sessions() const { return sessions_; }
  56. bool login_or_lock_screen_shown_for_test() const {
  57. return login_or_lock_screen_shown_for_test_;
  58. }
  59. protected:
  60. // Notifies UserManager about a user signs in when creating a user session.
  61. virtual void NotifyUserLoggedIn(const AccountId& user_account_id,
  62. const std::string& user_id_hash,
  63. bool browser_restart,
  64. bool is_child);
  65. // Sets SessionManager instance.
  66. static void SetInstance(SessionManager* session_manager);
  67. private:
  68. void CreateSessionInternal(const AccountId& user_account_id,
  69. const std::string& user_id_hash,
  70. bool browser_restart,
  71. bool is_child);
  72. // Pointer to the existing SessionManager instance (if any).
  73. // Set in ctor, reset in dtor. Not owned since specific implementation of
  74. // SessionManager should decide on its own appropriate owner of SessionManager
  75. // instance. For src/chrome implementation such place is
  76. // g_browser_process->platform_part().
  77. static SessionManager* instance;
  78. SessionState session_state_ = SessionState::UNKNOWN;
  79. // True if SessionStarted() has been called.
  80. bool session_started_ = false;
  81. // True if `NotifyLoginOrLockScreenVisible()` has been called. Used by test
  82. // classes to determine whether they should observe the session manager, as
  83. // the session manager may not be available when the test object is created.
  84. bool login_or_lock_screen_shown_for_test_ = false;
  85. // Id of the primary session, i.e. the first user session.
  86. static const SessionId kPrimarySessionId = 1;
  87. // ID assigned to the next session.
  88. SessionId next_id_ = kPrimarySessionId;
  89. // Keeps track of user sessions.
  90. std::vector<Session> sessions_;
  91. base::ObserverList<SessionManagerObserver> observers_;
  92. };
  93. } // namespace session_manager
  94. #endif // COMPONENTS_SESSION_MANAGER_CORE_SESSION_MANAGER_H_