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.
5 #include "chrome/browser/chromeos/login/screens/chrome_user_selection_screen.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/browser/browser_process.h"
15 #include "chrome/browser/browser_process_platform_part.h"
16 #include "chrome/browser/chromeos/login/ui/views/user_board_view.h"
17 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
18 #include "chrome/browser/ui/webui/chromeos/login/l10n_util.h"
19 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h"
20 #include "components/policy/core/common/cloud/cloud_policy_core.h"
21 #include "components/policy/core/common/cloud/cloud_policy_store.h"
22 #include "components/policy/core/common/policy_map.h"
23 #include "components/policy/core/common/policy_types.h"
24 #include "components/user_manager/user.h"
25 #include "components/user_manager/user_manager.h"
26 #include "components/user_manager/user_type.h"
27 #include "policy/policy_constants.h"
31 ChromeUserSelectionScreen::ChromeUserSelectionScreen(
32 const std::string
& display_type
)
33 : UserSelectionScreen(display_type
),
34 handler_initialized_(false),
36 device_local_account_policy_service_
= g_browser_process
->platform_part()->
37 browser_policy_connector_chromeos()->GetDeviceLocalAccountPolicyService();
38 device_local_account_policy_service_
->AddObserver(this);
41 ChromeUserSelectionScreen::~ChromeUserSelectionScreen() {
42 device_local_account_policy_service_
->RemoveObserver(this);
45 void ChromeUserSelectionScreen::Init(const user_manager::UserList
& users
,
47 UserSelectionScreen::Init(users
, show_guest
);
49 // Retrieve the current policy for all users.
50 for (user_manager::UserList::const_iterator it
= users
.begin();
51 it
!= users
.end(); ++it
) {
52 if ((*it
)->GetType() == user_manager::USER_TYPE_PUBLIC_ACCOUNT
)
53 OnPolicyUpdated((*it
)->GetUserID());
57 void ChromeUserSelectionScreen::SendUserList() {
58 UserSelectionScreen::SendUserList();
59 handler_initialized_
= true;
62 void ChromeUserSelectionScreen::OnPolicyUpdated(const std::string
& user_id
) {
63 policy::DeviceLocalAccountPolicyBroker
* broker
=
64 device_local_account_policy_service_
->GetBrokerForUser(user_id
);
68 CheckForPublicSessionDisplayNameChange(broker
);
69 CheckForPublicSessionLocalePolicyChange(broker
);
72 void ChromeUserSelectionScreen::OnDeviceLocalAccountsChanged() {
73 // Nothing to do here. When the list of device-local accounts changes, the
74 // entire UI is reloaded.
77 void ChromeUserSelectionScreen::CheckForPublicSessionDisplayNameChange(
78 policy::DeviceLocalAccountPolicyBroker
* broker
) {
79 const std::string
& user_id
= broker
->user_id();
80 const std::string
& display_name
= broker
->GetDisplayName();
81 if (display_name
== public_session_display_names_
[user_id
])
84 public_session_display_names_
[user_id
] = display_name
;
86 if (!handler_initialized_
)
89 if (!display_name
.empty()) {
90 // If a new display name was set by policy, notify the UI about it.
91 view_
->SetPublicSessionDisplayName(user_id
, display_name
);
95 // When no display name is set by policy, the |User|, owned by |UserManager|,
96 // decides what display name to use. However, the order in which |UserManager|
97 // and |this| are informed of the display name change is undefined. Post a
98 // task that will update the UI after the UserManager is guaranteed to have
99 // been informed of the change.
100 base::MessageLoop::current()->PostTask(
102 base::Bind(&ChromeUserSelectionScreen::SetPublicSessionDisplayName
,
103 weak_factory_
.GetWeakPtr(),
107 void ChromeUserSelectionScreen::CheckForPublicSessionLocalePolicyChange(
108 policy::DeviceLocalAccountPolicyBroker
* broker
) {
109 const std::string
& user_id
= broker
->user_id();
110 const policy::PolicyMap::Entry
* entry
=
111 broker
->core()->store()->policy_map().Get(policy::key::kSessionLocales
);
113 // Parse the list of recommended locales set by policy.
114 std::vector
<std::string
> new_recommended_locales
;
115 base::ListValue
const* list
= NULL
;
117 entry
->level
== policy::POLICY_LEVEL_RECOMMENDED
&&
119 entry
->value
->GetAsList(&list
)) {
120 for (base::ListValue::const_iterator it
= list
->begin(); it
!= list
->end();
123 if (!(*it
)->GetAsString(&locale
)) {
125 new_recommended_locales
.clear();
128 new_recommended_locales
.push_back(locale
);
132 std::vector
<std::string
>& recommended_locales
=
133 public_session_recommended_locales_
[user_id
];
135 if (new_recommended_locales
!= recommended_locales
)
136 SetPublicSessionLocales(user_id
, new_recommended_locales
);
138 if (new_recommended_locales
.empty())
139 public_session_recommended_locales_
.erase(user_id
);
141 recommended_locales
= new_recommended_locales
;
144 void ChromeUserSelectionScreen::SetPublicSessionDisplayName(
145 const std::string
& user_id
) {
146 const user_manager::User
* user
=
147 user_manager::UserManager::Get()->FindUser(user_id
);
148 if (!user
|| user
->GetType() != user_manager::USER_TYPE_PUBLIC_ACCOUNT
)
151 view_
->SetPublicSessionDisplayName(user_id
,
152 base::UTF16ToUTF8(user
->GetDisplayName()));
155 void ChromeUserSelectionScreen::SetPublicSessionLocales(
156 const std::string
& user_id
,
157 const std::vector
<std::string
>& recommended_locales
) {
158 if (!handler_initialized_
)
161 // Construct the list of available locales. This list consists of the
162 // recommended locales, followed by all others.
163 scoped_ptr
<base::ListValue
> available_locales
=
164 GetUILanguageList(&recommended_locales
, std::string());
166 // Set the initially selected locale to the first recommended locale that is
167 // actually available or the current UI locale if none of them are available.
168 const std::string default_locale
= FindMostRelevantLocale(
170 *available_locales
.get(),
171 g_browser_process
->GetApplicationLocale());
173 // Set a flag to indicate whether the list of recommended locales contains at
174 // least two entries. This is used to decide whether the public session pod
175 // expands to its basic form (for zero or one recommended locales) or the
176 // advanced form (two or more recommended locales).
177 const bool two_or_more_recommended_locales
= recommended_locales
.size() >= 2;
180 view_
->SetPublicSessionLocales(user_id
, available_locales
.Pass(),
182 two_or_more_recommended_locales
);
185 } // namespace chromeos