summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/core/browser/region_combobox_model.h
blob: aa16bc88e5df1209debaa596bd49d5143267f3ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2017 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_REGION_COMBOBOX_MODEL_H_
#define COMPONENTS_AUTOFILL_CORE_BROWSER_REGION_COMBOBOX_MODEL_H_

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "base/macros.h"
#include "base/observer_list.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/preload_supplier.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/source.h"
#include "third_party/libaddressinput/src/cpp/include/libaddressinput/storage.h"
#include "ui/base/models/combobox_model.h"

namespace autofill {

// A model for country regions (aka state/provinces) to be used to enter
// addresses. Note that loading these regions can happen asynchronously so a
// ui::ComboboxModelObserver should be attached to this model to be updated when
// the regions load is completed.
class RegionComboboxModel : public ui::ComboboxModel {
 public:
  // |source| and |storage| are needed to initialize the
  // ::i18n::addressinput::PreloadSupplier, |app_locale| is needed for
  // ::i18n::addressinput::RegionDataBuilder and |country_code| identifies which
  // country's region to load into the model.
  RegionComboboxModel(
      std::unique_ptr<const ::i18n::addressinput::Source> source,
      std::unique_ptr<::i18n::addressinput::Storage> storage,
      const std::string& app_locale,
      const std::string& country_code);
  ~RegionComboboxModel() override;

  bool pending_region_data_load() const { return pending_region_data_load_; }
  bool failed_to_load_data() const { return failed_to_load_data_; }

  // ui::ComboboxModel implementation:
  int GetItemCount() const override;
  base::string16 GetItemAt(int index) override;
  bool IsItemSeparatorAt(int index) override;
  void AddObserver(ui::ComboboxModelObserver* observer) override;
  void RemoveObserver(ui::ComboboxModelObserver* observer) override;

  // To allow testing failure states.
  void SetFailureModeForTests(bool failed_to_load_data);

 private:
  // Start the potentially asynchronous process of loading region data.
  void LoadRegionData(const std::string& country_code);

  // Callback for ::i18n::addressinput::PreloadSupplier::LoadRules
  void RegionDataLoaded(bool success, const std::string&, int rule_count);

  // Whether the region data load failed or not.
  bool failed_to_load_data_;

  // Set to true during region data load, and false otherwise. Whether the load
  // succeeded or not doesn't affect this value.
  bool pending_region_data_load_;

  // The application locale.
  const std::string app_locale_;

  // The callback to give to |region_data_supplier_| for async operations.
  ::i18n::addressinput::scoped_ptr<
      ::i18n::addressinput::PreloadSupplier::Callback>
      region_data_supplier_callback_;

  // A supplier of region data.
  ::i18n::addressinput::PreloadSupplier region_data_supplier_;

  // List of <code, name> pairs for ADDRESS_HOME_STATE combobox values;
  std::vector<std::pair<std::string, std::string>> regions_;

  // To be called when the data for the given country code was loaded.
  base::ObserverList<ui::ComboboxModelObserver> observers_;

  DISALLOW_COPY_AND_ASSIGN(RegionComboboxModel);
};

}  // namespace autofill

#endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_REGION_COMBOBOX_MODEL_H_