[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 | |
[email protected] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 5 | #include "components/autofill/browser/autofill_xml_parser.h" |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 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] | e4b2fa3 | 2013-03-09 22:56:56 | [diff] [blame] | 11 | #include "components/autofill/browser/autofill_server_field_info.h" |
[email protected] | 0116cf5 | 2011-11-01 02:44:32 | [diff] [blame] | 12 | #include "third_party/libjingle/source/talk/xmllite/qname.h" |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 13 | |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 14 | namespace autofill { |
| 15 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 16 | AutofillXmlParser::AutofillXmlParser() |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 17 | : succeeded_(true) { |
| 18 | } |
| 19 | |
[email protected] | 7c5935d | 2013-01-24 19:11:43 | [diff] [blame] | 20 | AutofillXmlParser::~AutofillXmlParser() {} |
| 21 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 22 | void AutofillXmlParser::CharacterData( |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 23 | buzz::XmlParseContext* context, const char* text, int len) { |
| 24 | } |
| 25 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 26 | void AutofillXmlParser::EndElement(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 27 | const char* name) { |
| 28 | } |
| 29 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 30 | void AutofillXmlParser::Error(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 31 | XML_Error error_code) { |
| 32 | succeeded_ = false; |
| 33 | } |
| 34 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 35 | AutofillQueryXmlParser::AutofillQueryXmlParser( |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 36 | std::vector<AutofillServerFieldInfo>* field_infos, |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 37 | UploadRequired* upload_required, |
| 38 | std::string* experiment_id) |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 39 | : field_infos_(field_infos), |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 40 | upload_required_(upload_required), |
[email protected] | e899b96 | 2013-01-18 20:52:08 | [diff] [blame] | 41 | current_page_number_(-1), |
| 42 | total_pages_(-1), |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 43 | experiment_id_(experiment_id) { |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 44 | DCHECK(upload_required_); |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 45 | DCHECK(experiment_id_); |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 46 | } |
| 47 | |
[email protected] | 7c5935d | 2013-01-24 19:11:43 | [diff] [blame] | 48 | AutofillQueryXmlParser::~AutofillQueryXmlParser() {} |
| 49 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 50 | void AutofillQueryXmlParser::StartElement(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 51 | const char* name, |
| 52 | const char** attrs) { |
| 53 | buzz::QName qname = context->ResolveQName(name, false); |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 54 | const std::string& element = qname.LocalPart(); |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 55 | if (element.compare("autofillqueryresponse") == 0) { |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 56 | // We check for the upload required attribute below, but if it's not |
| 57 | // present, we use the default upload rates. Likewise, by default we assume |
| 58 | // an empty experiment id. |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 59 | *upload_required_ = USE_UPLOAD_RATES; |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 60 | *experiment_id_ = std::string(); |
| 61 | |
| 62 | // |attrs| is a NULL-terminated list of (attribute, value) pairs. |
| 63 | while (*attrs) { |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 64 | buzz::QName attribute_qname = context->ResolveQName(*attrs, true); |
| 65 | ++attrs; |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 66 | const std::string& attribute_name = attribute_qname.LocalPart(); |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 67 | if (attribute_name.compare("uploadrequired") == 0) { |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 68 | if (strcmp(*attrs, "true") == 0) |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 69 | *upload_required_ = UPLOAD_REQUIRED; |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 70 | else if (strcmp(*attrs, "false") == 0) |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 71 | *upload_required_ = UPLOAD_NOT_REQUIRED; |
[email protected] | 5f372f8 | 2011-01-31 23:20:50 | [diff] [blame] | 72 | } else if (attribute_name.compare("experimentid") == 0) { |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 73 | *experiment_id_ = *attrs; |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 74 | } |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 75 | ++attrs; |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 76 | } |
| 77 | } else if (element.compare("field") == 0) { |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 78 | if (!*attrs) { |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 79 | // Missing the "autofilltype" attribute, abort. |
| 80 | context->RaiseError(XML_ERROR_ABORTED); |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | // Determine the field type from the attribute value. There should be one |
| 85 | // attribute (autofilltype) with an integer value. |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 86 | AutofillServerFieldInfo field_info; |
| 87 | field_info.field_type = UNKNOWN_TYPE; |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 88 | |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 89 | // |attrs| is a NULL-terminated list of (attribute, value) pairs. |
| 90 | while (*attrs) { |
| 91 | buzz::QName attribute_qname = context->ResolveQName(*attrs, true); |
| 92 | ++attrs; |
| 93 | const std::string& attribute_name = attribute_qname.LocalPart(); |
| 94 | if (attribute_name.compare("autofilltype") == 0) { |
| 95 | int value = GetIntValue(context, *attrs); |
| 96 | if (value >= 0 && value < MAX_VALID_FIELD_TYPE) |
| 97 | field_info.field_type = static_cast<AutofillFieldType>(value); |
| 98 | else |
| 99 | field_info.field_type = NO_SERVER_DATA; |
| 100 | } else if (field_info.field_type == FIELD_WITH_DEFAULT_VALUE && |
| 101 | attribute_name.compare("defaultvalue") == 0) { |
| 102 | field_info.default_value = *attrs; |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 103 | } |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 104 | ++attrs; |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 105 | } |
| 106 | |
[email protected] | 732acb0 | 2013-01-22 17:04:45 | [diff] [blame] | 107 | // Record this field type, default value pair. |
| 108 | field_infos_->push_back(field_info); |
[email protected] | e899b96 | 2013-01-18 20:52:08 | [diff] [blame] | 109 | } else if (element.compare("autofill_flow") == 0) { |
| 110 | // |attrs| is a NULL-terminated list of (attribute, value) pairs. |
| 111 | while (*attrs) { |
| 112 | buzz::QName attribute_qname = context->ResolveQName(*attrs, true); |
| 113 | ++attrs; |
| 114 | const std::string& attribute_name = attribute_qname.LocalPart(); |
| 115 | if (attribute_name.compare("page_no") == 0) |
| 116 | current_page_number_ = GetIntValue(context, *attrs); |
| 117 | else if (attribute_name.compare("total_pages") == 0) |
| 118 | total_pages_ = GetIntValue(context, *attrs); |
| 119 | ++attrs; |
| 120 | } |
[email protected] | 7c5935d | 2013-01-24 19:11:43 | [diff] [blame] | 121 | } else if (element.compare("page_advance_button") == 0) { |
| 122 | // |attrs| is a NULL-terminated list of (attribute, value) pairs. |
| 123 | // If both id and css_selector are set, the first one to appear will take |
| 124 | // precedence. |
| 125 | while (*attrs) { |
| 126 | buzz::QName attribute_qname = context->ResolveQName(*attrs, true); |
| 127 | ++attrs; |
| 128 | const std::string& attribute_name = attribute_qname.LocalPart(); |
| 129 | buzz::QName value_qname = context->ResolveQName(*attrs, true); |
| 130 | ++attrs; |
| 131 | const std::string& attribute_value = value_qname.LocalPart(); |
| 132 | if (attribute_name.compare("id") == 0 && !attribute_value.empty()) { |
| 133 | proceed_element_descriptor_.reset(new autofill::WebElementDescriptor()); |
| 134 | proceed_element_descriptor_->retrieval_method = |
| 135 | autofill::WebElementDescriptor::ID; |
| 136 | proceed_element_descriptor_->descriptor = attribute_value; |
| 137 | break; |
| 138 | } else if (attribute_name.compare("css_selector") == 0 && |
| 139 | !attribute_value.empty()) { |
| 140 | proceed_element_descriptor_.reset(new autofill::WebElementDescriptor()); |
| 141 | proceed_element_descriptor_->retrieval_method = |
| 142 | autofill::WebElementDescriptor::CSS_SELECTOR; |
| 143 | proceed_element_descriptor_->descriptor = attribute_value; |
| 144 | break; |
| 145 | } |
| 146 | } |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 150 | int AutofillQueryXmlParser::GetIntValue(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 151 | const char* attribute) { |
| 152 | char* attr_end = NULL; |
| 153 | int value = strtol(attribute, &attr_end, 10); |
| 154 | if (attr_end != NULL && attr_end == attribute) { |
| 155 | context->RaiseError(XML_ERROR_SYNTAX); |
| 156 | return 0; |
| 157 | } |
| 158 | return value; |
| 159 | } |
| 160 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 161 | AutofillUploadXmlParser::AutofillUploadXmlParser(double* positive_upload_rate, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 162 | double* negative_upload_rate) |
[email protected] | 8cd7fd0 | 2010-07-22 18:21:02 | [diff] [blame] | 163 | : succeeded_(false), |
| 164 | positive_upload_rate_(positive_upload_rate), |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 165 | negative_upload_rate_(negative_upload_rate) { |
| 166 | DCHECK(positive_upload_rate_); |
| 167 | DCHECK(negative_upload_rate_); |
| 168 | } |
| 169 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 170 | void AutofillUploadXmlParser::StartElement(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 171 | const char* name, |
| 172 | const char** attrs) { |
| 173 | buzz::QName qname = context->ResolveQName(name, false); |
| 174 | const std::string &element = qname.LocalPart(); |
| 175 | if (element.compare("autofilluploadresponse") == 0) { |
| 176 | // Loop over all attributes to get the upload rates. |
| 177 | while (*attrs) { |
| 178 | buzz::QName attribute_qname = context->ResolveQName(attrs[0], true); |
| 179 | const std::string &attribute_name = attribute_qname.LocalPart(); |
| 180 | if (attribute_name.compare("positiveuploadrate") == 0) { |
| 181 | *positive_upload_rate_ = GetDoubleValue(context, attrs[1]); |
| 182 | } else if (attribute_name.compare("negativeuploadrate") == 0) { |
| 183 | *negative_upload_rate_ = GetDoubleValue(context, attrs[1]); |
| 184 | } |
| 185 | attrs += 2; // We peeked at attrs[0] and attrs[1], skip past both. |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
[email protected] | 663bd9e | 2011-03-21 01:07:01 | [diff] [blame] | 190 | double AutofillUploadXmlParser::GetDoubleValue(buzz::XmlParseContext* context, |
[email protected] | ec64212b | 2010-03-18 01:02:43 | [diff] [blame] | 191 | const char* attribute) { |
| 192 | char* attr_end = NULL; |
| 193 | double value = strtod(attribute, &attr_end); |
| 194 | if (attr_end != NULL && attr_end == attribute) { |
| 195 | context->RaiseError(XML_ERROR_SYNTAX); |
| 196 | return 0.0; |
| 197 | } |
| 198 | return value; |
| 199 | } |
[email protected] | e217c563 | 2013-04-12 19:11:48 | [diff] [blame^] | 200 | |
| 201 | } // namespace autofill |