Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/ui/webui/options/autofill_options_handler.h" | 5 #include "chrome/browser/ui/webui/options/autofill_options_handler.h" |
| 6 | 6 |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string16.h" | 10 #include "base/string16.h" |
| 11 #include "base/string_number_conversions.h" | 11 #include "base/string_number_conversions.h" |
| 12 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/autofill/autofill_country.h" | 14 #include "chrome/browser/autofill/autofill_country.h" |
| 15 #include "chrome/browser/autofill/autofill_profile.h" | 15 #include "chrome/browser/autofill/autofill_profile.h" |
| 16 #include "chrome/browser/autofill/credit_card.h" | 16 #include "chrome/browser/autofill/credit_card.h" |
| 17 #include "chrome/browser/autofill/phone_number_i18n.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | 18 #include "chrome/browser/profiles/profile.h" |
| 18 #include "chrome/browser/ui/webui/web_ui_util.h" | 19 #include "chrome/browser/ui/webui/web_ui_util.h" |
| 19 #include "chrome/common/guid.h" | 20 #include "chrome/common/guid.h" |
| 20 #include "grit/generated_resources.h" | 21 #include "grit/generated_resources.h" |
| 21 #include "grit/webkit_resources.h" | 22 #include "grit/webkit_resources.h" |
| 22 #include "ui/base/l10n/l10n_util.h" | 23 #include "ui/base/l10n/l10n_util.h" |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Converts a credit card type to the appropriate resource ID of the CC icon. | 27 // Converts a credit card type to the appropriate resource ID of the CC icon. |
| (...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 108 AutofillProfile* profile) { | 109 AutofillProfile* profile) { |
| 109 std::vector<string16> values(list->GetSize()); | 110 std::vector<string16> values(list->GetSize()); |
| 110 for (size_t i = 0; i < list->GetSize(); ++i) { | 111 for (size_t i = 0; i < list->GetSize(); ++i) { |
| 111 string16 value; | 112 string16 value; |
| 112 if (list->GetString(i, &value)) | 113 if (list->GetString(i, &value)) |
| 113 values[i] = value; | 114 values[i] = value; |
| 114 } | 115 } |
| 115 profile->SetMultiInfo(type, values); | 116 profile->SetMultiInfo(type, values); |
| 116 } | 117 } |
| 117 | 118 |
| 119 // Pulls the phone number |index|, |list_value|, and |country_code| from the | |
| 120 // |args| input. | |
| 121 void ExtractPhoneNumberInformation(const ListValue* args, | |
| 122 size_t* index, | |
| 123 ListValue** list_value, | |
|
James Hawkins
2011/05/06 20:34:11
What are the contents of |list_value|? I propose t
dhollowa
2011/05/06 21:00:19
Done.
| |
| 124 std::string* country_code) { | |
| 125 double number = 0.0; | |
|
James Hawkins
2011/05/06 20:34:11
Why particularly using a double?
dhollowa
2011/05/06 21:00:19
All numbers come across from JavaScript as doubles
| |
| 126 if (!args->GetDouble(0, &number)) { | |
| 127 NOTREACHED(); | |
| 128 return; | |
| 129 } | |
| 130 *index = number; | |
| 131 | |
| 132 if (!args->GetList(1, list_value)) { | |
| 133 NOTREACHED(); | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 if (!args->GetString(2, country_code)) { | |
| 138 NOTREACHED(); | |
| 139 return; | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 // Searches the |list| for the value at |index|. If this value is present | |
| 144 // in any of the rest of the list, then the item (at |index|) is removed. | |
| 145 // The comparison of phone number values is done on normalized versions of the | |
| 146 // phone number values. | |
| 147 void RemoveDuplicatePhoneNumberAtIndex(size_t index, | |
| 148 const std::string& country_code, | |
| 149 ListValue* list) { | |
| 150 string16 new_value; | |
| 151 list->GetString(index, &new_value); | |
| 152 | |
| 153 bool is_duplicate = false; | |
| 154 for (size_t i = 0; i < list->GetSize() && !is_duplicate; ++i) { | |
| 155 if (i == index) | |
| 156 continue; | |
| 157 | |
| 158 string16 existing_value; | |
| 159 list->GetString(i, &existing_value); | |
| 160 is_duplicate = autofill_i18n::PhoneNumbersMatch(new_value, | |
| 161 existing_value, | |
| 162 country_code); | |
| 163 } | |
| 164 | |
| 165 if (is_duplicate) | |
| 166 list->Remove(index, NULL); | |
| 167 } | |
| 168 | |
| 169 void ValidatePhoneArguments(const ListValue* args, ListValue** list) { | |
| 170 size_t index = 0; | |
| 171 std::string country_code; | |
| 172 ExtractPhoneNumberInformation(args, &index, list, &country_code); | |
| 173 | |
| 174 RemoveDuplicatePhoneNumberAtIndex(index, country_code, *list); | |
| 175 } | |
| 176 | |
| 118 } // namespace | 177 } // namespace |
| 119 | 178 |
| 120 AutofillOptionsHandler::AutofillOptionsHandler() | 179 AutofillOptionsHandler::AutofillOptionsHandler() |
| 121 : personal_data_(NULL) { | 180 : personal_data_(NULL) { |
| 122 } | 181 } |
| 123 | 182 |
| 124 AutofillOptionsHandler::~AutofillOptionsHandler() { | 183 AutofillOptionsHandler::~AutofillOptionsHandler() { |
| 125 if (personal_data_) | 184 if (personal_data_) |
| 126 personal_data_->RemoveObserver(this); | 185 personal_data_->RemoveObserver(this); |
| 127 } | 186 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 NewCallback(this, &AutofillOptionsHandler::LoadAddressEditor)); | 233 NewCallback(this, &AutofillOptionsHandler::LoadAddressEditor)); |
| 175 web_ui_->RegisterMessageCallback( | 234 web_ui_->RegisterMessageCallback( |
| 176 "loadCreditCardEditor", | 235 "loadCreditCardEditor", |
| 177 NewCallback(this, &AutofillOptionsHandler::LoadCreditCardEditor)); | 236 NewCallback(this, &AutofillOptionsHandler::LoadCreditCardEditor)); |
| 178 web_ui_->RegisterMessageCallback( | 237 web_ui_->RegisterMessageCallback( |
| 179 "setAddress", | 238 "setAddress", |
| 180 NewCallback(this, &AutofillOptionsHandler::SetAddress)); | 239 NewCallback(this, &AutofillOptionsHandler::SetAddress)); |
| 181 web_ui_->RegisterMessageCallback( | 240 web_ui_->RegisterMessageCallback( |
| 182 "setCreditCard", | 241 "setCreditCard", |
| 183 NewCallback(this, &AutofillOptionsHandler::SetCreditCard)); | 242 NewCallback(this, &AutofillOptionsHandler::SetCreditCard)); |
| 243 web_ui_->RegisterMessageCallback( | |
| 244 "validatePhoneNumbers", | |
| 245 NewCallback(this, &AutofillOptionsHandler::ValidatePhoneNumbers)); | |
| 246 web_ui_->RegisterMessageCallback( | |
| 247 "validateFaxNumbers", | |
| 248 NewCallback(this, &AutofillOptionsHandler::ValidateFaxNumbers)); | |
| 184 } | 249 } |
| 185 | 250 |
| 186 ///////////////////////////////////////////////////////////////////////////// | 251 ///////////////////////////////////////////////////////////////////////////// |
| 187 // PersonalDataManager::Observer implementation: | 252 // PersonalDataManager::Observer implementation: |
| 188 void AutofillOptionsHandler::OnPersonalDataLoaded() { | 253 void AutofillOptionsHandler::OnPersonalDataLoaded() { |
| 189 LoadAutofillData(); | 254 LoadAutofillData(); |
| 190 } | 255 } |
| 191 | 256 |
| 192 void AutofillOptionsHandler::OnPersonalDataChanged() { | 257 void AutofillOptionsHandler::OnPersonalDataChanged() { |
| 193 LoadAutofillData(); | 258 LoadAutofillData(); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 445 if (args->GetString(4, &value)) | 510 if (args->GetString(4, &value)) |
| 446 credit_card.SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, value); | 511 credit_card.SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, value); |
| 447 | 512 |
| 448 if (!guid::IsValidGUID(credit_card.guid())) { | 513 if (!guid::IsValidGUID(credit_card.guid())) { |
| 449 credit_card.set_guid(guid::GenerateGUID()); | 514 credit_card.set_guid(guid::GenerateGUID()); |
| 450 personal_data_->AddCreditCard(credit_card); | 515 personal_data_->AddCreditCard(credit_card); |
| 451 } else { | 516 } else { |
| 452 personal_data_->UpdateCreditCard(credit_card); | 517 personal_data_->UpdateCreditCard(credit_card); |
| 453 } | 518 } |
| 454 } | 519 } |
| 520 | |
| 521 void AutofillOptionsHandler::ValidatePhoneNumbers(const ListValue* args) { | |
| 522 if (!personal_data_->IsDataLoaded()) | |
| 523 return; | |
| 524 | |
| 525 ListValue* list_value = NULL; | |
| 526 ValidatePhoneArguments(args, &list_value); | |
| 527 | |
| 528 web_ui_->CallJavascriptFunction( | |
| 529 "AutofillEditAddressOverlay.setValidatedPhoneNumbers", *list_value); | |
| 530 } | |
| 531 | |
| 532 void AutofillOptionsHandler::ValidateFaxNumbers(const ListValue* args) { | |
| 533 if (!personal_data_->IsDataLoaded()) | |
| 534 return; | |
| 535 | |
| 536 ListValue* list_value = NULL; | |
| 537 ValidatePhoneArguments(args, &list_value); | |
| 538 | |
| 539 web_ui_->CallJavascriptFunction( | |
| 540 "AutofillEditAddressOverlay.setValidatedFaxNumbers", *list_value); | |
| 541 } | |
| OLD | NEW |