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" |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
53 load_rules_listener_(load_rules_listener), | 53 load_rules_listener_(load_rules_listener), |
54 weak_factory_(this) {} | 54 weak_factory_(this) {} |
55 | 55 |
56 AddressValidator::~AddressValidator() {} | 56 AddressValidator::~AddressValidator() {} |
57 | 57 |
58 void AddressValidator::LoadRules(const std::string& region_code) { | 58 void AddressValidator::LoadRules(const std::string& region_code) { |
59 attempts_number_[region_code] = 0; | 59 attempts_number_[region_code] = 0; |
60 supplier_->LoadRules(region_code, *rules_loaded_); | 60 supplier_->LoadRules(region_code, *rules_loaded_); |
61 } | 61 } |
62 | 62 |
63 std::vector<std::string> AddressValidator::GetRegionSubKeys( | 63 std::vector<std::pair<std::string, std::string>> |
64 const std::string& region_code) { | 64 AddressValidator::GetRegionSubKeys(const std::string& region_code, |
65 const std::string& language) { | |
66 std::vector<std::pair<std::string, std::string>> subkeys_codes_names; | |
65 if (!AreRulesLoadedForRegion(region_code)) | 67 if (!AreRulesLoadedForRegion(region_code)) |
66 return std::vector<std::string>(); | 68 return subkeys_codes_names; |
67 | 69 |
68 auto rules = supplier_->GetRulesForRegion(region_code); | 70 auto rules = supplier_->GetRulesForRegion(region_code); |
69 auto rule_iterator = rules.find("data/" + region_code); | 71 auto rule_iterator = rules.find("data/" + region_code); |
72 // This should never happen. | |
73 if (rule_iterator == rules.end() || !rule_iterator->second) | |
sebsg
2017/07/07 18:05:04
Instead of saying "this should never happen" I wou
Parastoo
2017/07/10 15:40:20
As discussed: this can happen if the rules are bad
| |
74 return subkeys_codes_names; | |
70 | 75 |
71 if (rule_iterator == rules.end() || !rule_iterator->second) | 76 auto subkeys_codes = rule_iterator->second->GetSubKeys(); |
72 return std::vector<std::string>(); | |
73 | 77 |
74 return rule_iterator->second->GetSubKeys(); | 78 // If the device language is available, show the names in that language. |
79 // Otherwise, show the default names. | |
80 std::string lang_suffix = ""; | |
81 if (rules.find("data/" + region_code + "--" + language) != rules.end()) | |
82 lang_suffix = "--" + language; // ex: --fr | |
83 | |
84 for (auto subkey_code : subkeys_codes) { | |
85 auto rule = rules.find("data/" + region_code + '/' + subkey_code + | |
86 lang_suffix); // exp: data/CA/QC--fr | |
87 // This should never happen. | |
sebsg
2017/07/07 18:05:04
ditto
Parastoo
2017/07/10 15:40:20
Done.
| |
88 if (rule == rules.end() || !rule_iterator->second) | |
89 continue; | |
90 auto subkey_name = rule->second->GetName(); | |
91 if (subkey_name.empty()) // For some cases, the name is not available. | |
92 subkey_name = subkey_code; | |
93 subkeys_codes_names.push_back(make_pair(subkey_code, subkey_name)); | |
94 } | |
95 return subkeys_codes_names; | |
75 } | 96 } |
76 | 97 |
77 AddressValidator::Status AddressValidator::ValidateAddress( | 98 AddressValidator::Status AddressValidator::ValidateAddress( |
78 const AddressData& address, | 99 const AddressData& address, |
79 const FieldProblemMap* filter, | 100 const FieldProblemMap* filter, |
80 FieldProblemMap* problems) const { | 101 FieldProblemMap* problems) const { |
81 if (supplier_->IsPending(address.region_code)) { | 102 if (supplier_->IsPending(address.region_code)) { |
82 if (problems) | 103 if (problems) |
83 addressinput::ValidateRequiredFields(address, filter, problems); | 104 addressinput::ValidateRequiredFields(address, filter, problems); |
84 return RULES_NOT_READY; | 105 return RULES_NOT_READY; |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
168 weak_factory_.GetWeakPtr(), region_code), | 189 weak_factory_.GetWeakPtr(), region_code), |
169 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); | 190 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); |
170 } | 191 } |
171 | 192 |
172 void AddressValidator::RetryLoadRules(const std::string& region_code) { | 193 void AddressValidator::RetryLoadRules(const std::string& region_code) { |
173 // Do not reset retry count. | 194 // Do not reset retry count. |
174 supplier_->LoadRules(region_code, *rules_loaded_); | 195 supplier_->LoadRules(region_code, *rules_loaded_); |
175 } | 196 } |
176 | 197 |
177 } // namespace autofill | 198 } // namespace autofill |
OLD | NEW |