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. 4 5 #include "chrome/browser/ui/webui/options/chromeos/accounts_options_handler.h" 6 7 #include <string> 8 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/login/users/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" 26 27 namespace chromeos { 28 29 namespace { 30 31 // Adds specified user to the whitelist. Returns false if that user is already 32 // in the whitelist. 33 bool WhitelistUser(const std::string& username) { 34 CrosSettings* cros_settings = CrosSettings::Get(); 35 if (cros_settings->FindEmailInList(kAccountsPrefUsers, username, NULL)) 36 return false; 37 base::StringValue username_value(username); 38 cros_settings->AppendToList(kAccountsPrefUsers, &username_value); 39 return true; 40 } 41 42 } // namespace 43 44 namespace options { 45 46 AccountsOptionsHandler::AccountsOptionsHandler() { 47 } 48 49 AccountsOptionsHandler::~AccountsOptionsHandler() { 50 } 51 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))); 62 } 63 64 void AccountsOptionsHandler::GetLocalizedValues( 65 base::DictionaryValue* localized_strings) { 66 DCHECK(localized_strings); 67 68 RegisterTitle(localized_strings, "accountsPage", 69 IDS_OPTIONS_ACCOUNTS_TAB_LABEL); 70 71 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16( 72 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION)); 73 localized_strings->SetString( 74 "allow_supervised_users", 75 l10n_util::GetStringUTF16(IDS_OPTIONS_ACCOUNTS_ENABLE_SUPERVISED_USERS)); 76 localized_strings->SetString("use_whitelist", l10n_util::GetStringUTF16( 77 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION)); 78 localized_strings->SetString("show_user_on_signin", l10n_util::GetStringUTF16( 79 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION)); 80 localized_strings->SetString("username_edit_hint", l10n_util::GetStringUTF16( 81 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT)); 82 localized_strings->SetString("username_format", l10n_util::GetStringUTF16( 83 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT)); 84 localized_strings->SetString("add_users", l10n_util::GetStringUTF16( 85 IDS_OPTIONS_ACCOUNTS_ADD_USERS)); 86 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16( 87 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY)); 88 89 policy::BrowserPolicyConnectorChromeOS* connector = 90 g_browser_process->platform_part()->browser_policy_connector_chromeos(); 91 localized_strings->SetBoolean("whitelist_is_managed", 92 connector->IsEnterpriseManaged()); 93 94 AddAccountUITweaksLocalizedValues(localized_strings, 95 Profile::FromWebUI(web_ui())); 96 } 97 98 void AccountsOptionsHandler::HandleWhitelistUser(const base::ListValue* args) { 99 std::string typed_email; 100 std::string name; 101 if (!args->GetString(0, &typed_email) || 102 !args->GetString(1, &name)) { 103 return; 104 } 105 106 WhitelistUser(gaia::CanonicalizeEmail(typed_email)); 107 } 108 109 void AccountsOptionsHandler::HandleUnwhitelistUser( 110 const base::ListValue* args) { 111 std::string email; 112 if (!args->GetString(0, &email)) { 113 return; 114 } 115 116 base::StringValue canonical_email(gaia::CanonicalizeEmail(email)); 117 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &canonical_email); 118 UserManager::Get()->RemoveUser(email, NULL); 119 } 120 121 void AccountsOptionsHandler::HandleWhitelistExistingUsers( 122 const base::ListValue* args) { 123 DCHECK(args && args->empty()); 124 125 // Creates one list to set. This is needed because user white list update is 126 // asynchronous and sequential. Before previous write comes back, cached list 127 // is stale and should not be used for appending. See http://crbug.com/127215 128 scoped_ptr<base::ListValue> new_list; 129 130 CrosSettings* cros_settings = CrosSettings::Get(); 131 const base::ListValue* existing = NULL; 132 if (cros_settings->GetList(kAccountsPrefUsers, &existing) && existing) 133 new_list.reset(existing->DeepCopy()); 134 else 135 new_list.reset(new base::ListValue); 136 137 const UserList& users = UserManager::Get()->GetUsers(); 138 for (UserList::const_iterator it = users.begin(); it < users.end(); ++it) 139 new_list->AppendIfNotPresent(new base::StringValue((*it)->email())); 140 141 cros_settings->Set(kAccountsPrefUsers, *new_list.get()); 142 } 143 144 } // namespace options 145 } // namespace chromeos 146