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

Side by Side Diff: components/autofill/content/renderer/form_cache.cc

Issue 2650623002: Use explicit WebString conversions in autofill (Closed)
Patch Set: . Created 3 years, 11 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
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 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 "components/autofill/content/renderer/form_cache.h" 5 #include "components/autofill/content/renderer/form_cache.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "base/macros.h" 8 #include "base/macros.h"
9 #include "base/stl_util.h" 9 #include "base/stl_util.h"
10 #include "base/strings/utf_string_conversions.h" 10 #include "base/strings/utf_string_conversions.h"
(...skipping 23 matching lines...) Expand all
34 using blink::WebSelectElement; 34 using blink::WebSelectElement;
35 using blink::WebString; 35 using blink::WebString;
36 using blink::WebVector; 36 using blink::WebVector;
37 37
38 namespace autofill { 38 namespace autofill {
39 39
40 namespace { 40 namespace {
41 41
42 void LogDeprecationMessages(const WebFormControlElement& element) { 42 void LogDeprecationMessages(const WebFormControlElement& element) {
43 std::string autocomplete_attribute = 43 std::string autocomplete_attribute =
44 base::UTF16ToUTF8(base::StringPiece16( 44 element.getAttribute("autocomplete").utf8();
45 element.getAttribute("autocomplete")));
46 45
47 static const char* const deprecated[] = { "region", "locality" }; 46 static const char* const deprecated[] = { "region", "locality" };
48 for (const char* str : deprecated) { 47 for (const char* str : deprecated) {
49 if (autocomplete_attribute.find(str) == std::string::npos) 48 if (autocomplete_attribute.find(str) == std::string::npos)
50 continue; 49 continue;
51 std::string msg = std::string("autocomplete='") + str + 50 std::string msg = std::string("autocomplete='") + str +
52 "' is deprecated and will soon be ignored. See http://goo.gl/YjeSsW"; 51 "' is deprecated and will soon be ignored. See http://goo.gl/YjeSsW";
53 WebConsoleMessage console_message = WebConsoleMessage( 52 WebConsoleMessage console_message = WebConsoleMessage(
54 WebConsoleMessage::LevelWarning, 53 WebConsoleMessage::LevelWarning, WebString::fromASCII(msg));
55 WebString(base::ASCIIToUTF16(msg)));
56 element.document().frame()->addMessageToConsole(console_message); 54 element.document().frame()->addMessageToConsole(console_message);
57 } 55 }
58 } 56 }
59 57
60 // Determines whether the form is interesting enough to send to the browser 58 // Determines whether the form is interesting enough to send to the browser
61 // for further operations. 59 // for further operations.
62 bool IsFormInteresting(const FormData& form, size_t num_editable_elements) { 60 bool IsFormInteresting(const FormData& form, size_t num_editable_elements) {
63 if (form.fields.empty()) 61 if (form.fields.empty())
64 return false; 62 return false;
65 63
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after
205 203
206 // Don't clear field that was not autofilled 204 // Don't clear field that was not autofilled
207 if (!control_element.isAutofilled()) 205 if (!control_element.isAutofilled())
208 continue; 206 continue;
209 207
210 control_element.setAutofilled(false); 208 control_element.setAutofilled(false);
211 209
212 WebInputElement* input_element = toWebInputElement(&control_element); 210 WebInputElement* input_element = toWebInputElement(&control_element);
213 if (form_util::IsTextInput(input_element) || 211 if (form_util::IsTextInput(input_element) ||
214 form_util::IsMonthInput(input_element)) { 212 form_util::IsMonthInput(input_element)) {
215 input_element->setValue(base::string16(), true); 213 input_element->setValue(blink::WebString(), true);
216 214
217 // Clearing the value in the focused node (above) can cause selection 215 // Clearing the value in the focused node (above) can cause selection
218 // to be lost. We force selection range to restore the text cursor. 216 // to be lost. We force selection range to restore the text cursor.
219 if (element == *input_element) { 217 if (element == *input_element) {
220 int length = input_element->value().length(); 218 int length = input_element->value().length();
221 input_element->setSelectionRange(length, length); 219 input_element->setSelectionRange(length, length);
222 } 220 }
223 } else if (form_util::IsTextAreaElement(control_element)) { 221 } else if (form_util::IsTextAreaElement(control_element)) {
224 control_element.setValue(base::string16(), true); 222 control_element.setValue(blink::WebString(), true);
225 } else if (form_util::IsSelectElement(control_element)) { 223 } else if (form_util::IsSelectElement(control_element)) {
226 WebSelectElement select_element = control_element.to<WebSelectElement>(); 224 WebSelectElement select_element = control_element.to<WebSelectElement>();
227 225
228 std::map<const WebSelectElement, base::string16>::const_iterator 226 std::map<const WebSelectElement, base::string16>::const_iterator
229 initial_value_iter = initial_select_values_.find(select_element); 227 initial_value_iter = initial_select_values_.find(select_element);
230 if (initial_value_iter != initial_select_values_.end() && 228 if (initial_value_iter != initial_select_values_.end() &&
231 select_element.value() != initial_value_iter->second) { 229 select_element.value().utf16() != initial_value_iter->second) {
232 select_element.setValue(initial_value_iter->second, true); 230 select_element.setValue(
231 blink::WebString::fromUTF16(initial_value_iter->second), true);
233 } 232 }
234 } else { 233 } else {
235 WebInputElement input_element = control_element.to<WebInputElement>(); 234 WebInputElement input_element = control_element.to<WebInputElement>();
236 DCHECK(form_util::IsCheckableElement(&input_element)); 235 DCHECK(form_util::IsCheckableElement(&input_element));
237 std::map<const WebInputElement, bool>::const_iterator it = 236 std::map<const WebInputElement, bool>::const_iterator it =
238 initial_checked_state_.find(input_element); 237 initial_checked_state_.find(input_element);
239 if (it != initial_checked_state_.end() && 238 if (it != initial_checked_state_.end() &&
240 input_element.isChecked() != it->second) { 239 input_element.isChecked() != it->second) {
241 input_element.setChecked(it->second, true); 240 input_element.setChecked(it->second, true);
242 } 241 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 if (control_elements.size() != form.fields.size()) { 293 if (control_elements.size() != form.fields.size()) {
295 // Keep things simple. Don't show predictions for forms that were modified 294 // Keep things simple. Don't show predictions for forms that were modified
296 // between page load and the server's response to our query. 295 // between page load and the server's response to our query.
297 return false; 296 return false;
298 } 297 }
299 298
300 for (size_t i = 0; i < control_elements.size(); ++i) { 299 for (size_t i = 0; i < control_elements.size(); ++i) {
301 WebFormControlElement& element = control_elements[i]; 300 WebFormControlElement& element = control_elements[i];
302 301
303 const FormFieldData& field_data = form.data.fields[i]; 302 const FormFieldData& field_data = form.data.fields[i];
304 if (base::string16(element.nameForAutofill()) != field_data.name) { 303 if (element.nameForAutofill().utf16() != field_data.name) {
305 // Keep things simple. Don't show predictions for elements whose names 304 // Keep things simple. Don't show predictions for elements whose names
306 // were modified between page load and the server's response to our query. 305 // were modified between page load and the server's response to our query.
307 continue; 306 continue;
308 } 307 }
309 308
310 static const size_t kMaxLabelSize = 100; 309 static const size_t kMaxLabelSize = 100;
311 const base::string16 truncated_label = field_data.label.substr( 310 const base::string16 truncated_label = field_data.label.substr(
312 0, std::min(field_data.label.length(), kMaxLabelSize)); 311 0, std::min(field_data.label.length(), kMaxLabelSize));
313 312
314 const FormFieldDataPredictions& field = form.fields[i]; 313 const FormFieldDataPredictions& field = form.fields[i];
315 std::vector<base::string16> replacements; 314 std::vector<base::string16> replacements;
316 315
317 base::string16 overall_type = base::UTF8ToUTF16(field.overall_type); 316 base::string16 overall_type = base::UTF8ToUTF16(field.overall_type);
318 317
319 replacements.push_back(overall_type); 318 replacements.push_back(overall_type);
320 replacements.push_back(base::UTF8ToUTF16(field.server_type)); 319 replacements.push_back(base::UTF8ToUTF16(field.server_type));
321 replacements.push_back(base::UTF8ToUTF16(field.heuristic_type)); 320 replacements.push_back(base::UTF8ToUTF16(field.heuristic_type));
322 replacements.push_back(truncated_label); 321 replacements.push_back(truncated_label);
323 replacements.push_back(base::UTF8ToUTF16(field.parseable_name)); 322 replacements.push_back(base::UTF8ToUTF16(field.parseable_name));
324 replacements.push_back(base::UTF8ToUTF16(field.signature)); 323 replacements.push_back(base::UTF8ToUTF16(field.signature));
325 replacements.push_back(base::UTF8ToUTF16(form.signature)); 324 replacements.push_back(base::UTF8ToUTF16(form.signature));
326 const base::string16 title = l10n_util::GetStringFUTF16( 325 const base::string16 title = l10n_util::GetStringFUTF16(
327 IDS_AUTOFILL_SHOW_PREDICTIONS_TITLE, replacements, nullptr); 326 IDS_AUTOFILL_SHOW_PREDICTIONS_TITLE, replacements, nullptr);
328 element.setAttribute("title", WebString(title)); 327 element.setAttribute("title", WebString::fromUTF16(title));
329 328
330 element.setAttribute("autofill-prediction", WebString(overall_type)); 329 element.setAttribute("autofill-prediction",
330 WebString::fromUTF16(overall_type));
331 } 331 }
332 332
333 return true; 333 return true;
334 } 334 }
335 335
336 size_t FormCache::ScanFormControlElements( 336 size_t FormCache::ScanFormControlElements(
337 const std::vector<WebFormControlElement>& control_elements, 337 const std::vector<WebFormControlElement>& control_elements,
338 bool log_deprecation_messages) { 338 bool log_deprecation_messages) {
339 size_t num_editable_elements = 0; 339 size_t num_editable_elements = 0;
340 for (size_t i = 0; i < control_elements.size(); ++i) { 340 for (size_t i = 0; i < control_elements.size(); ++i) {
(...skipping 16 matching lines...) Expand all
357 return num_editable_elements; 357 return num_editable_elements;
358 } 358 }
359 359
360 void FormCache::SaveInitialValues( 360 void FormCache::SaveInitialValues(
361 const std::vector<WebFormControlElement>& control_elements) { 361 const std::vector<WebFormControlElement>& control_elements) {
362 for (const WebFormControlElement& element : control_elements) { 362 for (const WebFormControlElement& element : control_elements) {
363 if (form_util::IsSelectElement(element)) { 363 if (form_util::IsSelectElement(element)) {
364 const WebSelectElement select_element = 364 const WebSelectElement select_element =
365 element.toConst<WebSelectElement>(); 365 element.toConst<WebSelectElement>();
366 initial_select_values_.insert( 366 initial_select_values_.insert(
367 std::make_pair(select_element, select_element.value())); 367 std::make_pair(select_element, select_element.value().utf16()));
368 } else { 368 } else {
369 const WebInputElement* input_element = toWebInputElement(&element); 369 const WebInputElement* input_element = toWebInputElement(&element);
370 if (form_util::IsCheckableElement(input_element)) { 370 if (form_util::IsCheckableElement(input_element)) {
371 initial_checked_state_.insert( 371 initial_checked_state_.insert(
372 std::make_pair(*input_element, input_element->isChecked())); 372 std::make_pair(*input_element, input_element->isChecked()));
373 } 373 }
374 } 374 }
375 } 375 }
376 } 376 }
377 377
378 } // namespace autofill 378 } // namespace autofill
OLDNEW
« no previous file with comments | « components/autofill/content/renderer/form_autofill_util.cc ('k') | components/autofill/content/renderer/form_classifier.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698