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 193575abd8620b01f8a744c5f253946e1dec10b9..99be3cdb04adc01c79d372db35ba9efcd32f495e 100644 |
--- a/third_party/libaddressinput/chromium/cpp/src/address_validator.cc |
+++ b/third_party/libaddressinput/chromium/cpp/src/address_validator.cc |
@@ -18,34 +18,113 @@ |
#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 <map> |
#include <string> |
+#include <utility> |
+#include "country_rules_retriever.h" |
#include "retriever.h" |
+#include "ruleset.h" |
+#include "util/stl_util.h" |
#include "validating_storage.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) |
+ : retriever_(scoped_ptr<const Retriever>(new Retriever( |
VALIDATION_DATA_URL, |
downloader.Pass(), |
- scoped_ptr<Storage>(new ValidatingStorage(storage.Pass())))), |
- load_rules_delegate_(load_rules_delegate) {} |
+ scoped_ptr<Storage>(new ValidatingStorage(storage.Pass()))))), |
+ load_rules_delegate_(load_rules_delegate), |
+ status_(), |
+ rules_() {} |
-AddressValidator::~AddressValidator() {} |
+ virtual ~AddressValidatorImpl() { |
+ STLDeleteContainerPairSecondPointers(rules_.begin(), rules_.end()); |
+ } |
+ |
+ // AddressValidator implementation. |
+ virtual void LoadRules(const std::string& country_code) { |
+ std::map<std::string, Status>::const_iterator status_it = |
+ status_.find(country_code); |
+ if (status_it == status_.end()) { |
+ assert(rules_.find(country_code) == rules_.end()); |
+ status_.insert(std::make_pair(country_code, RULES_NOT_READY)); |
+ retriever_.RetrieveRules( |
+ country_code, |
+ BuildScopedPtrCallback(this, &AddressValidatorImpl::OnRulesLoaded)); |
+ } |
+ } |
+ |
+ // AddressValidator implementation. |
+ virtual Status ValidateAddress( |
+ const AddressData& address, |
+ const AddressProblemFilter& filter, |
+ const Localization& localization, |
+ AddressProblems* problems) const { |
+ return RULES_UNAVAILABLE; |
+ } |
+ |
+ private: |
+ // Called when CountryRulesRetriever::RetrieveRules finishes loading all rules |
+ // for the |country_code|. |
+ void OnRulesLoaded(bool success, |
+ const std::string& country_code, |
+ scoped_ptr<Ruleset> ruleset) { |
+ assert(rules_.find(country_code) == rules_.end()); |
+ if (success) { |
+ status_[country_code] = SUCCESS; |
+ rules_.insert(std::make_pair(country_code, ruleset.release())); |
+ } else { |
+ status_.erase(country_code); |
+ } |
+ if (load_rules_delegate_) { |
+ load_rules_delegate_->OnAddressValidationRulesLoaded( |
+ country_code, success); |
+ } |
+ } |
+ |
+ // Retrieves all rules for a country code. |
+ const CountryRulesRetriever retriever_; |
-void AddressValidator::LoadRules(const std::string& country_code) {} |
+ // An optional delegate to be invoked when rules for a country code finish |
+ // loading. |
+ LoadRulesDelegate* load_rules_delegate_; |
+ |
+ // A mapping of a country code to the status of its rule download. |
+ std::map<std::string, Status> status_; |
+ |
+ // A mapping of a country code to all rules loaded 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 |