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