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