[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "chrome/browser/autofill/autofill_xml_parser.h" |
| 6 | |
[email protected] | 8f608c5 | 2011-03-30 16:16:31 | [diff] [blame] | 7 | #include <stdlib.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include "base/logging.h" |
[email protected] | 0116cf5 | 2011-11-01 02:44:32 | [diff] [blame] | 11 | #include "third_party/libjingle/source/talk/xmllite/qname.h" |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 12 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 13 | AutofillXmlParser::AutofillXmlParser() |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 14 | : succeeded_(true) { |
| 15 | } |
| 16 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 17 | void AutofillXmlParser::CharacterData( |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 18 | buzz::XmlParseContext* context, const char* text, int len) { |
| 19 | } |
| 20 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 21 | void AutofillXmlParser::EndElement(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 22 | const char* name) { |
| 23 | } |
| 24 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 25 | void AutofillXmlParser::Error(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 26 | XML_Error error_code) { |
| 27 | succeeded_ = false; |
| 28 | } |
| 29 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 30 | AutofillQueryXmlParser::AutofillQueryXmlParser( |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 31 | std::vector<AutofillFieldType>* field_types, |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 32 | UploadRequired* upload_required, |
| 33 | std::string* experiment_id) |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 34 | : field_types_(field_types), |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 35 | upload_required_(upload_required), |
[email protected] | e899b96 | 2013-01-18 20:52:08 | [diff] [blame^] | 36 | current_page_number_(-1), |
| 37 | total_pages_(-1), |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 38 | experiment_id_(experiment_id) { |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 39 | DCHECK(upload_required_); |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 40 | DCHECK(experiment_id_); |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 41 | } |
| 42 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 43 | void AutofillQueryXmlParser::StartElement(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 44 | const char* name, |
| 45 | const char** attrs) { |
| 46 | buzz::QName qname = context->ResolveQName(name, false); |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 47 | const std::string& element = qname.LocalPart(); |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 48 | if (element.compare("autofillqueryresponse") == 0) { |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 49 | // We check for the upload required attribute below, but if it's not |
| 50 | // present, we use the default upload rates. Likewise, by default we assume |
| 51 | // an empty experiment id. |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 52 | *upload_required_ = USE_UPLOAD_RATES; |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 53 | *experiment_id_ = std::string(); |
| 54 | |
| 55 | // |attrs| is a NULL-terminated list of (attribute, value) pairs. |
| 56 | while (*attrs) { |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 57 | buzz::QName attribute_qname = context->ResolveQName(attrs[0], true); |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 58 | const std::string& attribute_name = attribute_qname.LocalPart(); |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 59 | if (attribute_name.compare("uploadrequired") == 0) { |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 60 | if (strcmp(attrs[1], "true") == 0) |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 61 | *upload_required_ = UPLOAD_REQUIRED; |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 62 | else if (strcmp(attrs[1], "false") == 0) |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 63 | *upload_required_ = UPLOAD_NOT_REQUIRED; |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 64 | } else if (attribute_name.compare("experimentid") == 0) { |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 65 | *experiment_id_ = attrs[1]; |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 66 | } |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 67 | |
| 68 | // Advance to the next (attribute, value) pair. |
| 69 | attrs += 2; |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 70 | } |
| 71 | } else if (element.compare("field") == 0) { |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 72 | if (!attrs[0]) { |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 73 | // Missing the "autofilltype" attribute, abort. |
| 74 | context->RaiseError(XML_ERROR_ABORTED); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | // Determine the field type from the attribute value. There should be one |
| 79 | // attribute (autofilltype) with an integer value. |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 80 | AutofillFieldType field_type = UNKNOWN_TYPE; |
| 81 | buzz::QName attribute_qname = context->ResolveQName(attrs[0], true); |
| 82 | const std::string& attribute_name = attribute_qname.LocalPart(); |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 83 | |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 84 | if (attribute_name.compare("autofilltype") == 0) { |
| 85 | int value = GetIntValue(context, attrs[1]); |
| 86 | field_type = static_cast<AutofillFieldType>(value); |
| 87 | if (field_type < 0 || field_type > MAX_VALID_FIELD_TYPE) { |
| 88 | field_type = NO_SERVER_DATA; |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 89 | } |
| 90 | } |
| 91 | |
[email protected] | 606d1080 | 2013-01-14 18:14:57 | [diff] [blame] | 92 | // Record this field type. |
| 93 | field_types_->push_back(field_type); |
[email protected] | e899b96 | 2013-01-18 20:52:08 | [diff] [blame^] | 94 | } else if (element.compare("autofill_flow") == 0) { |
| 95 | // |attrs| is a NULL-terminated list of (attribute, value) pairs. |
| 96 | while (*attrs) { |
| 97 | buzz::QName attribute_qname = context->ResolveQName(*attrs, true); |
| 98 | ++attrs; |
| 99 | const std::string& attribute_name = attribute_qname.LocalPart(); |
| 100 | if (attribute_name.compare("page_no") == 0) |
| 101 | current_page_number_ = GetIntValue(context, *attrs); |
| 102 | else if (attribute_name.compare("total_pages") == 0) |
| 103 | total_pages_ = GetIntValue(context, *attrs); |
| 104 | ++attrs; |
| 105 | } |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 109 | int AutofillQueryXmlParser::GetIntValue(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 110 | const char* attribute) { |
| 111 | char* attr_end = NULL; |
| 112 | int value = strtol(attribute, &attr_end, 10); |
| 113 | if (attr_end != NULL && attr_end == attribute) { |
| 114 | context->RaiseError(XML_ERROR_SYNTAX); |
| 115 | return 0; |
| 116 | } |
| 117 | return value; |
| 118 | } |
| 119 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 120 | AutofillUploadXmlParser::AutofillUploadXmlParser(double* positive_upload_rate, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 121 | double* negative_upload_rate) |
[email protected] | 8cd7fd0 | 2010-07-22 18:21:02 | [diff] [blame] | 122 | : succeeded_(false), |
| 123 | positive_upload_rate_(positive_upload_rate), |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 124 | negative_upload_rate_(negative_upload_rate) { |
| 125 | DCHECK(positive_upload_rate_); |
| 126 | DCHECK(negative_upload_rate_); |
| 127 | } |
| 128 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 129 | void AutofillUploadXmlParser::StartElement(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 130 | const char* name, |
| 131 | const char** attrs) { |
| 132 | buzz::QName qname = context->ResolveQName(name, false); |
| 133 | const std::string &element = qname.LocalPart(); |
| 134 | if (element.compare("autofilluploadresponse") == 0) { |
| 135 | // Loop over all attributes to get the upload rates. |
| 136 | while (*attrs) { |
| 137 | buzz::QName attribute_qname = context->ResolveQName(attrs[0], true); |
| 138 | const std::string &attribute_name = attribute_qname.LocalPart(); |
| 139 | if (attribute_name.compare("positiveuploadrate") == 0) { |
| 140 | *positive_upload_rate_ = GetDoubleValue(context, attrs[1]); |
| 141 | } else if (attribute_name.compare("negativeuploadrate") == 0) { |
| 142 | *negative_upload_rate_ = GetDoubleValue(context, attrs[1]); |
| 143 | } |
| 144 | attrs += 2; // We peeked at attrs[0] and attrs[1], skip past both. |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 149 | double AutofillUploadXmlParser::GetDoubleValue(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 150 | const char* attribute) { |
| 151 | char* attr_end = NULL; |
| 152 | double value = strtod(attribute, &attr_end); |
| 153 | if (attr_end != NULL && attr_end == attribute) { |
| 154 | context->RaiseError(XML_ERROR_SYNTAX); |
| 155 | return 0.0; |
| 156 | } |
| 157 | return value; |
| 158 | } |