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/settings/cros_settings.h"
19 #include "chrome/browser/policy/browser_policy_connector.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 localized_strings
->SetBoolean("whitelist_is_managed",
87 g_browser_process
->browser_policy_connector()->IsEnterpriseManaged());
89 AddAccountUITweaksLocalizedValues(localized_strings
);
92 void AccountsOptionsHandler::HandleWhitelistUser(const base::ListValue
* args
) {
93 std::string typed_email
;
95 if (!args
->GetString(0, &typed_email
) ||
96 !args
->GetString(1, &name
)) {
100 WhitelistUser(gaia::CanonicalizeEmail(typed_email
));
103 void AccountsOptionsHandler::HandleUnwhitelistUser(
104 const base::ListValue
* args
) {
106 if (!args
->GetString(0, &email
)) {
110 base::StringValue
canonical_email(gaia::CanonicalizeEmail(email
));
111 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers
, &canonical_email
);
112 UserManager::Get()->RemoveUser(email
, NULL
);
115 void AccountsOptionsHandler::HandleWhitelistExistingUsers(
116 const base::ListValue
* args
) {
117 DCHECK(args
&& args
->empty());
119 // Creates one list to set. This is needed because user white list update is
120 // asynchronous and sequential. Before previous write comes back, cached list
121 // is stale and should not be used for appending. See http://crbug.com/127215
122 scoped_ptr
<base::ListValue
> new_list
;
124 CrosSettings
* cros_settings
= CrosSettings::Get();
125 const base::ListValue
* existing
= NULL
;
126 if (cros_settings
->GetList(kAccountsPrefUsers
, &existing
) && existing
)
127 new_list
.reset(existing
->DeepCopy());
129 new_list
.reset(new base::ListValue
);
131 const UserList
& users
= UserManager::Get()->GetUsers();
132 for (UserList::const_iterator it
= users
.begin(); it
< users
.end(); ++it
)
133 new_list
->AppendIfNotPresent(new base::StringValue((*it
)->email()));
135 cros_settings
->Set(kAccountsPrefUsers
, *new_list
.get());
138 } // namespace options
139 } // namespace chromeos