summaryrefslogtreecommitdiffstats
path: root/webkit/glue/autofill_form.cc
blob: d6988bd5b0f17a94dc804cbdf9c3bf73bbdbac27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "config.h"
#include "base/compiler_specific.h"

MSVC_PUSH_WARNING_LEVEL(0);
#include "Frame.h"
#include "HTMLFormElement.h"
#include "HTMLInputElement.h"
#include "HTMLNames.h"
MSVC_POP_WARNING();

#undef LOG

#include "base/basictypes.h"
#include "base/logging.h"
#include "base/string_util.h"
#include "webkit/glue/autofill_form.h"
#include "webkit/glue/glue_util.h"

AutofillForm* AutofillForm::CreateAutofillForm(
    WebCore::HTMLFormElement* form) {

  DCHECK(form);

  WebCore::Frame* frame = form->document()->frame();

  if (!frame)
    return NULL;

  WebCore::FrameLoader* loader = frame->loader();

  if (!loader)
    return NULL;

  const WTF::Vector<WebCore::HTMLFormControlElement*>& form_elements =
      form->formElements;

  // Construct a new AutofillForm.
  AutofillForm* result = new AutofillForm();

  size_t form_element_count = form_elements.size();

  for (size_t i = 0; i < form_element_count; i++) {
    WebCore::HTMLFormControlElement* form_element = form_elements[i];

    if (!form_element->hasLocalName(WebCore::HTMLNames::inputTag))
      continue;

    WebCore::HTMLInputElement* input_element =
        static_cast<WebCore::HTMLInputElement*>(form_element);
    if (!input_element->isEnabled())
      continue;

    // Ignore all input types except TEXT.
    if (input_element->inputType() != WebCore::HTMLInputElement::TEXT)
      continue;

    // For each TEXT input field, store the name and value
    std::wstring value = webkit_glue::StringToStdWString(
        input_element->value());
    TrimWhitespace(value, TRIM_LEADING, &value);
    if (value.length() == 0)
      continue;

    std::wstring name = GetNameForInputElement(input_element);
    if (name.length() == 0)
      continue;  // If we have no name, there is nothing to store.

    result->elements.push_back(AutofillForm::Element(name, value));
  }

  return result;
}

// static
std::wstring AutofillForm::GetNameForInputElement(WebCore::HTMLInputElement*
    element) {
  std::wstring name = webkit_glue::StringToStdWString(element->name());
  std::wstring trimmed_name;
  TrimWhitespace(name, TRIM_LEADING, &trimmed_name);
  if (trimmed_name.length() > 0)
    return trimmed_name;

  name = webkit_glue::StringToStdWString(element->id());
  TrimWhitespace(name, TRIM_LEADING, &trimmed_name);
  if (trimmed_name.length() > 0)
    return trimmed_name;

  return std::wstring();
}