Chromium Code Reviews| 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" |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 25 | 25 |
| 26 AccountsOptionsHandler::AccountsOptionsHandler() { | 26 AccountsOptionsHandler::AccountsOptionsHandler() { |
| 27 } | 27 } |
| 28 | 28 |
| 29 AccountsOptionsHandler::~AccountsOptionsHandler() { | 29 AccountsOptionsHandler::~AccountsOptionsHandler() { |
| 30 } | 30 } |
| 31 | 31 |
| 32 void AccountsOptionsHandler::RegisterMessages() { | 32 void AccountsOptionsHandler::RegisterMessages() { |
| 33 DCHECK(web_ui_); | 33 DCHECK(web_ui_); |
| 34 web_ui_->RegisterMessageCallback("whitelistUser", | 34 web_ui_->RegisterMessageCallback("whitelistUser", |
| 35 base::Bind(&AccountsOptionsHandler::WhitelistUser, | 35 base::Bind(&AccountsOptionsHandler::HandleWhitelistUser, |
| 36 base::Unretained(this))); | 36 base::Unretained(this))); |
| 37 web_ui_->RegisterMessageCallback("unwhitelistUser", | 37 web_ui_->RegisterMessageCallback("unwhitelistUser", |
| 38 base::Bind(&AccountsOptionsHandler::UnwhitelistUser, | 38 base::Bind(&AccountsOptionsHandler::HandleUnwhitelistUser, |
| 39 base::Unretained(this))); | 39 base::Unretained(this))); |
| 40 web_ui_->RegisterMessageCallback("whitelistExistingUsers", | 40 web_ui_->RegisterMessageCallback("whitelistExistingUsers", |
| 41 base::Bind(&AccountsOptionsHandler::WhitelistExistingUsers, | 41 base::Bind(&AccountsOptionsHandler::HandleWhitelistExistingUsers, |
| 42 base::Unretained(this))); | 42 base::Unretained(this))); |
| 43 } | 43 } |
| 44 | 44 |
| 45 void AccountsOptionsHandler::GetLocalizedValues( | 45 void AccountsOptionsHandler::GetLocalizedValues( |
| 46 base::DictionaryValue* localized_strings) { | 46 base::DictionaryValue* localized_strings) { |
| 47 DCHECK(localized_strings); | 47 DCHECK(localized_strings); |
| 48 | 48 |
| 49 RegisterTitle(localized_strings, "accountsPage", | 49 RegisterTitle(localized_strings, "accountsPage", |
| 50 IDS_OPTIONS_ACCOUNTS_TAB_LABEL); | 50 IDS_OPTIONS_ACCOUNTS_TAB_LABEL); |
| 51 | 51 |
| 52 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16( | 52 localized_strings->SetString("allow_BWSI", l10n_util::GetStringUTF16( |
| 53 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION)); | 53 IDS_OPTIONS_ACCOUNTS_ALLOW_BWSI_DESCRIPTION)); |
| 54 localized_strings->SetString("use_whitelist",l10n_util::GetStringUTF16( | 54 localized_strings->SetString("use_whitelist",l10n_util::GetStringUTF16( |
| 55 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION)); | 55 IDS_OPTIONS_ACCOUNTS_USE_WHITELIST_DESCRIPTION)); |
| 56 localized_strings->SetString("show_user_on_signin",l10n_util::GetStringUTF16( | 56 localized_strings->SetString("show_user_on_signin",l10n_util::GetStringUTF16( |
| 57 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION)); | 57 IDS_OPTIONS_ACCOUNTS_SHOW_USER_NAMES_ON_SINGIN_DESCRIPTION)); |
| 58 localized_strings->SetString("username_edit_hint",l10n_util::GetStringUTF16( | 58 localized_strings->SetString("username_edit_hint",l10n_util::GetStringUTF16( |
| 59 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT)); | 59 IDS_OPTIONS_ACCOUNTS_USERNAME_EDIT_HINT)); |
| 60 localized_strings->SetString("username_format",l10n_util::GetStringUTF16( | 60 localized_strings->SetString("username_format",l10n_util::GetStringUTF16( |
| 61 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT)); | 61 IDS_OPTIONS_ACCOUNTS_USERNAME_FORMAT)); |
| 62 localized_strings->SetString("add_users",l10n_util::GetStringUTF16( | 62 localized_strings->SetString("add_users",l10n_util::GetStringUTF16( |
| 63 IDS_OPTIONS_ACCOUNTS_ADD_USERS)); | 63 IDS_OPTIONS_ACCOUNTS_ADD_USERS)); |
| 64 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16( | 64 localized_strings->SetString("owner_only", l10n_util::GetStringUTF16( |
| 65 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY)); | 65 IDS_OPTIONS_ACCOUNTS_OWNER_ONLY)); |
| 66 | 66 |
| 67 std::string owner; | 67 std::string owner_email; |
| 68 CrosSettings::Get()->GetString(kDeviceOwner, &owner); | 68 CrosSettings::Get()->GetString(kDeviceOwner, &owner_email); |
| 69 localized_strings->SetString("owner_user_id", UTF8ToUTF16(owner)); | 69 // Translate owner's email to the display email. |
| 70 std::string display_email = | |
| 71 UserManager::Get()->GetUserDisplayEmail(owner_email); | |
| 72 localized_strings->SetString("owner_user_id", UTF8ToUTF16(display_email)); | |
| 70 | 73 |
| 71 localized_strings->SetString("current_user_is_owner", | 74 localized_strings->SetString("current_user_is_owner", |
| 72 UserManager::Get()->current_user_is_owner() ? | 75 UserManager::Get()->current_user_is_owner() ? |
| 73 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 76 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
| 74 localized_strings->SetString("logged_in_as_guest", | 77 localized_strings->SetString("logged_in_as_guest", |
| 75 UserManager::Get()->IsLoggedInAsGuest() ? | 78 UserManager::Get()->IsLoggedInAsGuest() ? |
| 76 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 79 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
| 77 localized_strings->SetString("whitelist_is_managed", | 80 localized_strings->SetString("whitelist_is_managed", |
| 78 g_browser_process->browser_policy_connector()->IsEnterpriseManaged() ? | 81 g_browser_process->browser_policy_connector()->IsEnterpriseManaged() ? |
| 79 ASCIIToUTF16("true") : ASCIIToUTF16("false")); | 82 ASCIIToUTF16("true") : ASCIIToUTF16("false")); |
| 80 } | 83 } |
| 81 | 84 |
| 82 void AccountsOptionsHandler::WhitelistUser(const base::ListValue* args) { | 85 base::DictionaryValue* AccountsOptionsHandler::CreateUserInfo( |
| 86 const std::string& username, | |
| 87 const std::string& display_email, | |
| 88 const std::string& display_name) { | |
| 89 base::DictionaryValue* user_dict = new DictionaryValue; | |
|
James Hawkins
2011/12/02 18:12:01
This doesn't appear necessary to be on the object.
Ivan Korotkov
2011/12/02 18:30:30
This is called from CoreChromeOSOptionsHandler
| |
| 90 user_dict->SetString("username", username); | |
| 91 user_dict->SetString("name", display_email); | |
| 92 user_dict->SetString("email", display_name); | |
| 93 user_dict->SetBoolean("owner", IsLoggedInOwner(username)); | |
| 94 return user_dict; | |
| 95 } | |
| 96 | |
| 97 void AccountsOptionsHandler::HandleWhitelistUser(const base::ListValue* args) { | |
| 98 std::string email; | |
| 99 std::string name; | |
| 100 if (!args->GetString(0, &email) || | |
| 101 !args->GetString(1, &name)) { | |
| 102 return; | |
| 103 } | |
| 104 | |
| 105 std::string canonical_email = Authenticator::Canonicalize(email); | |
| 106 // Record a display email for the whitelisted user if it doesn't exist yet. | |
| 107 UserManager::Get()->SaveUserDisplayEmail(canonical_email, email, false); | |
| 108 | |
| 109 if (WhitelistUser(canonical_email)) { | |
| 110 base::ListValue single_user; | |
| 111 single_user.Append(CreateUserInfo(canonical_email, email, name)); | |
| 112 web_ui_->CallJavascriptFunction("AccountsOptions.addUsers", single_user); | |
| 113 } | |
| 114 } | |
| 115 | |
| 116 void AccountsOptionsHandler::HandleUnwhitelistUser( | |
| 117 const base::ListValue* args) { | |
| 83 std::string email; | 118 std::string email; |
| 84 if (!args->GetString(0, &email)) { | 119 if (!args->GetString(0, &email)) { |
| 85 return; | 120 return; |
| 86 } | 121 } |
| 87 | 122 |
| 88 scoped_ptr<base::StringValue> canonical_email( | 123 base::StringValue canonical_email(Authenticator::Canonicalize(email)); |
| 89 base::Value::CreateStringValue(Authenticator::Canonicalize(email))); | 124 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, &canonical_email); |
| 90 CrosSettings::Get()->AppendToList(kAccountsPrefUsers, canonical_email.get()); | 125 // TODO(ivankr): this will not remove user's diplayed email if there is |
| 91 } | 126 // no such user. |
| 92 | |
| 93 void AccountsOptionsHandler::UnwhitelistUser(const base::ListValue* args) { | |
| 94 std::string email; | |
| 95 if (!args->GetString(0, &email)) { | |
| 96 return; | |
| 97 } | |
| 98 | |
| 99 scoped_ptr<base::StringValue> canonical_email( | |
| 100 base::Value::CreateStringValue(Authenticator::Canonicalize(email))); | |
| 101 CrosSettings::Get()->RemoveFromList(kAccountsPrefUsers, | |
| 102 canonical_email.get()); | |
| 103 UserManager::Get()->RemoveUser(email, NULL); | 127 UserManager::Get()->RemoveUser(email, NULL); |
| 104 } | 128 } |
| 105 | 129 |
| 106 void AccountsOptionsHandler::WhitelistExistingUsers( | 130 void AccountsOptionsHandler::HandleWhitelistExistingUsers( |
| 107 const base::ListValue* args) { | 131 const base::ListValue* args) { |
| 108 base::ListValue whitelist_users; | 132 base::ListValue whitelist_users; |
| 109 const UserList& users = UserManager::Get()->GetUsers(); | 133 const UserList& users = UserManager::Get()->GetUsers(); |
| 110 for (UserList::const_iterator it = users.begin(); it < users.end(); ++it) { | 134 for (UserList::const_iterator it = users.begin(); it < users.end(); ++it) { |
| 111 const std::string& email = (*it)->email(); | 135 const std::string& email = (*it)->email(); |
| 112 if (!CrosSettings::Get()->FindEmailInList(kAccountsPrefUsers, email)) { | 136 if (WhitelistUser(email)) { |
| 113 base::DictionaryValue* user_dict = new DictionaryValue; | 137 whitelist_users.Append(CreateUserInfo( |
| 114 user_dict->SetString("name", (*it)->GetDisplayName()); | 138 email, (*it)->display_email(), (*it)->GetDisplayName())); |
| 115 user_dict->SetString("email", email); | |
| 116 user_dict->SetBoolean("owner", false); | |
| 117 | |
| 118 whitelist_users.Append(user_dict); | |
| 119 } | 139 } |
| 120 } | 140 } |
| 121 | 141 |
| 122 web_ui_->CallJavascriptFunction("AccountsOptions.addUsers", whitelist_users); | 142 web_ui_->CallJavascriptFunction("AccountsOptions.addUsers", whitelist_users); |
| 123 } | 143 } |
| 124 | 144 |
| 145 bool AccountsOptionsHandler::WhitelistUser(const std::string& username) { | |
|
James Hawkins
2011/12/02 18:12:01
Same as above; consider moving to unnamed namespac
Ivan Korotkov
2011/12/02 18:30:30
Done.
| |
| 146 CrosSettings* cros_settings = CrosSettings::Get(); | |
| 147 if (cros_settings->FindEmailInList(kAccountsPrefUsers, username)) | |
| 148 return false; | |
| 149 base::StringValue username_value(username); | |
| 150 cros_settings->AppendToList(kAccountsPrefUsers, &username_value); | |
| 151 return true; | |
| 152 } | |
| 153 | |
| 154 bool AccountsOptionsHandler::IsLoggedInOwner(const std::string& username) { | |
| 155 UserManager* user_manager = UserManager::Get(); | |
| 156 return user_manager->current_user_is_owner() && | |
| 157 user_manager->logged_in_user().email() == username; | |
| 158 } | |
| 159 | |
| 125 } // namespace chromeos | 160 } // namespace chromeos |
| OLD | NEW |