Commit a0baadbb authored by Matthias Körber's avatar Matthias Körber Committed by Commit Bot

[Autofill] Remove self-destruct on timeout for RegionDataLoaderImpl

The self-destruction timer is not necessary because the RegionDataLoader
is guaranteed to be destructed once the query to libaddressinput
terminates.

Change-Id: Iac364183aa9d456b91ddc4d5f6f29b4c0ffe816d
Bug: 1148749
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2544945Reviewed-by: default avatarDominic Battré <battre@chromium.org>
Reviewed-by: default avatarRouslan Solomakhin <rouslan@chromium.org>
Reviewed-by: default avatarChristoph Schwering <schwering@google.com>
Commit-Queue: Matthias Körber <koerber@google.com>
Cr-Commit-Position: refs/heads/master@{#829628}
parent 0753e413
...@@ -151,8 +151,7 @@ ShippingAddressEditorViewController::GetComboboxModelForType( ...@@ -151,8 +151,7 @@ ShippingAddressEditorViewController::GetComboboxModelForType(
region_model_ = model.get(); region_model_ = model.get();
if (chosen_country_index_ < countries_.size()) { if (chosen_country_index_ < countries_.size()) {
model->LoadRegionData(countries_[chosen_country_index_].first, model->LoadRegionData(countries_[chosen_country_index_].first,
state()->GetRegionDataLoader(), state()->GetRegionDataLoader());
/*timeout_ms=*/5000);
if (!model->IsPendingRegionDataLoad()) { if (!model->IsPendingRegionDataLoad()) {
// If the data was already pre-loaded, the observer won't get notified // If the data was already pre-loaded, the observer won't get notified
// so we have to check for failure here. // so we have to check for failure here.
......
...@@ -28,12 +28,11 @@ class RegionDataLoader { ...@@ -28,12 +28,11 @@ class RegionDataLoader {
const std::vector<const ::i18n::addressinput::RegionData*>& regions)> const std::vector<const ::i18n::addressinput::RegionData*>& regions)>
RegionDataLoaded; RegionDataLoaded;
virtual ~RegionDataLoader() {} virtual ~RegionDataLoader() = default;
// Calls |loaded_callback| when the region data for |country_code| is ready or // Calls |loaded_callback| when the region data for |country_code| is ready.
// when |timeout_ms| miliseconds have passed. This may happen synchronously. // This may happen synchronously.
virtual void LoadRegionData(const std::string& country_code, virtual void LoadRegionData(const std::string& country_code,
RegionDataLoaded callback, RegionDataLoaded callback) = 0;
int64_t timeout_ms) = 0;
// To forget about the |callback| givent to LoadRegionData, in cases where // To forget about the |callback| givent to LoadRegionData, in cases where
// callback owner is destroyed before loader. // callback owner is destroyed before loader.
virtual void ClearCallback() = 0; virtual void ClearCallback() = 0;
......
...@@ -24,19 +24,20 @@ RegionDataLoaderImpl::RegionDataLoaderImpl( ...@@ -24,19 +24,20 @@ RegionDataLoaderImpl::RegionDataLoaderImpl(
this, &RegionDataLoaderImpl::OnRegionDataLoaded)); this, &RegionDataLoaderImpl::OnRegionDataLoaded));
} }
RegionDataLoaderImpl::~RegionDataLoaderImpl() {} RegionDataLoaderImpl::~RegionDataLoaderImpl() = default;
void RegionDataLoaderImpl::LoadRegionData( void RegionDataLoaderImpl::LoadRegionData(
const std::string& country_code, const std::string& country_code,
RegionDataLoaderImpl::RegionDataLoaded callback, RegionDataLoaderImpl::RegionDataLoaded callback) {
int64_t timeout_ms) {
callback_ = callback; callback_ = callback;
// This is the first and only time |LoadRules()| is called on the
// |region_data_supplier_|. This guarantees that the supplied callback
// |region_data_supplier_callback_| will be invoked resulting in the
// destruction of this instance.
// |LoadRules()| may use a network request that has an internal timeout of
// 5 seconds.
region_data_supplier_.LoadRules(country_code, region_data_supplier_.LoadRules(country_code,
*region_data_supplier_callback_); *region_data_supplier_callback_);
timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(timeout_ms),
base::BindOnce(&RegionDataLoaderImpl::OnRegionDataLoaded,
base::Unretained(this), false, country_code, 0));
} }
void RegionDataLoaderImpl::ClearCallback() { void RegionDataLoaderImpl::ClearCallback() {
...@@ -46,7 +47,6 @@ void RegionDataLoaderImpl::ClearCallback() { ...@@ -46,7 +47,6 @@ void RegionDataLoaderImpl::ClearCallback() {
void RegionDataLoaderImpl::OnRegionDataLoaded(bool success, void RegionDataLoaderImpl::OnRegionDataLoaded(bool success,
const std::string& country_code, const std::string& country_code,
int unused_rule_count) { int unused_rule_count) {
timer_.Stop();
if (!callback_.is_null()) { if (!callback_.is_null()) {
if (success) { if (success) {
std::string best_region_tree_language_tag; std::string best_region_tree_language_tag;
......
...@@ -39,8 +39,7 @@ class RegionDataLoaderImpl : public RegionDataLoader { ...@@ -39,8 +39,7 @@ class RegionDataLoaderImpl : public RegionDataLoader {
// autofill::RegionDataLoader. // autofill::RegionDataLoader.
void LoadRegionData(const std::string& country_code, void LoadRegionData(const std::string& country_code,
RegionDataLoader::RegionDataLoaded callback, RegionDataLoader::RegionDataLoaded callback) override;
int64_t timeout_ms) override;
void ClearCallback() override; void ClearCallback() override;
private: private:
...@@ -58,7 +57,6 @@ class RegionDataLoaderImpl : public RegionDataLoader { ...@@ -58,7 +57,6 @@ class RegionDataLoaderImpl : public RegionDataLoader {
std::string app_locale_; std::string app_locale_;
RegionDataLoader::RegionDataLoaded callback_; RegionDataLoader::RegionDataLoaded callback_;
base::OneShotTimer timer_;
}; };
} // namespace autofill } // namespace autofill
......
...@@ -8,14 +8,13 @@ ...@@ -8,14 +8,13 @@
namespace autofill { namespace autofill {
TestRegionDataLoader::TestRegionDataLoader() {} TestRegionDataLoader::TestRegionDataLoader() = default;
TestRegionDataLoader::~TestRegionDataLoader() {} TestRegionDataLoader::~TestRegionDataLoader() = default;
void TestRegionDataLoader::LoadRegionData( void TestRegionDataLoader::LoadRegionData(
const std::string& country_code, const std::string& country_code,
autofill::RegionDataLoader::RegionDataLoaded callback, autofill::RegionDataLoader::RegionDataLoaded callback) {
int64_t unused_timeout_ms) {
if (synchronous_callback_) { if (synchronous_callback_) {
SendRegionData(regions_, callback); SendRegionData(regions_, callback);
} else { } else {
...@@ -45,9 +44,8 @@ void TestRegionDataLoader::SendRegionData( ...@@ -45,9 +44,8 @@ void TestRegionDataLoader::SendRegionData(
const std::vector<std::pair<std::string, std::string>>& regions, const std::vector<std::pair<std::string, std::string>>& regions,
autofill::RegionDataLoader::RegionDataLoaded callback) { autofill::RegionDataLoader::RegionDataLoaded callback) {
::i18n::addressinput::RegionData root_region(""); ::i18n::addressinput::RegionData root_region("");
for (const auto& region : regions) { for (const auto& region : regions)
root_region.AddSubRegion(region.first, region.second); root_region.AddSubRegion(region.first, region.second);
}
callback.Run(root_region.sub_regions()); callback.Run(root_region.sub_regions());
} }
......
...@@ -20,9 +20,9 @@ class TestRegionDataLoader : public RegionDataLoader { ...@@ -20,9 +20,9 @@ class TestRegionDataLoader : public RegionDataLoader {
~TestRegionDataLoader() override; ~TestRegionDataLoader() override;
// RegionDataLoader. // RegionDataLoader.
void LoadRegionData(const std::string& country_code, void LoadRegionData(
autofill::RegionDataLoader::RegionDataLoaded callback, const std::string& country_code,
int64_t timeout_ms) override; autofill::RegionDataLoader::RegionDataLoaded callback) override;
void ClearCallback() override; void ClearCallback() override;
std::string country_code() { return country_code_; } std::string country_code() { return country_code_; }
......
...@@ -26,16 +26,14 @@ RegionComboboxModel::~RegionComboboxModel() { ...@@ -26,16 +26,14 @@ RegionComboboxModel::~RegionComboboxModel() {
} }
void RegionComboboxModel::LoadRegionData(const std::string& country_code, void RegionComboboxModel::LoadRegionData(const std::string& country_code,
RegionDataLoader* region_data_loader, RegionDataLoader* region_data_loader) {
int64_t timeout_ms) {
DCHECK(region_data_loader); DCHECK(region_data_loader);
DCHECK(!region_data_loader_); DCHECK(!region_data_loader_);
region_data_loader_ = region_data_loader; region_data_loader_ = region_data_loader;
region_data_loader_->LoadRegionData( region_data_loader_->LoadRegionData(
country_code, country_code,
base::BindRepeating(&RegionComboboxModel::OnRegionDataLoaded, base::BindRepeating(&RegionComboboxModel::OnRegionDataLoaded,
weak_factory_.GetWeakPtr()), weak_factory_.GetWeakPtr()));
timeout_ms);
} }
int RegionComboboxModel::GetItemCount() const { int RegionComboboxModel::GetItemCount() const {
......
...@@ -34,8 +34,7 @@ class RegionComboboxModel : public ui::ComboboxModel { ...@@ -34,8 +34,7 @@ class RegionComboboxModel : public ui::ComboboxModel {
~RegionComboboxModel() override; ~RegionComboboxModel() override;
void LoadRegionData(const std::string& country_code, void LoadRegionData(const std::string& country_code,
RegionDataLoader* region_data_loader, RegionDataLoader* region_data_loader);
int64_t timeout_ms);
bool IsPendingRegionDataLoad() const { bool IsPendingRegionDataLoad() const {
return region_data_loader_ != nullptr; return region_data_loader_ != nullptr;
......
...@@ -28,7 +28,7 @@ const char kOntarioName[] = "Ontario"; ...@@ -28,7 +28,7 @@ const char kOntarioName[] = "Ontario";
TEST(RegionComboboxModelTest, QuebecOntarioRegions) { TEST(RegionComboboxModelTest, QuebecOntarioRegions) {
TestRegionDataLoader test_region_data_loader; TestRegionDataLoader test_region_data_loader;
RegionComboboxModel model; RegionComboboxModel model;
model.LoadRegionData("", &test_region_data_loader, 0); model.LoadRegionData("", &test_region_data_loader);
std::vector<std::pair<std::string, std::string>> regions; std::vector<std::pair<std::string, std::string>> regions;
regions.emplace_back(kQuebecCode, kQuebecName); regions.emplace_back(kQuebecCode, kQuebecName);
...@@ -47,7 +47,7 @@ TEST(RegionComboboxModelTest, QuebecOntarioRegions) { ...@@ -47,7 +47,7 @@ TEST(RegionComboboxModelTest, QuebecOntarioRegions) {
TEST(RegionComboboxModelTest, FailingSource) { TEST(RegionComboboxModelTest, FailingSource) {
TestRegionDataLoader test_region_data_loader; TestRegionDataLoader test_region_data_loader;
RegionComboboxModel model; RegionComboboxModel model;
model.LoadRegionData("", &test_region_data_loader, 0); model.LoadRegionData("", &test_region_data_loader);
test_region_data_loader.SendAsynchronousData( test_region_data_loader.SendAsynchronousData(
std::vector<std::pair<std::string, std::string>>()); std::vector<std::pair<std::string, std::string>>());
......
...@@ -94,6 +94,9 @@ void ChromeMetadataSource::Download(const std::string& key, ...@@ -94,6 +94,9 @@ void ChromeMetadataSource::Download(const std::string& key,
std::unique_ptr<network::SimpleURLLoader> loader = std::unique_ptr<network::SimpleURLLoader> loader =
network::SimpleURLLoader::Create(std::move(resource_request), network::SimpleURLLoader::Create(std::move(resource_request),
traffic_annotation); traffic_annotation);
// Limit the request duration to 5 seconds.
loader->SetTimeoutDuration(base::TimeDelta::FromSeconds(5));
auto it = requests_.insert( auto it = requests_.insert(
requests_.begin(), requests_.begin(),
std::make_unique<Request>(key, std::move(loader), downloaded)); std::make_unique<Request>(key, std::move(loader), downloaded));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment