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"
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/login/user_manager.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/ui/webui/chromeos/ui_account_tweaks.h"
21 #include "chromeos/settings/cros_settings_names.h"
22 #include "content/public/browser/web_ui.h"
23 #include "google_apis/gaia/gaia_auth_util.h"
24 #include "grit/generated_resources.h"
25 #include "ui/base/l10n/l10n_util.h"
31 // Adds specified user to the whitelist. Returns false if that user is already
33 bool WhitelistUser(const std::string& username) {
34 CrosSettings* cros_settings = CrosSettings::Get();
35 if (cros_settings->FindEmailInList(kAccountsPrefUsers, username))
37 base::StringValue username_value(username);
38 cros_settings->AppendToList(kAccountsPrefUsers, &username_value);
46 AccountsOptionsHandler::AccountsOptionsHandler() {
49 AccountsOptionsHandler::~AccountsOptionsHandler() {
52 void AccountsOptionsHandler::RegisterMessages() {
53 web_ui()->RegisterMessageCallback("whitelistUser",
54 base::Bind(&AccountsOptionsHandler::HandleWhitelistUser,
55 base::Unretained(this)));
56 web_ui()->RegisterMessageCallback("unwhitelistUser",
57 base::Bind(&AccountsOptionsHandler::HandleUnwhitelistUser,
58 base::Unretained(this)));
59 web_ui()->RegisterMessageCallback("whitelistExistingUsers",
60 base::Bind(&AccountsOptionsHandler::HandleWhitelistExistingUsers,
61 base::Unretained(this)));
64 void AccountsOptionsHandler::GetLocalizedValues(
65 base::DictionaryValue* localized_strings) {
66 DCHECK(localized_strings);
68 RegisterTitle(localized_strings, "accountsPage",
69 IDS_OPTIONS_ACCOUNTS_TAB_LABEL);
71 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16(
72 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION));
73 localized_strings->SetString("use_whitelist", l10n_util::GetStringUTF16(
74 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION));
75 localized_strings->SetString("show_user_on_signin", l10n_util::GetStringUTF16(
76 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION));
77 localized_strings->SetString("username_edit_hint", l10n_util::GetStringUTF16(
78 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT));
79 localized_strings->SetString("username_format", l10n_util::GetStringUTF16(
80 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT));
81 localized_strings->SetString("add_users", l10n_util::GetStringUTF16(
82 IDS_OPTIONS_ACCOUNTS_ADD_USERS));
83 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16(
84 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY));
86 policy::BrowserPolicyConnectorChromeOS* connector =
87 g_browser_process->platform_part()->browser_policy_connector_chromeos();
88 localized_strings->SetBoolean("whitelist_is_managed",
89 connector->IsEnterpriseManaged());
91 AddAccountUITweaksLocalizedValues(localized_strings);
94 void AccountsOptionsHandler::HandleWhitelistUser(const base::ListValue* args) {
95 std::string typed_email;
97 if (!args->GetString(0, &typed_email) ||
98 !args->GetString(1, &name)) {
102 WhitelistUser(gaia::CanonicalizeEmail(typed_email));
105 void AccountsOptionsHandler::HandleUnwhitelistUser(
106 const base::ListValue* args) {
108 if (!args->GetString(0, &email)) {
112 base::StringValue canonical_email(gaia::CanonicalizeEmail(email));
113 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &canonical_email);
114 UserManager::Get()->RemoveUser(email, NULL);
117 void AccountsOptionsHandler::HandleWhitelistExistingUsers(
118 const base::ListValue* args) {
119 DCHECK(args && args->empty());
121 // Creates one list to set. This is needed because user white list update is
122 // asynchronous and sequential. Before previous write comes back, cached list
123 // is stale and should not be used for appending. See http://crbug.com/127215
124 scoped_ptr<base::ListValue> new_list;
126 CrosSettings* cros_settings = CrosSettings::Get();
127 const base::ListValue* existing = NULL;
128 if (cros_settings->GetList(kAccountsPrefUsers, &existing) && existing)
129 new_list.reset(existing->DeepCopy());
131 new_list.reset(new base::ListValue);
133 const UserList& users = UserManager::Get()->GetUsers();
134 for (UserList::const_iterator it = users.begin(); it < users.end(); ++it)
135 new_list->AppendIfNotPresent(new base::StringValue((*it)->email()));
137 cros_settings->Set(kAccountsPrefUsers, *new_list.get());
140 } // namespace options
141 } // namespace chromeos