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.
5 #ifndef COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_XML_PARSER_H_
6 #define COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_XML_PARSER_H_
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"
21 // The base class that contains common functionality between
22 // AutofillQueryXmlParser and AutofillUploadXmlParser.
23 class AutofillXmlParser
: public buzz::XmlParseHandler
{
26 ~AutofillXmlParser() override
;
28 // Returns true if no parsing errors were encountered.
29 bool succeeded() const { return succeeded_
; }
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
;
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
41 // |len| is the length of the string pointed to by text.
42 void CharacterData(buzz::XmlParseContext
* context
,
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
;
51 // True if parsing succeeded.
54 DISALLOW_COPY_AND_ASSIGN(AutofillXmlParser
);
57 // The XML parse handler for parsing Autofill query responses. A typical
58 // response looks like:
60 // <autofillqueryresponse experimentid="1">
61 // <field autofilltype="0" />
62 // <field autofilltype="1" />
63 // <field autofilltype="3" />
64 // <field autofilltype="2" />
65 // </autofillqueryresponse>
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
{
74 AutofillQueryXmlParser(std::vector
<AutofillServerFieldInfo
>* field_infos
,
75 UploadRequired
* upload_required
);
76 ~AutofillQueryXmlParser() override
;
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
,
85 const char** attrs
) override
;
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
);
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
);
101 // The parsed <field type, default value> pairs.
102 std::vector
<AutofillServerFieldInfo
>* field_infos_
;
104 // A flag indicating whether the client should upload Autofill data when this
105 // form is submitted.
106 UploadRequired
* upload_required_
;
108 DISALLOW_COPY_AND_ASSIGN(AutofillQueryXmlParser
);
111 // The XML parser for handling Autofill upload responses. Typical upload
112 // responses look like:
114 // <autofilluploadresponse negativeuploadrate="0.00125" positiveuploadrate="1"/>
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
122 class AutofillUploadXmlParser
: public AutofillXmlParser
{
124 AutofillUploadXmlParser(double* positive_upload_rate
,
125 double* negative_upload_rate
);
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
,
134 const char** attrs
) override
;
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
);
142 // True if parsing succeeded.
145 double* positive_upload_rate_
;
146 double* negative_upload_rate_
;
148 DISALLOW_COPY_AND_ASSIGN(AutofillUploadXmlParser
);
151 } // namespace autofill
153 #endif // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_XML_PARSER_H_