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