| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/dom_ui/options/passwords_exceptions_handler.h" | 5 #include "chrome/browser/dom_ui/options/password_manager_handler.h" |
| 6 | 6 |
| 7 #include "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/stl_util-inl.h" | 9 #include "base/stl_util-inl.h" |
| 10 #include "base/string_number_conversions.h" | 10 #include "base/string_number_conversions.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/prefs/pref_service.h" | 13 #include "chrome/browser/prefs/pref_service.h" |
| 14 #include "chrome/browser/profiles/profile.h" | 14 #include "chrome/browser/profiles/profile.h" |
| 15 #include "chrome/common/pref_names.h" | 15 #include "chrome/common/pref_names.h" |
| 16 #include "chrome/common/url_constants.h" | 16 #include "chrome/common/url_constants.h" |
| 17 #include "grit/chromium_strings.h" | 17 #include "grit/chromium_strings.h" |
| 18 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
| 19 #include "net/base/net_util.h" | 19 #include "net/base/net_util.h" |
| 20 | 20 |
| 21 PasswordsExceptionsHandler::PasswordsExceptionsHandler() | 21 PasswordManagerHandler::PasswordManagerHandler() |
| 22 : ALLOW_THIS_IN_INITIALIZER_LIST(populater_(this)), | 22 : ALLOW_THIS_IN_INITIALIZER_LIST(populater_(this)), |
| 23 ALLOW_THIS_IN_INITIALIZER_LIST(exception_populater_(this)) { | 23 ALLOW_THIS_IN_INITIALIZER_LIST(exception_populater_(this)) { |
| 24 } | 24 } |
| 25 | 25 |
| 26 PasswordsExceptionsHandler::~PasswordsExceptionsHandler() { | 26 PasswordManagerHandler::~PasswordManagerHandler() { |
| 27 } | 27 } |
| 28 | 28 |
| 29 void PasswordsExceptionsHandler::GetLocalizedValues( | 29 void PasswordManagerHandler::GetLocalizedValues( |
| 30 DictionaryValue* localized_strings) { | 30 DictionaryValue* localized_strings) { |
| 31 DCHECK(localized_strings); | 31 DCHECK(localized_strings); |
| 32 | 32 |
| 33 localized_strings->SetString("savedPasswordsExceptionsTitle", | 33 localized_strings->SetString("savedPasswordsTitle", |
| 34 l10n_util::GetStringUTF16(IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE)); | 34 l10n_util::GetStringUTF16(IDS_PASSWORDS_EXCEPTIONS_WINDOW_TITLE)); |
| 35 localized_strings->SetString("passwordsTabTitle", | 35 localized_strings->SetString("passwordsTabTitle", |
| 36 l10n_util::GetStringUTF16(IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE)); | 36 l10n_util::GetStringUTF16(IDS_PASSWORDS_SHOW_PASSWORDS_TAB_TITLE)); |
| 37 localized_strings->SetString("passwordExceptionsTabTitle", | 37 localized_strings->SetString("passwordExceptionsTabTitle", |
| 38 l10n_util::GetStringUTF16(IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE)); | 38 l10n_util::GetStringUTF16(IDS_PASSWORDS_EXCEPTIONS_TAB_TITLE)); |
| 39 localized_strings->SetString("passwordsSiteColumn", | 39 localized_strings->SetString("passwordsSiteColumn", |
| 40 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_SITE_COLUMN)); | 40 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_SITE_COLUMN)); |
| 41 localized_strings->SetString("passwordsUsernameColumn", | 41 localized_strings->SetString("passwordsUsernameColumn", |
| 42 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_USERNAME_COLUMN)); | 42 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_USERNAME_COLUMN)); |
| 43 localized_strings->SetString("passwordsRemoveButton", | 43 localized_strings->SetString("passwordsRemoveButton", |
| 44 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_REMOVE_BUTTON)); | 44 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_REMOVE_BUTTON)); |
| 45 localized_strings->SetString("passwordsRemoveAllButton", | 45 localized_strings->SetString("passwordsRemoveAllButton", |
| 46 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_REMOVE_ALL_BUTTON)); | 46 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_REMOVE_ALL_BUTTON)); |
| 47 localized_strings->SetString("passwordsShowButton", | 47 localized_strings->SetString("passwordsShowButton", |
| 48 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON)); | 48 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_SHOW_BUTTON)); |
| 49 localized_strings->SetString("passwordsHideButton", | 49 localized_strings->SetString("passwordsHideButton", |
| 50 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON)); | 50 l10n_util::GetStringUTF16(IDS_PASSWORDS_PAGE_VIEW_HIDE_BUTTON)); |
| 51 localized_strings->SetString("passwordsRemoveAllTitle", | 51 localized_strings->SetString("passwordsRemoveAllTitle", |
| 52 l10n_util::GetStringUTF16( | 52 l10n_util::GetStringUTF16( |
| 53 IDS_PASSWORDS_PAGE_VIEW_CAPTION_DELETE_ALL_PASSWORDS)); | 53 IDS_PASSWORDS_PAGE_VIEW_CAPTION_DELETE_ALL_PASSWORDS)); |
| 54 localized_strings->SetString("passwordsRemoveAllWarning", | 54 localized_strings->SetString("passwordsRemoveAllWarning", |
| 55 l10n_util::GetStringUTF16( | 55 l10n_util::GetStringUTF16( |
| 56 IDS_PASSWORDS_PAGE_VIEW_TEXT_DELETE_ALL_PASSWORDS)); | 56 IDS_PASSWORDS_PAGE_VIEW_TEXT_DELETE_ALL_PASSWORDS)); |
| 57 } | 57 } |
| 58 | 58 |
| 59 void PasswordsExceptionsHandler::Initialize() { | 59 void PasswordManagerHandler::Initialize() { |
| 60 // We should not cache dom_ui_->GetProfile(). See crosbug.com/6304. | 60 // We should not cache dom_ui_->GetProfile(). See crosbug.com/6304. |
| 61 } | 61 } |
| 62 | 62 |
| 63 void PasswordsExceptionsHandler::RegisterMessages() { | 63 void PasswordManagerHandler::RegisterMessages() { |
| 64 DCHECK(dom_ui_); | 64 DCHECK(dom_ui_); |
| 65 | 65 |
| 66 dom_ui_->RegisterMessageCallback("loadLists", | 66 dom_ui_->RegisterMessageCallback("loadLists", |
| 67 NewCallback(this, &PasswordsExceptionsHandler::LoadLists)); | 67 NewCallback(this, &PasswordManagerHandler::LoadLists)); |
| 68 dom_ui_->RegisterMessageCallback("removeSavedPassword", | 68 dom_ui_->RegisterMessageCallback("removeSavedPassword", |
| 69 NewCallback(this, &PasswordsExceptionsHandler::RemoveSavedPassword)); | 69 NewCallback(this, &PasswordManagerHandler::RemoveSavedPassword)); |
| 70 dom_ui_->RegisterMessageCallback("removePasswordException", | 70 dom_ui_->RegisterMessageCallback("removePasswordException", |
| 71 NewCallback(this, &PasswordsExceptionsHandler::RemovePasswordException)); | 71 NewCallback(this, &PasswordManagerHandler::RemovePasswordException)); |
| 72 dom_ui_->RegisterMessageCallback("removeAllSavedPasswords", | 72 dom_ui_->RegisterMessageCallback("removeAllSavedPasswords", |
| 73 NewCallback(this, &PasswordsExceptionsHandler::RemoveAllSavedPasswords)); | 73 NewCallback(this, &PasswordManagerHandler::RemoveAllSavedPasswords)); |
| 74 dom_ui_->RegisterMessageCallback("removeAllPasswordExceptions", NewCallback( | 74 dom_ui_->RegisterMessageCallback("removeAllPasswordExceptions", NewCallback( |
| 75 this, &PasswordsExceptionsHandler::RemoveAllPasswordExceptions)); | 75 this, &PasswordManagerHandler::RemoveAllPasswordExceptions)); |
| 76 dom_ui_->RegisterMessageCallback("showSelectedPassword", | 76 dom_ui_->RegisterMessageCallback("showSelectedPassword", |
| 77 NewCallback(this, &PasswordsExceptionsHandler::ShowSelectedPassword)); | 77 NewCallback(this, &PasswordManagerHandler::ShowSelectedPassword)); |
| 78 } | 78 } |
| 79 | 79 |
| 80 PasswordStore* PasswordsExceptionsHandler::GetPasswordStore() { | 80 PasswordStore* PasswordManagerHandler::GetPasswordStore() { |
| 81 return dom_ui_->GetProfile()->GetPasswordStore(Profile::EXPLICIT_ACCESS); | 81 return dom_ui_->GetProfile()->GetPasswordStore(Profile::EXPLICIT_ACCESS); |
| 82 } | 82 } |
| 83 | 83 |
| 84 void PasswordsExceptionsHandler::LoadLists(const ListValue* args) { | 84 void PasswordManagerHandler::LoadLists(const ListValue* args) { |
| 85 languages_ = | 85 languages_ = |
| 86 dom_ui_->GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); | 86 dom_ui_->GetProfile()->GetPrefs()->GetString(prefs::kAcceptLanguages); |
| 87 populater_.Populate(); | 87 populater_.Populate(); |
| 88 exception_populater_.Populate(); | 88 exception_populater_.Populate(); |
| 89 } | 89 } |
| 90 | 90 |
| 91 void PasswordsExceptionsHandler::RemoveSavedPassword(const ListValue* args) { | 91 void PasswordManagerHandler::RemoveSavedPassword(const ListValue* args) { |
| 92 std::string string_value = WideToUTF8(ExtractStringValue(args)); | 92 std::string string_value = WideToUTF8(ExtractStringValue(args)); |
| 93 int index; | 93 int index; |
| 94 base::StringToInt(string_value, &index); | 94 base::StringToInt(string_value, &index); |
| 95 | 95 |
| 96 GetPasswordStore()->RemoveLogin(*password_list_[index]); | 96 GetPasswordStore()->RemoveLogin(*password_list_[index]); |
| 97 delete password_list_[index]; | 97 delete password_list_[index]; |
| 98 password_list_.erase(password_list_.begin() + index); | 98 password_list_.erase(password_list_.begin() + index); |
| 99 SetPasswordList(); | 99 SetPasswordList(); |
| 100 } | 100 } |
| 101 | 101 |
| 102 void PasswordsExceptionsHandler::RemovePasswordException( | 102 void PasswordManagerHandler::RemovePasswordException( |
| 103 const ListValue* args) { | 103 const ListValue* args) { |
| 104 std::string string_value = WideToUTF8(ExtractStringValue(args)); | 104 std::string string_value = WideToUTF8(ExtractStringValue(args)); |
| 105 int index; | 105 int index; |
| 106 base::StringToInt(string_value, &index); | 106 base::StringToInt(string_value, &index); |
| 107 | 107 |
| 108 GetPasswordStore()->RemoveLogin(*password_exception_list_[index]); | 108 GetPasswordStore()->RemoveLogin(*password_exception_list_[index]); |
| 109 delete password_exception_list_[index]; | 109 delete password_exception_list_[index]; |
| 110 password_exception_list_.erase(password_exception_list_.begin() + index); | 110 password_exception_list_.erase(password_exception_list_.begin() + index); |
| 111 SetPasswordExceptionList(); | 111 SetPasswordExceptionList(); |
| 112 } | 112 } |
| 113 | 113 |
| 114 void PasswordsExceptionsHandler::RemoveAllSavedPasswords( | 114 void PasswordManagerHandler::RemoveAllSavedPasswords( |
| 115 const ListValue* args) { | 115 const ListValue* args) { |
| 116 PasswordStore* store = GetPasswordStore(); | 116 PasswordStore* store = GetPasswordStore(); |
| 117 for (size_t i = 0; i < password_list_.size(); ++i) | 117 for (size_t i = 0; i < password_list_.size(); ++i) |
| 118 store->RemoveLogin(*password_list_[i]); | 118 store->RemoveLogin(*password_list_[i]); |
| 119 STLDeleteElements(&password_list_); | 119 STLDeleteElements(&password_list_); |
| 120 SetPasswordList(); | 120 SetPasswordList(); |
| 121 } | 121 } |
| 122 | 122 |
| 123 void PasswordsExceptionsHandler::RemoveAllPasswordExceptions( | 123 void PasswordManagerHandler::RemoveAllPasswordExceptions( |
| 124 const ListValue* args) { | 124 const ListValue* args) { |
| 125 PasswordStore* store = GetPasswordStore(); | 125 PasswordStore* store = GetPasswordStore(); |
| 126 for (size_t i = 0; i < password_exception_list_.size(); ++i) | 126 for (size_t i = 0; i < password_exception_list_.size(); ++i) |
| 127 store->RemoveLogin(*password_exception_list_[i]); | 127 store->RemoveLogin(*password_exception_list_[i]); |
| 128 STLDeleteElements(&password_exception_list_); | 128 STLDeleteElements(&password_exception_list_); |
| 129 SetPasswordExceptionList(); | 129 SetPasswordExceptionList(); |
| 130 } | 130 } |
| 131 | 131 |
| 132 void PasswordsExceptionsHandler::ShowSelectedPassword(const ListValue* args) { | 132 void PasswordManagerHandler::ShowSelectedPassword(const ListValue* args) { |
| 133 std::string string_value = WideToUTF8(ExtractStringValue(args)); | 133 std::string string_value = WideToUTF8(ExtractStringValue(args)); |
| 134 int index; | 134 int index; |
| 135 base::StringToInt(string_value, &index); | 135 base::StringToInt(string_value, &index); |
| 136 | 136 |
| 137 std::string pass = UTF16ToUTF8(password_list_[index]->password_value); | 137 std::string pass = UTF16ToUTF8(password_list_[index]->password_value); |
| 138 scoped_ptr<Value> password_string(Value::CreateStringValue(pass)); | 138 scoped_ptr<Value> password_string(Value::CreateStringValue(pass)); |
| 139 dom_ui_->CallJavascriptFunction( | 139 dom_ui_->CallJavascriptFunction( |
| 140 L"PasswordsExceptions.selectedPasswordCallback", *password_string.get()); | 140 L"PasswordManager.selectedPasswordCallback", *password_string.get()); |
| 141 } | 141 } |
| 142 | 142 |
| 143 void PasswordsExceptionsHandler::SetPasswordList() { | 143 void PasswordManagerHandler::SetPasswordList() { |
| 144 ListValue entries; | 144 ListValue entries; |
| 145 for (size_t i = 0; i < password_list_.size(); ++i) { | 145 for (size_t i = 0; i < password_list_.size(); ++i) { |
| 146 ListValue* entry = new ListValue(); | 146 ListValue* entry = new ListValue(); |
| 147 entry->Append(new StringValue(net::FormatUrl(password_list_[i]->origin, | 147 entry->Append(new StringValue(net::FormatUrl(password_list_[i]->origin, |
| 148 languages_))); | 148 languages_))); |
| 149 entry->Append(new StringValue(password_list_[i]->username_value)); | 149 entry->Append(new StringValue(password_list_[i]->username_value)); |
| 150 entries.Append(entry); | 150 entries.Append(entry); |
| 151 } | 151 } |
| 152 | 152 |
| 153 dom_ui_->CallJavascriptFunction( | 153 dom_ui_->CallJavascriptFunction( |
| 154 L"PasswordsExceptions.setSavedPasswordsList", entries); | 154 L"PasswordManager.setSavedPasswordsList", entries); |
| 155 } | 155 } |
| 156 | 156 |
| 157 void PasswordsExceptionsHandler::SetPasswordExceptionList() { | 157 void PasswordManagerHandler::SetPasswordExceptionList() { |
| 158 ListValue entries; | 158 ListValue entries; |
| 159 for (size_t i = 0; i < password_exception_list_.size(); ++i) { | 159 for (size_t i = 0; i < password_exception_list_.size(); ++i) { |
| 160 entries.Append(new StringValue( | 160 entries.Append(new StringValue( |
| 161 net::FormatUrl(password_exception_list_[i]->origin, languages_))); | 161 net::FormatUrl(password_exception_list_[i]->origin, languages_))); |
| 162 } | 162 } |
| 163 | 163 |
| 164 dom_ui_->CallJavascriptFunction( | 164 dom_ui_->CallJavascriptFunction( |
| 165 L"PasswordsExceptions.setPasswordExceptionsList", entries); | 165 L"PasswordManager.setPasswordExceptionsList", entries); |
| 166 } | 166 } |
| 167 | 167 |
| 168 void PasswordsExceptionsHandler::PasswordListPopulater::Populate() { | 168 void PasswordManagerHandler::PasswordListPopulater::Populate() { |
| 169 DCHECK(!pending_login_query_); | 169 DCHECK(!pending_login_query_); |
| 170 PasswordStore* store = page_->GetPasswordStore(); | 170 PasswordStore* store = page_->GetPasswordStore(); |
| 171 if (store != NULL) | 171 if (store != NULL) |
| 172 pending_login_query_ = store->GetAutofillableLogins(this); | 172 pending_login_query_ = store->GetAutofillableLogins(this); |
| 173 else | 173 else |
| 174 LOG(ERROR) << "No password store! Cannot display passwords."; | 174 LOG(ERROR) << "No password store! Cannot display passwords."; |
| 175 } | 175 } |
| 176 | 176 |
| 177 void PasswordsExceptionsHandler::PasswordListPopulater:: | 177 void PasswordManagerHandler::PasswordListPopulater:: |
| 178 OnPasswordStoreRequestDone(int handle, | 178 OnPasswordStoreRequestDone(int handle, |
| 179 const std::vector<webkit_glue::PasswordForm*>& result) { | 179 const std::vector<webkit_glue::PasswordForm*>& result) { |
| 180 DCHECK_EQ(pending_login_query_, handle); | 180 DCHECK_EQ(pending_login_query_, handle); |
| 181 pending_login_query_ = 0; | 181 pending_login_query_ = 0; |
| 182 page_->password_list_ = result; | 182 page_->password_list_ = result; |
| 183 page_->SetPasswordList(); | 183 page_->SetPasswordList(); |
| 184 } | 184 } |
| 185 | 185 |
| 186 void PasswordsExceptionsHandler::PasswordExceptionListPopulater::Populate() { | 186 void PasswordManagerHandler::PasswordExceptionListPopulater::Populate() { |
| 187 DCHECK(!pending_login_query_); | 187 DCHECK(!pending_login_query_); |
| 188 PasswordStore* store = page_->GetPasswordStore(); | 188 PasswordStore* store = page_->GetPasswordStore(); |
| 189 if (store != NULL) | 189 if (store != NULL) |
| 190 pending_login_query_ = store->GetBlacklistLogins(this); | 190 pending_login_query_ = store->GetBlacklistLogins(this); |
| 191 else | 191 else |
| 192 LOG(ERROR) << "No password store! Cannot display exceptions."; | 192 LOG(ERROR) << "No password store! Cannot display exceptions."; |
| 193 } | 193 } |
| 194 | 194 |
| 195 void PasswordsExceptionsHandler::PasswordExceptionListPopulater:: | 195 void PasswordManagerHandler::PasswordExceptionListPopulater:: |
| 196 OnPasswordStoreRequestDone(int handle, | 196 OnPasswordStoreRequestDone(int handle, |
| 197 const std::vector<webkit_glue::PasswordForm*>& result) { | 197 const std::vector<webkit_glue::PasswordForm*>& result) { |
| 198 DCHECK_EQ(pending_login_query_, handle); | 198 DCHECK_EQ(pending_login_query_, handle); |
| 199 pending_login_query_ = 0; | 199 pending_login_query_ = 0; |
| 200 page_->password_exception_list_ = result; | 200 page_->password_exception_list_ = result; |
| 201 page_->SetPasswordExceptionList(); | 201 page_->SetPasswordExceptionList(); |
| 202 } | 202 } |
| OLD | NEW |