OLD | NEW |
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 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_ADDRESS_COMBOBOX_MODEL_H_ | 5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_ADDRESS_COMBOBOX_MODEL_H_ |
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_ADDRESS_COMBOBOX_MODEL_H_ | 6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_ADDRESS_COMBOBOX_MODEL_H_ |
7 | 7 |
8 #include <memory> | 8 #include <memory> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
15 #include "base/strings/string16.h" | 15 #include "base/strings/string16.h" |
16 #include "ui/base/models/combobox_model.h" | 16 #include "ui/base/models/combobox_model.h" |
17 | 17 |
18 namespace autofill { | 18 namespace autofill { |
19 | 19 |
20 class AutofillProfile; | 20 class AutofillProfile; |
21 class PersonalDataManager; | 21 class PersonalDataManager; |
22 | 22 |
23 // A combobox model for listing addresses by a generated user visible string and | 23 // A combobox model for listing addresses by a generated user visible string and |
24 // have a unique id to identify the one selected by the user. | 24 // have a unique id to identify the one selected by the user. |
25 class AddressComboboxModel : public ui::ComboboxModel { | 25 class AddressComboboxModel : public ui::ComboboxModel { |
26 public: | 26 public: |
27 // Enumerate the profiles from |personal_data_manager| to expose them in a | 27 // Enumerate the profiles from |personal_data_manager| to expose them in a |
28 // combobox using |app_locale| for proper format. | 28 // combobox using |app_locale| for proper format. |default_selected_guid| is |
| 29 // an optional argument to specify which address should be selected by |
| 30 // default. |
29 AddressComboboxModel(const PersonalDataManager& personal_data_manager, | 31 AddressComboboxModel(const PersonalDataManager& personal_data_manager, |
30 const std::string& app_locale); | 32 const std::string& app_locale, |
| 33 const std::string& default_selected_guid); |
31 ~AddressComboboxModel() override; | 34 ~AddressComboboxModel() override; |
32 | 35 |
33 // ui::ComboboxModel implementation: | 36 // ui::ComboboxModel implementation: |
34 int GetItemCount() const override; | 37 int GetItemCount() const override; |
35 base::string16 GetItemAt(int index) override; | 38 base::string16 GetItemAt(int index) override; |
36 bool IsItemSeparatorAt(int index) override; | 39 bool IsItemSeparatorAt(int index) override; |
| 40 int GetDefaultIndex() const override; |
37 void AddObserver(ui::ComboboxModelObserver* observer) override; | 41 void AddObserver(ui::ComboboxModelObserver* observer) override; |
38 void RemoveObserver(ui::ComboboxModelObserver* observer) override; | 42 void RemoveObserver(ui::ComboboxModelObserver* observer) override; |
39 | 43 |
40 // Adds |profile| to model and return its combobox index. The lifespan of | 44 // Adds |profile| to model and return its combobox index. The lifespan of |
41 // |profile| beyond this call is undefined so a copy must be made. | 45 // |profile| beyond this call is undefined so a copy must be made. |
42 int AddNewProfile(const AutofillProfile& profile); | 46 int AddNewProfile(const AutofillProfile& profile); |
43 | 47 |
44 // Returns the unique identifier of the profile at |index|, unless |index| | 48 // Returns the unique identifier of the profile at |index|, unless |index| |
45 // refers to a special entry, in which case an empty string is returned. | 49 // refers to a special entry, in which case an empty string is returned. |
46 std::string GetItemIdentifierAt(int index); | 50 std::string GetItemIdentifierAt(int index); |
47 | 51 |
48 // Returns the combobox index of the item with the given id or -1 if it's not | 52 // Returns the combobox index of the item with the given id or -1 if it's not |
49 // found. | 53 // found. |
50 int GetIndexOfIdentifier(const std::string& identifier); | 54 int GetIndexOfIdentifier(const std::string& identifier) const; |
51 | 55 |
52 private: | 56 private: |
53 // Update |addresses_| based on |profiles_cache_| and notify observers. | 57 // Update |addresses_| based on |profiles_cache_| and notify observers. |
54 void UpdateAddresses(); | 58 void UpdateAddresses(); |
55 | 59 |
56 // List of <id, user visible string> pairs for the addresses extracted from | 60 // List of <id, user visible string> pairs for the addresses extracted from |
57 // the |personal_data_manager| passed in the constructor. | 61 // the |personal_data_manager| passed in the constructor. |
58 std::vector<std::pair<std::string, base::string16>> addresses_; | 62 std::vector<std::pair<std::string, base::string16>> addresses_; |
59 | 63 |
60 // A cached copy of all profiles to allow rebuilding the differentiating | 64 // A cached copy of all profiles to allow rebuilding the differentiating |
61 // labels when new profiles are added. | 65 // labels when new profiles are added. |
62 std::vector<std::unique_ptr<AutofillProfile>> profiles_cache_; | 66 std::vector<std::unique_ptr<AutofillProfile>> profiles_cache_; |
63 | 67 |
64 // Application locale, also needed when a new profile is added. | 68 // Application locale, also needed when a new profile is added. |
65 std::string app_locale_; | 69 std::string app_locale_; |
66 | 70 |
| 71 // If non empty, the guid of the address that should be selected by default. |
| 72 std::string default_selected_guid_; |
| 73 |
67 // To be called when the data for the given country code was loaded. | 74 // To be called when the data for the given country code was loaded. |
68 base::ObserverList<ui::ComboboxModelObserver> observers_; | 75 base::ObserverList<ui::ComboboxModelObserver> observers_; |
69 | 76 |
70 DISALLOW_COPY_AND_ASSIGN(AddressComboboxModel); | 77 DISALLOW_COPY_AND_ASSIGN(AddressComboboxModel); |
71 }; | 78 }; |
72 | 79 |
73 } // namespace autofill | 80 } // namespace autofill |
74 | 81 |
75 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_ADDRESS_COMBOBOX_MODEL_H_ | 82 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_ADDRESS_COMBOBOX_MODEL_H_ |
OLD | NEW |