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
, NULL
))
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
,
92 Profile::FromWebUI(web_ui()));
95 void AccountsOptionsHandler::HandleWhitelistUser(const base::ListValue
* args
) {
96 std::string typed_email
;
98 if (!args
->GetString(0, &typed_email
) ||
99 !args
->GetString(1, &name
)) {
103 WhitelistUser(gaia::CanonicalizeEmail(typed_email
));
106 void AccountsOptionsHandler::HandleUnwhitelistUser(
107 const base::ListValue
* args
) {
109 if (!args
->GetString(0, &email
)) {
113 base::StringValue
canonical_email(gaia::CanonicalizeEmail(email
));
114 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers
, &canonical_email
);
115 UserManager::Get()->RemoveUser(email
, NULL
);
118 void AccountsOptionsHandler::HandleWhitelistExistingUsers(
119 const base::ListValue
* args
) {
120 DCHECK(args
&& args
->empty());
122 // Creates one list to set. This is needed because user white list update is
123 // asynchronous and sequential. Before previous write comes back, cached list
124 // is stale and should not be used for appending. See http://crbug.com/127215
125 scoped_ptr
<base::ListValue
> new_list
;
127 CrosSettings
* cros_settings
= CrosSettings::Get();
128 const base::ListValue
* existing
= NULL
;
129 if (cros_settings
->GetList(kAccountsPrefUsers
, &existing
) && existing
)
130 new_list
.reset(existing
->DeepCopy());
132 new_list
.reset(new base::ListValue
);
134 const UserList
& users
= UserManager::Get()->GetUsers();
135 for (UserList::const_iterator it
= users
.begin(); it
< users
.end(); ++it
)
136 new_list
->AppendIfNotPresent(new base::StringValue((*it
)->email()));
138 cros_settings
->Set(kAccountsPrefUsers
, *new_list
.get());
141 } // namespace options
142 } // namespace chromeos