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