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

Side by Side Diff: chrome/browser/autofill/autofill_manager.cc

Issue 4591001: Display a warning when autofill is disabled for a website. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Move the smarts out of RenderViewHost Created 10 years, 1 month 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/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
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(false))
dhollowa 2010/11/15 18:47:57 These changes should not alter the types of form d
Ilya Sherman 2010/11/15 22:53:32 Done.
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))
dhollowa 2010/11/15 18:47:57 Instead of changing the |IsAutoFillable| and |Shou
Ilya Sherman 2010/11/15 22:53:32 As discussed offline, we only want to show the war
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
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
dhollowa 2010/11/15 18:47:57 nit: The convention in comments is to use "AutoFil
Ilya Sherman 2010/11/15 22:53:32 Done.
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
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 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(
603 const std::vector<webkit_glue::FormData>& forms) { 623 const std::vector<webkit_glue::FormData>& 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 // TODO(isherman): Might want to set aside forms that have method != POST,
dhollowa 2010/11/15 18:47:57 As mentioned above, we can keep |ShouldBeParsed| a
Ilya Sherman 2010/11/15 22:53:32 As mentioned in the reply above, keeping the seman
628 // and not send those to the server. It seems like that might not work
629 // though.
630 if (!form_structure->ShouldBeParsed(false))
609 continue; 631 continue;
610 632
611 DeterminePossibleFieldTypes(form_structure.get()); 633 DeterminePossibleFieldTypes(form_structure.get());
612 form_structures_.push_back(form_structure.release()); 634 form_structures_.push_back(form_structure.release());
613 } 635 }
614 636
615 // If none of the forms were parsed, no use querying the server. 637 // If none of the forms were parsed, no use querying the server.
616 if (!form_structures_.empty() && !disable_download_manager_requests_) 638 if (!form_structures_.empty() && !disable_download_manager_requests_)
617 download_manager_.StartQueryRequest(form_structures_); 639 download_manager_.StartQueryRequest(form_structures_);
618 } 640 }
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
666 return std::string(); 688 return std::string();
667 689
668 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id); 690 std::map<int, std::string>::const_iterator iter = id_guid_map_.find(id);
669 if (iter == id_guid_map_.end()) { 691 if (iter == id_guid_map_.end()) {
670 NOTREACHED(); 692 NOTREACHED();
671 return std::string(); 693 return std::string();
672 } 694 }
673 695
674 return iter->second; 696 return iter->second;
675 } 697 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698