account_chooser_model.h revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/string16.h"
13#include "components/autofill/browser/autofill_manager_delegate.h"
14#include "ui/base/models/simple_menu_model.h"
15
16class AutofillMetrics;
17class PrefService;
18
19namespace autofill {
20
21// A delegate interface to allow the AccountChooserModel to inform its owner
22// of changes.
23class 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.
40class 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.
82  void SetHadWalletError();
83
84  // Switches the dialog to the local Autofill data.
85  // Should be called when the Online Wallet sign-in attempt has failed.
86  void SetHadWalletSigninError();
87
88  bool had_wallet_error() const { return had_wallet_error_; }
89
90  // Returns true if the selected account is an Online Wallet account.
91  bool WalletIsSelected() const;
92
93  // Returns true if the current selection matches the currently active
94  // Wallet account.
95  bool IsActiveWalletAccountSelected() const;
96
97  // Returns the command id of the current selection.
98  int checked_item() const { return checked_item_; }
99
100 protected:
101  // Command IDs of the items in this menu; protected for the tests.
102  // kActiveWalletItemId is the currently active account.
103  // kAutofillItemId is "Pay without the Wallet" (local autofill data).
104  // In the future, kFirstAdditionalItemId will be added as the first id
105  // for additional accounts.
106  static const int kActiveWalletItemId;
107  static const int kAutofillItemId;
108
109 private:
110  // Reconstructs the set of menu items.
111  void ReconstructMenuItems();
112
113  AccountChooserModelDelegate* delegate_;
114
115  // The command id of the currently selected item.
116  int checked_item_;
117
118  // Whether there has been a Wallet error while the owning dialog has been
119  // open.
120  bool had_wallet_error_;
121
122  // For logging UMA metrics.
123  const AutofillMetrics& metric_logger_;
124  const DialogType dialog_type_;
125
126  // The name (email) of the account currently used in communications with the
127  // Online Wallet service.
128  string16 active_wallet_account_name_;
129
130  DISALLOW_COPY_AND_ASSIGN(AccountChooserModel);
131};
132
133}  // namespace autofill
134
135#endif  // CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_
136