Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/browser/dom_ui/options/autofill_options_handler.cc

Issue 6034005: DOMUI: Implement the new-style Autofill options page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/dom_ui/options/autofill_options_handler.h" 5 #include "chrome/browser/dom_ui/options/autofill_options_handler.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "app/l10n_util.h" 9 #include "app/l10n_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 16 matching lines...) Expand all
27 } 27 }
28 28
29 ///////////////////////////////////////////////////////////////////////////// 29 /////////////////////////////////////////////////////////////////////////////
30 // OptionsUIHandler implementation: 30 // OptionsUIHandler implementation:
31 void AutoFillOptionsHandler::GetLocalizedValues( 31 void AutoFillOptionsHandler::GetLocalizedValues(
32 DictionaryValue* localized_strings) { 32 DictionaryValue* localized_strings) {
33 DCHECK(localized_strings); 33 DCHECK(localized_strings);
34 34
35 localized_strings->SetString("autoFillOptionsTitle", 35 localized_strings->SetString("autoFillOptionsTitle",
36 l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_TITLE)); 36 l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_TITLE));
37 localized_strings->SetString("autoFillEnabled", 37 localized_strings->SetString("autoFillAddresses",
38 l10n_util::GetStringUTF16(IDS_OPTIONS_AUTOFILL_ENABLE));
39 localized_strings->SetString("addressesHeader",
40 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESSES_GROUP_NAME)); 38 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESSES_GROUP_NAME));
41 localized_strings->SetString("creditCardsHeader", 39 localized_strings->SetString("autoFillCreditCards",
42 l10n_util::GetStringUTF16(IDS_AUTOFILL_CREDITCARDS_GROUP_NAME)); 40 l10n_util::GetStringUTF16(IDS_AUTOFILL_CREDITCARDS_GROUP_NAME));
43 localized_strings->SetString("addAddressButton", 41 localized_strings->SetString("autoFillAddAddress",
44 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_BUTTON)); 42 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_BUTTON));
45 localized_strings->SetString("addCreditCardButton", 43 localized_strings->SetString("autoFillAddCreditCard",
46 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_BUTTON)); 44 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_BUTTON));
47 localized_strings->SetString("editButton",
48 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_BUTTON));
49 localized_strings->SetString("deleteButton",
50 l10n_util::GetStringUTF16(IDS_AUTOFILL_DELETE_BUTTON));
51 localized_strings->SetString("helpButton", 45 localized_strings->SetString("helpButton",
52 l10n_util::GetStringUTF16(IDS_AUTOFILL_HELP_LABEL)); 46 l10n_util::GetStringUTF16(IDS_AUTOFILL_HELP_LABEL));
53 localized_strings->SetString("addAddressTitle", 47 localized_strings->SetString("addAddressTitle",
54 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_CAPTION)); 48 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_CAPTION));
55 localized_strings->SetString("editAddressTitle", 49 localized_strings->SetString("editAddressTitle",
56 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION)); 50 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION));
57 localized_strings->SetString("addCreditCardTitle", 51 localized_strings->SetString("addCreditCardTitle",
58 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION)); 52 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION));
59 localized_strings->SetString("editCreditCardTitle", 53 localized_strings->SetString("editCreditCardTitle",
60 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION)); 54 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION));
61 55
62 SetAddressOverlayStrings(localized_strings); 56 SetAddressOverlayStrings(localized_strings);
63 SetCreditCardOverlayStrings(localized_strings); 57 SetCreditCardOverlayStrings(localized_strings);
64 } 58 }
65 59
66 void AutoFillOptionsHandler::Initialize() { 60 void AutoFillOptionsHandler::Initialize() {
67 personal_data_ = 61 personal_data_ = dom_ui_->GetProfile()->GetPersonalDataManager();
68 dom_ui_->GetProfile()->GetOriginalProfile()->GetPersonalDataManager();
69 personal_data_->SetObserver(this); 62 personal_data_->SetObserver(this);
70 63
71 LoadAutoFillData(); 64 LoadAutoFillData();
72 } 65 }
73 66
74 void AutoFillOptionsHandler::RegisterMessages() { 67 void AutoFillOptionsHandler::RegisterMessages() {
75 dom_ui_->RegisterMessageCallback( 68 dom_ui_->RegisterMessageCallback(
76 "updateAddress", 69 "removeAutoFillProfile",
77 NewCallback(this, &AutoFillOptionsHandler::UpdateAddress)); 70 NewCallback(this, &AutoFillOptionsHandler::RemoveAutoFillProfile));
78
79 dom_ui_->RegisterMessageCallback( 71 dom_ui_->RegisterMessageCallback(
80 "editAddress", 72 "loadProfileEditor",
81 NewCallback(this, &AutoFillOptionsHandler::EditAddress)); 73 NewCallback(this, &AutoFillOptionsHandler::LoadProfileEditor));
82
83 dom_ui_->RegisterMessageCallback( 74 dom_ui_->RegisterMessageCallback(
84 "removeAddress", 75 "setAddress",
85 NewCallback(this, &AutoFillOptionsHandler::RemoveAddress)); 76 NewCallback(this, &AutoFillOptionsHandler::SetAddress));
86
87 dom_ui_->RegisterMessageCallback( 77 dom_ui_->RegisterMessageCallback(
88 "updateCreditCard", 78 "setCreditCard",
89 NewCallback(this, &AutoFillOptionsHandler::UpdateCreditCard)); 79 NewCallback(this, &AutoFillOptionsHandler::SetCreditCard));
90
91 dom_ui_->RegisterMessageCallback(
92 "editCreditCard",
93 NewCallback(this, &AutoFillOptionsHandler::EditCreditCard));
94
95 dom_ui_->RegisterMessageCallback(
96 "removeCreditCard",
97 NewCallback(this, &AutoFillOptionsHandler::RemoveCreditCard));
98 } 80 }
99 81
100 ///////////////////////////////////////////////////////////////////////////// 82 /////////////////////////////////////////////////////////////////////////////
101 // PersonalDataManager::Observer implementation: 83 // PersonalDataManager::Observer implementation:
102 void AutoFillOptionsHandler::OnPersonalDataLoaded() { 84 void AutoFillOptionsHandler::OnPersonalDataLoaded() {
103 LoadAutoFillData(); 85 LoadAutoFillData();
104 } 86 }
105 87
106 void AutoFillOptionsHandler::OnPersonalDataChanged() { 88 void AutoFillOptionsHandler::OnPersonalDataChanged() {
107 LoadAutoFillData(); 89 LoadAutoFillData();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 } 132 }
151 133
152 void AutoFillOptionsHandler::LoadAutoFillData() { 134 void AutoFillOptionsHandler::LoadAutoFillData() {
153 if (!personal_data_->IsDataLoaded()) 135 if (!personal_data_->IsDataLoaded())
154 return; 136 return;
155 137
156 ListValue addresses; 138 ListValue addresses;
157 for (std::vector<AutoFillProfile*>::const_iterator i = 139 for (std::vector<AutoFillProfile*>::const_iterator i =
158 personal_data_->web_profiles().begin(); 140 personal_data_->web_profiles().begin();
159 i != personal_data_->web_profiles().end(); ++i) { 141 i != personal_data_->web_profiles().end(); ++i) {
160 DictionaryValue* address = new DictionaryValue(); 142 ListValue* entry = new ListValue();
161 address->SetString("label", (*i)->Label()); 143 entry->Append(new StringValue((*i)->guid()));
162 address->SetString("guid", (*i)->guid()); 144 entry->Append(new StringValue((*i)->Label()));
163 addresses.Append(address); 145 addresses.Append(entry);
164 } 146 }
165 147
166 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateAddresses", 148 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.setAddressList",
167 addresses); 149 addresses);
168 150
169 ListValue credit_cards; 151 ListValue credit_cards;
170 for (std::vector<CreditCard*>::const_iterator i = 152 for (std::vector<CreditCard*>::const_iterator i =
171 personal_data_->credit_cards().begin(); 153 personal_data_->credit_cards().begin();
172 i != personal_data_->credit_cards().end(); ++i) { 154 i != personal_data_->credit_cards().end(); ++i) {
173 DictionaryValue* credit_card = new DictionaryValue(); 155 ListValue* entry = new ListValue();
174 credit_card->SetString("label", (*i)->PreviewSummary()); 156 entry->Append(new StringValue((*i)->guid()));
175 credit_card->SetString("guid", (*i)->guid()); 157 entry->Append(new StringValue((*i)->PreviewSummary()));
176 credit_cards.Append(credit_card); 158 credit_cards.Append(entry);
177 } 159 }
178 160
179 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateCreditCards", 161 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.setCreditCardList",
180 credit_cards); 162 credit_cards);
181 } 163 }
182 164
183 void AutoFillOptionsHandler::UpdateAddress(const ListValue* args) { 165 void AutoFillOptionsHandler::RemoveAutoFillProfile(const ListValue* args) {
166 DCHECK(personal_data_->IsDataLoaded());
167
168 std::string guid;
169 if (!args->GetString(0, &guid)) {
170 NOTREACHED();
171 return;
172 }
173
174 // TODO(jhawkins): Make RemoveProfile return true/false depending on whether
175 // the profile was removed or not.
176 if (personal_data_->GetProfileByGUID(guid) != NULL)
177 personal_data_->RemoveProfile(guid);
178 else
179 personal_data_->RemoveCreditCard(guid);
180 }
181
182 void AutoFillOptionsHandler::LoadProfileEditor(const ListValue* args) {
183 DCHECK(personal_data_->IsDataLoaded());
184
185 std::string guid;
186 if (!args->GetString(0, &guid)) {
187 NOTREACHED();
188 return;
189 }
190
191 if (personal_data_->GetProfileByGUID(guid) != NULL)
arv (Not doing code reviews) 2010/12/22 18:14:07 I find this confusing (and same in RemoveAutoFillP
James Hawkins 2010/12/22 21:22:31 I'll add a comment to explain.
192 EditAddress(guid);
193 else
194 EditCreditCard(guid);
195 }
196
197 void AutoFillOptionsHandler::SetAddress(const ListValue* args) {
184 if (!personal_data_->IsDataLoaded()) 198 if (!personal_data_->IsDataLoaded())
185 return; 199 return;
186 200
187 std::string guid; 201 std::string guid;
188 if (!args->GetString(0, &guid)) { 202 if (!args->GetString(0, &guid)) {
189 NOTREACHED(); 203 NOTREACHED();
190 return; 204 return;
191 } 205 }
192 206
193 AutoFillProfile profile(guid); 207 AutoFillProfile profile(guid);
(...skipping 23 matching lines...) Expand all
217 profile.SetInfo(AutoFillType(EMAIL_ADDRESS), value); 231 profile.SetInfo(AutoFillType(EMAIL_ADDRESS), value);
218 232
219 if (!guid::IsValidGUID(profile.guid())) { 233 if (!guid::IsValidGUID(profile.guid())) {
220 profile.set_guid(guid::GenerateGUID()); 234 profile.set_guid(guid::GenerateGUID());
221 personal_data_->AddProfile(profile); 235 personal_data_->AddProfile(profile);
222 } else { 236 } else {
223 personal_data_->UpdateProfile(profile); 237 personal_data_->UpdateProfile(profile);
224 } 238 }
225 } 239 }
226 240
227 void AutoFillOptionsHandler::EditAddress(const ListValue* args) { 241 void AutoFillOptionsHandler::SetCreditCard(const ListValue* args) {
228 if (!personal_data_->IsDataLoaded()) 242 if (!personal_data_->IsDataLoaded())
229 return; 243 return;
230 244
231 std::string guid; 245 std::string guid;
232 if (!args->GetString(0, &guid)) { 246 if (!args->GetString(0, &guid)) {
233 NOTREACHED(); 247 NOTREACHED();
234 return; 248 return;
235 } 249 }
236 250
251 CreditCard credit_card(guid);
252
253 string16 value;
254 if (args->GetString(1, &value))
255 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NAME), value);
256 if (args->GetString(2, &value))
257 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), value);
258 if (args->GetString(3, &value))
259 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), value);
260 if (args->GetString(4, &value))
261 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), value);
262
263 if (!guid::IsValidGUID(credit_card.guid())) {
264 credit_card.set_guid(guid::GenerateGUID());
265 personal_data_->AddCreditCard(credit_card);
266 } else {
267 personal_data_->UpdateCreditCard(credit_card);
268 }
269 }
270
271 void AutoFillOptionsHandler::EditAddress(const std::string& guid) {
272 DCHECK(personal_data_->IsDataLoaded());
273
237 AutoFillProfile* profile = personal_data_->GetProfileByGUID(guid); 274 AutoFillProfile* profile = personal_data_->GetProfileByGUID(guid);
238 if (!profile) { 275 if (!profile) {
239 NOTREACHED(); 276 NOTREACHED();
240 return; 277 return;
241 } 278 }
242 279
243 // TODO(jhawkins): This is hacky because we can't send DictionaryValue 280 // TODO(jhawkins): This is hacky because we can't send DictionaryValue
244 // directly to CallJavascriptFunction(). 281 // directly to CallJavascriptFunction().
245 ListValue addressList; 282 ListValue addressList;
246 DictionaryValue* address = new DictionaryValue(); 283 DictionaryValue* address = new DictionaryValue();
(...skipping 21 matching lines...) Expand all
268 "fax", 305 "fax",
269 profile->GetFieldText(AutoFillType(PHONE_FAX_WHOLE_NUMBER))); 306 profile->GetFieldText(AutoFillType(PHONE_FAX_WHOLE_NUMBER)));
270 address->SetString("email", 307 address->SetString("email",
271 profile->GetFieldText(AutoFillType(EMAIL_ADDRESS))); 308 profile->GetFieldText(AutoFillType(EMAIL_ADDRESS)));
272 addressList.Append(address); 309 addressList.Append(address);
273 310
274 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editAddress", 311 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editAddress",
275 addressList); 312 addressList);
276 } 313 }
277 314
278 void AutoFillOptionsHandler::RemoveAddress(const ListValue* args) { 315 void AutoFillOptionsHandler::EditCreditCard(const std::string& guid) {
279 if (!personal_data_->IsDataLoaded()) 316 DCHECK(personal_data_->IsDataLoaded());
280 return;
281
282 std::string guid;
283 if (!args->GetString(0, &guid)) {
284 NOTREACHED();
285 return;
286 }
287
288 personal_data_->RemoveProfile(guid);
289 }
290
291 void AutoFillOptionsHandler::UpdateCreditCard(const ListValue* args) {
292 if (!personal_data_->IsDataLoaded())
293 return;
294
295 std::string guid;
296 if (!args->GetString(0, &guid)) {
297 NOTREACHED();
298 return;
299 }
300
301 CreditCard credit_card(guid);
302
303 string16 value;
304 if (args->GetString(1, &value))
305 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NAME), value);
306 if (args->GetString(2, &value))
307 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), value);
308 if (args->GetString(3, &value))
309 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), value);
310 if (args->GetString(4, &value))
311 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), value);
312
313 if (!guid::IsValidGUID(credit_card.guid())) {
314 credit_card.set_guid(guid::GenerateGUID());
315 personal_data_->AddCreditCard(credit_card);
316 } else {
317 personal_data_->UpdateCreditCard(credit_card);
318 }
319
320 }
321
322 void AutoFillOptionsHandler::EditCreditCard(const ListValue* args) {
323 if (!personal_data_->IsDataLoaded())
324 return;
325
326 std::string guid;
327 if (!args->GetString(0, &guid)) {
328 NOTREACHED();
329 return;
330 }
331 317
332 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid); 318 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid);
333
334 if (!credit_card) { 319 if (!credit_card) {
335 NOTREACHED(); 320 NOTREACHED();
336 return; 321 return;
337 } 322 }
338 323
339 // TODO(jhawkins): This is hacky because we can't send DictionaryValue 324 // TODO(jhawkins): This is hacky because we can't send DictionaryValue
340 // directly to CallJavascriptFunction(). 325 // directly to CallJavascriptFunction().
341 ListValue credit_card_list; 326 ListValue credit_card_list;
342 DictionaryValue* credit_card_data = new DictionaryValue(); 327 DictionaryValue* credit_card_data = new DictionaryValue();
343 credit_card_data->SetString("guid", credit_card->guid()); 328 credit_card_data->SetString("guid", credit_card->guid());
344 credit_card_data->SetString( 329 credit_card_data->SetString(
345 "nameOnCard", 330 "nameOnCard",
346 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NAME))); 331 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NAME)));
347 credit_card_data->SetString( 332 credit_card_data->SetString(
348 "creditCardNumber", 333 "creditCardNumber",
349 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NUMBER))); 334 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NUMBER)));
350 credit_card_data->SetString( 335 credit_card_data->SetString(
351 "expirationMonth", 336 "expirationMonth",
352 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_MONTH))); 337 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_MONTH)));
353 credit_card_data->SetString( 338 credit_card_data->SetString(
354 "expirationYear", 339 "expirationYear",
355 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR))); 340 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)));
356 credit_card_list.Append(credit_card_data); 341 credit_card_list.Append(credit_card_data);
357 342
358 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editCreditCard", 343 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editCreditCard",
359 credit_card_list); 344 credit_card_list);
360 } 345 }
361
362 void AutoFillOptionsHandler::RemoveCreditCard(const ListValue* args) {
363 if (!personal_data_->IsDataLoaded())
364 return;
365
366 std::string guid;
367 if (!args->GetString(0, &guid)) {
368 NOTREACHED();
369 return;
370 }
371
372 personal_data_->RemoveCreditCard(guid);
373 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698