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/options2/autofill_options_handler2.h" | |
6 | |
7 #include <vector> | |
8 | |
9 #include "base/bind.h" | |
10 #include "base/bind_helpers.h" | |
11 #include "base/guid.h" | |
12 #include "base/logging.h" | |
13 #include "base/string16.h" | |
14 #include "base/string_number_conversions.h" | |
15 #include "base/utf_string_conversions.h" | |
16 #include "base/values.h" | |
17 #include "chrome/browser/autofill/autofill_country.h" | |
18 #include "chrome/browser/autofill/autofill_profile.h" | |
19 #include "chrome/browser/autofill/credit_card.h" | |
20 #include "chrome/browser/autofill/personal_data_manager.h" | |
21 #include "chrome/browser/autofill/personal_data_manager_factory.h" | |
22 #include "chrome/browser/autofill/phone_number_i18n.h" | |
23 #include "chrome/browser/profiles/profile.h" | |
24 #include "chrome/browser/ui/webui/web_ui_util.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 namespace options2 { | |
263 | |
264 AutofillOptionsHandler::AutofillOptionsHandler() | |
265 : personal_data_(NULL) { | |
266 } | |
267 | |
268 AutofillOptionsHandler::~AutofillOptionsHandler() { | |
269 if (personal_data_) | |
270 personal_data_->RemoveObserver(this); | |
271 } | |
272 | |
273 ///////////////////////////////////////////////////////////////////////////// | |
274 // OptionsPageUIHandler implementation: | |
275 void AutofillOptionsHandler::GetLocalizedValues( | |
276 DictionaryValue* localized_strings) { | |
277 DCHECK(localized_strings); | |
278 | |
279 static OptionsStringResource resources[] = { | |
280 { "autofillAddresses", IDS_AUTOFILL_ADDRESSES_GROUP_NAME }, | |
281 { "autofillCreditCards", IDS_AUTOFILL_CREDITCARDS_GROUP_NAME }, | |
282 { "autofillAddAddress", IDS_AUTOFILL_ADD_ADDRESS_BUTTON }, | |
283 { "autofillAddCreditCard", IDS_AUTOFILL_ADD_CREDITCARD_BUTTON }, | |
284 { "autofillEditProfileButton", IDS_AUTOFILL_EDIT_PROFILE_BUTTON }, | |
285 { "helpButton", IDS_AUTOFILL_HELP_LABEL }, | |
286 { "addAddressTitle", IDS_AUTOFILL_ADD_ADDRESS_CAPTION }, | |
287 { "editAddressTitle", IDS_AUTOFILL_EDIT_ADDRESS_CAPTION }, | |
288 { "addCreditCardTitle", IDS_AUTOFILL_ADD_CREDITCARD_CAPTION }, | |
289 { "editCreditCardTitle", IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION }, | |
290 #if defined(OS_MACOSX) | |
291 { "auxiliaryProfilesEnabled", IDS_AUTOFILL_USE_MAC_ADDRESS_BOOK }, | |
292 #endif // defined(OS_MACOSX) | |
293 }; | |
294 | |
295 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
296 RegisterTitle(localized_strings, "autofillOptionsPage", | |
297 IDS_AUTOFILL_OPTIONS_TITLE); | |
298 | |
299 localized_strings->SetString("helpUrl", chrome::kAutofillHelpURL); | |
300 SetAddressOverlayStrings(localized_strings); | |
301 SetCreditCardOverlayStrings(localized_strings); | |
302 } | |
303 | |
304 void AutofillOptionsHandler::InitializeHandler() { | |
305 personal_data_ = PersonalDataManagerFactory::GetForProfile( | |
306 Profile::FromWebUI(web_ui())); | |
307 // personal_data_ is NULL in guest mode on Chrome OS. | |
308 if (personal_data_) | |
309 personal_data_->SetObserver(this); | |
310 } | |
311 | |
312 void AutofillOptionsHandler::InitializePage() { | |
313 if (personal_data_) | |
314 LoadAutofillData(); | |
315 } | |
316 | |
317 void AutofillOptionsHandler::RegisterMessages() { | |
318 web_ui()->RegisterMessageCallback( | |
319 "removeAddress", | |
320 base::Bind(&AutofillOptionsHandler::RemoveAddress, | |
321 base::Unretained(this))); | |
322 web_ui()->RegisterMessageCallback( | |
323 "removeCreditCard", | |
324 base::Bind(&AutofillOptionsHandler::RemoveCreditCard, | |
325 base::Unretained(this))); | |
326 web_ui()->RegisterMessageCallback( | |
327 "loadAddressEditor", | |
328 base::Bind(&AutofillOptionsHandler::LoadAddressEditor, | |
329 base::Unretained(this))); | |
330 web_ui()->RegisterMessageCallback( | |
331 "loadCreditCardEditor", | |
332 base::Bind(&AutofillOptionsHandler::LoadCreditCardEditor, | |
333 base::Unretained(this))); | |
334 web_ui()->RegisterMessageCallback( | |
335 "setAddress", | |
336 base::Bind(&AutofillOptionsHandler::SetAddress, base::Unretained(this))); | |
337 web_ui()->RegisterMessageCallback( | |
338 "setCreditCard", | |
339 base::Bind(&AutofillOptionsHandler::SetCreditCard, | |
340 base::Unretained(this))); | |
341 web_ui()->RegisterMessageCallback( | |
342 "validatePhoneNumbers", | |
343 base::Bind(&AutofillOptionsHandler::ValidatePhoneNumbers, | |
344 base::Unretained(this))); | |
345 } | |
346 | |
347 ///////////////////////////////////////////////////////////////////////////// | |
348 // PersonalDataManagerObserver implementation: | |
349 void AutofillOptionsHandler::OnPersonalDataChanged() { | |
350 LoadAutofillData(); | |
351 } | |
352 | |
353 void AutofillOptionsHandler::SetAddressOverlayStrings( | |
354 DictionaryValue* localized_strings) { | |
355 localized_strings->SetString("autofillEditAddressTitle", | |
356 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION)); | |
357 localized_strings->SetString("autofillFirstNameLabel", | |
358 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_FIRST_NAME)); | |
359 localized_strings->SetString("autofillMiddleNameLabel", | |
360 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MIDDLE_NAME)); | |
361 localized_strings->SetString("autofillLastNameLabel", | |
362 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_LAST_NAME)); | |
363 localized_strings->SetString("autofillCompanyNameLabel", | |
364 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_COMPANY_NAME)); | |
365 localized_strings->SetString("autofillAddrLine1Label", | |
366 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADDRESS_LINE_1)); | |
367 localized_strings->SetString("autofillAddrLine2Label", | |
368 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADDRESS_LINE_2)); | |
369 localized_strings->SetString("autofillCityLabel", | |
370 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CITY)); | |
371 localized_strings->SetString("autofillCountryLabel", | |
372 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_COUNTRY)); | |
373 localized_strings->SetString("autofillPhoneLabel", | |
374 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PHONE)); | |
375 localized_strings->SetString("autofillEmailLabel", | |
376 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EMAIL)); | |
377 localized_strings->SetString("autofillAddFirstNamePlaceholder", | |
378 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_FIRST_NAME)); | |
379 localized_strings->SetString("autofillAddMiddleNamePlaceholder", | |
380 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_MIDDLE_NAME)); | |
381 localized_strings->SetString("autofillAddLastNamePlaceholder", | |
382 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_LAST_NAME)); | |
383 localized_strings->SetString("autofillAddPhonePlaceholder", | |
384 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_PHONE)); | |
385 localized_strings->SetString("autofillAddEmailPlaceholder", | |
386 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_EMAIL)); | |
387 | |
388 std::string app_locale = AutofillCountry::ApplicationLocale(); | |
389 std::string default_country_code = | |
390 AutofillCountry::CountryCodeForLocale(app_locale); | |
391 localized_strings->SetString("defaultCountryCode", default_country_code); | |
392 localized_strings->Set("autofillCountryData", GetCountryData()); | |
393 } | |
394 | |
395 void AutofillOptionsHandler::SetCreditCardOverlayStrings( | |
396 DictionaryValue* localized_strings) { | |
397 localized_strings->SetString("autofillEditCreditCardTitle", | |
398 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION)); | |
399 localized_strings->SetString("nameOnCardLabel", | |
400 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NAME_ON_CARD)); | |
401 localized_strings->SetString("creditCardNumberLabel", | |
402 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CREDIT_CARD_NUMBER)); | |
403 localized_strings->SetString("creditCardExpirationDateLabel", | |
404 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EXPIRATION_DATE)); | |
405 } | |
406 | |
407 void AutofillOptionsHandler::LoadAutofillData() { | |
408 if (!IsPersonalDataLoaded()) | |
409 return; | |
410 | |
411 ListValue addresses; | |
412 for (std::vector<AutofillProfile*>::const_iterator i = | |
413 personal_data_->web_profiles().begin(); | |
414 i != personal_data_->web_profiles().end(); ++i) { | |
415 ListValue* entry = new ListValue(); | |
416 entry->Append(new StringValue((*i)->guid())); | |
417 entry->Append(new StringValue((*i)->Label())); | |
418 addresses.Append(entry); | |
419 } | |
420 | |
421 web_ui()->CallJavascriptFunction("AutofillOptions.setAddressList", addresses); | |
422 | |
423 ListValue credit_cards; | |
424 for (std::vector<CreditCard*>::const_iterator i = | |
425 personal_data_->credit_cards().begin(); | |
426 i != personal_data_->credit_cards().end(); ++i) { | |
427 ListValue* entry = new ListValue(); | |
428 entry->Append(new StringValue((*i)->guid())); | |
429 entry->Append(new StringValue((*i)->Label())); | |
430 int res = CreditCardTypeToResourceID((*i)->type()); | |
431 entry->Append( | |
432 new StringValue(web_ui_util::GetImageDataUrlFromResource(res))); | |
433 entry->Append(new StringValue(LocalizedCreditCardType((*i)->type()))); | |
434 credit_cards.Append(entry); | |
435 } | |
436 | |
437 web_ui()->CallJavascriptFunction("AutofillOptions.setCreditCardList", | |
438 credit_cards); | |
439 } | |
440 | |
441 void AutofillOptionsHandler::RemoveAddress(const ListValue* args) { | |
442 DCHECK(IsPersonalDataLoaded()); | |
443 | |
444 std::string guid; | |
445 if (!args->GetString(0, &guid)) { | |
446 NOTREACHED(); | |
447 return; | |
448 } | |
449 | |
450 personal_data_->RemoveProfile(guid); | |
451 } | |
452 | |
453 void AutofillOptionsHandler::RemoveCreditCard(const ListValue* args) { | |
454 DCHECK(IsPersonalDataLoaded()); | |
455 | |
456 std::string guid; | |
457 if (!args->GetString(0, &guid)) { | |
458 NOTREACHED(); | |
459 return; | |
460 } | |
461 | |
462 personal_data_->RemoveCreditCard(guid); | |
463 } | |
464 | |
465 void AutofillOptionsHandler::LoadAddressEditor(const ListValue* args) { | |
466 DCHECK(IsPersonalDataLoaded()); | |
467 | |
468 std::string guid; | |
469 if (!args->GetString(0, &guid)) { | |
470 NOTREACHED(); | |
471 return; | |
472 } | |
473 | |
474 AutofillProfile* profile = personal_data_->GetProfileByGUID(guid); | |
475 if (!profile) { | |
476 // There is a race where a user can click once on the close button and | |
477 // quickly click again on the list item before the item is removed (since | |
478 // the list is not updated until the model tells the list an item has been | |
479 // removed). This will activate the editor for a profile that has been | |
480 // removed. Do nothing in that case. | |
481 return; | |
482 } | |
483 | |
484 DictionaryValue address; | |
485 address.SetString("guid", profile->guid()); | |
486 scoped_ptr<ListValue> list; | |
487 GetNameList(*profile, &list); | |
488 address.Set("fullName", list.release()); | |
489 address.SetString("companyName", profile->GetInfo(COMPANY_NAME)); | |
490 address.SetString("addrLine1", profile->GetInfo(ADDRESS_HOME_LINE1)); | |
491 address.SetString("addrLine2", profile->GetInfo(ADDRESS_HOME_LINE2)); | |
492 address.SetString("city", profile->GetInfo(ADDRESS_HOME_CITY)); | |
493 address.SetString("state", profile->GetInfo(ADDRESS_HOME_STATE)); | |
494 address.SetString("postalCode", profile->GetInfo(ADDRESS_HOME_ZIP)); | |
495 address.SetString("country", profile->CountryCode()); | |
496 GetValueList(*profile, PHONE_HOME_WHOLE_NUMBER, &list); | |
497 address.Set("phone", list.release()); | |
498 GetValueList(*profile, EMAIL_ADDRESS, &list); | |
499 address.Set("email", list.release()); | |
500 | |
501 web_ui()->CallJavascriptFunction("AutofillOptions.editAddress", address); | |
502 } | |
503 | |
504 void AutofillOptionsHandler::LoadCreditCardEditor(const ListValue* args) { | |
505 DCHECK(IsPersonalDataLoaded()); | |
506 | |
507 std::string guid; | |
508 if (!args->GetString(0, &guid)) { | |
509 NOTREACHED(); | |
510 return; | |
511 } | |
512 | |
513 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid); | |
514 if (!credit_card) { | |
515 // There is a race where a user can click once on the close button and | |
516 // quickly click again on the list item before the item is removed (since | |
517 // the list is not updated until the model tells the list an item has been | |
518 // removed). This will activate the editor for a profile that has been | |
519 // removed. Do nothing in that case. | |
520 return; | |
521 } | |
522 | |
523 DictionaryValue credit_card_data; | |
524 credit_card_data.SetString("guid", credit_card->guid()); | |
525 credit_card_data.SetString("nameOnCard", | |
526 credit_card->GetInfo(CREDIT_CARD_NAME)); | |
527 credit_card_data.SetString("creditCardNumber", | |
528 credit_card->GetInfo(CREDIT_CARD_NUMBER)); | |
529 credit_card_data.SetString("expirationMonth", | |
530 credit_card->GetInfo(CREDIT_CARD_EXP_MONTH)); | |
531 credit_card_data.SetString( | |
532 "expirationYear", | |
533 credit_card->GetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | |
534 | |
535 web_ui()->CallJavascriptFunction("AutofillOptions.editCreditCard", | |
536 credit_card_data); | |
537 } | |
538 | |
539 void AutofillOptionsHandler::SetAddress(const ListValue* args) { | |
540 if (!IsPersonalDataLoaded()) | |
541 return; | |
542 | |
543 std::string guid; | |
544 if (!args->GetString(0, &guid)) { | |
545 NOTREACHED(); | |
546 return; | |
547 } | |
548 | |
549 AutofillProfile profile(guid); | |
550 | |
551 std::string country_code; | |
552 string16 value; | |
553 ListValue* list_value; | |
554 if (args->GetList(1, &list_value)) | |
555 SetNameList(list_value, &profile); | |
556 if (args->GetString(2, &value)) | |
557 profile.SetInfo(COMPANY_NAME, value); | |
558 if (args->GetString(3, &value)) | |
559 profile.SetInfo(ADDRESS_HOME_LINE1, value); | |
560 if (args->GetString(4, &value)) | |
561 profile.SetInfo(ADDRESS_HOME_LINE2, value); | |
562 if (args->GetString(5, &value)) | |
563 profile.SetInfo(ADDRESS_HOME_CITY, value); | |
564 if (args->GetString(6, &value)) | |
565 profile.SetInfo(ADDRESS_HOME_STATE, value); | |
566 if (args->GetString(7, &value)) | |
567 profile.SetInfo(ADDRESS_HOME_ZIP, value); | |
568 if (args->GetString(8, &country_code)) | |
569 profile.SetCountryCode(country_code); | |
570 if (args->GetList(9, &list_value)) | |
571 SetValueList(list_value, PHONE_HOME_WHOLE_NUMBER, &profile); | |
572 if (args->GetList(10, &list_value)) | |
573 SetValueList(list_value, EMAIL_ADDRESS, &profile); | |
574 | |
575 if (!base::IsValidGUID(profile.guid())) { | |
576 profile.set_guid(base::GenerateGUID()); | |
577 personal_data_->AddProfile(profile); | |
578 } else { | |
579 personal_data_->UpdateProfile(profile); | |
580 } | |
581 } | |
582 | |
583 void AutofillOptionsHandler::SetCreditCard(const ListValue* args) { | |
584 if (!IsPersonalDataLoaded()) | |
585 return; | |
586 | |
587 std::string guid; | |
588 if (!args->GetString(0, &guid)) { | |
589 NOTREACHED(); | |
590 return; | |
591 } | |
592 | |
593 CreditCard credit_card(guid); | |
594 | |
595 string16 value; | |
596 if (args->GetString(1, &value)) | |
597 credit_card.SetInfo(CREDIT_CARD_NAME, value); | |
598 if (args->GetString(2, &value)) | |
599 credit_card.SetInfo(CREDIT_CARD_NUMBER, value); | |
600 if (args->GetString(3, &value)) | |
601 credit_card.SetInfo(CREDIT_CARD_EXP_MONTH, value); | |
602 if (args->GetString(4, &value)) | |
603 credit_card.SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, value); | |
604 | |
605 if (!base::IsValidGUID(credit_card.guid())) { | |
606 credit_card.set_guid(base::GenerateGUID()); | |
607 personal_data_->AddCreditCard(credit_card); | |
608 } else { | |
609 personal_data_->UpdateCreditCard(credit_card); | |
610 } | |
611 } | |
612 | |
613 void AutofillOptionsHandler::ValidatePhoneNumbers(const ListValue* args) { | |
614 if (!IsPersonalDataLoaded()) | |
615 return; | |
616 | |
617 ListValue* list_value = NULL; | |
618 ValidatePhoneArguments(args, &list_value); | |
619 | |
620 web_ui()->CallJavascriptFunction( | |
621 "AutofillEditAddressOverlay.setValidatedPhoneNumbers", *list_value); | |
622 } | |
623 | |
624 bool AutofillOptionsHandler::IsPersonalDataLoaded() const { | |
625 return personal_data_ && personal_data_->IsDataLoaded(); | |
626 } | |
627 | |
628 } // namespace options2 | |
OLD | NEW |