OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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 "chrome/browser/autofill/autofill_xml_parser.h" | 5 #include "chrome/browser/autofill/autofill_xml_parser.h" |
6 | 6 |
7 #include <stdlib.h> | 7 #include <stdlib.h> |
8 #include <string.h> | 8 #include <string.h> |
9 | 9 |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
11 #include "chrome/browser/autofill/autofill_server_field_info.h" | |
11 #include "third_party/libjingle/source/talk/xmllite/qname.h" | 12 #include "third_party/libjingle/source/talk/xmllite/qname.h" |
12 | 13 |
13 AutofillXmlParser::AutofillXmlParser() | 14 AutofillXmlParser::AutofillXmlParser() |
14 : succeeded_(true) { | 15 : succeeded_(true) { |
15 } | 16 } |
16 | 17 |
17 void AutofillXmlParser::CharacterData( | 18 void AutofillXmlParser::CharacterData( |
18 buzz::XmlParseContext* context, const char* text, int len) { | 19 buzz::XmlParseContext* context, const char* text, int len) { |
19 } | 20 } |
20 | 21 |
21 void AutofillXmlParser::EndElement(buzz::XmlParseContext* context, | 22 void AutofillXmlParser::EndElement(buzz::XmlParseContext* context, |
22 const char* name) { | 23 const char* name) { |
23 } | 24 } |
24 | 25 |
25 void AutofillXmlParser::Error(buzz::XmlParseContext* context, | 26 void AutofillXmlParser::Error(buzz::XmlParseContext* context, |
26 XML_Error error_code) { | 27 XML_Error error_code) { |
27 succeeded_ = false; | 28 succeeded_ = false; |
28 } | 29 } |
29 | 30 |
30 AutofillQueryXmlParser::AutofillQueryXmlParser( | 31 AutofillQueryXmlParser::AutofillQueryXmlParser( |
31 std::vector<AutofillFieldType>* field_types, | 32 std::vector<AutofillServerFieldInfo>* field_infos, |
32 UploadRequired* upload_required, | 33 UploadRequired* upload_required, |
33 std::string* experiment_id) | 34 std::string* experiment_id) |
34 : field_types_(field_types), | 35 : field_infos_(field_infos), |
35 upload_required_(upload_required), | 36 upload_required_(upload_required), |
36 experiment_id_(experiment_id) { | 37 experiment_id_(experiment_id) { |
37 DCHECK(upload_required_); | 38 DCHECK(upload_required_); |
38 DCHECK(experiment_id_); | 39 DCHECK(experiment_id_); |
39 } | 40 } |
40 | 41 |
41 void AutofillQueryXmlParser::StartElement(buzz::XmlParseContext* context, | 42 void AutofillQueryXmlParser::StartElement(buzz::XmlParseContext* context, |
42 const char* name, | 43 const char* name, |
43 const char** attrs) { | 44 const char** attrs) { |
44 buzz::QName qname = context->ResolveQName(name, false); | 45 buzz::QName qname = context->ResolveQName(name, false); |
45 const std::string& element = qname.LocalPart(); | 46 const std::string& element = qname.LocalPart(); |
46 if (element.compare("autofillqueryresponse") == 0) { | 47 if (element.compare("autofillqueryresponse") == 0) { |
47 // We check for the upload required attribute below, but if it's not | 48 // We check for the upload required attribute below, but if it's not |
48 // present, we use the default upload rates. Likewise, by default we assume | 49 // present, we use the default upload rates. Likewise, by default we assume |
49 // an empty experiment id. | 50 // an empty experiment id. |
50 *upload_required_ = USE_UPLOAD_RATES; | 51 *upload_required_ = USE_UPLOAD_RATES; |
51 *experiment_id_ = std::string(); | 52 *experiment_id_ = std::string(); |
52 | 53 |
53 // |attrs| is a NULL-terminated list of (attribute, value) pairs. | 54 // |attrs| is a NULL-terminated list of (attribute, value) pairs. |
54 while (*attrs) { | 55 while (*attrs) { |
55 buzz::QName attribute_qname = context->ResolveQName(attrs[0], true); | 56 buzz::QName attribute_qname = context->ResolveQName(*attrs, true); |
57 ++attrs; | |
56 const std::string& attribute_name = attribute_qname.LocalPart(); | 58 const std::string& attribute_name = attribute_qname.LocalPart(); |
57 if (attribute_name.compare("uploadrequired") == 0) { | 59 if (attribute_name.compare("uploadrequired") == 0) { |
58 if (strcmp(attrs[1], "true") == 0) | 60 if (strcmp(*attrs, "true") == 0) |
59 *upload_required_ = UPLOAD_REQUIRED; | 61 *upload_required_ = UPLOAD_REQUIRED; |
60 else if (strcmp(attrs[1], "false") == 0) | 62 else if (strcmp(*attrs, "false") == 0) |
61 *upload_required_ = UPLOAD_NOT_REQUIRED; | 63 *upload_required_ = UPLOAD_NOT_REQUIRED; |
62 } else if (attribute_name.compare("experimentid") == 0) { | 64 } else if (attribute_name.compare("experimentid") == 0) { |
63 *experiment_id_ = attrs[1]; | 65 *experiment_id_ = *attrs; |
64 } | 66 } |
65 | 67 ++attrs; |
66 // Advance to the next (attribute, value) pair. | |
67 attrs += 2; | |
68 } | 68 } |
69 } else if (element.compare("field") == 0) { | 69 } else if (element.compare("field") == 0) { |
70 if (!attrs[0]) { | 70 if (!*attrs) { |
71 // Missing the "autofilltype" attribute, abort. | 71 // Missing the "autofilltype" attribute, abort. |
72 context->RaiseError(XML_ERROR_ABORTED); | 72 context->RaiseError(XML_ERROR_ABORTED); |
73 return; | 73 return; |
74 } | 74 } |
75 | 75 |
76 // Determine the field type from the attribute value. There should be one | 76 // Determine the field type from the attribute value. There should be one |
77 // attribute (autofilltype) with an integer value. | 77 // attribute (autofilltype) with an integer value. |
78 AutofillFieldType field_type = UNKNOWN_TYPE; | 78 AutofillServerFieldInfo field_info; |
79 buzz::QName attribute_qname = context->ResolveQName(attrs[0], true); | 79 field_info.field_type = UNKNOWN_TYPE; |
80 const std::string& attribute_name = attribute_qname.LocalPart(); | |
81 | 80 |
82 if (attribute_name.compare("autofilltype") == 0) { | 81 // |attrs| is a NULL-terminated list of (attribute, value) pairs. |
83 int value = GetIntValue(context, attrs[1]); | 82 while (*attrs) { |
84 field_type = static_cast<AutofillFieldType>(value); | 83 buzz::QName attribute_qname = context->ResolveQName(*attrs, true); |
85 if (field_type < 0 || field_type > MAX_VALID_FIELD_TYPE) { | 84 ++attrs; |
86 field_type = NO_SERVER_DATA; | 85 const std::string& attribute_name = attribute_qname.LocalPart(); |
86 if (attribute_name.compare("autofilltype") == 0) { | |
87 int value = GetIntValue(context, *attrs); | |
88 field_info.field_type = static_cast<AutofillFieldType>(value); | |
89 if (value < 0 || value > MAX_VALID_FIELD_TYPE) | |
90 field_info.field_type = NO_SERVER_DATA; | |
Ilya Sherman
2012/12/15 01:08:36
nit: To further reduce the chances of this getting
Raman Kakilate
2012/12/17 20:11:01
Done.
| |
91 } else if (field_info.field_type == FIELD_WITH_DEFAULT_VALUE && | |
92 attribute_name.compare("defaultvalue") == 0) { | |
93 field_info.default_value = *attrs; | |
87 } | 94 } |
95 ++attrs; | |
88 } | 96 } |
89 | 97 |
90 // Record this field type. | 98 // Record this field type, default value pair. |
91 field_types_->push_back(field_type); | 99 field_infos_->push_back(field_info); |
92 } | 100 } |
93 } | 101 } |
94 | 102 |
95 int AutofillQueryXmlParser::GetIntValue(buzz::XmlParseContext* context, | 103 int AutofillQueryXmlParser::GetIntValue(buzz::XmlParseContext* context, |
96 const char* attribute) { | 104 const char* attribute) { |
97 char* attr_end = NULL; | 105 char* attr_end = NULL; |
98 int value = strtol(attribute, &attr_end, 10); | 106 int value = strtol(attribute, &attr_end, 10); |
99 if (attr_end != NULL && attr_end == attribute) { | 107 if (attr_end != NULL && attr_end == attribute) { |
100 context->RaiseError(XML_ERROR_SYNTAX); | 108 context->RaiseError(XML_ERROR_SYNTAX); |
101 return 0; | 109 return 0; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
135 double AutofillUploadXmlParser::GetDoubleValue(buzz::XmlParseContext* context, | 143 double AutofillUploadXmlParser::GetDoubleValue(buzz::XmlParseContext* context, |
136 const char* attribute) { | 144 const char* attribute) { |
137 char* attr_end = NULL; | 145 char* attr_end = NULL; |
138 double value = strtod(attribute, &attr_end); | 146 double value = strtod(attribute, &attr_end); |
139 if (attr_end != NULL && attr_end == attribute) { | 147 if (attr_end != NULL && attr_end == attribute) { |
140 context->RaiseError(XML_ERROR_SYNTAX); | 148 context->RaiseError(XML_ERROR_SYNTAX); |
141 return 0.0; | 149 return 0.0; |
142 } | 150 } |
143 return value; | 151 return value; |
144 } | 152 } |
OLD | NEW |