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

Unified Diff: chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc

Issue 145553009: rAc: use libaddressinput to validate international addresses. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: nits Created 6 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc
diff --git a/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc b/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc
index db0ef1073aeb0762c2bab5f090ec49d47f918635..4e33a613a9d6ccedafd821061ed4c8aeb77abb27 100644
--- a/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc
+++ b/chrome/browser/ui/autofill/autofill_dialog_controller_impl.cc
@@ -25,6 +25,7 @@
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "base/time/time.h"
+#include "chrome/browser/autofill/address_storage_factory.h"
#include "chrome/browser/autofill/personal_data_manager_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/profiles/profile.h"
@@ -79,6 +80,10 @@
#include "grit/theme_resources.h"
#include "grit/webkit_resources.h"
#include "net/cert/cert_status_flags.h"
+#include "third_party/libaddressinput/chromium/chrome_downloader_impl.h"
+#include "third_party/libaddressinput/chromium/chrome_storage_impl.h"
+#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_data.h"
+#include "third_party/libaddressinput/chromium/cpp/include/libaddressinput/address_problem.h"
#include "ui/base/base_window.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/models/combobox_model.h"
@@ -88,6 +93,12 @@
#include "ui/gfx/image/image_skia_operations.h"
#include "ui/gfx/skia_util.h"
+using ::i18n::addressinput::AddressData;
+using ::i18n::addressinput::AddressProblem;
+using ::i18n::addressinput::AddressProblemFilter;
+using ::i18n::addressinput::AddressProblems;
+using ::i18n::addressinput::AddressValidator;
+
namespace autofill {
namespace {
@@ -480,9 +491,10 @@ gfx::Image CvcIconForCreditCardType(const base::string16& credit_card_type) {
return rb.GetImageNamed(IDR_CREDIT_CARD_CVC_HINT);
}
-ServerFieldType CountryTypeForSection(DialogSection section) {
- return section == SECTION_SHIPPING ? ADDRESS_HOME_COUNTRY :
- ADDRESS_BILLING_COUNTRY;
+ServerFieldType TypeForSection(DialogSection section, ServerFieldType type) {
+ return section == SECTION_SHIPPING ?
+ AutofillType(type).GetStorableType() :
+ AutofillType::GetEquivalentBillingFieldType(type);
}
} // namespace
@@ -1107,8 +1119,8 @@ void AutofillDialogControllerImpl::ShowEditUiIfBadSuggestion(
}
if (wrapper && IsEditingExistingData(section)) {
- base::string16 country =
- wrapper->GetInfo(AutofillType(CountryTypeForSection(section)));
+ base::string16 country = wrapper->GetInfo(AutofillType(
+ TypeForSection(section, ADDRESS_HOME_COUNTRY)));
if (!country.empty()) {
// There's no user input to restore here as this is only called after
// resetting all section input.
@@ -1700,7 +1712,8 @@ base::string16 AutofillDialogControllerImpl::InputValidityMessage(
break;
case ADDRESS_HOME_STATE:
- if (!value.empty() && !autofill::IsValidState(value) &&
+ if (!i18ninput::Enabled() && !value.empty() &&
+ !autofill::IsValidState(value) &&
CountryCodeForSection(section) == "US") {
return l10n_util::GetStringUTF16(
IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_REGION);
@@ -1708,7 +1721,8 @@ base::string16 AutofillDialogControllerImpl::InputValidityMessage(
break;
case ADDRESS_HOME_ZIP:
- if (!value.empty() && !autofill::IsValidZip(value) &&
+ if (!i18ninput::Enabled() && !value.empty() &&
Evan Stade 2014/01/28 19:23:12 I feel like all the address stuff should be remove
Dan Beam 2014/01/30 04:51:36 why not just remove when we axe the flag?
Evan Stade 2014/01/30 04:58:20 because then the behavior will be wrong until we a
Dan Beam 2014/01/30 06:28:58 Done.
+ !autofill::IsValidZip(value) &&
CountryCodeForSection(section) == "US") {
return l10n_util::GetStringUTF16(
IDS_AUTOFILL_DIALOG_VALIDATION_INVALID_ZIP_CODE);
@@ -1747,9 +1761,59 @@ ValidityMessages AutofillDialogControllerImpl::InputsAreValid(
const FieldValueMap& inputs) {
ValidityMessages messages;
FieldValueMap field_values;
+
+ if (section != SECTION_CC) {
+ FieldValueMap values = inputs;
Evan Stade 2014/01/28 19:23:12 why do you need to make this copy?
Dan Beam 2014/01/30 04:51:36 was to create empty base::string16() by non-const
+
+ AddressData address_data;
+ address_data.language_code = g_browser_process->GetApplicationLocale();
+ address_data.country_code = AutofillCountry::GetCountryCode(
+ values[TypeForSection(section, ADDRESS_HOME_COUNTRY)],
+ g_browser_process->GetApplicationLocale());
+ address_data.administrative_area = UTF16ToUTF8(
Evan Stade 2014/01/28 19:23:12 DataModelWrapper already has code that creates an
Dan Beam 2014/01/30 04:51:36 Done.
+ values[TypeForSection(section, ADDRESS_HOME_STATE)]);
+ address_data.locality = UTF16ToUTF8(
+ values[TypeForSection(section, ADDRESS_HOME_CITY)]);
+ address_data.dependent_locality = UTF16ToUTF8(
+ values[TypeForSection(section, ADDRESS_HOME_DEPENDENT_LOCALITY)]);
+ address_data.sorting_code = UTF16ToUTF8(
+ values[TypeForSection(section, ADDRESS_HOME_SORTING_CODE)]);
+ address_data.postal_code = UTF16ToUTF8(
+ values[TypeForSection(section, ADDRESS_HOME_ZIP)]);
+ address_data.recipient = UTF16ToUTF8(
+ values[TypeForSection(section, NAME_FULL)]);
+
+ base::string16 street_line1 =
+ values[TypeForSection(section, ADDRESS_HOME_LINE1)];
+ if (!street_line1.empty())
+ address_data.address_lines.push_back(UTF16ToUTF8(street_line1));
+
+ base::string16 street_line2 =
+ values[TypeForSection(section, ADDRESS_HOME_LINE2)];
+ if (!street_line2.empty())
+ address_data.address_lines.push_back(UTF16ToUTF8(street_line2));
+
+ AddressProblems problems;
+ AddressValidator::Status status = validator_->ValidateAddress(
+ address_data, AddressProblemFilter(), &problems);
+ if (status == AddressValidator::SUCCESS) {
+ common::AddressType address_type = section == SECTION_SHIPPING ?
+ common::ADDRESS_TYPE_SHIPPING : common::ADDRESS_TYPE_BILLING;
+ for (size_t i = 0; i < problems.size(); ++i) {
+ const AddressProblem& problem = problems[i];
+ bool sure = problem.type != AddressProblem::MISSING_REQUIRED_FIELD;
+ base::string16 text = l10n_util::GetStringUTF16(problem.description_id);
+ messages.Set(i18ninput::TypeForField(problem.field, address_type),
+ ValidityMessage(text, sure));
+ }
+ }
+ }
+
for (FieldValueMap::const_iterator iter = inputs.begin();
iter != inputs.end(); ++iter) {
const ServerFieldType type = iter->first;
+ if (messages.HasSureError(type))
+ continue;
base::string16 text = InputValidityMessage(section, type, iter->second);
@@ -2580,7 +2644,6 @@ AutofillDialogControllerImpl::AutofillDialogControllerImpl(
suggested_shipping_(this),
cares_about_shipping_(true),
popup_input_type_(UNKNOWN_TYPE),
- weak_ptr_factory_(this),
waiting_for_explicit_sign_in_response_(false),
has_accepted_legal_documents_(false),
is_submitting_(false),
@@ -2588,9 +2651,16 @@ AutofillDialogControllerImpl::AutofillDialogControllerImpl(
wallet_server_validation_recoverable_(true),
data_was_passed_back_(false),
was_ui_latency_logged_(false),
- card_generated_animation_(2000, 60, this) {
+ card_generated_animation_(2000, 60, this),
+ weak_ptr_factory_(this) {
// TODO(estade): remove duplicates from |form_structure|?
DCHECK(!callback_.is_null());
+
+ scoped_ptr< ::i18n::addressinput::Downloader> downloader(
Evan Stade 2014/01/28 19:25:09 move this all to show
Dan Beam 2014/01/30 04:51:36 Done.
+ new autofill::ChromeDownloaderImpl(profile_->GetRequestContext()));
+ validator_ = AddressValidator::Build(
+ downloader.Pass(), AddressStorageFactory::CreateStorage(), this);
+ validator_->LoadRules(GetManager()->GetDefaultCountryCodeForNewAddress());
}
AutofillDialogView* AutofillDialogControllerImpl::CreateView() {
@@ -3075,7 +3145,8 @@ std::string AutofillDialogControllerImpl::CountryCodeForSection(
scoped_ptr<DataModelWrapper> wrapper = CreateWrapper(section);
if (wrapper) {
return AutofillCountry::GetCountryCode(
- wrapper->GetInfo(AutofillType(CountryTypeForSection(section))),
+ wrapper->GetInfo(
+ AutofillType(TypeForSection(section, ADDRESS_HOME_COUNTRY))),
g_browser_process->GetApplicationLocale());
}
@@ -3094,7 +3165,7 @@ bool AutofillDialogControllerImpl::RebuildInputsForCountry(
view_->GetUserInput(section, &outputs);
// If |country_name| is the same as the view, no-op and let the caller know.
- if (outputs[CountryTypeForSection(section)] == country_name)
+ if (outputs[TypeForSection(section, ADDRESS_HOME_COUNTRY)] == country_name)
return false;
}
@@ -3397,6 +3468,12 @@ void AutofillDialogControllerImpl::AnimationEnded(
DoFinishSubmit();
}
+void AutofillDialogControllerImpl::OnAddressValidationRulesLoaded(
+ const std::string& country_code,
+ bool success) {
+ // TODO(dbeam): ask |view_| to re-validate its contents if necessary.
+}
+
void AutofillDialogControllerImpl::DoFinishSubmit() {
FillOutputForSection(SECTION_CC);
FillOutputForSection(SECTION_BILLING);

Powered by Google App Engine
This is Rietveld 408576698