Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(38)

Side by Side Diff: ash/common/session/session_controller.cc

Issue 2545723003: ash: Add SessionController/Client mojo interfaces (Closed)
Patch Set: use a default to fix null avatar image failures Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
5 #include "ash/common/session/session_controller.h"
6
7 #include <set>
8
9 #include "ash/common/session/session_state_observer.h"
10 #include "base/bind.h"
11 #include "base/bind_helpers.h"
12 #include "components/signin/core/account_id/account_id.h"
13 #include "services/service_manager/public/cpp/connector.h"
14
15 namespace ash {
16
17 SessionController::SessionController() {}
18
19 SessionController::~SessionController() {}
20
21 void SessionController::BindRequest(mojom::SessionControllerRequest request) {
22 bindings_.AddBinding(this, std::move(request));
23 }
24
25 int SessionController::GetMaximumNumberOfLoggedInUsers() const {
26 return static_cast<int>(max_users_);
27 }
28
29 int SessionController::NumberOfLoggedInUsers() const {
30 return static_cast<int>(user_sessions_.size());
31 }
32
33 AddUserSessionPolicy SessionController::GetAddUserPolicy() const {
34 return add_user_session_policy_;
35 }
36
37 bool SessionController::IsActiveUserSessionStarted() const {
38 return !user_sessions_.empty();
39 }
40
41 bool SessionController::CanLockScreen() const {
42 return can_lock_;
43 }
44
45 bool SessionController::IsScreenLocked() const {
46 return state_ == session_manager::SessionState::LOCKED;
47 }
48
49 bool SessionController::ShouldLockScreenAutomatically() const {
50 return should_lock_screen_automatically_;
51 }
52
53 bool SessionController::IsUserSessionBlocked() const {
54 return state_ != session_manager::SessionState::ACTIVE;
55 }
56
57 session_manager::SessionState SessionController::GetSessionState() const {
58 return state_;
59 }
60
61 const std::vector<mojom::UserSessionPtr>& SessionController::GetUserSessions()
62 const {
63 return user_sessions_;
64 }
65
66 void SessionController::LockScreen() {
67 client_->RequestLockScreen();
James Cook 2016/12/01 23:01:43 Check if (client_) since it's possible someday we'
xiyuan 2016/12/06 00:46:33 Done.
68 }
69
70 void SessionController::SwitchActiveUser(const AccountId& account_id) {
71 client_->SwitchActiveUser(account_id.Serialize());
72 }
73
74 void SessionController::CycleActiveUser(bool next_user) {
75 client_->CycleActiveUser(next_user);
76 }
77
78 void SessionController::AddSessionStateObserver(
79 SessionStateObserver* observer) {
80 observers_.AddObserver(observer);
81 }
82
83 void SessionController::RemoveSessionStateObserver(
84 SessionStateObserver* observer) {
85 observers_.RemoveObserver(observer);
86 }
87
88 void SessionController::SetClient(mojom::SessionControllerClientPtr client) {
89 client_ = std::move(client);
90 }
91
92 void SessionController::SetMaxUsers(uint32_t max_users) {
93 max_users_ = max_users;
94 }
95
96 void SessionController::SetCanLockScreen(bool can_lock) {
97 can_lock_ = can_lock;
98 }
99
100 void SessionController::SetShouldLockScreenAutomatically(bool should_lock) {
101 should_lock_screen_automatically_ = should_lock;
102 }
103
104 void SessionController::SetAddUserSessionPolicy(
105 AddUserSessionPolicy add_user_session_policy) {
106 add_user_session_policy_ = add_user_session_policy;
107 }
108
109 void SessionController::SetSessionState(session_manager::SessionState state) {
110 if (state_ == state)
111 return;
112
113 state_ = state;
114 for (auto& observer : observers_)
115 observer.SessionStateChanged(state_);
116 }
117
118 void SessionController::SetUserSessions(
119 std::vector<mojom::UserSessionPtr> user_sessions) {
120 const std::string old_active_user =
121 user_sessions_.empty() ? std::string() : user_sessions_[0]->account_id;
122
123 std::set<std::string> existing_users;
124 for (auto& user_session : user_sessions_)
James Cook 2016/12/01 23:01:43 const auto&, if possible, here and below
xiyuan 2016/12/06 00:46:33 Code removed.
125 existing_users.insert(user_session->account_id);
126
127 user_sessions_ = std::move(user_sessions);
128
129 for (auto& user_session : user_sessions) {
James Cook 2016/12/01 23:01:43 user_sessions_? isn't |user_sessions| empty now th
xiyuan 2016/12/06 00:46:33 Right. The code is wrong. Removed in new patch sin
130 if (existing_users.find(user_session->account_id) == existing_users.end()) {
James Cook 2016/12/01 23:01:43 optional: if ( ... != end) continue; which would r
xiyuan 2016/12/06 00:46:33 Code removed.
131 AccountId account_id(EmptyAccountId());
132 if (!AccountId::Deserialize(user_session->account_id, &account_id)) {
133 LOG(ERROR) << "Failed to deserialize account id.";
134 continue;
135 }
136 for (auto& observer : observers_)
137 observer.UserAddedToSession(account_id);
138 }
139 }
James Cook 2016/12/01 23:01:43 nit: this function could use some comments explain
xiyuan 2016/12/06 00:46:33 Acknowledged.
140
141 const std::string new_active_user =
142 user_sessions_.empty() ? std::string() : user_sessions_[0]->account_id;
143 if (new_active_user != old_active_user) {
144 AccountId account_id(EmptyAccountId());
145 if (AccountId::Deserialize(new_active_user, &account_id)) {
146 for (auto& observer : observers_)
147 observer.ActiveUserChanged(account_id);
148 }
149 }
150 }
James Cook 2016/12/01 23:01:43 Can you add a simple unit test for this class? Jus
xiyuan 2016/12/06 00:46:33 Done.
151
152 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698