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 <string.h> | 7 #include <string.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <map> | 10 #include <map> |
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
139 const xmlChar* name, | 139 const xmlChar* name, |
140 const xmlChar** atts); | 140 const xmlChar** atts); |
141 static void EndElementImpl(void* ctx, const xmlChar* name); | 141 static void EndElementImpl(void* ctx, const xmlChar* name); |
142 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); | 142 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); |
143 | 143 |
144 // Returns a TemplateURL representing the result of parsing. This will be | 144 // Returns a TemplateURL representing the result of parsing. This will be |
145 // null if parsing failed or if the results were invalid for some reason (e.g. | 145 // null if parsing failed or if the results were invalid for some reason (e.g. |
146 // the resulting URL was not HTTP[S], a name wasn't supplied, a resulting | 146 // the resulting URL was not HTTP[S], a name wasn't supplied, a resulting |
147 // TemplateURLRef was invalid, etc.). | 147 // TemplateURLRef was invalid, etc.). |
148 std::unique_ptr<TemplateURL> GetTemplateURL( | 148 std::unique_ptr<TemplateURL> GetTemplateURL( |
149 const SearchTermsData& search_terms_data, | 149 const SearchTermsData& search_terms_data); |
150 bool show_in_default_list); | |
151 | 150 |
152 private: | 151 private: |
153 // Key is UTF8 encoded. | 152 // Key is UTF8 encoded. |
154 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; | 153 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; |
155 | 154 |
156 static void InitMapping(); | 155 static void InitMapping(); |
157 | 156 |
158 void ParseURL(const xmlChar** atts); | 157 void ParseURL(const xmlChar** atts); |
159 void ParseImage(const xmlChar** atts); | 158 void ParseImage(const xmlChar** atts); |
160 void ParseParam(const xmlChar** atts); | 159 void ParseParam(const xmlChar** atts); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 // static | 294 // static |
296 void TemplateURLParsingContext::CharactersImpl(void* ctx, | 295 void TemplateURLParsingContext::CharactersImpl(void* ctx, |
297 const xmlChar* ch, | 296 const xmlChar* ch, |
298 int len) { | 297 int len) { |
299 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += | 298 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += |
300 base::UTF8ToUTF16( | 299 base::UTF8ToUTF16( |
301 base::StringPiece(reinterpret_cast<const char*>(ch), len)); | 300 base::StringPiece(reinterpret_cast<const char*>(ch), len)); |
302 } | 301 } |
303 | 302 |
304 std::unique_ptr<TemplateURL> TemplateURLParsingContext::GetTemplateURL( | 303 std::unique_ptr<TemplateURL> TemplateURLParsingContext::GetTemplateURL( |
305 const SearchTermsData& search_terms_data, | 304 const SearchTermsData& search_terms_data) { |
306 bool show_in_default_list) { | |
307 // TODO(jcampan): Support engines that use POST; see http://crbug.com/18107 | 305 // TODO(jcampan): Support engines that use POST; see http://crbug.com/18107 |
308 if (method_ == TemplateURLParsingContext::POST || | 306 if (method_ == TemplateURLParsingContext::POST || |
309 data_.short_name().empty() || !IsHTTPRef(data_.url()) || | 307 data_.short_name().empty() || !IsHTTPRef(data_.url()) || |
310 !IsHTTPRef(data_.suggestions_url)) | 308 !IsHTTPRef(data_.suggestions_url)) |
311 return nullptr; | 309 return nullptr; |
312 if (suggestion_method_ == TemplateURLParsingContext::POST) | 310 if (suggestion_method_ == TemplateURLParsingContext::POST) |
313 data_.suggestions_url.clear(); | 311 data_.suggestions_url.clear(); |
314 | 312 |
315 // If the image was a data URL, use the favicon from the search URL instead. | 313 // If the image was a data URL, use the favicon from the search URL instead. |
316 // (see the TODO in EndElementImpl()). | 314 // (see the TODO in EndElementImpl()). |
317 GURL search_url(data_.url()); | 315 GURL search_url(data_.url()); |
318 if (derive_image_from_url_ && data_.favicon_url.is_empty()) | 316 if (derive_image_from_url_ && data_.favicon_url.is_empty()) |
319 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); | 317 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); |
320 | 318 |
321 // Generate a keyword for this search engine if a custom one was not present | 319 // Generate a keyword for this search engine if a custom one was not present |
322 // in the imported data. | 320 // in the imported data. |
323 if (!has_custom_keyword_) | 321 if (!has_custom_keyword_) |
324 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); | 322 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); |
325 | 323 |
326 data_.show_in_default_list = show_in_default_list; | |
327 | |
328 // Bail if the search URL is empty or if either TemplateURLRef is invalid. | 324 // Bail if the search URL is empty or if either TemplateURLRef is invalid. |
329 std::unique_ptr<TemplateURL> template_url = | 325 std::unique_ptr<TemplateURL> template_url = |
330 base::MakeUnique<TemplateURL>(data_); | 326 base::MakeUnique<TemplateURL>(data_); |
331 if (template_url->url().empty() || | 327 if (template_url->url().empty() || |
332 !template_url->url_ref().IsValid(search_terms_data) || | 328 !template_url->url_ref().IsValid(search_terms_data) || |
333 (!template_url->suggestions_url().empty() && | 329 (!template_url->suggestions_url().empty() && |
334 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { | 330 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { |
335 return nullptr; | 331 return nullptr; |
336 } | 332 } |
337 | 333 |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
488 return (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && | 484 return (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && |
489 elements_[1] == URL && elements_[2] == PARAM) ? PARAM : UNKNOWN; | 485 elements_[1] == URL && elements_[2] == PARAM) ? PARAM : UNKNOWN; |
490 } | 486 } |
491 | 487 |
492 | 488 |
493 // TemplateURLParser ---------------------------------------------------------- | 489 // TemplateURLParser ---------------------------------------------------------- |
494 | 490 |
495 // static | 491 // static |
496 std::unique_ptr<TemplateURL> TemplateURLParser::Parse( | 492 std::unique_ptr<TemplateURL> TemplateURLParser::Parse( |
497 const SearchTermsData& search_terms_data, | 493 const SearchTermsData& search_terms_data, |
498 bool show_in_default_list, | |
499 const char* data, | 494 const char* data, |
500 size_t length, | 495 size_t length, |
501 TemplateURLParser::ParameterFilter* param_filter) { | 496 TemplateURLParser::ParameterFilter* param_filter) { |
502 // xmlSubstituteEntitiesDefault(1) makes it so that & isn't mapped to | 497 // xmlSubstituteEntitiesDefault(1) makes it so that & isn't mapped to |
503 // & . Unfortunately xmlSubstituteEntitiesDefault affects global state. | 498 // & . Unfortunately xmlSubstituteEntitiesDefault affects global state. |
504 // If this becomes problematic we'll need to provide our own entity | 499 // If this becomes problematic we'll need to provide our own entity |
505 // type for &, or strip out & by hand after parsing. | 500 // type for &, or strip out & by hand after parsing. |
506 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); | 501 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); |
507 TemplateURLParsingContext context(param_filter); | 502 TemplateURLParsingContext context(param_filter); |
508 xmlSAXHandler sax_handler; | 503 xmlSAXHandler sax_handler; |
509 memset(&sax_handler, 0, sizeof(sax_handler)); | 504 memset(&sax_handler, 0, sizeof(sax_handler)); |
510 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; | 505 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; |
511 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; | 506 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; |
512 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; | 507 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; |
513 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, | 508 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, |
514 static_cast<int>(length)); | 509 static_cast<int>(length)); |
515 xmlSubstituteEntitiesDefault(last_sub_entities_value); | 510 xmlSubstituteEntitiesDefault(last_sub_entities_value); |
516 | 511 |
517 return error ? nullptr : context.GetTemplateURL(search_terms_data, | 512 return error ? nullptr : context.GetTemplateURL(search_terms_data); |
518 show_in_default_list); | |
519 } | 513 } |
OLD | NEW |