[Password manager] Temporarily add some CHECKs to investigate a crash
[chromium-blink-merge.git] / chrome / browser / ui / autofill / account_chooser_model.cc
blobb2e7d1ed384d1b4b3104809462fc32c72e21a617
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"
7 #include "base/bind.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 "chrome/grit/generated_resources.h"
16 #include "components/autofill/core/browser/autofill_metrics.h"
17 #include "ui/base/l10n/l10n_util.h"
19 namespace autofill {
21 const int AccountChooserModel::kWalletAddAccountId = 0;
22 const int AccountChooserModel::kAutofillItemId = 1;
23 const int AccountChooserModel::kWalletAccountsStartId = 2;
25 AccountChooserModelDelegate::~AccountChooserModelDelegate() {}
27 AccountChooserModel::AccountChooserModel(AccountChooserModelDelegate* delegate,
28 Profile* profile,
29 bool disable_wallet)
30 : ui::SimpleMenuModel(this),
31 delegate_(delegate),
32 checked_item_(kWalletAccountsStartId),
33 had_wallet_error_(false) {
34 if (profile->GetPrefs()->GetBoolean(
35 ::prefs::kAutofillDialogPayWithoutWallet) ||
36 profile->IsOffTheRecord() ||
37 disable_wallet) {
38 checked_item_ = kAutofillItemId;
41 ReconstructMenuItems();
44 AccountChooserModel::~AccountChooserModel() {}
46 void AccountChooserModel::SelectWalletAccount(size_t user_index) {
47 DCHECK(user_index == 0U || user_index < wallet_accounts_.size());
48 checked_item_ = kWalletAccountsStartId + user_index;
51 void AccountChooserModel::SelectUseAutofill() {
52 checked_item_ = kAutofillItemId;
55 bool AccountChooserModel::HasAccountsToChoose() const {
56 return !wallet_accounts_.empty();
59 void AccountChooserModel::SetWalletAccounts(
60 const std::vector<std::string>& accounts,
61 size_t active_index) {
62 wallet_accounts_ = accounts;
63 SelectWalletAccount(active_index);
65 ReconstructMenuItems();
66 delegate_->UpdateAccountChooserView();
69 void AccountChooserModel::ClearWalletAccounts() {
70 wallet_accounts_.clear();
71 if (WalletIsSelected())
72 checked_item_ = kWalletAccountsStartId;
74 ReconstructMenuItems();
75 delegate_->UpdateAccountChooserView();
78 base::string16 AccountChooserModel::GetActiveWalletAccountName() const {
79 if (wallet_accounts_.empty())
80 return base::string16();
82 return base::UTF8ToUTF16(wallet_accounts_[GetActiveWalletAccountIndex()]);
85 size_t AccountChooserModel::GetActiveWalletAccountIndex() const {
86 if (!WalletIsSelected())
87 return 0;
89 return checked_item_ - kWalletAccountsStartId;
92 bool AccountChooserModel::IsCommandIdChecked(int command_id) const {
93 return command_id == checked_item_;
96 bool AccountChooserModel::IsCommandIdEnabled(int command_id) const {
97 // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts.
98 if (command_id != kAutofillItemId && had_wallet_error_)
99 return false;
101 return true;
104 bool AccountChooserModel::GetAcceleratorForCommandId(
105 int command_id,
106 ui::Accelerator* accelerator) {
107 return false;
110 void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) {
111 if (checked_item_ == command_id)
112 return;
114 // Log metrics.
115 AutofillMetrics::DialogUiEvent chooser_event;
116 if (command_id == kAutofillItemId) {
117 chooser_event =
118 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_AUTOFILL;
119 } else if (command_id == kWalletAddAccountId) {
120 chooser_event =
121 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_TRIED_TO_ADD_ACCOUNT;
122 } else if (checked_item_ == kAutofillItemId) {
123 chooser_event =
124 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_WALLET;
125 } else {
126 chooser_event =
127 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_WALLET_ACCOUNT;
129 AutofillMetrics::LogDialogUiEvent(chooser_event);
131 DoAccountSwitch(command_id);
134 void AccountChooserModel::SetHadWalletError() {
135 // Any non-sign-in error disables all Wallet accounts.
136 had_wallet_error_ = true;
137 ClearWalletAccounts();
138 DoAccountSwitch(kAutofillItemId);
141 void AccountChooserModel::SetHadWalletSigninError() {
142 ClearWalletAccounts();
143 DoAccountSwitch(kAutofillItemId);
146 bool AccountChooserModel::WalletIsSelected() const {
147 return checked_item_ != kAutofillItemId;
150 void AccountChooserModel::ReconstructMenuItems() {
151 Clear();
153 if (!wallet_accounts_.empty()) {
154 for (size_t i = 0; i < wallet_accounts_.size(); ++i) {
155 int item_id = kWalletAccountsStartId + i;
156 AddCheckItem(item_id, base::UTF8ToUTF16(wallet_accounts_[i]));
158 } else if (checked_item_ == kWalletAccountsStartId) {
159 // A selected active Wallet account without account names means
160 // that the sign-in attempt is in progress.
161 AddCheckItem(kWalletAccountsStartId,
162 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET));
165 AddCheckItemWithStringId(kWalletAddAccountId,
166 IDS_AUTOFILL_DIALOG_ADD_ACCOUNT);
167 AddCheckItemWithStringId(kAutofillItemId,
168 IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
171 void AccountChooserModel::DoAccountSwitch(int command_id) {
172 if (checked_item_ == command_id)
173 return;
175 if (command_id == kWalletAddAccountId) {
176 delegate_->AddAccount();
177 return;
180 checked_item_ = command_id;
181 ReconstructMenuItems();
182 delegate_->AccountChoiceChanged();
185 } // namespace autofill