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 when the active account has changed.
31 virtual void AccountChoiceChanged() = 0;
33 // Called when the user selects the "add account" menu item.
34 virtual void AddAccount() = 0;
36 // Called when the account chooser UI needs to be updated.
37 virtual void UpdateAccountChooserView() = 0;
40 // A menu model for the account chooser. This allows users to switch between
41 // Online Wallet accounts and local Autofill data.
43 // - "Active Wallet account": the account used for communications with the
44 // Online Wallet service. There may be multiple signed-in accounts, but at any
45 // point of time at most one of is active.
46 class AccountChooserModel
: public ui::SimpleMenuModel
,
47 public ui::SimpleMenuModel::Delegate
{
49 // |disable_wallet| overrides the starting value, which is normally set by a
51 AccountChooserModel(AccountChooserModelDelegate
* delegate
,
54 const AutofillMetrics
& metric_logger
);
55 virtual ~AccountChooserModel();
57 // ui::SimpleMenuModel::Delegate implementation.
58 virtual bool IsCommandIdChecked(int command_id
) const OVERRIDE
;
59 virtual bool IsCommandIdEnabled(int command_id
) const OVERRIDE
;
60 virtual bool GetAcceleratorForCommandId(
62 ui::Accelerator
* accelerator
) OVERRIDE
;
63 virtual void ExecuteCommand(int command_id
, int event_flags
) OVERRIDE
;
65 // Sets the selection to the given wallet account.
66 void SelectWalletAccount(size_t user_index
);
68 // Sets the selection to the local Autofill data.
69 void SelectUseAutofill();
71 // Returns true if there are any accounts for the user to choose from.
72 bool HasAccountsToChoose() const;
74 // Sets the name of the accounts used to communicate with the Online Wallet
75 // and switches |checked_item_| to point to the menu item for the account.
76 void SetWalletAccounts(const std::vector
<std::string
>& accounts
,
79 // Clears the set of accounts used to communicate with the Online Wallet.
80 // Any Wallet error automatically clears the currently active account name.
81 void ClearWalletAccounts();
83 // Returns the name of the currently active account, or an empty string.
84 // The currently active account may not be the checked item in the menu, but
85 // will be the most recently checked wallet account item.
86 base::string16
GetActiveWalletAccountName() const;
88 // Returns the index of the account that is currently active.
89 size_t GetActiveWalletAccountIndex() const;
91 // Disables all Wallet accounts and switches to the local Autofill data.
92 // Should be called when the Wallet server returns an error.
93 void SetHadWalletError();
95 // Switches the dialog to the local Autofill data.
96 // Should be called when the Online Wallet sign-in attempt has failed.
97 void SetHadWalletSigninError();
99 // Returns true if the selected account is an Online Wallet account.
100 bool WalletIsSelected() const;
103 // Command IDs of the items in this menu; protected for the tests.
104 // The menu item for adding a new wallet account (for multilogin).
105 static const int kWalletAddAccountId
;
106 // kAutofillItemId is "Pay without the Wallet" (local autofill data).
107 static const int kAutofillItemId
;
108 // Wallet account menu item IDs are this value + the account index.
109 static const int kWalletAccountsStartId
;
112 // Reconstructs the set of menu items.
113 void ReconstructMenuItems();
115 // Switches to the account option represented by |command_id|.
116 void DoAccountSwitch(int command_id
);
118 AccountChooserModelDelegate
* delegate_
;
120 // The command id of the currently selected item.
123 // Whether there has been a Wallet error.
124 bool had_wallet_error_
;
126 // For logging UMA metrics.
127 const AutofillMetrics
& metric_logger_
;
129 // The names (emails) of the signed in accounts, gotten from the
130 // Online Wallet service.
131 std::vector
<std::string
> wallet_accounts_
;
133 DISALLOW_COPY_AND_ASSIGN(AccountChooserModel
);
136 } // namespace autofill
138 #endif // CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_