| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/chromeos/accounts_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/chromeos/accounts_options_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
| 9 #include "base/json/json_reader.h" | 9 #include "base/json/json_reader.h" |
| 10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/chromeos/cros_settings.h" | 14 #include "chrome/browser/chromeos/cros_settings.h" |
| 15 #include "chrome/browser/chromeos/cros_settings_names.h" | 15 #include "chrome/browser/chromeos/cros_settings_names.h" |
| 16 #include "chrome/browser/chromeos/login/authenticator.h" | 16 #include "chrome/browser/chromeos/login/authenticator.h" |
| 17 #include "chrome/browser/chromeos/login/user_manager.h" | 17 #include "chrome/browser/chromeos/login/user_manager.h" |
| 18 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 19 #include "chrome/browser/policy/browser_policy_connector.h" | 19 #include "chrome/browser/policy/browser_policy_connector.h" |
| 20 #include "grit/generated_resources.h" | 20 #include "grit/generated_resources.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 | 22 |
| 23 namespace chromeos { | 23 namespace chromeos { |
| 24 | 24 |
| 25 namespace { |
| 26 |
| 27 // Adds specified user to the whitelist. Returns false if that user is already |
| 28 // in the whitelist. |
| 29 bool WhitelistUser(const std::string& username) { |
| 30 CrosSettings* cros_settings = CrosSettings::Get(); |
| 31 if (cros_settings->FindEmailInList(kAccountsPrefUsers, username)) |
| 32 return false; |
| 33 base::StringValue username_value(username); |
| 34 cros_settings->AppendToList(kAccountsPrefUsers, &username_value); |
| 35 return true; |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 25 AccountsOptionsHandler::AccountsOptionsHandler() { | 40 AccountsOptionsHandler::AccountsOptionsHandler() { |
| 26 } | 41 } |
| 27 | 42 |
| 28 AccountsOptionsHandler::~AccountsOptionsHandler() { | 43 AccountsOptionsHandler::~AccountsOptionsHandler() { |
| 29 } | 44 } |
| 30 | 45 |
| 31 void AccountsOptionsHandler::RegisterMessages() { | 46 void AccountsOptionsHandler::RegisterMessages() { |
| 32 DCHECK(web_ui_); | 47 DCHECK(web_ui_); |
| 33 web_ui_->RegisterMessageCallback("whitelistUser", | 48 web_ui_->RegisterMessageCallback("whitelistUser", |
| 34 base::Bind(&AccountsOptionsHandler::WhitelistUser, | 49 base::Bind(&AccountsOptionsHandler::HandleWhitelistUser, |
| 35 base::Unretained(this))); | 50 base::Unretained(this))); |
| 36 web_ui_->RegisterMessageCallback("unwhitelistUser", | 51 web_ui_->RegisterMessageCallback("unwhitelistUser", |
| 37 base::Bind(&AccountsOptionsHandler::UnwhitelistUser, | 52 base::Bind(&AccountsOptionsHandler::HandleUnwhitelistUser, |
| 38 base::Unretained(this))); | 53 base::Unretained(this))); |
| 39 web_ui_->RegisterMessageCallback("whitelistExistingUsers", | 54 web_ui_->RegisterMessageCallback("whitelistExistingUsers", |
| 40 base::Bind(&AccountsOptionsHandler::WhitelistExistingUsers, | 55 base::Bind(&AccountsOptionsHandler::HandleWhitelistExistingUsers, |
| 41 base::Unretained(this))); | 56 base::Unretained(this))); |
| 42 } | 57 } |
| 43 | 58 |
| 44 void AccountsOptionsHandler::GetLocalizedValues( | 59 void AccountsOptionsHandler::GetLocalizedValues( |
| 45 base::DictionaryValue* localized_strings) { | 60 base::DictionaryValue* localized_strings) { |
| 46 DCHECK(localized_strings); | 61 DCHECK(localized_strings); |
| 47 | 62 |
| 48 RegisterTitle(localized_strings, "accountsPage", | 63 RegisterTitle(localized_strings, "accountsPage", |
| 49 IDS_OPTIONS_ACCOUNTS_TAB_LABEL); | 64 IDS_OPTIONS_ACCOUNTS_TAB_LABEL); |
| 50 | 65 |
| 51 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16( | 66 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16( |
| 52 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION)); | 67 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION)); |
| 53 localized_strings->SetString("use_whitelist",l10n_util::GetStringUTF16( | 68 localized_strings->SetString("use_whitelist",l10n_util::GetStringUTF16( |
| 54 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION)); | 69 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION)); |
| 55 localized_strings->SetString("show_user_on_signin",l10n_util::GetStringUTF16( | 70 localized_strings->SetString("show_user_on_signin",l10n_util::GetStringUTF16( |
| 56 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION)); | 71 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION)); |
| 57 localized_strings->SetString("username_edit_hint",l10n_util::GetStringUTF16( | 72 localized_strings->SetString("username_edit_hint",l10n_util::GetStringUTF16( |
| 58 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT)); | 73 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT)); |
| 59 localized_strings->SetString("username_format",l10n_util::GetStringUTF16( | 74 localized_strings->SetString("username_format",l10n_util::GetStringUTF16( |
| 60 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT)); | 75 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT)); |
| 61 localized_strings->SetString("add_users",l10n_util::GetStringUTF16( | 76 localized_strings->SetString("add_users",l10n_util::GetStringUTF16( |
| 62 IDS_OPTIONS_ACCOUNTS_ADD_USERS)); | 77 IDS_OPTIONS_ACCOUNTS_ADD_USERS)); |
| 63 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16( | 78 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16( |
| 64 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY)); | 79 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY)); |
| 65 | 80 |
| 66 std::string owner; | 81 std::string owner_email; |
| 67 CrosSettings::Get()->GetString(kDeviceOwner, &owner); | 82 CrosSettings::Get()->GetString(kDeviceOwner, &owner_email); |
| 68 localized_strings->SetString("owner_user_id", UTF8ToUTF16(owner)); | 83 // Translate owner's email to the display email. |
| 84 std::string display_email = |
| 85 UserManager::Get()->GetUserDisplayEmail(owner_email); |
| 86 localized_strings->SetString("owner_user_id", UTF8ToUTF16(display_email)); |
| 69 | 87 |
| 70 localized_strings->SetString("current_user_is_owner", | 88 localized_strings->SetString("current_user_is_owner", |
| 71 UserManager::Get()->current_user_is_owner() ? | 89 UserManager::Get()->current_user_is_owner() ? |
| 72 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 90 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
| 73 localized_strings->SetString("logged_in_as_guest", | 91 localized_strings->SetString("logged_in_as_guest", |
| 74 UserManager::Get()->IsLoggedInAsGuest() ? | 92 UserManager::Get()->IsLoggedInAsGuest() ? |
| 75 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 93 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
| 76 localized_strings->SetString("whitelist_is_managed", | 94 localized_strings->SetString("whitelist_is_managed", |
| 77 g_browser_process->browser_policy_connector()->IsEnterpriseManaged() ? | 95 g_browser_process->browser_policy_connector()->IsEnterpriseManaged() ? |
| 78 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 96 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
| 79 } | 97 } |
| 80 | 98 |
| 81 void AccountsOptionsHandler::WhitelistUser(const base::ListValue* args) { | 99 void 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(Authenticator::Canonicalize(typed_email)); |
| 108 } |
| 109 |
| 110 void AccountsOptionsHandler::HandleUnwhitelistUser( |
| 111 const base::ListValue* args) { |
| 82 std::string email; | 112 std::string email; |
| 83 if (!args->GetString(0, &email)) { | 113 if (!args->GetString(0, &email)) { |
| 84 return; | 114 return; |
| 85 } | 115 } |
| 86 | 116 |
| 87 scoped_ptr<base::StringValue> canonical_email( | 117 base::StringValue canonical_email(Authenticator::Canonicalize(email)); |
| 88 base::Value::CreateStringValue(Authenticator::Canonicalize(email))); | 118 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &canonical_email); |
| 89 CrosSettings::Get()->AppendToList(kAccountsPrefUsers, canonical_email.get()); | |
| 90 } | |
| 91 | |
| 92 void AccountsOptionsHandler::UnwhitelistUser(const base::ListValue* args) { | |
| 93 std::string email; | |
| 94 if (!args->GetString(0, &email)) { | |
| 95 return; | |
| 96 } | |
| 97 | |
| 98 scoped_ptr<base::StringValue> canonical_email( | |
| 99 base::Value::CreateStringValue(Authenticator::Canonicalize(email))); | |
| 100 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, | |
| 101 canonical_email.get()); | |
| 102 UserManager::Get()->RemoveUser(email, NULL); | 119 UserManager::Get()->RemoveUser(email, NULL); |
| 103 } | 120 } |
| 104 | 121 |
| 105 void AccountsOptionsHandler::WhitelistExistingUsers( | 122 void AccountsOptionsHandler::HandleWhitelistExistingUsers( |
| 106 const base::ListValue* args) { | 123 const base::ListValue* args) { |
| 107 base::ListValue whitelist_users; | 124 DCHECK(args && args->empty()); |
| 125 |
| 108 const UserList& users = UserManager::Get()->GetUsers(); | 126 const UserList& users = UserManager::Get()->GetUsers(); |
| 109 for (UserList::const_iterator it = users.begin(); it < users.end(); ++it) { | 127 for (UserList::const_iterator it = users.begin(); it < users.end(); ++it) { |
| 110 const std::string& email = (*it)->email(); | 128 WhitelistUser((*it)->email()); |
| 111 if (!CrosSettings::Get()->FindEmailInList(kAccountsPrefUsers, email)) { | |
| 112 base::DictionaryValue* user_dict = new DictionaryValue; | |
| 113 user_dict->SetString("name", (*it)->GetDisplayName()); | |
| 114 user_dict->SetString("email", email); | |
| 115 user_dict->SetBoolean("owner", false); | |
| 116 | |
| 117 whitelist_users.Append(user_dict); | |
| 118 } | |
| 119 } | 129 } |
| 120 | |
| 121 web_ui_->CallJavascriptFunction("AccountsOptions.addUsers", whitelist_users); | |
| 122 } | 130 } |
| 123 | 131 |
| 124 } // namespace chromeos | 132 } // namespace chromeos |
| OLD | NEW |