OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/passwords/password_manager_presenter.h" | 5 #include "chrome/browser/ui/passwords/password_manager_presenter.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
70 return "2"; | 70 return "2"; |
71 } | 71 } |
72 | 72 |
73 // Creates key for sorting password or password exception entries. | 73 // Creates key for sorting password or password exception entries. |
74 // The key is eTLD+1 followed by subdomains | 74 // The key is eTLD+1 followed by subdomains |
75 // (e.g. secure.accounts.example.com => example.com.accounts.secure). | 75 // (e.g. secure.accounts.example.com => example.com.accounts.secure). |
76 // If |username_and_password_in_key == true|, username and password is appended | 76 // If |username_and_password_in_key == true|, username and password is appended |
77 // to the key. The entry type code (non-Android, Android w/ or w/o affiliated | 77 // to the key. The entry type code (non-Android, Android w/ or w/o affiliated |
78 // web realm) is also appended to the key. | 78 // web realm) is also appended to the key. |
79 std::string CreateSortKey(const autofill::PasswordForm& form, | 79 std::string CreateSortKey(const autofill::PasswordForm& form, |
80 const std::string& languages, | |
81 bool username_and_password_in_key) { | 80 bool username_and_password_in_key) { |
82 bool is_android_uri = false; | 81 bool is_android_uri = false; |
83 bool is_clickable = false; | 82 bool is_clickable = false; |
84 GURL link_url; | 83 GURL link_url; |
85 std::string origin = password_manager::GetShownOriginAndLinkUrl( | 84 std::string origin = password_manager::GetShownOriginAndLinkUrl( |
86 form, languages, &is_android_uri, &link_url, &is_clickable); | 85 form, &is_android_uri, &link_url, &is_clickable); |
87 | 86 |
88 if (!is_clickable) { // e.g. android://com.example.r => r.example.com. | 87 if (!is_clickable) { // e.g. android://com.example.r => r.example.com. |
89 origin = SplitByDotAndReverse( | 88 origin = SplitByDotAndReverse( |
90 StringPiece(&origin[kAndroidAppSchemeAndDelimiterLength], | 89 StringPiece(&origin[kAndroidAppSchemeAndDelimiterLength], |
91 origin.length() - kAndroidAppSchemeAndDelimiterLength)); | 90 origin.length() - kAndroidAppSchemeAndDelimiterLength)); |
92 } | 91 } |
93 | 92 |
94 std::string site_name = | 93 std::string site_name = |
95 net::registry_controlled_domains::GetDomainAndRegistry( | 94 net::registry_controlled_domains::GetDomainAndRegistry( |
96 origin, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); | 95 origin, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES); |
(...skipping 12 matching lines...) Expand all Loading... |
109 // Since Android and non-Android entries shouldn't be merged into one entry, | 108 // Since Android and non-Android entries shouldn't be merged into one entry, |
110 // add the entry type code to the sort key. | 109 // add the entry type code to the sort key. |
111 key += | 110 key += |
112 kSortKeyPartsSeparator + GetEntryTypeCode(is_android_uri, is_clickable); | 111 kSortKeyPartsSeparator + GetEntryTypeCode(is_android_uri, is_clickable); |
113 return key; | 112 return key; |
114 } | 113 } |
115 | 114 |
116 // Finds duplicates of |form| in |duplicates|, removes them from |store| and | 115 // Finds duplicates of |form| in |duplicates|, removes them from |store| and |
117 // from |duplicates|. | 116 // from |duplicates|. |
118 void RemoveDuplicates(const autofill::PasswordForm& form, | 117 void RemoveDuplicates(const autofill::PasswordForm& form, |
119 const std::string& languages, | |
120 DuplicatesMap* duplicates, | 118 DuplicatesMap* duplicates, |
121 PasswordStore* store, | 119 PasswordStore* store, |
122 bool username_and_password_in_key) { | 120 bool username_and_password_in_key) { |
123 std::string key = | 121 std::string key = |
124 CreateSortKey(form, languages, username_and_password_in_key); | 122 CreateSortKey(form, username_and_password_in_key); |
125 std::pair<DuplicatesMap::iterator, DuplicatesMap::iterator> dups = | 123 std::pair<DuplicatesMap::iterator, DuplicatesMap::iterator> dups = |
126 duplicates->equal_range(key); | 124 duplicates->equal_range(key); |
127 for (DuplicatesMap::iterator it = dups.first; it != dups.second; ++it) | 125 for (DuplicatesMap::iterator it = dups.first; it != dups.second; ++it) |
128 store->RemoveLogin(*it->second); | 126 store->RemoveLogin(*it->second); |
129 duplicates->erase(key); | 127 duplicates->erase(key); |
130 } | 128 } |
131 | 129 |
132 } // namespace | 130 } // namespace |
133 | 131 |
134 PasswordManagerPresenter::PasswordManagerPresenter( | 132 PasswordManagerPresenter::PasswordManagerPresenter( |
135 PasswordUIView* password_view) | 133 PasswordUIView* password_view) |
136 : populater_(this), | 134 : populater_(this), |
137 exception_populater_(this), | 135 exception_populater_(this), |
138 password_view_(password_view) { | 136 password_view_(password_view) { |
139 DCHECK(password_view_); | 137 DCHECK(password_view_); |
140 } | 138 } |
141 | 139 |
142 PasswordManagerPresenter::~PasswordManagerPresenter() { | 140 PasswordManagerPresenter::~PasswordManagerPresenter() { |
143 PasswordStore* store = GetPasswordStore(); | 141 PasswordStore* store = GetPasswordStore(); |
144 if (store) | 142 if (store) |
145 store->RemoveObserver(this); | 143 store->RemoveObserver(this); |
146 } | 144 } |
147 | 145 |
148 void PasswordManagerPresenter::Initialize() { | 146 void PasswordManagerPresenter::Initialize() { |
149 PasswordStore* store = GetPasswordStore(); | 147 PasswordStore* store = GetPasswordStore(); |
150 if (store) | 148 if (store) |
151 store->AddObserver(this); | 149 store->AddObserver(this); |
152 | |
153 languages_ = password_view_->GetProfile()->GetPrefs()-> | |
154 GetString(prefs::kAcceptLanguages); | |
155 } | 150 } |
156 | 151 |
157 void PasswordManagerPresenter::OnLoginsChanged( | 152 void PasswordManagerPresenter::OnLoginsChanged( |
158 const password_manager::PasswordStoreChangeList& changes) { | 153 const password_manager::PasswordStoreChangeList& changes) { |
159 // Entire list is updated for convenience. | 154 // Entire list is updated for convenience. |
160 UpdatePasswordLists(); | 155 UpdatePasswordLists(); |
161 } | 156 } |
162 | 157 |
163 PasswordStore* PasswordManagerPresenter::GetPasswordStore() { | 158 PasswordStore* PasswordManagerPresenter::GetPasswordStore() { |
164 return PasswordStoreFactory::GetForProfile(password_view_->GetProfile(), | 159 return PasswordStoreFactory::GetForProfile(password_view_->GetProfile(), |
(...skipping 19 matching lines...) Expand all Loading... |
184 if (index >= password_list_.size()) { | 179 if (index >= password_list_.size()) { |
185 // |index| out of bounds might come from a compromised renderer, don't let | 180 // |index| out of bounds might come from a compromised renderer, don't let |
186 // it crash the browser. http://crbug.com/362054 | 181 // it crash the browser. http://crbug.com/362054 |
187 NOTREACHED(); | 182 NOTREACHED(); |
188 return; | 183 return; |
189 } | 184 } |
190 PasswordStore* store = GetPasswordStore(); | 185 PasswordStore* store = GetPasswordStore(); |
191 if (!store) | 186 if (!store) |
192 return; | 187 return; |
193 | 188 |
194 RemoveDuplicates(*password_list_[index], languages_, &password_duplicates_, | 189 RemoveDuplicates(*password_list_[index], &password_duplicates_, |
195 store, true); | 190 store, true); |
196 store->RemoveLogin(*password_list_[index]); | 191 store->RemoveLogin(*password_list_[index]); |
197 content::RecordAction( | 192 content::RecordAction( |
198 base::UserMetricsAction("PasswordManager_RemoveSavedPassword")); | 193 base::UserMetricsAction("PasswordManager_RemoveSavedPassword")); |
199 } | 194 } |
200 | 195 |
201 void PasswordManagerPresenter::RemovePasswordException(size_t index) { | 196 void PasswordManagerPresenter::RemovePasswordException(size_t index) { |
202 if (index >= password_exception_list_.size()) { | 197 if (index >= password_exception_list_.size()) { |
203 // |index| out of bounds might come from a compromised renderer, don't let | 198 // |index| out of bounds might come from a compromised renderer, don't let |
204 // it crash the browser. http://crbug.com/362054 | 199 // it crash the browser. http://crbug.com/362054 |
205 NOTREACHED(); | 200 NOTREACHED(); |
206 return; | 201 return; |
207 } | 202 } |
208 PasswordStore* store = GetPasswordStore(); | 203 PasswordStore* store = GetPasswordStore(); |
209 if (!store) | 204 if (!store) |
210 return; | 205 return; |
211 RemoveDuplicates(*password_exception_list_[index], languages_, | 206 RemoveDuplicates(*password_exception_list_[index], |
212 &password_exception_duplicates_, store, false); | 207 &password_exception_duplicates_, store, false); |
213 store->RemoveLogin(*password_exception_list_[index]); | 208 store->RemoveLogin(*password_exception_list_[index]); |
214 content::RecordAction( | 209 content::RecordAction( |
215 base::UserMetricsAction("PasswordManager_RemovePasswordException")); | 210 base::UserMetricsAction("PasswordManager_RemovePasswordException")); |
216 } | 211 } |
217 | 212 |
218 void PasswordManagerPresenter::RequestShowPassword(size_t index) { | 213 void PasswordManagerPresenter::RequestShowPassword(size_t index) { |
219 #if !defined(OS_ANDROID) // This is never called on Android. | 214 #if !defined(OS_ANDROID) // This is never called on Android. |
220 if (index >= password_list_.size()) { | 215 if (index >= password_list_.size()) { |
221 // |index| out of bounds might come from a compromised renderer, don't let | 216 // |index| out of bounds might come from a compromised renderer, don't let |
(...skipping 24 matching lines...) Expand all Loading... |
246 } | 241 } |
247 if (password_manager::sync_util::IsSyncAccountCredential( | 242 if (password_manager::sync_util::IsSyncAccountCredential( |
248 *password_list_[index], sync_service, | 243 *password_list_[index], sync_service, |
249 SigninManagerFactory::GetForProfile(password_view_->GetProfile()))) { | 244 SigninManagerFactory::GetForProfile(password_view_->GetProfile()))) { |
250 content::RecordAction( | 245 content::RecordAction( |
251 base::UserMetricsAction("PasswordManager_SyncCredentialShown")); | 246 base::UserMetricsAction("PasswordManager_SyncCredentialShown")); |
252 } | 247 } |
253 | 248 |
254 // Call back the front end to reveal the password. | 249 // Call back the front end to reveal the password. |
255 std::string origin_url = password_manager::GetHumanReadableOrigin( | 250 std::string origin_url = password_manager::GetHumanReadableOrigin( |
256 *password_list_[index], languages_); | 251 *password_list_[index]); |
257 password_view_->ShowPassword( | 252 password_view_->ShowPassword( |
258 index, | 253 index, |
259 origin_url, | 254 origin_url, |
260 base::UTF16ToUTF8(password_list_[index]->username_value), | 255 base::UTF16ToUTF8(password_list_[index]->username_value), |
261 password_list_[index]->password_value); | 256 password_list_[index]->password_value); |
262 #endif | 257 #endif |
263 } | 258 } |
264 | 259 |
265 const autofill::PasswordForm* PasswordManagerPresenter::GetPassword( | 260 const autofill::PasswordForm* PasswordManagerPresenter::GetPassword( |
266 size_t index) { | 261 size_t index) { |
(...skipping 19 matching lines...) Expand all Loading... |
286 | 281 |
287 void PasswordManagerPresenter::SetPasswordList() { | 282 void PasswordManagerPresenter::SetPasswordList() { |
288 password_view_->SetPasswordList(password_list_); | 283 password_view_->SetPasswordList(password_list_); |
289 } | 284 } |
290 | 285 |
291 void PasswordManagerPresenter::SetPasswordExceptionList() { | 286 void PasswordManagerPresenter::SetPasswordExceptionList() { |
292 password_view_->SetPasswordExceptionList(password_exception_list_); | 287 password_view_->SetPasswordExceptionList(password_exception_list_); |
293 } | 288 } |
294 | 289 |
295 void PasswordManagerPresenter::SortEntriesAndHideDuplicates( | 290 void PasswordManagerPresenter::SortEntriesAndHideDuplicates( |
296 const std::string& languages, | |
297 std::vector<scoped_ptr<autofill::PasswordForm>>* list, | 291 std::vector<scoped_ptr<autofill::PasswordForm>>* list, |
298 DuplicatesMap* duplicates, | 292 DuplicatesMap* duplicates, |
299 bool username_and_password_in_key) { | 293 bool username_and_password_in_key) { |
300 std::vector<std::pair<std::string, scoped_ptr<autofill::PasswordForm>>> pairs; | 294 std::vector<std::pair<std::string, scoped_ptr<autofill::PasswordForm>>> pairs; |
301 pairs.reserve(list->size()); | 295 pairs.reserve(list->size()); |
302 for (auto& form : *list) { | 296 for (auto& form : *list) { |
303 pairs.push_back(std::make_pair( | 297 pairs.push_back(std::make_pair( |
304 CreateSortKey(*form, languages, username_and_password_in_key), | 298 CreateSortKey(*form, username_and_password_in_key), std::move(form))); |
305 std::move(form))); | |
306 } | 299 } |
307 | 300 |
308 std::sort( | 301 std::sort( |
309 pairs.begin(), pairs.end(), | 302 pairs.begin(), pairs.end(), |
310 [](const std::pair<std::string, scoped_ptr<autofill::PasswordForm>>& left, | 303 [](const std::pair<std::string, scoped_ptr<autofill::PasswordForm>>& left, |
311 const std::pair<std::string, scoped_ptr<autofill::PasswordForm>>& | 304 const std::pair<std::string, scoped_ptr<autofill::PasswordForm>>& |
312 right) { return left.first < right.first; }); | 305 right) { return left.first < right.first; }); |
313 | 306 |
314 list->clear(); | 307 list->clear(); |
315 duplicates->clear(); | 308 duplicates->clear(); |
(...skipping 26 matching lines...) Expand all Loading... |
342 store->GetAutofillableLoginsWithAffiliatedRealms(this); | 335 store->GetAutofillableLoginsWithAffiliatedRealms(this); |
343 } else { | 336 } else { |
344 LOG(ERROR) << "No password store! Cannot display passwords."; | 337 LOG(ERROR) << "No password store! Cannot display passwords."; |
345 } | 338 } |
346 } | 339 } |
347 | 340 |
348 void PasswordManagerPresenter::PasswordListPopulater::OnGetPasswordStoreResults( | 341 void PasswordManagerPresenter::PasswordListPopulater::OnGetPasswordStoreResults( |
349 ScopedVector<autofill::PasswordForm> results) { | 342 ScopedVector<autofill::PasswordForm> results) { |
350 page_->password_list_ = | 343 page_->password_list_ = |
351 password_manager_util::ConvertScopedVector(std::move(results)); | 344 password_manager_util::ConvertScopedVector(std::move(results)); |
352 page_->SortEntriesAndHideDuplicates(page_->languages_, &page_->password_list_, | 345 page_->SortEntriesAndHideDuplicates(&page_->password_list_, |
353 &page_->password_duplicates_, | 346 &page_->password_duplicates_, |
354 true /* use username and password */); | 347 true /* use username and password */); |
355 page_->SetPasswordList(); | 348 page_->SetPasswordList(); |
356 } | 349 } |
357 | 350 |
358 PasswordManagerPresenter::PasswordExceptionListPopulater:: | 351 PasswordManagerPresenter::PasswordExceptionListPopulater:: |
359 PasswordExceptionListPopulater(PasswordManagerPresenter* page) | 352 PasswordExceptionListPopulater(PasswordManagerPresenter* page) |
360 : ListPopulater(page) { | 353 : ListPopulater(page) { |
361 } | 354 } |
362 | 355 |
363 void PasswordManagerPresenter::PasswordExceptionListPopulater::Populate() { | 356 void PasswordManagerPresenter::PasswordExceptionListPopulater::Populate() { |
364 PasswordStore* store = page_->GetPasswordStore(); | 357 PasswordStore* store = page_->GetPasswordStore(); |
365 if (store != NULL) { | 358 if (store != NULL) { |
366 cancelable_task_tracker()->TryCancelAll(); | 359 cancelable_task_tracker()->TryCancelAll(); |
367 store->GetBlacklistLoginsWithAffiliatedRealms(this); | 360 store->GetBlacklistLoginsWithAffiliatedRealms(this); |
368 } else { | 361 } else { |
369 LOG(ERROR) << "No password store! Cannot display exceptions."; | 362 LOG(ERROR) << "No password store! Cannot display exceptions."; |
370 } | 363 } |
371 } | 364 } |
372 | 365 |
373 void PasswordManagerPresenter::PasswordExceptionListPopulater:: | 366 void PasswordManagerPresenter::PasswordExceptionListPopulater:: |
374 OnGetPasswordStoreResults(ScopedVector<autofill::PasswordForm> results) { | 367 OnGetPasswordStoreResults(ScopedVector<autofill::PasswordForm> results) { |
375 page_->password_exception_list_ = | 368 page_->password_exception_list_ = |
376 password_manager_util::ConvertScopedVector(std::move(results)); | 369 password_manager_util::ConvertScopedVector(std::move(results)); |
377 page_->SortEntriesAndHideDuplicates( | 370 page_->SortEntriesAndHideDuplicates(&page_->password_exception_list_, |
378 page_->languages_, &page_->password_exception_list_, | |
379 &page_->password_exception_duplicates_, | 371 &page_->password_exception_duplicates_, |
380 false /* don't use username and password*/); | 372 false /* don't use username and password*/); |
381 page_->SetPasswordExceptionList(); | 373 page_->SetPasswordExceptionList(); |
382 } | 374 } |
OLD | NEW |