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 0732826b9562917ab6b6c7bad98351cb347e3afe..ba48487649b67e35119199e97e44c89a50f9abf9 100644 |
--- a/third_party/libaddressinput/chromium/cpp/src/address_validator.cc |
+++ b/third_party/libaddressinput/chromium/cpp/src/address_validator.cc |
@@ -18,33 +18,109 @@ |
#include <libaddressinput/load_rules_delegate.h> |
#include <libaddressinput/localization.h> |
#include <libaddressinput/storage.h> |
+#include <libaddressinput/util/basictypes.h> |
+#include <libaddressinput/util/scoped_ptr.h> |
+#include <cassert> |
+#include <cstddef> |
+#include <map> |
+#include <set> |
#include <string> |
+#include "country_rules_aggregator.h" |
#include "retriever.h" |
+#include "ruleset.h" |
+#include "util/stl_util.h" |
namespace i18n { |
namespace addressinput { |
-AddressValidator::AddressValidator(scoped_ptr<const Downloader> downloader, |
- scoped_ptr<Storage> storage, |
- LoadRulesDelegate* load_rules_delegate) |
- : retriever_(new Retriever( |
+namespace { |
+ |
+// Validates AddressData structure. |
+class AddressValidatorImpl : public AddressValidator { |
+ public: |
+ // Takes ownership of |downloader| and |storage|. Does not take ownership of |
+ // |load_rules_delegate|. |
+ AddressValidatorImpl(scoped_ptr<const Downloader> downloader, |
+ scoped_ptr<Storage> storage, |
+ LoadRulesDelegate* load_rules_delegate) |
+ : aggregator_(scoped_ptr<Retriever>(new Retriever( |
VALIDATION_DATA_URL, |
downloader.Pass(), |
- scoped_ptr<Storage>(storage.Pass()))), |
- load_rules_delegate_(load_rules_delegate) {} |
+ storage.Pass()))), |
+ load_rules_delegate_(load_rules_delegate), |
+ loading_rules_(), |
+ rules_() {} |
-AddressValidator::~AddressValidator() {} |
+ virtual ~AddressValidatorImpl() { |
+ STLDeleteValues(&rules_); |
+ } |
+ |
+ // AddressValidator implementation. |
+ virtual void LoadRules(const std::string& country_code) { |
+ if (rules_.find(country_code) == rules_.end() && |
+ loading_rules_.find(country_code) == loading_rules_.end()) { |
+ loading_rules_.insert(country_code); |
+ aggregator_.AggregateRules( |
+ country_code, |
+ BuildScopedPtrCallback(this, &AddressValidatorImpl::OnRulesLoaded)); |
+ } |
+ } |
+ |
+ // AddressValidator implementation. |
+ virtual Status ValidateAddress( |
+ const AddressData& address, |
+ const AddressProblemFilter& filter, |
+ const Localization& localization, |
+ AddressProblems* problems) const { |
+ // TODO(rouslan): Validate the address. |
+ return RULES_UNAVAILABLE; |
+ } |
+ |
+ private: |
+ // Called when CountryRulesAggregator::AggregateRules loads the |ruleset| for |
+ // the |country_code|. |
+ void OnRulesLoaded(bool success, |
+ const std::string& country_code, |
+ scoped_ptr<Ruleset> ruleset) { |
+ assert(rules_.find(country_code) == rules_.end()); |
+ loading_rules_.erase(country_code); |
+ if (success) { |
+ rules_[country_code] = ruleset.release(); |
+ } |
+ if (load_rules_delegate_ != NULL) { |
+ load_rules_delegate_->OnAddressValidationRulesLoaded( |
+ country_code, success); |
+ } |
+ } |
+ |
+ // Loads the ruleset for a country code. |
+ CountryRulesAggregator aggregator_; |
-void AddressValidator::LoadRules(const std::string& country_code) {} |
+ // An optional delegate to be invoked when a ruleset finishes loading. |
+ LoadRulesDelegate* load_rules_delegate_; |
+ |
+ // A set of country codes for which a ruleset is being loaded. |
+ std::set<std::string> loading_rules_; |
+ |
+ // A mapping of a country code to the owned ruleset for that country code. |
+ std::map<std::string, Ruleset*> rules_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(AddressValidatorImpl); |
+}; |
+ |
+} // namespace |
+ |
+AddressValidator::~AddressValidator() {} |
-AddressValidator::Status AddressValidator::ValidateAddress( |
- const AddressData& address, |
- const AddressProblemFilter& filter, |
- const Localization& localization, |
- AddressProblems* problems) const { |
- return RULES_UNAVAILABLE; |
+// static |
+scoped_ptr<AddressValidator> AddressValidator::Build( |
+ scoped_ptr<const Downloader> downloader, |
+ scoped_ptr<Storage> storage, |
+ LoadRulesDelegate* load_rules_delegate) { |
+ return scoped_ptr<AddressValidator>(new AddressValidatorImpl( |
+ downloader.Pass(), storage.Pass(), load_rules_delegate)); |
} |
} // namespace addressinput |