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_client.h"
15 #include "ui/base/models/simple_menu_model.h"
21 class AccountChooserModel
;
23 // A delegate interface to allow the AccountChooserModel to inform its owner
25 class AccountChooserModelDelegate
{
27 virtual ~AccountChooserModelDelegate();
29 // Called when the active account has changed.
30 virtual void AccountChoiceChanged() = 0;
32 // Called when the user selects the "add account" menu item.
33 virtual void AddAccount() = 0;
35 // Called when the account chooser UI needs to be updated.
36 virtual void UpdateAccountChooserView() = 0;
39 // A menu model for the account chooser. This allows users to switch between
40 // Online Wallet accounts and local Autofill data.
42 // - "Active Wallet account": the account used for communications with the
43 // Online Wallet service. There may be multiple signed-in accounts, but at any
44 // point of time at most one of is active.
45 class AccountChooserModel
: public ui::SimpleMenuModel
,
46 public ui::SimpleMenuModel::Delegate
{
48 // |disable_wallet| overrides the starting value, which is normally set by a
50 AccountChooserModel(AccountChooserModelDelegate
* delegate
,
53 ~AccountChooserModel() override
;
55 // ui::SimpleMenuModel::Delegate implementation.
56 bool IsCommandIdChecked(int command_id
) const override
;
57 bool IsCommandIdEnabled(int command_id
) const override
;
58 bool GetAcceleratorForCommandId(int command_id
,
59 ui::Accelerator
* accelerator
) override
;
60 void ExecuteCommand(int command_id
, int event_flags
) override
;
62 // Sets the selection to the given wallet account.
63 void SelectWalletAccount(size_t user_index
);
65 // Sets the selection to the local Autofill data.
66 void SelectUseAutofill();
68 // Returns true if there are any accounts for the user to choose from.
69 bool HasAccountsToChoose() const;
71 // Sets the name of the accounts used to communicate with the Online Wallet
72 // and switches |checked_item_| to point to the menu item for the account.
73 void SetWalletAccounts(const std::vector
<std::string
>& accounts
,
76 // Clears the set of accounts used to communicate with the Online Wallet.
77 // Any Wallet error automatically clears the currently active account name.
78 void ClearWalletAccounts();
80 // Returns the name of the currently active account, or an empty string.
81 // The currently active account may not be the checked item in the menu, but
82 // will be the most recently checked wallet account item.
83 base::string16
GetActiveWalletAccountName() const;
85 // Returns the index of the account that is currently active.
86 size_t GetActiveWalletAccountIndex() const;
88 // Disables all Wallet accounts and switches to the local Autofill data.
89 // Should be called when the Wallet server returns an error.
90 void SetHadWalletError();
92 // Switches the dialog to the local Autofill data.
93 // Should be called when the Online Wallet sign-in attempt has failed.
94 void SetHadWalletSigninError();
96 // Returns true if the selected account is an Online Wallet account.
97 bool WalletIsSelected() const;
100 // Command IDs of the items in this menu; protected for the tests.
101 // The menu item for adding a new wallet account (for multilogin).
102 static const int kWalletAddAccountId
;
103 // kAutofillItemId is "Pay without the Wallet" (local autofill data).
104 static const int kAutofillItemId
;
105 // Wallet account menu item IDs are this value + the account index.
106 static const int kWalletAccountsStartId
;
109 // Reconstructs the set of menu items.
110 void ReconstructMenuItems();
112 // Switches to the account option represented by |command_id|.
113 void DoAccountSwitch(int command_id
);
115 AccountChooserModelDelegate
* delegate_
;
117 // The command id of the currently selected item.
120 // Whether there has been a Wallet error.
121 bool had_wallet_error_
;
123 // The names (emails) of the signed in accounts, gotten from the
124 // Online Wallet service.
125 std::vector
<std::string
> wallet_accounts_
;
127 DISALLOW_COPY_AND_ASSIGN(AccountChooserModel
);
130 } // namespace autofill
132 #endif // CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_