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