1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
|
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/webui/options/autofill_options_handler.h"
#include <vector>
#include "base/logging.h"
#include "base/string16.h"
#include "base/string_number_conversions.h"
#include "base/utf_string_conversions.h"
#include "base/values.h"
#include "chrome/browser/autofill/autofill_country.h"
#include "chrome/browser/autofill/autofill_profile.h"
#include "chrome/browser/autofill/credit_card.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/webui/web_ui_util.h"
#include "chrome/common/guid.h"
#include "grit/generated_resources.h"
#include "grit/webkit_resources.h"
#include "ui/base/l10n/l10n_util.h"
namespace {
// Converts a credit card type to the appropriate resource ID of the CC icon.
int CreditCardTypeToResourceID(const string16& type16) {
std::string type = UTF16ToUTF8(type16);
if (type == kAmericanExpressCard)
return IDR_AUTOFILL_CC_AMEX;
else if (type == kDinersCard)
return IDR_AUTOFILL_CC_DINERS;
else if (type == kDiscoverCard)
return IDR_AUTOFILL_CC_DISCOVER;
else if (type == kGenericCard)
return IDR_AUTOFILL_CC_GENERIC;
else if (type == kJCBCard)
return IDR_AUTOFILL_CC_JCB;
else if (type == kMasterCard)
return IDR_AUTOFILL_CC_MASTERCARD;
else if (type == kSoloCard)
return IDR_AUTOFILL_CC_SOLO;
else if (type == kVisaCard)
return IDR_AUTOFILL_CC_VISA;
NOTREACHED();
return 0;
}
// Returns a dictionary that maps country codes to data for the country.
DictionaryValue* GetCountryData() {
std::string app_locale = AutofillCountry::ApplicationLocale();
std::vector<std::string> country_codes;
AutofillCountry::GetAvailableCountries(&country_codes);
DictionaryValue* country_data = new DictionaryValue();
for (size_t i = 0; i < country_codes.size(); ++i) {
const AutofillCountry country(country_codes[i], app_locale);
DictionaryValue* details = new DictionaryValue();
details->SetString("name", country.name());
details->SetString("postalCodeLabel", country.postal_code_label());
details->SetString("stateLabel", country.state_label());
country_data->Set(country.country_code(), details);
}
return country_data;
}
} // namespace
AutofillOptionsHandler::AutofillOptionsHandler()
: personal_data_(NULL) {
}
AutofillOptionsHandler::~AutofillOptionsHandler() {
if (personal_data_)
personal_data_->RemoveObserver(this);
}
/////////////////////////////////////////////////////////////////////////////
// OptionsPageUIHandler implementation:
void AutofillOptionsHandler::GetLocalizedValues(
DictionaryValue* localized_strings) {
DCHECK(localized_strings);
static OptionsStringResource resources[] = {
{ "autofillAddresses", IDS_AUTOFILL_ADDRESSES_GROUP_NAME },
{ "autofillCreditCards", IDS_AUTOFILL_CREDITCARDS_GROUP_NAME },
{ "autofillAddAddress", IDS_AUTOFILL_ADD_ADDRESS_BUTTON },
{ "autofillAddCreditCard", IDS_AUTOFILL_ADD_CREDITCARD_BUTTON },
{ "helpButton", IDS_AUTOFILL_HELP_LABEL },
{ "addAddressTitle", IDS_AUTOFILL_ADD_ADDRESS_CAPTION },
{ "editAddressTitle", IDS_AUTOFILL_EDIT_ADDRESS_CAPTION },
{ "addCreditCardTitle", IDS_AUTOFILL_ADD_CREDITCARD_CAPTION },
{ "editCreditCardTitle", IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION },
#if defined(OS_MACOSX)
{ "auxiliaryProfilesEnabled", IDS_AUTOFILL_USE_MAC_ADDRESS_BOOK },
#endif // defined(OS_MACOSX)
};
RegisterStrings(localized_strings, resources, arraysize(resources));
RegisterTitle(localized_strings, "autofillOptionsPage",
IDS_AUTOFILL_OPTIONS_TITLE);
SetAddressOverlayStrings(localized_strings);
SetCreditCardOverlayStrings(localized_strings);
}
void AutofillOptionsHandler::Initialize() {
personal_data_ = web_ui_->GetProfile()->GetPersonalDataManager();
personal_data_->SetObserver(this);
LoadAutofillData();
}
void AutofillOptionsHandler::RegisterMessages() {
web_ui_->RegisterMessageCallback(
"removeAddress",
NewCallback(this, &AutofillOptionsHandler::RemoveAddress));
web_ui_->RegisterMessageCallback(
"removeCreditCard",
NewCallback(this, &AutofillOptionsHandler::RemoveCreditCard));
web_ui_->RegisterMessageCallback(
"loadAddressEditor",
NewCallback(this, &AutofillOptionsHandler::LoadAddressEditor));
web_ui_->RegisterMessageCallback(
"loadCreditCardEditor",
NewCallback(this, &AutofillOptionsHandler::LoadCreditCardEditor));
web_ui_->RegisterMessageCallback(
"setAddress",
NewCallback(this, &AutofillOptionsHandler::SetAddress));
web_ui_->RegisterMessageCallback(
"setCreditCard",
NewCallback(this, &AutofillOptionsHandler::SetCreditCard));
}
/////////////////////////////////////////////////////////////////////////////
// PersonalDataManager::Observer implementation:
void AutofillOptionsHandler::OnPersonalDataLoaded() {
LoadAutofillData();
}
void AutofillOptionsHandler::OnPersonalDataChanged() {
LoadAutofillData();
}
void AutofillOptionsHandler::SetAddressOverlayStrings(
DictionaryValue* localized_strings) {
localized_strings->SetString("autofillEditAddressTitle",
l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION));
localized_strings->SetString("fullNameLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_FULL_NAME));
localized_strings->SetString("companyNameLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_COMPANY_NAME));
localized_strings->SetString("addrLine1Label",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADDRESS_LINE_1));
localized_strings->SetString("addrLine2Label",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_ADDRESS_LINE_2));
localized_strings->SetString("cityLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CITY));
localized_strings->SetString("countryLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_COUNTRY));
localized_strings->SetString("phoneLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_PHONE));
localized_strings->SetString("faxLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_FAX));
localized_strings->SetString("emailLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EMAIL));
std::string app_locale = AutofillCountry::ApplicationLocale();
std::string default_country_code =
AutofillCountry::CountryCodeForLocale(app_locale);
localized_strings->SetString("defaultCountryCode", default_country_code);
localized_strings->Set("autofillCountryData", GetCountryData());
}
void AutofillOptionsHandler::SetCreditCardOverlayStrings(
DictionaryValue* localized_strings) {
localized_strings->SetString("autofillEditCreditCardTitle",
l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION));
localized_strings->SetString("nameOnCardLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_NAME_ON_CARD));
localized_strings->SetString("creditCardNumberLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_CREDIT_CARD_NUMBER));
localized_strings->SetString("creditCardExpirationDateLabel",
l10n_util::GetStringUTF16(IDS_AUTOFILL_DIALOG_EXPIRATION_DATE));
}
void AutofillOptionsHandler::LoadAutofillData() {
if (!personal_data_->IsDataLoaded())
return;
ListValue addresses;
for (std::vector<AutofillProfile*>::const_iterator i =
personal_data_->web_profiles().begin();
i != personal_data_->web_profiles().end(); ++i) {
ListValue* entry = new ListValue();
entry->Append(new StringValue((*i)->guid()));
entry->Append(new StringValue((*i)->Label()));
addresses.Append(entry);
}
web_ui_->CallJavascriptFunction("AutofillOptions.setAddressList", addresses);
ListValue credit_cards;
for (std::vector<CreditCard*>::const_iterator i =
personal_data_->credit_cards().begin();
i != personal_data_->credit_cards().end(); ++i) {
ListValue* entry = new ListValue();
entry->Append(new StringValue((*i)->guid()));
entry->Append(new StringValue((*i)->Label()));
int res = CreditCardTypeToResourceID((*i)->type());
entry->Append(
new StringValue(web_ui_util::GetImageDataUrlFromResource(res)));
credit_cards.Append(entry);
}
web_ui_->CallJavascriptFunction("AutofillOptions.setCreditCardList",
credit_cards);
}
void AutofillOptionsHandler::RemoveAddress(const ListValue* args) {
DCHECK(personal_data_->IsDataLoaded());
std::string guid;
if (!args->GetString(0, &guid)) {
NOTREACHED();
return;
}
personal_data_->RemoveProfile(guid);
}
void AutofillOptionsHandler::RemoveCreditCard(const ListValue* args) {
DCHECK(personal_data_->IsDataLoaded());
std::string guid;
if (!args->GetString(0, &guid)) {
NOTREACHED();
return;
}
personal_data_->RemoveCreditCard(guid);
}
void AutofillOptionsHandler::LoadAddressEditor(const ListValue* args) {
DCHECK(personal_data_->IsDataLoaded());
std::string guid;
if (!args->GetString(0, &guid)) {
NOTREACHED();
return;
}
AutofillProfile* profile = personal_data_->GetProfileByGUID(guid);
if (!profile) {
// There is a race where a user can click once on the close button and
// quickly click again on the list item before the item is removed (since
// the list is not updated until the model tells the list an item has been
// removed). This will activate the editor for a profile that has been
// removed. Do nothing in that case.
return;
}
DictionaryValue address;
address.SetString("guid", profile->guid());
address.SetString("fullName", profile->GetInfo(NAME_FULL));
address.SetString("companyName", profile->GetInfo(COMPANY_NAME));
address.SetString("addrLine1", profile->GetInfo(ADDRESS_HOME_LINE1));
address.SetString("addrLine2", profile->GetInfo(ADDRESS_HOME_LINE2));
address.SetString("city", profile->GetInfo(ADDRESS_HOME_CITY));
address.SetString("state", profile->GetInfo(ADDRESS_HOME_STATE));
address.SetString("postalCode", profile->GetInfo(ADDRESS_HOME_ZIP));
address.SetString("country", profile->CountryCode());
address.SetString("phone", profile->GetInfo(PHONE_HOME_WHOLE_NUMBER));
address.SetString("fax", profile->GetInfo(PHONE_FAX_WHOLE_NUMBER));
address.SetString("email", profile->GetInfo(EMAIL_ADDRESS));
web_ui_->CallJavascriptFunction("AutofillOptions.editAddress", address);
}
void AutofillOptionsHandler::LoadCreditCardEditor(const ListValue* args) {
DCHECK(personal_data_->IsDataLoaded());
std::string guid;
if (!args->GetString(0, &guid)) {
NOTREACHED();
return;
}
CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid);
if (!credit_card) {
// There is a race where a user can click once on the close button and
// quickly click again on the list item before the item is removed (since
// the list is not updated until the model tells the list an item has been
// removed). This will activate the editor for a profile that has been
// removed. Do nothing in that case.
return;
}
DictionaryValue credit_card_data;
credit_card_data.SetString("guid", credit_card->guid());
credit_card_data.SetString("nameOnCard",
credit_card->GetInfo(CREDIT_CARD_NAME));
credit_card_data.SetString("creditCardNumber",
credit_card->GetInfo(CREDIT_CARD_NUMBER));
credit_card_data.SetString("expirationMonth",
credit_card->GetInfo(CREDIT_CARD_EXP_MONTH));
credit_card_data.SetString(
"expirationYear",
credit_card->GetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR));
web_ui_->CallJavascriptFunction("AutofillOptions.editCreditCard",
credit_card_data);
}
void AutofillOptionsHandler::SetAddress(const ListValue* args) {
if (!personal_data_->IsDataLoaded())
return;
std::string guid;
if (!args->GetString(0, &guid)) {
NOTREACHED();
return;
}
AutofillProfile profile(guid);
std::string country_code;
string16 value;
if (args->GetString(1, &value))
profile.SetInfo(NAME_FULL, value);
if (args->GetString(2, &value))
profile.SetInfo(COMPANY_NAME, value);
if (args->GetString(3, &value))
profile.SetInfo(ADDRESS_HOME_LINE1, value);
if (args->GetString(4, &value))
profile.SetInfo(ADDRESS_HOME_LINE2, value);
if (args->GetString(5, &value))
profile.SetInfo(ADDRESS_HOME_CITY, value);
if (args->GetString(6, &value))
profile.SetInfo(ADDRESS_HOME_STATE, value);
if (args->GetString(7, &value))
profile.SetInfo(ADDRESS_HOME_ZIP, value);
if (args->GetString(8, &country_code))
profile.SetCountryCode(country_code);
if (args->GetString(9, &value))
profile.SetInfo(PHONE_HOME_WHOLE_NUMBER, value);
if (args->GetString(10, &value))
profile.SetInfo(PHONE_FAX_WHOLE_NUMBER, value);
if (args->GetString(11, &value))
profile.SetInfo(EMAIL_ADDRESS, value);
if (!guid::IsValidGUID(profile.guid())) {
profile.set_guid(guid::GenerateGUID());
personal_data_->AddProfile(profile);
} else {
personal_data_->UpdateProfile(profile);
}
}
void AutofillOptionsHandler::SetCreditCard(const ListValue* args) {
if (!personal_data_->IsDataLoaded())
return;
std::string guid;
if (!args->GetString(0, &guid)) {
NOTREACHED();
return;
}
CreditCard credit_card(guid);
string16 value;
if (args->GetString(1, &value))
credit_card.SetInfo(CREDIT_CARD_NAME, value);
if (args->GetString(2, &value))
credit_card.SetInfo(CREDIT_CARD_NUMBER, value);
if (args->GetString(3, &value))
credit_card.SetInfo(CREDIT_CARD_EXP_MONTH, value);
if (args->GetString(4, &value))
credit_card.SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, value);
if (!guid::IsValidGUID(credit_card.guid())) {
credit_card.set_guid(guid::GenerateGUID());
personal_data_->AddCreditCard(credit_card);
} else {
personal_data_->UpdateCreditCard(credit_card);
}
}
|