account_chooser_model.h revision f2477e01787aa58f445919b809d89e252beef54f
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 <string>
9#include <vector>
10
11#include "base/basictypes.h"
12#include "base/compiler_specific.h"
13#include "base/strings/string16.h"
14#include "components/autofill/core/browser/autofill_manager_delegate.h"
15#include "ui/base/models/simple_menu_model.h"
16
17class AutofillMetrics;
18class Profile;
19
20namespace autofill {
21
22class AccountChooserModel;
23
24// A delegate interface to allow the AccountChooserModel to inform its owner
25// of changes.
26class AccountChooserModelDelegate {
27 public:
28  virtual ~AccountChooserModelDelegate();
29
30  // Called right before the account chooser is shown.
31  virtual void AccountChooserWillShow() = 0;
32
33  // Called when the active account has changed.
34  virtual void AccountChoiceChanged() = 0;
35
36  // Called when the user selects the "add account" menu item.
37  virtual void AddAccount() = 0;
38
39  // Called when the account chooser UI needs to be updated.
40  virtual void UpdateAccountChooserView() = 0;
41};
42
43// A menu model for the account chooser. This allows users to switch between
44// Online Wallet accounts and local Autofill data.
45// Terminology:
46// - "Active Wallet account": the account used for communications with the
47// Online Wallet service. There may be multiple signed-in accounts, but at any
48// point of time at most one of is active.
49class AccountChooserModel : public ui::SimpleMenuModel,
50                            public ui::SimpleMenuModel::Delegate {
51 public:
52  AccountChooserModel(AccountChooserModelDelegate* delegate,
53                      Profile* profile,
54                      const AutofillMetrics& metric_logger);
55  virtual ~AccountChooserModel();
56
57  // ui::SimpleMenuModel implementation.
58  virtual void MenuWillShow() OVERRIDE;
59
60  // ui::SimpleMenuModel::Delegate implementation.
61  virtual bool IsCommandIdChecked(int command_id) const OVERRIDE;
62  virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE;
63  virtual bool GetAcceleratorForCommandId(
64      int command_id,
65      ui::Accelerator* accelerator) OVERRIDE;
66  virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE;
67  virtual void MenuWillShow(ui::SimpleMenuModel* source) OVERRIDE;
68
69  // Sets the selection to the given wallet account.
70  void SelectWalletAccount(size_t user_index);
71
72  // Sets the selection to the local Autofill data.
73  void SelectUseAutofill();
74
75  // Returns true if there are any accounts for the user to choose from.
76  bool HasAccountsToChoose() const;
77
78  // Sets the name of the accounts used to communicate with the Online Wallet
79  // and switches |checked_item_| to point to the menu item for the account.
80  void SetWalletAccounts(const std::vector<std::string>& accounts,
81                         size_t active_index);
82
83  // Clears the set of accounts used to communicate with the Online Wallet.
84  // Any Wallet error automatically clears the currently active account name.
85  void ClearWalletAccounts();
86
87  // Returns the name of the currently active account, or an empty string.
88  // The currently active account may not be the checked item in the menu, but
89  // will be the most recently checked wallet account item.
90  base::string16 GetActiveWalletAccountName() const;
91
92  // Returns the index of the account that is currently active.
93  size_t GetActiveWalletAccountIndex() const;
94
95  // Disables all Wallet accounts and switches to the local Autofill data.
96  // Should be called when the Wallet server returns an error.
97  void SetHadWalletError();
98
99  // Switches the dialog to the local Autofill data.
100  // Should be called when the Online Wallet sign-in attempt has failed.
101  void SetHadWalletSigninError();
102
103  // Returns true if the selected account is an Online Wallet account.
104  bool WalletIsSelected() const;
105
106 protected:
107  // Command IDs of the items in this menu; protected for the tests.
108  // The menu item for adding a new wallet account (for multilogin).
109  static const int kWalletAddAccountId;
110  // kAutofillItemId is "Pay without the Wallet" (local autofill data).
111  static const int kAutofillItemId;
112  // Wallet account menu item IDs are this value + the account index.
113  static const int kWalletAccountsStartId;
114
115 private:
116  // Reconstructs the set of menu items.
117  void ReconstructMenuItems();
118
119  AccountChooserModelDelegate* delegate_;
120
121  // The command id of the currently selected item.
122  int checked_item_;
123
124  // Whether there has been a Wallet error.
125  bool had_wallet_error_;
126
127  // For logging UMA metrics.
128  const AutofillMetrics& metric_logger_;
129
130  // The names (emails) of the signed in accounts, gotten from the
131  // Online Wallet service.
132  std::vector<std::string> wallet_accounts_;
133
134  DISALLOW_COPY_AND_ASSIGN(AccountChooserModel);
135};
136
137}  // namespace autofill
138
139#endif  // CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_
140