account_chooser_model.cc revision 58537e28ecd584eab876aee8be7156509866d23a
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/common/pref_names.h"
14#include "components/autofill/core/browser/autofill_metrics.h"
15#include "grit/generated_resources.h"
16#include "grit/theme_resources.h"
17#include "ui/base/l10n/l10n_util.h"
18#include "ui/base/resource/resource_bundle.h"
19
20namespace autofill {
21
22const int AccountChooserModel::kActiveWalletItemId = 0;
23const int AccountChooserModel::kAutofillItemId = 1;
24
25AccountChooserModelDelegate::~AccountChooserModelDelegate() {}
26
27AccountChooserModel::AccountChooserModel(
28    AccountChooserModelDelegate* delegate,
29    PrefService* prefs,
30    const AutofillMetrics& metric_logger)
31    : ui::SimpleMenuModel(this),
32      delegate_(delegate),
33      checked_item_(
34          prefs->GetBoolean(::prefs::kAutofillDialogPayWithoutWallet) ?
35              kAutofillItemId : kActiveWalletItemId),
36      had_wallet_error_(false),
37      metric_logger_(metric_logger) {
38  ReconstructMenuItems();
39}
40
41AccountChooserModel::~AccountChooserModel() {
42}
43
44void AccountChooserModel::SelectActiveWalletAccount() {
45  ExecuteCommand(kActiveWalletItemId, 0);
46}
47
48void AccountChooserModel::SelectUseAutofill() {
49  ExecuteCommand(kAutofillItemId, 0);
50}
51
52bool AccountChooserModel::HasAccountsToChoose() const {
53  return !active_wallet_account_name_.empty();
54}
55
56void AccountChooserModel::SetActiveWalletAccountName(
57    const string16& account) {
58  active_wallet_account_name_ = account;
59  ReconstructMenuItems();
60  delegate_->UpdateAccountChooserView();
61}
62
63void AccountChooserModel::ClearActiveWalletAccountName() {
64  active_wallet_account_name_.clear();
65  ReconstructMenuItems();
66  delegate_->UpdateAccountChooserView();
67}
68
69bool AccountChooserModel::IsCommandIdChecked(int command_id) const {
70  return command_id == checked_item_;
71}
72
73bool AccountChooserModel::IsCommandIdEnabled(int command_id) const {
74  // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts.
75  if (command_id != kAutofillItemId && had_wallet_error_)
76    return false;
77
78  return true;
79}
80
81bool AccountChooserModel::GetAcceleratorForCommandId(
82    int command_id,
83    ui::Accelerator* accelerator) {
84  return false;
85}
86
87void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) {
88  if (checked_item_ == command_id)
89    return;
90
91  // Log metrics.
92  AutofillMetrics::DialogUiEvent chooser_event;
93  if (command_id == kAutofillItemId) {
94    chooser_event =
95        AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_AUTOFILL;
96  } else if (checked_item_ == kAutofillItemId) {
97    chooser_event =
98        AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_WALLET;
99  } else {
100    chooser_event =
101        AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_WALLET_ACCOUNT;
102  }
103  metric_logger_.LogDialogUiEvent(chooser_event);
104
105  checked_item_ = command_id;
106  ReconstructMenuItems();
107  delegate_->AccountChoiceChanged();
108}
109
110void AccountChooserModel::SetHadWalletError() {
111  // Any non-sign-in error disables all Wallet accounts.
112  had_wallet_error_ = true;
113  ClearActiveWalletAccountName();
114  ExecuteCommand(kAutofillItemId, 0);
115}
116
117void AccountChooserModel::SetHadWalletSigninError() {
118  ClearActiveWalletAccountName();
119  ExecuteCommand(kAutofillItemId, 0);
120}
121
122bool AccountChooserModel::WalletIsSelected() const {
123  return checked_item_ != kAutofillItemId;
124}
125
126bool AccountChooserModel::IsActiveWalletAccountSelected() const {
127  return checked_item_ == kActiveWalletItemId;
128}
129
130void AccountChooserModel::ReconstructMenuItems() {
131  Clear();
132  const gfx::Image& wallet_icon =
133      ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON);
134
135  if (!active_wallet_account_name_.empty()) {
136    AddCheckItem(kActiveWalletItemId, active_wallet_account_name_);
137    SetIcon(GetIndexOfCommandId(kActiveWalletItemId), wallet_icon);
138  } else if (checked_item_ == kActiveWalletItemId) {
139    // A selected active Wallet account with an empty account name means
140    // that the sign-in attempt is in progress.
141    // TODO(aruslan): http://crbug.com/230932
142    // A throbber should be shown until the Wallet account name is set.
143    AddCheckItem(kActiveWalletItemId,
144                 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET));
145  }
146
147  AddCheckItemWithStringId(kAutofillItemId,
148                           IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
149}
150
151}  // namespace autofill
152