| OLD | NEW |
| 1 // Copyright (C) 2013 Google Inc. | 1 // Copyright (C) 2013 Google Inc. |
| 2 // | 2 // |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | 3 // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 // you may not use this file except in compliance with the License. | 4 // you may not use this file except in compliance with the License. |
| 5 // You may obtain a copy of the License at | 5 // You may obtain a copy of the License at |
| 6 // | 6 // |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | 7 // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 // | 8 // |
| 9 // Unless required by applicable law or agreed to in writing, software | 9 // Unless required by applicable law or agreed to in writing, software |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | 10 // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 // See the License for the specific language governing permissions and | 12 // See the License for the specific language governing permissions and |
| 13 // limitations under the License. | 13 // limitations under the License. |
| 14 | 14 |
| 15 #include <libaddressinput/address_validator.h> | 15 #include <libaddressinput/address_validator.h> |
| 16 | 16 |
| 17 #include <libaddressinput/downloader.h> | 17 #include <libaddressinput/downloader.h> |
| 18 #include <libaddressinput/load_rules_delegate.h> | 18 #include <libaddressinput/load_rules_delegate.h> |
| 19 #include <libaddressinput/localization.h> | 19 #include <libaddressinput/localization.h> |
| 20 #include <libaddressinput/storage.h> | 20 #include <libaddressinput/storage.h> |
| 21 | 21 |
| 22 #include <cassert> |
| 23 #include <map> |
| 22 #include <string> | 24 #include <string> |
| 25 #include <utility> |
| 23 | 26 |
| 27 #include "country_rules_retriever.h" |
| 28 #include "lookup_key.h" |
| 24 #include "retriever.h" | 29 #include "retriever.h" |
| 25 #include "rule_retriever.h" | 30 #include "rule_retriever.h" |
| 26 #include "validating_storage.h" | 31 #include "validating_storage.h" |
| 27 | 32 |
| 28 namespace i18n { | 33 namespace i18n { |
| 29 namespace addressinput { | 34 namespace addressinput { |
| 30 | 35 |
| 31 AddressValidator::AddressValidator(const Downloader* downloader, | 36 namespace { |
| 37 |
| 38 class AddressValidatorImpl : public AddressValidator { |
| 39 public: |
| 40 struct CountryRules { |
| 41 CountryRules(const std::string& country_code, |
| 42 CountryRulesRetriever::Callback* rules_ready) |
| 43 : status(RULES_NOT_READY), |
| 44 root(country_code), |
| 45 rules_ready(rules_ready) {} |
| 46 |
| 47 ~CountryRules() {} |
| 48 |
| 49 Status status; |
| 50 LookupKey root; |
| 51 scoped_ptr<CountryRulesRetriever::Callback> rules_ready; |
| 52 }; |
| 53 |
| 54 typedef std::map<std::string, CountryRules*> CountryRulesMap; |
| 55 |
| 56 AddressValidatorImpl(const Downloader* downloader, |
| 32 Storage* storage, | 57 Storage* storage, |
| 33 LoadRulesDelegate* load_rules_delegate) | 58 LoadRulesDelegate* load_rules_delegate) |
| 34 : rule_retriever_(new RuleRetriever(new Retriever( | 59 : retriever_(new CountryRulesRetriever(new RuleRetriever(new Retriever( |
| 35 VALIDATION_DATA_URL, downloader, new ValidatingStorage(storage)))), | 60 VALIDATION_DATA_URL, downloader, new ValidatingStorage(storage))))), |
| 36 load_rules_delegate_(load_rules_delegate) {} | 61 load_rules_delegate_(load_rules_delegate) {} |
| 37 | 62 |
| 63 virtual ~AddressValidatorImpl() { |
| 64 for (CountryRulesMap::const_iterator rules_it = rules_.begin(); |
| 65 rules_it != rules_.end(); ++rules_it) { |
| 66 // TODO(rouslan): Carefully double-check that this does not cause crashes |
| 67 // if AddressValidatorImpl is deleted before all downloades are finished. |
| 68 delete rules_it->second; |
| 69 } |
| 70 } |
| 71 |
| 72 virtual void LoadRules(const std::string& country_code) { |
| 73 CountryRulesMap::const_iterator rules_it = rules_.find(country_code); |
| 74 if (rules_it == rules_.end()) { |
| 75 CountryRules* country_rules = new CountryRules( |
| 76 country_code, |
| 77 BuildCallback(this, &AddressValidatorImpl::OnRulesLoaded)); |
| 78 rules_.insert(std::make_pair(country_code, country_rules)); |
| 79 retriever_->RetrieveRules(country_code, |
| 80 *country_rules->rules_ready, |
| 81 &country_rules->root); |
| 82 return; |
| 83 } |
| 84 |
| 85 if (rules_it->second->status == RULES_UNAVAILABLE) { |
| 86 rules_it->second->status = RULES_NOT_READY; |
| 87 retriever_->RetrieveRules(country_code, |
| 88 *rules_it->second->rules_ready, |
| 89 &rules_it->second->root); |
| 90 } |
| 91 } |
| 92 |
| 93 virtual Status ValidateAddress( |
| 94 const AddressData& address, |
| 95 const AddressProblemFilter& filter, |
| 96 const Localization& localization, |
| 97 AddressProblems* problems) const { |
| 98 return RULES_UNAVAILABLE; |
| 99 } |
| 100 |
| 101 private: |
| 102 void OnRulesLoaded(bool success, |
| 103 const std::string& country_code, |
| 104 const LookupKey& lookup_key) { |
| 105 CountryRulesMap::const_iterator rules_it = rules_.find(country_code); |
| 106 assert(rules_it != rules_.end()); |
| 107 rules_it->second->status = success ? SUCCESS : RULES_UNAVAILABLE; |
| 108 if (load_rules_delegate_) { |
| 109 load_rules_delegate_->OnAddressValidationRulesLoaded( |
| 110 country_code, success); |
| 111 } |
| 112 } |
| 113 |
| 114 scoped_ptr<const CountryRulesRetriever> retriever_; |
| 115 LoadRulesDelegate* load_rules_delegate_; |
| 116 CountryRulesMap rules_; |
| 117 |
| 118 DISALLOW_COPY_AND_ASSIGN(AddressValidatorImpl); |
| 119 }; |
| 120 |
| 121 } // namespace |
| 122 |
| 123 // static |
| 124 AddressValidator* AddressValidator::Build( |
| 125 const Downloader* downloader, |
| 126 Storage* storage, |
| 127 LoadRulesDelegate* load_rules_delegate) { |
| 128 return new AddressValidatorImpl(downloader, storage, load_rules_delegate); |
| 129 } |
| 130 |
| 38 AddressValidator::~AddressValidator() {} | 131 AddressValidator::~AddressValidator() {} |
| 39 | 132 |
| 40 void AddressValidator::LoadRules(const std::string& country_code) {} | |
| 41 | |
| 42 AddressValidator::Status AddressValidator::ValidateAddress( | |
| 43 const AddressData& address, | |
| 44 const AddressProblemFilter& filter, | |
| 45 const Localization& localization, | |
| 46 AddressProblems* problems) const { | |
| 47 return RULES_UNAVAILABLE; | |
| 48 } | |
| 49 | |
| 50 } // namespace addressinput | 133 } // namespace addressinput |
| 51 } // namespace i18n | 134 } // namespace i18n |
| OLD | NEW |