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.
5 #ifndef CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_
6 #define CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_
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"
17 class AutofillMetrics
;
22 class AccountChooserModel
;
24 // A delegate interface to allow the AccountChooserModel to inform its owner
26 class AccountChooserModelDelegate
{
28 virtual ~AccountChooserModelDelegate();
30 // Called right before the account chooser is shown.
31 virtual void AccountChooserWillShow() = 0;
33 // Called when the active account has changed.
34 virtual void AccountChoiceChanged() = 0;
36 // Called when the user selects the "add account" menu item.
37 virtual void AddAccount() = 0;
39 // Called when the account chooser UI needs to be updated.
40 virtual void UpdateAccountChooserView() = 0;
43 // A menu model for the account chooser. This allows users to switch between
44 // Online Wallet accounts and local Autofill data.
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.
49 class AccountChooserModel
: public ui::SimpleMenuModel
,
50 public ui::SimpleMenuModel::Delegate
{
52 // |disable_wallet| overrides the starting value, which is normally set by a
54 AccountChooserModel(AccountChooserModelDelegate
* delegate
,
57 const AutofillMetrics
& metric_logger
);
58 virtual ~AccountChooserModel();
60 // ui::SimpleMenuModel implementation.
61 virtual void MenuWillShow() OVERRIDE
;
63 // ui::SimpleMenuModel::Delegate implementation.
64 virtual bool IsCommandIdChecked(int command_id
) const OVERRIDE
;
65 virtual bool IsCommandIdEnabled(int command_id
) const OVERRIDE
;
66 virtual bool GetAcceleratorForCommandId(
68 ui::Accelerator
* accelerator
) OVERRIDE
;
69 virtual void ExecuteCommand(int command_id
, int event_flags
) OVERRIDE
;
70 virtual void MenuWillShow(ui::SimpleMenuModel
* source
) OVERRIDE
;
72 // Sets the selection to the given wallet account.
73 void SelectWalletAccount(size_t user_index
);
75 // Sets the selection to the local Autofill data.
76 void SelectUseAutofill();
78 // Returns true if there are any accounts for the user to choose from.
79 bool HasAccountsToChoose() const;
81 // Sets the name of the accounts used to communicate with the Online Wallet
82 // and switches |checked_item_| to point to the menu item for the account.
83 void SetWalletAccounts(const std::vector
<std::string
>& accounts
,
86 // Clears the set of accounts used to communicate with the Online Wallet.
87 // Any Wallet error automatically clears the currently active account name.
88 void ClearWalletAccounts();
90 // Returns the name of the currently active account, or an empty string.
91 // The currently active account may not be the checked item in the menu, but
92 // will be the most recently checked wallet account item.
93 base::string16
GetActiveWalletAccountName() const;
95 // Returns the index of the account that is currently active.
96 size_t GetActiveWalletAccountIndex() const;
98 // Disables all Wallet accounts and switches to the local Autofill data.
99 // Should be called when the Wallet server returns an error.
100 void SetHadWalletError();
102 // Switches the dialog to the local Autofill data.
103 // Should be called when the Online Wallet sign-in attempt has failed.
104 void SetHadWalletSigninError();
106 // Returns true if the selected account is an Online Wallet account.
107 bool WalletIsSelected() const;
110 // Command IDs of the items in this menu; protected for the tests.
111 // The menu item for adding a new wallet account (for multilogin).
112 static const int kWalletAddAccountId
;
113 // kAutofillItemId is "Pay without the Wallet" (local autofill data).
114 static const int kAutofillItemId
;
115 // Wallet account menu item IDs are this value + the account index.
116 static const int kWalletAccountsStartId
;
119 // Reconstructs the set of menu items.
120 void ReconstructMenuItems();
122 AccountChooserModelDelegate
* delegate_
;
124 // The command id of the currently selected item.
127 // Whether there has been a Wallet error.
128 bool had_wallet_error_
;
130 // For logging UMA metrics.
131 const AutofillMetrics
& metric_logger_
;
133 // The names (emails) of the signed in accounts, gotten from the
134 // Online Wallet service.
135 std::vector
<std::string
> wallet_accounts_
;
137 DISALLOW_COPY_AND_ASSIGN(AccountChooserModel
);
140 } // namespace autofill
142 #endif // CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_