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> |
11 #include <memory> | 11 #include <memory> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/logging.h" | 14 #include "base/logging.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/ptr_util.h" | |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
18 #include "base/strings/utf_string_conversions.h" | 19 #include "base/strings/utf_string_conversions.h" |
19 #include "components/search_engines/search_terms_data.h" | 20 #include "components/search_engines/search_terms_data.h" |
20 #include "components/search_engines/template_url.h" | 21 #include "components/search_engines/template_url.h" |
21 #include "libxml/parser.h" | 22 #include "libxml/parser.h" |
22 #include "libxml/xmlwriter.h" | 23 #include "libxml/xmlwriter.h" |
23 #include "ui/gfx/favicon_size.h" | 24 #include "ui/gfx/favicon_size.h" |
24 #include "url/gurl.h" | 25 #include "url/gurl.h" |
25 #include "url/url_constants.h" | 26 #include "url/url_constants.h" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
133 | 134 |
134 explicit TemplateURLParsingContext( | 135 explicit TemplateURLParsingContext( |
135 TemplateURLParser::ParameterFilter* parameter_filter); | 136 TemplateURLParser::ParameterFilter* parameter_filter); |
136 | 137 |
137 static void StartElementImpl(void* ctx, | 138 static void StartElementImpl(void* ctx, |
138 const xmlChar* name, | 139 const xmlChar* name, |
139 const xmlChar** atts); | 140 const xmlChar** atts); |
140 static void EndElementImpl(void* ctx, const xmlChar* name); | 141 static void EndElementImpl(void* ctx, const xmlChar* name); |
141 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); | 142 static void CharactersImpl(void* ctx, const xmlChar* ch, int len); |
142 | 143 |
143 // Returns a heap-allocated TemplateURL representing the result of parsing. | 144 // Returns a TemplateURL representing the result of parsing. This will be an |
144 // This will be NULL if parsing failed or if the results were invalid for some | 145 // empty unique_ptr if parsing failed or if the results were invalid for some |
Peter Kasting
2016/09/01 22:55:41
Nit: "This will be null"?
Avi (use Gerrit)
2016/09/01 23:28:33
Done.
| |
145 // reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied, | 146 // reason (e.g. the resulting URL was not HTTP[S], a name wasn't supplied, a |
146 // a resulting TemplateURLRef was invalid, etc.). | 147 // resulting TemplateURLRef was invalid, etc.). |
147 TemplateURL* GetTemplateURL(const SearchTermsData& search_terms_data, | 148 std::unique_ptr<TemplateURL> GetTemplateURL( |
148 bool show_in_default_list); | 149 const SearchTermsData& search_terms_data, |
150 bool show_in_default_list); | |
149 | 151 |
150 private: | 152 private: |
151 // Key is UTF8 encoded. | 153 // Key is UTF8 encoded. |
152 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; | 154 typedef std::map<std::string, ElementType> ElementNameToElementTypeMap; |
153 | 155 |
154 static void InitMapping(); | 156 static void InitMapping(); |
155 | 157 |
156 void ParseURL(const xmlChar** atts); | 158 void ParseURL(const xmlChar** atts); |
157 void ParseImage(const xmlChar** atts); | 159 void ParseImage(const xmlChar** atts); |
158 void ParseParam(const xmlChar** atts); | 160 void ParseParam(const xmlChar** atts); |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
292 | 294 |
293 // static | 295 // static |
294 void TemplateURLParsingContext::CharactersImpl(void* ctx, | 296 void TemplateURLParsingContext::CharactersImpl(void* ctx, |
295 const xmlChar* ch, | 297 const xmlChar* ch, |
296 int len) { | 298 int len) { |
297 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += | 299 reinterpret_cast<TemplateURLParsingContext*>(ctx)->string_ += |
298 base::UTF8ToUTF16( | 300 base::UTF8ToUTF16( |
299 base::StringPiece(reinterpret_cast<const char*>(ch), len)); | 301 base::StringPiece(reinterpret_cast<const char*>(ch), len)); |
300 } | 302 } |
301 | 303 |
302 TemplateURL* TemplateURLParsingContext::GetTemplateURL( | 304 std::unique_ptr<TemplateURL> TemplateURLParsingContext::GetTemplateURL( |
303 const SearchTermsData& search_terms_data, | 305 const SearchTermsData& search_terms_data, |
304 bool show_in_default_list) { | 306 bool show_in_default_list) { |
305 // TODO(jcampan): Support engines that use POST; see http://crbug.com/18107 | 307 // TODO(jcampan): Support engines that use POST; see http://crbug.com/18107 |
306 if (method_ == TemplateURLParsingContext::POST || | 308 if (method_ == TemplateURLParsingContext::POST || |
307 data_.short_name().empty() || !IsHTTPRef(data_.url()) || | 309 data_.short_name().empty() || !IsHTTPRef(data_.url()) || |
308 !IsHTTPRef(data_.suggestions_url)) | 310 !IsHTTPRef(data_.suggestions_url)) |
309 return NULL; | 311 return std::unique_ptr<TemplateURL>(); |
Peter Kasting
2016/09/01 22:55:41
Nit: return nullptr?
| |
310 if (suggestion_method_ == TemplateURLParsingContext::POST) | 312 if (suggestion_method_ == TemplateURLParsingContext::POST) |
311 data_.suggestions_url.clear(); | 313 data_.suggestions_url.clear(); |
312 | 314 |
313 // If the image was a data URL, use the favicon from the search URL instead. | 315 // If the image was a data URL, use the favicon from the search URL instead. |
314 // (see the TODO in EndElementImpl()). | 316 // (see the TODO in EndElementImpl()). |
315 GURL search_url(data_.url()); | 317 GURL search_url(data_.url()); |
316 if (derive_image_from_url_ && data_.favicon_url.is_empty()) | 318 if (derive_image_from_url_ && data_.favicon_url.is_empty()) |
317 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); | 319 data_.favicon_url = TemplateURL::GenerateFaviconURL(search_url); |
318 | 320 |
319 // Generate a keyword for this search engine if a custom one was not present | 321 // Generate a keyword for this search engine if a custom one was not present |
320 // in the imported data. | 322 // in the imported data. |
321 if (!has_custom_keyword_) | 323 if (!has_custom_keyword_) |
322 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); | 324 data_.SetKeyword(TemplateURL::GenerateKeyword(search_url)); |
323 | 325 |
324 data_.show_in_default_list = show_in_default_list; | 326 data_.show_in_default_list = show_in_default_list; |
325 | 327 |
326 // Bail if the search URL is empty or if either TemplateURLRef is invalid. | 328 // Bail if the search URL is empty or if either TemplateURLRef is invalid. |
327 std::unique_ptr<TemplateURL> template_url(new TemplateURL(data_)); | 329 std::unique_ptr<TemplateURL> template_url( |
Peter Kasting
2016/09/01 22:55:41
Nit: () -> =
Avi (use Gerrit)
2016/09/01 23:28:33
Done.
| |
330 base::MakeUnique<TemplateURL>(data_)); | |
328 if (template_url->url().empty() || | 331 if (template_url->url().empty() || |
329 !template_url->url_ref().IsValid(search_terms_data) || | 332 !template_url->url_ref().IsValid(search_terms_data) || |
330 (!template_url->suggestions_url().empty() && | 333 (!template_url->suggestions_url().empty() && |
331 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { | 334 !template_url->suggestions_url_ref().IsValid(search_terms_data))) { |
332 return NULL; | 335 return std::unique_ptr<TemplateURL>(); |
Peter Kasting
2016/09/01 22:55:41
Nit: return nullptr?
Avi (use Gerrit)
2016/09/01 23:28:33
Done.
| |
333 } | 336 } |
334 | 337 |
335 return template_url.release(); | 338 return template_url; |
336 } | 339 } |
337 | 340 |
338 // static | 341 // static |
339 void TemplateURLParsingContext::InitMapping() { | 342 void TemplateURLParsingContext::InitMapping() { |
340 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; | 343 kElementNameToElementTypeMap = new std::map<std::string, ElementType>; |
341 (*kElementNameToElementTypeMap)[kURLElement] = URL; | 344 (*kElementNameToElementTypeMap)[kURLElement] = URL; |
342 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; | 345 (*kElementNameToElementTypeMap)[kParamElement] = PARAM; |
343 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; | 346 (*kElementNameToElementTypeMap)[kShortNameElement] = SHORT_NAME; |
344 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; | 347 (*kElementNameToElementTypeMap)[kImageElement] = IMAGE; |
345 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = | 348 (*kElementNameToElementTypeMap)[kOpenSearchDescriptionElement] = |
(...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
483 return elements_[1]; | 486 return elements_[1]; |
484 // We only expect PARAM nodes under the URL node. | 487 // We only expect PARAM nodes under the URL node. |
485 return (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && | 488 return (elements_.size() == 3 && elements_[0] == OPEN_SEARCH_DESCRIPTION && |
486 elements_[1] == URL && elements_[2] == PARAM) ? PARAM : UNKNOWN; | 489 elements_[1] == URL && elements_[2] == PARAM) ? PARAM : UNKNOWN; |
487 } | 490 } |
488 | 491 |
489 | 492 |
490 // TemplateURLParser ---------------------------------------------------------- | 493 // TemplateURLParser ---------------------------------------------------------- |
491 | 494 |
492 // static | 495 // static |
493 TemplateURL* TemplateURLParser::Parse( | 496 std::unique_ptr<TemplateURL> TemplateURLParser::Parse( |
494 const SearchTermsData& search_terms_data, | 497 const SearchTermsData& search_terms_data, |
495 bool show_in_default_list, | 498 bool show_in_default_list, |
496 const char* data, | 499 const char* data, |
497 size_t length, | 500 size_t length, |
498 TemplateURLParser::ParameterFilter* param_filter) { | 501 TemplateURLParser::ParameterFilter* param_filter) { |
499 // xmlSubstituteEntitiesDefault(1) makes it so that & isn't mapped to | 502 // xmlSubstituteEntitiesDefault(1) makes it so that & isn't mapped to |
500 // & . Unfortunately xmlSubstituteEntitiesDefault affects global state. | 503 // & . Unfortunately xmlSubstituteEntitiesDefault affects global state. |
501 // If this becomes problematic we'll need to provide our own entity | 504 // If this becomes problematic we'll need to provide our own entity |
502 // type for &, or strip out & by hand after parsing. | 505 // type for &, or strip out & by hand after parsing. |
503 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); | 506 int last_sub_entities_value = xmlSubstituteEntitiesDefault(1); |
504 TemplateURLParsingContext context(param_filter); | 507 TemplateURLParsingContext context(param_filter); |
505 xmlSAXHandler sax_handler; | 508 xmlSAXHandler sax_handler; |
506 memset(&sax_handler, 0, sizeof(sax_handler)); | 509 memset(&sax_handler, 0, sizeof(sax_handler)); |
507 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; | 510 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; |
508 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; | 511 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; |
509 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; | 512 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; |
510 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, | 513 int error = xmlSAXUserParseMemory(&sax_handler, &context, data, |
511 static_cast<int>(length)); | 514 static_cast<int>(length)); |
512 xmlSubstituteEntitiesDefault(last_sub_entities_value); | 515 xmlSubstituteEntitiesDefault(last_sub_entities_value); |
513 | 516 |
514 return error ? | 517 return error ? nullptr : context.GetTemplateURL(search_terms_data, |
515 NULL : context.GetTemplateURL(search_terms_data, show_in_default_list); | 518 show_in_default_list); |
516 } | 519 } |
OLD | NEW |