account_chooser_model.h revision 58537e28ecd584eab876aee8be7156509866d23a
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector> 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/strings/string16.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "components/autofill/core/browser/autofill_manager_delegate.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/base/models/simple_menu_model.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AutofillMetrics; 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class PrefService; 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace autofill { 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A delegate interface to allow the AccountChooserModel to inform its owner 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// of changes. 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AccountChooserModelDelegate { 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~AccountChooserModelDelegate(); 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called when the active account has changed. 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void AccountChoiceChanged() = 0; 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Called when the account chooser UI needs to be updated. 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void UpdateAccountChooserView() = 0; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A menu model for the account chooser. This allows users to switch between 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Online Wallet accounts and local Autofill data. 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Terminology: 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// - "Active Wallet account": the account used for communications with the 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Online Wallet service. There may be multiple signed-in accounts, but at any 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// point of time at most one of is active. 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class AccountChooserModel : public ui::SimpleMenuModel, 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public ui::SimpleMenuModel::Delegate { 42cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) public: 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) AccountChooserModel(AccountChooserModelDelegate* delegate, 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PrefService* prefs, 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const AutofillMetrics& metric_logger); 46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles) virtual ~AccountChooserModel(); 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // ui::SimpleMenuModel::Delegate implementation. 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool IsCommandIdChecked(int command_id) const OVERRIDE; 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool IsCommandIdEnabled(int command_id) const OVERRIDE; 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual bool GetAcceleratorForCommandId( 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) int command_id, 532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ui::Accelerator* accelerator) OVERRIDE; 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void ExecuteCommand(int command_id, int event_flags) OVERRIDE; 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Sets the selection to the currently active Online Wallet account. 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Should be called if the user attempts to sign into the Online Wallet 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // (e.g. when the user clicks the "Sign-in" link). 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void SelectActiveWalletAccount(); 60 61 // Sets the selection to the local Autofill data. 62 void SelectUseAutofill(); 63 64 // Returns true if there are any accounts for the user to choose from. 65 bool HasAccountsToChoose() const; 66 67 // Sets the name of the account used to communicate with the Online Wallet. 68 void SetActiveWalletAccountName(const string16& account); 69 70 // Clears the name of the account used to communicate with the Online Wallet. 71 // Any Wallet error automatically clears the currently active account name. 72 void ClearActiveWalletAccountName(); 73 74 // Returns the name of the currently active account, or an empty string. 75 const string16& active_wallet_account_name() const { 76 return active_wallet_account_name_; 77 } 78 79 // Disables all Wallet accounts and switches to the local Autofill data. 80 // Should be called when the Wallet server returns an error. 81 void SetHadWalletError(); 82 83 // Switches the dialog to the local Autofill data. 84 // Should be called when the Online Wallet sign-in attempt has failed. 85 void SetHadWalletSigninError(); 86 87 // Returns true if the selected account is an Online Wallet account. 88 bool WalletIsSelected() const; 89 90 // Returns true if the current selection matches the currently active 91 // Wallet account. 92 bool IsActiveWalletAccountSelected() const; 93 94 // Returns the command id of the current selection. 95 int checked_item() const { return checked_item_; } 96 97 protected: 98 // Command IDs of the items in this menu; protected for the tests. 99 // kActiveWalletItemId is the currently active account. 100 // kAutofillItemId is "Pay without the Wallet" (local autofill data). 101 // In the future, kFirstAdditionalItemId will be added as the first id 102 // for additional accounts. 103 static const int kActiveWalletItemId; 104 static const int kAutofillItemId; 105 106 private: 107 // Reconstructs the set of menu items. 108 void ReconstructMenuItems(); 109 110 AccountChooserModelDelegate* delegate_; 111 112 // The command id of the currently selected item. 113 int checked_item_; 114 115 // Whether there has been a Wallet error. 116 bool had_wallet_error_; 117 118 // For logging UMA metrics. 119 const AutofillMetrics& metric_logger_; 120 121 // The name (email) of the account currently used in communications with the 122 // Online Wallet service. 123 string16 active_wallet_account_name_; 124 125 DISALLOW_COPY_AND_ASSIGN(AccountChooserModel); 126}; 127 128} // namespace autofill 129 130#endif // CHROME_BROWSER_UI_AUTOFILL_ACCOUNT_CHOOSER_MODEL_H_ 131