| 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 (or fax) number |index|, |phone_number_list|, and |
| 120 // |country_code| from the |args| input. |
| 121 void ExtractPhoneNumberInformation(const ListValue* args, |
| 122 size_t* index, |
| 123 ListValue** phone_number_list, |
| 124 std::string* country_code) { |
| 125 // Retrieve index as a |double|, as that is how it comes across from |
| 126 // JavaScript. |
| 127 double number = 0.0; |
| 128 if (!args->GetDouble(0, &number)) { |
| 129 NOTREACHED(); |
| 130 return; |
| 131 } |
| 132 *index = number; |
| 133 |
| 134 if (!args->GetList(1, phone_number_list)) { |
| 135 NOTREACHED(); |
| 136 return; |
| 137 } |
| 138 |
| 139 if (!args->GetString(2, country_code)) { |
| 140 NOTREACHED(); |
| 141 return; |
| 142 } |
| 143 } |
| 144 |
| 145 // Searches the |list| for the value at |index|. If this value is present |
| 146 // in any of the rest of the list, then the item (at |index|) is removed. |
| 147 // The comparison of phone number values is done on normalized versions of the |
| 148 // phone number values. |
| 149 void RemoveDuplicatePhoneNumberAtIndex(size_t index, |
| 150 const std::string& country_code, |
| 151 ListValue* list) { |
| 152 string16 new_value; |
| 153 list->GetString(index, &new_value); |
| 154 |
| 155 bool is_duplicate = false; |
| 156 for (size_t i = 0; i < list->GetSize() && !is_duplicate; ++i) { |
| 157 if (i == index) |
| 158 continue; |
| 159 |
| 160 string16 existing_value; |
| 161 list->GetString(i, &existing_value); |
| 162 is_duplicate = autofill_i18n::PhoneNumbersMatch(new_value, |
| 163 existing_value, |
| 164 country_code); |
| 165 } |
| 166 |
| 167 if (is_duplicate) |
| 168 list->Remove(index, NULL); |
| 169 } |
| 170 |
| 171 void ValidatePhoneArguments(const ListValue* args, ListValue** list) { |
| 172 size_t index = 0; |
| 173 std::string country_code; |
| 174 ExtractPhoneNumberInformation(args, &index, list, &country_code); |
| 175 |
| 176 RemoveDuplicatePhoneNumberAtIndex(index, country_code, *list); |
| 177 } |
| 178 |
| 118 } // namespace | 179 } // namespace |
| 119 | 180 |
| 120 AutofillOptionsHandler::AutofillOptionsHandler() | 181 AutofillOptionsHandler::AutofillOptionsHandler() |
| 121 : personal_data_(NULL) { | 182 : personal_data_(NULL) { |
| 122 } | 183 } |
| 123 | 184 |
| 124 AutofillOptionsHandler::~AutofillOptionsHandler() { | 185 AutofillOptionsHandler::~AutofillOptionsHandler() { |
| 125 if (personal_data_) | 186 if (personal_data_) |
| 126 personal_data_->RemoveObserver(this); | 187 personal_data_->RemoveObserver(this); |
| 127 } | 188 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 174 NewCallback(this, &AutofillOptionsHandler::LoadAddressEditor)); | 235 NewCallback(this, &AutofillOptionsHandler::LoadAddressEditor)); |
| 175 web_ui_->RegisterMessageCallback( | 236 web_ui_->RegisterMessageCallback( |
| 176 "loadCreditCardEditor", | 237 "loadCreditCardEditor", |
| 177 NewCallback(this, &AutofillOptionsHandler::LoadCreditCardEditor)); | 238 NewCallback(this, &AutofillOptionsHandler::LoadCreditCardEditor)); |
| 178 web_ui_->RegisterMessageCallback( | 239 web_ui_->RegisterMessageCallback( |
| 179 "setAddress", | 240 "setAddress", |
| 180 NewCallback(this, &AutofillOptionsHandler::SetAddress)); | 241 NewCallback(this, &AutofillOptionsHandler::SetAddress)); |
| 181 web_ui_->RegisterMessageCallback( | 242 web_ui_->RegisterMessageCallback( |
| 182 "setCreditCard", | 243 "setCreditCard", |
| 183 NewCallback(this, &AutofillOptionsHandler::SetCreditCard)); | 244 NewCallback(this, &AutofillOptionsHandler::SetCreditCard)); |
| 245 web_ui_->RegisterMessageCallback( |
| 246 "validatePhoneNumbers", |
| 247 NewCallback(this, &AutofillOptionsHandler::ValidatePhoneNumbers)); |
| 248 web_ui_->RegisterMessageCallback( |
| 249 "validateFaxNumbers", |
| 250 NewCallback(this, &AutofillOptionsHandler::ValidateFaxNumbers)); |
| 184 } | 251 } |
| 185 | 252 |
| 186 ///////////////////////////////////////////////////////////////////////////// | 253 ///////////////////////////////////////////////////////////////////////////// |
| 187 // PersonalDataManager::Observer implementation: | 254 // PersonalDataManager::Observer implementation: |
| 188 void AutofillOptionsHandler::OnPersonalDataLoaded() { | 255 void AutofillOptionsHandler::OnPersonalDataLoaded() { |
| 189 LoadAutofillData(); | 256 LoadAutofillData(); |
| 190 } | 257 } |
| 191 | 258 |
| 192 void AutofillOptionsHandler::OnPersonalDataChanged() { | 259 void AutofillOptionsHandler::OnPersonalDataChanged() { |
| 193 LoadAutofillData(); | 260 LoadAutofillData(); |
| (...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 if (args->GetString(4, &value)) | 512 if (args->GetString(4, &value)) |
| 446 credit_card.SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, value); | 513 credit_card.SetInfo(CREDIT_CARD_EXP_4_DIGIT_YEAR, value); |
| 447 | 514 |
| 448 if (!guid::IsValidGUID(credit_card.guid())) { | 515 if (!guid::IsValidGUID(credit_card.guid())) { |
| 449 credit_card.set_guid(guid::GenerateGUID()); | 516 credit_card.set_guid(guid::GenerateGUID()); |
| 450 personal_data_->AddCreditCard(credit_card); | 517 personal_data_->AddCreditCard(credit_card); |
| 451 } else { | 518 } else { |
| 452 personal_data_->UpdateCreditCard(credit_card); | 519 personal_data_->UpdateCreditCard(credit_card); |
| 453 } | 520 } |
| 454 } | 521 } |
| 522 |
| 523 void AutofillOptionsHandler::ValidatePhoneNumbers(const ListValue* args) { |
| 524 if (!personal_data_->IsDataLoaded()) |
| 525 return; |
| 526 |
| 527 ListValue* list_value = NULL; |
| 528 ValidatePhoneArguments(args, &list_value); |
| 529 |
| 530 web_ui_->CallJavascriptFunction( |
| 531 "AutofillEditAddressOverlay.setValidatedPhoneNumbers", *list_value); |
| 532 } |
| 533 |
| 534 void AutofillOptionsHandler::ValidateFaxNumbers(const ListValue* args) { |
| 535 if (!personal_data_->IsDataLoaded()) |
| 536 return; |
| 537 |
| 538 ListValue* list_value = NULL; |
| 539 ValidatePhoneArguments(args, &list_value); |
| 540 |
| 541 web_ui_->CallJavascriptFunction( |
| 542 "AutofillEditAddressOverlay.setValidatedFaxNumbers", *list_value); |
| 543 } |
| OLD | NEW |