1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
ash / public / cpp / holding_space / holding_space_controller.cc [blame]
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ash/public/cpp/holding_space/holding_space_controller.h"
#include "ash/public/cpp/holding_space/holding_space_controller_observer.h"
#include "ash/public/cpp/session/session_controller.h"
#include "base/check.h"
#include "base/check_is_test.h"
namespace ash {
namespace {
HoldingSpaceController* g_instance = nullptr;
} // namespace
// HoldingSpaceController ------------------------------------------------------
HoldingSpaceController::HoldingSpaceController() {
CHECK(!g_instance);
g_instance = this;
// `SessionController` may not exist during tests.
if (auto* session_controller = SessionController::Get()) {
session_controller->AddObserver(this);
} else {
CHECK_IS_TEST();
}
}
HoldingSpaceController::~HoldingSpaceController() {
CHECK_EQ(g_instance, this);
for (auto& observer : observers_) {
observer.OnHoldingSpaceControllerDestroying();
}
SetClient(nullptr);
SetModel(nullptr);
g_instance = nullptr;
// `SessionController` may not exist during tests.
if (auto* session_controller = SessionController::Get()) {
session_controller->RemoveObserver(this);
} else {
CHECK_IS_TEST();
}
}
// static
HoldingSpaceController* HoldingSpaceController::Get() {
return g_instance;
}
void HoldingSpaceController::AddObserver(
HoldingSpaceControllerObserver* observer) {
observers_.AddObserver(observer);
}
void HoldingSpaceController::RemoveObserver(
HoldingSpaceControllerObserver* observer) {
observers_.RemoveObserver(observer);
}
void HoldingSpaceController::RegisterClientAndModelForUser(
const AccountId& account_id,
HoldingSpaceClient* client,
HoldingSpaceModel* model) {
clients_and_models_by_account_id_[account_id] = std::make_pair(client, model);
if (account_id == active_user_account_id_) {
SetClient(client);
SetModel(model);
}
}
void HoldingSpaceController::OnActiveUserSessionChanged(
const AccountId& account_id) {
active_user_account_id_ = account_id;
auto client_and_model_it = clients_and_models_by_account_id_.find(account_id);
if (client_and_model_it == clients_and_models_by_account_id_.end()) {
SetClient(nullptr);
SetModel(nullptr);
return;
}
SetClient(client_and_model_it->second.first);
SetModel(client_and_model_it->second.second);
}
void HoldingSpaceController::SetClient(HoldingSpaceClient* client) {
client_ = client;
}
void HoldingSpaceController::SetModel(HoldingSpaceModel* model) {
if (model_) {
HoldingSpaceModel* const old_model = model_;
model_ = nullptr;
for (auto& observer : observers_)
observer.OnHoldingSpaceModelDetached(old_model);
}
model_ = model;
if (model_) {
for (auto& observer : observers_)
observer.OnHoldingSpaceModelAttached(model_);
}
}
} // namespace ash