account_chooser_model.cc revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/ui/autofill/account_chooser_model.h" 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h" 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/prefs/pref_service.h" 92a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "base/strings/string_number_conversions.h" 101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/strings/stringprintf.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/strings/utf_string_conversions.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/time/time.h" 13bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch#include "chrome/browser/profiles/profile.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/common/pref_names.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "components/autofill/core/browser/autofill_metrics.h" 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/generated_resources.h" 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "grit/theme_resources.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/l10n/l10n_util.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/resource/resource_bundle.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace autofill { 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int AccountChooserModel::kWalletAddAccountId = 0; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const int AccountChooserModel::kAutofillItemId = 1; 25a02191e04bc25c4935f804f2c080ae28663d096dBen Murdochconst int AccountChooserModel::kWalletAccountsStartId = 2; 26a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 27a02191e04bc25c4935f804f2c080ae28663d096dBen MurdochAccountChooserModelDelegate::~AccountChooserModelDelegate() {} 28a02191e04bc25c4935f804f2c080ae28663d096dBen Murdoch 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AccountChooserModel::AccountChooserModel( 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AccountChooserModelDelegate* delegate, 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* profile, 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool disable_wallet, 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const AutofillMetrics& metric_logger) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) : ui::SimpleMenuModel(this), 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) delegate_(delegate), 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) checked_item_(kWalletAccountsStartId), 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) had_wallet_error_(false), 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) metric_logger_(metric_logger) { 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) if (profile->GetPrefs()->GetBoolean( 402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ::prefs::kAutofillDialogPayWithoutWallet) || 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) profile->IsOffTheRecord() || 422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) disable_wallet) { 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) checked_item_ = kAutofillItemId; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) } 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 46a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) ReconstructMenuItems(); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)AccountChooserModel::~AccountChooserModel() {} 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void AccountChooserModel::MenuWillShow() { 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ui::SimpleMenuModel::MenuWillShow(); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55void AccountChooserModel::SelectWalletAccount(size_t user_index) { 56 DCHECK(user_index == 0U || user_index < wallet_accounts_.size()); 57 checked_item_ = kWalletAccountsStartId + user_index; 58} 59 60void AccountChooserModel::SelectUseAutofill() { 61 checked_item_ = kAutofillItemId; 62} 63 64bool AccountChooserModel::HasAccountsToChoose() const { 65 return !wallet_accounts_.empty(); 66} 67 68void AccountChooserModel::SetWalletAccounts( 69 const std::vector<std::string>& accounts, 70 size_t active_index) { 71 wallet_accounts_ = accounts; 72 SelectWalletAccount(active_index); 73 74 ReconstructMenuItems(); 75 delegate_->UpdateAccountChooserView(); 76} 77 78void AccountChooserModel::ClearWalletAccounts() { 79 wallet_accounts_.clear(); 80 if (WalletIsSelected()) 81 checked_item_ = kWalletAccountsStartId; 82 83 ReconstructMenuItems(); 84 delegate_->UpdateAccountChooserView(); 85} 86 87base::string16 AccountChooserModel::GetActiveWalletAccountName() const { 88 if (wallet_accounts_.empty()) 89 return base::string16(); 90 91 return base::UTF8ToUTF16(wallet_accounts_[GetActiveWalletAccountIndex()]); 92} 93 94size_t AccountChooserModel::GetActiveWalletAccountIndex() const { 95 if (!WalletIsSelected()) 96 return 0; 97 98 return checked_item_ - kWalletAccountsStartId; 99} 100 101bool AccountChooserModel::IsCommandIdChecked(int command_id) const { 102 return command_id == checked_item_; 103} 104 105bool AccountChooserModel::IsCommandIdEnabled(int command_id) const { 106 // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts. 107 if (command_id != kAutofillItemId && had_wallet_error_) 108 return false; 109 110 return true; 111} 112 113bool AccountChooserModel::GetAcceleratorForCommandId( 114 int command_id, 115 ui::Accelerator* accelerator) { 116 return false; 117} 118 119void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) { 120 if (checked_item_ == command_id) 121 return; 122 123 // Log metrics. 124 AutofillMetrics::DialogUiEvent chooser_event; 125 if (command_id == kAutofillItemId) { 126 chooser_event = 127 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_AUTOFILL; 128 } else if (command_id == kWalletAddAccountId) { 129 chooser_event = 130 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_TRIED_TO_ADD_ACCOUNT; 131 } else if (checked_item_ == kAutofillItemId) { 132 chooser_event = 133 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_TO_WALLET; 134 } else { 135 chooser_event = 136 AutofillMetrics::DIALOG_UI_ACCOUNT_CHOOSER_SWITCHED_WALLET_ACCOUNT; 137 } 138 metric_logger_.LogDialogUiEvent(chooser_event); 139 140 DoAccountSwitch(command_id); 141} 142 143void AccountChooserModel::MenuWillShow(ui::SimpleMenuModel* source) { 144 delegate_->AccountChooserWillShow(); 145} 146 147void AccountChooserModel::SetHadWalletError() { 148 // Any non-sign-in error disables all Wallet accounts. 149 had_wallet_error_ = true; 150 ClearWalletAccounts(); 151 DoAccountSwitch(kAutofillItemId); 152} 153 154void AccountChooserModel::SetHadWalletSigninError() { 155 ClearWalletAccounts(); 156 DoAccountSwitch(kAutofillItemId); 157} 158 159bool AccountChooserModel::WalletIsSelected() const { 160 return checked_item_ != kAutofillItemId; 161} 162 163void AccountChooserModel::ReconstructMenuItems() { 164 Clear(); 165 166 if (!wallet_accounts_.empty()) { 167 for (size_t i = 0; i < wallet_accounts_.size(); ++i) { 168 int item_id = kWalletAccountsStartId + i; 169 AddCheckItem(item_id, base::UTF8ToUTF16(wallet_accounts_[i])); 170 } 171 } else if (checked_item_ == kWalletAccountsStartId) { 172 // A selected active Wallet account without account names means 173 // that the sign-in attempt is in progress. 174 AddCheckItem(kWalletAccountsStartId, 175 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET)); 176 } 177 178 AddCheckItemWithStringId(kWalletAddAccountId, 179 IDS_AUTOFILL_DIALOG_ADD_ACCOUNT); 180 AddCheckItemWithStringId(kAutofillItemId, 181 IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET); 182} 183 184void AccountChooserModel::DoAccountSwitch(int command_id) { 185 if (checked_item_ == command_id) 186 return; 187 188 if (command_id == kWalletAddAccountId) { 189 delegate_->AddAccount(); 190 return; 191 } 192 193 checked_item_ = command_id; 194 ReconstructMenuItems(); 195 delegate_->AccountChoiceChanged(); 196} 197 198} // namespace autofill 199