OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/webui/options2/autofill_options_handler.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/logging.h" | |
12 #include "base/string16.h" | |
13 #include "base/string_number_conversions.h" | |
14 #include "base/utf_string_conversions.h" | |
15 #include "base/values.h" | |
16 #include "chrome/browser/autofill/autofill_country.h" | |
17 #include "chrome/browser/autofill/autofill_profile.h" | |
18 #include "chrome/browser/autofill/credit_card.h" | |
19 #include "chrome/browser/autofill/personal_data_manager.h" | |
20 #include "chrome/browser/autofill/personal_data_manager_factory.h" | |
21 #include "chrome/browser/autofill/phone_number_i18n.h" | |
22 #include "chrome/browser/profiles/profile.h" | |
23 #include "chrome/browser/ui/webui/web_ui_util.h" | |
24 #include "chrome/common/guid.h" | |
25 #include "grit/generated_resources.h" | |
26 #include "grit/webkit_resources.h" | |
27 #include "ui/base/l10n/l10n_util.h" | |
28 | |
29 namespace { | |
30 | |
31 // Converts a credit card type to the appropriate resource ID of the CC icon. | |
32 int CreditCardTypeToResourceID(const std::string& type) { | |
33 if (type == kAmericanExpressCard) | |
34 return IDR_AUTOFILL_CC_AMEX; | |
35 else if (type == kDinersCard) | |
36 return IDR_AUTOFILL_CC_DINERS; | |
37 else if (type == kDiscoverCard) | |
38 return IDR_AUTOFILL_CC_DISCOVER; | |
39 else if (type == kGenericCard) | |
40 return IDR_AUTOFILL_CC_GENERIC; | |
41 else if (type == kJCBCard) | |
42 return IDR_AUTOFILL_CC_JCB; | |
43 else if (type == kMasterCard) | |
44 return IDR_AUTOFILL_CC_MASTERCARD; | |
45 else if (type == kSoloCard) | |
46 return IDR_AUTOFILL_CC_SOLO; | |
47 else if (type == kVisaCard) | |
48 return IDR_AUTOFILL_CC_VISA; | |
49 | |
50 NOTREACHED(); | |
51 return 0; | |
52 } | |
53 | |
54 // Converts a credit card type to the appropriate localized card type. | |
55 string16 LocalizedCreditCardType(const std::string& type) { | |
56 if (type == kAmericanExpressCard) | |
57 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_AMEX); | |
58 else if (type == kDinersCard) | |
59 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DINERS); | |
60 else if (type == kDiscoverCard) | |
61 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_DISCOVER); | |
62 else if (type == kGenericCard) | |
63 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_GENERIC); | |
64 else if (type == kJCBCard) | |
65 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_JCB); | |
66 else if (type == kMasterCard) | |
67 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_MASTERCARD); | |
68 else if (type == kSoloCard) | |
69 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_SOLO); | |
70 else if (type == kVisaCard) | |
71 return l10n_util::GetStringUTF16(IDS_AUTOFILL_CC_VISA); | |
72 | |
73 NOTREACHED(); | |
74 return string16(); | |
75 } | |
76 | |
77 // Returns a dictionary that maps country codes to data for the country. | |
78 DictionaryValue* GetCountryData() { | |
79 std::string app_locale = AutofillCountry::ApplicationLocale(); | |
80 std::vector<std::string> country_codes; | |
81 AutofillCountry::GetAvailableCountries(&country_codes); | |
82 | |
83 DictionaryValue* country_data = new DictionaryValue(); | |
84 for (size_t i = 0; i < country_codes.size(); ++i) { | |
85 const AutofillCountry country(country_codes[i], app_locale); | |
86 | |
87 DictionaryValue* details = new DictionaryValue(); | |
88 details->SetString("name", country.name()); | |
89 details->SetString("postalCodeLabel", country.postal_code_label()); | |
90 details->SetString("stateLabel", country.state_label()); | |
91 | |
92 country_data->Set(country.country_code(), details); | |
93 } | |
94 | |
95 return country_data; | |
96 } | |
97 | |
98 // Get the multi-valued element for |type| and return it in |ListValue| form. | |
99 void GetValueList(const AutofillProfile& profile, | |
100 AutofillFieldType type, | |
101 scoped_ptr<ListValue>* list) { | |
102 list->reset(new ListValue); | |
103 | |
104 std::vector<string16> values; | |
105 profile.GetMultiInfo(type, &values); | |
106 | |
107 // |GetMultiInfo()| always returns at least one, potentially empty, item. | |
108 if (values.size() == 1 && values.front().empty()) | |
109 return; | |
110 | |
111 for (size_t i = 0; i < values.size(); ++i) { | |
112 (*list)->Set(i, Value::CreateStringValue(values[i])); | |
113 } | |
114 } | |
115 | |
116 // Set the multi-valued element for |type| from input |list| values. | |
117 void SetValueList(const ListValue* list, | |
118 AutofillFieldType type, | |
119 AutofillProfile* profile) { | |
120 std::vector<string16> values(list->GetSize()); | |
121 for (size_t i = 0; i < list->GetSize(); ++i) { | |
122 string16 value; | |
123 if (list->GetString(i, &value)) | |
124 values[i] = value; | |
125 } | |
126 profile->SetMultiInfo(type, values); | |
127 } | |
128 | |
129 // Get the multi-valued element for |type| and return it in |ListValue| form. | |
130 void GetNameList(const AutofillProfile& profile, | |
131 scoped_ptr<ListValue>* names) { | |
132 names->reset(new ListValue); | |
133 | |
134 std::vector<string16> first_names; | |
135 std::vector<string16> middle_names; | |
136 std::vector<string16> last_names; | |
137 profile.GetMultiInfo(NAME_FIRST, &first_names); | |
138 profile.GetMultiInfo(NAME_MIDDLE, &middle_names); | |
139 profile.GetMultiInfo(NAME_LAST, &last_names); | |
140 DCHECK_EQ(first_names.size(), middle_names.size()); | |
141 DCHECK_EQ(first_names.size(), last_names.size()); | |
142 | |
143 // |GetMultiInfo()| always returns at least one, potentially empty, item. | |
144 if (first_names.size() == 1 && first_names.front().empty() && | |
145 middle_names.front().empty() && last_names.front().empty()) { | |
146 return; | |
147 } | |
148 | |
149 for (size_t i = 0; i < first_names.size(); ++i) { | |
150 ListValue* name = new ListValue; // owned by |list| | |
151 name->Set(0, Value::CreateStringValue(first_names[i])); | |
152 name->Set(1, Value::CreateStringValue(middle_names[i])); | |
153 name->Set(2, Value::CreateStringValue(last_names[i])); | |
154 (*names)->Set(i, name); | |
155 } | |
156 } | |
157 | |
158 // Set the multi-valued element for |type| from input |list| values. | |
159 void SetNameList(const ListValue* names, | |
160 AutofillProfile* profile) { | |
161 const size_t size = names->GetSize(); | |
162 std::vector<string16> first_names(size); | |
163 std::vector<string16> middle_names(size); | |
164 std::vector<string16> last_names(size); | |
165 | |
166 for (size_t i = 0; i < size; ++i) { | |
167 ListValue* name; | |
168 bool success = names->GetList(i, &name); | |
169 DCHECK(success); | |
170 | |
171 string16 first_name; | |
172 success = name->GetString(0, &first_name); | |
173 DCHECK(success); | |
174 first_names[i] = first_name; | |
175 | |
176 string16 middle_name; | |
177 success = name->GetString(1, &middle_name); | |
178 DCHECK(success); | |
179 middle_names[i] = middle_name; | |
180 | |
181 string16 last_name; | |
182 success = name->GetString(2, &last_name); | |
183 DCHECK(success); | |
184 last_names[i] = last_name; | |
185 } | |
186 | |
187 profile->SetMultiInfo(NAME_FIRST, first_names); | |
188 profile->SetMultiInfo(NAME_MIDDLE, middle_names); | |
189 profile->SetMultiInfo(NAME_LAST, last_names); | |
190 } | |
191 | |
192 // Pulls the phone number |index|, |phone_number_list|, and |country_code| from | |
193 // the |args| input. | |
194 void ExtractPhoneNumberInformation(const ListValue* args, | |
195 size_t* index, | |
196 ListValue** phone_number_list, | |
197 std::string* country_code) { | |
198 // Retrieve index as a |double|, as that is how it comes across from | |
199 // JavaScript. | |
200 double number = 0.0; | |
201 if (!args->GetDouble(0, &number)) { | |
202 NOTREACHED(); | |
203 return; | |
204 } | |
205 *index = number; | |
206 | |
207 if (!args->GetList(1, phone_number_list)) { | |
208 NOTREACHED(); | |
209 return; | |
210 } | |
211 | |
212 if (!args->GetString(2, country_code)) { | |
213 NOTREACHED(); | |
214 return; | |
215 } | |
216 } | |
217 | |
218 // Searches the |list| for the value at |index|. If this value is present | |
219 // in any of the rest of the list, then the item (at |index|) is removed. | |
220 // The comparison of phone number values is done on normalized versions of the | |
221 // phone number values. | |
222 void RemoveDuplicatePhoneNumberAtIndex(size_t index, | |
223 const std::string& country_code, | |
224 ListValue* list) { | |
225 string16 new_value; | |
226 if (!list->GetString(index, &new_value)) { | |
227 NOTREACHED() << "List should have a value at index " << index; | |
228 return; | |
229 } | |
230 | |
231 bool is_duplicate = false; | |
232 for (size_t i = 0; i < list->GetSize() && !is_duplicate; ++i) { | |
233 if (i == index) | |
234 continue; | |
235 | |
236 string16 existing_value; | |
237 if (!list->GetString(i, &existing_value)) { | |
238 NOTREACHED() << "List should have a value at index " << i; | |
239 continue; | |
240 } | |
241 is_duplicate = autofill_i18n::PhoneNumbersMatch(new_value, | |
242 existing_value, | |
243 country_code); | |
244 } | |
245 | |
246 if (is_duplicate) | |
247 list->Remove(index, NULL); | |
248 } | |
249 | |
250 void ValidatePhoneArguments(const ListValue* args, ListValue** list) { | |
251 size_t index = 0; | |
252 std::string country_code; | |
253 ExtractPhoneNumberInformation(args, &index, list, &country_code); | |
254 | |
255 RemoveDuplicatePhoneNumberAtIndex(index, country_code, *list); | |
256 } | |
257 | |
258 } // namespace | |
259 | |
260 AutofillOptionsHandler::AutofillOptionsHandler() | |
261 : personal_data_(NULL) { | |
262 } | |
263 | |
264 AutofillOptionsHandler::~AutofillOptionsHandler() { | |
265 if (personal_data_) | |
266 personal_data_->RemoveObserver(this); | |
267 } | |
268 | |
269 ///////////////////////////////////////////////////////////////////////////// | |
270 // OptionsPageUIHandler implementation: | |
271 void AutofillOptionsHandler::GetLocalizedValues( | |
272 DictionaryValue* localized_strings) { | |
273 DCHECK(localized_strings); | |
274 | |
275 static OptionsStringResource resources[] = { | |
276 { "autofillAddresses", IDS_AUTOFILL_ADDRESSES_GROUP_NAME }, | |
277 { "autofillCreditCards", IDS_AUTOFILL_CREDITCARDS_GROUP_NAME }, | |
278 { "autofillAddAddress", IDS_AUTOFILL_ADD_ADDRESS_BUTTON }, | |
279 { "autofillAddCreditCard", IDS_AUTOFILL_ADD_CREDITCARD_BUTTON }, | |
280 { "autofillEditProfileButton", IDS_AUTOFILL_EDIT_PROFILE_BUTTON }, | |
281 { "helpButton", IDS_AUTOFILL_HELP_LABEL }, | |
282 { "addAddressTitle", IDS_AUTOFILL_ADD_ADDRESS_CAPTION }, | |
283 { "editAddressTitle", IDS_AUTOFILL_EDIT_ADDRESS_CAPTION }, | |
284 { "addCreditCardTitle", IDS_AUTOFILL_ADD_CREDITCARD_CAPTION }, | |
285 { "editCreditCardTitle", IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION }, | |
286 #if defined(OS_MACOSX) | |
287 { "auxiliaryProfilesEnabled", IDS_AUTOFILL_USE_MAC_ADDRESS_BOOK }, | |
288 #endif // defined(OS_MACOSX) | |
289 }; | |
290 | |
291 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
292 RegisterTitle(localized_strings, "autofillOptionsPage", | |
293 IDS_AUTOFILL_OPTIONS_TITLE); | |
294 | |
295 SetAddressOverlayStrings(localized_strings); | |
296 SetCreditCardOverlayStrings(localized_strings); | |
297 } | |
298 | |
299 void AutofillOptionsHandler::Initialize() { | |
300 personal_data_ = PersonalDataManagerFactory::GetForProfile( | |
301 Profile::FromWebUI(web_ui_)); | |
302 // personal_data_ is NULL in guest mode on Chrome OS. | |
303 if (personal_data_) { | |
304 personal_data_->SetObserver(this); | |
305 LoadAutofillData(); | |
306 } | |
307 } | |
308 | |
309 void AutofillOptionsHandler::RegisterMessages() { | |
310 web_ui_->RegisterMessageCallback( | |
311 "removeAddress", | |
312 base::Bind(&AutofillOptionsHandler::RemoveAddress, | |
313 base::Unretained(this))); | |
314 web_ui_->RegisterMessageCallback( | |
315 "removeCreditCard", | |
316 base::Bind(&AutofillOptionsHandler::RemoveCreditCard, | |
317 base::Unretained(this))); | |
318 web_ui_->RegisterMessageCallback( | |
319 "loadAddressEditor", | |
320 base::Bind(&AutofillOptionsHandler::LoadAddressEditor, | |
321 base::Unretained(this))); | |
322 web_ui_->RegisterMessageCallback( | |
323 "loadCreditCardEditor", | |
324 base::Bind(&AutofillOptionsHandler::LoadCreditCardEditor, | |
325 base::Unretained(this))); | |
326 web_ui_->RegisterMessageCallback( | |
327 "setAddress", | |
328 base::Bind(&AutofillOptionsHandler::SetAddress, base::Unretained(this))); | |
329 web_ui_->RegisterMessageCallback( | |
330 "setCreditCard", | |
331 base::Bind(&AutofillOptionsHandler::SetCreditCard, | |
332 base::Unretained(this))); | |
333 web_ui_->RegisterMessageCallback( | |
334 "validatePhoneNumbers", | |
335 base::Bind(&AutofillOptionsHandler::ValidatePhoneNumbers, | |
336 base::Unretained(this))); | |
337 } | |
338 | |
339 ///////////////////////////////////////////////////////////////////////////// | |
340 // PersonalDataManagerObserver implementation: | |
341 void AutofillOptionsHandler::OnPersonalDataChanged() { | |
342 LoadAutofillData(); | |
343 } | |
344 | |
345 void AutofillOptionsHandler::SetAddressOverlayStrings( | |
346 DictionaryValue* localized_strings) { | |
347 localized_strings->SetString("autofillEditAddressTitle", | |
348 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION)); | |
349 localized_strings->SetString("autofillFirstNameLabel", | |
350 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_FIRST_NAME)); | |
351 localized_strings->SetString("autofillMiddleNameLabel", | |
352 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MIDDLE_NAME)); | |
353 localized_strings->SetString("autofillLastNameLabel", | |
354 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_LAST_NAME)); | |
355 localized_strings->SetString("autofillCompanyNameLabel", | |
356 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_COMPANY_NAME)); | |
357 localized_strings->SetString("autofillAddrLine1Label", | |
358 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADDRESS_LINE_1)); | |
359 localized_strings->SetString("autofillAddrLine2Label", | |
360 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADDRESS_LINE_2)); | |
361 localized_strings->SetString("autofillCityLabel", | |
362 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CITY)); | |
363 localized_strings->SetString("autofillCountryLabel", | |
364 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_COUNTRY)); | |
365 localized_strings->SetString("autofillPhoneLabel", | |
366 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PHONE)); | |
367 localized_strings->SetString("autofillEmailLabel", | |
368 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EMAIL)); | |
369 localized_strings->SetString("autofillAddFirstNamePlaceholder", | |
370 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_FIRST_NAME)); | |
371 localized_strings->SetString("autofillAddMiddleNamePlaceholder", | |
372 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_MIDDLE_NAME)); | |
373 localized_strings->SetString("autofillAddLastNamePlaceholder", | |
374 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_LAST_NAME)); | |
375 localized_strings->SetString("autofillAddPhonePlaceholder", | |
376 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_PHONE)); | |
377 localized_strings->SetString("autofillAddEmailPlaceholder", | |
378 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_EMAIL)); | |
379 | |
380 std::string app_locale = AutofillCountry::ApplicationLocale(); | |
381 std::string default_country_code = | |
382 AutofillCountry::CountryCodeForLocale(app_locale); | |
383 localized_strings->SetString("defaultCountryCode", default_country_code); | |
384 localized_strings->Set("autofillCountryData", GetCountryData()); | |
385 } | |
386 | |
387 void AutofillOptionsHandler::SetCreditCardOverlayStrings( | |
388 DictionaryValue* localized_strings) { | |
389 localized_strings->SetString("autofillEditCreditCardTitle", | |
390 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION)); | |
391 localized_strings->SetString("nameOnCardLabel", | |
392 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NAME_ON_CARD)); | |
393 localized_strings->SetString("creditCardNumberLabel", | |
394 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CREDIT_CARD_NUMBER)); | |
395 localized_strings->SetString("creditCardExpirationDateLabel", | |
396 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EXPIRATION_DATE)); | |
397 } | |
398 | |
399 void AutofillOptionsHandler::LoadAutofillData() { | |
400 if (!personal_data_->IsDataLoaded()) | |
401 return; | |
402 | |
403 ListValue addresses; | |
404 for (std::vector<AutofillProfile*>::const_iterator i = | |
405 personal_data_->web_profiles().begin(); | |
406 i != personal_data_->web_profiles().end(); ++i) { | |
407 ListValue* entry = new ListValue(); | |
408 entry->Append(new StringValue((*i)->guid())); | |
409 entry->Append(new StringValue((*i)->Label())); | |
410 addresses.Append(entry); | |
411 } | |
412 | |
413 web_ui_->CallJavascriptFunction("AutofillOptions.setAddressList", addresses); | |
414 | |
415 ListValue credit_cards; | |
416 for (std::vector<CreditCard*>::const_iterator i = | |
417 personal_data_->credit_cards().begin(); | |
418 i != personal_data_->credit_cards().end(); ++i) { | |
419 ListValue* entry = new ListValue(); | |
420 entry->Append(new StringValue((*i)->guid())); | |
421 entry->Append(new StringValue((*i)->Label())); | |
422 int res = CreditCardTypeToResourceID((*i)->type()); | |
423 entry->Append( | |
424 new StringValue(web_ui_util::GetImageDataUrlFromResource(res))); | |
425 entry->Append(new StringValue(LocalizedCreditCardType((*i)->type()))); | |
426 credit_cards.Append(entry); | |
427 } | |
428 | |
429 web_ui_->CallJavascriptFunction("AutofillOptions.setCreditCardList", | |
430 credit_cards); | |
431 } | |
432 | |
433 void AutofillOptionsHandler::RemoveAddress(const ListValue* args) { | |
434 DCHECK(personal_data_->IsDataLoaded()); | |
435 | |
436 std::string guid; | |
437 if (!args->GetString(0, &guid)) { | |
438 NOTREACHED(); | |
439 return; | |
440 } | |
441 | |
442 personal_data_->RemoveProfile(guid); | |
443 } | |
444 | |
445 void AutofillOptionsHandler::RemoveCreditCard(const ListValue* args) { | |
446 DCHECK(personal_data_->IsDataLoaded()); | |
447 | |
448 std::string guid; | |
449 if (!args->GetString(0, &guid)) { | |
450 NOTREACHED(); | |
451 return; | |
452 } | |
453 | |
454 personal_data_->RemoveCreditCard(guid); | |
455 } | |
456 | |
457 void AutofillOptionsHandler::LoadAddressEditor(const ListValue* args) { | |
458 DCHECK(personal_data_->IsDataLoaded()); | |
459 | |
460 std::string guid; | |
461 if (!args->GetString(0, &guid)) { | |
462 NOTREACHED(); | |
463 return; | |
464 } | |
465 | |
466 AutofillProfile* profile = personal_data_->GetProfileByGUID(guid); | |
467 if (!profile) { | |
468 // There is a race where a user can click once on the close button and | |
469 // quickly click again on the list item before the item is removed (since | |
470 // the list is not updated until the model tells the list an item has been | |
471 // removed). This will activate the editor for a profile that has been | |
472 // removed. Do nothing in that case. | |
473 return; | |
474 } | |
475 | |
476 DictionaryValue address; | |
477 address.SetString("guid", profile->guid()); | |
478 scoped_ptr<ListValue> list; | |
479 GetNameList(*profile, &list); | |
480 address.Set("fullName", list.release()); | |
481 address.SetString("companyName", profile->GetInfo(COMPANY_NAME)); | |
482 address.SetString("addrLine1", profile->GetInfo(ADDRESS_HOME_LINE1)); | |
483 address.SetString("addrLine2", profile->GetInfo(ADDRESS_HOME_LINE2)); | |
484 address.SetString("city", profile->GetInfo(ADDRESS_HOME_CITY)); | |
485 address.SetString("state", profile->GetInfo(ADDRESS_HOME_STATE)); | |
486 address.SetString("postalCode", profile->GetInfo(ADDRESS_HOME_ZIP)); | |
487 address.SetString("country", profile->CountryCode()); | |
488 GetValueList(*profile, PHONE_HOME_WHOLE_NUMBER, &list); | |
489 address.Set("phone", list.release()); | |
490 GetValueList(*profile, EMAIL_ADDRESS, &list); | |
491 address.Set("email", list.release()); | |
492 | |
493 web_ui_->CallJavascriptFunction("AutofillOptions.editAddress", address); | |
494 } | |
495 | |
496 void AutofillOptionsHandler::LoadCreditCardEditor(const ListValue* args) { | |
497 DCHECK(personal_data_->IsDataLoaded()); | |
498 | |
499 std::string guid; | |
500 if (!args->GetString(0, &guid)) { | |
501 NOTREACHED(); | |
502 return; | |
503 } | |
504 | |
505 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid); | |
506 if (!credit_card) { | |
507 // There is a race where a user can click once on the close button and | |
508 // quickly click again on the list item before the item is removed (since | |
509 // the list is not updated until the model tells the list an item has been | |
510 // removed). This will activate the editor for a profile that has been | |
511 // removed. Do nothing in that case. | |
512 return; | |
513 } | |
514 | |
515 DictionaryValue credit_card_data; | |
516 credit_card_data.SetString("guid", credit_card->guid()); | |
517 credit_card_data.SetString("nameOnCard", | |
518 credit_card->GetInfo(CREDIT_CARD_NAME)); | |
519 credit_card_data.SetString("creditCardNumber", | |
520 credit_card->GetInfo(CREDIT_CARD_NUMBER)); | |
521 credit_card_data.SetString("expirationMonth", | |
522 credit_card->GetInfo(CREDIT_CARD_EXP_MONTH)); | |
523 credit_card_data.SetString( | |
524 "expirationYear", | |
525 credit_card->GetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | |
526 | |
527 web_ui_->CallJavascriptFunction("AutofillOptions.editCreditCard", | |
528 credit_card_data); | |
529 } | |
530 | |
531 void AutofillOptionsHandler::SetAddress(const ListValue* args) { | |
532 if (!personal_data_->IsDataLoaded()) | |
533 return; | |
534 | |
535 std::string guid; | |
536 if (!args->GetString(0, &guid)) { | |
537 NOTREACHED(); | |
538 return; | |
539 } | |
540 | |
541 AutofillProfile profile(guid); | |
542 | |
543 std::string country_code; | |
544 string16 value; | |
545 ListValue* list_value; | |
546 if (args->GetList(1, &list_value)) | |
547 SetNameList(list_value, &profile); | |
548 if (args->GetString(2, &value)) | |
549 profile.SetInfo(COMPANY_NAME, value); | |
550 if (args->GetString(3, &value)) | |
551 profile.SetInfo(ADDRESS_HOME_LINE1, value); | |
552 if (args->GetString(4, &value)) | |
553 profile.SetInfo(ADDRESS_HOME_LINE2, value); | |
554 if (args->GetString(5, &value)) | |
555 profile.SetInfo(ADDRESS_HOME_CITY, value); | |
556 if (args->GetString(6, &value)) | |
557 profile.SetInfo(ADDRESS_HOME_STATE, value); | |
558 if (args->GetString(7, &value)) | |
559 profile.SetInfo(ADDRESS_HOME_ZIP, value); | |
560 if (args->GetString(8, &country_code)) | |
561 profile.SetCountryCode(country_code); | |
562 if (args->GetList(9, &list_value)) | |
563 SetValueList(list_value, PHONE_HOME_WHOLE_NUMBER, &profile); | |
564 if (args->GetList(10, &list_value)) | |
565 SetValueList(list_value, EMAIL_ADDRESS, &profile); | |
566 | |
567 if (!guid::IsValidGUID(profile.guid())) { | |
568 profile.set_guid(guid::GenerateGUID()); | |
569 personal_data_->AddProfile(profile); | |
570 } else { | |
571 personal_data_->UpdateProfile(profile); | |
572 } | |
573 } | |
574 | |
575 void AutofillOptionsHandler::SetCreditCard(const ListValue* args) { | |
576 if (!personal_data_->IsDataLoaded()) | |
577 return; | |
578 | |
579 std::string guid; | |
580 if (!args->GetString(0, &guid)) { | |
581 NOTREACHED(); | |
582 return; | |
583 } | |
584 | |
585 CreditCard credit_card(guid); | |
586 | |
587 string16 value; | |
588 if (args->GetString(1, &value)) | |
589 credit_card.SetInfo(CREDIT_CARD_NAME, value); | |
590 if (args->GetString(2, &value)) | |
591 credit_card.SetInfo(CREDIT_CARD_NUMBER, value); | |
592 if (args->GetString(3, &value)) | |
593 credit_card.SetInfo(CREDIT_CARD_EXP_MONTH, value); | |
594 if (args->GetString(4, &value)) | |
595 credit_card.SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, value); | |
596 | |
597 if (!guid::IsValidGUID(credit_card.guid())) { | |
598 credit_card.set_guid(guid::GenerateGUID()); | |
599 personal_data_->AddCreditCard(credit_card); | |
600 } else { | |
601 personal_data_->UpdateCreditCard(credit_card); | |
602 } | |
603 } | |
604 | |
605 void AutofillOptionsHandler::ValidatePhoneNumbers(const ListValue* args) { | |
606 if (!personal_data_->IsDataLoaded()) | |
607 return; | |
608 | |
609 ListValue* list_value = NULL; | |
610 ValidatePhoneArguments(args, &list_value); | |
611 | |
612 web_ui_->CallJavascriptFunction( | |
613 "AutofillEditAddressOverlay.setValidatedPhoneNumbers", *list_value); | |
614 } | |
OLD | NEW |