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