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() || !rule_iterator->second) |
| 72 return std::vector<std::string>(); |
| 73 |
| 74 return rule_iterator->second->GetSubKeys(); |
| 75 } |
| 76 |
61 AddressValidator::Status AddressValidator::ValidateAddress( | 77 AddressValidator::Status AddressValidator::ValidateAddress( |
62 const AddressData& address, | 78 const AddressData& address, |
63 const FieldProblemMap* filter, | 79 const FieldProblemMap* filter, |
64 FieldProblemMap* problems) const { | 80 FieldProblemMap* problems) const { |
65 if (supplier_->IsPending(address.region_code)) { | 81 if (supplier_->IsPending(address.region_code)) { |
66 if (problems) | 82 if (problems) |
67 addressinput::ValidateRequiredFields(address, filter, problems); | 83 addressinput::ValidateRequiredFields(address, filter, problems); |
68 return RULES_NOT_READY; | 84 return RULES_NOT_READY; |
69 } | 85 } |
70 | 86 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
134 void AddressValidator::Validated(bool success, | 150 void AddressValidator::Validated(bool success, |
135 const AddressData&, | 151 const AddressData&, |
136 const FieldProblemMap&) { | 152 const FieldProblemMap&) { |
137 DCHECK(success); | 153 DCHECK(success); |
138 } | 154 } |
139 | 155 |
140 void AddressValidator::RulesLoaded(bool success, | 156 void AddressValidator::RulesLoaded(bool success, |
141 const std::string& region_code, | 157 const std::string& region_code, |
142 int) { | 158 int) { |
143 if (load_rules_listener_) | 159 if (load_rules_listener_) |
144 load_rules_listener_->OnAddressValidationRulesLoaded(region_code, success); | 160 load_rules_listener_->OnAddressRulesLoaded(region_code, success); |
145 | 161 |
146 // Count the first failed attempt to load rules as well. | 162 // Count the first failed attempt to load rules as well. |
147 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber) | 163 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber) |
148 return; | 164 return; |
149 | 165 |
150 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 166 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
151 FROM_HERE, base::Bind(&AddressValidator::RetryLoadRules, | 167 FROM_HERE, base::Bind(&AddressValidator::RetryLoadRules, |
152 weak_factory_.GetWeakPtr(), region_code), | 168 weak_factory_.GetWeakPtr(), region_code), |
153 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); | 169 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); |
154 } | 170 } |
155 | 171 |
156 void AddressValidator::RetryLoadRules(const std::string& region_code) { | 172 void AddressValidator::RetryLoadRules(const std::string& region_code) { |
157 // Do not reset retry count. | 173 // Do not reset retry count. |
158 supplier_->LoadRules(region_code, *rules_loaded_); | 174 supplier_->LoadRules(region_code, *rules_loaded_); |
159 } | 175 } |
160 | 176 |
161 } // namespace autofill | 177 } // namespace autofill |
OLD | NEW |