account_chooser_model.cc revision f2477e01787aa58f445919b809d89e252beef54f
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.
4
5#include "chrome/browser/ui/autofill/account_chooser_model.h"
6
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 "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"
20
21namespace autofill {
22
23const int AccountChooserModel::kWalletAddAccountId = 0;
24const int AccountChooserModel::kAutofillItemId = 1;
25const int AccountChooserModel::kWalletAccountsStartId = 2;
26
27AccountChooserModelDelegate::~AccountChooserModelDelegate() {}
28
29AccountChooserModel::AccountChooserModel(
30    AccountChooserModelDelegate* delegate,
31    Profile* profile,
32    const AutofillMetrics& metric_logger)
33    : ui::SimpleMenuModel(this),
34      delegate_(delegate),
35      checked_item_(kWalletAccountsStartId),
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;
42  }
43
44  ReconstructMenuItems();
45}
46
47AccountChooserModel::~AccountChooserModel() {}
48
49void AccountChooserModel::MenuWillShow() {
50  ui::SimpleMenuModel::MenuWillShow();
51}
52
53void AccountChooserModel::SelectWalletAccount(size_t user_index) {
54  DCHECK(user_index == 0U || user_index < wallet_accounts_.size());
55  checked_item_ = kWalletAccountsStartId + user_index;
56}
57
58void AccountChooserModel::SelectUseAutofill() {
59  checked_item_ = kAutofillItemId;
60}
61
62bool AccountChooserModel::HasAccountsToChoose() const {
63  return !wallet_accounts_.empty();
64}
65
66void AccountChooserModel::SetWalletAccounts(
67    const std::vector<std::string>& accounts,
68    size_t active_index) {
69  wallet_accounts_ = accounts;
70  SelectWalletAccount(active_index);
71
72  ReconstructMenuItems();
73  delegate_->UpdateAccountChooserView();
74}
75
76void AccountChooserModel::ClearWalletAccounts() {
77  wallet_accounts_.clear();
78  if (WalletIsSelected())
79    checked_item_ = kWalletAccountsStartId;
80
81  ReconstructMenuItems();
82  delegate_->UpdateAccountChooserView();
83}
84
85base::string16 AccountChooserModel::GetActiveWalletAccountName() const {
86  if (wallet_accounts_.empty())
87    return base::string16();
88
89  return UTF8ToUTF16(wallet_accounts_[GetActiveWalletAccountIndex()]);
90}
91
92size_t AccountChooserModel::GetActiveWalletAccountIndex() const {
93  if (!WalletIsSelected())
94    return 0;
95
96  return checked_item_ - kWalletAccountsStartId;
97}
98
99bool AccountChooserModel::IsCommandIdChecked(int command_id) const {
100  return command_id == checked_item_;
101}
102
103bool AccountChooserModel::IsCommandIdEnabled(int command_id) const {
104  // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts.
105  if (command_id != kAutofillItemId && had_wallet_error_)
106    return false;
107
108  return true;
109}
110
111bool AccountChooserModel::GetAcceleratorForCommandId(
112    int command_id,
113    ui::Accelerator* accelerator) {
114  return false;
115}
116
117void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) {
118  if (checked_item_ == command_id)
119    return;
120
121  // Log metrics.
122  AutofillMetrics::DialogUiEvent chooser_event;
123  if (command_id == kAutofillItemId) {
124    chooser_event =
125        AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_AUTOFILL;
126  } else if (command_id == kWalletAddAccountId) {
127    chooser_event =
128        AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_TRIED_TO_ADD_ACCOUNT;
129  } else if (checked_item_ == kAutofillItemId) {
130    chooser_event =
131        AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_WALLET;
132  } else {
133    chooser_event =
134        AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_WALLET_ACCOUNT;
135  }
136  metric_logger_.LogDialogUiEvent(chooser_event);
137
138  if (command_id == kWalletAddAccountId) {
139    delegate_->AddAccount();
140    return;
141  }
142
143  checked_item_ = command_id;
144  ReconstructMenuItems();
145  delegate_->AccountChoiceChanged();
146}
147
148void AccountChooserModel::MenuWillShow(ui::SimpleMenuModel* source) {
149  delegate_->AccountChooserWillShow();
150}
151
152void AccountChooserModel::SetHadWalletError() {
153  // Any non-sign-in error disables all Wallet accounts.
154  had_wallet_error_ = true;
155  ClearWalletAccounts();
156  ExecuteCommand(kAutofillItemId, 0);
157}
158
159void AccountChooserModel::SetHadWalletSigninError() {
160  ClearWalletAccounts();
161  ExecuteCommand(kAutofillItemId, 0);
162}
163
164bool AccountChooserModel::WalletIsSelected() const {
165  return checked_item_ != kAutofillItemId;
166}
167
168void AccountChooserModel::ReconstructMenuItems() {
169  Clear();
170
171  if (!wallet_accounts_.empty()) {
172    for (size_t i = 0; i < wallet_accounts_.size(); ++i) {
173      int item_id = kWalletAccountsStartId + i;
174      AddCheckItem(item_id, UTF8ToUTF16(wallet_accounts_[i]));
175    }
176  } else if (checked_item_ == kWalletAccountsStartId) {
177    // A selected active Wallet account without account names means
178    // that the sign-in attempt is in progress.
179    AddCheckItem(kWalletAccountsStartId,
180                 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET));
181  }
182
183  AddCheckItemWithStringId(kWalletAddAccountId,
184                           IDS_AUTOFILL_DIALOG_ADD_ACCOUNT);
185  AddCheckItemWithStringId(kAutofillItemId,
186                           IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
187}
188
189}  // namespace autofill
190