Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(419)

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: Rebase. Created 10 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | 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...) Expand all
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", 45 localized_strings->SetString("editButton",
James Hawkins 2010/12/22 01:39:40 Not used, remove.
James Hawkins 2010/12/22 02:23:53 Done.
48 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_BUTTON)); 46 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_BUTTON));
49 localized_strings->SetString("deleteButton", 47 localized_strings->SetString("deleteButton",
James Hawkins 2010/12/22 01:39:40 Not used, remove.
James Hawkins 2010/12/22 02:23:53 Done.
50 l10n_util::GetStringUTF16(IDS_AUTOFILL_DELETE_BUTTON)); 48 l10n_util::GetStringUTF16(IDS_AUTOFILL_DELETE_BUTTON));
51 localized_strings->SetString("helpButton", 49 localized_strings->SetString("helpButton",
52 l10n_util::GetStringUTF16(IDS_AUTOFILL_HELP_LABEL)); 50 l10n_util::GetStringUTF16(IDS_AUTOFILL_HELP_LABEL));
53 localized_strings->SetString("addAddressTitle", 51 localized_strings->SetString("addAddressTitle",
54 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_CAPTION)); 52 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_ADDRESS_CAPTION));
55 localized_strings->SetString("editAddressTitle", 53 localized_strings->SetString("editAddressTitle",
56 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION)); 54 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_ADDRESS_CAPTION));
57 localized_strings->SetString("addCreditCardTitle", 55 localized_strings->SetString("addCreditCardTitle",
58 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION)); 56 l10n_util::GetStringUTF16(IDS_AUTOFILL_ADD_CREDITCARD_CAPTION));
59 localized_strings->SetString("editCreditCardTitle", 57 localized_strings->SetString("editCreditCardTitle",
60 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION)); 58 l10n_util::GetStringUTF16(IDS_AUTOFILL_EDIT_CREDITCARD_CAPTION));
61 59
62 SetAddressOverlayStrings(localized_strings); 60 SetAddressOverlayStrings(localized_strings);
63 SetCreditCardOverlayStrings(localized_strings); 61 SetCreditCardOverlayStrings(localized_strings);
64 } 62 }
65 63
66 void AutoFillOptionsHandler::Initialize() { 64 void AutoFillOptionsHandler::Initialize() {
67 personal_data_ = 65 personal_data_ = dom_ui_->GetProfile()->GetPersonalDataManager();
68 dom_ui_->GetProfile()->GetOriginalProfile()->GetPersonalDataManager();
69 personal_data_->SetObserver(this); 66 personal_data_->SetObserver(this);
70 67
71 LoadAutoFillData(); 68 LoadAutoFillData();
72 } 69 }
73 70
74 void AutoFillOptionsHandler::RegisterMessages() { 71 void AutoFillOptionsHandler::RegisterMessages() {
75 dom_ui_->RegisterMessageCallback( 72 dom_ui_->RegisterMessageCallback(
76 "updateAddress", 73 "removeAutoFillProfile",
77 NewCallback(this, &AutoFillOptionsHandler::UpdateAddress)); 74 NewCallback(this, &AutoFillOptionsHandler::RemoveAutoFillProfile));
78
79 dom_ui_->RegisterMessageCallback( 75 dom_ui_->RegisterMessageCallback(
80 "editAddress", 76 "loadProfileEditor",
81 NewCallback(this, &AutoFillOptionsHandler::EditAddress)); 77 NewCallback(this, &AutoFillOptionsHandler::LoadProfileEditor));
82
83 dom_ui_->RegisterMessageCallback( 78 dom_ui_->RegisterMessageCallback(
84 "removeAddress", 79 "setAddress",
85 NewCallback(this, &AutoFillOptionsHandler::RemoveAddress)); 80 NewCallback(this, &AutoFillOptionsHandler::SetAddress));
86
87 dom_ui_->RegisterMessageCallback( 81 dom_ui_->RegisterMessageCallback(
88 "updateCreditCard", 82 "setCreditCard",
89 NewCallback(this, &AutoFillOptionsHandler::UpdateCreditCard)); 83 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 } 84 }
99 85
100 ///////////////////////////////////////////////////////////////////////////// 86 /////////////////////////////////////////////////////////////////////////////
101 // PersonalDataManager::Observer implementation: 87 // PersonalDataManager::Observer implementation:
102 void AutoFillOptionsHandler::OnPersonalDataLoaded() { 88 void AutoFillOptionsHandler::OnPersonalDataLoaded() {
103 LoadAutoFillData(); 89 LoadAutoFillData();
104 } 90 }
105 91
106 void AutoFillOptionsHandler::OnPersonalDataChanged() { 92 void AutoFillOptionsHandler::OnPersonalDataChanged() {
107 LoadAutoFillData(); 93 LoadAutoFillData();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
150 } 136 }
151 137
152 void AutoFillOptionsHandler::LoadAutoFillData() { 138 void AutoFillOptionsHandler::LoadAutoFillData() {
153 if (!personal_data_->IsDataLoaded()) 139 if (!personal_data_->IsDataLoaded())
154 return; 140 return;
155 141
156 ListValue addresses; 142 ListValue addresses;
157 for (std::vector<AutoFillProfile*>::const_iterator i = 143 for (std::vector<AutoFillProfile*>::const_iterator i =
158 personal_data_->web_profiles().begin(); 144 personal_data_->web_profiles().begin();
159 i != personal_data_->web_profiles().end(); ++i) { 145 i != personal_data_->web_profiles().end(); ++i) {
160 DictionaryValue* address = new DictionaryValue(); 146 ListValue* entry = new ListValue();
161 address->SetString("label", (*i)->Label()); 147 entry->Append(new StringValue((*i)->guid()));
162 address->SetString("guid", (*i)->guid()); 148 entry->Append(new StringValue((*i)->Label()));
163 addresses.Append(address); 149 addresses.Append(entry);
164 } 150 }
165 151
166 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateAddresses", 152 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.setAddressList",
167 addresses); 153 addresses);
168 154
169 ListValue credit_cards; 155 ListValue credit_cards;
170 for (std::vector<CreditCard*>::const_iterator i = 156 for (std::vector<CreditCard*>::const_iterator i =
171 personal_data_->credit_cards().begin(); 157 personal_data_->credit_cards().begin();
172 i != personal_data_->credit_cards().end(); ++i) { 158 i != personal_data_->credit_cards().end(); ++i) {
173 DictionaryValue* credit_card = new DictionaryValue(); 159 ListValue* entry = new ListValue();
174 credit_card->SetString("label", (*i)->PreviewSummary()); 160 entry->Append(new StringValue((*i)->guid()));
175 credit_card->SetString("guid", (*i)->guid()); 161 entry->Append(new StringValue((*i)->PreviewSummary()));
176 credit_cards.Append(credit_card); 162 credit_cards.Append(entry);
177 } 163 }
178 164
179 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.updateCreditCards", 165 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.setCreditCardList",
180 credit_cards); 166 credit_cards);
181 } 167 }
182 168
183 void AutoFillOptionsHandler::UpdateAddress(const ListValue* args) { 169 void AutoFillOptionsHandler::RemoveAutoFillProfile(const ListValue* args) {
170 DCHECK(personal_data_->IsDataLoaded());
171
172 std::string guid;
173 if (!args->GetString(0, &guid)) {
174 NOTREACHED();
175 return;
176 }
177
178 // TODO(jhawkins): Make RemoveProfile return true/false depending on whether
179 // the profile was removed or not.
180 if (personal_data_->GetProfileByGUID(guid) != NULL)
181 personal_data_->RemoveProfile(guid);
182 else
183 personal_data_->RemoveCreditCard(guid);
184 }
185
186 void AutoFillOptionsHandler::LoadProfileEditor(const ListValue* args) {
187 printf("LoadProfileEditor\n");
James Hawkins 2010/12/22 01:39:40 Remove cruft.
James Hawkins 2010/12/22 02:23:53 Done.
188 DCHECK(personal_data_->IsDataLoaded());
189
190 std::string guid;
191 if (!args->GetString(0, &guid)) {
192 NOTREACHED();
193 return;
194 }
195
196 // TODO(jhawkins): Refactor this.
197 AutoFillProfile* address = personal_data_->GetProfileByGUID(guid);
198 if (address != NULL)
199 EditAddress(guid);
200 else
201 EditCreditCard(guid);
202 }
203
204 void AutoFillOptionsHandler::SetAddress(const ListValue* args) {
184 if (!personal_data_->IsDataLoaded()) 205 if (!personal_data_->IsDataLoaded())
185 return; 206 return;
186 207
187 std::string guid; 208 std::string guid;
188 if (!args->GetString(0, &guid)) { 209 if (!args->GetString(0, &guid)) {
189 NOTREACHED(); 210 NOTREACHED();
190 return; 211 return;
191 } 212 }
192 213
193 AutoFillProfile profile(guid); 214 AutoFillProfile profile(guid);
(...skipping 23 matching lines...) Expand all
217 profile.SetInfo(AutoFillType(EMAIL_ADDRESS), value); 238 profile.SetInfo(AutoFillType(EMAIL_ADDRESS), value);
218 239
219 if (!guid::IsValidGUID(profile.guid())) { 240 if (!guid::IsValidGUID(profile.guid())) {
220 profile.set_guid(guid::GenerateGUID()); 241 profile.set_guid(guid::GenerateGUID());
221 personal_data_->AddProfile(profile); 242 personal_data_->AddProfile(profile);
222 } else { 243 } else {
223 personal_data_->UpdateProfile(profile); 244 personal_data_->UpdateProfile(profile);
224 } 245 }
225 } 246 }
226 247
227 void AutoFillOptionsHandler::EditAddress(const ListValue* args) { 248 void AutoFillOptionsHandler::SetCreditCard(const ListValue* args) {
228 if (!personal_data_->IsDataLoaded()) 249 if (!personal_data_->IsDataLoaded())
229 return; 250 return;
230 251
231 std::string guid; 252 std::string guid;
232 if (!args->GetString(0, &guid)) { 253 if (!args->GetString(0, &guid)) {
233 NOTREACHED(); 254 NOTREACHED();
234 return; 255 return;
235 } 256 }
236 257
258 CreditCard credit_card(guid);
259
260 string16 value;
261 if (args->GetString(1, &value))
262 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NAME), value);
263 if (args->GetString(2, &value))
264 credit_card.SetInfo(AutoFillType(CREDIT_CARD_NUMBER), value);
265 if (args->GetString(3, &value))
266 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_MONTH), value);
267 if (args->GetString(4, &value))
268 credit_card.SetInfo(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR), value);
269
270 if (!guid::IsValidGUID(credit_card.guid())) {
271 credit_card.set_guid(guid::GenerateGUID());
272 personal_data_->AddCreditCard(credit_card);
273 } else {
274 personal_data_->UpdateCreditCard(credit_card);
275 }
276 }
277
278 void AutoFillOptionsHandler::EditAddress(const std::string& guid) {
279 DCHECK(personal_data_->IsDataLoaded());
280
237 AutoFillProfile* profile = personal_data_->GetProfileByGUID(guid); 281 AutoFillProfile* profile = personal_data_->GetProfileByGUID(guid);
238 if (!profile) { 282 if (!profile) {
239 NOTREACHED(); 283 NOTREACHED();
240 return; 284 return;
241 } 285 }
242 286
243 // TODO(jhawkins): This is hacky because we can't send DictionaryValue 287 // TODO(jhawkins): This is hacky because we can't send DictionaryValue
244 // directly to CallJavascriptFunction(). 288 // directly to CallJavascriptFunction().
245 ListValue addressList; 289 ListValue addressList;
246 DictionaryValue* address = new DictionaryValue(); 290 DictionaryValue* address = new DictionaryValue();
(...skipping 21 matching lines...) Expand all
268 "fax", 312 "fax",
269 profile->GetFieldText(AutoFillType(PHONE_FAX_WHOLE_NUMBER))); 313 profile->GetFieldText(AutoFillType(PHONE_FAX_WHOLE_NUMBER)));
270 address->SetString("email", 314 address->SetString("email",
271 profile->GetFieldText(AutoFillType(EMAIL_ADDRESS))); 315 profile->GetFieldText(AutoFillType(EMAIL_ADDRESS)));
272 addressList.Append(address); 316 addressList.Append(address);
273 317
274 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editAddress", 318 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editAddress",
275 addressList); 319 addressList);
276 } 320 }
277 321
278 void AutoFillOptionsHandler::RemoveAddress(const ListValue* args) { 322 void AutoFillOptionsHandler::EditCreditCard(const std::string& guid) {
279 if (!personal_data_->IsDataLoaded()) 323 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 324
332 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid); 325 CreditCard* credit_card = personal_data_->GetCreditCardByGUID(guid);
333
334 if (!credit_card) { 326 if (!credit_card) {
335 NOTREACHED(); 327 NOTREACHED();
336 return; 328 return;
337 } 329 }
338 330
339 // TODO(jhawkins): This is hacky because we can't send DictionaryValue 331 // TODO(jhawkins): This is hacky because we can't send DictionaryValue
340 // directly to CallJavascriptFunction(). 332 // directly to CallJavascriptFunction().
341 ListValue credit_card_list; 333 ListValue credit_card_list;
342 DictionaryValue* credit_card_data = new DictionaryValue(); 334 DictionaryValue* credit_card_data = new DictionaryValue();
343 credit_card_data->SetString("guid", credit_card->guid()); 335 credit_card_data->SetString("guid", credit_card->guid());
344 credit_card_data->SetString( 336 credit_card_data->SetString(
345 "nameOnCard", 337 "nameOnCard",
346 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NAME))); 338 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NAME)));
347 credit_card_data->SetString( 339 credit_card_data->SetString(
348 "creditCardNumber", 340 "creditCardNumber",
349 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NUMBER))); 341 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_NUMBER)));
350 credit_card_data->SetString( 342 credit_card_data->SetString(
351 "expirationMonth", 343 "expirationMonth",
352 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_MONTH))); 344 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_MONTH)));
353 credit_card_data->SetString( 345 credit_card_data->SetString(
354 "expirationYear", 346 "expirationYear",
355 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR))); 347 credit_card->GetFieldText(AutoFillType(CREDIT_CARD_EXP_4_DIGIT_YEAR)));
356 credit_card_list.Append(credit_card_data); 348 credit_card_list.Append(credit_card_data);
357 349
358 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editCreditCard", 350 dom_ui_->CallJavascriptFunction(L"AutoFillOptions.editCreditCard",
359 credit_card_list); 351 credit_card_list);
360 } 352 }
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
This is Rietveld 408576698