| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/search_engines/template_url_parser.h" | 5 #include "chrome/browser/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 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/search_engines/ui_thread_search_terms_data.h" | |
| 17 #include "components/search_engines/template_url.h" | 16 #include "components/search_engines/template_url.h" |
| 18 #include "libxml/parser.h" | 17 #include "libxml/parser.h" |
| 19 #include "libxml/xmlwriter.h" | 18 #include "libxml/xmlwriter.h" |
| 20 #include "ui/gfx/favicon_size.h" | 19 #include "ui/gfx/favicon_size.h" |
| 21 #include "url/gurl.h" | 20 #include "url/gurl.h" |
| 22 #include "url/url_constants.h" | 21 #include "url/url_constants.h" |
| 23 | 22 |
| 24 namespace { | 23 namespace { |
| 25 | 24 |
| 26 // NOTE: libxml uses the UTF-8 encoding. As 0-127 of UTF-8 corresponds | 25 // NOTE: libxml uses the UTF-8 encoding. As 0-127 of UTF-8 corresponds |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 132 static void StartElementImpl(void* ctx, | 131 static void StartElementImpl(void* ctx, |
| 133 const xmlChar* name, | 132 const xmlChar* name, |
| 134 const xmlChar** atts); | 133 const xmlChar** atts); |
| 135 static void EndElementImpl(void* ctx, const xmlChar* name); | 134 static void EndElementImpl(void* ctx, const xmlChar* name); |
| 136 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); | 135 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); |
| 137 | 136 |
| 138 // Returns a heap-allocated TemplateURL representing the result of parsing. | 137 // Returns a heap-allocated TemplateURL representing the result of parsing. |
| 139 // This will be NULL if parsing failed or if the results were invalid for some | 138 // This will be NULL if parsing failed or if the results were invalid for some |
| 140 // reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied, | 139 // reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied, |
| 141 // a resulting TemplateURLRef was invalid, etc.). | 140 // a resulting TemplateURLRef was invalid, etc.). |
| 142 TemplateURL* GetTemplateURL(Profile* profile, bool show_in_default_list); | 141 TemplateURL* GetTemplateURL(const SearchTermsData& search_terms_data, |
| 142 bool show_in_default_list); |
| 143 | 143 |
| 144 private: | 144 private: |
| 145 // Key is UTF8 encoded. | 145 // Key is UTF8 encoded. |
| 146 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; | 146 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; |
| 147 | 147 |
| 148 static void InitMapping(); | 148 static void InitMapping(); |
| 149 | 149 |
| 150 void ParseURL(const xmlChar** atts); | 150 void ParseURL(const xmlChar** atts); |
| 151 void ParseImage(const xmlChar** atts); | 151 void ParseImage(const xmlChar** atts); |
| 152 void ParseParam(const xmlChar** atts); | 152 void ParseParam(const xmlChar** atts); |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 276 | 276 |
| 277 // static | 277 // static |
| 278 void TemplateURLParsingContext::CharactersImpl(void* ctx, | 278 void TemplateURLParsingContext::CharactersImpl(void* ctx, |
| 279 const xmlChar* ch, | 279 const xmlChar* ch, |
| 280 int len) { | 280 int len) { |
| 281 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += | 281 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += |
| 282 base::UTF8ToUTF16(std::string(reinterpret_cast<const char*>(ch), len)); | 282 base::UTF8ToUTF16(std::string(reinterpret_cast<const char*>(ch), len)); |
| 283 } | 283 } |
| 284 | 284 |
| 285 TemplateURL* TemplateURLParsingContext::GetTemplateURL( | 285 TemplateURL* TemplateURLParsingContext::GetTemplateURL( |
| 286 Profile* profile, | 286 const SearchTermsData& search_terms_data, |
| 287 bool show_in_default_list) { | 287 bool show_in_default_list) { |
| 288 // TODO(jcampan): Support engines that use POST; see http://crbug.com/18107 | 288 // TODO(jcampan): Support engines that use POST; see http://crbug.com/18107 |
| 289 if (method_ == TemplateURLParsingContext::POST || data_.short_name.empty() || | 289 if (method_ == TemplateURLParsingContext::POST || data_.short_name.empty() || |
| 290 !IsHTTPRef(data_.url()) || !IsHTTPRef(data_.suggestions_url)) | 290 !IsHTTPRef(data_.url()) || !IsHTTPRef(data_.suggestions_url)) |
| 291 return NULL; | 291 return NULL; |
| 292 if (suggestion_method_ == TemplateURLParsingContext::POST) | 292 if (suggestion_method_ == TemplateURLParsingContext::POST) |
| 293 data_.suggestions_url.clear(); | 293 data_.suggestions_url.clear(); |
| 294 | 294 |
| 295 // If the image was a data URL, use the favicon from the search URL instead. | 295 // If the image was a data URL, use the favicon from the search URL instead. |
| 296 // (see the TODO in EndElementImpl()). | 296 // (see the TODO in EndElementImpl()). |
| 297 GURL search_url(data_.url()); | 297 GURL search_url(data_.url()); |
| 298 if (derive_image_from_url_ && data_.favicon_url.is_empty()) | 298 if (derive_image_from_url_ && data_.favicon_url.is_empty()) |
| 299 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); | 299 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); |
| 300 | 300 |
| 301 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); | 301 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); |
| 302 data_.show_in_default_list = show_in_default_list; | 302 data_.show_in_default_list = show_in_default_list; |
| 303 | 303 |
| 304 // Bail if the search URL is empty or if either TemplateURLRef is invalid. | 304 // Bail if the search URL is empty or if either TemplateURLRef is invalid. |
| 305 scoped_ptr<TemplateURL> template_url(new TemplateURL(data_)); | 305 scoped_ptr<TemplateURL> template_url(new TemplateURL(data_)); |
| 306 scoped_ptr<SearchTermsData> search_terms_data(profile ? | |
| 307 new UIThreadSearchTermsData(profile) : new SearchTermsData()); | |
| 308 if (template_url->url().empty() || | 306 if (template_url->url().empty() || |
| 309 !template_url->url_ref().IsValid(*search_terms_data) || | 307 !template_url->url_ref().IsValid(search_terms_data) || |
| 310 (!template_url->suggestions_url().empty() && | 308 (!template_url->suggestions_url().empty() && |
| 311 !template_url->suggestions_url_ref().IsValid(*search_terms_data))) { | 309 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { |
| 312 return NULL; | 310 return NULL; |
| 313 } | 311 } |
| 314 | 312 |
| 315 return template_url.release(); | 313 return template_url.release(); |
| 316 } | 314 } |
| 317 | 315 |
| 318 // static | 316 // static |
| 319 void TemplateURLParsingContext::InitMapping() { | 317 void TemplateURLParsingContext::InitMapping() { |
| 320 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; | 318 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; |
| 321 (*kElementNameToElementTypeMap)[kURLElement] = URL; | 319 (*kElementNameToElementTypeMap)[kURLElement] = URL; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 463 // We only expect PARAM nodes under the URL node. | 461 // We only expect PARAM nodes under the URL node. |
| 464 return (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && | 462 return (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && |
| 465 elements_[1] == URL && elements_[2] == PARAM) ? PARAM : UNKNOWN; | 463 elements_[1] == URL && elements_[2] == PARAM) ? PARAM : UNKNOWN; |
| 466 } | 464 } |
| 467 | 465 |
| 468 | 466 |
| 469 // TemplateURLParser ---------------------------------------------------------- | 467 // TemplateURLParser ---------------------------------------------------------- |
| 470 | 468 |
| 471 // static | 469 // static |
| 472 TemplateURL* TemplateURLParser::Parse( | 470 TemplateURL* TemplateURLParser::Parse( |
| 473 Profile* profile, | 471 const SearchTermsData& search_terms_data, |
| 474 bool show_in_default_list, | 472 bool show_in_default_list, |
| 475 const char* data, | 473 const char* data, |
| 476 size_t length, | 474 size_t length, |
| 477 TemplateURLParser::ParameterFilter* param_filter) { | 475 TemplateURLParser::ParameterFilter* param_filter) { |
| 478 // xmlSubstituteEntitiesDefault(1) makes it so that & isn't mapped to | 476 // xmlSubstituteEntitiesDefault(1) makes it so that & isn't mapped to |
| 479 // & . Unfortunately xmlSubstituteEntitiesDefault affects global state. | 477 // & . Unfortunately xmlSubstituteEntitiesDefault affects global state. |
| 480 // If this becomes problematic we'll need to provide our own entity | 478 // If this becomes problematic we'll need to provide our own entity |
| 481 // type for &, or strip out & by hand after parsing. | 479 // type for &, or strip out & by hand after parsing. |
| 482 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); | 480 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); |
| 483 TemplateURLParsingContext context(param_filter); | 481 TemplateURLParsingContext context(param_filter); |
| 484 xmlSAXHandler sax_handler; | 482 xmlSAXHandler sax_handler; |
| 485 memset(&sax_handler, 0, sizeof(sax_handler)); | 483 memset(&sax_handler, 0, sizeof(sax_handler)); |
| 486 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; | 484 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; |
| 487 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; | 485 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; |
| 488 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; | 486 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; |
| 489 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, | 487 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, |
| 490 static_cast<int>(length)); | 488 static_cast<int>(length)); |
| 491 xmlSubstituteEntitiesDefault(last_sub_entities_value); | 489 xmlSubstituteEntitiesDefault(last_sub_entities_value); |
| 492 | 490 |
| 493 return error ? NULL : context.GetTemplateURL(profile, show_in_default_list); | 491 return error ? |
| 492 NULL : context.GetTemplateURL(search_terms_data, show_in_default_list); |
| 494 } | 493 } |
| OLD | NEW |