OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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::kActiveWalletItemId = 0; | |
23 const int AccountChooserModel::kAutofillItemId = 1; | |
24 const int AccountChooserModel::kFirstAdditionalItemId = 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 checked_item_(prefs->GetBoolean(prefs::kAutofillDialogPayWithoutWallet) ? | |
34 kAutofillItemId : kActiveWalletItemId), | |
35 had_wallet_error_(false) { | |
36 ReconstructMenuItems(); | |
37 } | |
38 | |
39 AccountChooserModel::~AccountChooserModel() { | |
40 } | |
41 | |
42 void AccountChooserModel::ForceSelectWalletAccount() { | |
43 ExecuteCommand(kActiveWalletItemId, 0); | |
44 } | |
45 | |
46 bool AccountChooserModel::HasAccountsToChoose() const { | |
47 return !had_wallet_error_ && | |
48 !(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.
| |
49 } | |
50 | |
51 void AccountChooserModel::SetAdditionalAccounts( | |
52 const std::vector<string16>& accounts) { | |
53 additional_accounts_ = accounts; | |
54 ReconstructMenuItems(); | |
55 account_delegate_->UpdateAccountChooserView(); | |
56 } | |
57 | |
58 std::vector<string16> AccountChooserModel::GetAllAccounts() const { | |
59 std::vector<string16> accounts(additional_accounts_); | |
60 if (!current_username_.empty() && | |
61 !std::find(accounts.begin(), accounts.end(), current_username_)) { | |
62 accounts.push_back(current_username_); | |
63 } | |
64 return accounts; | |
65 } | |
66 | |
67 void AccountChooserModel::SetActiveWalletAccountName( | |
68 const string16& account) { | |
69 current_username_ = account; | |
70 ReconstructMenuItems(); | |
71 account_delegate_->UpdateAccountChooserView(); | |
72 } | |
73 | |
74 string16 AccountChooserModel::GetActiveWalletAccountName() const { | |
75 return current_username_; | |
76 } | |
77 | |
78 void AccountChooserModel::ClearActiveWalletAccountName() { | |
79 current_username_.clear(); | |
80 ReconstructMenuItems(); | |
81 } | |
82 | |
83 bool AccountChooserModel::IsCommandIdChecked(int command_id) const { | |
84 return command_id == checked_item_; | |
85 } | |
86 | |
87 bool AccountChooserModel::IsCommandIdEnabled(int command_id) const { | |
88 // Currently, _any_ (non-sign-in) error disables _all_ Wallet accounts. | |
89 if (command_id != kAutofillItemId && had_wallet_error_) | |
90 return false; | |
91 | |
92 return true; | |
93 } | |
94 | |
95 bool AccountChooserModel::GetAcceleratorForCommandId( | |
96 int command_id, | |
97 ui::Accelerator* accelerator) { | |
98 return false; | |
99 } | |
100 | |
101 void AccountChooserModel::ExecuteCommand(int command_id, int event_flags) { | |
102 if (checked_item_ == command_id) | |
103 return; | |
104 | |
105 checked_item_ = command_id; | |
106 account_delegate_->AccountChoiceChanged(); | |
107 } | |
108 | |
109 void AccountChooserModel::SetHadWalletError() { | |
110 // Any non-sign-in error disables all Wallet accounts. | |
111 had_wallet_error_ = true; | |
112 ClearActiveWalletAccountName(); | |
113 ExecuteCommand(kAutofillItemId, 0); | |
114 } | |
115 | |
116 void AccountChooserModel::SetHadWalletSigninError() { | |
117 ClearActiveWalletAccountName(); | |
118 ExecuteCommand(kAutofillItemId, 0); | |
119 } | |
120 | |
121 bool AccountChooserModel::WalletIsSelected() const { | |
122 return checked_item_ != kAutofillItemId; | |
123 } | |
124 | |
125 bool AccountChooserModel::IsActiveWalletAccountSelected() const { | |
126 return checked_item_ == kActiveWalletItemId; | |
127 } | |
128 | |
129 void AccountChooserModel::ReconstructMenuItems() { | |
130 // If menu has to be reconstructed, either the currently active Wallet | |
131 // account or the autofill data should be selected. | |
132 DCHECK_GE(kAutofillItemId, checked_item_); | |
133 Clear(); | |
134 const gfx::Image& wallet_icon = | |
135 ui::ResourceBundle::GetSharedInstance().GetImageNamed(IDR_WALLET_ICON); | |
136 | |
137 if (!current_username_.empty()) { | |
138 AddCheckItem(kActiveWalletItemId, current_username_); | |
139 SetIcon(GetIndexOfCommandId(kActiveWalletItemId), wallet_icon); | |
140 } else { | |
141 // This will be empty until SetActiveWalletAccountName() is called. | |
142 // A throbber should be shown until the user name is set. | |
143 AddCheckItem(kActiveWalletItemId, | |
144 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_GOOGLE_WALLET)); | |
145 } | |
146 | |
147 for (size_t i = 0; i < additional_accounts_.size(); ++i) { | |
148 if (additional_accounts_[i] != current_username_) { | |
149 AddCheckItem(kFirstAdditionalItemId + i, additional_accounts_[i]); | |
150 SetIcon(GetIndexOfCommandId(kFirstAdditionalItemId + i), wallet_icon); | |
151 } | |
152 } | |
153 | |
154 AddCheckItemWithStringId(kAutofillItemId, | |
155 IDS_AUTOFILL_DIALOG_PAY_WITHOUT_WALLET); | |
156 } | |
157 | |
158 } // autofill | |
OLD | NEW |