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

Side by Side Diff: components/autofill/core/browser/region_combobox_model.cc

Issue 2803443003: [Payments] Added region load failure tolerance and tests to PR editor. (Closed)
Patch Set: Rename function post-rebase Created 3 years, 8 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 unified diff | Download patch
OLDNEW
1 // Copyright 2017 The Chromium Authors. All rights reserved. 1 // Copyright 2017 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 "components/autofill/core/browser/region_combobox_model.h" 5 #include "components/autofill/core/browser/region_combobox_model.h"
6 6
7 #include <utility> 7 #include <utility>
8 8
9 #include "base/callback.h" 9 #include "base/callback.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
11 #include "base/strings/utf_string_conversions.h" 11 #include "base/strings/utf_string_conversions.h"
12 #include "components/strings/grit/components_strings.h" 12 #include "components/strings/grit/components_strings.h"
13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/region_dat a.h" 13 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/region_dat a.h"
14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/region_dat a_builder.h" 14 #include "third_party/libaddressinput/src/cpp/include/libaddressinput/region_dat a_builder.h"
15 #include "ui/base/l10n/l10n_util.h" 15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/models/combobox_model_observer.h" 16 #include "ui/base/models/combobox_model_observer.h"
17 17
18 namespace autofill { 18 namespace autofill {
19 19
20 RegionComboboxModel::RegionComboboxModel( 20 RegionComboboxModel::RegionComboboxModel(
21 std::unique_ptr<const ::i18n::addressinput::Source> source, 21 std::unique_ptr<const ::i18n::addressinput::Source> source,
22 std::unique_ptr<::i18n::addressinput::Storage> storage, 22 std::unique_ptr<::i18n::addressinput::Storage> storage,
23 const std::string& app_locale, 23 const std::string& app_locale,
24 const std::string& country_code) 24 const std::string& country_code)
25 : app_locale_(app_locale), 25 : app_locale_(app_locale),
26 region_data_supplier_(source.release(), storage.release()) { 26 region_data_supplier_(source.release(), storage.release()) {
please use gerrit instead 2017/04/11 21:05:35 Constructor initialization is neat to organize lik
MAD 2017/04/12 02:46:08 I had done it like this to make it clear that it's
27 pending_region_data_load_ = true;
27 region_data_supplier_callback_.reset(::i18n::addressinput::BuildCallback( 28 region_data_supplier_callback_.reset(::i18n::addressinput::BuildCallback(
28 this, &RegionComboboxModel::RegionDataLoaded)); 29 this, &RegionComboboxModel::RegionDataLoaded));
29 region_data_supplier_.LoadRules(country_code, 30 region_data_supplier_.LoadRules(country_code,
30 *region_data_supplier_callback_.get()); 31 *region_data_supplier_callback_.get());
31 } 32 }
32 33
33 RegionComboboxModel::~RegionComboboxModel() {} 34 RegionComboboxModel::~RegionComboboxModel() {}
34 35
35 int RegionComboboxModel::GetItemCount() const { 36 int RegionComboboxModel::GetItemCount() const {
36 // The combobox view needs to always have at least one item. If the regions 37 // The combobox view needs to always have at least one item. If the regions
37 // have not been completely loaded yet, we display a single "loading" item. 38 // have not been completely loaded yet, we display a single "loading" item.
38 // But if we failed to load, we return 0 so that the view can be identified 39 if (regions_.size() == 0)
39 // as empty and potentially replaced by another view during ReLayout.
40 if (regions_.size() == 0 && !failed_to_load_data_)
41 return 1; 40 return 1;
42 return regions_.size(); 41 return regions_.size();
43 } 42 }
44 43
45 base::string16 RegionComboboxModel::GetItemAt(int index) { 44 base::string16 RegionComboboxModel::GetItemAt(int index) {
46 DCHECK_GE(index, 0); 45 DCHECK_GE(index, 0);
47 // This might happen because of the asynchonous nature of the data. 46 // This might happen because of the asynchonous nature of the data.
48 if (static_cast<size_t>(index) >= regions_.size()) 47 if (static_cast<size_t>(index) >= regions_.size())
49 return l10n_util::GetStringUTF16(IDS_AUTOFILL_LOADING_REGIONS); 48 return l10n_util::GetStringUTF16(IDS_AUTOFILL_LOADING_REGIONS);
50 49
(...skipping 14 matching lines...) Expand all
65 } 64 }
66 65
67 void RegionComboboxModel::AddObserver(ui::ComboboxModelObserver* observer) { 66 void RegionComboboxModel::AddObserver(ui::ComboboxModelObserver* observer) {
68 observers_.AddObserver(observer); 67 observers_.AddObserver(observer);
69 } 68 }
70 69
71 void RegionComboboxModel::RemoveObserver(ui::ComboboxModelObserver* observer) { 70 void RegionComboboxModel::RemoveObserver(ui::ComboboxModelObserver* observer) {
72 observers_.RemoveObserver(observer); 71 observers_.RemoveObserver(observer);
73 } 72 }
74 73
74 void RegionComboboxModel::SetFailureModeForTests(bool failed_to_load_data) {
75 failed_to_load_data_ = failed_to_load_data;
76 for (auto& observer : observers_) {
77 observer.OnComboboxModelChanged(this);
78 }
79 }
80
75 void RegionComboboxModel::RegionDataLoaded(bool success, 81 void RegionComboboxModel::RegionDataLoaded(bool success,
76 const std::string& country_code, 82 const std::string& country_code,
77 int rule_count) { 83 int rule_count) {
84 pending_region_data_load_ = false;
78 if (success) { 85 if (success) {
79 failed_to_load_data_ = false;
80 std::string best_region_tree_language_tag; 86 std::string best_region_tree_language_tag;
81 ::i18n::addressinput::RegionDataBuilder builder(&region_data_supplier_); 87 ::i18n::addressinput::RegionDataBuilder builder(&region_data_supplier_);
82 const std::vector<const ::i18n::addressinput::RegionData*>& regions = 88 const std::vector<const ::i18n::addressinput::RegionData*>& regions =
83 builder.Build(country_code, app_locale_, &best_region_tree_language_tag) 89 builder.Build(country_code, app_locale_, &best_region_tree_language_tag)
84 .sub_regions(); 90 .sub_regions();
85 for (auto* const region : regions) { 91 // For some reason, some countries expose a state field but have not
please use gerrit instead 2017/04/11 21:05:35 Please remove "For some reason". That's a property
MAD 2017/04/12 02:46:08 Done.
86 regions_.push_back(std::make_pair(region->key(), region->name())); 92 // region names available.
93 if (regions.size() > 0) {
94 failed_to_load_data_ = false;
95 for (auto* const region : regions) {
96 regions_.push_back(std::make_pair(region->key(), region->name()));
97 }
98 } else {
99 failed_to_load_data_ = true;
87 } 100 }
88 } else { 101 } else {
89 // TODO(mad): Maybe use a static list as is done for countries in 102 // TODO(mad): Maybe use a static list as is done for countries in
90 // components\autofill\core\browser\country_data.cc 103 // components\autofill\core\browser\country_data.cc
91 failed_to_load_data_ = true; 104 failed_to_load_data_ = true;
92 } 105 }
93 106
94 for (auto& observer : observers_) { 107 for (auto& observer : observers_) {
95 observer.OnComboboxModelChanged(this); 108 observer.OnComboboxModelChanged(this);
96 } 109 }
97 } 110 }
98 111
99 } // namespace autofill 112 } // namespace autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698