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

Unified Diff: third_party/libaddressinput/chromium/chrome_address_validator.cc

Issue 392083002: Retry downloading rules for libaddressinput. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 6 years, 5 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 side-by-side diff with in-line comments
Download patch
Index: third_party/libaddressinput/chromium/chrome_address_validator.cc
diff --git a/third_party/libaddressinput/chromium/chrome_address_validator.cc b/third_party/libaddressinput/chromium/chrome_address_validator.cc
index 2f2300a27ebdc42b76fd0e93aa0e7ef6c151a3a3..c01a3b3d9d1a507460fcbdaae5f8eba1865c2107 100644
--- a/third_party/libaddressinput/chromium/chrome_address_validator.cc
+++ b/third_party/libaddressinput/chromium/chrome_address_validator.cc
@@ -4,26 +4,21 @@
#include "third_party/libaddressinput/chromium/chrome_address_validator.h"
-#include <cstddef>
-#include <string>
-#include <vector>
+#include <cmath>
-#include "base/basictypes.h"
+#include "base/bind.h"
+#include "base/location.h"
#include "base/logging.h"
-#include "base/macros.h"
-#include "base/memory/scoped_ptr.h"
+#include "base/message_loop/message_loop.h"
#include "third_party/libaddressinput/chromium/addressinput_util.h"
#include "third_party/libaddressinput/chromium/input_suggester.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_data.h"
-#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_field.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_normalizer.h"
-#include "third_party/libaddressinput/src/cpp/include/libaddressinput/address_validator.h"
-#include "third_party/libaddressinput/src/cpp/include/libaddressinput/callback.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/downloader.h"
-#include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_supplier.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
namespace autofill {
+namespace {
using ::i18n::addressinput::AddressData;
using ::i18n::addressinput::AddressField;
@@ -38,6 +33,15 @@ using ::i18n::addressinput::ADMIN_AREA;
using ::i18n::addressinput::DEPENDENT_LOCALITY;
using ::i18n::addressinput::POSTAL_CODE;
+// 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.
+// rules, if the first attempt failed.
+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.
+
+// The maximum number attempts to load rules.
+static const int kMaxAttemptsNumber = 8;
+
+} // namespace
+
AddressValidator::AddressValidator(const std::string& validation_data_url,
scoped_ptr<Downloader> downloader,
scoped_ptr<Storage> storage,
@@ -50,12 +54,15 @@ AddressValidator::AddressValidator(const std::string& validation_data_url,
validator_(new ::i18n::addressinput::AddressValidator(supplier_.get())),
validated_(BuildCallback(this, &AddressValidator::Validated)),
rules_loaded_(BuildCallback(this, &AddressValidator::RulesLoaded)),
- load_rules_listener_(load_rules_listener) {}
+ load_rules_listener_(load_rules_listener),
+ weak_factory_(this) {
+ retrier_.reset(new Retrier(weak_factory_.GetWeakPtr()));
+}
AddressValidator::~AddressValidator() {}
void AddressValidator::LoadRules(const std::string& region_code) {
- DCHECK(supplier_);
+ retrier_->ResetRetryCount(region_code);
supplier_->LoadRules(region_code, *rules_loaded_);
}
@@ -63,9 +70,6 @@ AddressValidator::Status AddressValidator::ValidateAddress(
const AddressData& address,
const FieldProblemMap* filter,
FieldProblemMap* problems) const {
- DCHECK(supplier_);
- DCHECK(validator_);
-
if (supplier_->IsPending(address.region_code)) {
if (problems)
addressinput::ValidateRequiredFields(address, filter, problems);
@@ -96,9 +100,6 @@ AddressValidator::Status AddressValidator::GetSuggestions(
AddressField focused_field,
size_t suggestion_limit,
std::vector<AddressData>* suggestions) const {
- DCHECK(supplier_);
- DCHECK(input_suggester_);
-
if (supplier_->IsPending(user_input.region_code))
return RULES_NOT_READY;
@@ -121,10 +122,6 @@ AddressValidator::Status AddressValidator::GetSuggestions(
bool AddressValidator::CanonicalizeAdministrativeArea(
AddressData* address) const {
- DCHECK(address);
- DCHECK(supplier_);
- DCHECK(normalizer_);
-
if (!supplier_->IsLoaded(address->region_code))
return false;
@@ -136,7 +133,31 @@ bool AddressValidator::CanonicalizeAdministrativeArea(
return true;
}
-AddressValidator::AddressValidator() : load_rules_listener_(NULL) {}
+AddressValidator::Retrier::Retrier(
+ const base::WeakPtr<AddressValidator>& rule_loader)
+ : rule_loader_(rule_loader),
+ retry_period_(
+ base::TimeDelta::FromSeconds(kLoadRulesRetryPeriodSeconds)) {}
+
+AddressValidator::Retrier::~Retrier() {}
+
+void AddressValidator::Retrier::ResetRetryCount(
+ const std::string& region_code) {
+ attempts_number_[region_code] = 0;
+}
+
+void AddressValidator::Retrier::RetryLoadRules(const std::string& region_code) {
+ // Count the first failed attempt and this attempt as well.
+ if (attempts_number_[region_code] + 2 > kMaxAttemptsNumber)
+ return;
Evan Stade 2014/07/16 02:11:25 nit: \n
please use gerrit instead 2014/07/16 23:22:03 Done.
+ base::MessageLoop::current()->PostDelayedTask(
+ FROM_HERE,
+ base::Bind(&AddressValidator::RetryLoadRules, rule_loader_, region_code),
+ retry_period_ * pow(2, attempts_number_[region_code]++));
+}
+
+AddressValidator::AddressValidator()
+ : load_rules_listener_(NULL), weak_factory_(this) {}
void AddressValidator::Validated(bool success,
const AddressData&,
@@ -145,10 +166,20 @@ void AddressValidator::Validated(bool success,
}
void AddressValidator::RulesLoaded(bool success,
- const std::string& country_code,
+ const std::string& region_code,
int) {
if (load_rules_listener_)
- load_rules_listener_->OnAddressValidationRulesLoaded(country_code, success);
+ load_rules_listener_->OnAddressValidationRulesLoaded(region_code, success);
+
+ if (success)
+ 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
+ else
+ retrier_->RetryLoadRules(region_code);
+}
+
+void AddressValidator::RetryLoadRules(const std::string& region_code) {
+ // Do not reset retry count.
+ supplier_->LoadRules(region_code, *rules_loaded_);
}
} // namespace autofill

Powered by Google App Engine
This is Rietveld 408576698