Chromium Code Reviews| 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 number of seconds to wait between the first and second attempts to load | |
|
Evan Stade
2014/07/16 02:11:25
this isn't quite right. It's the amount of time be
please use gerrit instead
2014/07/16 23:22:03
Right. Fixed.
| |
| 37 // rules, if the first attempt failed. | |
| 38 static const int kLoadRulesRetryPeriodSeconds = 60; | |
|
Evan Stade
2014/07/16 02:11:25
I think we should set this lower. As noted above,
please use gerrit instead
2014/07/16 23:22:03
Done.
| |
| 39 | |
| 40 // The maximum number attempts to load rules. | |
| 41 static const int kMaxAttemptsNumber = 8; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 41 AddressValidator::AddressValidator(const std::string& validation_data_url, | 45 AddressValidator::AddressValidator(const std::string& validation_data_url, |
| 42 scoped_ptr<Downloader> downloader, | 46 scoped_ptr<Downloader> downloader, |
| 43 scoped_ptr<Storage> storage, | 47 scoped_ptr<Storage> storage, |
| 44 LoadRulesListener* load_rules_listener) | 48 LoadRulesListener* load_rules_listener) |
| 45 : supplier_(new PreloadSupplier(validation_data_url, | 49 : supplier_(new PreloadSupplier(validation_data_url, |
| 46 downloader.release(), | 50 downloader.release(), |
| 47 storage.release())), | 51 storage.release())), |
| 48 input_suggester_(new InputSuggester(supplier_.get())), | 52 input_suggester_(new InputSuggester(supplier_.get())), |
| 49 normalizer_(new AddressNormalizer(supplier_.get())), | 53 normalizer_(new AddressNormalizer(supplier_.get())), |
| 50 validator_(new ::i18n::addressinput::AddressValidator(supplier_.get())), | 54 validator_(new ::i18n::addressinput::AddressValidator(supplier_.get())), |
| 51 validated_(BuildCallback(this, &AddressValidator::Validated)), | 55 validated_(BuildCallback(this, &AddressValidator::Validated)), |
| 52 rules_loaded_(BuildCallback(this, &AddressValidator::RulesLoaded)), | 56 rules_loaded_(BuildCallback(this, &AddressValidator::RulesLoaded)), |
| 53 load_rules_listener_(load_rules_listener) {} | 57 load_rules_listener_(load_rules_listener), |
| 58 weak_factory_(this) { | |
| 59 retrier_.reset(new Retrier(weak_factory_.GetWeakPtr())); | |
| 60 } | |
| 54 | 61 |
| 55 AddressValidator::~AddressValidator() {} | 62 AddressValidator::~AddressValidator() {} |
| 56 | 63 |
| 57 void AddressValidator::LoadRules(const std::string& region_code) { | 64 void AddressValidator::LoadRules(const std::string& region_code) { |
| 58 DCHECK(supplier_); | 65 retrier_->ResetRetryCount(region_code); |
| 59 supplier_->LoadRules(region_code, *rules_loaded_); | 66 supplier_->LoadRules(region_code, *rules_loaded_); |
| 60 } | 67 } |
| 61 | 68 |
| 62 AddressValidator::Status AddressValidator::ValidateAddress( | 69 AddressValidator::Status AddressValidator::ValidateAddress( |
| 63 const AddressData& address, | 70 const AddressData& address, |
| 64 const FieldProblemMap* filter, | 71 const FieldProblemMap* filter, |
| 65 FieldProblemMap* problems) const { | 72 FieldProblemMap* problems) const { |
| 66 DCHECK(supplier_); | |
| 67 DCHECK(validator_); | |
| 68 | |
| 69 if (supplier_->IsPending(address.region_code)) { | 73 if (supplier_->IsPending(address.region_code)) { |
| 70 if (problems) | 74 if (problems) |
| 71 addressinput::ValidateRequiredFields(address, filter, problems); | 75 addressinput::ValidateRequiredFields(address, filter, problems); |
| 72 return RULES_NOT_READY; | 76 return RULES_NOT_READY; |
| 73 } | 77 } |
| 74 | 78 |
| 75 if (!supplier_->IsLoaded(address.region_code)) { | 79 if (!supplier_->IsLoaded(address.region_code)) { |
| 76 if (problems) | 80 if (problems) |
| 77 addressinput::ValidateRequiredFields(address, filter, problems); | 81 addressinput::ValidateRequiredFields(address, filter, problems); |
| 78 return RULES_UNAVAILABLE; | 82 return RULES_UNAVAILABLE; |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 89 *validated_); | 93 *validated_); |
| 90 | 94 |
| 91 return SUCCESS; | 95 return SUCCESS; |
| 92 } | 96 } |
| 93 | 97 |
| 94 AddressValidator::Status AddressValidator::GetSuggestions( | 98 AddressValidator::Status AddressValidator::GetSuggestions( |
| 95 const AddressData& user_input, | 99 const AddressData& user_input, |
| 96 AddressField focused_field, | 100 AddressField focused_field, |
| 97 size_t suggestion_limit, | 101 size_t suggestion_limit, |
| 98 std::vector<AddressData>* suggestions) const { | 102 std::vector<AddressData>* suggestions) const { |
| 99 DCHECK(supplier_); | |
| 100 DCHECK(input_suggester_); | |
| 101 | |
| 102 if (supplier_->IsPending(user_input.region_code)) | 103 if (supplier_->IsPending(user_input.region_code)) |
| 103 return RULES_NOT_READY; | 104 return RULES_NOT_READY; |
| 104 | 105 |
| 105 if (!supplier_->IsLoaded(user_input.region_code)) | 106 if (!supplier_->IsLoaded(user_input.region_code)) |
| 106 return RULES_UNAVAILABLE; | 107 return RULES_UNAVAILABLE; |
| 107 | 108 |
| 108 if (!suggestions) | 109 if (!suggestions) |
| 109 return SUCCESS; | 110 return SUCCESS; |
| 110 | 111 |
| 111 suggestions->clear(); | 112 suggestions->clear(); |
| 112 | 113 |
| 113 if (focused_field == POSTAL_CODE || | 114 if (focused_field == POSTAL_CODE || |
| 114 (focused_field >= ADMIN_AREA && focused_field <= DEPENDENT_LOCALITY)) { | 115 (focused_field >= ADMIN_AREA && focused_field <= DEPENDENT_LOCALITY)) { |
| 115 input_suggester_->GetSuggestions( | 116 input_suggester_->GetSuggestions( |
| 116 user_input, focused_field, suggestion_limit, suggestions); | 117 user_input, focused_field, suggestion_limit, suggestions); |
| 117 } | 118 } |
| 118 | 119 |
| 119 return SUCCESS; | 120 return SUCCESS; |
| 120 } | 121 } |
| 121 | 122 |
| 122 bool AddressValidator::CanonicalizeAdministrativeArea( | 123 bool AddressValidator::CanonicalizeAdministrativeArea( |
| 123 AddressData* address) const { | 124 AddressData* address) const { |
| 124 DCHECK(address); | |
| 125 DCHECK(supplier_); | |
| 126 DCHECK(normalizer_); | |
| 127 | |
| 128 if (!supplier_->IsLoaded(address->region_code)) | 125 if (!supplier_->IsLoaded(address->region_code)) |
| 129 return false; | 126 return false; |
| 130 | 127 |
| 131 // TODO: It would probably be beneficial to use the full canonicalization. | 128 // TODO: It would probably be beneficial to use the full canonicalization. |
| 132 AddressData tmp(*address); | 129 AddressData tmp(*address); |
| 133 normalizer_->Normalize(&tmp); | 130 normalizer_->Normalize(&tmp); |
| 134 address->administrative_area = tmp.administrative_area; | 131 address->administrative_area = tmp.administrative_area; |
| 135 | 132 |
| 136 return true; | 133 return true; |
| 137 } | 134 } |
| 138 | 135 |
| 139 AddressValidator::AddressValidator() : load_rules_listener_(NULL) {} | 136 AddressValidator::Retrier::Retrier( |
| 137 const base::WeakPtr<AddressValidator>& rule_loader) | |
| 138 : rule_loader_(rule_loader), | |
| 139 retry_period_( | |
| 140 base::TimeDelta::FromSeconds(kLoadRulesRetryPeriodSeconds)) {} | |
| 141 | |
| 142 AddressValidator::Retrier::~Retrier() {} | |
| 143 | |
| 144 void AddressValidator::Retrier::ResetRetryCount( | |
| 145 const std::string& region_code) { | |
| 146 attempts_number_[region_code] = 0; | |
| 147 } | |
| 148 | |
| 149 void AddressValidator::Retrier::RetryLoadRules(const std::string& region_code) { | |
| 150 // Count the first failed attempt and this attempt as well. | |
| 151 if (attempts_number_[region_code] + 2 > kMaxAttemptsNumber) | |
| 152 return; | |
|
Evan Stade
2014/07/16 02:11:25
nit: \n
please use gerrit instead
2014/07/16 23:22:03
Done.
| |
| 153 base::MessageLoop::current()->PostDelayedTask( | |
| 154 FROM_HERE, | |
| 155 base::Bind(&AddressValidator::RetryLoadRules, rule_loader_, region_code), | |
| 156 retry_period_ * pow(2, attempts_number_[region_code]++)); | |
| 157 } | |
| 158 | |
| 159 AddressValidator::AddressValidator() | |
| 160 : load_rules_listener_(NULL), weak_factory_(this) {} | |
| 140 | 161 |
| 141 void AddressValidator::Validated(bool success, | 162 void AddressValidator::Validated(bool success, |
| 142 const AddressData&, | 163 const AddressData&, |
| 143 const FieldProblemMap&) { | 164 const FieldProblemMap&) { |
| 144 DCHECK(success); | 165 DCHECK(success); |
| 145 } | 166 } |
| 146 | 167 |
| 147 void AddressValidator::RulesLoaded(bool success, | 168 void AddressValidator::RulesLoaded(bool success, |
| 148 const std::string& country_code, | 169 const std::string& region_code, |
| 149 int) { | 170 int) { |
| 150 if (load_rules_listener_) | 171 if (load_rules_listener_) |
| 151 load_rules_listener_->OnAddressValidationRulesLoaded(country_code, success); | 172 load_rules_listener_->OnAddressValidationRulesLoaded(region_code, success); |
| 173 | |
| 174 if (success) | |
| 175 retrier_->ResetRetryCount(region_code); | |
|
Evan Stade
2014/07/16 02:11:25
why do you need to reset it here?
please use gerrit instead
2014/07/16 23:22:03
True, come to think of it, I don't need it. Remove
| |
| 176 else | |
| 177 retrier_->RetryLoadRules(region_code); | |
| 178 } | |
| 179 | |
| 180 void AddressValidator::RetryLoadRules(const std::string& region_code) { | |
| 181 // Do not reset retry count. | |
| 182 supplier_->LoadRules(region_code, *rules_loaded_); | |
| 152 } | 183 } |
| 153 | 184 |
| 154 } // namespace autofill | 185 } // namespace autofill |
| OLD | NEW |