account_chooser_model.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
13883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher// Copyright 2013 The Chromium Authors. All rights reserved.
23883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher// Use of this source code is governed by a BSD-style license that can be
33883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher// found in the LICENSE file.
43883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher
53883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher#include "chrome/browser/ui/autofill/account_chooser_model.h"
63883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher
73883e66cfd55de70d89831cf26f9ae53931d11d3Eric Christopher#include "base/bind.h"
8#include "base/prefs/pref_service.h"
9#include "base/stringprintf.h"
10#include "base/strings/string_number_conversions.h"
11#include "base/time.h"
12#include "base/utf_string_conversions.h"
13#include "chrome/common/pref_names.h"
14#include "components/autofill/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    DialogType dialog_type)
32    : ui::SimpleMenuModel(this),
33      delegate_(delegate),
34      checked_item_(
35          prefs->GetBoolean(::prefs::kAutofillDialogPayWithoutWallet) ?
36              kAutofillItemId : kActiveWalletItemId),
37      had_wallet_error_(false),
38      metric_logger_(metric_logger),
39      dialog_type_(dialog_type) {
40  ReconstructMenuItems();
41}
42
43AccountChooserModel::~AccountChooserModel() {
44}
45
46void AccountChooserModel::SelectActiveWalletAccount() {
47  ExecuteCommand(kActiveWalletItemId, 0);
48}
49
50void AccountChooserModel::SelectUseAutofill() {
51  ExecuteCommand(kAutofillItemId, 0);
52}
53
54bool AccountChooserModel::HasAccountsToChoose() const {
55  return !active_wallet_account_name_.empty();
56}
57
58void AccountChooserModel::SetActiveWalletAccountName(
59    const string16& account) {
60  active_wallet_account_name_ = account;
61  ReconstructMenuItems();
62  delegate_->UpdateAccountChooserView();
63}
64
65void AccountChooserModel::ClearActiveWalletAccountName() {
66  active_wallet_account_name_.clear();
67  ReconstructMenuItems();
68  delegate_->UpdateAccountChooserView();
69}
70
71bool AccountChooserModel::IsCommandIdChecked(int command_id) const {
72  return command_id == checked_item_;
73}
74
75bool AccountChooserModel::IsCommandIdEnabled(int command_id) const {
76  // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts.
77  if (command_id != kAutofillItemId && had_wallet_error_)
78    return false;
79
80  return true;
81}
82
83bool AccountChooserModel::GetAcceleratorForCommandId(
84    int command_id,
85    ui::Accelerator* accelerator) {
86  return false;
87}
88
89void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) {
90  if (checked_item_ == command_id)
91    return;
92
93  // Log metrics.
94   AutofillMetrics::DialogUiEvent chooser_event;
95   if (command_id == kAutofillItemId) {
96     chooser_event =
97         AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_AUTOFILL;
98   } else if (checked_item_ == kAutofillItemId) {
99     chooser_event =
100         AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_WALLET;
101   } else {
102     chooser_event =
103         AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_WALLET_ACCOUNT;
104   }
105   metric_logger_.LogDialogUiEvent(dialog_type_, chooser_event);
106
107   checked_item_ = command_id;
108  ReconstructMenuItems();
109  delegate_->AccountChoiceChanged();
110}
111
112void AccountChooserModel::SetHadWalletError() {
113  // Any non-sign-in error disables all Wallet accounts.
114  had_wallet_error_ = true;
115  ClearActiveWalletAccountName();
116  ExecuteCommand(kAutofillItemId, 0);
117}
118
119void AccountChooserModel::SetHadWalletSigninError() {
120  ClearActiveWalletAccountName();
121  ExecuteCommand(kAutofillItemId, 0);
122}
123
124bool AccountChooserModel::WalletIsSelected() const {
125  return checked_item_ != kAutofillItemId;
126}
127
128bool AccountChooserModel::IsActiveWalletAccountSelected() const {
129  return checked_item_ == kActiveWalletItemId;
130}
131
132void AccountChooserModel::ReconstructMenuItems() {
133  Clear();
134  const gfx::Image& wallet_icon =
135      ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON);
136
137  if (!active_wallet_account_name_.empty()) {
138    AddCheckItem(kActiveWalletItemId, active_wallet_account_name_);
139    SetIcon(GetIndexOfCommandId(kActiveWalletItemId), wallet_icon);
140  } else if (checked_item_ == kActiveWalletItemId) {
141    // A selected active Wallet account with an empty account name means
142    // that the sign-in attempt is in progress.
143    // TODO(aruslan): http://crbug.com/230932
144    // A throbber should be shown until the Wallet account name is set.
145    AddCheckItem(kActiveWalletItemId,
146                 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET));
147  }
148
149  AddCheckItemWithStringId(kAutofillItemId,
150                           IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
151}
152
153}  // namespace autofill
154