| 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 <cstddef> | 7 #include <cmath> |
| 8 #include <string> | |
| 9 #include <vector> | |
| 10 | 8 |
| 11 #include "base/basictypes.h" | 9 #include "base/bind.h" |
| 10 #include "base/location.h" |
| 12 #include "base/logging.h" | 11 #include "base/logging.h" |
| 13 #include "base/macros.h" | 12 #include "base/message_loop/message_loop.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "third_party/libaddressinput/chromium/addressinput_util.h" | 13 #include "third_party/libaddressinput/chromium/addressinput_util.h" |
| 16 #include "third_party/libaddressinput/chromium/input_suggester.h" | 14 #include "third_party/libaddressinput/chromium/input_suggester.h" |
| 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" | 15 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_da
ta.h" |
| 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_fi
eld.h" | |
| 19 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_no
rmalizer.h" | 16 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_no
rmalizer.h" |
| 20 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_va
lidator.h" | |
| 21 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h
" | |
| 22 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader
.h" | 17 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader
.h" |
| 23 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_su
pplier.h" | |
| 24 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" | 18 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h" |
| 25 | 19 |
| 26 namespace autofill { | 20 namespace autofill { |
| 21 namespace { |
| 27 | 22 |
| 28 using ::i18n::addressinput::AddressData; | 23 using ::i18n::addressinput::AddressData; |
| 29 using ::i18n::addressinput::AddressField; | 24 using ::i18n::addressinput::AddressField; |
| 30 using ::i18n::addressinput::AddressNormalizer; | 25 using ::i18n::addressinput::AddressNormalizer; |
| 31 using ::i18n::addressinput::BuildCallback; | 26 using ::i18n::addressinput::BuildCallback; |
| 32 using ::i18n::addressinput::Downloader; | 27 using ::i18n::addressinput::Downloader; |
| 33 using ::i18n::addressinput::FieldProblemMap; | 28 using ::i18n::addressinput::FieldProblemMap; |
| 34 using ::i18n::addressinput::PreloadSupplier; | 29 using ::i18n::addressinput::PreloadSupplier; |
| 35 using ::i18n::addressinput::Storage; | 30 using ::i18n::addressinput::Storage; |
| 36 | 31 |
| 37 using ::i18n::addressinput::ADMIN_AREA; | 32 using ::i18n::addressinput::ADMIN_AREA; |
| 38 using ::i18n::addressinput::DEPENDENT_LOCALITY; | 33 using ::i18n::addressinput::DEPENDENT_LOCALITY; |
| 39 using ::i18n::addressinput::POSTAL_CODE; | 34 using ::i18n::addressinput::POSTAL_CODE; |
| 40 | 35 |
| 36 // The maximum number attempts to load rules. |
| 37 static const int kMaxAttemptsNumber = 8; |
| 38 |
| 39 } // namespace |
| 40 |
| 41 AddressValidator::AddressValidator(const std::string& validation_data_url, | 41 AddressValidator::AddressValidator(const std::string& validation_data_url, |
| 42 scoped_ptr<Downloader> downloader, | 42 scoped_ptr<Downloader> downloader, |
| 43 scoped_ptr<Storage> storage, | 43 scoped_ptr<Storage> storage, |
| 44 LoadRulesListener* load_rules_listener) | 44 LoadRulesListener* load_rules_listener) |
| 45 : supplier_(new PreloadSupplier(validation_data_url, | 45 : supplier_(new PreloadSupplier(validation_data_url, |
| 46 downloader.release(), | 46 downloader.release(), |
| 47 storage.release())), | 47 storage.release())), |
| 48 input_suggester_(new InputSuggester(supplier_.get())), | 48 input_suggester_(new InputSuggester(supplier_.get())), |
| 49 normalizer_(new AddressNormalizer(supplier_.get())), | 49 normalizer_(new AddressNormalizer(supplier_.get())), |
| 50 validator_(new ::i18n::addressinput::AddressValidator(supplier_.get())), | 50 validator_(new ::i18n::addressinput::AddressValidator(supplier_.get())), |
| 51 validated_(BuildCallback(this, &AddressValidator::Validated)), | 51 validated_(BuildCallback(this, &AddressValidator::Validated)), |
| 52 rules_loaded_(BuildCallback(this, &AddressValidator::RulesLoaded)), | 52 rules_loaded_(BuildCallback(this, &AddressValidator::RulesLoaded)), |
| 53 load_rules_listener_(load_rules_listener) {} | 53 load_rules_listener_(load_rules_listener), |
| 54 weak_factory_(this) {} |
| 54 | 55 |
| 55 AddressValidator::~AddressValidator() {} | 56 AddressValidator::~AddressValidator() {} |
| 56 | 57 |
| 57 void AddressValidator::LoadRules(const std::string& region_code) { | 58 void AddressValidator::LoadRules(const std::string& region_code) { |
| 58 DCHECK(supplier_); | 59 attempts_number_[region_code] = 0; |
| 59 supplier_->LoadRules(region_code, *rules_loaded_); | 60 supplier_->LoadRules(region_code, *rules_loaded_); |
| 60 } | 61 } |
| 61 | 62 |
| 62 AddressValidator::Status AddressValidator::ValidateAddress( | 63 AddressValidator::Status AddressValidator::ValidateAddress( |
| 63 const AddressData& address, | 64 const AddressData& address, |
| 64 const FieldProblemMap* filter, | 65 const FieldProblemMap* filter, |
| 65 FieldProblemMap* problems) const { | 66 FieldProblemMap* problems) const { |
| 66 DCHECK(supplier_); | |
| 67 DCHECK(validator_); | |
| 68 | |
| 69 if (supplier_->IsPending(address.region_code)) { | 67 if (supplier_->IsPending(address.region_code)) { |
| 70 if (problems) | 68 if (problems) |
| 71 addressinput::ValidateRequiredFields(address, filter, problems); | 69 addressinput::ValidateRequiredFields(address, filter, problems); |
| 72 return RULES_NOT_READY; | 70 return RULES_NOT_READY; |
| 73 } | 71 } |
| 74 | 72 |
| 75 if (!supplier_->IsLoaded(address.region_code)) { | 73 if (!supplier_->IsLoaded(address.region_code)) { |
| 76 if (problems) | 74 if (problems) |
| 77 addressinput::ValidateRequiredFields(address, filter, problems); | 75 addressinput::ValidateRequiredFields(address, filter, problems); |
| 78 return RULES_UNAVAILABLE; | 76 return RULES_UNAVAILABLE; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 89 *validated_); | 87 *validated_); |
| 90 | 88 |
| 91 return SUCCESS; | 89 return SUCCESS; |
| 92 } | 90 } |
| 93 | 91 |
| 94 AddressValidator::Status AddressValidator::GetSuggestions( | 92 AddressValidator::Status AddressValidator::GetSuggestions( |
| 95 const AddressData& user_input, | 93 const AddressData& user_input, |
| 96 AddressField focused_field, | 94 AddressField focused_field, |
| 97 size_t suggestion_limit, | 95 size_t suggestion_limit, |
| 98 std::vector<AddressData>* suggestions) const { | 96 std::vector<AddressData>* suggestions) const { |
| 99 DCHECK(supplier_); | |
| 100 DCHECK(input_suggester_); | |
| 101 | |
| 102 if (supplier_->IsPending(user_input.region_code)) | 97 if (supplier_->IsPending(user_input.region_code)) |
| 103 return RULES_NOT_READY; | 98 return RULES_NOT_READY; |
| 104 | 99 |
| 105 if (!supplier_->IsLoaded(user_input.region_code)) | 100 if (!supplier_->IsLoaded(user_input.region_code)) |
| 106 return RULES_UNAVAILABLE; | 101 return RULES_UNAVAILABLE; |
| 107 | 102 |
| 108 if (!suggestions) | 103 if (!suggestions) |
| 109 return SUCCESS; | 104 return SUCCESS; |
| 110 | 105 |
| 111 suggestions->clear(); | 106 suggestions->clear(); |
| 112 | 107 |
| 113 if (focused_field == POSTAL_CODE || | 108 if (focused_field == POSTAL_CODE || |
| 114 (focused_field >= ADMIN_AREA && focused_field <= DEPENDENT_LOCALITY)) { | 109 (focused_field >= ADMIN_AREA && focused_field <= DEPENDENT_LOCALITY)) { |
| 115 input_suggester_->GetSuggestions( | 110 input_suggester_->GetSuggestions( |
| 116 user_input, focused_field, suggestion_limit, suggestions); | 111 user_input, focused_field, suggestion_limit, suggestions); |
| 117 } | 112 } |
| 118 | 113 |
| 119 return SUCCESS; | 114 return SUCCESS; |
| 120 } | 115 } |
| 121 | 116 |
| 122 bool AddressValidator::CanonicalizeAdministrativeArea( | 117 bool AddressValidator::CanonicalizeAdministrativeArea( |
| 123 AddressData* address) const { | 118 AddressData* address) const { |
| 124 DCHECK(address); | |
| 125 DCHECK(supplier_); | |
| 126 DCHECK(normalizer_); | |
| 127 | |
| 128 if (!supplier_->IsLoaded(address->region_code)) | 119 if (!supplier_->IsLoaded(address->region_code)) |
| 129 return false; | 120 return false; |
| 130 | 121 |
| 131 // TODO: It would probably be beneficial to use the full canonicalization. | 122 // TODO: It would probably be beneficial to use the full canonicalization. |
| 132 AddressData tmp(*address); | 123 AddressData tmp(*address); |
| 133 normalizer_->Normalize(&tmp); | 124 normalizer_->Normalize(&tmp); |
| 134 address->administrative_area = tmp.administrative_area; | 125 address->administrative_area = tmp.administrative_area; |
| 135 | 126 |
| 136 return true; | 127 return true; |
| 137 } | 128 } |
| 138 | 129 |
| 139 AddressValidator::AddressValidator() : load_rules_listener_(NULL) {} | 130 AddressValidator::AddressValidator() |
| 131 : load_rules_listener_(NULL), weak_factory_(this) {} |
| 132 |
| 133 base::TimeDelta AddressValidator::GetBaseRetryPeriod() const { |
| 134 return base::TimeDelta::FromSeconds(8); |
| 135 } |
| 140 | 136 |
| 141 void AddressValidator::Validated(bool success, | 137 void AddressValidator::Validated(bool success, |
| 142 const AddressData&, | 138 const AddressData&, |
| 143 const FieldProblemMap&) { | 139 const FieldProblemMap&) { |
| 144 DCHECK(success); | 140 DCHECK(success); |
| 145 } | 141 } |
| 146 | 142 |
| 147 void AddressValidator::RulesLoaded(bool success, | 143 void AddressValidator::RulesLoaded(bool success, |
| 148 const std::string& country_code, | 144 const std::string& region_code, |
| 149 int) { | 145 int) { |
| 150 if (load_rules_listener_) | 146 if (load_rules_listener_) |
| 151 load_rules_listener_->OnAddressValidationRulesLoaded(country_code, success); | 147 load_rules_listener_->OnAddressValidationRulesLoaded(region_code, success); |
| 148 |
| 149 // Count the first failed attempt to load rules as well. |
| 150 if (success || attempts_number_[region_code] + 1 >= kMaxAttemptsNumber) |
| 151 return; |
| 152 |
| 153 base::MessageLoop::current()->PostDelayedTask( |
| 154 FROM_HERE, |
| 155 base::Bind(&AddressValidator::RetryLoadRules, |
| 156 weak_factory_.GetWeakPtr(), |
| 157 region_code), |
| 158 GetBaseRetryPeriod() * pow(2, attempts_number_[region_code]++)); |
| 159 } |
| 160 |
| 161 void AddressValidator::RetryLoadRules(const std::string& region_code) { |
| 162 // Do not reset retry count. |
| 163 supplier_->LoadRules(region_code, *rules_loaded_); |
| 152 } | 164 } |
| 153 | 165 |
| 154 } // namespace autofill | 166 } // namespace autofill |
| OLD | NEW |