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

Side by Side Diff: chrome/renderer/autofill/form_cache.cc

Issue 7983028: Don't cache WebFrame pointers for Autofill, as these can become stale. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 3 months 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
« no previous file with comments | « chrome/renderer/autofill/form_cache.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/renderer/autofill/form_cache.h" 5 #include "chrome/renderer/autofill/form_cache.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h" 8 #include "base/utf_string_conversions.h"
9 #include "chrome/renderer/autofill/form_autofill_util.h" 9 #include "chrome/renderer/autofill/form_autofill_util.h"
10 #include "grit/generated_resources.h" 10 #include "grit/generated_resources.h"
11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 11 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement .h" 12 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormControlElement .h"
13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h" 13 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFormElement.h"
14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 14 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h" 15 #include "third_party/WebKit/Source/WebKit/chromium/public/WebInputElement.h"
16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSelectElement.h" 16 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSelectElement.h"
17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" 17 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h" 18 #include "third_party/WebKit/Source/WebKit/chromium/public/WebVector.h"
19 #include "ui/base/l10n/l10n_util.h" 19 #include "ui/base/l10n/l10n_util.h"
20 #include "webkit/glue/form_data.h" 20 #include "webkit/glue/form_data.h"
21 #include "webkit/glue/form_data_predictions.h" 21 #include "webkit/glue/form_data_predictions.h"
22 #include "webkit/glue/form_field.h" 22 #include "webkit/glue/form_field.h"
23 #include "webkit/glue/form_field_predictions.h" 23 #include "webkit/glue/form_field_predictions.h"
24 24
25 using WebKit::WebDocument;
25 using WebKit::WebFormControlElement; 26 using WebKit::WebFormControlElement;
26 using WebKit::WebFormElement; 27 using WebKit::WebFormElement;
27 using WebKit::WebFrame; 28 using WebKit::WebFrame;
28 using WebKit::WebInputElement; 29 using WebKit::WebInputElement;
29 using WebKit::WebSelectElement; 30 using WebKit::WebSelectElement;
30 using WebKit::WebString; 31 using WebKit::WebString;
31 using WebKit::WebVector; 32 using WebKit::WebVector;
32 using webkit_glue::FormData; 33 using webkit_glue::FormData;
33 using webkit_glue::FormDataPredictions; 34 using webkit_glue::FormDataPredictions;
34 35
(...skipping 17 matching lines...) Expand all
52 namespace autofill { 53 namespace autofill {
53 54
54 FormCache::FormCache() { 55 FormCache::FormCache() {
55 } 56 }
56 57
57 FormCache::~FormCache() { 58 FormCache::~FormCache() {
58 } 59 }
59 60
60 void FormCache::ExtractForms(const WebFrame& frame, 61 void FormCache::ExtractForms(const WebFrame& frame,
61 std::vector<FormData>* forms) { 62 std::vector<FormData>* forms) {
62 // Reset the vector of FormElements for this frame. 63 // Reset the cache for this frame.
63 ResetFrame(frame); 64 ResetFrame(frame);
64 web_frames_.insert(&frame); 65
66 WebDocument document = frame.document();
67 if (document.isNull())
68 return;
69
70 web_documents_.insert(document);
65 71
66 WebVector<WebFormElement> web_forms; 72 WebVector<WebFormElement> web_forms;
67 frame.document().forms(web_forms); 73 document.forms(web_forms);
68 74
69 size_t num_fields_seen = 0; 75 size_t num_fields_seen = 0;
70 for (size_t i = 0; i < web_forms.size(); ++i) { 76 for (size_t i = 0; i < web_forms.size(); ++i) {
71 WebFormElement form_element = web_forms[i]; 77 WebFormElement form_element = web_forms[i];
72 78
73 std::vector<WebFormControlElement> control_elements; 79 std::vector<WebFormControlElement> control_elements;
74 ExtractAutofillableElements(form_element, &control_elements); 80 ExtractAutofillableElements(form_element, &control_elements);
75 for (size_t j = 0; j < control_elements.size(); ++j) { 81 for (size_t j = 0; j < control_elements.size(); ++j) {
76 WebFormControlElement element = control_elements[j]; 82 WebFormControlElement element = control_elements[j];
77 83
(...skipping 20 matching lines...) Expand all
98 num_fields_seen += form.fields.size(); 104 num_fields_seen += form.fields.size();
99 if (num_fields_seen > kMaxParseableFields) 105 if (num_fields_seen > kMaxParseableFields)
100 break; 106 break;
101 107
102 if (form.fields.size() >= kRequiredAutofillFields) 108 if (form.fields.size() >= kRequiredAutofillFields)
103 forms->push_back(form); 109 forms->push_back(form);
104 } 110 }
105 } 111 }
106 112
107 void FormCache::ResetFrame(const WebFrame& frame) { 113 void FormCache::ResetFrame(const WebFrame& frame) {
108 web_frames_.erase(&frame); 114 std::vector<WebDocument> documents_to_delete;
115 for (std::set<WebDocument>::const_iterator it = web_documents_.begin();
116 it != web_documents_.end(); ++it) {
117 const WebFrame* document_frame = it->frame();
118 if (!document_frame || document_frame == &frame)
119 documents_to_delete.push_back(*it);
120 }
109 121
110 std::vector<WebSelectElement> to_delete; 122 for (std::vector<WebDocument>::const_iterator it =
123 documents_to_delete.begin();
124 it != documents_to_delete.end(); ++it) {
125 web_documents_.erase(*it);
126 }
127
128 std::vector<WebSelectElement> select_values_to_delete;
111 for (std::map<const WebSelectElement, string16>::const_iterator it = 129 for (std::map<const WebSelectElement, string16>::const_iterator it =
112 initial_select_values_.begin(); 130 initial_select_values_.begin();
113 it != initial_select_values_.end(); ++it) { 131 it != initial_select_values_.end(); ++it) {
114 WebFormElement form_element = it->first.form(); 132 WebFormElement form_element = it->first.form();
115 if (form_element.isNull() || form_element.document().frame() == &frame) 133 if (form_element.isNull()) {
116 to_delete.push_back(it->first); 134 select_values_to_delete.push_back(it->first);
135 } else {
136 const WebFrame* element_frame = form_element.document().frame();
137 if (!element_frame || element_frame == &frame)
138 select_values_to_delete.push_back(it->first);
139 }
117 } 140 }
118 141
119 for (std::vector<WebSelectElement>::const_iterator it = 142 for (std::vector<WebSelectElement>::const_iterator it =
120 to_delete.begin(); 143 select_values_to_delete.begin();
121 it != to_delete.end(); ++it) { 144 it != select_values_to_delete.end(); ++it) {
122 initial_select_values_.erase(*it); 145 initial_select_values_.erase(*it);
123 } 146 }
124 } 147 }
125 148
126 bool FormCache::ClearFormWithElement(const WebInputElement& element) { 149 bool FormCache::ClearFormWithElement(const WebInputElement& element) {
127 WebFormElement form_element = element.form(); 150 WebFormElement form_element = element.form();
128 if (form_element.isNull()) 151 if (form_element.isNull())
129 return false; 152 return false;
130 153
131 std::vector<WebFormControlElement> control_elements; 154 std::vector<WebFormControlElement> control_elements;
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
163 186
164 return true; 187 return true;
165 } 188 }
166 189
167 bool FormCache::ShowPredictions(const FormDataPredictions& form) { 190 bool FormCache::ShowPredictions(const FormDataPredictions& form) {
168 DCHECK_EQ(form.data.fields.size(), form.fields.size()); 191 DCHECK_EQ(form.data.fields.size(), form.fields.size());
169 192
170 // Find the form. 193 // Find the form.
171 bool found_form = false; 194 bool found_form = false;
172 WebFormElement form_element; 195 WebFormElement form_element;
173 for (std::set<const WebFrame*>::const_iterator it = web_frames_.begin(); 196 for (std::set<WebDocument>::const_iterator it = web_documents_.begin();
174 it != web_frames_.end() && !found_form; ++it) { 197 it != web_documents_.end() && !found_form; ++it) {
175 WebVector<WebFormElement> web_forms; 198 WebVector<WebFormElement> web_forms;
176 (*it)->document().forms(web_forms); 199 it->forms(web_forms);
177 200
178 for (size_t i = 0; i < web_forms.size(); ++i) { 201 for (size_t i = 0; i < web_forms.size(); ++i) {
179 form_element = web_forms[i]; 202 form_element = web_forms[i];
180 203
181 // Note: matching on the form name here which is not guaranteed to be 204 // Note: matching on the form name here which is not guaranteed to be
182 // unique for the page, nor is it guaranteed to be non-empty. Ideally, we 205 // unique for the page, nor is it guaranteed to be non-empty. Ideally, we
183 // would have a way to uniquely identify the form cross-process. For now, 206 // would have a way to uniquely identify the form cross-process. For now,
184 // we'll check form name and form action for identity. 207 // we'll check form name and form action for identity.
185 // Also note that WebString() == WebString(string16()) does not evaluate 208 // Also note that WebString() == WebString(string16()) does not evaluate
186 // to |true| -- WebKit distinguishes between a "null" string (lhs) and an 209 // to |true| -- WebKit distinguishes between a "null" string (lhs) and an
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
225 UTF8ToUTF16(form.experiment_id)); 248 UTF8ToUTF16(form.experiment_id));
226 if (!element->hasAttribute("placeholder")) 249 if (!element->hasAttribute("placeholder"))
227 element->setAttribute("placeholder", WebString(UTF8ToUTF16(placeholder))); 250 element->setAttribute("placeholder", WebString(UTF8ToUTF16(placeholder)));
228 element->setAttribute("title", WebString(title)); 251 element->setAttribute("title", WebString(title));
229 } 252 }
230 253
231 return true; 254 return true;
232 } 255 }
233 256
234 } // namespace autofill 257 } // namespace autofill
OLDNEW
« no previous file with comments | « chrome/renderer/autofill/form_cache.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698