Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(75)

Side by Side Diff: third_party/libaddressinput/chromium/cpp/src/address_validator.cc

Issue 109323011: [rac] Download all rules for a country code in libaddressinput. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fix windows build. Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <map>
22 #include <string> 26 #include <string>
27 #include <utility>
23 28
29 #include "country_rules_retriever.h"
24 #include "retriever.h" 30 #include "retriever.h"
31 #include "ruleset.h"
32 #include "util/stl_util.h"
25 #include "validating_storage.h" 33 #include "validating_storage.h"
26 34
27 namespace i18n { 35 namespace i18n {
28 namespace addressinput { 36 namespace addressinput {
29 37
30 AddressValidator::AddressValidator(scoped_ptr<const Downloader> downloader, 38 namespace {
31 scoped_ptr<Storage> storage, 39
32 LoadRulesDelegate* load_rules_delegate) 40 // Validates AddressData structure.
33 : 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 : retriever_(scoped_ptr<Retriever>(new Retriever(
34 VALIDATION_DATA_URL, 49 VALIDATION_DATA_URL,
35 downloader.Pass(), 50 downloader.Pass(),
36 scoped_ptr<Storage>(new ValidatingStorage(storage.Pass())))), 51 scoped_ptr<Storage>(new ValidatingStorage(storage.Pass()))))),
37 load_rules_delegate_(load_rules_delegate) {} 52 load_rules_delegate_(load_rules_delegate),
53 status_(),
54 rules_() {}
55
56 virtual ~AddressValidatorImpl() {
57 STLDeleteContainerPairSecondPointers(rules_.begin(), rules_.end());
58 }
59
60 // AddressValidator implementation.
61 virtual void LoadRules(const std::string& country_code) {
62 std::map<std::string, Status>::const_iterator status_it =
63 status_.find(country_code);
64 if (status_it == status_.end()) {
65 assert(rules_.find(country_code) == rules_.end());
66 status_.insert(std::make_pair(country_code, RULES_NOT_READY));
67 retriever_.RetrieveRules(
68 country_code,
69 BuildScopedPtrCallback(this, &AddressValidatorImpl::OnRulesLoaded));
70 }
71 }
72
73 // AddressValidator implementation.
74 virtual Status ValidateAddress(
75 const AddressData& address,
76 const AddressProblemFilter& filter,
77 const Localization& localization,
78 AddressProblems* problems) const {
79 return RULES_UNAVAILABLE;
80 }
81
82 private:
83 // Called when CountryRulesRetriever::RetrieveRules finishes loading all rules
84 // for the |country_code|.
85 void OnRulesLoaded(bool success,
86 const std::string& country_code,
87 scoped_ptr<Ruleset> ruleset) {
88 assert(rules_.find(country_code) == rules_.end());
89 if (success) {
90 status_[country_code] = SUCCESS;
91 rules_.insert(std::make_pair(country_code, ruleset.release()));
92 } else {
93 status_.erase(country_code);
94 }
95 if (load_rules_delegate_) {
96 load_rules_delegate_->OnAddressValidationRulesLoaded(
97 country_code, success);
98 }
99 }
100
101 // Retrieves all rules for a country code.
102 CountryRulesRetriever retriever_;
103
104 // An optional delegate to be invoked when rules for a country code finish
105 // loading.
106 LoadRulesDelegate* load_rules_delegate_;
107
108 // A mapping of a country code to the status of its rule download.
109 std::map<std::string, Status> status_;
110
111 // A mapping of a country code to all rules loaded for that country code.
112 std::map<std::string, Ruleset*> rules_;
113
114 DISALLOW_COPY_AND_ASSIGN(AddressValidatorImpl);
115 };
116
117 } // namespace
38 118
39 AddressValidator::~AddressValidator() {} 119 AddressValidator::~AddressValidator() {}
40 120
41 void AddressValidator::LoadRules(const std::string& country_code) {} 121 // static
42 122 scoped_ptr<AddressValidator> AddressValidator::Build(
43 AddressValidator::Status AddressValidator::ValidateAddress( 123 scoped_ptr<const Downloader> downloader,
44 const AddressData& address, 124 scoped_ptr<Storage> storage,
45 const AddressProblemFilter& filter, 125 LoadRulesDelegate* load_rules_delegate) {
46 const Localization& localization, 126 return scoped_ptr<AddressValidator>(new AddressValidatorImpl(
47 AddressProblems* problems) const { 127 downloader.Pass(), storage.Pass(), load_rules_delegate));
48 return RULES_UNAVAILABLE;
49 } 128 }
50 129
51 } // namespace addressinput 130 } // namespace addressinput
52 } // namespace i18n 131 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698