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" | |
21 | 20 |
22 namespace autofill { | 21 namespace autofill { |
23 namespace { | 22 namespace { |
24 | 23 |
25 using ::i18n::addressinput::AddressData; | 24 using ::i18n::addressinput::AddressData; |
26 using ::i18n::addressinput::AddressField; | 25 using ::i18n::addressinput::AddressField; |
27 using ::i18n::addressinput::AddressNormalizer; | 26 using ::i18n::addressinput::AddressNormalizer; |
28 using ::i18n::addressinput::BuildCallback; | 27 using ::i18n::addressinput::BuildCallback; |
29 using ::i18n::addressinput::FieldProblemMap; | 28 using ::i18n::addressinput::FieldProblemMap; |
30 using ::i18n::addressinput::PreloadSupplier; | 29 using ::i18n::addressinput::PreloadSupplier; |
31 using ::i18n::addressinput::Rule; | |
32 using ::i18n::addressinput::Source; | 30 using ::i18n::addressinput::Source; |
33 using ::i18n::addressinput::Storage; | 31 using ::i18n::addressinput::Storage; |
34 | 32 |
35 using ::i18n::addressinput::ADMIN_AREA; | 33 using ::i18n::addressinput::ADMIN_AREA; |
36 using ::i18n::addressinput::DEPENDENT_LOCALITY; | 34 using ::i18n::addressinput::DEPENDENT_LOCALITY; |
37 using ::i18n::addressinput::POSTAL_CODE; | 35 using ::i18n::addressinput::POSTAL_CODE; |
38 | 36 |
39 // The maximum number attempts to load rules. | 37 // The maximum number attempts to load rules. |
40 static const int kMaxAttemptsNumber = 8; | 38 static const int kMaxAttemptsNumber = 8; |
41 | 39 |
(...skipping 11 matching lines...) Expand all Loading... |
53 load_rules_listener_(load_rules_listener), | 51 load_rules_listener_(load_rules_listener), |
54 weak_factory_(this) {} | 52 weak_factory_(this) {} |
55 | 53 |
56 AddressValidator::~AddressValidator() {} | 54 AddressValidator::~AddressValidator() {} |
57 | 55 |
58 void AddressValidator::LoadRules(const std::string& region_code) { | 56 void AddressValidator::LoadRules(const std::string& region_code) { |
59 attempts_number_[region_code] = 0; | 57 attempts_number_[region_code] = 0; |
60 supplier_->LoadRules(region_code, *rules_loaded_); | 58 supplier_->LoadRules(region_code, *rules_loaded_); |
61 } | 59 } |
62 | 60 |
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 | |
77 AddressValidator::Status AddressValidator::ValidateAddress( | 61 AddressValidator::Status AddressValidator::ValidateAddress( |
78 const AddressData& address, | 62 const AddressData& address, |
79 const FieldProblemMap* filter, | 63 const FieldProblemMap* filter, |
80 FieldProblemMap* problems) const { | 64 FieldProblemMap* problems) const { |
81 if (supplier_->IsPending(address.region_code)) { | 65 if (supplier_->IsPending(address.region_code)) { |
82 if (problems) | 66 if (problems) |
83 addressinput::ValidateRequiredFields(address, filter, problems); | 67 addressinput::ValidateRequiredFields(address, filter, problems); |
84 return RULES_NOT_READY; | 68 return RULES_NOT_READY; |
85 } | 69 } |
86 | 70 |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 void AddressValidator::Validated(bool success, | 134 void AddressValidator::Validated(bool success, |
151 const AddressData&, | 135 const AddressData&, |
152 const FieldProblemMap&) { | 136 const FieldProblemMap&) { |
153 DCHECK(success); | 137 DCHECK(success); |
154 } | 138 } |
155 | 139 |
156 void AddressValidator::RulesLoaded(bool success, | 140 void AddressValidator::RulesLoaded(bool success, |
157 const std::string& region_code, | 141 const std::string& region_code, |
158 int) { | 142 int) { |
159 if (load_rules_listener_) | 143 if (load_rules_listener_) |
160 load_rules_listener_->OnAddressRulesLoaded(region_code, success); | 144 load_rules_listener_->OnAddressValidationRulesLoaded(region_code, success); |
161 | 145 |
162 // Count the first failed attempt to load rules as well. | 146 // Count the first failed attempt to load rules as well. |
163 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber) | 147 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber) |
164 return; | 148 return; |
165 | 149 |
166 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( | 150 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
167 FROM_HERE, base::Bind(&AddressValidator::RetryLoadRules, | 151 FROM_HERE, base::Bind(&AddressValidator::RetryLoadRules, |
168 weak_factory_.GetWeakPtr(), region_code), | 152 weak_factory_.GetWeakPtr(), region_code), |
169 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); | 153 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); |
170 } | 154 } |
171 | 155 |
172 void AddressValidator::RetryLoadRules(const std::string& region_code) { | 156 void AddressValidator::RetryLoadRules(const std::string& region_code) { |
173 // Do not reset retry count. | 157 // Do not reset retry count. |
174 supplier_->LoadRules(region_code, *rules_loaded_); | 158 supplier_->LoadRules(region_code, *rules_loaded_); |
175 } | 159 } |
176 | 160 |
177 } // namespace autofill | 161 } // namespace autofill |
OLD | NEW |