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 #include <libaddressinput/util/basictypes.h> | |
22 #include <libaddressinput/util/scoped_ptr.h> | |
21 | 23 |
24 #include <cassert> | |
25 #include <cstddef> | |
26 #include <map> | |
27 #include <set> | |
22 #include <string> | 28 #include <string> |
23 | 29 |
30 #include "country_rules_aggregator.h" | |
24 #include "retriever.h" | 31 #include "retriever.h" |
32 #include "ruleset.h" | |
33 #include "util/stl_util.h" | |
25 | 34 |
26 namespace i18n { | 35 namespace i18n { |
27 namespace addressinput { | 36 namespace addressinput { |
28 | 37 |
29 AddressValidator::AddressValidator(scoped_ptr<const Downloader> downloader, | 38 namespace { |
30 scoped_ptr<Storage> storage, | 39 |
31 LoadRulesDelegate* load_rules_delegate) | 40 // Validates AddressData structure. |
32 : retriever_(new Retriever( | 41 class AddressValidatorImpl : public AddressValidator { |
42 public: | |
43 // Takes ownership of |downloader| and |storage|. Does not take ownership of | |
44 // |load_rules_delegate|. | |
45 AddressValidatorImpl(scoped_ptr<const Downloader> downloader, | |
46 scoped_ptr<Storage> storage, | |
47 LoadRulesDelegate* load_rules_delegate) | |
48 : aggregator_(scoped_ptr<Retriever>(new Retriever( | |
Evan Stade
2014/01/09 01:01:39
I'm thinking the storage validation can go into Re
please use gerrit instead
2014/01/09 01:19:08
Yes, the Retriever is the correct place for valida
| |
33 VALIDATION_DATA_URL, | 49 VALIDATION_DATA_URL, |
34 downloader.Pass(), | 50 downloader.Pass(), |
35 scoped_ptr<Storage>(storage.Pass()))), | 51 storage.Pass()))), |
36 load_rules_delegate_(load_rules_delegate) {} | 52 load_rules_delegate_(load_rules_delegate), |
53 loading_rules_(), | |
54 rules_() {} | |
55 | |
56 virtual ~AddressValidatorImpl() { | |
57 STLDeleteValues(&rules_); | |
58 } | |
59 | |
60 // AddressValidator implementation. | |
61 virtual void LoadRules(const std::string& country_code) { | |
62 if (rules_.find(country_code) == rules_.end() && | |
63 loading_rules_.find(country_code) == loading_rules_.end()) { | |
64 loading_rules_.insert(country_code); | |
65 aggregator_.AggregateRules( | |
66 country_code, | |
67 BuildScopedPtrCallback(this, &AddressValidatorImpl::OnRulesLoaded)); | |
68 } | |
69 } | |
70 | |
71 // AddressValidator implementation. | |
72 virtual Status ValidateAddress( | |
73 const AddressData& address, | |
74 const AddressProblemFilter& filter, | |
75 const Localization& localization, | |
76 AddressProblems* problems) const { | |
77 // TODO(rouslan): Validate the address. | |
78 return RULES_UNAVAILABLE; | |
79 } | |
80 | |
81 private: | |
82 // Called when CountryRulesAggregator::AggregateRules loads the |ruleset| for | |
83 // the |country_code|. | |
84 void OnRulesLoaded(bool success, | |
85 const std::string& country_code, | |
86 scoped_ptr<Ruleset> ruleset) { | |
87 assert(rules_.find(country_code) == rules_.end()); | |
88 loading_rules_.erase(country_code); | |
89 if (success) { | |
90 rules_[country_code] = ruleset.release(); | |
91 } | |
92 if (load_rules_delegate_ != NULL) { | |
93 load_rules_delegate_->OnAddressValidationRulesLoaded( | |
94 country_code, success); | |
95 } | |
96 } | |
97 | |
98 // Loads the ruleset for a country code. | |
99 CountryRulesAggregator aggregator_; | |
100 | |
101 // An optional delegate to be invoked when a ruleset finishes loading. | |
102 LoadRulesDelegate* load_rules_delegate_; | |
103 | |
104 // A set of country codes for which a ruleset is being loaded. | |
105 std::set<std::string> loading_rules_; | |
106 | |
107 // A mapping of a country code to the owned ruleset for that country code. | |
108 std::map<std::string, Ruleset*> rules_; | |
109 | |
110 DISALLOW_COPY_AND_ASSIGN(AddressValidatorImpl); | |
111 }; | |
112 | |
113 } // namespace | |
37 | 114 |
38 AddressValidator::~AddressValidator() {} | 115 AddressValidator::~AddressValidator() {} |
39 | 116 |
40 void AddressValidator::LoadRules(const std::string& country_code) {} | 117 // static |
41 | 118 scoped_ptr<AddressValidator> AddressValidator::Build( |
42 AddressValidator::Status AddressValidator::ValidateAddress( | 119 scoped_ptr<const Downloader> downloader, |
43 const AddressData& address, | 120 scoped_ptr<Storage> storage, |
44 const AddressProblemFilter& filter, | 121 LoadRulesDelegate* load_rules_delegate) { |
45 const Localization& localization, | 122 return scoped_ptr<AddressValidator>(new AddressValidatorImpl( |
46 AddressProblems* problems) const { | 123 downloader.Pass(), storage.Pass(), load_rules_delegate)); |
47 return RULES_UNAVAILABLE; | |
48 } | 124 } |
49 | 125 |
50 } // namespace addressinput | 126 } // namespace addressinput |
51 } // namespace i18n | 127 } // namespace i18n |
OLD | NEW |