OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" | 5 #include "third_party/libaddressinput/chromium/chrome_address_validator.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
10 #include "base/location.h" | 10 #include "base/location.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/single_thread_task_runner.h" | 12 #include "base/single_thread_task_runner.h" |
13 #include "base/threading/thread_task_runner_handle.h" | 13 #include "base/threading/thread_task_runner_handle.h" |
14 #include "third_party/libaddressinput/chromium/addressinput_util.h" | 14 #include "third_party/libaddressinput/chromium/addressinput_util.h" |
15 #include "third_party/libaddressinput/chromium/input_suggester.h" | 15 #include "third_party/libaddressinput/chromium/input_suggester.h" |
16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" | 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da ta.h" |
17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_no rmalizer.h" | 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_no rmalizer.h" |
18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" | 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h" |
19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
20 #include "third_party/libaddressinput/src/cpp/src/rule.h" | |
20 | 21 |
21 namespace autofill { | 22 namespace autofill { |
22 namespace { | 23 namespace { |
23 | 24 |
24 using ::i18n::addressinput::AddressData; | 25 using ::i18n::addressinput::AddressData; |
25 using ::i18n::addressinput::AddressField; | 26 using ::i18n::addressinput::AddressField; |
26 using ::i18n::addressinput::AddressNormalizer; | 27 using ::i18n::addressinput::AddressNormalizer; |
27 using ::i18n::addressinput::BuildCallback; | 28 using ::i18n::addressinput::BuildCallback; |
28 using ::i18n::addressinput::FieldProblemMap; | 29 using ::i18n::addressinput::FieldProblemMap; |
29 using ::i18n::addressinput::PreloadSupplier; | 30 using ::i18n::addressinput::PreloadSupplier; |
31 using ::i18n::addressinput::Rule; | |
30 using ::i18n::addressinput::Source; | 32 using ::i18n::addressinput::Source; |
31 using ::i18n::addressinput::Storage; | 33 using ::i18n::addressinput::Storage; |
32 | 34 |
33 using ::i18n::addressinput::ADMIN_AREA; | 35 using ::i18n::addressinput::ADMIN_AREA; |
34 using ::i18n::addressinput::DEPENDENT_LOCALITY; | 36 using ::i18n::addressinput::DEPENDENT_LOCALITY; |
35 using ::i18n::addressinput::POSTAL_CODE; | 37 using ::i18n::addressinput::POSTAL_CODE; |
36 | 38 |
37 // The maximum number attempts to load rules. | 39 // The maximum number attempts to load rules. |
38 static const int kMaxAttemptsNumber = 8; | 40 static const int kMaxAttemptsNumber = 8; |
39 | 41 |
(...skipping 11 matching lines...) Expand all Loading... | |
51 load_rules_listener_(load_rules_listener), | 53 load_rules_listener_(load_rules_listener), |
52 weak_factory_(this) {} | 54 weak_factory_(this) {} |
53 | 55 |
54 AddressValidator::~AddressValidator() {} | 56 AddressValidator::~AddressValidator() {} |
55 | 57 |
56 void AddressValidator::LoadRules(const std::string& region_code) { | 58 void AddressValidator::LoadRules(const std::string& region_code) { |
57 attempts_number_[region_code] = 0; | 59 attempts_number_[region_code] = 0; |
58 supplier_->LoadRules(region_code, *rules_loaded_); | 60 supplier_->LoadRules(region_code, *rules_loaded_); |
59 } | 61 } |
60 | 62 |
63 std::vector<std::string> AddressValidator::GetRegionSubKeys( | |
64 const std::string& region_code) { | |
65 if (!AreRulesLoadedForRegion(region_code)) | |
66 return std::vector<std::string>(); | |
67 | |
68 auto rules = supplier_->GetRulesForRegion(region_code); | |
69 auto rule_iterator = rules.find("data/" + region_code); | |
70 | |
71 if (rule_iterator != rules.end()) { | |
72 if (rule_iterator->second) | |
73 return (rule_iterator->second)->GetSubKeys(); | |
74 return std::vector<std::string>(); | |
75 } else { | |
76 return std::vector<std::string>(); | |
77 } | |
78 } | |
79 | |
61 AddressValidator::Status AddressValidator::ValidateAddress( | 80 AddressValidator::Status AddressValidator::ValidateAddress( |
62 const AddressData& address, | 81 const AddressData& address, |
63 const FieldProblemMap* filter, | 82 const FieldProblemMap* filter, |
64 FieldProblemMap* problems) const { | 83 FieldProblemMap* problems) const { |
65 if (supplier_->IsPending(address.region_code)) { | 84 if (supplier_->IsPending(address.region_code)) { |
66 if (problems) | 85 if (problems) |
67 addressinput::ValidateRequiredFields(address, filter, problems); | 86 addressinput::ValidateRequiredFields(address, filter, problems); |
68 return RULES_NOT_READY; | 87 return RULES_NOT_READY; |
69 } | 88 } |
70 | 89 |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 | 152 |
134 void AddressValidator::Validated(bool success, | 153 void AddressValidator::Validated(bool success, |
135 const AddressData&, | 154 const AddressData&, |
136 const FieldProblemMap&) { | 155 const FieldProblemMap&) { |
137 DCHECK(success); | 156 DCHECK(success); |
138 } | 157 } |
139 | 158 |
140 void AddressValidator::RulesLoaded(bool success, | 159 void AddressValidator::RulesLoaded(bool success, |
141 const std::string& region_code, | 160 const std::string& region_code, |
142 int) { | 161 int) { |
143 if (load_rules_listener_) | 162 if (load_rules_listener_) { |
sebsg
2017/02/28 16:14:00
no need for brackets.
Parastoo
2017/03/21 14:30:44
Done.
| |
144 load_rules_listener_->OnAddressValidationRulesLoaded(region_code, success); | 163 load_rules_listener_->OnAddressRulesLoaded(region_code, success); |
164 } | |
145 | 165 |
146 // Count the first failed attempt to load rules as well. | 166 // Count the first failed attempt to load rules as well. |
147 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber) | 167 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber) |
148 return; | 168 return; |
149 | 169 |
150 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 170 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
151 FROM_HERE, base::Bind(&AddressValidator::RetryLoadRules, | 171 FROM_HERE, base::Bind(&AddressValidator::RetryLoadRules, |
152 weak_factory_.GetWeakPtr(), region_code), | 172 weak_factory_.GetWeakPtr(), region_code), |
153 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); | 173 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); |
154 } | 174 } |
155 | 175 |
156 void AddressValidator::RetryLoadRules(const std::string& region_code) { | 176 void AddressValidator::RetryLoadRules(const std::string& region_code) { |
157 // Do not reset retry count. | 177 // Do not reset retry count. |
158 supplier_->LoadRules(region_code, *rules_loaded_); | 178 supplier_->LoadRules(region_code, *rules_loaded_); |
159 } | 179 } |
160 | 180 |
161 } // namespace autofill | 181 } // namespace autofill |
OLD | NEW |