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

Side by Side Diff: chrome/browser/ui/webui/options/autofill_options_handler.cc

Issue 1868003003: Preserving first/middle/last names when an Autofill profile is submitted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Cleaning up refactor cruft Created 4 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
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 "chrome/browser/ui/webui/options/autofill_options_handler.h" 5 #include "chrome/browser/ui/webui/options/autofill_options_handler.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <vector> 9 #include <vector>
10 10
(...skipping 10 matching lines...) Expand all
21 #include "base/values.h" 21 #include "base/values.h"
22 #include "chrome/browser/autofill/personal_data_manager_factory.h" 22 #include "chrome/browser/autofill/personal_data_manager_factory.h"
23 #include "chrome/browser/browser_process.h" 23 #include "chrome/browser/browser_process.h"
24 #include "chrome/browser/profiles/profile.h" 24 #include "chrome/browser/profiles/profile.h"
25 #include "chrome/browser/ui/autofill/country_combobox_model.h" 25 #include "chrome/browser/ui/autofill/country_combobox_model.h"
26 #include "chrome/common/url_constants.h" 26 #include "chrome/common/url_constants.h"
27 #include "chrome/grit/chromium_strings.h" 27 #include "chrome/grit/chromium_strings.h"
28 #include "chrome/grit/generated_resources.h" 28 #include "chrome/grit/generated_resources.h"
29 #include "components/autofill/content/browser/wallet/wallet_service_url.h" 29 #include "components/autofill/content/browser/wallet/wallet_service_url.h"
30 #include "components/autofill/core/browser/autofill_country.h" 30 #include "components/autofill/core/browser/autofill_country.h"
31 #include "components/autofill/core/browser/autofill_data_util.h"
31 #include "components/autofill/core/browser/autofill_profile.h" 32 #include "components/autofill/core/browser/autofill_profile.h"
32 #include "components/autofill/core/browser/credit_card.h" 33 #include "components/autofill/core/browser/credit_card.h"
33 #include "components/autofill/core/browser/personal_data_manager.h" 34 #include "components/autofill/core/browser/personal_data_manager.h"
34 #include "components/autofill/core/browser/phone_number_i18n.h" 35 #include "components/autofill/core/browser/phone_number_i18n.h"
35 #include "components/autofill/core/common/autofill_constants.h" 36 #include "components/autofill/core/common/autofill_constants.h"
36 #include "components/autofill/core/common/autofill_switches.h" 37 #include "components/autofill/core/common/autofill_switches.h"
37 #include "content/public/browser/web_ui.h" 38 #include "content/public/browser/web_ui.h"
38 #include "grit/components_strings.h" 39 #include "grit/components_strings.h"
39 #include "third_party/libaddressinput/messages.h" 40 #include "third_party/libaddressinput/messages.h"
40 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h" 41 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_ui .h"
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 const std::string& ui_language_code, 91 const std::string& ui_language_code,
91 base::ListValue* address_components, 92 base::ListValue* address_components,
92 std::string* components_language_code) { 93 std::string* components_language_code) {
93 DCHECK(address_components); 94 DCHECK(address_components);
94 95
95 i18n::addressinput::Localization localization; 96 i18n::addressinput::Localization localization;
96 localization.SetGetter(l10n_util::GetStringUTF8); 97 localization.SetGetter(l10n_util::GetStringUTF8);
97 std::string not_used; 98 std::string not_used;
98 std::vector<AddressUiComponent> components = 99 std::vector<AddressUiComponent> components =
99 i18n::addressinput::BuildComponents( 100 i18n::addressinput::BuildComponents(
100 country_code, 101 country_code, localization, ui_language_code,
101 localization, 102 components_language_code == nullptr ? &not_used
102 ui_language_code, 103 : components_language_code);
103 components_language_code == NULL ?
104 &not_used : components_language_code);
105 if (components.empty()) { 104 if (components.empty()) {
106 static const char kDefaultCountryCode[] = "US"; 105 static const char kDefaultCountryCode[] = "US";
107 components = i18n::addressinput::BuildComponents( 106 components = i18n::addressinput::BuildComponents(
108 kDefaultCountryCode, 107 kDefaultCountryCode, localization, ui_language_code,
109 localization, 108 components_language_code == nullptr ? &not_used
110 ui_language_code, 109 : components_language_code);
111 components_language_code == NULL ?
112 &not_used : components_language_code);
113 } 110 }
114 DCHECK(!components.empty()); 111 DCHECK(!components.empty());
115 112
116 base::ListValue* line = NULL; 113 base::ListValue* line = nullptr;
117 static const char kField[] = "field"; 114 static const char kField[] = "field";
118 static const char kLength[] = "length"; 115 static const char kLength[] = "length";
119 for (size_t i = 0; i < components.size(); ++i) { 116 for (size_t i = 0; i < components.size(); ++i) {
120 if (i == 0 || 117 if (i == 0 ||
121 components[i - 1].length_hint == AddressUiComponent::HINT_LONG || 118 components[i - 1].length_hint == AddressUiComponent::HINT_LONG ||
122 components[i].length_hint == AddressUiComponent::HINT_LONG) { 119 components[i].length_hint == AddressUiComponent::HINT_LONG) {
123 line = new base::ListValue; 120 line = new base::ListValue;
124 address_components->Append(line); 121 address_components->Append(line);
125 } 122 }
126 123
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 localized_strings->Set("autofillDefaultCountryComponents", 198 localized_strings->Set("autofillDefaultCountryComponents",
202 default_country_components.release()); 199 default_country_components.release());
203 localized_strings->SetString("autofillDefaultCountryLanguageCode", 200 localized_strings->SetString("autofillDefaultCountryLanguageCode",
204 default_country_language_code); 201 default_country_language_code);
205 } 202 }
206 203
207 } // namespace 204 } // namespace
208 205
209 namespace options { 206 namespace options {
210 207
211 AutofillOptionsHandler::AutofillOptionsHandler() : personal_data_(NULL) {} 208 AutofillOptionsHandler::AutofillOptionsHandler()
209 : personal_data_(nullptr), prior_profile_(nullptr) {}
212 210
213 AutofillOptionsHandler::~AutofillOptionsHandler() { 211 AutofillOptionsHandler::~AutofillOptionsHandler() {
214 if (personal_data_) 212 if (personal_data_)
215 personal_data_->RemoveObserver(this); 213 personal_data_->RemoveObserver(this);
216 } 214 }
217 215
218 ///////////////////////////////////////////////////////////////////////////// 216 /////////////////////////////////////////////////////////////////////////////
219 // OptionsPageUIHandler implementation: 217 // OptionsPageUIHandler implementation:
220 void AutofillOptionsHandler::GetLocalizedValues( 218 void AutofillOptionsHandler::GetLocalizedValues(
221 base::DictionaryValue* localized_strings) { 219 base::DictionaryValue* localized_strings) {
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 385
388 void AutofillOptionsHandler::LoadAddressEditor(const base::ListValue* args) { 386 void AutofillOptionsHandler::LoadAddressEditor(const base::ListValue* args) {
389 DCHECK(IsPersonalDataLoaded()); 387 DCHECK(IsPersonalDataLoaded());
390 388
391 std::string guid; 389 std::string guid;
392 if (!args->GetString(0, &guid)) { 390 if (!args->GetString(0, &guid)) {
393 NOTREACHED(); 391 NOTREACHED();
394 return; 392 return;
395 } 393 }
396 394
397 AutofillProfile* profile = personal_data_->GetProfileByGUID(guid); 395 prior_profile_ = personal_data_->GetProfileByGUID(guid);
398 if (!profile) { 396 if (!prior_profile_) {
399 // There is a race where a user can click once on the close button and 397 // There is a race where a user can click once on the close button and
400 // quickly click again on the list item before the item is removed (since 398 // quickly click again on the list item before the item is removed (since
401 // the list is not updated until the model tells the list an item has been 399 // the list is not updated until the model tells the list an item has been
402 // removed). This will activate the editor for a profile that has been 400 // removed). This will activate the editor for a profile that has been
403 // removed. Do nothing in that case. 401 // removed. Do nothing in that case.
404 return; 402 return;
405 } 403 }
406 404
407 base::DictionaryValue address; 405 base::DictionaryValue address;
408 AutofillProfileToDictionary(*profile, &address); 406 AutofillProfileToDictionary(*prior_profile_, &address);
409 407
410 web_ui()->CallJavascriptFunction("AutofillOptions.editAddress", address); 408 web_ui()->CallJavascriptFunction("AutofillOptions.editAddress", address);
411 } 409 }
412 410
413 void AutofillOptionsHandler::LoadAddressEditorComponents( 411 void AutofillOptionsHandler::LoadAddressEditorComponents(
414 const base::ListValue* args) { 412 const base::ListValue* args) {
415 std::string country_code; 413 std::string country_code;
416 if (!args->GetString(0, &country_code)) { 414 if (!args->GetString(0, &country_code)) {
417 NOTREACHED(); 415 NOTREACHED();
418 return; 416 return;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 471
474 int arg_counter = 0; 472 int arg_counter = 0;
475 std::string guid; 473 std::string guid;
476 if (!args->GetString(arg_counter++, &guid)) { 474 if (!args->GetString(arg_counter++, &guid)) {
477 NOTREACHED(); 475 NOTREACHED();
478 return; 476 return;
479 } 477 }
480 478
481 AutofillProfile profile(guid, kSettingsOrigin); 479 AutofillProfile profile(guid, kSettingsOrigin);
482 480
483 base::string16 value; 481 base::string16 full_name;
484 if (args->GetString(arg_counter++, &value)) { 482 if (args->GetString(arg_counter++, &full_name)) {
485 profile.SetInfo(AutofillType(autofill::NAME_FULL), value, 483 // Although First/Middle/Last are not displayed on the form, we transfer
486 g_browser_process->GetApplicationLocale()); 484 // this
485 // information when they match the full name given. This is because it may
Mathieu 2016/04/13 18:12:24 nit: line wrapping got weird here!
tmartino 2016/04/13 19:43:25 git cl format is not as smart as I thought it was.
486 // not
487 // be possible later to correctly tokenize the concatenated full name--e.g.,
488 // when the last name contains a space, the first word would be treated as a
489 // middle name.
490 if (prior_profile_ && autofill::data_util::ProfileMatchesFullName(
491 full_name, *prior_profile_)) {
492 profile.SetRawInfo(autofill::NAME_FULL, full_name);
493
494 profile.SetRawInfo(autofill::NAME_FIRST,
495 prior_profile_->GetRawInfo(autofill::NAME_FIRST));
496 profile.SetRawInfo(autofill::NAME_MIDDLE,
497 prior_profile_->GetRawInfo(autofill::NAME_MIDDLE));
498 profile.SetRawInfo(autofill::NAME_LAST,
499 prior_profile_->GetRawInfo(autofill::NAME_LAST));
500 } else {
501 // In contrast to SetRawInfo, SetInfo will naively attempt to populate the
502 // First/Middle/Last fields by tokenization.
503 profile.SetInfo(AutofillType(autofill::NAME_FULL), full_name,
504 g_browser_process->GetApplicationLocale());
505 }
487 } 506 }
488 507
508 base::string16 value;
489 if (args->GetString(arg_counter++, &value)) 509 if (args->GetString(arg_counter++, &value))
490 profile.SetRawInfo(autofill::COMPANY_NAME, value); 510 profile.SetRawInfo(autofill::COMPANY_NAME, value);
491 511
492 if (args->GetString(arg_counter++, &value)) 512 if (args->GetString(arg_counter++, &value))
493 profile.SetRawInfo(autofill::ADDRESS_HOME_STREET_ADDRESS, value); 513 profile.SetRawInfo(autofill::ADDRESS_HOME_STREET_ADDRESS, value);
494 514
495 if (args->GetString(arg_counter++, &value)) 515 if (args->GetString(arg_counter++, &value))
496 profile.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, value); 516 profile.SetRawInfo(autofill::ADDRESS_HOME_DEPENDENT_LOCALITY, value);
497 517
498 if (args->GetString(arg_counter++, &value)) 518 if (args->GetString(arg_counter++, &value))
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
601 address->SetString(kCountryField, 621 address->SetString(kCountryField,
602 profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)); 622 profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY));
603 address->SetString("phone", 623 address->SetString("phone",
604 profile.GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER)); 624 profile.GetRawInfo(autofill::PHONE_HOME_WHOLE_NUMBER));
605 address->SetString("email", profile.GetRawInfo(autofill::EMAIL_ADDRESS)); 625 address->SetString("email", profile.GetRawInfo(autofill::EMAIL_ADDRESS));
606 address->SetString(kLanguageCode, profile.language_code()); 626 address->SetString(kLanguageCode, profile.language_code());
607 627
608 scoped_ptr<base::ListValue> components(new base::ListValue); 628 scoped_ptr<base::ListValue> components(new base::ListValue);
609 GetAddressComponents( 629 GetAddressComponents(
610 base::UTF16ToUTF8(profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)), 630 base::UTF16ToUTF8(profile.GetRawInfo(autofill::ADDRESS_HOME_COUNTRY)),
611 profile.language_code(), 631 profile.language_code(), components.get(), nullptr);
612 components.get(),
613 NULL);
614 address->Set(kComponents, components.release()); 632 address->Set(kComponents, components.release());
615 } 633 }
616 634
617 } // namespace options 635 } // namespace options
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698