Chromium Code Reviews

Side by Side Diff: chrome/browser/dom_ui/options/autofill_options_handler.cc

Issue 6034005: DOMUI: Implement the new-style Autofill options page. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Review fixes 2. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/dom_ui/options/autofill_options_handler.h" 5 #include "chrome/browser/dom_ui/options/autofill_options_handler.h"
6 6
7 #include <vector> 7 #include <vector>
8 8
9 #include "app/l10n_util.h" 9 #include "app/l10n_util.h"
10 #include "base/logging.h" 10 #include "base/logging.h"
(...skipping 16 matching lines...)
27 } 27 }
28 28
29 ///////////////////////////////////////////////////////////////////////////// 29 /////////////////////////////////////////////////////////////////////////////
30 // OptionsUIHandler implementation: 30 // OptionsUIHandler implementation:
31 void AutoFillOptionsHandler::GetLocalizedValues( 31 void AutoFillOptionsHandler::GetLocalizedValues(
32 DictionaryValue* localized_strings) { 32 DictionaryValue* localized_strings) {
33 DCHECK(localized_strings); 33 DCHECK(localized_strings);
34 34
35 localized_strings->SetString("autoFillOptionsTitle", 35 localized_strings->SetString("autoFillOptionsTitle",
36 l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_TITLE)); 36 l10n_util::GetStringUTF16(IDS_AUTOFILL_OPTIONS_TITLE));
37 localized_strings->SetString("autoFillEnabled", 37 localized_strings->SetString("autoFillAddresses",
38 l10n_util::GetStringUTF16(IDS_OPTIONS_AUTOFILL_ENABLE));
39 localized_strings->SetString("addressesHeader",
40 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESSES_GROUP_NAME)); 38 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADDRESSES_GROUP_NAME));
41 localized_strings->SetString("creditCardsHeader", 39 localized_strings->SetString("autoFillCreditCards",
42 l10n_util::GetStringUTF16(IDS_AUTOFILL_CREDITCARDS_GROUP_NAME)); 40 l10n_util::GetStringUTF16(IDS_AUTOFILL_CREDITCARDS_GROUP_NAME));
43 localized_strings->SetString("addAddressButton", 41 localized_strings->SetString("autoFillAddAddress",
44 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_BUTTON)); 42 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_BUTTON));
45 localized_strings->SetString("addCreditCardButton", 43 localized_strings->SetString("autoFillAddCreditCard",
46 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_BUTTON)); 44 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_BUTTON));
47 localized_strings->SetString("editButton",
48 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_BUTTON));
49 localized_strings->SetString("deleteButton",
50 l10n_util::GetStringUTF16(IDS_AUTOFILL_DELETE_BUTTON));
51 localized_strings->SetString("helpButton", 45 localized_strings->SetString("helpButton",
52 l10n_util::GetStringUTF16(IDS_AUTOFILL_HELP_LABEL)); 46 l10n_util::GetStringUTF16(IDS_AUTOFILL_HELP_LABEL));
53 localized_strings->SetString("addAddressTitle", 47 localized_strings->SetString("addAddressTitle",
54 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_CAPTION)); 48 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_CAPTION));
55 localized_strings->SetString("editAddressTitle", 49 localized_strings->SetString("editAddressTitle",
56 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION)); 50 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION));
57 localized_strings->SetString("addCreditCardTitle", 51 localized_strings->SetString("addCreditCardTitle",
58 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION)); 52 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION));
59 localized_strings->SetString("editCreditCardTitle", 53 localized_strings->SetString("editCreditCardTitle",
60 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION)); 54 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION));
61 55
62 SetAddressOverlayStrings(localized_strings); 56 SetAddressOverlayStrings(localized_strings);
63 SetCreditCardOverlayStrings(localized_strings); 57 SetCreditCardOverlayStrings(localized_strings);
64 } 58 }
65 59
66 void AutoFillOptionsHandler::Initialize() { 60 void AutoFillOptionsHandler::Initialize() {
67 personal_data_ = 61 personal_data_ = dom_ui_->GetProfile()->GetPersonalDataManager();
68 dom_ui_->GetProfile()->GetOriginalProfile()->GetPersonalDataManager();
69 personal_data_->SetObserver(this); 62 personal_data_->SetObserver(this);
70 63
71 LoadAutoFillData(); 64 LoadAutoFillData();
72 } 65 }
73 66
74 void AutoFillOptionsHandler::RegisterMessages() { 67 void AutoFillOptionsHandler::RegisterMessages() {
75 dom_ui_->RegisterMessageCallback( 68 dom_ui_->RegisterMessageCallback(
76 "updateAddress", 69 "removeAutoFillProfile",
77 NewCallback(this, &AutoFillOptionsHandler::UpdateAddress)); 70 NewCallback(this, &AutoFillOptionsHandler::RemoveAutoFillProfile));
78
79 dom_ui_->RegisterMessageCallback( 71 dom_ui_->RegisterMessageCallback(
80 "editAddress", 72 "loadProfileEditor",
81 NewCallback(this, &AutoFillOptionsHandler::EditAddress)); 73 NewCallback(this, &AutoFillOptionsHandler::LoadProfileEditor));
82
83 dom_ui_->RegisterMessageCallback( 74 dom_ui_->RegisterMessageCallback(
84 "removeAddress", 75 "setAddress",
85 NewCallback(this, &AutoFillOptionsHandler::RemoveAddress)); 76 NewCallback(this, &AutoFillOptionsHandler::SetAddress));
86
87 dom_ui_->RegisterMessageCallback( 77 dom_ui_->RegisterMessageCallback(
88 "updateCreditCard", 78 "setCreditCard",
89 NewCallback(this, &AutoFillOptionsHandler::UpdateCreditCard)); 79 NewCallback(this, &AutoFillOptionsHandler::SetCreditCard));
90
91 dom_ui_->RegisterMessageCallback(
92 "editCreditCard",
93 NewCallback(this, &AutoFillOptionsHandler::EditCreditCard));
94
95 dom_ui_->RegisterMessageCallback(
96 "removeCreditCard",
97 NewCallback(this, &AutoFillOptionsHandler::RemoveCreditCard));
98 } 80 }
99 81
100 ///////////////////////////////////////////////////////////////////////////// 82 /////////////////////////////////////////////////////////////////////////////
101 // PersonalDataManager::Observer implementation: 83 // PersonalDataManager::Observer implementation:
102 void AutoFillOptionsHandler::OnPersonalDataLoaded() { 84 void AutoFillOptionsHandler::OnPersonalDataLoaded() {
103 LoadAutoFillData(); 85 LoadAutoFillData();
104 } 86 }
105 87
106 void AutoFillOptionsHandler::OnPersonalDataChanged() { 88 void AutoFillOptionsHandler::OnPersonalDataChanged() {
107 LoadAutoFillData(); 89 LoadAutoFillData();
(...skipping 42 matching lines...)
150 } 132 }
151 133
152 void AutoFillOptionsHandler::LoadAutoFillData() { 134 void AutoFillOptionsHandler::LoadAutoFillData() {
153 if (!personal_data_->IsDataLoaded()) 135 if (!personal_data_->IsDataLoaded())
154 return; 136 return;
155 137
156 ListValue addresses; 138 ListValue addresses;
157 for (std::vector<AutoFillProfile*>::const_iterator i = 139 for (std::vector<AutoFillProfile*>::const_iterator i =
158 personal_data_->web_profiles().begin(); 140 personal_data_->web_profiles().begin();
159 i != personal_data_->web_profiles().end(); ++i) { 141 i != personal_data_->web_profiles().end(); ++i) {
160 DictionaryValue* address = new DictionaryValue(); 142 ListValue* entry = new ListValue();
161 address->SetString("label", (*i)->Label()); 143 entry->Append(new StringValue((*i)->guid()));
162 address->SetString("guid", (*i)->guid()); 144 entry->Append(new StringValue((*i)->Label()));
163 addresses.Append(address); 145 addresses.Append(entry);
164 } 146 }
165 147
166 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateAddresses", 148 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.setAddressList",
167 addresses); 149 addresses);
168 150
169 ListValue credit_cards; 151 ListValue credit_cards;
170 for (std::vector<CreditCard*>::const_iterator i = 152 for (std::vector<CreditCard*>::const_iterator i =
171 personal_data_->credit_cards().begin(); 153 personal_data_->credit_cards().begin();
172 i != personal_data_->credit_cards().end(); ++i) { 154 i != personal_data_->credit_cards().end(); ++i) {
173 DictionaryValue* credit_card = new DictionaryValue(); 155 ListValue* entry = new ListValue();
174 credit_card->SetString("label", (*i)->PreviewSummary()); 156 entry->Append(new StringValue((*i)->guid()));
175 credit_card->SetString("guid", (*i)->guid()); 157 entry->Append(new StringValue((*i)->PreviewSummary()));
176 credit_cards.Append(credit_card); 158 credit_cards.Append(entry);
177 } 159 }
178 160
179 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateCreditCards", 161 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.setCreditCardList",
180 credit_cards); 162 credit_cards);
181 } 163 }
182 164
183 void AutoFillOptionsHandler::UpdateAddress(const ListValue* args) { 165 void AutoFillOptionsHandler::RemoveAutoFillProfile(const ListValue* args) {
166 DCHECK(personal_data_->IsDataLoaded());
167
168 std::string guid;
169 if (!args->GetString(0, &guid)) {
170 NOTREACHED();
171 return;
172 }
173
174 // |guid| is the GUID of either an address or a credit card. Try to load the
175 // corresponding address. If it exists, then remove that address; otherwise,
176 // the GUID identifies a credit card, so remove the credit card.
177 // TODO(jhawkins): Make RemoveProfile return true/false depending on whether
178 // the profile was removed or not.
179 if (personal_data_->GetProfileByGUID(guid) != NULL)
180 personal_data_->RemoveProfile(guid);
181 else
182 personal_data_->RemoveCreditCard(guid);
183 }
184
185 void AutoFillOptionsHandler::LoadProfileEditor(const ListValue* args) {
186 DCHECK(personal_data_->IsDataLoaded());
187
188 std::string guid;
189 if (!args->GetString(0, &guid)) {
190 NOTREACHED();
191 return;
192 }
193
194 // |guid| is the GUID of either an address or a credit card. Try to load the
195 // corresponding address. If it exists, then edit that address; otherwise, the
196 // GUID identifies a credit card, so load the credit card editor.
197 if (personal_data_->GetProfileByGUID(guid) != NULL)
198 EditAddress(guid);
199 else
200 EditCreditCard(guid);
201 }
202
203 void AutoFillOptionsHandler::SetAddress(const ListValue* args) {
184 if (!personal_data_->IsDataLoaded()) 204 if (!personal_data_->IsDataLoaded())
185 return; 205 return;
186 206
187 std::string guid; 207 std::string guid;
188 if (!args->GetString(0, &guid)) { 208 if (!args->GetString(0, &guid)) {
189 NOTREACHED(); 209 NOTREACHED();
190 return; 210 return;
191 } 211 }
192 212
193 AutoFillProfile profile(guid); 213 AutoFillProfile profile(guid);
(...skipping 23 matching lines...)
217 profile.SetInfo(AutoFillType(EMAIL_ADDRESS), value); 237 profile.SetInfo(AutoFillType(EMAIL_ADDRESS), value);
218 238
219 if (!guid::IsValidGUID(profile.guid())) { 239 if (!guid::IsValidGUID(profile.guid())) {
220 profile.set_guid(guid::GenerateGUID()); 240 profile.set_guid(guid::GenerateGUID());
221 personal_data_->AddProfile(profile); 241 personal_data_->AddProfile(profile);
222 } else { 242 } else {
223 personal_data_->UpdateProfile(profile); 243 personal_data_->UpdateProfile(profile);
224 } 244 }
225 } 245 }
226 246
227 void AutoFillOptionsHandler::EditAddress(const ListValue* args) { 247 void AutoFillOptionsHandler::SetCreditCard(const ListValue* args) {
228 if (!personal_data_->IsDataLoaded()) 248 if (!personal_data_->IsDataLoaded())
229 return; 249 return;
230 250
231 std::string guid; 251 std::string guid;
232 if (!args->GetString(0, &guid)) { 252 if (!args->GetString(0, &guid)) {
233 NOTREACHED(); 253 NOTREACHED();
234 return; 254 return;
235 } 255 }
236 256
257 CreditCard credit_card(guid);
258
259 string16 value;
260 if (args->GetString(1, &value))
261 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NAME), value);
262 if (args->GetString(2, &value))
263 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), value);
264 if (args->GetString(3, &value))
265 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), value);
266 if (args->GetString(4, &value))
267 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), value);
268
269 if (!guid::IsValidGUID(credit_card.guid())) {
270 credit_card.set_guid(guid::GenerateGUID());
271 personal_data_->AddCreditCard(credit_card);
272 } else {
273 personal_data_->UpdateCreditCard(credit_card);
274 }
275 }
276
277 void AutoFillOptionsHandler::EditAddress(const std::string& guid) {
278 DCHECK(personal_data_->IsDataLoaded());
279
237 AutoFillProfile* profile = personal_data_->GetProfileByGUID(guid); 280 AutoFillProfile* profile = personal_data_->GetProfileByGUID(guid);
238 if (!profile) { 281 if (!profile) {
239 NOTREACHED(); 282 NOTREACHED();
240 return; 283 return;
241 } 284 }
242 285
243 // TODO(jhawkins): This is hacky because we can't send DictionaryValue 286 // TODO(jhawkins): This is hacky because we can't send DictionaryValue
244 // directly to CallJavascriptFunction(). 287 // directly to CallJavascriptFunction().
245 ListValue addressList; 288 ListValue addressList;
246 DictionaryValue* address = new DictionaryValue(); 289 DictionaryValue* address = new DictionaryValue();
(...skipping 21 matching lines...)
268 "fax", 311 "fax",
269 profile->GetFieldText(AutoFillType(PHONE_FAX_WHOLE_NUMBER))); 312 profile->GetFieldText(AutoFillType(PHONE_FAX_WHOLE_NUMBER)));
270 address->SetString("email", 313 address->SetString("email",
271 profile->GetFieldText(AutoFillType(EMAIL_ADDRESS))); 314 profile->GetFieldText(AutoFillType(EMAIL_ADDRESS)));
272 addressList.Append(address); 315 addressList.Append(address);
273 316
274 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editAddress", 317 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editAddress",
275 addressList); 318 addressList);
276 } 319 }
277 320
278 void AutoFillOptionsHandler::RemoveAddress(const ListValue* args) { 321 void AutoFillOptionsHandler::EditCreditCard(const std::string& guid) {
279 if (!personal_data_->IsDataLoaded()) 322 DCHECK(personal_data_->IsDataLoaded());
280 return;
281
282 std::string guid;
283 if (!args->GetString(0, &guid)) {
284 NOTREACHED();
285 return;
286 }
287
288 personal_data_->RemoveProfile(guid);
289 }
290
291 void AutoFillOptionsHandler::UpdateCreditCard(const ListValue* args) {
292 if (!personal_data_->IsDataLoaded())
293 return;
294
295 std::string guid;
296 if (!args->GetString(0, &guid)) {
297 NOTREACHED();
298 return;
299 }
300
301 CreditCard credit_card(guid);
302
303 string16 value;
304 if (args->GetString(1, &value))
305 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NAME), value);
306 if (args->GetString(2, &value))
307 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), value);
308 if (args->GetString(3, &value))
309 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), value);
310 if (args->GetString(4, &value))
311 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), value);
312
313 if (!guid::IsValidGUID(credit_card.guid())) {
314 credit_card.set_guid(guid::GenerateGUID());
315 personal_data_->AddCreditCard(credit_card);
316 } else {
317 personal_data_->UpdateCreditCard(credit_card);
318 }
319
320 }
321
322 void AutoFillOptionsHandler::EditCreditCard(const ListValue* args) {
323 if (!personal_data_->IsDataLoaded())
324 return;
325
326 std::string guid;
327 if (!args->GetString(0, &guid)) {
328 NOTREACHED();
329 return;
330 }
331 323
332 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid); 324 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid);
333
334 if (!credit_card) { 325 if (!credit_card) {
335 NOTREACHED(); 326 NOTREACHED();
336 return; 327 return;
337 } 328 }
338 329
339 // TODO(jhawkins): This is hacky because we can't send DictionaryValue 330 // TODO(jhawkins): This is hacky because we can't send DictionaryValue
340 // directly to CallJavascriptFunction(). 331 // directly to CallJavascriptFunction().
341 ListValue credit_card_list; 332 ListValue credit_card_list;
342 DictionaryValue* credit_card_data = new DictionaryValue(); 333 DictionaryValue* credit_card_data = new DictionaryValue();
343 credit_card_data->SetString("guid", credit_card->guid()); 334 credit_card_data->SetString("guid", credit_card->guid());
344 credit_card_data->SetString( 335 credit_card_data->SetString(
345 "nameOnCard", 336 "nameOnCard",
346 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NAME))); 337 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NAME)));
347 credit_card_data->SetString( 338 credit_card_data->SetString(
348 "creditCardNumber", 339 "creditCardNumber",
349 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NUMBER))); 340 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NUMBER)));
350 credit_card_data->SetString( 341 credit_card_data->SetString(
351 "expirationMonth", 342 "expirationMonth",
352 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_MONTH))); 343 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_MONTH)));
353 credit_card_data->SetString( 344 credit_card_data->SetString(
354 "expirationYear", 345 "expirationYear",
355 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR))); 346 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)));
356 credit_card_list.Append(credit_card_data); 347 credit_card_list.Append(credit_card_data);
357 348
358 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editCreditCard", 349 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editCreditCard",
359 credit_card_list); 350 credit_card_list);
360 } 351 }
361
362 void AutoFillOptionsHandler::RemoveCreditCard(const ListValue* args) {
363 if (!personal_data_->IsDataLoaded())
364 return;
365
366 std::string guid;
367 if (!args->GetString(0, &guid)) {
368 NOTREACHED();
369 return;
370 }
371
372 personal_data_->RemoveCreditCard(guid);
373 }
OLDNEW

Powered by Google App Engine