OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2012 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. | |
4 | |
5 #include "chrome/browser/ui/autofill/account_chooser_model.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/prefs/pref_service.h" | |
9 #include "base/stringprintf.h" | |
10 #include "base/strings/string_number_conversions.h" | |
11 #include "base/time.h" | |
12 #include "base/utf_string_conversions.h" | |
13 #include "chrome/common/pref_names.h" | |
14 #include "components/autofill/browser/autofill_country.h" | |
15 #include "grit/generated_resources.h" | |
16 #include "grit/theme_resources.h" | |
17 #include "ui/base/l10n/l10n_util.h" | |
18 #include "ui/base/resource/resource_bundle.h" | |
19 | |
20 namespace autofill { | |
21 | |
22 const int AccountChooserModel::kSignedInWalletItemId = 0; | |
23 const int AccountChooserModel::kAutofillItemId = 1; | |
24 const int AccountChooserModel::kFirstAccountItemId = 2; | |
25 | |
26 AccountChooserModelDelegate::~AccountChooserModelDelegate() {} | |
27 | |
28 AccountChooserModel::AccountChooserModel( | |
29 AccountChooserModelDelegate* delegate, | |
30 PrefService* prefs) | |
31 : ALLOW_THIS_IN_INITIALIZER_LIST(ui::SimpleMenuModel(this)), | |
32 account_delegate_(delegate), | |
33 prefs_(prefs), | |
34 checked_item_(kSignedInWalletItemId), | |
35 had_wallet_error_(false) { | |
36 pref_change_registrar_.Init(prefs); | |
37 pref_change_registrar_.Add( | |
38 prefs::kAutofillDialogPayWithoutWallet, | |
39 base::Bind(&AccountChooserModel::PrefChanged, base::Unretained(this))); | |
40 UpdateCheckmarkFromPref(); | |
41 ReconstructMenuItems(); | |
42 } | |
43 | |
44 AccountChooserModel::~AccountChooserModel() { | |
45 } | |
46 | |
47 void AccountChooserModel::ForceSelectWalletAccount() { | |
48 ExecuteCommand(kSignedInWalletItemId, 0); | |
49 } | |
50 | |
51 bool AccountChooserModel::HasAccountsToChoose() const { | |
52 return !had_wallet_error_ && | |
53 !(current_username_.empty() && available_accounts_.empty()); | |
54 } | |
55 | |
56 void AccountChooserModel::SetAvailableAccounts( | |
57 const std::vector<std::string>& accounts) { | |
58 available_accounts_ = accounts; | |
59 ReconstructMenuItems(); | |
60 } | |
61 | |
62 void AccountChooserModel::SetCurrentlySignedInAccount( | |
63 const std::string& account) { | |
64 current_username_ = account; | |
65 ReconstructMenuItems(); | |
66 } | |
67 | |
68 std::string AccountChooserModel::GetCurrentlySignedInAccount() const { | |
69 return current_username_; | |
70 } | |
71 | |
72 void AccountChooserModel::ResetCurrentlySignedInAccount() { | |
73 current_username_.clear(); | |
74 ReconstructMenuItems(); | |
75 } | |
76 | |
77 bool AccountChooserModel::IsCommandIdChecked(int command_id) const { | |
78 return command_id == checked_item_; | |
79 } | |
80 | |
81 bool AccountChooserModel::IsCommandIdEnabled(int command_id) const { | |
82 // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts. | |
83 if (command_id != kAutofillItemId && had_wallet_error_) | |
84 return false; | |
85 | |
86 return true; | |
87 } | |
88 | |
89 bool AccountChooserModel::GetAcceleratorForCommandId( | |
90 int command_id, | |
91 ui::Accelerator* accelerator) { | |
92 return false; | |
93 } | |
94 | |
95 void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) { | |
96 if (checked_item_ == command_id) | |
97 return; | |
98 | |
99 checked_item_ = command_id; | |
100 account_delegate_->AccountChoiceChanged(); | |
101 } | |
102 | |
103 void AccountChooserModel::SetHadWalletError() { | |
104 // Any non-sign-in error disables all Wallet accounts. | |
105 had_wallet_error_ = true; | |
106 checked_item_ = kAutofillItemId; | |
107 ResetCurrentlySignedInAccount(); | |
108 account_delegate_->AccountChoiceChanged(); | |
109 } | |
110 | |
111 void AccountChooserModel::SetHadWalletSigninError() { | |
112 checked_item_ = kAutofillItemId; | |
113 ResetCurrentlySignedInAccount(); | |
114 account_delegate_->AccountChoiceChanged(); | |
115 } | |
116 | |
117 bool AccountChooserModel::WalletIsSelected() const { | |
118 return checked_item_ != kAutofillItemId; | |
119 } | |
120 | |
121 bool AccountChooserModel::IsCurrentlySignedInAccountSelected() const { | |
122 return checked_item_ == kSignedInWalletItemId; | |
123 } | |
124 | |
125 void AccountChooserModel::PrefChanged(const std::string& pref) { | |
126 DCHECK(pref == prefs::kAutofillDialogPayWithoutWallet); | |
127 UpdateCheckmarkFromPref(); | |
128 account_delegate_->AccountChoiceChanged(); | |
129 } | |
130 | |
131 void AccountChooserModel::UpdateCheckmarkFromPref() { | |
132 if (prefs_->GetBoolean(prefs::kAutofillDialogPayWithoutWallet)) | |
Evan Stade
2013/04/01 23:54:34
nit: prefer ternary
aruslan
2013/04/10 17:40:31
Done.
| |
133 checked_item_ = kAutofillItemId; | |
134 else | |
135 checked_item_ = kSignedInWalletItemId; | |
136 } | |
137 | |
138 void AccountChooserModel::ReconstructMenuItems() { | |
139 // If menu has to be reconstructed, either the currently sign-in account | |
140 // should be selected, or the autofill data should be used. | |
141 DCHECK_GE(kAutofillItemId, checked_item_); | |
142 Clear(); | |
143 const gfx::Image& wallet_icon = | |
144 ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON); | |
145 | |
146 if (!current_username_.empty()) { | |
147 AddCheckItem(kSignedInWalletItemId, ASCIIToUTF16(current_username_)); | |
Evan Stade
2013/04/01 23:54:34
you want UTF8ToUTF16.
Also, this conversion shoul
| |
148 SetIcon(GetIndexOfCommandId(kSignedInWalletItemId), wallet_icon); | |
149 } | |
150 | |
151 for (size_t i = 0; i < available_accounts_.size(); ++i) { | |
152 if (available_accounts_[i] != current_username_) { | |
153 AddCheckItem(kFirstAccountItemId + i, | |
154 ASCIIToUTF16(available_accounts_[i])); | |
155 SetIcon(GetIndexOfCommandId(kFirstAccountItemId + i), wallet_icon); | |
156 } | |
157 } | |
158 | |
159 AddCheckItemWithStringId(kAutofillItemId, | |
160 IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET); | |
161 } | |
162 | |
163 } // autofill | |
OLD | NEW |