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

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 mac 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 33
26 namespace i18n { 34 namespace i18n {
27 namespace addressinput { 35 namespace addressinput {
28 36
29 AddressValidator::AddressValidator(scoped_ptr<const Downloader> downloader, 37 namespace {
30 scoped_ptr<Storage> storage, 38
31 LoadRulesDelegate* load_rules_delegate) 39 // Validates AddressData structure.
32 : retriever_(new Retriever( 40 class AddressValidatorImpl : public AddressValidator {
41 public:
42 // Takes ownership of |downloader| and |storage|. Does not take ownership of
43 // |load_rules_delegate|.
44 AddressValidatorImpl(scoped_ptr<const Downloader> downloader,
45 scoped_ptr<Storage> storage,
46 LoadRulesDelegate* load_rules_delegate)
47 : retriever_(scoped_ptr<Retriever>(new Retriever(
33 VALIDATION_DATA_URL, 48 VALIDATION_DATA_URL,
34 downloader.Pass(), 49 downloader.Pass(),
35 scoped_ptr<Storage>(storage.Pass()))), 50 scoped_ptr<Storage>(storage.Pass())))),
Evan Stade 2014/01/08 21:08:59 I'm not sure but I don't think you need the scoped
please use gerrit instead 2014/01/09 00:38:11 Done.
36 load_rules_delegate_(load_rules_delegate) {} 51 load_rules_delegate_(load_rules_delegate),
52 status_(),
53 rules_() {}
54
55 virtual ~AddressValidatorImpl() {
56 STLDeleteContainerPairSecondPointers(rules_.begin(), rules_.end());
Evan Stade 2014/01/08 21:08:59 STLDeleteValues
please use gerrit instead 2014/01/09 00:38:11 Done.
57 }
58
59 // AddressValidator implementation.
60 virtual void LoadRules(const std::string& country_code) {
61 std::map<std::string, Status>::const_iterator status_it =
62 status_.find(country_code);
63 if (status_it == status_.end()) {
64 assert(rules_.find(country_code) == rules_.end());
65 status_.insert(std::make_pair(country_code, RULES_NOT_READY));
Evan Stade 2014/01/08 21:08:59 do you really need this to be a map? It seems like
please use gerrit instead 2014/01/09 00:38:11 Done.
66 retriever_.RetrieveRules(
67 country_code,
68 BuildScopedPtrCallback(this, &AddressValidatorImpl::OnRulesLoaded));
69 }
70 }
71
72 // AddressValidator implementation.
73 virtual Status ValidateAddress(
74 const AddressData& address,
75 const AddressProblemFilter& filter,
76 const Localization& localization,
77 AddressProblems* problems) const {
78 return RULES_UNAVAILABLE;
Evan Stade 2014/01/08 21:08:59 add TODO to implement (so there's no confusion abo
please use gerrit instead 2014/01/09 00:38:11 Done.
79 }
80
81 private:
82 // Called when CountryRulesRetriever::RetrieveRules finishes loading all rules
83 // for 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 if (success) {
89 status_[country_code] = SUCCESS;
90 rules_.insert(std::make_pair(country_code, ruleset.release()));
91 } else {
92 status_.erase(country_code);
93 }
94 if (load_rules_delegate_) {
95 load_rules_delegate_->OnAddressValidationRulesLoaded(
96 country_code, success);
97 }
98 }
99
100 // Retrieves all rules for a country code.
101 CountryRulesRetriever retriever_;
102
103 // An optional delegate to be invoked when rules for a country code finish
104 // loading.
105 LoadRulesDelegate* load_rules_delegate_;
106
107 // A mapping of a country code to the status of its rule download.
108 std::map<std::string, Status> status_;
109
110 // A mapping of a country code to all rules loaded for that country code.
Evan Stade 2014/01/08 21:08:59 Ruleset ownership comment plx
please use gerrit instead 2014/01/09 00:38:11 Done.
111 std::map<std::string, Ruleset*> rules_;
112
113 DISALLOW_COPY_AND_ASSIGN(AddressValidatorImpl);
114 };
115
116 } // namespace
37 117
38 AddressValidator::~AddressValidator() {} 118 AddressValidator::~AddressValidator() {}
39 119
40 void AddressValidator::LoadRules(const std::string& country_code) {} 120 // static
41 121 scoped_ptr<AddressValidator> AddressValidator::Build(
42 AddressValidator::Status AddressValidator::ValidateAddress( 122 scoped_ptr<const Downloader> downloader,
43 const AddressData& address, 123 scoped_ptr<Storage> storage,
44 const AddressProblemFilter& filter, 124 LoadRulesDelegate* load_rules_delegate) {
45 const Localization& localization, 125 return scoped_ptr<AddressValidator>(new AddressValidatorImpl(
46 AddressProblems* problems) const { 126 downloader.Pass(), storage.Pass(), load_rules_delegate));
47 return RULES_UNAVAILABLE;
48 } 127 }
49 128
50 } // namespace addressinput 129 } // namespace addressinput
51 } // namespace i18n 130 } // namespace i18n
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698