OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/common/form_data.h" | 9 #include "chrome/common/form_data.h" |
10 #include "chrome/common/form_data_predictions.h" | 10 #include "chrome/common/form_data_predictions.h" |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 for (size_t j = 0; j < control_elements.size(); ++j) { | 73 for (size_t j = 0; j < control_elements.size(); ++j) { |
74 WebFormControlElement element = control_elements[j]; | 74 WebFormControlElement element = control_elements[j]; |
75 | 75 |
76 // Save original values of <select> elements so we can restore them | 76 // Save original values of <select> elements so we can restore them |
77 // when |ClearFormWithNode()| is invoked. | 77 // when |ClearFormWithNode()| is invoked. |
78 if (IsSelectElement(element)) { | 78 if (IsSelectElement(element)) { |
79 const WebSelectElement select_element = | 79 const WebSelectElement select_element = |
80 element.toConst<WebSelectElement>(); | 80 element.toConst<WebSelectElement>(); |
81 initial_select_values_.insert(std::make_pair(select_element, | 81 initial_select_values_.insert(std::make_pair(select_element, |
82 select_element.value())); | 82 select_element.value())); |
| 83 } else { |
| 84 const WebInputElement input_element = |
| 85 element.toConst<WebInputElement>(); |
| 86 if (IsRadioButtonElement(&input_element) || |
| 87 IsCheckboxElement(&input_element)) |
| 88 initial_checked_state_.insert( |
| 89 std::make_pair(input_element, input_element.isChecked())); |
83 } | 90 } |
84 } | 91 } |
85 | 92 |
86 // To avoid overly expensive computation, we impose a minimum number of | 93 // To avoid overly expensive computation, we impose a minimum number of |
87 // allowable fields. The corresponding maximum number of allowable fields | 94 // allowable fields. The corresponding maximum number of allowable fields |
88 // is imposed by WebFormElementToFormData(). | 95 // is imposed by WebFormElementToFormData(). |
89 if (control_elements.size() < kRequiredAutofillFields) | 96 if (control_elements.size() < kRequiredAutofillFields) |
90 continue; | 97 continue; |
91 | 98 |
92 FormData form; | 99 FormData form; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 if (!element_frame || element_frame == &frame) | 138 if (!element_frame || element_frame == &frame) |
132 select_values_to_delete.push_back(it->first); | 139 select_values_to_delete.push_back(it->first); |
133 } | 140 } |
134 } | 141 } |
135 | 142 |
136 for (std::vector<WebSelectElement>::const_iterator it = | 143 for (std::vector<WebSelectElement>::const_iterator it = |
137 select_values_to_delete.begin(); | 144 select_values_to_delete.begin(); |
138 it != select_values_to_delete.end(); ++it) { | 145 it != select_values_to_delete.end(); ++it) { |
139 initial_select_values_.erase(*it); | 146 initial_select_values_.erase(*it); |
140 } | 147 } |
| 148 |
| 149 std::vector<WebInputElement> checked_states_to_delete; |
| 150 for (std::map<const WebInputElement, bool>::const_iterator it = |
| 151 initial_checked_state_.begin(); |
| 152 it != initial_checked_state_.end(); ++it) { |
| 153 WebFormElement form_element = it->first.form(); |
| 154 if (form_element.isNull()) { |
| 155 checked_states_to_delete.push_back(it->first); |
| 156 } else { |
| 157 const WebFrame* element_frame = form_element.document().frame(); |
| 158 if (!element_frame || element_frame == &frame) |
| 159 checked_states_to_delete.push_back(it->first); |
| 160 } |
| 161 } |
| 162 |
| 163 for (std::vector<WebInputElement>::const_iterator it = |
| 164 checked_states_to_delete.begin(); |
| 165 it != checked_states_to_delete.end(); ++it) { |
| 166 initial_checked_state_.erase(*it); |
| 167 } |
141 } | 168 } |
142 | 169 |
143 bool FormCache::ClearFormWithElement(const WebInputElement& element) { | 170 bool FormCache::ClearFormWithElement(const WebInputElement& element) { |
144 WebFormElement form_element = element.form(); | 171 WebFormElement form_element = element.form(); |
145 if (form_element.isNull()) | 172 if (form_element.isNull()) |
146 return false; | 173 return false; |
147 | 174 |
148 std::vector<WebFormControlElement> control_elements; | 175 std::vector<WebFormControlElement> control_elements; |
149 ExtractAutofillableElements(form_element, autofill::REQUIRE_NONE, | 176 ExtractAutofillableElements(form_element, autofill::REQUIRE_NONE, |
150 &control_elements); | 177 &control_elements); |
151 for (size_t i = 0; i < control_elements.size(); ++i) { | 178 for (size_t i = 0; i < control_elements.size(); ++i) { |
152 WebFormControlElement control_element = control_elements[i]; | 179 WebFormControlElement control_element = control_elements[i]; |
153 WebInputElement* input_element = toWebInputElement(&control_element); | 180 WebInputElement* input_element = toWebInputElement(&control_element); |
154 if (IsTextInput(input_element)) { | 181 if (IsTextInput(input_element)) { |
155 // We don't modify the value of disabled fields. | 182 // We don't modify the value of disabled fields. |
156 if (!input_element->isEnabled()) | 183 if (!input_element->isEnabled()) |
157 continue; | 184 continue; |
158 | 185 |
159 input_element->setValue(string16(), true); | 186 input_element->setValue(string16(), true); |
160 input_element->setAutofilled(false); | 187 input_element->setAutofilled(false); |
161 | 188 |
162 // Clearing the value in the focused node (above) can cause selection | 189 // Clearing the value in the focused node (above) can cause selection |
163 // to be lost. We force selection range to restore the text cursor. | 190 // to be lost. We force selection range to restore the text cursor. |
164 if (element == *input_element) { | 191 if (element == *input_element) { |
165 int length = input_element->value().length(); | 192 int length = input_element->value().length(); |
166 input_element->setSelectionRange(length, length); | 193 input_element->setSelectionRange(length, length); |
167 } | 194 } |
168 } else { | 195 } else if (IsSelectElement(control_element)) { |
169 DCHECK(IsSelectElement(control_element)); | |
170 WebSelectElement select_element = control_element.to<WebSelectElement>(); | 196 WebSelectElement select_element = control_element.to<WebSelectElement>(); |
171 | 197 |
172 std::map<const WebSelectElement, string16>::const_iterator | 198 std::map<const WebSelectElement, string16>::const_iterator |
173 initial_value_iter = initial_select_values_.find(select_element); | 199 initial_value_iter = initial_select_values_.find(select_element); |
174 if (initial_value_iter != initial_select_values_.end() && | 200 if (initial_value_iter != initial_select_values_.end() && |
175 select_element.value() != initial_value_iter->second) { | 201 select_element.value() != initial_value_iter->second) { |
176 select_element.setValue(initial_value_iter->second); | 202 select_element.setValue(initial_value_iter->second); |
177 select_element.dispatchFormControlChangeEvent(); | 203 select_element.dispatchFormControlChangeEvent(); |
178 } | 204 } |
| 205 } else { |
| 206 WebInputElement input_element = control_element.to<WebInputElement>(); |
| 207 DCHECK(IsRadioButtonElement(&input_element) || |
| 208 IsCheckboxElement(&input_element)); |
| 209 std::map<const WebInputElement, bool>::const_iterator it = |
| 210 initial_checked_state_.find(input_element); |
| 211 if (it != initial_checked_state_.end() && |
| 212 input_element.isChecked() != it->second) { |
| 213 input_element.setChecked(it->second, true); |
| 214 } |
179 } | 215 } |
180 } | 216 } |
181 | 217 |
182 return true; | 218 return true; |
183 } | 219 } |
184 | 220 |
185 bool FormCache::ShowPredictions(const FormDataPredictions& form) { | 221 bool FormCache::ShowPredictions(const FormDataPredictions& form) { |
186 DCHECK_EQ(form.data.fields.size(), form.fields.size()); | 222 DCHECK_EQ(form.data.fields.size(), form.fields.size()); |
187 | 223 |
188 // Find the form. | 224 // Find the form. |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 UTF8ToUTF16(form.experiment_id)); | 280 UTF8ToUTF16(form.experiment_id)); |
245 if (!element->hasAttribute("placeholder")) | 281 if (!element->hasAttribute("placeholder")) |
246 element->setAttribute("placeholder", WebString(UTF8ToUTF16(placeholder))); | 282 element->setAttribute("placeholder", WebString(UTF8ToUTF16(placeholder))); |
247 element->setAttribute("title", WebString(title)); | 283 element->setAttribute("title", WebString(title)); |
248 } | 284 } |
249 | 285 |
250 return true; | 286 return true; |
251 } | 287 } |
252 | 288 |
253 } // namespace autofill | 289 } // namespace autofill |
OLD | NEW |