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