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 #ifndef CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_
      6 #define CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_
      7 
      8 #include <vector>
      9 
     10 #include "base/basictypes.h"
     11 #include "base/compiler_specific.h"
     12 #include "base/strings/string16.h"
     13 #include "components/autofill/core/browser/autofill_manager_delegate.h"
     14 #include "ui/base/models/simple_menu_model.h"
     15 
     16 class AutofillMetrics;
     17 class PrefService;
     18 
     19 namespace autofill {
     20 
     21 // A delegate interface to allow the AccountChooserModel to inform its owner
     22 // of changes.
     23 class AccountChooserModelDelegate {
     24  public:
     25   virtual ~AccountChooserModelDelegate();
     26 
     27   // Called when the active account has changed.
     28   virtual void AccountChoiceChanged() = 0;
     29 
     30   // Called when the account chooser UI needs to be updated.
     31   virtual void UpdateAccountChooserView() = 0;
     32 };
     33 
     34 // A menu model for the account chooser. This allows users to switch between
     35 // Online Wallet accounts and local Autofill data.
     36 // Terminology:
     37 // - "Active Wallet account": the account used for communications with the
     38 // Online Wallet service. There may be multiple signed-in accounts, but at any
     39 // point of time at most one of is active.
     40 class AccountChooserModel : public ui::SimpleMenuModel,
     41                             public ui::SimpleMenuModel::Delegate {
     42  public:
     43   AccountChooserModel(AccountChooserModelDelegate* delegate,
     44                       PrefService* prefs,
     45                       const AutofillMetrics& metric_logger,
     46                       DialogType dialog_type);
     47   virtual ~AccountChooserModel();
     48 
     49   // ui::SimpleMenuModel::Delegate implementation.
     50   virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
     51   virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
     52   virtual bool GetAcceleratorForCommandId(
     53       int command_id,
     54       ui::Accelerator* accelerator) OVERRIDE;
     55   virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
     56 
     57   // Sets the selection to the currently active Online Wallet account.
     58   // Should be called if the user attempts to sign into the Online Wallet
     59   // (e.g. when the user clicks the "Sign-in" link).
     60   void SelectActiveWalletAccount();
     61 
     62   // Sets the selection to the local Autofill data.
     63   void SelectUseAutofill();
     64 
     65   // Returns true if there are any accounts for the user to choose from.
     66   bool HasAccountsToChoose() const;
     67 
     68   // Sets the name of the account used to communicate with the Online Wallet.
     69   void SetActiveWalletAccountName(const string16& account);
     70 
     71   // Clears the name of the account used to communicate with the Online Wallet.
     72   // Any Wallet error automatically clears the currently active account name.
     73   void ClearActiveWalletAccountName();
     74 
     75   // Returns the name of the currently active account, or an empty string.
     76   const string16& active_wallet_account_name() const {
     77     return active_wallet_account_name_;
     78   }
     79 
     80   // Disables all Wallet accounts and switches to the local Autofill data.
     81   // Should be called when the Wallet server returns an error with the message
     82   // to be displayed. If |message| is empty the error state will be cleared.
     83   void SetHadWalletError(const base::string16& message);
     84 
     85   bool HadWalletError() const;
     86 
     87   // Switches the dialog to the local Autofill data.
     88   // Should be called when the Online Wallet sign-in attempt has failed.
     89   void SetHadWalletSigninError();
     90 
     91   // Returns true if the selected account is an Online Wallet account.
     92   bool WalletIsSelected() const;
     93 
     94   // Returns true if the current selection matches the currently active
     95   // Wallet account.
     96   bool IsActiveWalletAccountSelected() const;
     97 
     98   // Returns the command id of the current selection.
     99   int checked_item() const { return checked_item_; }
    100 
    101   base::string16 wallet_error_message() const { return wallet_error_message_; }
    102 
    103  protected:
    104   // Command IDs of the items in this menu; protected for the tests.
    105   // kActiveWalletItemId is the currently active account.
    106   // kAutofillItemId is "Pay without the Wallet" (local autofill data).
    107   // In the future, kFirstAdditionalItemId will be added as the first id
    108   // for additional accounts.
    109   static const int kActiveWalletItemId;
    110   static const int kAutofillItemId;
    111 
    112  private:
    113   // Reconstructs the set of menu items.
    114   void ReconstructMenuItems();
    115 
    116   AccountChooserModelDelegate* delegate_;
    117 
    118   // The command id of the currently selected item.
    119   int checked_item_;
    120 
    121   // The message to be displayed if there is a Wallet error. This message is
    122   // only non-empty if a Wallet error has occurred.
    123   base::string16 wallet_error_message_;
    124 
    125   // For logging UMA metrics.
    126   const AutofillMetrics& metric_logger_;
    127   const DialogType dialog_type_;
    128 
    129   // The name (email) of the account currently used in communications with the
    130   // Online Wallet service.
    131   string16 active_wallet_account_name_;
    132 
    133   DISALLOW_COPY_AND_ASSIGN(AccountChooserModel);
    134 };
    135 
    136 }  // namespace autofill
    137 
    138 #endif  // CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_
    139