Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / autofill / core / browser / autofill_xml_parser.h
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
3 // found in the LICENSE file.
4
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_XML_PARSER_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_XML_PARSER_H_
7
8 #include <string>
9 #include <vector>
10
11 #include "base/basictypes.h"
12 #include "base/compiler_specific.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "components/autofill/core/browser/autofill_server_field_info.h"
15 #include "components/autofill/core/browser/field_types.h"
16 #include "components/autofill/core/browser/form_structure.h"
17 #include "third_party/webrtc/libjingle/xmllite/xmlparser.h"
18
19 namespace autofill {
20
21 // The base class that contains common functionality between
22 // AutofillQueryXmlParser and AutofillUploadXmlParser.
23 class AutofillXmlParser : public buzz::XmlParseHandler {
24  public:
25   AutofillXmlParser();
26   ~AutofillXmlParser() override;
27
28   // Returns true if no parsing errors were encountered.
29   bool succeeded() const { return succeeded_; }
30
31  private:
32   // A callback for the end of an </element>, called by Expat.
33   // |context| is a parsing context used to resolve element/attribute names.
34   // |name| is the name of the element.
35   void EndElement(buzz::XmlParseContext* context, const char* name) override;
36
37   // The callback for character data between tags (<element>text...</element>).
38   // |context| is a parsing context used to resolve element/attribute names.
39   // |text| is a pointer to the beginning of character data (not null
40   // terminated).
41   // |len| is the length of the string pointed to by text.
42   void CharacterData(buzz::XmlParseContext* context,
43                      const char* text,
44                      int len) override;
45
46   // The callback for parsing errors.
47   // |context| is a parsing context used to resolve names.
48   // |error_code| is a code representing the parsing error.
49   void Error(buzz::XmlParseContext* context, XML_Error error_code) override;
50
51   // True if parsing succeeded.
52   bool succeeded_;
53
54   DISALLOW_COPY_AND_ASSIGN(AutofillXmlParser);
55 };
56
57 // The XML parse handler for parsing Autofill query responses.  A typical
58 // response looks like:
59 //
60 // <autofillqueryresponse experimentid="1">
61 //   <field autofilltype="0" />
62 //   <field autofilltype="1" />
63 //   <field autofilltype="3" />
64 //   <field autofilltype="2" />
65 // </autofillqueryresponse>
66 //
67 // Fields are returned in the same order they were sent to the server.
68 // autofilltype: The server's guess at what type of field this is.  0
69 // is unknown, other types are documented in
70 // components/autofill/core/browser/field_types.h.
71 // Experiment ids are currently ignored.
72 class AutofillQueryXmlParser : public AutofillXmlParser {
73  public:
74   AutofillQueryXmlParser(std::vector<AutofillServerFieldInfo>* field_infos,
75                          UploadRequired* upload_required);
76   ~AutofillQueryXmlParser() override;
77
78  private:
79   // A callback for the beginning of a new <element>, called by Expat.
80   // |context| is a parsing context used to resolve element/attribute names.
81   // |name| is the name of the element.
82   // |attrs| is the list of attributes (names and values) for the element.
83   void StartElement(buzz::XmlParseContext* context,
84                     const char* name,
85                     const char** attrs) override;
86
87   // A helper function to parse a |WebElementDescriptor|.
88   // |context| is the current parsing context.
89   // |attrs| is the list of attributes (names and values) for the element.
90   // |element_descriptor| will be populated by this function.
91   void ParseElementDescriptor(buzz::XmlParseContext* context,
92                               const char* const* attrs,
93                               WebElementDescriptor* element_descriptor);
94
95   // A helper function to retrieve integer values from strings.  Raises an
96   // XML parse error if it fails.
97   // |context| is the current parsing context.
98   // |value| is the string to convert.
99   int GetIntValue(buzz::XmlParseContext* context, const char* attribute);
100
101   // The parsed <field type, default value> pairs.
102   std::vector<AutofillServerFieldInfo>* field_infos_;
103
104   // A flag indicating whether the client should upload Autofill data when this
105   // form is submitted.
106   UploadRequired* upload_required_;
107
108   DISALLOW_COPY_AND_ASSIGN(AutofillQueryXmlParser);
109 };
110
111 // The XML parser for handling Autofill upload responses.  Typical upload
112 // responses look like:
113 //
114 // <autofilluploadresponse negativeuploadrate="0.00125" positiveuploadrate="1"/>
115 //
116 // The positive upload rate is the percentage of uploads to send to the server
117 // when something in the users profile matches what they have entered in a form.
118 // The negative upload rate is the percentage of uploads to send when nothing in
119 // the form matches what's in the users profile.
120 // The negative upload rate is typically much lower than the positive upload
121 // rate.
122 class AutofillUploadXmlParser : public AutofillXmlParser {
123  public:
124   AutofillUploadXmlParser(double* positive_upload_rate,
125                           double* negative_upload_rate);
126
127  private:
128   // A callback for the beginning of a new <element>, called by Expat.
129   // |context| is a parsing context used to resolve element/attribute names.
130   // |name| is the name of the element.
131   // |attrs| is the list of attributes (names and values) for the element.
132   void StartElement(buzz::XmlParseContext* context,
133                     const char* name,
134                     const char** attrs) override;
135
136   // A helper function to retrieve double values from strings.  Raises an XML
137   // parse error if it fails.
138   // |context| is the current parsing context.
139   // |value| is the string to convert.
140   double GetDoubleValue(buzz::XmlParseContext* context, const char* attribute);
141
142   // True if parsing succeeded.
143   bool succeeded_;
144
145   double* positive_upload_rate_;
146   double* negative_upload_rate_;
147
148   DISALLOW_COPY_AND_ASSIGN(AutofillUploadXmlParser);
149 };
150
151 }  // namespace autofill
152
153 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_XML_PARSER_H_