| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/search_engines/template_url_parser.h" | 5 #include "components/search_engines/template_url_parser.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <map> | 8 #include <map> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 // having to convert to wide, then do comparisons. | 27 // having to convert to wide, then do comparisons. |
| 28 | 28 |
| 29 // Defines for element names of the OSD document: | 29 // Defines for element names of the OSD document: |
| 30 const char kURLElement[] = "Url"; | 30 const char kURLElement[] = "Url"; |
| 31 const char kParamElement[] = "Param"; | 31 const char kParamElement[] = "Param"; |
| 32 const char kShortNameElement[] = "ShortName"; | 32 const char kShortNameElement[] = "ShortName"; |
| 33 const char kImageElement[] = "Image"; | 33 const char kImageElement[] = "Image"; |
| 34 const char kOpenSearchDescriptionElement[] = "OpenSearchDescription"; | 34 const char kOpenSearchDescriptionElement[] = "OpenSearchDescription"; |
| 35 const char kFirefoxSearchDescriptionElement[] = "SearchPlugin"; | 35 const char kFirefoxSearchDescriptionElement[] = "SearchPlugin"; |
| 36 const char kInputEncodingElement[] = "InputEncoding"; | 36 const char kInputEncodingElement[] = "InputEncoding"; |
| 37 const char kAliasElement[] = "Alias"; |
| 37 | 38 |
| 38 // Various XML attributes used. | 39 // Various XML attributes used. |
| 39 const char kURLTypeAttribute[] = "type"; | 40 const char kURLTypeAttribute[] = "type"; |
| 40 const char kURLTemplateAttribute[] = "template"; | 41 const char kURLTemplateAttribute[] = "template"; |
| 41 const char kImageTypeAttribute[] = "type"; | 42 const char kImageTypeAttribute[] = "type"; |
| 42 const char kImageWidthAttribute[] = "width"; | 43 const char kImageWidthAttribute[] = "width"; |
| 43 const char kImageHeightAttribute[] = "height"; | 44 const char kImageHeightAttribute[] = "height"; |
| 44 const char kParamNameAttribute[] = "name"; | 45 const char kParamNameAttribute[] = "name"; |
| 45 const char kParamValueAttribute[] = "value"; | 46 const char kParamValueAttribute[] = "value"; |
| 46 const char kParamMethodAttribute[] = "method"; | 47 const char kParamMethodAttribute[] = "method"; |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 108 public: | 109 public: |
| 109 // Enum of the known element types. | 110 // Enum of the known element types. |
| 110 enum ElementType { | 111 enum ElementType { |
| 111 UNKNOWN, | 112 UNKNOWN, |
| 112 OPEN_SEARCH_DESCRIPTION, | 113 OPEN_SEARCH_DESCRIPTION, |
| 113 URL, | 114 URL, |
| 114 PARAM, | 115 PARAM, |
| 115 SHORT_NAME, | 116 SHORT_NAME, |
| 116 IMAGE, | 117 IMAGE, |
| 117 INPUT_ENCODING, | 118 INPUT_ENCODING, |
| 119 ALIAS, |
| 118 }; | 120 }; |
| 119 | 121 |
| 120 enum Method { | 122 enum Method { |
| 121 GET, | 123 GET, |
| 122 POST | 124 POST |
| 123 }; | 125 }; |
| 124 | 126 |
| 125 // Key/value of a Param node. | 127 // Key/value of a Param node. |
| 126 typedef std::pair<std::string, std::string> Param; | 128 typedef std::pair<std::string, std::string> Param; |
| 127 | 129 |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 break; | 235 break; |
| 234 } | 236 } |
| 235 context->string_.clear(); | 237 context->string_.clear(); |
| 236 } | 238 } |
| 237 | 239 |
| 238 // static | 240 // static |
| 239 void TemplateURLParsingContext::EndElementImpl(void* ctx, const xmlChar* name) { | 241 void TemplateURLParsingContext::EndElementImpl(void* ctx, const xmlChar* name) { |
| 240 TemplateURLParsingContext* context = | 242 TemplateURLParsingContext* context = |
| 241 reinterpret_cast<TemplateURLParsingContext*>(ctx); | 243 reinterpret_cast<TemplateURLParsingContext*>(ctx); |
| 242 switch (context->GetKnownType()) { | 244 switch (context->GetKnownType()) { |
| 245 case TemplateURLParsingContext::URL: |
| 246 context->ProcessURLParams(); |
| 247 break; |
| 243 case TemplateURLParsingContext::SHORT_NAME: | 248 case TemplateURLParsingContext::SHORT_NAME: |
| 244 context->data_.short_name = context->string_; | 249 context->data_.short_name = context->string_; |
| 245 break; | 250 break; |
| 246 case TemplateURLParsingContext::IMAGE: { | 251 case TemplateURLParsingContext::IMAGE: { |
| 247 GURL image_url(base::UTF16ToUTF8(context->string_)); | 252 GURL image_url(base::UTF16ToUTF8(context->string_)); |
| 248 if (image_url.SchemeIs(url::kDataScheme)) { | 253 if (image_url.SchemeIs(url::kDataScheme)) { |
| 249 // TODO (jcampan): bug 1169256: when dealing with data URL, we need to | 254 // TODO (jcampan): bug 1169256: when dealing with data URL, we need to |
| 250 // decode the data URL in the renderer. For now, we'll just point to the | 255 // decode the data URL in the renderer. For now, we'll just point to the |
| 251 // favicon from the URL. | 256 // favicon from the URL. |
| 252 context->derive_image_from_url_ = true; | 257 context->derive_image_from_url_ = true; |
| 253 } else if (context->image_is_valid_for_favicon_ && image_url.is_valid() && | 258 } else if (context->image_is_valid_for_favicon_ && image_url.is_valid() && |
| 254 (image_url.SchemeIs(url::kHttpScheme) || | 259 (image_url.SchemeIs(url::kHttpScheme) || |
| 255 image_url.SchemeIs(url::kHttpsScheme))) { | 260 image_url.SchemeIs(url::kHttpsScheme))) { |
| 256 context->data_.favicon_url = image_url; | 261 context->data_.favicon_url = image_url; |
| 257 } | 262 } |
| 258 context->image_is_valid_for_favicon_ = false; | 263 context->image_is_valid_for_favicon_ = false; |
| 259 break; | 264 break; |
| 260 } | 265 } |
| 261 case TemplateURLParsingContext::INPUT_ENCODING: { | 266 case TemplateURLParsingContext::INPUT_ENCODING: { |
| 262 std::string input_encoding = base::UTF16ToASCII(context->string_); | 267 std::string input_encoding = base::UTF16ToASCII(context->string_); |
| 263 if (IsValidEncodingString(input_encoding)) | 268 if (IsValidEncodingString(input_encoding)) |
| 264 context->data_.input_encodings.push_back(input_encoding); | 269 context->data_.input_encodings.push_back(input_encoding); |
| 265 break; | 270 break; |
| 266 } | 271 } |
| 267 case TemplateURLParsingContext::URL: | 272 case TemplateURLParsingContext::ALIAS: { |
| 268 context->ProcessURLParams(); | 273 context->data_.SetKeyword(context->string_); |
| 274 context->data_.has_custom_keyword = true; |
| 269 break; | 275 break; |
| 276 } |
| 270 default: | 277 default: |
| 271 break; | 278 break; |
| 272 } | 279 } |
| 273 context->string_.clear(); | 280 context->string_.clear(); |
| 274 context->elements_.pop_back(); | 281 context->elements_.pop_back(); |
| 275 } | 282 } |
| 276 | 283 |
| 277 // static | 284 // static |
| 278 void TemplateURLParsingContext::CharactersImpl(void* ctx, | 285 void TemplateURLParsingContext::CharactersImpl(void* ctx, |
| 279 const xmlChar* ch, | 286 const xmlChar* ch, |
| (...skipping 11 matching lines...) Expand all Loading... |
| 291 return NULL; | 298 return NULL; |
| 292 if (suggestion_method_ == TemplateURLParsingContext::POST) | 299 if (suggestion_method_ == TemplateURLParsingContext::POST) |
| 293 data_.suggestions_url.clear(); | 300 data_.suggestions_url.clear(); |
| 294 | 301 |
| 295 // If the image was a data URL, use the favicon from the search URL instead. | 302 // If the image was a data URL, use the favicon from the search URL instead. |
| 296 // (see the TODO in EndElementImpl()). | 303 // (see the TODO in EndElementImpl()). |
| 297 GURL search_url(data_.url()); | 304 GURL search_url(data_.url()); |
| 298 if (derive_image_from_url_ && data_.favicon_url.is_empty()) | 305 if (derive_image_from_url_ && data_.favicon_url.is_empty()) |
| 299 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); | 306 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); |
| 300 | 307 |
| 301 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); | 308 // Generate a keyword for this search engine if a custom one was not present |
| 309 // in the imported data. |
| 310 if (!data_.has_custom_keyword) |
| 311 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); |
| 312 |
| 302 data_.show_in_default_list = show_in_default_list; | 313 data_.show_in_default_list = show_in_default_list; |
| 303 | 314 |
| 304 // Bail if the search URL is empty or if either TemplateURLRef is invalid. | 315 // Bail if the search URL is empty or if either TemplateURLRef is invalid. |
| 305 scoped_ptr<TemplateURL> template_url(new TemplateURL(data_)); | 316 scoped_ptr<TemplateURL> template_url(new TemplateURL(data_)); |
| 306 if (template_url->url().empty() || | 317 if (template_url->url().empty() || |
| 307 !template_url->url_ref().IsValid(search_terms_data) || | 318 !template_url->url_ref().IsValid(search_terms_data) || |
| 308 (!template_url->suggestions_url().empty() && | 319 (!template_url->suggestions_url().empty() && |
| 309 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { | 320 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { |
| 310 return NULL; | 321 return NULL; |
| 311 } | 322 } |
| 312 | 323 |
| 313 return template_url.release(); | 324 return template_url.release(); |
| 314 } | 325 } |
| 315 | 326 |
| 316 // static | 327 // static |
| 317 void TemplateURLParsingContext::InitMapping() { | 328 void TemplateURLParsingContext::InitMapping() { |
| 318 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; | 329 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; |
| 319 (*kElementNameToElementTypeMap)[kURLElement] = URL; | 330 (*kElementNameToElementTypeMap)[kURLElement] = URL; |
| 320 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; | 331 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; |
| 321 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; | 332 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; |
| 322 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; | 333 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; |
| 323 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = | 334 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = |
| 324 OPEN_SEARCH_DESCRIPTION; | 335 OPEN_SEARCH_DESCRIPTION; |
| 325 (*kElementNameToElementTypeMap)[kFirefoxSearchDescriptionElement] = | 336 (*kElementNameToElementTypeMap)[kFirefoxSearchDescriptionElement] = |
| 326 OPEN_SEARCH_DESCRIPTION; | 337 OPEN_SEARCH_DESCRIPTION; |
| 327 (*kElementNameToElementTypeMap)[kInputEncodingElement] = INPUT_ENCODING; | 338 (*kElementNameToElementTypeMap)[kInputEncodingElement] = INPUT_ENCODING; |
| 339 (*kElementNameToElementTypeMap)[kAliasElement] = ALIAS; |
| 328 } | 340 } |
| 329 | 341 |
| 330 void TemplateURLParsingContext::ParseURL(const xmlChar** atts) { | 342 void TemplateURLParsingContext::ParseURL(const xmlChar** atts) { |
| 331 if (!atts) | 343 if (!atts) |
| 332 return; | 344 return; |
| 333 | 345 |
| 334 std::string template_url; | 346 std::string template_url; |
| 335 bool is_post = false; | 347 bool is_post = false; |
| 336 bool is_html_url = false; | 348 bool is_html_url = false; |
| 337 bool is_suggest_url = false; | 349 bool is_suggest_url = false; |
| (...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 484 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; | 496 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; |
| 485 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; | 497 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; |
| 486 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; | 498 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; |
| 487 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, | 499 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, |
| 488 static_cast<int>(length)); | 500 static_cast<int>(length)); |
| 489 xmlSubstituteEntitiesDefault(last_sub_entities_value); | 501 xmlSubstituteEntitiesDefault(last_sub_entities_value); |
| 490 | 502 |
| 491 return error ? | 503 return error ? |
| 492 NULL : context.GetTemplateURL(search_terms_data, show_in_default_list); | 504 NULL : context.GetTemplateURL(search_terms_data, show_in_default_list); |
| 493 } | 505 } |
| OLD | NEW |