| Index: third_party/libaddressinput/chromium/cpp/src/address_validator.cc
|
| diff --git a/third_party/libaddressinput/chromium/cpp/src/address_validator.cc b/third_party/libaddressinput/chromium/cpp/src/address_validator.cc
|
| index 011396d336f69c04ce7c133b75fc1dfe9ef66882..401edc5ca07d2fa67cb25f078acc7ac165b285cc 100644
|
| --- a/third_party/libaddressinput/chromium/cpp/src/address_validator.cc
|
| +++ b/third_party/libaddressinput/chromium/cpp/src/address_validator.cc
|
| @@ -19,8 +19,13 @@
|
| #include <libaddressinput/localization.h>
|
| #include <libaddressinput/storage.h>
|
|
|
| +#include <cassert>
|
| +#include <map>
|
| #include <string>
|
| +#include <utility>
|
|
|
| +#include "country_rules_retriever.h"
|
| +#include "lookup_key.h"
|
| #include "retriever.h"
|
| #include "rule_retriever.h"
|
| #include "validating_storage.h"
|
| @@ -28,24 +33,102 @@
|
| namespace i18n {
|
| namespace addressinput {
|
|
|
| -AddressValidator::AddressValidator(const Downloader* downloader,
|
| +namespace {
|
| +
|
| +class AddressValidatorImpl : public AddressValidator {
|
| + public:
|
| + struct CountryRules {
|
| + CountryRules(const std::string& country_code,
|
| + CountryRulesRetriever::Callback* rules_ready)
|
| + : status(RULES_NOT_READY),
|
| + root(country_code),
|
| + rules_ready(rules_ready) {}
|
| +
|
| + ~CountryRules() {}
|
| +
|
| + Status status;
|
| + LookupKey root;
|
| + scoped_ptr<CountryRulesRetriever::Callback> rules_ready;
|
| + };
|
| +
|
| + typedef std::map<std::string, CountryRules*> CountryRulesMap;
|
| +
|
| + AddressValidatorImpl(const Downloader* downloader,
|
| Storage* storage,
|
| LoadRulesDelegate* load_rules_delegate)
|
| - : rule_retriever_(new RuleRetriever(new Retriever(
|
| - VALIDATION_DATA_URL, downloader, new ValidatingStorage(storage)))),
|
| + : retriever_(new CountryRulesRetriever(new RuleRetriever(new Retriever(
|
| + VALIDATION_DATA_URL, downloader, new ValidatingStorage(storage))))),
|
| load_rules_delegate_(load_rules_delegate) {}
|
|
|
| -AddressValidator::~AddressValidator() {}
|
| + virtual ~AddressValidatorImpl() {
|
| + for (CountryRulesMap::const_iterator rules_it = rules_.begin();
|
| + rules_it != rules_.end(); ++rules_it) {
|
| + // TODO(rouslan): Carefully double-check that this does not cause crashes
|
| + // if AddressValidatorImpl is deleted before all downloades are finished.
|
| + delete rules_it->second;
|
| + }
|
| + }
|
| +
|
| + virtual void LoadRules(const std::string& country_code) {
|
| + CountryRulesMap::const_iterator rules_it = rules_.find(country_code);
|
| + if (rules_it == rules_.end()) {
|
| + CountryRules* country_rules = new CountryRules(
|
| + country_code,
|
| + BuildCallback(this, &AddressValidatorImpl::OnRulesLoaded));
|
| + rules_.insert(std::make_pair(country_code, country_rules));
|
| + retriever_->RetrieveRules(country_code,
|
| + *country_rules->rules_ready,
|
| + &country_rules->root);
|
| + return;
|
| + }
|
|
|
| -void AddressValidator::LoadRules(const std::string& country_code) {}
|
| + if (rules_it->second->status == RULES_UNAVAILABLE) {
|
| + rules_it->second->status = RULES_NOT_READY;
|
| + retriever_->RetrieveRules(country_code,
|
| + *rules_it->second->rules_ready,
|
| + &rules_it->second->root);
|
| + }
|
| + }
|
|
|
| -AddressValidator::Status AddressValidator::ValidateAddress(
|
| - const AddressData& address,
|
| - const AddressProblemFilter& filter,
|
| - const Localization& localization,
|
| - AddressProblems* problems) const {
|
| - return RULES_UNAVAILABLE;
|
| + virtual Status ValidateAddress(
|
| + const AddressData& address,
|
| + const AddressProblemFilter& filter,
|
| + const Localization& localization,
|
| + AddressProblems* problems) const {
|
| + return RULES_UNAVAILABLE;
|
| + }
|
| +
|
| + private:
|
| + void OnRulesLoaded(bool success,
|
| + const std::string& country_code,
|
| + const LookupKey& lookup_key) {
|
| + CountryRulesMap::const_iterator rules_it = rules_.find(country_code);
|
| + assert(rules_it != rules_.end());
|
| + rules_it->second->status = success ? SUCCESS : RULES_UNAVAILABLE;
|
| + if (load_rules_delegate_) {
|
| + load_rules_delegate_->OnAddressValidationRulesLoaded(
|
| + country_code, success);
|
| + }
|
| + }
|
| +
|
| + scoped_ptr<const CountryRulesRetriever> retriever_;
|
| + LoadRulesDelegate* load_rules_delegate_;
|
| + CountryRulesMap rules_;
|
| +
|
| + DISALLOW_COPY_AND_ASSIGN(AddressValidatorImpl);
|
| +};
|
| +
|
| +} // namespace
|
| +
|
| +// static
|
| +AddressValidator* AddressValidator::Build(
|
| + const Downloader* downloader,
|
| + Storage* storage,
|
| + LoadRulesDelegate* load_rules_delegate) {
|
| + return new AddressValidatorImpl(downloader, storage, load_rules_delegate);
|
| }
|
|
|
| +AddressValidator::~AddressValidator() {}
|
| +
|
| } // namespace addressinput
|
| } // namespace i18n
|
|
|