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..a5a3c1a8a2ee61a6a3ffb84cfe57f5e76c065771 |
--- /dev/null |
+++ b/chrome/browser/ui/autofill/account_chooser_model.cc |
@@ -0,0 +1,133 @@ |
+// 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; |
+ |
+AccountChooserModelDelegate::~AccountChooserModelDelegate() {} |
+ |
+AccountChooserModel::AccountChooserModel( |
+ AccountChooserModelDelegate* delegate, |
+ PrefService* prefs) |
+ : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
+ delegate_(delegate), |
+ checked_item_(prefs->GetBoolean(prefs::kAutofillDialogPayWithoutWallet) ? |
+ kAutofillItemId : kActiveWalletItemId), |
+ had_wallet_error_(false) { |
+ ReconstructMenuItems(); |
+} |
+ |
+AccountChooserModel::~AccountChooserModel() { |
+} |
+ |
+void AccountChooserModel::SelectActiveWalletAccount() { |
+ ExecuteCommand(kActiveWalletItemId, 0); |
+} |
+ |
+void AccountChooserModel::SelectAutofillData() { |
+ ExecuteCommand(kAutofillItemId, 0); |
+} |
+ |
+bool AccountChooserModel::HasAccountsToChoose() const { |
+ return !active_wallet_account_name_.empty(); |
+} |
+ |
+void AccountChooserModel::SetActiveWalletAccountName( |
+ const string16& account) { |
+ active_wallet_account_name_ = account; |
+ ReconstructMenuItems(); |
+ delegate_->UpdateAccountChooserView(); |
+} |
+ |
+void AccountChooserModel::ClearActiveWalletAccountName() { |
+ active_wallet_account_name_.clear(); |
+ ReconstructMenuItems(); |
+ delegate_->UpdateAccountChooserView(); |
+} |
+ |
+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; |
+ ReconstructMenuItems(); |
+ 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() { |
+ Clear(); |
+ const gfx::Image& wallet_icon = |
+ ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON); |
+ |
+ if (!active_wallet_account_name_.empty()) { |
+ AddCheckItem(kActiveWalletItemId, active_wallet_account_name_); |
+ SetIcon(GetIndexOfCommandId(kActiveWalletItemId), wallet_icon); |
+ } else if (checked_item_ == kActiveWalletItemId) { |
+ // A selected active Wallet account with an empty account name means |
+ // that the sign-in attempt is in progress. |
+ // A throbber should be shown until the Wallet account name is set. |
Evan Stade
2013/04/12 17:06:36
is there a bug filed specifically for this?
aruslan
2013/04/12 20:43:30
Done.
|
+ AddCheckItem(kActiveWalletItemId, |
+ l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET)); |
+ } |
+ |
+ AddCheckItemWithStringId(kAutofillItemId, |
+ IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET); |
+} |
+ |
+} // namespace autofill |