| OLD | NEW |
| (Empty) |
| 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/options2/password_manager_handler.h" | |
| 6 | |
| 7 #include "base/bind.h" | |
| 8 #include "base/string_number_conversions.h" | |
| 9 #include "base/utf_string_conversions.h" | |
| 10 #include "base/values.h" | |
| 11 #include "chrome/browser/password_manager/password_store_factory.h" | |
| 12 #include "chrome/browser/prefs/pref_service.h" | |
| 13 #include "chrome/browser/profiles/profile.h" | |
| 14 #include "chrome/common/chrome_notification_types.h" | |
| 15 #include "chrome/common/pref_names.h" | |
| 16 #include "chrome/common/url_constants.h" | |
| 17 #include "content/public/browser/notification_details.h" | |
| 18 #include "content/public/browser/notification_source.h" | |
| 19 #include "content/public/browser/web_ui.h" | |
| 20 #include "grit/chromium_strings.h" | |
| 21 #include "grit/generated_resources.h" | |
| 22 #include "net/base/net_util.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 24 #include "webkit/forms/password_form.h" | |
| 25 | |
| 26 namespace options { | |
| 27 | |
| 28 PasswordManagerHandler::PasswordManagerHandler() | |
| 29 : ALLOW_THIS_IN_INITIALIZER_LIST(populater_(this)), | |
| 30 ALLOW_THIS_IN_INITIALIZER_LIST(exception_populater_(this)) { | |
| 31 } | |
| 32 | |
| 33 PasswordManagerHandler::~PasswordManagerHandler() { | |
| 34 PasswordStore* store = GetPasswordStore(); | |
| 35 if (store) | |
| 36 store->RemoveObserver(this); | |
| 37 } | |
| 38 | |
| 39 void PasswordManagerHandler::GetLocalizedValues( | |
| 40 DictionaryValue* localized_strings) { | |
| 41 DCHECK(localized_strings); | |
| 42 | |
| 43 static const OptionsStringResource resources[] = { | |
| 44 { "savedPasswordsTitle", | |
| 45 IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE }, | |
| 46 { "passwordExceptionsTitle", | |
| 47 IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE }, | |
| 48 { "passwordSearchPlaceholder", | |
| 49 IDS_PASSWORDS_PAGE_SEARCH_PASSWORDS }, | |
| 50 { "passwordShowButton", | |
| 51 IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON }, | |
| 52 { "passwordHideButton", | |
| 53 IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON }, | |
| 54 { "passwordsNoPasswordsDescription", | |
| 55 IDS_PASSWORDS_PAGE_VIEW_NO_PASSWORDS_DESCRIPTION }, | |
| 56 { "passwordsNoExceptionsDescription", | |
| 57 IDS_PASSWORDS_PAGE_VIEW_NO_EXCEPTIONS_DESCRIPTION }, | |
| 58 }; | |
| 59 | |
| 60 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
| 61 RegisterTitle(localized_strings, "passwordsPage", | |
| 62 IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE); | |
| 63 | |
| 64 localized_strings->SetString("passwordManagerLearnMoreURL", | |
| 65 chrome::kPasswordManagerLearnMoreURL); | |
| 66 } | |
| 67 | |
| 68 void PasswordManagerHandler::InitializeHandler() { | |
| 69 // Due to the way that handlers are (re)initialized under certain types of | |
| 70 // navigation, we may already be initialized. (See bugs 88986 and 86448.) | |
| 71 // If this is the case, return immediately. This is a hack. | |
| 72 // TODO(mdm): remove this hack once it is no longer necessary. | |
| 73 if (!show_passwords_.GetPrefName().empty()) | |
| 74 return; | |
| 75 | |
| 76 show_passwords_.Init(prefs::kPasswordManagerAllowShowPasswords, | |
| 77 Profile::FromWebUI(web_ui())->GetPrefs(), this); | |
| 78 // We should not cache web_ui()->GetProfile(). See crosbug.com/6304. | |
| 79 PasswordStore* store = GetPasswordStore(); | |
| 80 if (store) | |
| 81 store->AddObserver(this); | |
| 82 } | |
| 83 | |
| 84 void PasswordManagerHandler::RegisterMessages() { | |
| 85 web_ui()->RegisterMessageCallback("updatePasswordLists", | |
| 86 base::Bind(&PasswordManagerHandler::UpdatePasswordLists, | |
| 87 base::Unretained(this))); | |
| 88 web_ui()->RegisterMessageCallback("removeSavedPassword", | |
| 89 base::Bind(&PasswordManagerHandler::RemoveSavedPassword, | |
| 90 base::Unretained(this))); | |
| 91 web_ui()->RegisterMessageCallback("removePasswordException", | |
| 92 base::Bind(&PasswordManagerHandler::RemovePasswordException, | |
| 93 base::Unretained(this))); | |
| 94 web_ui()->RegisterMessageCallback("removeAllSavedPasswords", | |
| 95 base::Bind(&PasswordManagerHandler::RemoveAllSavedPasswords, | |
| 96 base::Unretained(this))); | |
| 97 web_ui()->RegisterMessageCallback("removeAllPasswordExceptions", | |
| 98 base::Bind(&PasswordManagerHandler::RemoveAllPasswordExceptions, | |
| 99 base::Unretained(this))); | |
| 100 } | |
| 101 | |
| 102 void PasswordManagerHandler::OnLoginsChanged() { | |
| 103 UpdatePasswordLists(NULL); | |
| 104 } | |
| 105 | |
| 106 PasswordStore* PasswordManagerHandler::GetPasswordStore() { | |
| 107 return PasswordStoreFactory::GetForProfile( | |
| 108 Profile::FromWebUI(web_ui()), | |
| 109 Profile::EXPLICIT_ACCESS); | |
| 110 } | |
| 111 | |
| 112 void PasswordManagerHandler::Observe( | |
| 113 int type, | |
| 114 const content::NotificationSource& source, | |
| 115 const content::NotificationDetails& details) { | |
| 116 if (type == chrome::NOTIFICATION_PREF_CHANGED) { | |
| 117 std::string* pref_name = content::Details<std::string>(details).ptr(); | |
| 118 if (*pref_name == prefs::kPasswordManagerAllowShowPasswords) { | |
| 119 UpdatePasswordLists(NULL); | |
| 120 } | |
| 121 } | |
| 122 | |
| 123 OptionsPageUIHandler::Observe(type, source, details); | |
| 124 } | |
| 125 | |
| 126 void PasswordManagerHandler::UpdatePasswordLists(const ListValue* args) { | |
| 127 // Reset the current lists. | |
| 128 password_list_.clear(); | |
| 129 password_exception_list_.clear(); | |
| 130 | |
| 131 languages_ = Profile::FromWebUI(web_ui())->GetPrefs()-> | |
| 132 GetString(prefs::kAcceptLanguages); | |
| 133 populater_.Populate(); | |
| 134 exception_populater_.Populate(); | |
| 135 } | |
| 136 | |
| 137 void PasswordManagerHandler::RemoveSavedPassword(const ListValue* args) { | |
| 138 PasswordStore* store = GetPasswordStore(); | |
| 139 if (!store) | |
| 140 return; | |
| 141 std::string string_value = UTF16ToUTF8(ExtractStringValue(args)); | |
| 142 int index; | |
| 143 if (base::StringToInt(string_value, &index) && index >= 0 && | |
| 144 static_cast<size_t>(index) < password_list_.size()) | |
| 145 store->RemoveLogin(*password_list_[index]); | |
| 146 } | |
| 147 | |
| 148 void PasswordManagerHandler::RemovePasswordException( | |
| 149 const ListValue* args) { | |
| 150 PasswordStore* store = GetPasswordStore(); | |
| 151 if (!store) | |
| 152 return; | |
| 153 std::string string_value = UTF16ToUTF8(ExtractStringValue(args)); | |
| 154 int index; | |
| 155 if (base::StringToInt(string_value, &index) && index >= 0 && | |
| 156 static_cast<size_t>(index) < password_exception_list_.size()) | |
| 157 store->RemoveLogin(*password_exception_list_[index]); | |
| 158 } | |
| 159 | |
| 160 void PasswordManagerHandler::RemoveAllSavedPasswords( | |
| 161 const ListValue* args) { | |
| 162 // TODO(jhawkins): This will cause a list refresh for every password in the | |
| 163 // list. Add PasswordStore::RemoveAllLogins(). | |
| 164 PasswordStore* store = GetPasswordStore(); | |
| 165 if (!store) | |
| 166 return; | |
| 167 for (size_t i = 0; i < password_list_.size(); ++i) | |
| 168 store->RemoveLogin(*password_list_[i]); | |
| 169 } | |
| 170 | |
| 171 void PasswordManagerHandler::RemoveAllPasswordExceptions( | |
| 172 const ListValue* args) { | |
| 173 PasswordStore* store = GetPasswordStore(); | |
| 174 if (!store) | |
| 175 return; | |
| 176 for (size_t i = 0; i < password_exception_list_.size(); ++i) | |
| 177 store->RemoveLogin(*password_exception_list_[i]); | |
| 178 } | |
| 179 | |
| 180 void PasswordManagerHandler::SetPasswordList() { | |
| 181 // Due to the way that handlers are (re)initialized under certain types of | |
| 182 // navigation, we may not be initialized yet. (See bugs 88986 and 86448.) | |
| 183 // If this is the case, initialize on demand. This is a hack. | |
| 184 // TODO(mdm): remove this hack once it is no longer necessary. | |
| 185 if (show_passwords_.GetPrefName().empty()) | |
| 186 InitializeHandler(); | |
| 187 | |
| 188 ListValue entries; | |
| 189 bool show_passwords = *show_passwords_; | |
| 190 string16 empty; | |
| 191 for (size_t i = 0; i < password_list_.size(); ++i) { | |
| 192 ListValue* entry = new ListValue(); | |
| 193 entry->Append(new StringValue(net::FormatUrl(password_list_[i]->origin, | |
| 194 languages_))); | |
| 195 entry->Append(new StringValue(password_list_[i]->username_value)); | |
| 196 entry->Append(new StringValue( | |
| 197 show_passwords ? password_list_[i]->password_value : empty)); | |
| 198 entries.Append(entry); | |
| 199 } | |
| 200 | |
| 201 web_ui()->CallJavascriptFunction("PasswordManager.setSavedPasswordsList", | |
| 202 entries); | |
| 203 } | |
| 204 | |
| 205 void PasswordManagerHandler::SetPasswordExceptionList() { | |
| 206 ListValue entries; | |
| 207 for (size_t i = 0; i < password_exception_list_.size(); ++i) { | |
| 208 entries.Append(new StringValue( | |
| 209 net::FormatUrl(password_exception_list_[i]->origin, languages_))); | |
| 210 } | |
| 211 | |
| 212 web_ui()->CallJavascriptFunction("PasswordManager.setPasswordExceptionsList", | |
| 213 entries); | |
| 214 } | |
| 215 | |
| 216 PasswordManagerHandler::ListPopulater::ListPopulater( | |
| 217 PasswordManagerHandler* page) | |
| 218 : page_(page), | |
| 219 pending_login_query_(0) { | |
| 220 } | |
| 221 | |
| 222 PasswordManagerHandler::ListPopulater::~ListPopulater() { | |
| 223 } | |
| 224 | |
| 225 PasswordManagerHandler::PasswordListPopulater::PasswordListPopulater( | |
| 226 PasswordManagerHandler* page) : ListPopulater(page) { | |
| 227 } | |
| 228 | |
| 229 void PasswordManagerHandler::PasswordListPopulater::Populate() { | |
| 230 PasswordStore* store = page_->GetPasswordStore(); | |
| 231 if (store != NULL) { | |
| 232 if (pending_login_query_) | |
| 233 store->CancelRequest(pending_login_query_); | |
| 234 | |
| 235 pending_login_query_ = store->GetAutofillableLogins(this); | |
| 236 } else { | |
| 237 LOG(ERROR) << "No password store! Cannot display passwords."; | |
| 238 } | |
| 239 } | |
| 240 | |
| 241 void PasswordManagerHandler::PasswordListPopulater:: | |
| 242 OnPasswordStoreRequestDone( | |
| 243 CancelableRequestProvider::Handle handle, | |
| 244 const std::vector<webkit::forms::PasswordForm*>& result) { | |
| 245 DCHECK_EQ(pending_login_query_, handle); | |
| 246 pending_login_query_ = 0; | |
| 247 page_->password_list_.clear(); | |
| 248 page_->password_list_.insert(page_->password_list_.end(), | |
| 249 result.begin(), result.end()); | |
| 250 page_->SetPasswordList(); | |
| 251 } | |
| 252 | |
| 253 PasswordManagerHandler::PasswordExceptionListPopulater:: | |
| 254 PasswordExceptionListPopulater(PasswordManagerHandler* page) | |
| 255 : ListPopulater(page) { | |
| 256 } | |
| 257 | |
| 258 void PasswordManagerHandler::PasswordExceptionListPopulater::Populate() { | |
| 259 PasswordStore* store = page_->GetPasswordStore(); | |
| 260 if (store != NULL) { | |
| 261 if (pending_login_query_) | |
| 262 store->CancelRequest(pending_login_query_); | |
| 263 | |
| 264 pending_login_query_ = store->GetBlacklistLogins(this); | |
| 265 } else { | |
| 266 LOG(ERROR) << "No password store! Cannot display exceptions."; | |
| 267 } | |
| 268 } | |
| 269 | |
| 270 void PasswordManagerHandler::PasswordExceptionListPopulater:: | |
| 271 OnPasswordStoreRequestDone( | |
| 272 CancelableRequestProvider::Handle handle, | |
| 273 const std::vector<webkit::forms::PasswordForm*>& result) { | |
| 274 DCHECK_EQ(pending_login_query_, handle); | |
| 275 pending_login_query_ = 0; | |
| 276 page_->password_exception_list_.clear(); | |
| 277 page_->password_exception_list_.insert(page_->password_exception_list_.end(), | |
| 278 result.begin(), result.end()); | |
| 279 page_->SetPasswordExceptionList(); | |
| 280 } | |
| 281 | |
| 282 } // namespace options | |
| OLD | NEW |