1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/autofill/account_chooser_model.h"
8 #include "base/prefs/pref_service.h"
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "base/time/time.h"
13 #include "chrome/browser/profiles/profile.h"
14 #include "chrome/common/pref_names.h"
15 #include "components/autofill/core/browser/autofill_metrics.h"
16 #include "grit/generated_resources.h"
17 #include "grit/theme_resources.h"
18 #include "ui/base/l10n/l10n_util.h"
19 #include "ui/base/resource/resource_bundle.h"
23 const int AccountChooserModel::kAutofillItemId = 0;
24 const int AccountChooserModel::kWalletAccountsStartId = 1;
26 AccountChooserModelDelegate::~AccountChooserModelDelegate() {}
28 AccountChooserModel::AccountChooserModel(
29 AccountChooserModelDelegate* delegate,
31 const AutofillMetrics& metric_logger)
32 : ui::SimpleMenuModel(this),
34 checked_item_(kWalletAccountsStartId),
35 active_wallet_account_(0U),
36 had_wallet_error_(false),
37 metric_logger_(metric_logger) {
38 if (profile->GetPrefs()->GetBoolean(
39 ::prefs::kAutofillDialogPayWithoutWallet) ||
40 profile->IsOffTheRecord()) {
41 checked_item_ = kAutofillItemId;
44 ReconstructMenuItems();
47 AccountChooserModel::~AccountChooserModel() {
50 void AccountChooserModel::MenuWillShow() {
51 ui::SimpleMenuModel::MenuWillShow();
54 void AccountChooserModel::SelectActiveWalletAccount() {
55 ExecuteCommand(kWalletAccountsStartId + active_wallet_account_, 0);
58 void AccountChooserModel::SelectUseAutofill() {
59 ExecuteCommand(kAutofillItemId, 0);
62 bool AccountChooserModel::HasAccountsToChoose() const {
63 return !wallet_accounts_.empty();
66 void AccountChooserModel::SetWalletAccounts(
67 const std::vector<std::string>& accounts) {
68 wallet_accounts_ = accounts;
69 ReconstructMenuItems();
70 delegate_->UpdateAccountChooserView();
73 void AccountChooserModel::ClearWalletAccounts() {
74 wallet_accounts_.clear();
75 if (WalletIsSelected())
76 checked_item_ = kWalletAccountsStartId;
78 ReconstructMenuItems();
79 delegate_->UpdateAccountChooserView();
82 base::string16 AccountChooserModel::GetActiveWalletAccountName() const {
83 if (wallet_accounts_.empty())
84 return base::string16();
86 return UTF8ToUTF16(wallet_accounts_[GetActiveWalletAccountIndex()]);
89 size_t AccountChooserModel::GetActiveWalletAccountIndex() const {
90 return active_wallet_account_;
93 bool AccountChooserModel::IsCommandIdChecked(int command_id) const {
94 return command_id == checked_item_;
97 bool AccountChooserModel::IsCommandIdEnabled(int command_id) const {
98 // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts.
99 if (command_id != kAutofillItemId && had_wallet_error_)
105 bool AccountChooserModel::GetAcceleratorForCommandId(
107 ui::Accelerator* accelerator) {
111 void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) {
112 if (checked_item_ == command_id)
116 AutofillMetrics::DialogUiEvent chooser_event;
117 if (command_id == kAutofillItemId) {
119 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_AUTOFILL;
120 } else if (checked_item_ == kAutofillItemId) {
122 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_WALLET;
125 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_WALLET_ACCOUNT;
127 metric_logger_.LogDialogUiEvent(chooser_event);
129 checked_item_ = command_id;
130 if (checked_item_ >= kWalletAccountsStartId)
131 active_wallet_account_ = checked_item_ - kWalletAccountsStartId;
133 ReconstructMenuItems();
134 delegate_->AccountChoiceChanged();
137 void AccountChooserModel::MenuWillShow(ui::SimpleMenuModel* source) {
138 delegate_->AccountChooserWillShow();
141 void AccountChooserModel::SetHadWalletError() {
142 // Any non-sign-in error disables all Wallet accounts.
143 had_wallet_error_ = true;
144 ClearWalletAccounts();
145 ExecuteCommand(kAutofillItemId, 0);
148 void AccountChooserModel::SetHadWalletSigninError() {
149 ClearWalletAccounts();
150 ExecuteCommand(kAutofillItemId, 0);
153 bool AccountChooserModel::WalletIsSelected() const {
154 return checked_item_ != kAutofillItemId;
157 void AccountChooserModel::ReconstructMenuItems() {
159 const gfx::Image& wallet_icon =
160 ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON);
162 if (!wallet_accounts_.empty()) {
163 for (size_t i = 0; i < wallet_accounts_.size(); ++i) {
164 int item_id = kWalletAccountsStartId + i;
165 AddCheckItem(item_id, UTF8ToUTF16(wallet_accounts_[i]));
166 SetIcon(GetIndexOfCommandId(item_id), wallet_icon);
168 } else if (checked_item_ == kWalletAccountsStartId) {
169 // A selected active Wallet account without account names means
170 // that the sign-in attempt is in progress.
171 AddCheckItem(kWalletAccountsStartId,
172 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET));
175 AddCheckItemWithStringId(kAutofillItemId,
176 IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
179 } // namespace autofill