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