Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "components/autofill/core/browser/autofill_xml_parser.h" | 5 #include "components/autofill/core/browser/autofill_xml_parser.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
|
Mathieu
2015/11/25 17:59:10
Is it advised to #include <utility> for std::move?
vabr (Chromium)
2015/11/26 09:36:12
Good point, thanks!
Also, removed <string.h>, beca
| |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "components/autofill/core/browser/autofill_server_field_info.h" | 11 #include "components/autofill/core/browser/autofill_server_field_info.h" |
| 12 #include "third_party/webrtc/libjingle/xmllite/qname.h" | 12 #include "components/autofill/core/browser/field_types.h" |
| 13 #include "third_party/libxml/chromium/libxml_utils.h" | |
| 13 | 14 |
| 14 namespace autofill { | 15 namespace autofill { |
| 15 | 16 |
| 16 AutofillXmlParser::AutofillXmlParser() | 17 bool ParseAutofillQueryXml(std::string xml, |
| 17 : succeeded_(true) { | 18 std::vector<AutofillServerFieldInfo>* field_infos, |
| 18 } | 19 UploadRequired* upload_required) { |
| 20 DCHECK(field_infos); | |
| 21 DCHECK(upload_required); | |
| 19 | 22 |
| 20 AutofillXmlParser::~AutofillXmlParser() {} | 23 XmlReader reader; |
| 24 if (!reader.Load(xml)) | |
| 25 return false; | |
| 21 | 26 |
| 22 void AutofillXmlParser::CharacterData( | 27 // Seek to the first opening tag. |
| 23 buzz::XmlParseContext* context, const char* text, int len) { | 28 if (!reader.Read()) |
| 24 } | 29 return false; // Malformed input. |
| 25 | 30 |
| 26 void AutofillXmlParser::EndElement(buzz::XmlParseContext* context, | 31 if (reader.NodeName() != "autofillqueryresponse") |
| 27 const char* name) { | 32 return false; // Malformed input. |
| 28 } | |
| 29 | 33 |
| 30 void AutofillXmlParser::Error(buzz::XmlParseContext* context, | 34 *upload_required = USE_UPLOAD_RATES; |
| 31 XML_Error error_code) { | 35 std::string upload_required_value; |
| 32 succeeded_ = false; | 36 if (reader.NodeAttribute("uploadrequired", &upload_required_value)) { |
| 33 } | 37 if (upload_required_value == "true") |
| 38 *upload_required = UPLOAD_REQUIRED; | |
| 39 else if (upload_required_value == "false") | |
| 40 *upload_required = UPLOAD_NOT_REQUIRED; | |
| 41 } | |
| 34 | 42 |
| 35 AutofillQueryXmlParser::AutofillQueryXmlParser( | 43 field_infos->clear(); |
| 36 std::vector<AutofillServerFieldInfo>* field_infos, | 44 if (reader.IsClosingElement()) |
| 37 UploadRequired* upload_required) | 45 return true; |
| 38 : field_infos_(field_infos), | |
| 39 upload_required_(upload_required) { | |
| 40 DCHECK(upload_required_); | |
| 41 } | |
| 42 | 46 |
| 43 AutofillQueryXmlParser::~AutofillQueryXmlParser() {} | 47 if (!reader.Read()) |
| 44 | 48 return false; // Malformed input. |
| 45 void AutofillQueryXmlParser::StartElement(buzz::XmlParseContext* context, | 49 while (reader.NodeName() == "field") { |
| 46 const char* name, | |
| 47 const char** attrs) { | |
| 48 buzz::QName qname = context->ResolveQName(name, false); | |
| 49 const std::string& element = qname.LocalPart(); | |
| 50 if (element.compare("autofillqueryresponse") == 0) { | |
| 51 // We check for the upload required attribute below, but if it's not | |
| 52 // present, we use the default upload rates. | |
| 53 *upload_required_ = USE_UPLOAD_RATES; | |
| 54 | |
| 55 // |attrs| is a NULL-terminated list of (attribute, value) pairs. | |
| 56 while (*attrs) { | |
| 57 buzz::QName attribute_qname = context->ResolveQName(*attrs, true); | |
| 58 ++attrs; | |
| 59 const std::string& attribute_name = attribute_qname.LocalPart(); | |
| 60 if (attribute_name.compare("uploadrequired") == 0) { | |
| 61 if (strcmp(*attrs, "true") == 0) | |
| 62 *upload_required_ = UPLOAD_REQUIRED; | |
| 63 else if (strcmp(*attrs, "false") == 0) | |
| 64 *upload_required_ = UPLOAD_NOT_REQUIRED; | |
| 65 } | |
| 66 ++attrs; | |
| 67 } | |
| 68 } else if (element.compare("field") == 0) { | |
| 69 if (!*attrs) { | |
| 70 // Missing the "autofilltype" attribute, abort. | |
| 71 context->RaiseError(XML_ERROR_ABORTED); | |
| 72 return; | |
| 73 } | |
| 74 | |
| 75 // Determine the field type from the attribute value. There should be one | |
| 76 // attribute (autofilltype) with an integer value. | |
| 77 AutofillServerFieldInfo field_info; | 50 AutofillServerFieldInfo field_info; |
| 78 field_info.field_type = UNKNOWN_TYPE; | 51 field_info.field_type = UNKNOWN_TYPE; |
| 79 | 52 |
| 80 // |attrs| is a NULL-terminated list of (attribute, value) pairs. | 53 std::string autofill_type_value; |
| 81 while (*attrs) { | 54 if (!reader.NodeAttribute("autofilltype", &autofill_type_value)) |
| 82 buzz::QName attribute_qname = context->ResolveQName(*attrs, true); | 55 return false; // Missing required attribute. |
| 83 ++attrs; | 56 int parsed_type = 0; |
| 84 const std::string& attribute_name = attribute_qname.LocalPart(); | 57 if (!base::StringToInt(autofill_type_value, &parsed_type) || |
| 85 if (attribute_name.compare("autofilltype") == 0) { | 58 parsed_type < 0 || parsed_type >= MAX_VALID_FIELD_TYPE) { |
| 86 int value = GetIntValue(context, *attrs); | 59 // Invalid attribute value should not make the whole parse fail. |
| 87 if (value >= 0 && value < MAX_VALID_FIELD_TYPE) | 60 field_info.field_type = NO_SERVER_DATA; |
| 88 field_info.field_type = static_cast<ServerFieldType>(value); | 61 } else { |
| 89 else | 62 field_info.field_type = static_cast<ServerFieldType>(parsed_type); |
| 90 field_info.field_type = NO_SERVER_DATA; | |
| 91 } else if (field_info.field_type == FIELD_WITH_DEFAULT_VALUE && | |
| 92 attribute_name.compare("defaultvalue") == 0) { | |
| 93 field_info.default_value = *attrs; | |
| 94 } | |
| 95 ++attrs; | |
| 96 } | 63 } |
| 97 | 64 |
| 98 // Record this field type, default value pair. | 65 std::string default_value; |
| 99 field_infos_->push_back(field_info); | 66 if (field_info.field_type == FIELD_WITH_DEFAULT_VALUE && |
| 67 reader.NodeAttribute("defaultvalue", &default_value)) { | |
| 68 field_info.default_value = std::move(default_value); | |
| 69 } | |
| 70 | |
| 71 field_infos->push_back(field_info); | |
| 72 | |
| 73 if (!reader.Read()) | |
| 74 return false; // Malformed input. | |
| 100 } | 75 } |
| 76 | |
| 77 if (reader.NodeName() != "autofillqueryresponse" || | |
| 78 !reader.IsClosingElement()) { | |
| 79 return false; // Malformed input. | |
| 80 } | |
| 81 return true; | |
| 101 } | 82 } |
| 102 | 83 |
| 103 int AutofillQueryXmlParser::GetIntValue(buzz::XmlParseContext* context, | 84 bool ParseAutofillUploadXml(std::string xml, |
| 104 const char* attribute) { | 85 double* positive_upload_rate, |
| 105 int value = 0; | 86 double* negative_upload_rate) { |
| 106 if (!base::StringToInt(attribute, &value)) { | 87 DCHECK(positive_upload_rate); |
| 107 context->RaiseError(XML_ERROR_SYNTAX); | 88 DCHECK(negative_upload_rate); |
| 108 return 0; | |
| 109 } | |
| 110 return value; | |
| 111 } | |
| 112 | 89 |
| 113 AutofillUploadXmlParser::AutofillUploadXmlParser(double* positive_upload_rate, | 90 XmlReader reader; |
| 114 double* negative_upload_rate) | 91 if (!reader.Load(xml)) |
| 115 : succeeded_(false), | 92 return false; |
| 116 positive_upload_rate_(positive_upload_rate), | |
| 117 negative_upload_rate_(negative_upload_rate) { | |
| 118 DCHECK(positive_upload_rate_); | |
| 119 DCHECK(negative_upload_rate_); | |
| 120 } | |
| 121 | 93 |
| 122 void AutofillUploadXmlParser::StartElement(buzz::XmlParseContext* context, | 94 // Seek to the first opening tag. |
| 123 const char* name, | 95 if (!reader.Read()) |
| 124 const char** attrs) { | 96 return false; // Malformed input. |
| 125 buzz::QName qname = context->ResolveQName(name, false); | |
| 126 const std::string &element = qname.LocalPart(); | |
| 127 if (element.compare("autofilluploadresponse") == 0) { | |
| 128 // Loop over all attributes to get the upload rates. | |
| 129 while (*attrs) { | |
| 130 buzz::QName attribute_qname = context->ResolveQName(attrs[0], true); | |
| 131 const std::string &attribute_name = attribute_qname.LocalPart(); | |
| 132 if (attribute_name.compare("positiveuploadrate") == 0) { | |
| 133 *positive_upload_rate_ = GetDoubleValue(context, attrs[1]); | |
| 134 } else if (attribute_name.compare("negativeuploadrate") == 0) { | |
| 135 *negative_upload_rate_ = GetDoubleValue(context, attrs[1]); | |
| 136 } | |
| 137 attrs += 2; // We peeked at attrs[0] and attrs[1], skip past both. | |
| 138 } | |
| 139 } | |
| 140 } | |
| 141 | 97 |
| 142 double AutofillUploadXmlParser::GetDoubleValue(buzz::XmlParseContext* context, | 98 if (reader.NodeName() != "autofilluploadresponse") |
| 143 const char* attribute) { | 99 return false; // Malformed input. |
| 144 double value = 0; | 100 |
| 145 if (!base::StringToDouble(attribute, &value)) { | 101 std::string attribute_value; |
| 146 context->RaiseError(XML_ERROR_SYNTAX); | 102 if (!reader.NodeAttribute("positiveuploadrate", &attribute_value)) |
|
Mathieu
2015/11/25 17:59:10
Can you put a TODO in my name to update this to be
vabr (Chromium)
2015/11/26 09:36:12
Sure, thanks for pointing this out!
Instead of TOD
| |
| 147 return 0.0; | 103 return false; // Missing required attribute. |
| 148 } | 104 if (!base::StringToDouble(attribute_value, positive_upload_rate)) |
| 149 return value; | 105 return false; // Invalid attribute value. |
| 106 if (!reader.NodeAttribute("negativeuploadrate", &attribute_value)) | |
| 107 return false; // Missing required attribute. | |
| 108 if (!base::StringToDouble(attribute_value, negative_upload_rate)) | |
| 109 return false; // Invalid attribute value. | |
| 110 return true; | |
| 150 } | 111 } |
| 151 | 112 |
| 152 } // namespace autofill | 113 } // namespace autofill |
| OLD | NEW |