| 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" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 DCHECK_EQ(instance, this); | 24 DCHECK_EQ(instance, this); |
| 25 SessionManager::SetInstance(nullptr); | 25 SessionManager::SetInstance(nullptr); |
| 26 } | 26 } |
| 27 | 27 |
| 28 // static | 28 // static |
| 29 SessionManager* SessionManager::Get() { | 29 SessionManager* SessionManager::Get() { |
| 30 return SessionManager::instance; | 30 return SessionManager::instance; |
| 31 } | 31 } |
| 32 | 32 |
| 33 void SessionManager::SetSessionState(SessionState state) { | 33 void SessionManager::SetSessionState(SessionState state) { |
| 34 if (session_state_ == state) |
| 35 return; |
| 36 |
| 34 VLOG(1) << "Changing session state to: " << static_cast<int>(state); | 37 VLOG(1) << "Changing session state to: " << static_cast<int>(state); |
| 35 | 38 |
| 36 if (session_state_ != state) { | 39 session_state_ = state; |
| 37 // TODO(nkostylev): Notify observers about the state change. | 40 for (auto* observer : observers_) |
| 38 // TODO(nkostylev): Add code to process session state change and probably | 41 observer->OnSessionStateChanged(); |
| 39 // replace delegate_ if needed. | |
| 40 session_state_ = state; | |
| 41 } | |
| 42 } | 42 } |
| 43 | 43 |
| 44 void SessionManager::CreateSession(const AccountId& user_account_id, | 44 void SessionManager::CreateSession(const AccountId& user_account_id, |
| 45 const std::string& user_id_hash) { | 45 const std::string& user_id_hash) { |
| 46 CreateSessionInternal(user_account_id, user_id_hash, | 46 CreateSessionInternal(user_account_id, user_id_hash, |
| 47 false /* browser_restart */); | 47 false /* browser_restart */); |
| 48 } | 48 } |
| 49 | 49 |
| 50 void SessionManager::CreateSessionForRestart(const AccountId& user_account_id, | 50 void SessionManager::CreateSessionForRestart(const AccountId& user_account_id, |
| 51 const std::string& user_id_hash) { | 51 const std::string& user_id_hash) { |
| 52 CreateSessionInternal(user_account_id, user_id_hash, | 52 CreateSessionInternal(user_account_id, user_id_hash, |
| 53 true /* browser_restart */); | 53 true /* browser_restart */); |
| 54 } | 54 } |
| 55 | 55 |
| 56 bool SessionManager::IsSessionStarted() const { | 56 bool SessionManager::IsSessionStarted() const { |
| 57 return session_started_; | 57 return session_started_; |
| 58 } | 58 } |
| 59 | 59 |
| 60 void SessionManager::SessionStarted() { | 60 void SessionManager::SessionStarted() { |
| 61 session_started_ = true; | 61 session_started_ = true; |
| 62 } | 62 } |
| 63 | 63 |
| 64 void SessionManager::AddObserver(SessionManagerObserver* observer) { |
| 65 observers_.AddObserver(observer); |
| 66 } |
| 67 |
| 68 void SessionManager::RemoveObserver(SessionManagerObserver* observer) { |
| 69 observers_.RemoveObserver(observer); |
| 70 } |
| 71 |
| 64 void SessionManager::NotifyUserLoggedIn(const AccountId& user_account_id, | 72 void SessionManager::NotifyUserLoggedIn(const AccountId& user_account_id, |
| 65 const std::string& user_id_hash, | 73 const std::string& user_id_hash, |
| 66 bool browser_restart) { | 74 bool browser_restart) { |
| 67 auto* user_manager = user_manager::UserManager::Get(); | 75 auto* user_manager = user_manager::UserManager::Get(); |
| 68 if (!user_manager) | 76 if (!user_manager) |
| 69 return; | 77 return; |
| 70 user_manager->UserLoggedIn(user_account_id, user_id_hash, browser_restart); | 78 user_manager->UserLoggedIn(user_account_id, user_id_hash, browser_restart); |
| 71 } | 79 } |
| 72 | 80 |
| 73 // static | 81 // static |
| 74 void SessionManager::SetInstance(SessionManager* session_manager) { | 82 void SessionManager::SetInstance(SessionManager* session_manager) { |
| 75 SessionManager::instance = session_manager; | 83 SessionManager::instance = session_manager; |
| 76 } | 84 } |
| 77 | 85 |
| 78 void SessionManager::CreateSessionInternal(const AccountId& user_account_id, | 86 void SessionManager::CreateSessionInternal(const AccountId& user_account_id, |
| 79 const std::string& user_id_hash, | 87 const std::string& user_id_hash, |
| 80 bool browser_restart) { | 88 bool browser_restart) { |
| 81 DCHECK(std::find_if(sessions_.begin(), sessions_.end(), | 89 DCHECK(std::find_if(sessions_.begin(), sessions_.end(), |
| 82 [user_account_id](const Session& session) { | 90 [user_account_id](const Session& session) { |
| 83 return session.user_account_id == user_account_id; | 91 return session.user_account_id == user_account_id; |
| 84 }) == sessions_.end()); | 92 }) == sessions_.end()); |
| 85 | 93 |
| 86 sessions_.push_back({next_id_++, user_account_id}); | 94 sessions_.push_back({next_id_++, user_account_id}); |
| 87 NotifyUserLoggedIn(user_account_id, user_id_hash, browser_restart); | 95 NotifyUserLoggedIn(user_account_id, user_id_hash, browser_restart); |
| 88 } | 96 } |
| 89 | 97 |
| 90 } // namespace session_manager | 98 } // namespace session_manager |
| OLD | NEW |