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

Side by Side Diff: chrome/browser/ui/autofill/autofill_dialog_models.cc

Issue 13331007: Multi-account AccountChooser for interactive autocomplete. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "chrome/browser/ui/autofill/autofill_dialog_models.h" 5 #include "chrome/browser/ui/autofill/autofill_dialog_models.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/prefs/pref_service.h" 8 #include "base/prefs/pref_service.h"
9 #include "base/stringprintf.h" 9 #include "base/stringprintf.h"
10 #include "base/strings/string_number_conversions.h" 10 #include "base/strings/string_number_conversions.h"
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 return false; 91 return false;
92 } 92 }
93 93
94 void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) { 94 void SuggestionsMenuModel::ExecuteCommand(int command_id, int event_flags) {
95 checked_item_ = command_id; 95 checked_item_ = command_id;
96 delegate_->SuggestionItemSelected(*this); 96 delegate_->SuggestionItemSelected(*this);
97 } 97 }
98 98
99 // AccountChooserModel --------------------------------------------------------- 99 // AccountChooserModel ---------------------------------------------------------
100 100
101 const int AccountChooserModel::kWalletItemId = 0; 101 const int AccountChooserModel::kContentAreaWalletItemId = 0;
Evan Stade 2013/03/29 23:08:45 I do not understand this variable name or its purp
aruslan 2013/03/30 00:07:47 Done.
102 const int AccountChooserModel::kAutofillItemId = 1; 102 const int AccountChooserModel::kAutofillItemId = 1;
103 const int AccountChooserModel::kFirstAccountItemId = 2;
103 104
104 AccountChooserModelDelegate::~AccountChooserModelDelegate() {} 105 AccountChooserModelDelegate::~AccountChooserModelDelegate() {}
105 106
106 AccountChooserModel::AccountChooserModel( 107 AccountChooserModel::AccountChooserModel(
107 AccountChooserModelDelegate* delegate, 108 AccountChooserModelDelegate* delegate,
108 PrefService* prefs) 109 PrefService* prefs)
109 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), 110 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)),
110 account_delegate_(delegate), 111 account_delegate_(delegate),
111 prefs_(prefs), 112 prefs_(prefs),
112 checked_item_(kWalletItemId), 113 checked_item_(kContentAreaWalletItemId),
113 had_wallet_error_(false) { 114 had_wallet_error_(false) {
114 pref_change_registrar_.Init(prefs); 115 pref_change_registrar_.Init(prefs);
115 pref_change_registrar_.Add( 116 pref_change_registrar_.Add(
116 prefs::kAutofillDialogPayWithoutWallet, 117 prefs::kAutofillDialogPayWithoutWallet,
117 base::Bind(&AccountChooserModel::PrefChanged, base::Unretained(this))); 118 base::Bind(&AccountChooserModel::PrefChanged, base::Unretained(this)));
118
119 // TODO(estade): proper strings and l10n.
120 AddCheckItem(kWalletItemId, ASCIIToUTF16("Google Wallet"));
121 SetIcon(
122 kWalletItemId,
123 ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON));
124 AddCheckItemWithStringId(kAutofillItemId,
125 IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
126 UpdateCheckmarkFromPref(); 119 UpdateCheckmarkFromPref();
120 ReconstructMenuItems();
127 } 121 }
128 122
129 AccountChooserModel::~AccountChooserModel() { 123 AccountChooserModel::~AccountChooserModel() {
130 } 124 }
131 125
126 void AccountChooserModel::ForceWalletAccountSelected() {
127 if (checked_item_ == kContentAreaWalletItemId) return;
Evan Stade 2013/03/29 23:08:45 please put return on a new line.
aruslan 2013/03/30 00:07:47 Done.
128 checked_item_ = kContentAreaWalletItemId;
129 account_delegate_->AccountChoiceChanged();
130 }
131
132 bool AccountChooserModel::HasAccountsToChoose() const {
133 return !had_wallet_error_ &&
134 !(current_username_.empty() && available_accounts_.empty());
135 }
136
137 void AccountChooserModel::SetAvailableAccounts(
138 const std::vector<std::string>& accounts) {
139 available_accounts_ = accounts;
140 ReconstructMenuItems();
141 }
142
143 void AccountChooserModel::SetCurrentlySignedInAccount(
144 const std::string& account) {
145 current_username_ = account;
146 ReconstructMenuItems();
147 }
148
149 std::string AccountChooserModel::GetCurrentlySignedInAccount() const {
150 return current_username_;
151 }
152
153 void AccountChooserModel::ResetCurrentlySignedInAccount() {
154 current_username_.clear();
155 ReconstructMenuItems();
156 }
157
132 bool AccountChooserModel::IsCommandIdChecked(int command_id) const { 158 bool AccountChooserModel::IsCommandIdChecked(int command_id) const {
133 return command_id == checked_item_; 159 return command_id == checked_item_;
134 } 160 }
135 161
136 bool AccountChooserModel::IsCommandIdEnabled(int command_id) const { 162 bool AccountChooserModel::IsCommandIdEnabled(int command_id) const {
137 if (command_id == kWalletItemId && had_wallet_error_) 163 // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts.
164 if (command_id != kAutofillItemId && had_wallet_error_)
138 return false; 165 return false;
139 166
140 return true; 167 return true;
141 } 168 }
142 169
143 bool AccountChooserModel::GetAcceleratorForCommandId( 170 bool AccountChooserModel::GetAcceleratorForCommandId(
144 int command_id, 171 int command_id,
145 ui::Accelerator* accelerator) { 172 ui::Accelerator* accelerator) {
146 return false; 173 return false;
147 } 174 }
148 175
149 void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) { 176 void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) {
150 if (checked_item_ == command_id) 177 if (checked_item_ == command_id)
151 return; 178 return;
152 179
153 checked_item_ = command_id; 180 checked_item_ = command_id;
154 account_delegate_->AccountChoiceChanged(); 181 account_delegate_->AccountChoiceChanged();
155 } 182 }
156 183
157 void AccountChooserModel::SetHadWalletError() { 184 void AccountChooserModel::SetHadWalletError() {
185 // Any non-sign-in error disables all Wallet accounts.
158 had_wallet_error_ = true; 186 had_wallet_error_ = true;
159 checked_item_ = kAutofillItemId; 187 checked_item_ = kAutofillItemId;
188 ResetCurrentlySignedInAccount();
160 account_delegate_->AccountChoiceChanged(); 189 account_delegate_->AccountChoiceChanged();
161 } 190 }
162 191
163 void AccountChooserModel::SetHadWalletSigninError() { 192 void AccountChooserModel::SetHadWalletSigninError() {
164 checked_item_ = kAutofillItemId; 193 checked_item_ = kAutofillItemId;
194 ResetCurrentlySignedInAccount();
165 account_delegate_->AccountChoiceChanged(); 195 account_delegate_->AccountChoiceChanged();
166 } 196 }
167 197
168 bool AccountChooserModel::WalletIsSelected() const { 198 bool AccountChooserModel::WalletIsSelected() const {
169 return checked_item_ == kWalletItemId; 199 return checked_item_ != kAutofillItemId;
200 }
201
202 bool AccountChooserModel::IsCurrentlySignedInAccountSelected() const {
203 return checked_item_ == kContentAreaWalletItemId;
170 } 204 }
171 205
172 void AccountChooserModel::PrefChanged(const std::string& pref) { 206 void AccountChooserModel::PrefChanged(const std::string& pref) {
173 DCHECK(pref == prefs::kAutofillDialogPayWithoutWallet); 207 DCHECK(pref == prefs::kAutofillDialogPayWithoutWallet);
174 UpdateCheckmarkFromPref(); 208 UpdateCheckmarkFromPref();
175 account_delegate_->AccountChoiceChanged(); 209 account_delegate_->AccountChoiceChanged();
176 } 210 }
177 211
178 void AccountChooserModel::UpdateCheckmarkFromPref() { 212 void AccountChooserModel::UpdateCheckmarkFromPref() {
179 if (prefs_->GetBoolean(prefs::kAutofillDialogPayWithoutWallet)) 213 if (prefs_->GetBoolean(prefs::kAutofillDialogPayWithoutWallet))
180 checked_item_ = kAutofillItemId; 214 checked_item_ = kAutofillItemId;
181 else 215 else
182 checked_item_ = kWalletItemId; 216 checked_item_ = kContentAreaWalletItemId;
217 }
218
219 void AccountChooserModel::ReconstructMenuItems() {
220 // If menu has to be reconstructed, either the currently sign-in account
221 // should be selected, or the autofill data should be used.
222 DCHECK_GE(kAutofillItemId, checked_item_);
223 Clear();
224 const gfx::Image& wallet_icon =
225 ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON);
226
227 if (!current_username_.empty()) {
228 AddCheckItem(kContentAreaWalletItemId, ASCIIToUTF16(current_username_));
229 SetIcon(GetIndexOfCommandId(kContentAreaWalletItemId), wallet_icon);
230 }
231
232 for (size_t i = 0; i < available_accounts_.size(); ++i) {
233 if (available_accounts_[i] != current_username_) {
234 AddCheckItem(kFirstAccountItemId + i,
235 ASCIIToUTF16(available_accounts_[i]));
236 SetIcon(GetIndexOfCommandId(kFirstAccountItemId + i), wallet_icon);
237 }
238 }
239
240 AddCheckItemWithStringId(kAutofillItemId,
241 IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
183 } 242 }
184 243
185 // MonthComboboxModel ---------------------------------------------------------- 244 // MonthComboboxModel ----------------------------------------------------------
186 245
187 MonthComboboxModel::MonthComboboxModel() {} 246 MonthComboboxModel::MonthComboboxModel() {}
188 247
189 MonthComboboxModel::~MonthComboboxModel() {} 248 MonthComboboxModel::~MonthComboboxModel() {}
190 249
191 int MonthComboboxModel::GetItemCount() const { 250 int MonthComboboxModel::GetItemCount() const {
192 // 12 months plus the empty entry. 251 // 12 months plus the empty entry.
(...skipping 26 matching lines...) Expand all
219 } 278 }
220 279
221 string16 YearComboboxModel::GetItemAt(int index) { 280 string16 YearComboboxModel::GetItemAt(int index) {
222 if (index == 0) 281 if (index == 0)
223 return string16(); 282 return string16();
224 283
225 return base::IntToString16(this_year_ + index - 1); 284 return base::IntToString16(this_year_ + index - 1);
226 } 285 }
227 286
228 } // autofill 287 } // autofill
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698