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