form_structure.h revision 3345a6884c488ff3a535c2c9acdd33d74b37e311
1// Copyright (c) 2009 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef CHROME_BROWSER_AUTOFILL_FORM_STRUCTURE_H_
6#define CHROME_BROWSER_AUTOFILL_FORM_STRUCTURE_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/scoped_vector.h"
13#include "chrome/browser/autofill/autofill_field.h"
14#include "chrome/browser/autofill/autofill_type.h"
15#include "chrome/browser/autofill/field_types.h"
16#include "googleurl/src/gurl.h"
17#include "webkit/glue/form_data.h"
18
19namespace buzz {
20  class XmlElement;
21}  // namespace buzz
22
23enum RequestMethod {
24  GET,
25  POST
26};
27
28enum UploadRequired {
29  UPLOAD_NOT_REQUIRED,
30  UPLOAD_REQUIRED,
31  USE_UPLOAD_RATES
32};
33
34// FormStructure stores a single HTML form together with the values entered
35// in the fields along with additional information needed by AutoFill.
36class FormStructure {
37 public:
38  explicit FormStructure(const webkit_glue::FormData& form);
39
40  // Encodes the XML upload request from this FormStructure.
41  bool EncodeUploadRequest(bool auto_fill_used,
42                           std::string* encoded_xml) const;
43
44  // Encodes the XML query request for the set of forms.
45  // All fields are returned in one XML. For example, there are three forms,
46  // with 2, 4, and 3 fields. The returned XML would have type info for 9
47  // fields, first two of which would be for the first form, next 4 for the
48  // second, and the rest is for the third.
49  static bool EncodeQueryRequest(const ScopedVector<FormStructure>& forms,
50                                 std::string* encoded_xml);
51
52  // Parses the field types from the server query response. |forms| must be the
53  // same as the one passed to EncodeQueryRequest when constructing the query.
54  static void ParseQueryResponse(const std::string& response_xml,
55                                 const std::vector<FormStructure*>& forms,
56                                 UploadRequired* upload_required);
57
58  // The unique signature for this form, composed of the target url domain,
59  // the form name, and the form field names in a 64-bit hash.
60  std::string FormSignature() const;
61
62  // Runs a quick heuristic to rule out pages but obviously not auto-fillable,
63  // like google/yahoo/msn search, etc.
64  bool IsAutoFillable() const;
65
66  // Returns true if at least one of the form fields relevant for AutoFill
67  // is not empty.
68  bool HasAutoFillableValues() const;
69
70  // Returns true if at least one of the form fields is a billing field, which
71  // includes billing address fields and credit card fields.
72  bool HasBillingFields() const;
73
74  // Returns true if at least one of the form fields is a non-billing field,
75  // which includes billing address fields and credit card fields.
76  bool HasNonBillingFields() const;
77
78  // Resets |autofill_count_| and counts the number of auto-fillable fields.
79  // This is used when we receive server data for form fields.  At that time,
80  // we may have more known fields than just the number of fields we matched
81  // heuristically.
82  void UpdateAutoFillCount();
83
84  // Returns true if this form matches the structural requirements for AutoFill.
85  bool ShouldBeParsed() const;
86
87  // Sets the possible types for the field at |index|.
88  void set_possible_types(int index, const FieldTypeSet& types);
89
90  const AutoFillField* field(int index) const;
91  size_t field_count() const;
92
93  // Returns the number of fields that are able to be autofilled.
94  size_t autofill_count() const { return autofill_count_; }
95
96  // Used for iterating over the fields.
97  std::vector<AutoFillField*>::const_iterator begin() const {
98    return fields_.begin();
99  }
100  std::vector<AutoFillField*>::const_iterator end() const {
101    return fields_.end();
102  }
103
104  const GURL& source_url() const { return source_url_; }
105
106  bool operator==(const webkit_glue::FormData& form) const;
107  bool operator!=(const webkit_glue::FormData& form) const;
108
109 private:
110  enum EncodeRequestType {
111    QUERY,
112    UPLOAD,
113  };
114
115  // Runs several heuristics against the form fields to determine their possible
116  // types.
117  void GetHeuristicAutoFillTypes();
118
119  // Associates the field with the heuristic type for each of the field views.
120  void GetHeuristicFieldInfo(FieldTypeMap* field_types_map);
121
122  // Adds form info to |encompassing_xml_element|. |request_type| indicates if
123  // it is a query or upload.
124  bool EncodeFormRequest(EncodeRequestType request_type,
125                         buzz::XmlElement* encompassing_xml_element) const;
126
127  // The name of the form.
128  string16 form_name_;
129
130  // The source URL.
131  GURL source_url_;
132
133  // The target URL.
134  GURL target_url_;
135
136  bool has_credit_card_field_;
137  bool has_autofillable_field_;
138  bool has_password_fields_;
139
140  // The number of fields able to be auto-filled.
141  size_t autofill_count_;
142
143  // A vector of all the input fields in the form.  The vector is terminated by
144  // a NULL entry.
145  ScopedVector<AutoFillField> fields_;
146
147  // The names of the form input elements, that are part of the form signature.
148  // The string starts with "&" and the names are also separated by the "&"
149  // character. E.g.: "&form_input1_name&form_input2_name&...&form_inputN_name"
150  std::string form_signature_field_names_;
151
152  // GET or POST.
153  RequestMethod method_;
154
155  DISALLOW_COPY_AND_ASSIGN(FormStructure);
156};
157
158#endif  // CHROME_BROWSER_AUTOFILL_FORM_STRUCTURE_H_
159