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

Side by Side Diff: third_party/libaddressinput/chromium/chrome_address_validator.cc

Issue 2680143002: Use dropdown list for admin areas in pr form. (Closed)
Patch Set: Another one with addressing Math's comments. Created 3 years, 8 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
OLDNEW
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
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 (rule_iterator->second)->GetSubKeys();
please use gerrit instead 2017/03/30 18:48:59 No need for ().
Parastoo 2017/03/31 15:44:15 Done.
73 return std::vector<std::string>();
74 }
75
61 AddressValidator::Status AddressValidator::ValidateAddress( 76 AddressValidator::Status AddressValidator::ValidateAddress(
62 const AddressData& address, 77 const AddressData& address,
63 const FieldProblemMap* filter, 78 const FieldProblemMap* filter,
64 FieldProblemMap* problems) const { 79 FieldProblemMap* problems) const {
65 if (supplier_->IsPending(address.region_code)) { 80 if (supplier_->IsPending(address.region_code)) {
66 if (problems) 81 if (problems)
67 addressinput::ValidateRequiredFields(address, filter, problems); 82 addressinput::ValidateRequiredFields(address, filter, problems);
68 return RULES_NOT_READY; 83 return RULES_NOT_READY;
69 } 84 }
70 85
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
134 void AddressValidator::Validated(bool success, 149 void AddressValidator::Validated(bool success,
135 const AddressData&, 150 const AddressData&,
136 const FieldProblemMap&) { 151 const FieldProblemMap&) {
137 DCHECK(success); 152 DCHECK(success);
138 } 153 }
139 154
140 void AddressValidator::RulesLoaded(bool success, 155 void AddressValidator::RulesLoaded(bool success,
141 const std::string& region_code, 156 const std::string& region_code,
142 int) { 157 int) {
143 if (load_rules_listener_) 158 if (load_rules_listener_)
144 load_rules_listener_->OnAddressValidationRulesLoaded(region_code, success); 159 load_rules_listener_->OnAddressRulesLoaded(region_code, success);
145 160
146 // Count the first failed attempt to load rules as well. 161 // Count the first failed attempt to load rules as well.
147 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber) 162 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber)
148 return; 163 return;
149 164
150 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( 165 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
151 FROM_HERE, base::Bind(&AddressValidator::RetryLoadRules, 166 FROM_HERE, base::Bind(&AddressValidator::RetryLoadRules,
152 weak_factory_.GetWeakPtr(), region_code), 167 weak_factory_.GetWeakPtr(), region_code),
153 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); 168 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++));
154 } 169 }
155 170
156 void AddressValidator::RetryLoadRules(const std::string& region_code) { 171 void AddressValidator::RetryLoadRules(const std::string& region_code) {
157 // Do not reset retry count. 172 // Do not reset retry count.
158 supplier_->LoadRules(region_code, *rules_loaded_); 173 supplier_->LoadRules(region_code, *rules_loaded_);
159 } 174 }
160 175
161 } // namespace autofill 176 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698