Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: components/autofill/browser/personal_data_manager.cc

Issue 13488009: Remove application locale cache in autofill code. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 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 "components/autofill/browser/personal_data_manager.h" 5 #include "components/autofill/browser/personal_data_manager.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <iterator> 9 #include <iterator>
10 10
11 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/prefs/pref_service.h" 13 #include "base/prefs/pref_service.h"
14 #include "base/strings/string_number_conversions.h" 14 #include "base/strings/string_number_conversions.h"
15 #include "base/utf_string_conversions.h" 15 #include "base/utf_string_conversions.h"
16 #include "chrome/common/chrome_notification_types.h" 16 #include "chrome/common/chrome_notification_types.h"
17 #include "components/autofill/browser/autofill-inl.h" 17 #include "components/autofill/browser/autofill-inl.h"
18 #include "components/autofill/browser/autofill_country.h"
19 #include "components/autofill/browser/autofill_field.h" 18 #include "components/autofill/browser/autofill_field.h"
20 #include "components/autofill/browser/autofill_metrics.h" 19 #include "components/autofill/browser/autofill_metrics.h"
21 #include "components/autofill/browser/form_group.h" 20 #include "components/autofill/browser/form_group.h"
22 #include "components/autofill/browser/form_structure.h" 21 #include "components/autofill/browser/form_structure.h"
23 #include "components/autofill/browser/personal_data_manager_observer.h" 22 #include "components/autofill/browser/personal_data_manager_observer.h"
24 #include "components/autofill/browser/phone_number.h" 23 #include "components/autofill/browser/phone_number.h"
25 #include "components/autofill/browser/phone_number_i18n.h" 24 #include "components/autofill/browser/phone_number_i18n.h"
26 #include "components/autofill/browser/validation.h" 25 #include "components/autofill/browser/validation.h"
27 #include "components/autofill/common/autofill_pref_names.h" 26 #include "components/autofill/common/autofill_pref_names.h"
28 #include "components/user_prefs/user_prefs.h" 27 #include "components/user_prefs/user_prefs.h"
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 // Abandon the import if an email address value shows up in a field that is 102 // Abandon the import if an email address value shows up in a field that is
104 // not an email address. 103 // not an email address.
105 if (field_type != EMAIL_ADDRESS && autofill::IsValidEmailAddress(value)) 104 if (field_type != EMAIL_ADDRESS && autofill::IsValidEmailAddress(value))
106 return false; 105 return false;
107 106
108 return true; 107 return true;
109 } 108 }
110 109
111 } // namespace 110 } // namespace
112 111
113 PersonalDataManager::PersonalDataManager() 112 PersonalDataManager::PersonalDataManager(const std::string& app_locale)
114 : browser_context_(NULL), 113 : browser_context_(NULL),
115 is_data_loaded_(false), 114 is_data_loaded_(false),
116 pending_profiles_query_(0), 115 pending_profiles_query_(0),
117 pending_creditcards_query_(0), 116 pending_creditcards_query_(0),
117 app_locale_(app_locale),
118 metric_logger_(new AutofillMetrics), 118 metric_logger_(new AutofillMetrics),
119 has_logged_profile_count_(false) {} 119 has_logged_profile_count_(false) {}
120 120
121 void PersonalDataManager::Init(BrowserContext* browser_context) { 121 void PersonalDataManager::Init(BrowserContext* browser_context) {
122 browser_context_ = browser_context; 122 browser_context_ = browser_context;
123 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled()); 123 metric_logger_->LogIsAutofillEnabledAtStartup(IsAutofillEnabled());
124 124
125 scoped_refptr<AutofillWebDataService> autofill_data( 125 scoped_refptr<AutofillWebDataService> autofill_data(
126 AutofillWebDataService::FromBrowserContext(browser_context_)); 126 AutofillWebDataService::FromBrowserContext(browser_context_));
127 127
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
211 // possible to import. 211 // possible to import.
212 int importable_credit_card_fields = 0; 212 int importable_credit_card_fields = 0;
213 213
214 // Detect and discard forms with multiple fields of the same type. 214 // Detect and discard forms with multiple fields of the same type.
215 std::set<AutofillFieldType> types_seen; 215 std::set<AutofillFieldType> types_seen;
216 216
217 // We only set complete phone, so aggregate phone parts in these vars and set 217 // We only set complete phone, so aggregate phone parts in these vars and set
218 // complete at the end. 218 // complete at the end.
219 PhoneNumber::PhoneCombineHelper home; 219 PhoneNumber::PhoneCombineHelper home;
220 220
221 const std::string app_locale = AutofillCountry::ApplicationLocale();
222 for (size_t i = 0; i < form.field_count(); ++i) { 221 for (size_t i = 0; i < form.field_count(); ++i) {
223 const AutofillField* field = form.field(i); 222 const AutofillField* field = form.field(i);
224 string16 value = CollapseWhitespace(field->value, false); 223 string16 value = CollapseWhitespace(field->value, false);
225 224
226 // If we don't know the type of the field, or the user hasn't entered any 225 // If we don't know the type of the field, or the user hasn't entered any
227 // information into the field, then skip it. 226 // information into the field, then skip it.
228 if (!field->IsFieldFillable() || value.empty()) 227 if (!field->IsFieldFillable() || value.empty())
229 continue; 228 continue;
230 229
231 AutofillFieldType field_type = field->type(); 230 AutofillFieldType field_type = field->type();
232 FieldTypeGroup group(AutofillType(field_type).group()); 231 FieldTypeGroup group(AutofillType(field_type).group());
233 232
234 // If the |field_type| and |value| don't pass basic validity checks then 233 // If the |field_type| and |value| don't pass basic validity checks then
235 // abandon the import. 234 // abandon the import.
236 if (!IsValidFieldTypeAndValue(types_seen, field_type, value)) { 235 if (!IsValidFieldTypeAndValue(types_seen, field_type, value)) {
237 imported_profile.reset(); 236 imported_profile.reset();
238 local_imported_credit_card.reset(); 237 local_imported_credit_card.reset();
239 break; 238 break;
240 } 239 }
241 240
242 types_seen.insert(field_type); 241 types_seen.insert(field_type);
243 242
244 if (group == AutofillType::CREDIT_CARD) { 243 if (group == AutofillType::CREDIT_CARD) {
245 if (LowerCaseEqualsASCII(field->form_control_type, "month")) { 244 if (LowerCaseEqualsASCII(field->form_control_type, "month")) {
246 DCHECK_EQ(CREDIT_CARD_EXP_MONTH, field_type); 245 DCHECK_EQ(CREDIT_CARD_EXP_MONTH, field_type);
247 local_imported_credit_card->SetInfoForMonthInputType(value); 246 local_imported_credit_card->SetInfoForMonthInputType(value);
248 } else { 247 } else {
249 local_imported_credit_card->SetInfo(field_type, value, app_locale); 248 local_imported_credit_card->SetInfo(field_type, value, app_locale_);
250 } 249 }
251 ++importable_credit_card_fields; 250 ++importable_credit_card_fields;
252 } else { 251 } else {
253 // We need to store phone data in the variables, before building the whole 252 // We need to store phone data in the variables, before building the whole
254 // number at the end. The rest of the fields are set "as is". 253 // number at the end. The rest of the fields are set "as is".
255 // If the fields are not the phone fields in question home.SetInfo() is 254 // If the fields are not the phone fields in question home.SetInfo() is
256 // going to return false. 255 // going to return false.
257 if (!home.SetInfo(field_type, value)) 256 if (!home.SetInfo(field_type, value))
258 imported_profile->SetInfo(field_type, value, app_locale); 257 imported_profile->SetInfo(field_type, value, app_locale_);
259 258
260 // Reject profiles with invalid country information. 259 // Reject profiles with invalid country information.
261 if (field_type == ADDRESS_HOME_COUNTRY && 260 if (field_type == ADDRESS_HOME_COUNTRY &&
262 !value.empty() && imported_profile->CountryCode().empty()) { 261 !value.empty() && imported_profile->CountryCode().empty()) {
263 imported_profile.reset(); 262 imported_profile.reset();
264 break; 263 break;
265 } 264 }
266 } 265 }
267 } 266 }
268 267
269 // Construct the phone number. Reject the profile if the number is invalid. 268 // Construct the phone number. Reject the profile if the number is invalid.
270 if (imported_profile.get() && !home.IsEmpty()) { 269 if (imported_profile.get() && !home.IsEmpty()) {
271 string16 constructed_number; 270 string16 constructed_number;
272 if (!home.ParseNumber(*imported_profile, app_locale, &constructed_number) || 271 if (!home.ParseNumber(*imported_profile, app_locale_,
272 &constructed_number) ||
273 !imported_profile->SetInfo(PHONE_HOME_WHOLE_NUMBER, constructed_number, 273 !imported_profile->SetInfo(PHONE_HOME_WHOLE_NUMBER, constructed_number,
274 app_locale)) { 274 app_locale_)) {
275 imported_profile.reset(); 275 imported_profile.reset();
276 } 276 }
277 } 277 }
278 278
279 // Reject the profile if minimum address and validation requirements are not 279 // Reject the profile if minimum address and validation requirements are not
280 // met. 280 // met.
281 if (imported_profile.get() && !IsValidLearnableProfile(*imported_profile)) 281 if (imported_profile.get() && !IsValidLearnableProfile(*imported_profile))
282 imported_profile.reset(); 282 imported_profile.reset();
283 283
284 // Reject the credit card if we did not detect enough filled credit card 284 // Reject the credit card if we did not detect enough filled credit card
285 // fields or if the credit card number does not seem to be valid. 285 // fields or if the credit card number does not seem to be valid.
286 if (local_imported_credit_card.get() && 286 if (local_imported_credit_card.get() &&
287 !local_imported_credit_card->IsComplete()) { 287 !local_imported_credit_card->IsComplete()) {
288 local_imported_credit_card.reset(); 288 local_imported_credit_card.reset();
289 } 289 }
290 290
291 // Don't import if we already have this info. 291 // Don't import if we already have this info.
292 // Don't present an infobar if we have already saved this card number. 292 // Don't present an infobar if we have already saved this card number.
293 bool merged_credit_card = false; 293 bool merged_credit_card = false;
294 if (local_imported_credit_card.get()) { 294 if (local_imported_credit_card.get()) {
295 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin(); 295 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin();
296 iter != credit_cards_.end(); 296 iter != credit_cards_.end();
297 ++iter) { 297 ++iter) {
298 if ((*iter)->UpdateFromImportedCard(*local_imported_credit_card.get(), 298 if ((*iter)->UpdateFromImportedCard(*local_imported_credit_card.get(),
299 app_locale)) { 299 app_locale_)) {
300 merged_credit_card = true; 300 merged_credit_card = true;
301 UpdateCreditCard(**iter); 301 UpdateCreditCard(**iter);
302 local_imported_credit_card.reset(); 302 local_imported_credit_card.reset();
303 break; 303 break;
304 } 304 }
305 } 305 }
306 } 306 }
307 307
308 if (imported_profile.get()) { 308 if (imported_profile.get()) {
309 // We always save imported profiles. 309 // We always save imported profiles.
310 SaveImportedProfile(*imported_profile); 310 SaveImportedProfile(*imported_profile);
311 } 311 }
312 *imported_credit_card = local_imported_credit_card.release(); 312 *imported_credit_card = local_imported_credit_card.release();
313 313
314 if (imported_profile.get() || *imported_credit_card || merged_credit_card) { 314 if (imported_profile.get() || *imported_credit_card || merged_credit_card) {
315 return true; 315 return true;
316 } else { 316 } else {
317 FOR_EACH_OBSERVER(PersonalDataManagerObserver, observers_, 317 FOR_EACH_OBSERVER(PersonalDataManagerObserver, observers_,
318 OnInsufficientFormData()); 318 OnInsufficientFormData());
319 return false; 319 return false;
320 } 320 }
321 } 321 }
322 322
323 void PersonalDataManager::AddProfile(const AutofillProfile& profile) { 323 void PersonalDataManager::AddProfile(const AutofillProfile& profile) {
324 if (browser_context_->IsOffTheRecord()) 324 if (browser_context_->IsOffTheRecord())
325 return; 325 return;
326 326
327 if (profile.IsEmpty()) 327 if (profile.IsEmpty(app_locale_))
328 return; 328 return;
329 329
330 // Don't add an existing profile. 330 // Don't add an existing profile.
331 if (FindByGUID<AutofillProfile>(web_profiles_, profile.guid())) 331 if (FindByGUID<AutofillProfile>(web_profiles_, profile.guid()))
332 return; 332 return;
333 333
334 scoped_refptr<AutofillWebDataService> autofill_data( 334 scoped_refptr<AutofillWebDataService> autofill_data(
335 AutofillWebDataService::FromBrowserContext(browser_context_)); 335 AutofillWebDataService::FromBrowserContext(browser_context_));
336 if (!autofill_data.get()) 336 if (!autofill_data.get())
337 return; 337 return;
338 338
339 // Don't add a duplicate. 339 // Don't add a duplicate.
340 if (FindByContents(web_profiles_, profile)) 340 if (FindByContents(web_profiles_, profile))
341 return; 341 return;
342 342
343 // Add the new profile to the web database. 343 // Add the new profile to the web database.
344 autofill_data->AddAutofillProfile(profile); 344 autofill_data->AddAutofillProfile(profile);
345 345
346 // Refresh our local cache and send notifications to observers. 346 // Refresh our local cache and send notifications to observers.
347 Refresh(); 347 Refresh();
348 } 348 }
349 349
350 void PersonalDataManager::UpdateProfile(const AutofillProfile& profile) { 350 void PersonalDataManager::UpdateProfile(const AutofillProfile& profile) {
351 if (browser_context_->IsOffTheRecord()) 351 if (browser_context_->IsOffTheRecord())
352 return; 352 return;
353 353
354 if (!FindByGUID<AutofillProfile>(web_profiles_, profile.guid())) 354 if (!FindByGUID<AutofillProfile>(web_profiles_, profile.guid()))
355 return; 355 return;
356 356
357 if (profile.IsEmpty()) { 357 if (profile.IsEmpty(app_locale_)) {
358 RemoveByGUID(profile.guid()); 358 RemoveByGUID(profile.guid());
359 return; 359 return;
360 } 360 }
361 361
362 scoped_refptr<AutofillWebDataService> autofill_data( 362 scoped_refptr<AutofillWebDataService> autofill_data(
363 AutofillWebDataService::FromBrowserContext(browser_context_)); 363 AutofillWebDataService::FromBrowserContext(browser_context_));
364 if (!autofill_data.get()) 364 if (!autofill_data.get())
365 return; 365 return;
366 366
367 // Make the update. 367 // Make the update.
(...skipping 11 matching lines...) Expand all
379 if ((*iter)->guid() == guid) 379 if ((*iter)->guid() == guid)
380 return *iter; 380 return *iter;
381 } 381 }
382 return NULL; 382 return NULL;
383 } 383 }
384 384
385 void PersonalDataManager::AddCreditCard(const CreditCard& credit_card) { 385 void PersonalDataManager::AddCreditCard(const CreditCard& credit_card) {
386 if (browser_context_->IsOffTheRecord()) 386 if (browser_context_->IsOffTheRecord())
387 return; 387 return;
388 388
389 if (credit_card.IsEmpty()) 389 if (credit_card.IsEmpty(app_locale_))
390 return; 390 return;
391 391
392 if (FindByGUID<CreditCard>(credit_cards_, credit_card.guid())) 392 if (FindByGUID<CreditCard>(credit_cards_, credit_card.guid()))
393 return; 393 return;
394 394
395 scoped_refptr<AutofillWebDataService> autofill_data( 395 scoped_refptr<AutofillWebDataService> autofill_data(
396 AutofillWebDataService::FromBrowserContext(browser_context_)); 396 AutofillWebDataService::FromBrowserContext(browser_context_));
397 if (!autofill_data.get()) 397 if (!autofill_data.get())
398 return; 398 return;
399 399
400 // Don't add a duplicate. 400 // Don't add a duplicate.
401 if (FindByContents(credit_cards_, credit_card)) 401 if (FindByContents(credit_cards_, credit_card))
402 return; 402 return;
403 403
404 // Add the new credit card to the web database. 404 // Add the new credit card to the web database.
405 autofill_data->AddCreditCard(credit_card); 405 autofill_data->AddCreditCard(credit_card);
406 406
407 // Refresh our local cache and send notifications to observers. 407 // Refresh our local cache and send notifications to observers.
408 Refresh(); 408 Refresh();
409 } 409 }
410 410
411 void PersonalDataManager::UpdateCreditCard(const CreditCard& credit_card) { 411 void PersonalDataManager::UpdateCreditCard(const CreditCard& credit_card) {
412 if (browser_context_->IsOffTheRecord()) 412 if (browser_context_->IsOffTheRecord())
413 return; 413 return;
414 414
415 if (!FindByGUID<CreditCard>(credit_cards_, credit_card.guid())) 415 if (!FindByGUID<CreditCard>(credit_cards_, credit_card.guid()))
416 return; 416 return;
417 417
418 if (credit_card.IsEmpty()) { 418 if (credit_card.IsEmpty(app_locale_)) {
419 RemoveByGUID(credit_card.guid()); 419 RemoveByGUID(credit_card.guid());
420 return; 420 return;
421 } 421 }
422 422
423 scoped_refptr<AutofillWebDataService> autofill_data( 423 scoped_refptr<AutofillWebDataService> autofill_data(
424 AutofillWebDataService::FromBrowserContext(browser_context_)); 424 AutofillWebDataService::FromBrowserContext(browser_context_));
425 if (!autofill_data.get()) 425 if (!autofill_data.get())
426 return; 426 return;
427 427
428 // Make the update. 428 // Make the update.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
460 for (std::vector<CreditCard*>::iterator iter = credit_cards_.begin(); 460 for (std::vector<CreditCard*>::iterator iter = credit_cards_.begin();
461 iter != credit_cards_.end(); ++iter) { 461 iter != credit_cards_.end(); ++iter) {
462 if ((*iter)->guid() == guid) 462 if ((*iter)->guid() == guid)
463 return *iter; 463 return *iter;
464 } 464 }
465 return NULL; 465 return NULL;
466 } 466 }
467 467
468 void PersonalDataManager::GetNonEmptyTypes( 468 void PersonalDataManager::GetNonEmptyTypes(
469 FieldTypeSet* non_empty_types) { 469 FieldTypeSet* non_empty_types) {
470 const std::string app_locale = AutofillCountry::ApplicationLocale();
471 const std::vector<AutofillProfile*>& profiles = GetProfiles(); 470 const std::vector<AutofillProfile*>& profiles = GetProfiles();
472 for (std::vector<AutofillProfile*>::const_iterator iter = profiles.begin(); 471 for (std::vector<AutofillProfile*>::const_iterator iter = profiles.begin();
473 iter != profiles.end(); ++iter) { 472 iter != profiles.end(); ++iter) {
474 (*iter)->GetNonEmptyTypes(app_locale, non_empty_types); 473 (*iter)->GetNonEmptyTypes(app_locale_, non_empty_types);
475 } 474 }
476 475
477 for (ScopedVector<CreditCard>::const_iterator iter = credit_cards_.begin(); 476 for (ScopedVector<CreditCard>::const_iterator iter = credit_cards_.begin();
478 iter != credit_cards_.end(); ++iter) { 477 iter != credit_cards_.end(); ++iter) {
479 (*iter)->GetNonEmptyTypes(app_locale, non_empty_types); 478 (*iter)->GetNonEmptyTypes(app_locale_, non_empty_types);
480 } 479 }
481 } 480 }
482 481
483 bool PersonalDataManager::IsDataLoaded() const { 482 bool PersonalDataManager::IsDataLoaded() const {
484 return is_data_loaded_; 483 return is_data_loaded_;
485 } 484 }
486 485
487 const std::vector<AutofillProfile*>& PersonalDataManager::GetProfiles() { 486 const std::vector<AutofillProfile*>& PersonalDataManager::GetProfiles() {
488 if (!components::UserPrefs::Get(browser_context_)->GetBoolean( 487 if (!components::UserPrefs::Get(browser_context_)->GetBoolean(
489 prefs::kAutofillAuxiliaryProfilesEnabled)) { 488 prefs::kAutofillAuxiliaryProfilesEnabled)) {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
522 std::vector<string16>* values, 521 std::vector<string16>* values,
523 std::vector<string16>* labels, 522 std::vector<string16>* labels,
524 std::vector<string16>* icons, 523 std::vector<string16>* icons,
525 std::vector<GUIDPair>* guid_pairs) { 524 std::vector<GUIDPair>* guid_pairs) {
526 values->clear(); 525 values->clear();
527 labels->clear(); 526 labels->clear();
528 icons->clear(); 527 icons->clear();
529 guid_pairs->clear(); 528 guid_pairs->clear();
530 529
531 const std::vector<AutofillProfile*>& profiles = GetProfiles(); 530 const std::vector<AutofillProfile*>& profiles = GetProfiles();
532 const std::string app_locale = AutofillCountry::ApplicationLocale();
533 std::vector<AutofillProfile*> matched_profiles; 531 std::vector<AutofillProfile*> matched_profiles;
534 for (std::vector<AutofillProfile*>::const_iterator iter = profiles.begin(); 532 for (std::vector<AutofillProfile*>::const_iterator iter = profiles.begin();
535 iter != profiles.end(); ++iter) { 533 iter != profiles.end(); ++iter) {
536 AutofillProfile* profile = *iter; 534 AutofillProfile* profile = *iter;
537 535
538 // The value of the stored data for this field type in the |profile|. 536 // The value of the stored data for this field type in the |profile|.
539 std::vector<string16> multi_values; 537 std::vector<string16> multi_values;
540 profile->GetMultiInfo(type, app_locale, &multi_values); 538 profile->GetMultiInfo(type, app_locale_, &multi_values);
541 539
542 for (size_t i = 0; i < multi_values.size(); ++i) { 540 for (size_t i = 0; i < multi_values.size(); ++i) {
543 if (!field_is_autofilled) { 541 if (!field_is_autofilled) {
544 // Suggest data that starts with what the user has typed. 542 // Suggest data that starts with what the user has typed.
545 if (!multi_values[i].empty() && 543 if (!multi_values[i].empty() &&
546 StartsWith(multi_values[i], field_contents, false)) { 544 StartsWith(multi_values[i], field_contents, false)) {
547 matched_profiles.push_back(profile); 545 matched_profiles.push_back(profile);
548 values->push_back(multi_values[i]); 546 values->push_back(multi_values[i]);
549 guid_pairs->push_back(GUIDPair(profile->guid(), i)); 547 guid_pairs->push_back(GUIDPair(profile->guid(), i));
550 } 548 }
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
595 icons->resize(values->size()); 593 icons->resize(values->size());
596 } 594 }
597 595
598 void PersonalDataManager::GetCreditCardSuggestions( 596 void PersonalDataManager::GetCreditCardSuggestions(
599 AutofillFieldType type, 597 AutofillFieldType type,
600 const string16& field_contents, 598 const string16& field_contents,
601 std::vector<string16>* values, 599 std::vector<string16>* values,
602 std::vector<string16>* labels, 600 std::vector<string16>* labels,
603 std::vector<string16>* icons, 601 std::vector<string16>* icons,
604 std::vector<GUIDPair>* guid_pairs) { 602 std::vector<GUIDPair>* guid_pairs) {
605 const std::string app_locale = AutofillCountry::ApplicationLocale();
606 for (std::vector<CreditCard*>::const_iterator iter = credit_cards().begin(); 603 for (std::vector<CreditCard*>::const_iterator iter = credit_cards().begin();
607 iter != credit_cards().end(); ++iter) { 604 iter != credit_cards().end(); ++iter) {
608 CreditCard* credit_card = *iter; 605 CreditCard* credit_card = *iter;
609 606
610 // The value of the stored data for this field type in the |credit_card|. 607 // The value of the stored data for this field type in the |credit_card|.
611 string16 creditcard_field_value = credit_card->GetInfo(type, app_locale); 608 string16 creditcard_field_value = credit_card->GetInfo(type, app_locale_);
612 if (!creditcard_field_value.empty() && 609 if (!creditcard_field_value.empty() &&
613 StartsWith(creditcard_field_value, field_contents, false)) { 610 StartsWith(creditcard_field_value, field_contents, false)) {
614 if (type == CREDIT_CARD_NUMBER) 611 if (type == CREDIT_CARD_NUMBER)
615 creditcard_field_value = credit_card->ObfuscatedNumber(); 612 creditcard_field_value = credit_card->ObfuscatedNumber();
616 613
617 string16 label; 614 string16 label;
618 if (credit_card->number().empty()) { 615 if (credit_card->number().empty()) {
619 // If there is no CC number, return name to show something. 616 // If there is no CC number, return name to show something.
620 label = credit_card->GetInfo(CREDIT_CARD_NAME, app_locale); 617 label = credit_card->GetInfo(CREDIT_CARD_NAME, app_locale_);
621 } else { 618 } else {
622 label = kCreditCardPrefix; 619 label = kCreditCardPrefix;
623 label.append(credit_card->LastFourDigits()); 620 label.append(credit_card->LastFourDigits());
624 } 621 }
625 622
626 values->push_back(creditcard_field_value); 623 values->push_back(creditcard_field_value);
627 labels->push_back(label); 624 labels->push_back(label);
628 icons->push_back(UTF8ToUTF16(credit_card->type())); 625 icons->push_back(UTF8ToUTF16(credit_card->type()));
629 guid_pairs->push_back(GUIDPair(credit_card->guid(), 0)); 626 guid_pairs->push_back(GUIDPair(credit_card->guid(), 0));
630 } 627 }
(...skipping 28 matching lines...) Expand all
659 !autofill::IsValidZip(zip)) 656 !autofill::IsValidZip(zip))
660 return false; 657 return false;
661 658
662 return true; 659 return true;
663 } 660 }
664 661
665 // static 662 // static
666 bool PersonalDataManager::MergeProfile( 663 bool PersonalDataManager::MergeProfile(
667 const AutofillProfile& profile, 664 const AutofillProfile& profile,
668 const std::vector<AutofillProfile*>& existing_profiles, 665 const std::vector<AutofillProfile*>& existing_profiles,
666 const std::string& app_locale,
669 std::vector<AutofillProfile>* merged_profiles) { 667 std::vector<AutofillProfile>* merged_profiles) {
670 merged_profiles->clear(); 668 merged_profiles->clear();
671 669
672 // Set to true if |profile| is merged into |existing_profiles|. 670 // Set to true if |profile| is merged into |existing_profiles|.
673 bool merged = false; 671 bool merged = false;
674 672
675 // If we have already saved this address, merge in any missing values. 673 // If we have already saved this address, merge in any missing values.
676 // Only merge with the first match. 674 // Only merge with the first match.
677 for (std::vector<AutofillProfile*>::const_iterator iter = 675 for (std::vector<AutofillProfile*>::const_iterator iter =
678 existing_profiles.begin(); 676 existing_profiles.begin();
679 iter != existing_profiles.end(); ++iter) { 677 iter != existing_profiles.end(); ++iter) {
680 if (!merged) { 678 if (!merged) {
681 if (!profile.PrimaryValue().empty() && 679 if (!profile.PrimaryValue().empty() &&
682 StringToLowerASCII((*iter)->PrimaryValue()) == 680 StringToLowerASCII((*iter)->PrimaryValue()) ==
683 StringToLowerASCII(profile.PrimaryValue())) { 681 StringToLowerASCII(profile.PrimaryValue())) {
684 merged = true; 682 merged = true;
685 (*iter)->OverwriteWithOrAddTo(profile); 683 (*iter)->OverwriteWithOrAddTo(profile, app_locale);
686 } 684 }
687 } 685 }
688 merged_profiles->push_back(**iter); 686 merged_profiles->push_back(**iter);
689 } 687 }
690 688
691 // If the new profile was not merged with an existing one, add it to the list. 689 // If the new profile was not merged with an existing one, add it to the list.
692 if (!merged) 690 if (!merged)
693 merged_profiles->push_back(profile); 691 merged_profiles->push_back(profile);
694 692
695 return merged; 693 return merged;
696 } 694 }
697 695
698 void PersonalDataManager::SetProfiles(std::vector<AutofillProfile>* profiles) { 696 void PersonalDataManager::SetProfiles(std::vector<AutofillProfile>* profiles) {
699 if (browser_context_->IsOffTheRecord()) 697 if (browser_context_->IsOffTheRecord())
700 return; 698 return;
701 699
702 // Remove empty profiles from input. 700 // Remove empty profiles from input.
703 profiles->erase( 701 for (std::vector<AutofillProfile>::iterator i = profiles->begin();
Ilya Sherman 2013/04/04 04:27:38 nit: Please use "it" for the iterator name.
jam 2013/04/04 17:58:12 Done.
jam 2013/04/04 17:58:12 Done.
704 std::remove_if(profiles->begin(), profiles->end(), 702 i != profiles->end();) {
705 std::mem_fun_ref(&AutofillProfile::IsEmpty)), 703 if (i->IsEmpty(app_locale_)) {
706 profiles->end()); 704 profiles->erase(i);
705 } else {
706 i++;
707 }
Ilya Sherman 2013/04/04 04:27:38 nit: No need for curly braces.
jam 2013/04/04 17:58:12 yeah, i prefer having them when there's an else. t
Ilya Sherman 2013/04/04 23:00:40 The style guide might not set a rule for this, but
708 }
707 709
708 // Ensure that profile labels are up to date. Currently, sync relies on 710 // Ensure that profile labels are up to date. Currently, sync relies on
709 // labels to identify a profile. 711 // labels to identify a profile.
710 // TODO(dhollowa): We need to deprecate labels and update the way sync 712 // TODO(dhollowa): We need to deprecate labels and update the way sync
711 // identifies profiles. 713 // identifies profiles.
712 std::vector<AutofillProfile*> profile_pointers(profiles->size()); 714 std::vector<AutofillProfile*> profile_pointers(profiles->size());
713 std::transform(profiles->begin(), profiles->end(), profile_pointers.begin(), 715 std::transform(profiles->begin(), profiles->end(), profile_pointers.begin(),
714 address_of<AutofillProfile>); 716 address_of<AutofillProfile>);
715 AutofillProfile::AdjustInferredLabels(&profile_pointers); 717 AutofillProfile::AdjustInferredLabels(&profile_pointers);
716 718
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
753 // Refresh our local cache and send notifications to observers. 755 // Refresh our local cache and send notifications to observers.
754 Refresh(); 756 Refresh();
755 } 757 }
756 758
757 void PersonalDataManager::SetCreditCards( 759 void PersonalDataManager::SetCreditCards(
758 std::vector<CreditCard>* credit_cards) { 760 std::vector<CreditCard>* credit_cards) {
759 if (browser_context_->IsOffTheRecord()) 761 if (browser_context_->IsOffTheRecord())
760 return; 762 return;
761 763
762 // Remove empty credit cards from input. 764 // Remove empty credit cards from input.
763 credit_cards->erase( 765 for (std::vector<CreditCard>::iterator i = credit_cards->begin();
764 std::remove_if( 766 i != credit_cards->end();) {
Ilya Sherman 2013/04/04 04:27:38 nit: Please use "it" for the iterator name.
jam 2013/04/04 17:58:12 Done.
765 credit_cards->begin(), credit_cards->end(), 767 if (i->IsEmpty(app_locale_)) {
766 std::mem_fun_ref(&CreditCard::IsEmpty)), 768 credit_cards->erase(i);
767 credit_cards->end()); 769 } else {
770 i++;
771 }
Ilya Sherman 2013/04/04 04:27:38 nit: No need for curly braces.
772 }
768 773
769 scoped_refptr<AutofillWebDataService> autofill_data( 774 scoped_refptr<AutofillWebDataService> autofill_data(
770 AutofillWebDataService::FromBrowserContext(browser_context_)); 775 AutofillWebDataService::FromBrowserContext(browser_context_));
771 if (!autofill_data.get()) 776 if (!autofill_data.get())
772 return; 777 return;
773 778
774 // Any credit cards that are not in the new credit card list should be 779 // Any credit cards that are not in the new credit card list should be
775 // removed. 780 // removed.
776 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin(); 781 for (std::vector<CreditCard*>::const_iterator iter = credit_cards_.begin();
777 iter != credit_cards_.end(); ++iter) { 782 iter != credit_cards_.end(); ++iter) {
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after
895 // upon form submission. 900 // upon form submission.
896 NOTREACHED(); 901 NOTREACHED();
897 return; 902 return;
898 } 903 }
899 904
900 // Don't save a web profile if the data in the profile is a subset of an 905 // Don't save a web profile if the data in the profile is a subset of an
901 // auxiliary profile. 906 // auxiliary profile.
902 for (std::vector<AutofillProfile*>::const_iterator iter = 907 for (std::vector<AutofillProfile*>::const_iterator iter =
903 auxiliary_profiles_.begin(); 908 auxiliary_profiles_.begin();
904 iter != auxiliary_profiles_.end(); ++iter) { 909 iter != auxiliary_profiles_.end(); ++iter) {
905 if (imported_profile.IsSubsetOf(**iter)) 910 if (imported_profile.IsSubsetOf(**iter, app_locale_))
906 return; 911 return;
907 } 912 }
908 913
909 std::vector<AutofillProfile> profiles; 914 std::vector<AutofillProfile> profiles;
910 MergeProfile(imported_profile, web_profiles_.get(), &profiles); 915 MergeProfile(imported_profile, web_profiles_.get(), app_locale_, &profiles);
911 SetProfiles(&profiles); 916 SetProfiles(&profiles);
912 } 917 }
913 918
914 919
915 void PersonalDataManager::SaveImportedCreditCard( 920 void PersonalDataManager::SaveImportedCreditCard(
916 const CreditCard& imported_card) { 921 const CreditCard& imported_card) {
917 DCHECK(!imported_card.number().empty()); 922 DCHECK(!imported_card.number().empty());
918 if (browser_context_->IsOffTheRecord()) { 923 if (browser_context_->IsOffTheRecord()) {
919 // The |IsOffTheRecord| check should happen earlier in the import process, 924 // The |IsOffTheRecord| check should happen earlier in the import process,
920 // upon form submission. 925 // upon form submission.
921 NOTREACHED(); 926 NOTREACHED();
922 return; 927 return;
923 } 928 }
924 929
925 // Set to true if |imported_card| is merged into the credit card list. 930 // Set to true if |imported_card| is merged into the credit card list.
926 bool merged = false; 931 bool merged = false;
927 932
928 const std::string app_locale = AutofillCountry::ApplicationLocale();
929 std::vector<CreditCard> credit_cards; 933 std::vector<CreditCard> credit_cards;
930 for (std::vector<CreditCard*>::const_iterator card = credit_cards_.begin(); 934 for (std::vector<CreditCard*>::const_iterator card = credit_cards_.begin();
931 card != credit_cards_.end(); 935 card != credit_cards_.end();
932 ++card) { 936 ++card) {
933 // If |imported_card| has not yet been merged, check whether it should be 937 // If |imported_card| has not yet been merged, check whether it should be
934 // with the current |card|. 938 // with the current |card|.
935 if (!merged && (*card)->UpdateFromImportedCard(imported_card, app_locale)) 939 if (!merged && (*card)->UpdateFromImportedCard(imported_card, app_locale_))
936 merged = true; 940 merged = true;
937 941
938 credit_cards.push_back(**card); 942 credit_cards.push_back(**card);
939 } 943 }
940 944
941 if (!merged) 945 if (!merged)
942 credit_cards.push_back(imported_card); 946 credit_cards.push_back(imported_card);
943 947
944 SetCreditCards(&credit_cards); 948 SetCreditCards(&credit_cards);
945 } 949 }
(...skipping 11 matching lines...) Expand all
957 961
958 void PersonalDataManager::set_metric_logger( 962 void PersonalDataManager::set_metric_logger(
959 const AutofillMetrics* metric_logger) { 963 const AutofillMetrics* metric_logger) {
960 metric_logger_.reset(metric_logger); 964 metric_logger_.reset(metric_logger);
961 } 965 }
962 966
963 void PersonalDataManager::set_browser_context( 967 void PersonalDataManager::set_browser_context(
964 content::BrowserContext* context) { 968 content::BrowserContext* context) {
965 browser_context_ = context; 969 browser_context_ = context;
966 } 970 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698