Home | History | Annotate | Download | only in browser
      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 
     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/core/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 callback for the end of an </element>, called by Expat.
    101   // |context| is a parsing context used to resolve element/attribute names.
    102   // |name| is the name of the element.
    103   virtual void EndElement(buzz::XmlParseContext* context,
    104                           const char* name) OVERRIDE;
    105 
    106   // The callback for character data between tags (<element>text...</element>).
    107   // |context| is a parsing context used to resolve element/attribute names.
    108   // |text| is a pointer to the beginning of character data (not null
    109   // terminated).
    110   // |len| is the length of the string pointed to by text.
    111   virtual void CharacterData(buzz::XmlParseContext* context,
    112                              const char* text,
    113                              int len) OVERRIDE;
    114 
    115   // A helper function to retrieve integer values from strings.  Raises an
    116   // XML parse error if it fails.
    117   // |context| is the current parsing context.
    118   // |value| is the string to convert.
    119   int GetIntValue(buzz::XmlParseContext* context, const char* attribute);
    120 
    121   // The parsed <field type, default value> pairs.
    122   std::vector<AutofillServerFieldInfo>* field_infos_;
    123 
    124   // A flag indicating whether the client should upload Autofill data when this
    125   // form is submitted.
    126   UploadRequired* upload_required_;
    127 
    128   // The server experiment to which this query response belongs.
    129   // For the default server implementation, this is empty.
    130   std::string* experiment_id_;
    131 
    132   // Page metadata for multipage autofill flow.
    133   AutocheckoutPageMetaData* page_meta_data_;
    134 
    135   // The click element the parser is currently processing.
    136   WebElementDescriptor* current_click_element_;
    137 
    138   // Number of page whose type is currently being parsed.
    139   int current_page_number_for_page_types_;
    140 
    141   // Whether the instance is currently parsing inside 'type' tags.
    142   bool is_in_type_section_;
    143 
    144   DISALLOW_COPY_AND_ASSIGN(AutofillQueryXmlParser);
    145 };
    146 
    147 // The XML parser for handling Autofill upload responses.  Typical upload
    148 // responses look like:
    149 //
    150 // <autofilluploadresponse negativeuploadrate="0.00125" positiveuploadrate="1"/>
    151 //
    152 // The positive upload rate is the percentage of uploads to send to the server
    153 // when something in the users profile matches what they have entered in a form.
    154 // The negative upload rate is the percentage of uploads to send when nothing in
    155 // the form matches what's in the users profile.
    156 // The negative upload rate is typically much lower than the positive upload
    157 // rate.
    158 class AutofillUploadXmlParser : public AutofillXmlParser {
    159  public:
    160   AutofillUploadXmlParser(double* positive_upload_rate,
    161                           double* negative_upload_rate);
    162 
    163  private:
    164   // A callback for the beginning of a new <element>, called by Expat.
    165   // |context| is a parsing context used to resolve element/attribute names.
    166   // |name| is the name of the element.
    167   // |attrs| is the list of attributes (names and values) for the element.
    168   virtual void StartElement(buzz::XmlParseContext* context,
    169                             const char* name,
    170                             const char** attrs) OVERRIDE;
    171 
    172   // A helper function to retrieve double values from strings.  Raises an XML
    173   // parse error if it fails.
    174   // |context| is the current parsing context.
    175   // |value| is the string to convert.
    176   double GetDoubleValue(buzz::XmlParseContext* context, const char* attribute);
    177 
    178   // True if parsing succeeded.
    179   bool succeeded_;
    180 
    181   double* positive_upload_rate_;
    182   double* negative_upload_rate_;
    183 
    184   DISALLOW_COPY_AND_ASSIGN(AutofillUploadXmlParser);
    185 };
    186 
    187 }  // namespace autofill
    188 
    189 #endif  // COMPONENTS_AUTOFILL_CORE_BROWSER_AUTOFILL_XML_PARSER_H_
    190