Index: chrome/browser/ui/autofill/account_chooser_model.cc |
diff --git a/chrome/browser/ui/autofill/account_chooser_model.cc b/chrome/browser/ui/autofill/account_chooser_model.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..2e8f4583749dbbfe44f2df76d38d14db6fb2794e |
--- /dev/null |
+++ b/chrome/browser/ui/autofill/account_chooser_model.cc |
@@ -0,0 +1,158 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "chrome/browser/ui/autofill/account_chooser_model.h" |
+ |
+#include "base/bind.h" |
+#include "base/prefs/pref_service.h" |
+#include "base/stringprintf.h" |
+#include "base/strings/string_number_conversions.h" |
+#include "base/time.h" |
+#include "base/utf_string_conversions.h" |
+#include "chrome/common/pref_names.h" |
+#include "components/autofill/browser/autofill_country.h" |
+#include "grit/generated_resources.h" |
+#include "grit/theme_resources.h" |
+#include "ui/base/l10n/l10n_util.h" |
+#include "ui/base/resource/resource_bundle.h" |
+ |
+namespace autofill { |
+ |
+const int AccountChooserModel::kActiveWalletItemId = 0; |
+const int AccountChooserModel::kAutofillItemId = 1; |
+const int AccountChooserModel::kFirstAdditionalItemId = 2; |
+ |
+AccountChooserModelDelegate::~AccountChooserModelDelegate() {} |
+ |
+AccountChooserModel::AccountChooserModel( |
+ AccountChooserModelDelegate* delegate, |
+ PrefService* prefs) |
+ : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
+ account_delegate_(delegate), |
+ checked_item_(prefs->GetBoolean(prefs::kAutofillDialogPayWithoutWallet) ? |
+ kAutofillItemId : kActiveWalletItemId), |
+ had_wallet_error_(false) { |
+ ReconstructMenuItems(); |
+} |
+ |
+AccountChooserModel::~AccountChooserModel() { |
+} |
+ |
+void AccountChooserModel::ForceSelectWalletAccount() { |
+ ExecuteCommand(kActiveWalletItemId, 0); |
+} |
+ |
+bool AccountChooserModel::HasAccountsToChoose() const { |
+ return !had_wallet_error_ && |
+ !(current_username_.empty() && additional_accounts_.empty()); |
Evan Stade
2013/04/11 19:27:45
are you sure we want to show the sign in link when
aruslan
2013/04/12 00:46:02
Done.
|
+} |
+ |
+void AccountChooserModel::SetAdditionalAccounts( |
+ const std::vector<string16>& accounts) { |
+ additional_accounts_ = accounts; |
+ ReconstructMenuItems(); |
+ account_delegate_->UpdateAccountChooserView(); |
+} |
+ |
+std::vector<string16> AccountChooserModel::GetAllAccounts() const { |
+ std::vector<string16> accounts(additional_accounts_); |
+ if (!current_username_.empty() && |
+ !std::find(accounts.begin(), accounts.end(), current_username_)) { |
+ accounts.push_back(current_username_); |
+ } |
+ return accounts; |
+} |
+ |
+void AccountChooserModel::SetActiveWalletAccountName( |
+ const string16& account) { |
+ current_username_ = account; |
+ ReconstructMenuItems(); |
+ account_delegate_->UpdateAccountChooserView(); |
+} |
+ |
+string16 AccountChooserModel::GetActiveWalletAccountName() const { |
+ return current_username_; |
+} |
+ |
+void AccountChooserModel::ClearActiveWalletAccountName() { |
+ current_username_.clear(); |
+ ReconstructMenuItems(); |
+} |
+ |
+bool AccountChooserModel::IsCommandIdChecked(int command_id) const { |
+ return command_id == checked_item_; |
+} |
+ |
+bool AccountChooserModel::IsCommandIdEnabled(int command_id) const { |
+ // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts. |
+ if (command_id != kAutofillItemId && had_wallet_error_) |
+ return false; |
+ |
+ return true; |
+} |
+ |
+bool AccountChooserModel::GetAcceleratorForCommandId( |
+ int command_id, |
+ ui::Accelerator* accelerator) { |
+ return false; |
+} |
+ |
+void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) { |
+ if (checked_item_ == command_id) |
+ return; |
+ |
+ checked_item_ = command_id; |
+ account_delegate_->AccountChoiceChanged(); |
+} |
+ |
+void AccountChooserModel::SetHadWalletError() { |
+ // Any non-sign-in error disables all Wallet accounts. |
+ had_wallet_error_ = true; |
+ ClearActiveWalletAccountName(); |
+ ExecuteCommand(kAutofillItemId, 0); |
+} |
+ |
+void AccountChooserModel::SetHadWalletSigninError() { |
+ ClearActiveWalletAccountName(); |
+ ExecuteCommand(kAutofillItemId, 0); |
+} |
+ |
+bool AccountChooserModel::WalletIsSelected() const { |
+ return checked_item_ != kAutofillItemId; |
+} |
+ |
+bool AccountChooserModel::IsActiveWalletAccountSelected() const { |
+ return checked_item_ == kActiveWalletItemId; |
+} |
+ |
+void AccountChooserModel::ReconstructMenuItems() { |
+ // If menu has to be reconstructed, either the currently active Wallet |
+ // account or the autofill data should be selected. |
+ DCHECK_GE(kAutofillItemId, checked_item_); |
+ Clear(); |
+ const gfx::Image& wallet_icon = |
+ ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON); |
+ |
+ if (!current_username_.empty()) { |
+ AddCheckItem(kActiveWalletItemId, current_username_); |
+ SetIcon(GetIndexOfCommandId(kActiveWalletItemId), wallet_icon); |
+ } else { |
+ // This will be empty until SetActiveWalletAccountName() is called. |
+ // A throbber should be shown until the user name is set. |
+ AddCheckItem(kActiveWalletItemId, |
+ l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET)); |
+ } |
+ |
+ for (size_t i = 0; i < additional_accounts_.size(); ++i) { |
+ if (additional_accounts_[i] != current_username_) { |
+ AddCheckItem(kFirstAdditionalItemId + i, additional_accounts_[i]); |
+ SetIcon(GetIndexOfCommandId(kFirstAdditionalItemId + i), wallet_icon); |
+ } |
+ } |
+ |
+ AddCheckItemWithStringId(kAutofillItemId, |
+ IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET); |
+} |
+ |
+} // autofill |