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_handler.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, AutofillProfile* profile) { | |
162 const size_t size = names->GetSize(); | |
163 std::vector<string16> first_names(size); | |
164 std::vector<string16> middle_names(size); | |
165 std::vector<string16> last_names(size); | |
166 | |
167 for (size_t i = 0; i < size; ++i) { | |
168 const ListValue* name; | |
169 bool success = names->GetList(i, &name); | |
170 DCHECK(success); | |
171 | |
172 string16 first_name; | |
173 success = name->GetString(0, &first_name); | |
174 DCHECK(success); | |
175 first_names[i] = first_name; | |
176 | |
177 string16 middle_name; | |
178 success = name->GetString(1, &middle_name); | |
179 DCHECK(success); | |
180 middle_names[i] = middle_name; | |
181 | |
182 string16 last_name; | |
183 success = name->GetString(2, &last_name); | |
184 DCHECK(success); | |
185 last_names[i] = last_name; | |
186 } | |
187 | |
188 profile->SetMultiInfo(NAME_FIRST, first_names); | |
189 profile->SetMultiInfo(NAME_MIDDLE, middle_names); | |
190 profile->SetMultiInfo(NAME_LAST, last_names); | |
191 } | |
192 | |
193 // Pulls the phone number |index|, |phone_number_list|, and |country_code| from | |
194 // the |args| input. | |
195 void ExtractPhoneNumberInformation(const ListValue* args, | |
196 size_t* index, | |
197 const ListValue** phone_number_list, | |
198 std::string* country_code) { | |
199 // Retrieve index as a |double|, as that is how it comes across from | |
200 // JavaScript. | |
201 double number = 0.0; | |
202 if (!args->GetDouble(0, &number)) { | |
203 NOTREACHED(); | |
204 return; | |
205 } | |
206 *index = number; | |
207 | |
208 if (!args->GetList(1, phone_number_list)) { | |
209 NOTREACHED(); | |
210 return; | |
211 } | |
212 | |
213 if (!args->GetString(2, country_code)) { | |
214 NOTREACHED(); | |
215 return; | |
216 } | |
217 } | |
218 | |
219 // Searches the |list| for the value at |index|. If this value is present | |
220 // in any of the rest of the list, then the item (at |index|) is removed. | |
221 // The comparison of phone number values is done on normalized versions of the | |
222 // phone number values. | |
223 void RemoveDuplicatePhoneNumberAtIndex(size_t index, | |
224 const std::string& country_code, | |
225 ListValue* list) { | |
226 string16 new_value; | |
227 if (!list->GetString(index, &new_value)) { | |
228 NOTREACHED() << "List should have a value at index " << index; | |
229 return; | |
230 } | |
231 | |
232 bool is_duplicate = false; | |
233 for (size_t i = 0; i < list->GetSize() && !is_duplicate; ++i) { | |
234 if (i == index) | |
235 continue; | |
236 | |
237 string16 existing_value; | |
238 if (!list->GetString(i, &existing_value)) { | |
239 NOTREACHED() << "List should have a value at index " << i; | |
240 continue; | |
241 } | |
242 is_duplicate = autofill_i18n::PhoneNumbersMatch(new_value, | |
243 existing_value, | |
244 country_code); | |
245 } | |
246 | |
247 if (is_duplicate) | |
248 list->Remove(index, NULL); | |
249 } | |
250 | |
251 scoped_ptr<ListValue> ValidatePhoneArguments(const ListValue* args) { | |
252 size_t index = 0; | |
253 std::string country_code; | |
254 const ListValue* extracted_list = NULL; | |
255 ExtractPhoneNumberInformation(args, &index, &extracted_list, &country_code); | |
256 | |
257 scoped_ptr<ListValue> list(extracted_list->DeepCopy()); | |
258 RemoveDuplicatePhoneNumberAtIndex(index, country_code, list.get()); | |
259 return list.Pass(); | |
260 } | |
261 | |
262 } // namespace | |
263 | |
264 namespace options { | |
265 | |
266 AutofillOptionsHandler::AutofillOptionsHandler() | |
267 : personal_data_(NULL) { | |
268 } | |
269 | |
270 AutofillOptionsHandler::~AutofillOptionsHandler() { | |
271 if (personal_data_) | |
272 personal_data_->RemoveObserver(this); | |
273 } | |
274 | |
275 ///////////////////////////////////////////////////////////////////////////// | |
276 // OptionsPageUIHandler implementation: | |
277 void AutofillOptionsHandler::GetLocalizedValues( | |
278 DictionaryValue* localized_strings) { | |
279 DCHECK(localized_strings); | |
280 | |
281 static OptionsStringResource resources[] = { | |
282 { "autofillAddresses", IDS_AUTOFILL_ADDRESSES_GROUP_NAME }, | |
283 { "autofillCreditCards", IDS_AUTOFILL_CREDITCARDS_GROUP_NAME }, | |
284 { "autofillAddAddress", IDS_AUTOFILL_ADD_ADDRESS_BUTTON }, | |
285 { "autofillAddCreditCard", IDS_AUTOFILL_ADD_CREDITCARD_BUTTON }, | |
286 { "autofillEditProfileButton", IDS_AUTOFILL_EDIT_PROFILE_BUTTON }, | |
287 { "helpButton", IDS_AUTOFILL_HELP_LABEL }, | |
288 { "addAddressTitle", IDS_AUTOFILL_ADD_ADDRESS_CAPTION }, | |
289 { "editAddressTitle", IDS_AUTOFILL_EDIT_ADDRESS_CAPTION }, | |
290 { "addCreditCardTitle", IDS_AUTOFILL_ADD_CREDITCARD_CAPTION }, | |
291 { "editCreditCardTitle", IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION }, | |
292 #if defined(OS_MACOSX) | |
293 { "auxiliaryProfilesEnabled", IDS_AUTOFILL_USE_MAC_ADDRESS_BOOK }, | |
294 #endif // defined(OS_MACOSX) | |
295 }; | |
296 | |
297 RegisterStrings(localized_strings, resources, arraysize(resources)); | |
298 RegisterTitle(localized_strings, "autofillOptionsPage", | |
299 IDS_AUTOFILL_OPTIONS_TITLE); | |
300 | |
301 localized_strings->SetString("helpUrl", chrome::kAutofillHelpURL); | |
302 SetAddressOverlayStrings(localized_strings); | |
303 SetCreditCardOverlayStrings(localized_strings); | |
304 } | |
305 | |
306 void AutofillOptionsHandler::InitializeHandler() { | |
307 personal_data_ = PersonalDataManagerFactory::GetForProfile( | |
308 Profile::FromWebUI(web_ui())); | |
309 // personal_data_ is NULL in guest mode on Chrome OS. | |
310 if (personal_data_) | |
311 personal_data_->SetObserver(this); | |
312 } | |
313 | |
314 void AutofillOptionsHandler::InitializePage() { | |
315 if (personal_data_) | |
316 LoadAutofillData(); | |
317 } | |
318 | |
319 void AutofillOptionsHandler::RegisterMessages() { | |
320 web_ui()->RegisterMessageCallback( | |
321 "removeAddress", | |
322 base::Bind(&AutofillOptionsHandler::RemoveAddress, | |
323 base::Unretained(this))); | |
324 web_ui()->RegisterMessageCallback( | |
325 "removeCreditCard", | |
326 base::Bind(&AutofillOptionsHandler::RemoveCreditCard, | |
327 base::Unretained(this))); | |
328 web_ui()->RegisterMessageCallback( | |
329 "loadAddressEditor", | |
330 base::Bind(&AutofillOptionsHandler::LoadAddressEditor, | |
331 base::Unretained(this))); | |
332 web_ui()->RegisterMessageCallback( | |
333 "loadCreditCardEditor", | |
334 base::Bind(&AutofillOptionsHandler::LoadCreditCardEditor, | |
335 base::Unretained(this))); | |
336 web_ui()->RegisterMessageCallback( | |
337 "setAddress", | |
338 base::Bind(&AutofillOptionsHandler::SetAddress, base::Unretained(this))); | |
339 web_ui()->RegisterMessageCallback( | |
340 "setCreditCard", | |
341 base::Bind(&AutofillOptionsHandler::SetCreditCard, | |
342 base::Unretained(this))); | |
343 web_ui()->RegisterMessageCallback( | |
344 "validatePhoneNumbers", | |
345 base::Bind(&AutofillOptionsHandler::ValidatePhoneNumbers, | |
346 base::Unretained(this))); | |
347 } | |
348 | |
349 ///////////////////////////////////////////////////////////////////////////// | |
350 // PersonalDataManagerObserver implementation: | |
351 void AutofillOptionsHandler::OnPersonalDataChanged() { | |
352 LoadAutofillData(); | |
353 } | |
354 | |
355 void AutofillOptionsHandler::SetAddressOverlayStrings( | |
356 DictionaryValue* localized_strings) { | |
357 localized_strings->SetString("autofillEditAddressTitle", | |
358 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION)); | |
359 localized_strings->SetString("autofillFirstNameLabel", | |
360 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_FIRST_NAME)); | |
361 localized_strings->SetString("autofillMiddleNameLabel", | |
362 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_MIDDLE_NAME)); | |
363 localized_strings->SetString("autofillLastNameLabel", | |
364 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_LAST_NAME)); | |
365 localized_strings->SetString("autofillCompanyNameLabel", | |
366 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_COMPANY_NAME)); | |
367 localized_strings->SetString("autofillAddrLine1Label", | |
368 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADDRESS_LINE_1)); | |
369 localized_strings->SetString("autofillAddrLine2Label", | |
370 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADDRESS_LINE_2)); | |
371 localized_strings->SetString("autofillCityLabel", | |
372 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CITY)); | |
373 localized_strings->SetString("autofillCountryLabel", | |
374 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_COUNTRY)); | |
375 localized_strings->SetString("autofillPhoneLabel", | |
376 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PHONE)); | |
377 localized_strings->SetString("autofillEmailLabel", | |
378 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EMAIL)); | |
379 localized_strings->SetString("autofillAddFirstNamePlaceholder", | |
380 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_FIRST_NAME)); | |
381 localized_strings->SetString("autofillAddMiddleNamePlaceholder", | |
382 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_MIDDLE_NAME)); | |
383 localized_strings->SetString("autofillAddLastNamePlaceholder", | |
384 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_LAST_NAME)); | |
385 localized_strings->SetString("autofillAddPhonePlaceholder", | |
386 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_PHONE)); | |
387 localized_strings->SetString("autofillAddEmailPlaceholder", | |
388 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADD_EMAIL)); | |
389 | |
390 std::string app_locale = AutofillCountry::ApplicationLocale(); | |
391 std::string default_country_code = | |
392 AutofillCountry::CountryCodeForLocale(app_locale); | |
393 localized_strings->SetString("defaultCountryCode", default_country_code); | |
394 localized_strings->Set("autofillCountryData", GetCountryData()); | |
395 } | |
396 | |
397 void AutofillOptionsHandler::SetCreditCardOverlayStrings( | |
398 DictionaryValue* localized_strings) { | |
399 localized_strings->SetString("autofillEditCreditCardTitle", | |
400 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION)); | |
401 localized_strings->SetString("nameOnCardLabel", | |
402 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NAME_ON_CARD)); | |
403 localized_strings->SetString("creditCardNumberLabel", | |
404 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CREDIT_CARD_NUMBER)); | |
405 localized_strings->SetString("creditCardExpirationDateLabel", | |
406 l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EXPIRATION_DATE)); | |
407 } | |
408 | |
409 void AutofillOptionsHandler::LoadAutofillData() { | |
410 if (!IsPersonalDataLoaded()) | |
411 return; | |
412 | |
413 ListValue addresses; | |
414 for (std::vector<AutofillProfile*>::const_iterator i = | |
415 personal_data_->web_profiles().begin(); | |
416 i != personal_data_->web_profiles().end(); ++i) { | |
417 ListValue* entry = new ListValue(); | |
418 entry->Append(new StringValue((*i)->guid())); | |
419 entry->Append(new StringValue((*i)->Label())); | |
420 addresses.Append(entry); | |
421 } | |
422 | |
423 web_ui()->CallJavascriptFunction("AutofillOptions.setAddressList", addresses); | |
424 | |
425 ListValue credit_cards; | |
426 for (std::vector<CreditCard*>::const_iterator i = | |
427 personal_data_->credit_cards().begin(); | |
428 i != personal_data_->credit_cards().end(); ++i) { | |
429 ListValue* entry = new ListValue(); | |
430 entry->Append(new StringValue((*i)->guid())); | |
431 entry->Append(new StringValue((*i)->Label())); | |
432 int res = CreditCardTypeToResourceID((*i)->type()); | |
433 entry->Append( | |
434 new StringValue(web_ui_util::GetImageDataUrlFromResource(res))); | |
435 entry->Append(new StringValue(LocalizedCreditCardType((*i)->type()))); | |
436 credit_cards.Append(entry); | |
437 } | |
438 | |
439 web_ui()->CallJavascriptFunction("AutofillOptions.setCreditCardList", | |
440 credit_cards); | |
441 } | |
442 | |
443 void AutofillOptionsHandler::RemoveAddress(const ListValue* args) { | |
444 DCHECK(IsPersonalDataLoaded()); | |
445 | |
446 std::string guid; | |
447 if (!args->GetString(0, &guid)) { | |
448 NOTREACHED(); | |
449 return; | |
450 } | |
451 | |
452 personal_data_->RemoveProfile(guid); | |
453 } | |
454 | |
455 void AutofillOptionsHandler::RemoveCreditCard(const ListValue* args) { | |
456 DCHECK(IsPersonalDataLoaded()); | |
457 | |
458 std::string guid; | |
459 if (!args->GetString(0, &guid)) { | |
460 NOTREACHED(); | |
461 return; | |
462 } | |
463 | |
464 personal_data_->RemoveCreditCard(guid); | |
465 } | |
466 | |
467 void AutofillOptionsHandler::LoadAddressEditor(const ListValue* args) { | |
468 DCHECK(IsPersonalDataLoaded()); | |
469 | |
470 std::string guid; | |
471 if (!args->GetString(0, &guid)) { | |
472 NOTREACHED(); | |
473 return; | |
474 } | |
475 | |
476 AutofillProfile* profile = personal_data_->GetProfileByGUID(guid); | |
477 if (!profile) { | |
478 // There is a race where a user can click once on the close button and | |
479 // quickly click again on the list item before the item is removed (since | |
480 // the list is not updated until the model tells the list an item has been | |
481 // removed). This will activate the editor for a profile that has been | |
482 // removed. Do nothing in that case. | |
483 return; | |
484 } | |
485 | |
486 DictionaryValue address; | |
487 address.SetString("guid", profile->guid()); | |
488 scoped_ptr<ListValue> list; | |
489 GetNameList(*profile, &list); | |
490 address.Set("fullName", list.release()); | |
491 address.SetString("companyName", profile->GetInfo(COMPANY_NAME)); | |
492 address.SetString("addrLine1", profile->GetInfo(ADDRESS_HOME_LINE1)); | |
493 address.SetString("addrLine2", profile->GetInfo(ADDRESS_HOME_LINE2)); | |
494 address.SetString("city", profile->GetInfo(ADDRESS_HOME_CITY)); | |
495 address.SetString("state", profile->GetInfo(ADDRESS_HOME_STATE)); | |
496 address.SetString("postalCode", profile->GetInfo(ADDRESS_HOME_ZIP)); | |
497 address.SetString("country", profile->CountryCode()); | |
498 GetValueList(*profile, PHONE_HOME_WHOLE_NUMBER, &list); | |
499 address.Set("phone", list.release()); | |
500 GetValueList(*profile, EMAIL_ADDRESS, &list); | |
501 address.Set("email", list.release()); | |
502 | |
503 web_ui()->CallJavascriptFunction("AutofillOptions.editAddress", address); | |
504 } | |
505 | |
506 void AutofillOptionsHandler::LoadCreditCardEditor(const ListValue* args) { | |
507 DCHECK(IsPersonalDataLoaded()); | |
508 | |
509 std::string guid; | |
510 if (!args->GetString(0, &guid)) { | |
511 NOTREACHED(); | |
512 return; | |
513 } | |
514 | |
515 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid); | |
516 if (!credit_card) { | |
517 // There is a race where a user can click once on the close button and | |
518 // quickly click again on the list item before the item is removed (since | |
519 // the list is not updated until the model tells the list an item has been | |
520 // removed). This will activate the editor for a profile that has been | |
521 // removed. Do nothing in that case. | |
522 return; | |
523 } | |
524 | |
525 DictionaryValue credit_card_data; | |
526 credit_card_data.SetString("guid", credit_card->guid()); | |
527 credit_card_data.SetString("nameOnCard", | |
528 credit_card->GetInfo(CREDIT_CARD_NAME)); | |
529 credit_card_data.SetString("creditCardNumber", | |
530 credit_card->GetInfo(CREDIT_CARD_NUMBER)); | |
531 credit_card_data.SetString("expirationMonth", | |
532 credit_card->GetInfo(CREDIT_CARD_EXP_MONTH)); | |
533 credit_card_data.SetString( | |
534 "expirationYear", | |
535 credit_card->GetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR)); | |
536 | |
537 web_ui()->CallJavascriptFunction("AutofillOptions.editCreditCard", | |
538 credit_card_data); | |
539 } | |
540 | |
541 void AutofillOptionsHandler::SetAddress(const ListValue* args) { | |
542 if (!IsPersonalDataLoaded()) | |
543 return; | |
544 | |
545 std::string guid; | |
546 if (!args->GetString(0, &guid)) { | |
547 NOTREACHED(); | |
548 return; | |
549 } | |
550 | |
551 AutofillProfile profile(guid); | |
552 | |
553 std::string country_code; | |
554 string16 value; | |
555 const ListValue* list_value; | |
556 if (args->GetList(1, &list_value)) | |
557 SetNameList(list_value, &profile); | |
558 if (args->GetString(2, &value)) | |
559 profile.SetInfo(COMPANY_NAME, value); | |
560 if (args->GetString(3, &value)) | |
561 profile.SetInfo(ADDRESS_HOME_LINE1, value); | |
562 if (args->GetString(4, &value)) | |
563 profile.SetInfo(ADDRESS_HOME_LINE2, value); | |
564 if (args->GetString(5, &value)) | |
565 profile.SetInfo(ADDRESS_HOME_CITY, value); | |
566 if (args->GetString(6, &value)) | |
567 profile.SetInfo(ADDRESS_HOME_STATE, value); | |
568 if (args->GetString(7, &value)) | |
569 profile.SetInfo(ADDRESS_HOME_ZIP, value); | |
570 if (args->GetString(8, &country_code)) | |
571 profile.SetCountryCode(country_code); | |
572 if (args->GetList(9, &list_value)) | |
573 SetValueList(list_value, PHONE_HOME_WHOLE_NUMBER, &profile); | |
574 if (args->GetList(10, &list_value)) | |
575 SetValueList(list_value, EMAIL_ADDRESS, &profile); | |
576 | |
577 if (!base::IsValidGUID(profile.guid())) { | |
578 profile.set_guid(base::GenerateGUID()); | |
579 personal_data_->AddProfile(profile); | |
580 } else { | |
581 personal_data_->UpdateProfile(profile); | |
582 } | |
583 } | |
584 | |
585 void AutofillOptionsHandler::SetCreditCard(const ListValue* args) { | |
586 if (!IsPersonalDataLoaded()) | |
587 return; | |
588 | |
589 std::string guid; | |
590 if (!args->GetString(0, &guid)) { | |
591 NOTREACHED(); | |
592 return; | |
593 } | |
594 | |
595 CreditCard credit_card(guid); | |
596 | |
597 string16 value; | |
598 if (args->GetString(1, &value)) | |
599 credit_card.SetInfo(CREDIT_CARD_NAME, value); | |
600 if (args->GetString(2, &value)) | |
601 credit_card.SetInfo(CREDIT_CARD_NUMBER, value); | |
602 if (args->GetString(3, &value)) | |
603 credit_card.SetInfo(CREDIT_CARD_EXP_MONTH, value); | |
604 if (args->GetString(4, &value)) | |
605 credit_card.SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, value); | |
606 | |
607 if (!base::IsValidGUID(credit_card.guid())) { | |
608 credit_card.set_guid(base::GenerateGUID()); | |
609 personal_data_->AddCreditCard(credit_card); | |
610 } else { | |
611 personal_data_->UpdateCreditCard(credit_card); | |
612 } | |
613 } | |
614 | |
615 void AutofillOptionsHandler::ValidatePhoneNumbers(const ListValue* args) { | |
616 if (!IsPersonalDataLoaded()) | |
617 return; | |
618 | |
619 scoped_ptr<ListValue> list_value = ValidatePhoneArguments(args); | |
620 | |
621 web_ui()->CallJavascriptFunction( | |
622 "AutofillEditAddressOverlay.setValidatedPhoneNumbers", *list_value); | |
623 } | |
624 | |
625 bool AutofillOptionsHandler::IsPersonalDataLoaded() const { | |
626 return personal_data_ && personal_data_->IsDataLoaded(); | |
627 } | |
628 | |
629 } // namespace options | |
OLD | NEW |