| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/session_manager/core/session_manager.h" | 5 #include "components/session_manager/core/session_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "components/session_manager/core/session_manager_observer.h" |
| 11 #include "components/user_manager/user_manager.h" | 12 #include "components/user_manager/user_manager.h" |
| 12 | 13 |
| 13 namespace session_manager { | 14 namespace session_manager { |
| 14 | 15 |
| 15 // static | 16 // static |
| 16 SessionManager* SessionManager::instance = nullptr; | 17 SessionManager* SessionManager::instance = nullptr; |
| 17 | 18 |
| 18 SessionManager::SessionManager() { | 19 SessionManager::SessionManager() { |
| 19 DCHECK(!SessionManager::Get()); | 20 DCHECK(!SessionManager::Get()); |
| 20 SessionManager::SetInstance(this); | 21 SessionManager::SetInstance(this); |
| 21 } | 22 } |
| 22 | 23 |
| 23 SessionManager::~SessionManager() { | 24 SessionManager::~SessionManager() { |
| 24 DCHECK_EQ(instance, this); | 25 DCHECK_EQ(instance, this); |
| 25 SessionManager::SetInstance(nullptr); | 26 SessionManager::SetInstance(nullptr); |
| 26 } | 27 } |
| 27 | 28 |
| 28 // static | 29 // static |
| 29 SessionManager* SessionManager::Get() { | 30 SessionManager* SessionManager::Get() { |
| 30 return SessionManager::instance; | 31 return SessionManager::instance; |
| 31 } | 32 } |
| 32 | 33 |
| 33 void SessionManager::SetSessionState(SessionState state) { | 34 void SessionManager::SetSessionState(SessionState state) { |
| 35 if (session_state_ == state) |
| 36 return; |
| 37 |
| 34 VLOG(1) << "Changing session state to: " << static_cast<int>(state); | 38 VLOG(1) << "Changing session state to: " << static_cast<int>(state); |
| 35 | 39 |
| 36 if (session_state_ != state) { | 40 session_state_ = state; |
| 37 // TODO(nkostylev): Notify observers about the state change. | 41 for (auto& observer : observers_) |
| 38 // TODO(nkostylev): Add code to process session state change and probably | 42 observer.OnSessionStateChanged(); |
| 39 // replace delegate_ if needed. | |
| 40 session_state_ = state; | |
| 41 } | |
| 42 } | 43 } |
| 43 | 44 |
| 44 void SessionManager::CreateSession(const AccountId& user_account_id, | 45 void SessionManager::CreateSession(const AccountId& user_account_id, |
| 45 const std::string& user_id_hash) { | 46 const std::string& user_id_hash) { |
| 46 CreateSessionInternal(user_account_id, user_id_hash, | 47 CreateSessionInternal(user_account_id, user_id_hash, |
| 47 false /* browser_restart */); | 48 false /* browser_restart */); |
| 48 } | 49 } |
| 49 | 50 |
| 50 void SessionManager::CreateSessionForRestart(const AccountId& user_account_id, | 51 void SessionManager::CreateSessionForRestart(const AccountId& user_account_id, |
| 51 const std::string& user_id_hash) { | 52 const std::string& user_id_hash) { |
| 52 CreateSessionInternal(user_account_id, user_id_hash, | 53 CreateSessionInternal(user_account_id, user_id_hash, |
| 53 true /* browser_restart */); | 54 true /* browser_restart */); |
| 54 } | 55 } |
| 55 | 56 |
| 56 bool SessionManager::IsSessionStarted() const { | 57 bool SessionManager::IsSessionStarted() const { |
| 57 return session_started_; | 58 return session_started_; |
| 58 } | 59 } |
| 59 | 60 |
| 60 void SessionManager::SessionStarted() { | 61 void SessionManager::SessionStarted() { |
| 61 session_started_ = true; | 62 session_started_ = true; |
| 62 } | 63 } |
| 63 | 64 |
| 65 void SessionManager::AddObserver(SessionManagerObserver* observer) { |
| 66 observers_.AddObserver(observer); |
| 67 } |
| 68 |
| 69 void SessionManager::RemoveObserver(SessionManagerObserver* observer) { |
| 70 observers_.RemoveObserver(observer); |
| 71 } |
| 72 |
| 64 void SessionManager::NotifyUserLoggedIn(const AccountId& user_account_id, | 73 void SessionManager::NotifyUserLoggedIn(const AccountId& user_account_id, |
| 65 const std::string& user_id_hash, | 74 const std::string& user_id_hash, |
| 66 bool browser_restart) { | 75 bool browser_restart) { |
| 67 auto* user_manager = user_manager::UserManager::Get(); | 76 auto* user_manager = user_manager::UserManager::Get(); |
| 68 if (!user_manager) | 77 if (!user_manager) |
| 69 return; | 78 return; |
| 70 user_manager->UserLoggedIn(user_account_id, user_id_hash, browser_restart); | 79 user_manager->UserLoggedIn(user_account_id, user_id_hash, browser_restart); |
| 71 } | 80 } |
| 72 | 81 |
| 73 // static | 82 // static |
| 74 void SessionManager::SetInstance(SessionManager* session_manager) { | 83 void SessionManager::SetInstance(SessionManager* session_manager) { |
| 75 SessionManager::instance = session_manager; | 84 SessionManager::instance = session_manager; |
| 76 } | 85 } |
| 77 | 86 |
| 78 void SessionManager::CreateSessionInternal(const AccountId& user_account_id, | 87 void SessionManager::CreateSessionInternal(const AccountId& user_account_id, |
| 79 const std::string& user_id_hash, | 88 const std::string& user_id_hash, |
| 80 bool browser_restart) { | 89 bool browser_restart) { |
| 81 DCHECK(std::find_if(sessions_.begin(), sessions_.end(), | 90 DCHECK(std::find_if(sessions_.begin(), sessions_.end(), |
| 82 [user_account_id](const Session& session) { | 91 [user_account_id](const Session& session) { |
| 83 return session.user_account_id == user_account_id; | 92 return session.user_account_id == user_account_id; |
| 84 }) == sessions_.end()); | 93 }) == sessions_.end()); |
| 85 | 94 |
| 86 sessions_.push_back({next_id_++, user_account_id}); | 95 sessions_.push_back({next_id_++, user_account_id}); |
| 87 NotifyUserLoggedIn(user_account_id, user_id_hash, browser_restart); | 96 NotifyUserLoggedIn(user_account_id, user_id_hash, browser_restart); |
| 88 } | 97 } |
| 89 | 98 |
| 90 } // namespace session_manager | 99 } // namespace session_manager |
| OLD | NEW |