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

Unified Diff: third_party/libaddressinput/chromium/preload_address_validator.cc

Issue 298863012: Use upstream libaddressinput in Chrome. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Self review. Created 6 years, 6 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: third_party/libaddressinput/chromium/preload_address_validator.cc
diff --git a/third_party/libaddressinput/chromium/preload_address_validator.cc b/third_party/libaddressinput/chromium/preload_address_validator.cc
index 816e3a5d522702e1ee041d3e33ab866dc4256185..e11a1fe464ad7f318e80264b31e7a17b95030244 100644
--- a/third_party/libaddressinput/chromium/preload_address_validator.cc
+++ b/third_party/libaddressinput/chromium/preload_address_validator.cc
@@ -7,30 +7,34 @@
#include <vector>
#include "base/basictypes.h"
+#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
-
-#define I18N_ADDRESSINPUT_UTIL_BASICTYPES_H_
#include "third_party/libaddressinput/chromium/preload_address_validator.h"
#include "third_party/libaddressinput/chromium/suggestions.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
+#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_input_helper.h"
+#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_normalizer.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_validator.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_supplier.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
-#include "third_party/libaddressinput/src/cpp/include/libaddressinput/synonyms.h"
namespace autofill {
using ::i18n::addressinput::AddressData;
using ::i18n::addressinput::AddressField;
+using ::i18n::addressinput::AddressInputHelper;
+using ::i18n::addressinput::AddressNormalizer;
using ::i18n::addressinput::AddressValidator;
using ::i18n::addressinput::BuildCallback;
using ::i18n::addressinput::Downloader;
using ::i18n::addressinput::FieldProblemMap;
+using ::i18n::addressinput::INVALID_FORMAT;
+using ::i18n::addressinput::MISMATCHING_VALUE;
+using ::i18n::addressinput::POSTAL_CODE;
using ::i18n::addressinput::PreloadSupplier;
using ::i18n::addressinput::Storage;
-using ::i18n::addressinput::Synonyms;
PreloadAddressValidator::PreloadAddressValidator(
const std::string& validation_data_url,
@@ -44,21 +48,22 @@ PreloadAddressValidator::PreloadAddressValidator(
suggestions_(
new Suggestions(
supplier_.get())),
- synonyms_(
- new Synonyms(
+ normalizer_(
+ new AddressNormalizer(
supplier_.get())),
validator_(
new AddressValidator(
supplier_.get())),
+ input_helper_(
+ new AddressInputHelper(supplier_.get())),
validated_(BuildCallback(this, &PreloadAddressValidator::Validated)) {
}
-PreloadAddressValidator::~PreloadAddressValidator() {
-}
+PreloadAddressValidator::~PreloadAddressValidator() {}
void PreloadAddressValidator::LoadRules(const std::string& region_code,
const Callback& loaded) {
- assert(supplier_ != NULL);
+ DCHECK(supplier_);
supplier_->LoadRules(region_code, loaded);
}
@@ -66,19 +71,21 @@ PreloadAddressValidator::Status PreloadAddressValidator::Validate(
const AddressData& address,
const FieldProblemMap* filter,
FieldProblemMap* problems) const {
- assert(supplier_ != NULL);
- assert(validator_ != NULL);
+ DCHECK(supplier_);
+ DCHECK(validator_);
+ DCHECK(normalizer_);
- if (supplier_->IsPending(address.region_code)) {
+ if (supplier_->IsPending(address.region_code))
return RULES_NOT_READY;
- }
- if (!supplier_->IsLoaded(address.region_code)) {
+ if (!supplier_->IsLoaded(address.region_code))
return RULES_UNAVAILABLE;
- }
+
+ AddressData normalized_address = address;
+ normalizer_->Normalize(&normalized_address);
validator_->Validate(
- address,
+ normalized_address,
/*allow_postal*/ false,
/*require_name*/ false,
filter,
@@ -93,39 +100,72 @@ PreloadAddressValidator::Status PreloadAddressValidator::GetSuggestions(
AddressField focused_field,
size_t suggestion_limit,
std::vector<AddressData>* suggestions) const {
- assert(suggestions_ != NULL);
- assert(supplier_ != NULL);
+ DCHECK(suggestions);
+ DCHECK(suggestions_);
+ DCHECK(input_helper_);
+ DCHECK(supplier_);
- if (supplier_->IsPending(user_input.region_code)) {
+ if (supplier_->IsPending(user_input.region_code))
return RULES_NOT_READY;
- }
- if (!supplier_->IsLoaded(user_input.region_code)) {
+ if (!supplier_->IsLoaded(user_input.region_code))
return RULES_UNAVAILABLE;
+
+ AddressData address_copy = user_input;
+ FieldProblemMap filter;
+ FieldProblemMap problems;
+ if (focused_field == POSTAL_CODE) {
+ filter.insert(std::make_pair(POSTAL_CODE, INVALID_FORMAT));
+
+ Status status = Validate(address_copy, &filter, &problems);
+ DCHECK(status == SUCCESS);
+ (void)status;
+
+ if (!problems.empty())
+ return SUCCESS;
+
+ input_helper_->FillAddress(&address_copy);
}
suggestions_->GetSuggestions(
- user_input,
+ address_copy,
focused_field,
suggestion_limit,
suggestions);
+ filter.clear();
+ filter.insert(std::make_pair(POSTAL_CODE, MISMATCHING_VALUE));
+ std::vector<AddressData> valid_postal_codes;
+ for (std::vector<AddressData>::const_iterator suggestion_it =
+ suggestions->begin();
+ suggestion_it != suggestions->end();
+ ++suggestion_it) {
+ problems.clear();
+
+ Status status = Validate(address_copy, &filter, &problems);
+ DCHECK(status == SUCCESS);
+ (void)status;
+
+ if (problems.empty())
+ valid_postal_codes.push_back(*suggestion_it);
+ }
+ suggestions->swap(valid_postal_codes);
+
return SUCCESS;
}
bool PreloadAddressValidator::CanonicalizeAdministrativeArea(
AddressData* address) const {
- assert(address != NULL);
- assert(supplier_ != NULL);
- assert(synonyms_ != NULL);
+ DCHECK(address);
+ DCHECK(supplier_);
+ DCHECK(normalizer_);
- if (!supplier_->IsLoaded(address->region_code)) {
+ if (!supplier_->IsLoaded(address->region_code))
return false;
- }
// TODO: It would probably be beneficial to use the full canonicalization.
AddressData tmp(*address);
- synonyms_->NormalizeForDisplay(&tmp);
+ normalizer_->Normalize(&tmp);
address->administrative_area = tmp.administrative_area;
return true;
@@ -134,11 +174,10 @@ bool PreloadAddressValidator::CanonicalizeAdministrativeArea(
void PreloadAddressValidator::Validated(bool success,
const AddressData&,
const FieldProblemMap&) {
- assert(success);
+ DCHECK(success);
}
// Stub constructor for use by MockAddressValidator.
-PreloadAddressValidator::PreloadAddressValidator() {
-}
+PreloadAddressValidator::PreloadAddressValidator() {}
} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698