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

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: Work in progress for suggestions impl. 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..8664563354ba47ff7697ab7a4c8183ff7e5a34eb 100644
--- a/third_party/libaddressinput/chromium/preload_address_validator.cc
+++ b/third_party/libaddressinput/chromium/preload_address_validator.cc
@@ -2,83 +2,86 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include "third_party/libaddressinput/chromium/preload_address_validator.h"
+
#include <cstddef>
#include <string>
#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/load_rules_delegate.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::AddressValidator;
+using ::i18n::addressinput::AddressInputHelper;
+using ::i18n::addressinput::AddressNormalizer;
+using ::i18n::addressinput::ADMIN_AREA;
using ::i18n::addressinput::BuildCallback;
+using ::i18n::addressinput::DEPENDENT_LOCALITY;
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,
- scoped_ptr<Downloader> downloader,
- scoped_ptr<Storage> storage)
- : supplier_(
- new PreloadSupplier(
- validation_data_url,
- downloader.release(),
- storage.release())),
- suggestions_(
- new Suggestions(
- supplier_.get())),
- synonyms_(
- new Synonyms(
- supplier_.get())),
- validator_(
- new AddressValidator(
- supplier_.get())),
- validated_(BuildCallback(this, &PreloadAddressValidator::Validated)) {
-}
-
-PreloadAddressValidator::~PreloadAddressValidator() {
-}
-void PreloadAddressValidator::LoadRules(const std::string& region_code,
- const Callback& loaded) {
- assert(supplier_ != NULL);
- supplier_->LoadRules(region_code, loaded);
+AddressValidator::AddressValidator(const std::string& validation_data_url,
+ scoped_ptr<Downloader> downloader,
+ scoped_ptr<Storage> storage,
+ LoadRulesDelegate* load_rules_delegate)
+ : supplier_(new PreloadSupplier(validation_data_url, downloader.release(),
+ storage.release())),
+ suggestions_(new Suggestions(supplier_.get())),
+ normalizer_(new AddressNormalizer(supplier_.get())),
+ validator_(new ::i18n::addressinput::AddressValidator(supplier_.get())),
+ input_helper_(new AddressInputHelper(supplier_.get())),
+ validated_(BuildCallback(this, &AddressValidator::Validated)),
+ rules_loaded_(BuildCallback(this, &AddressValidator::RulesLoaded)),
+ load_rules_delegate_(load_rules_delegate) {}
+
+AddressValidator::~AddressValidator() {}
+
+void AddressValidator::LoadRules(const std::string& region_code) {
+ DCHECK(supplier_);
+ supplier_->LoadRules(region_code, *rules_loaded_);
}
-PreloadAddressValidator::Status PreloadAddressValidator::Validate(
+AddressValidator::Status AddressValidator::ValidateAddress(
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;
- }
+
+ if (!problems)
+ return SUCCESS;
+
+ AddressData normalized_address = address;
+ normalizer_->Normalize(&normalized_address);
validator_->Validate(
- address,
+ normalized_address,
/*allow_postal*/ false,
/*require_name*/ false,
filter,
@@ -88,57 +91,100 @@ PreloadAddressValidator::Status PreloadAddressValidator::Validate(
return SUCCESS;
}
-PreloadAddressValidator::Status PreloadAddressValidator::GetSuggestions(
+AddressValidator::Status AddressValidator::GetSuggestions(
const AddressData& user_input,
AddressField focused_field,
size_t suggestion_limit,
std::vector<AddressData>* suggestions) const {
- assert(suggestions_ != NULL);
- assert(supplier_ != NULL);
+ 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;
+
+ if (!suggestions)
+ return SUCCESS;
+
+ suggestions->clear();
+
+ 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 = ValidateAddress(address_copy, &filter, &problems);
+ DCHECK(status == SUCCESS);
+ (void)status;
+
+ if (!problems.empty())
+ return SUCCESS;
+
+ input_helper_->FillAddress(&address_copy);
+ }
+
+ if (focused_field == POSTAL_CODE ||
+ (focused_field >= ADMIN_AREA && focused_field <= DEPENDENT_LOCALITY)) {
+ suggestions_->GetSuggestions(address_copy, focused_field, suggestion_limit,
+ suggestions);
}
- suggestions_->GetSuggestions(
- user_input,
- 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 = ValidateAddress(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(
+bool AddressValidator::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;
}
-void PreloadAddressValidator::Validated(bool success,
- const AddressData&,
- const FieldProblemMap&) {
- assert(success);
+void AddressValidator::Validated(bool success,
+ const AddressData&,
+ const FieldProblemMap&) {
+ DCHECK(success);
}
-// Stub constructor for use by MockAddressValidator.
-PreloadAddressValidator::PreloadAddressValidator() {
+void AddressValidator::RulesLoaded(bool success,
+ const std::string& country_code,
+ const int&) {
+ if (load_rules_delegate_)
+ load_rules_delegate_->OnAddressValidationRulesLoaded(country_code, success);
}
+// Stub constructor for use by MockAddressValidator.
+AddressValidator::AddressValidator() : load_rules_delegate_(NULL) {}
+
} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698