Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / options / chromeos / accounts_options_handler.cc
blobbf980784dfd0cff2e987c23a81694fbe5443b066
1 // Copyright (c) 2012 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/ui/webui/options/chromeos/accounts_options_handler.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/bind_helpers.h"
11 #include "base/json/json_reader.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "base/prefs/pref_service.h"
14 #include "base/strings/utf_string_conversions.h"
15 #include "base/values.h"
16 #include "chrome/browser/browser_process.h"
17 #include "chrome/browser/chromeos/ownership/owner_settings_service_chromeos.h"
18 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
19 #include "chrome/browser/chromeos/settings/cros_settings.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/ui/webui/chromeos/ui_account_tweaks.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "chromeos/login/user_names.h"
25 #include "chromeos/settings/cros_settings_names.h"
26 #include "components/user_manager/user_manager.h"
27 #include "content/public/browser/web_ui.h"
28 #include "google_apis/gaia/gaia_auth_util.h"
29 #include "ui/base/l10n/l10n_util.h"
31 namespace chromeos {
33 namespace {
35 // Adds specified user to the whitelist. Returns false if that user is already
36 // in the whitelist.
37 bool WhitelistUser(OwnerSettingsServiceChromeOS* service,
38 const std::string& username) {
39 if (CrosSettings::Get()->FindEmailInList(kAccountsPrefUsers, username, NULL))
40 return false;
41 if (service) {
42 base::StringValue username_value(username);
43 service->AppendToList(kAccountsPrefUsers, username_value);
45 return true;
48 } // namespace
50 namespace options {
52 AccountsOptionsHandler::AccountsOptionsHandler() {
55 AccountsOptionsHandler::~AccountsOptionsHandler() {
58 void AccountsOptionsHandler::RegisterMessages() {
59 web_ui()->RegisterMessageCallback("whitelistUser",
60 base::Bind(&AccountsOptionsHandler::HandleWhitelistUser,
61 base::Unretained(this)));
62 web_ui()->RegisterMessageCallback("unwhitelistUser",
63 base::Bind(&AccountsOptionsHandler::HandleUnwhitelistUser,
64 base::Unretained(this)));
65 web_ui()->RegisterMessageCallback("updateWhitelist",
66 base::Bind(&AccountsOptionsHandler::HandleUpdateWhitelist,
67 base::Unretained(this)));
70 void AccountsOptionsHandler::GetLocalizedValues(
71 base::DictionaryValue* localized_strings) {
72 DCHECK(localized_strings);
74 RegisterTitle(localized_strings, "accountsPage",
75 IDS_OPTIONS_ACCOUNTS_TAB_LABEL);
77 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16(
78 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION));
79 localized_strings->SetString(
80 "allow_supervised_users",
81 l10n_util::GetStringUTF16(IDS_OPTIONS_ACCOUNTS_ENABLE_SUPERVISED_USERS));
82 localized_strings->SetString("use_whitelist", l10n_util::GetStringUTF16(
83 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION));
84 localized_strings->SetString("show_user_on_signin", l10n_util::GetStringUTF16(
85 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION));
86 localized_strings->SetString("username_edit_hint", l10n_util::GetStringUTF16(
87 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT));
88 localized_strings->SetString("username_format", l10n_util::GetStringUTF16(
89 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT));
90 localized_strings->SetString("add_users", l10n_util::GetStringUTF16(
91 IDS_OPTIONS_ACCOUNTS_ADD_USERS));
92 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16(
93 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY));
95 policy::BrowserPolicyConnectorChromeOS* connector =
96 g_browser_process->platform_part()->browser_policy_connector_chromeos();
97 localized_strings->SetBoolean("whitelist_is_managed",
98 connector->IsEnterpriseManaged());
100 AddAccountUITweaksLocalizedValues(localized_strings,
101 Profile::FromWebUI(web_ui()));
104 void AccountsOptionsHandler::HandleWhitelistUser(const base::ListValue* args) {
105 std::string typed_email;
106 std::string name;
107 if (!args->GetString(0, &typed_email) ||
108 !args->GetString(1, &name)) {
109 return;
112 if (OwnerSettingsServiceChromeOS* service =
113 OwnerSettingsServiceChromeOS::FromWebUI(web_ui())) {
114 WhitelistUser(service, gaia::CanonicalizeEmail(typed_email));
118 void AccountsOptionsHandler::HandleUnwhitelistUser(
119 const base::ListValue* args) {
120 std::string email;
121 if (!args->GetString(0, &email)) {
122 return;
125 base::StringValue canonical_email(gaia::CanonicalizeEmail(email));
126 if (OwnerSettingsServiceChromeOS* service =
127 OwnerSettingsServiceChromeOS::FromWebUI(web_ui())) {
128 service->RemoveFromList(kAccountsPrefUsers, canonical_email);
130 user_manager::UserManager::Get()->RemoveUser(email, NULL);
133 void AccountsOptionsHandler::HandleUpdateWhitelist(
134 const base::ListValue* args) {
135 DCHECK(args && args->empty());
137 // Creates one list to set. This is needed because user white list update is
138 // asynchronous and sequential. Before previous write comes back, cached list
139 // is stale and should not be used for appending. See http://crbug.com/127215
140 scoped_ptr<base::ListValue> new_list;
142 CrosSettings* cros_settings = CrosSettings::Get();
143 const base::ListValue* existing = NULL;
144 if (cros_settings->GetList(kAccountsPrefUsers, &existing) && existing)
145 new_list.reset(existing->DeepCopy());
146 else
147 new_list.reset(new base::ListValue);
149 // Remove all supervised users. On the next step only supervised users present
150 // on the device will be added back. Thus not present SU are removed.
151 // No need to remove usual users as they can simply login back.
152 for (size_t i = 0; i < new_list->GetSize(); ++i) {
153 std::string whitelisted_user;
154 new_list->GetString(i, &whitelisted_user);
155 if (gaia::ExtractDomainName(whitelisted_user) ==
156 chromeos::login::kSupervisedUserDomain) {
157 new_list->Remove(i, NULL);
158 --i;
162 const user_manager::UserList& users =
163 user_manager::UserManager::Get()->GetUsers();
164 for (user_manager::UserList::const_iterator it = users.begin();
165 it < users.end();
166 ++it)
167 new_list->AppendIfNotPresent(new base::StringValue((*it)->email()));
169 if (OwnerSettingsServiceChromeOS* service =
170 OwnerSettingsServiceChromeOS::FromWebUI(web_ui())) {
171 service->Set(kAccountsPrefUsers, *new_list.get());
175 } // namespace options
176 } // namespace chromeos