| OLD | NEW |
| 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/autofill/autofill_manager.h" | 5 #include "chrome/browser/autofill/autofill_manager.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "app/l10n_util.h" | |
| 11 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 12 #include "base/string16.h" | 11 #include "base/string16.h" |
| 13 #include "base/utf_string_conversions.h" | 12 #include "base/utf_string_conversions.h" |
| 14 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" | 13 #include "chrome/browser/autofill/autofill_cc_infobar_delegate.h" |
| 15 #include "chrome/browser/autofill/autofill_dialog.h" | 14 #include "chrome/browser/autofill/autofill_dialog.h" |
| 16 #include "chrome/browser/autofill/form_structure.h" | 15 #include "chrome/browser/autofill/form_structure.h" |
| 17 #include "chrome/browser/autofill/select_control_handler.h" | 16 #include "chrome/browser/autofill/select_control_handler.h" |
| 18 #include "chrome/browser/guid.h" | 17 #include "chrome/browser/guid.h" |
| 19 #include "chrome/browser/prefs/pref_service.h" | 18 #include "chrome/browser/prefs/pref_service.h" |
| 20 #include "chrome/browser/profile.h" | 19 #include "chrome/browser/profile.h" |
| 21 #include "chrome/browser/renderer_host/render_view_host.h" | 20 #include "chrome/browser/renderer_host/render_view_host.h" |
| 22 #include "chrome/browser/tab_contents/tab_contents.h" | 21 #include "chrome/browser/tab_contents/tab_contents.h" |
| 23 #include "chrome/common/chrome_switches.h" | 22 #include "chrome/common/chrome_switches.h" |
| 24 #include "chrome/common/pref_names.h" | 23 #include "chrome/common/pref_names.h" |
| 25 #include "chrome/common/url_constants.h" | 24 #include "chrome/common/url_constants.h" |
| 26 #include "grit/generated_resources.h" | |
| 27 #include "webkit/glue/form_data.h" | 25 #include "webkit/glue/form_data.h" |
| 28 #include "webkit/glue/form_field.h" | 26 #include "webkit/glue/form_field.h" |
| 29 | 27 |
| 30 using webkit_glue::FormData; | 28 using webkit_glue::FormData; |
| 31 using webkit_glue::FormField; | 29 using webkit_glue::FormField; |
| 32 | 30 |
| 33 namespace { | 31 namespace { |
| 34 | 32 |
| 35 // We only send a fraction of the forms to upload server. | 33 // We only send a fraction of the forms to upload server. |
| 36 // The rate for positive/negative matches potentially could be different. | 34 // The rate for positive/negative matches potentially could be different. |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 if (tab_contents_->profile()->IsOffTheRecord()) | 125 if (tab_contents_->profile()->IsOffTheRecord()) |
| 128 return; | 126 return; |
| 129 | 127 |
| 130 // Don't save data that was submitted through JavaScript. | 128 // Don't save data that was submitted through JavaScript. |
| 131 if (!form.user_submitted) | 129 if (!form.user_submitted) |
| 132 return; | 130 return; |
| 133 | 131 |
| 134 // Grab a copy of the form data. | 132 // Grab a copy of the form data. |
| 135 upload_form_structure_.reset(new FormStructure(form)); | 133 upload_form_structure_.reset(new FormStructure(form)); |
| 136 | 134 |
| 137 if (!upload_form_structure_->IsAutoFillable(true)) | 135 if (!upload_form_structure_->IsAutoFillable()) |
| 138 return; | 136 return; |
| 139 | 137 |
| 140 // Determine the possible field types and upload the form structure to the | 138 // Determine the possible field types and upload the form structure to the |
| 141 // PersonalDataManager. | 139 // PersonalDataManager. |
| 142 DeterminePossibleFieldTypes(upload_form_structure_.get()); | 140 DeterminePossibleFieldTypes(upload_form_structure_.get()); |
| 143 HandleSubmit(); | 141 HandleSubmit(); |
| 144 } | 142 } |
| 145 | 143 |
| 146 void AutoFillManager::FormsSeen(const std::vector<FormData>& forms) { | 144 void AutoFillManager::FormsSeen(const std::vector<FormData>& forms) { |
| 147 if (!IsAutoFillEnabled()) | 145 if (!IsAutoFillEnabled()) |
| 148 return; | 146 return; |
| 149 | 147 |
| 150 ParseForms(forms); | 148 ParseForms(forms); |
| 151 } | 149 } |
| 152 | 150 |
| 153 bool AutoFillManager::GetAutoFillSuggestions(bool field_autofilled, | 151 bool AutoFillManager::GetAutoFillSuggestions(int query_id, |
| 152 bool field_autofilled, |
| 154 const FormField& field) { | 153 const FormField& field) { |
| 155 if (!IsAutoFillEnabled()) | 154 if (!IsAutoFillEnabled()) |
| 156 return false; | 155 return false; |
| 157 | 156 |
| 158 RenderViewHost* host = tab_contents_->render_view_host(); | 157 RenderViewHost* host = tab_contents_->render_view_host(); |
| 159 if (!host) | 158 if (!host) |
| 160 return false; | 159 return false; |
| 161 | 160 |
| 162 if (personal_data_->profiles().empty() && | 161 if (personal_data_->profiles().empty() && |
| 163 personal_data_->credit_cards().empty()) | 162 personal_data_->credit_cards().empty()) |
| 164 return false; | 163 return false; |
| 165 | 164 |
| 166 // Loops through the cached FormStructures looking for the FormStructure that | 165 // Loops through the cached FormStructures looking for the FormStructure that |
| 167 // contains |field| and the associated AutoFillFieldType. | 166 // contains |field| and the associated AutoFillFieldType. |
| 168 FormStructure* form = NULL; | 167 FormStructure* form = NULL; |
| 169 AutoFillField* autofill_field = NULL; | 168 AutoFillField* autofill_field = NULL; |
| 170 for (std::vector<FormStructure*>::iterator form_iter = | 169 for (std::vector<FormStructure*>::iterator form_iter = |
| 171 form_structures_.begin(); | 170 form_structures_.begin(); |
| 172 form_iter != form_structures_.end() && !autofill_field; ++form_iter) { | 171 form_iter != form_structures_.end() && !autofill_field; ++form_iter) { |
| 173 form = *form_iter; | 172 form = *form_iter; |
| 174 | 173 |
| 175 // Don't send suggestions for forms that aren't auto-fillable. | 174 // Don't send suggestions for forms that aren't auto-fillable. |
| 176 if (!form->IsAutoFillable(false)) | 175 if (!form->IsAutoFillable()) |
| 177 continue; | 176 continue; |
| 178 | 177 |
| 179 for (std::vector<AutoFillField*>::const_iterator iter = form->begin(); | 178 for (std::vector<AutoFillField*>::const_iterator iter = form->begin(); |
| 180 iter != form->end(); ++iter) { | 179 iter != form->end(); ++iter) { |
| 181 // The field list is terminated with a NULL AutoFillField, so don't try to | 180 // The field list is terminated with a NULL AutoFillField, so don't try to |
| 182 // dereference it. | 181 // dereference it. |
| 183 if (!*iter) | 182 if (!*iter) |
| 184 break; | 183 break; |
| 185 | 184 |
| 186 if ((**iter) == field) { | 185 if ((**iter) == field) { |
| (...skipping 21 matching lines...) Expand all Loading... |
| 208 } | 207 } |
| 209 | 208 |
| 210 DCHECK_EQ(values.size(), labels.size()); | 209 DCHECK_EQ(values.size(), labels.size()); |
| 211 DCHECK_EQ(values.size(), icons.size()); | 210 DCHECK_EQ(values.size(), icons.size()); |
| 212 DCHECK_EQ(values.size(), unique_ids.size()); | 211 DCHECK_EQ(values.size(), unique_ids.size()); |
| 213 | 212 |
| 214 // No suggestions. | 213 // No suggestions. |
| 215 if (values.empty()) | 214 if (values.empty()) |
| 216 return false; | 215 return false; |
| 217 | 216 |
| 218 // Don't provide AutoFill suggestions when AutoFill is disabled, but provide a | |
| 219 // warning to the user. | |
| 220 if (!form->IsAutoFillable(true)) { | |
| 221 values.assign( | |
| 222 1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_FORM_DISABLED)); | |
| 223 labels.assign(1, string16()); | |
| 224 icons.assign(1, string16()); | |
| 225 unique_ids.assign(1, -1); | |
| 226 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); | |
| 227 return true; | |
| 228 } | |
| 229 | |
| 230 // Don't provide credit card suggestions for non-HTTPS pages, but provide a | |
| 231 // warning to the user. | |
| 232 if (!FormIsHTTPS(form) && type.group() == AutoFillType::CREDIT_CARD) { | |
| 233 values.assign( | |
| 234 1, l10n_util::GetStringUTF16(IDS_AUTOFILL_WARNING_INSECURE_CONNECTION)); | |
| 235 labels.assign(1, string16()); | |
| 236 icons.assign(1, string16()); | |
| 237 unique_ids.assign(1, -1); | |
| 238 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); | |
| 239 return true; | |
| 240 } | |
| 241 | |
| 242 // If the form is auto-filled and the renderer is querying for suggestions, | 217 // If the form is auto-filled and the renderer is querying for suggestions, |
| 243 // then the user is editing the value of a field. In this case, mimick | 218 // then the user is editing the value of a field. In this case, mimick |
| 244 // autocomplete. In particular, don't display labels, as that information is | 219 // autocomplete. In particular, don't display labels, as that information is |
| 245 // redundant. In addition, remove duplicate values. | 220 // redundant. In addition, remove duplicate values. |
| 246 if (field_autofilled) { | 221 if (field_autofilled) { |
| 247 RemoveDuplicateElements(&values, &unique_ids); | 222 RemoveDuplicateElements(&values, &unique_ids); |
| 248 labels.resize(values.size()); | 223 labels.resize(values.size()); |
| 249 icons.resize(values.size()); | 224 icons.resize(values.size()); |
| 250 unique_ids.resize(values.size()); | 225 unique_ids.resize(values.size()); |
| 251 | 226 |
| 252 for (size_t i = 0; i < labels.size(); ++i) { | 227 for (size_t i = 0; i < labels.size(); ++i) { |
| 253 labels[i] = string16(); | 228 labels[i] = string16(); |
| 254 icons[i] = string16(); | 229 icons[i] = string16(); |
| 255 unique_ids[i] = 0; | 230 unique_ids[i] = 0; |
| 256 } | 231 } |
| 257 } | 232 } |
| 258 | 233 |
| 259 host->AutoFillSuggestionsReturned(values, labels, icons, unique_ids); | 234 host->AutoFillSuggestionsReturned( |
| 235 query_id, values, labels, icons, unique_ids); |
| 260 return true; | 236 return true; |
| 261 } | 237 } |
| 262 | 238 |
| 263 bool AutoFillManager::FillAutoFillFormData(int query_id, | 239 bool AutoFillManager::FillAutoFillFormData(int query_id, |
| 264 const FormData& form, | 240 const FormData& form, |
| 265 int unique_id) { | 241 int unique_id) { |
| 266 if (!IsAutoFillEnabled()) | 242 if (!IsAutoFillEnabled()) |
| 267 return false; | 243 return false; |
| 268 | 244 |
| 269 RenderViewHost* host = tab_contents_->render_view_host(); | 245 RenderViewHost* host = tab_contents_->render_view_host(); |
| (...skipping 272 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 542 icons->resize(values->size()); | 518 icons->resize(values->size()); |
| 543 } | 519 } |
| 544 | 520 |
| 545 void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, | 521 void AutoFillManager::GetCreditCardSuggestions(FormStructure* form, |
| 546 const FormField& field, | 522 const FormField& field, |
| 547 AutoFillType type, | 523 AutoFillType type, |
| 548 std::vector<string16>* values, | 524 std::vector<string16>* values, |
| 549 std::vector<string16>* labels, | 525 std::vector<string16>* labels, |
| 550 std::vector<string16>* icons, | 526 std::vector<string16>* icons, |
| 551 std::vector<int>* unique_ids) { | 527 std::vector<int>* unique_ids) { |
| 528 // Don't return CC suggestions for non-HTTPS pages. |
| 529 if (!FormIsHTTPS(form)) |
| 530 return; |
| 531 |
| 552 for (std::vector<CreditCard*>::const_iterator iter = | 532 for (std::vector<CreditCard*>::const_iterator iter = |
| 553 personal_data_->credit_cards().begin(); | 533 personal_data_->credit_cards().begin(); |
| 554 iter != personal_data_->credit_cards().end(); ++iter) { | 534 iter != personal_data_->credit_cards().end(); ++iter) { |
| 555 CreditCard* credit_card = *iter; | 535 CreditCard* credit_card = *iter; |
| 556 | 536 |
| 557 // The value of the stored data for this field type in the |credit_card|. | 537 // The value of the stored data for this field type in the |credit_card|. |
| 558 string16 creditcard_field_value = credit_card->GetFieldText(type); | 538 string16 creditcard_field_value = credit_card->GetFieldText(type); |
| 559 if (!creditcard_field_value.empty() && | 539 if (!creditcard_field_value.empty() && |
| 560 StartsWith(creditcard_field_value, field.value(), false)) { | 540 StartsWith(creditcard_field_value, field.value(), false)) { |
| 561 if (type.field_type() == CREDIT_CARD_NUMBER) | 541 if (type.field_type() == CREDIT_CARD_NUMBER) |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 } else if (has_valid_suffix_and_prefix && | 592 } else if (has_valid_suffix_and_prefix && |
| 613 field->size() == kAutoFillPhoneNumberSuffixCount) { | 593 field->size() == kAutoFillPhoneNumberSuffixCount) { |
| 614 number = number.substr(kAutoFillPhoneNumberSuffixOffset, | 594 number = number.substr(kAutoFillPhoneNumberSuffixOffset, |
| 615 kAutoFillPhoneNumberSuffixCount); | 595 kAutoFillPhoneNumberSuffixCount); |
| 616 field->set_value(number); | 596 field->set_value(number); |
| 617 } else { | 597 } else { |
| 618 field->set_value(number); | 598 field->set_value(number); |
| 619 } | 599 } |
| 620 } | 600 } |
| 621 | 601 |
| 622 void AutoFillManager::ParseForms(const std::vector<FormData>& forms) { | 602 void AutoFillManager::ParseForms( |
| 623 std::vector<FormStructure *> non_queryable_forms; | 603 const std::vector<webkit_glue::FormData>& forms) { |
| 624 for (std::vector<FormData>::const_iterator iter = forms.begin(); | 604 for (std::vector<FormData>::const_iterator iter = |
| 605 forms.begin(); |
| 625 iter != forms.end(); ++iter) { | 606 iter != forms.end(); ++iter) { |
| 626 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); | 607 scoped_ptr<FormStructure> form_structure(new FormStructure(*iter)); |
| 627 if (!form_structure->ShouldBeParsed(false)) | 608 if (!form_structure->ShouldBeParsed()) |
| 628 continue; | 609 continue; |
| 629 | 610 |
| 630 DeterminePossibleFieldTypes(form_structure.get()); | 611 DeterminePossibleFieldTypes(form_structure.get()); |
| 631 | 612 form_structures_.push_back(form_structure.release()); |
| 632 // Set aside forms with method GET so that they are not included in the | |
| 633 // query to the server. | |
| 634 if (form_structure->ShouldBeParsed(true)) | |
| 635 form_structures_.push_back(form_structure.release()); | |
| 636 else | |
| 637 non_queryable_forms.push_back(form_structure.release()); | |
| 638 } | 613 } |
| 639 | 614 |
| 640 // If none of the forms were parsed, no use querying the server. | 615 // If none of the forms were parsed, no use querying the server. |
| 641 if (!form_structures_.empty() && !disable_download_manager_requests_) | 616 if (!form_structures_.empty() && !disable_download_manager_requests_) |
| 642 download_manager_.StartQueryRequest(form_structures_); | 617 download_manager_.StartQueryRequest(form_structures_); |
| 643 | |
| 644 for (std::vector<FormStructure *>::const_iterator iter = | |
| 645 non_queryable_forms.begin(); | |
| 646 iter != non_queryable_forms.end(); ++iter) { | |
| 647 form_structures_.push_back(*iter); | |
| 648 } | |
| 649 } | 618 } |
| 650 | 619 |
| 651 // When sending IDs (across processes) to the renderer we pack credit card and | 620 // When sending IDs (across processes) to the renderer we pack credit card and |
| 652 // profile IDs into a single integer. Credit card IDs are sent in the high | 621 // profile IDs into a single integer. Credit card IDs are sent in the high |
| 653 // word and profile IDs are sent in the low word. | 622 // word and profile IDs are sent in the low word. |
| 654 int AutoFillManager::PackGUIDs(const std::string& cc_guid, | 623 int AutoFillManager::PackGUIDs(const std::string& cc_guid, |
| 655 const std::string& profile_guid) { | 624 const std::string& profile_guid) { |
| 656 int cc_id = GUIDToID(cc_guid); | 625 int cc_id = GUIDToID(cc_guid); |
| 657 int profile_id = GUIDToID(profile_guid); | 626 int profile_id = GUIDToID(profile_guid); |
| 658 | 627 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 697 return std::string(); | 666 return std::string(); |
| 698 | 667 |
| 699 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); | 668 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); |
| 700 if (iter == id_guid_map_.end()) { | 669 if (iter == id_guid_map_.end()) { |
| 701 NOTREACHED(); | 670 NOTREACHED(); |
| 702 return std::string(); | 671 return std::string(); |
| 703 } | 672 } |
| 704 | 673 |
| 705 return iter->second; | 674 return iter->second; |
| 706 } | 675 } |
| OLD | NEW |