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..d5869160d14b702c133acdfcbc2bcda8a5424058 |
--- /dev/null |
+++ b/chrome/browser/ui/autofill/account_chooser_model.cc |
@@ -0,0 +1,163 @@ |
+// Copyright (c) 2012 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::kSignedInWalletItemId = 0; |
+const int AccountChooserModel::kAutofillItemId = 1; |
+const int AccountChooserModel::kFirstAccountItemId = 2; |
+ |
+AccountChooserModelDelegate::~AccountChooserModelDelegate() {} |
+ |
+AccountChooserModel::AccountChooserModel( |
+ AccountChooserModelDelegate* delegate, |
+ PrefService* prefs) |
+ : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), |
+ account_delegate_(delegate), |
+ prefs_(prefs), |
+ checked_item_(kSignedInWalletItemId), |
+ had_wallet_error_(false) { |
+ pref_change_registrar_.Init(prefs); |
+ pref_change_registrar_.Add( |
+ prefs::kAutofillDialogPayWithoutWallet, |
+ base::Bind(&AccountChooserModel::PrefChanged, base::Unretained(this))); |
+ UpdateCheckmarkFromPref(); |
+ ReconstructMenuItems(); |
+} |
+ |
+AccountChooserModel::~AccountChooserModel() { |
+} |
+ |
+void AccountChooserModel::ForceSelectWalletAccount() { |
+ ExecuteCommand(kSignedInWalletItemId, 0); |
+} |
+ |
+bool AccountChooserModel::HasAccountsToChoose() const { |
+ return !had_wallet_error_ && |
+ !(current_username_.empty() && available_accounts_.empty()); |
+} |
+ |
+void AccountChooserModel::SetAvailableAccounts( |
+ const std::vector<std::string>& accounts) { |
+ available_accounts_ = accounts; |
+ ReconstructMenuItems(); |
+} |
+ |
+void AccountChooserModel::SetCurrentlySignedInAccount( |
+ const std::string& account) { |
+ current_username_ = account; |
+ ReconstructMenuItems(); |
+} |
+ |
+std::string AccountChooserModel::GetCurrentlySignedInAccount() const { |
+ return current_username_; |
+} |
+ |
+void AccountChooserModel::ResetCurrentlySignedInAccount() { |
+ 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; |
+ checked_item_ = kAutofillItemId; |
+ ResetCurrentlySignedInAccount(); |
+ account_delegate_->AccountChoiceChanged(); |
+} |
+ |
+void AccountChooserModel::SetHadWalletSigninError() { |
+ checked_item_ = kAutofillItemId; |
+ ResetCurrentlySignedInAccount(); |
+ account_delegate_->AccountChoiceChanged(); |
+} |
+ |
+bool AccountChooserModel::WalletIsSelected() const { |
+ return checked_item_ != kAutofillItemId; |
+} |
+ |
+bool AccountChooserModel::IsCurrentlySignedInAccountSelected() const { |
+ return checked_item_ == kSignedInWalletItemId; |
+} |
+ |
+void AccountChooserModel::PrefChanged(const std::string& pref) { |
+ DCHECK(pref == prefs::kAutofillDialogPayWithoutWallet); |
+ UpdateCheckmarkFromPref(); |
+ account_delegate_->AccountChoiceChanged(); |
+} |
+ |
+void AccountChooserModel::UpdateCheckmarkFromPref() { |
+ if (prefs_->GetBoolean(prefs::kAutofillDialogPayWithoutWallet)) |
Evan Stade
2013/04/01 23:54:34
nit: prefer ternary
aruslan
2013/04/10 17:40:31
Done.
|
+ checked_item_ = kAutofillItemId; |
+ else |
+ checked_item_ = kSignedInWalletItemId; |
+} |
+ |
+void AccountChooserModel::ReconstructMenuItems() { |
+ // If menu has to be reconstructed, either the currently sign-in account |
+ // should be selected, or the autofill data should be used. |
+ DCHECK_GE(kAutofillItemId, checked_item_); |
+ Clear(); |
+ const gfx::Image& wallet_icon = |
+ ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON); |
+ |
+ if (!current_username_.empty()) { |
+ AddCheckItem(kSignedInWalletItemId, ASCIIToUTF16(current_username_)); |
Evan Stade
2013/04/01 23:54:34
you want UTF8ToUTF16.
Also, this conversion shoul
|
+ SetIcon(GetIndexOfCommandId(kSignedInWalletItemId), wallet_icon); |
+ } |
+ |
+ for (size_t i = 0; i < available_accounts_.size(); ++i) { |
+ if (available_accounts_[i] != current_username_) { |
+ AddCheckItem(kFirstAccountItemId + i, |
+ ASCIIToUTF16(available_accounts_[i])); |
+ SetIcon(GetIndexOfCommandId(kFirstAccountItemId + i), wallet_icon); |
+ } |
+ } |
+ |
+ AddCheckItemWithStringId(kAutofillItemId, |
+ IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET); |
+} |
+ |
+} // autofill |