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/renderer/password_autocomplete_manager.h" | 5 #include "chrome/renderer/password_autocomplete_manager.h" |
6 | 6 |
7 #include "base/message_loop.h" | 7 #include "base/message_loop.h" |
8 #include "base/scoped_ptr.h" | 8 #include "base/scoped_ptr.h" |
9 #include "chrome/common/render_messages.h" | 9 #include "chrome/common/render_messages.h" |
10 #include "chrome/renderer/render_view.h" | 10 #include "chrome/renderer/render_view.h" |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
46 // in |data|, and add results to |result|. | 46 // in |data|, and add results to |result|. |
47 static bool FindFormInputElements(WebKit::WebFormElement* fe, | 47 static bool FindFormInputElements(WebKit::WebFormElement* fe, |
48 const webkit_glue::FormData& data, | 48 const webkit_glue::FormData& data, |
49 FormElements* result) { | 49 FormElements* result) { |
50 // Loop through the list of elements we need to find on the form in order to | 50 // Loop through the list of elements we need to find on the form in order to |
51 // autocomplete it. If we don't find any one of them, abort processing this | 51 // autocomplete it. If we don't find any one of them, abort processing this |
52 // form; it can't be the right one. | 52 // form; it can't be the right one. |
53 for (size_t j = 0; j < data.fields.size(); j++) { | 53 for (size_t j = 0; j < data.fields.size(); j++) { |
54 WebKit::WebVector<WebKit::WebNode> temp_elements; | 54 WebKit::WebVector<WebKit::WebNode> temp_elements; |
55 fe->getNamedElements(data.fields[j].name(), temp_elements); | 55 fe->getNamedElements(data.fields[j].name(), temp_elements); |
56 if (temp_elements.isEmpty()) { | 56 |
57 // We didn't find a required element. This is not the right form. | 57 // Match the first input element, if any. |
58 // Make sure no input elements from a partially matched form in this | 58 // |getNamedElements| may return non-input elements where the names match, |
59 // iteration remain in the result set. | 59 // so the results are filtered for input elements. |
60 // Note: clear will remove a reference from each InputElement. | 60 // If more than one match is made, then we have ambiguity (due to misuse |
61 // of "name" attribute) so is considered not found. | |
Ilya Sherman
2011/01/19 04:58:35
nit: Should be "so it is" or something like that
dhollowa
2011/01/19 16:36:57
Done.
| |
62 bool found_input = false; | |
63 for (size_t i = 0; i < temp_elements.size(); ++i) { | |
64 // This element matched, add it to our temporary result. It's possible | |
65 // there are multiple matches, but for purposes of identifying the form | |
66 // one suffices and if some function needs to deal with multiple matching | |
67 // elements it can get at them through the FormElement*. | |
68 // Note: This assignment adds a reference to the InputElement. | |
Ilya Sherman
2011/01/19 04:58:35
nit: I think this comment should be moved down to
dhollowa
2011/01/19 16:36:57
Done.
| |
69 if (temp_elements[i].to<WebKit::WebElement>().hasTagName("input")) { | |
70 // Check for a non-unique match. | |
71 if (found_input) { | |
72 found_input = false; | |
73 break; | |
74 } | |
75 result->input_elements[data.fields[j].name()] = | |
76 temp_elements[i].to<WebKit::WebInputElement>(); | |
77 found_input = true; | |
78 } | |
79 } | |
80 | |
81 // A required element was not found. This is not the right form. | |
82 // Make sure no input elements from a partially matched form in this | |
83 // iteration remain in the result set. | |
84 // Note: clear will remove a reference from each InputElement. | |
85 if (!found_input) { | |
61 result->input_elements.clear(); | 86 result->input_elements.clear(); |
62 return false; | 87 return false; |
63 } | 88 } |
64 // This element matched, add it to our temporary result. It's possible there | |
65 // are multiple matches, but for purposes of identifying the form one | |
66 // suffices and if some function needs to deal with multiple matching | |
67 // elements it can get at them through the FormElement*. | |
68 // Note: This assignment adds a reference to the InputElement. | |
69 result->input_elements[data.fields[j].name()] = | |
70 temp_elements[0].to<WebKit::WebInputElement>(); | |
71 } | 89 } |
72 return true; | 90 return true; |
73 } | 91 } |
74 | 92 |
75 // Helper to locate form elements identified by |data|. | 93 // Helper to locate form elements identified by |data|. |
76 void FindFormElements(WebKit::WebView* view, | 94 void FindFormElements(WebKit::WebView* view, |
77 const webkit_glue::FormData& data, | 95 const webkit_glue::FormData& data, |
78 FormElementsList* results) { | 96 FormElementsList* results) { |
79 DCHECK(view); | 97 DCHECK(view); |
80 DCHECK(results); | 98 DCHECK(results); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
121 bool FillForm(FormElements* fe, const webkit_glue::FormData& data) { | 139 bool FillForm(FormElements* fe, const webkit_glue::FormData& data) { |
122 if (!fe->form_element.autoComplete()) | 140 if (!fe->form_element.autoComplete()) |
123 return false; | 141 return false; |
124 | 142 |
125 std::map<string16, string16> data_map; | 143 std::map<string16, string16> data_map; |
126 for (size_t i = 0; i < data.fields.size(); i++) | 144 for (size_t i = 0; i < data.fields.size(); i++) |
127 data_map[data.fields[i].name()] = data.fields[i].value(); | 145 data_map[data.fields[i].name()] = data.fields[i].value(); |
128 | 146 |
129 for (FormInputElementMap::iterator it = fe->input_elements.begin(); | 147 for (FormInputElementMap::iterator it = fe->input_elements.begin(); |
130 it != fe->input_elements.end(); ++it) { | 148 it != fe->input_elements.end(); ++it) { |
131 WebKit::WebInputElement& element = it->second; | 149 WebKit::WebInputElement element = it->second; |
132 if (!element.value().isEmpty()) // Don't overwrite pre-filled values. | 150 if (!element.value().isEmpty()) // Don't overwrite pre-filled values. |
133 continue; | 151 continue; |
134 if (element.isPasswordField() && | 152 if (element.isPasswordField() && |
135 (!element.isEnabledFormControl() || element.hasAttribute("readonly"))) { | 153 (!element.isEnabledFormControl() || element.hasAttribute("readonly"))) { |
136 continue; // Don't fill uneditable password fields. | 154 continue; // Don't fill uneditable password fields. |
137 } | 155 } |
138 // TODO(tkent): Check maxlength and pattern. | 156 // TODO(tkent): Check maxlength and pattern. |
139 element.setValue(data_map[it->first]); | 157 element.setValue(data_map[it->first]); |
140 element.setAutofilled(true); | 158 element.setAutofilled(true); |
141 element.dispatchFormControlChangeEvent(); | 159 element.dispatchFormControlChangeEvent(); |
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
490 username_element->setSelectionRange(current_username.length(), | 508 username_element->setSelectionRange(current_username.length(), |
491 username.length()); | 509 username.length()); |
492 } | 510 } |
493 | 511 |
494 SetElementAutofilled(username_element, true); | 512 SetElementAutofilled(username_element, true); |
495 if (IsElementEditable(*password_element)) | 513 if (IsElementEditable(*password_element)) |
496 password_element->setValue(password); | 514 password_element->setValue(password); |
497 SetElementAutofilled(password_element, true); | 515 SetElementAutofilled(password_element, true); |
498 return true; | 516 return true; |
499 } | 517 } |
OLD | NEW |