- add sources.
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / autofill / account_chooser_model.cc
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
21 namespace autofill {
22
23 const int AccountChooserModel::kAutofillItemId = 0;
24 const int AccountChooserModel::kWalletAccountsStartId = 1;
25
26 AccountChooserModelDelegate::~AccountChooserModelDelegate() {}
27
28 AccountChooserModel::AccountChooserModel(
29     AccountChooserModelDelegate* delegate,
30     Profile* profile,
31     const AutofillMetrics& metric_logger)
32     : ui::SimpleMenuModel(this),
33       delegate_(delegate),
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;
42   }
43
44   ReconstructMenuItems();
45 }
46
47 AccountChooserModel::~AccountChooserModel() {
48 }
49
50 void AccountChooserModel::MenuWillShow() {
51   ui::SimpleMenuModel::MenuWillShow();
52 }
53
54 void AccountChooserModel::SelectActiveWalletAccount() {
55   ExecuteCommand(kWalletAccountsStartId + active_wallet_account_, 0);
56 }
57
58 void AccountChooserModel::SelectUseAutofill() {
59   ExecuteCommand(kAutofillItemId, 0);
60 }
61
62 bool AccountChooserModel::HasAccountsToChoose() const {
63   return !wallet_accounts_.empty();
64 }
65
66 void AccountChooserModel::SetWalletAccounts(
67     const std::vector<std::string>& accounts) {
68   wallet_accounts_ = accounts;
69   ReconstructMenuItems();
70   delegate_->UpdateAccountChooserView();
71 }
72
73 void AccountChooserModel::ClearWalletAccounts() {
74   wallet_accounts_.clear();
75   if (WalletIsSelected())
76     checked_item_ = kWalletAccountsStartId;
77
78   ReconstructMenuItems();
79   delegate_->UpdateAccountChooserView();
80 }
81
82 base::string16 AccountChooserModel::GetActiveWalletAccountName() const {
83   if (wallet_accounts_.empty())
84     return base::string16();
85
86   return UTF8ToUTF16(wallet_accounts_[GetActiveWalletAccountIndex()]);
87 }
88
89 size_t AccountChooserModel::GetActiveWalletAccountIndex() const {
90   return active_wallet_account_;
91 }
92
93 bool AccountChooserModel::IsCommandIdChecked(int command_id) const {
94   return command_id == checked_item_;
95 }
96
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_)
100     return false;
101
102   return true;
103 }
104
105 bool AccountChooserModel::GetAcceleratorForCommandId(
106     int command_id,
107     ui::Accelerator* accelerator) {
108   return false;
109 }
110
111 void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) {
112   if (checked_item_ == command_id)
113     return;
114
115   // Log metrics.
116   AutofillMetrics::DialogUiEvent chooser_event;
117   if (command_id == kAutofillItemId) {
118     chooser_event =
119         AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_AUTOFILL;
120   } else if (checked_item_ == kAutofillItemId) {
121     chooser_event =
122         AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_WALLET;
123   } else {
124     chooser_event =
125         AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_WALLET_ACCOUNT;
126   }
127   metric_logger_.LogDialogUiEvent(chooser_event);
128
129   checked_item_ = command_id;
130   if (checked_item_ >= kWalletAccountsStartId)
131     active_wallet_account_ = checked_item_ - kWalletAccountsStartId;
132
133   ReconstructMenuItems();
134   delegate_->AccountChoiceChanged();
135 }
136
137 void AccountChooserModel::MenuWillShow(ui::SimpleMenuModel* source) {
138   delegate_->AccountChooserWillShow();
139 }
140
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);
146 }
147
148 void AccountChooserModel::SetHadWalletSigninError() {
149   ClearWalletAccounts();
150   ExecuteCommand(kAutofillItemId, 0);
151 }
152
153 bool AccountChooserModel::WalletIsSelected() const {
154   return checked_item_ != kAutofillItemId;
155 }
156
157 void AccountChooserModel::ReconstructMenuItems() {
158   Clear();
159   const gfx::Image& wallet_icon =
160       ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON);
161
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);
167     }
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));
173   }
174
175   AddCheckItemWithStringId(kAutofillItemId,
176                            IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET);
177 }
178
179 }  // namespace autofill