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 |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 std::string* query) { | 87 std::string* query) { |
88 if (!query->empty()) | 88 if (!query->empty()) |
89 query->append("&"); | 89 query->append("&"); |
90 if (!key.empty()) { | 90 if (!key.empty()) { |
91 query->append(key); | 91 query->append(key); |
92 query->append("="); | 92 query->append("="); |
93 } | 93 } |
94 query->append(value); | 94 query->append(value); |
95 } | 95 } |
96 | 96 |
97 // Returns true if the ref is null, or the url wrapped by ref is | 97 // Returns true if |url| is empty or is a valid URL with a scheme of HTTP[S]. |
98 // valid with a spec of http/https. | 98 bool IsHTTPRef(const std::string& url) { |
99 bool IsHTTPRef(const TemplateURLRef* ref) { | 99 if (url.empty()) |
100 if (ref == NULL) | |
101 return true; | 100 return true; |
102 GURL url(ref->url()); | 101 GURL gurl(url); |
103 return (url.is_valid() && (url.SchemeIs(chrome::kHttpScheme) || | 102 return (gurl.is_valid() && (gurl.SchemeIs(chrome::kHttpScheme) || |
104 url.SchemeIs(chrome::kHttpsScheme))); | 103 gurl.SchemeIs(chrome::kHttpsScheme))); |
105 } | 104 } |
106 | 105 |
107 } // namespace | 106 } // namespace |
108 | 107 |
109 | 108 |
110 // TemplateURLParsingContext -------------------------------------------------- | 109 // TemplateURLParsingContext -------------------------------------------------- |
111 | 110 |
112 // To minimize memory overhead while parsing, a SAX style parser is used. | 111 // To minimize memory overhead while parsing, a SAX style parser is used. |
113 // TemplateURLParsingContext is used to maintain the state we're in the document | 112 // TemplateURLParsingContext is used to maintain the state we're in the document |
114 // while parsing. | 113 // while parsing. |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
292 } | 291 } |
293 | 292 |
294 TemplateURL* TemplateURLParsingContext::GetTemplateURL(Profile* profile) { | 293 TemplateURL* TemplateURLParsingContext::GetTemplateURL(Profile* profile) { |
295 // Basic legality checks. | 294 // Basic legality checks. |
296 if (url_->short_name_.empty() || !IsHTTPRef(url_->url()) || | 295 if (url_->short_name_.empty() || !IsHTTPRef(url_->url()) || |
297 !IsHTTPRef(url_->suggestions_url())) | 296 !IsHTTPRef(url_->suggestions_url())) |
298 return NULL; | 297 return NULL; |
299 | 298 |
300 // If the image was a data URL, use the favicon from the search URL instead. | 299 // If the image was a data URL, use the favicon from the search URL instead. |
301 // (see TODO inEndElementImpl()). | 300 // (see TODO inEndElementImpl()). |
302 GURL url(url_->url()->url()); | 301 GURL url(url_->url()); |
303 if (derive_image_from_url_ && url_->favicon_url().is_empty()) | 302 if (derive_image_from_url_ && url_->favicon_url().is_empty()) |
304 url_->set_favicon_url(TemplateURL::GenerateFaviconURL(url)); | 303 url_->set_favicon_url(TemplateURL::GenerateFaviconURL(url)); |
305 | 304 |
306 // TODO(jcampan): http://b/issue?id=1196285 we do not support search engines | 305 // TODO(jcampan): http://b/issue?id=1196285 we do not support search engines |
307 // that use POST yet. | 306 // that use POST yet. |
308 if (method_ == TemplateURLParsingContext::POST) | 307 if (method_ == TemplateURLParsingContext::POST) |
309 return NULL; | 308 return NULL; |
310 if (suggestion_method_ == TemplateURLParsingContext::POST) | 309 if (suggestion_method_ == TemplateURLParsingContext::POST) |
311 url_->SetSuggestionsURL(std::string()); | 310 url_->SetSuggestionsURL(std::string()); |
312 | 311 |
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
407 | 406 |
408 if (!key.empty() && | 407 if (!key.empty() && |
409 (!parameter_filter_ || parameter_filter_->KeepParameter(key, value))) | 408 (!parameter_filter_ || parameter_filter_->KeepParameter(key, value))) |
410 extra_params_.push_back(Param(key, value)); | 409 extra_params_.push_back(Param(key, value)); |
411 } | 410 } |
412 | 411 |
413 void TemplateURLParsingContext::ProcessURLParams() { | 412 void TemplateURLParsingContext::ProcessURLParams() { |
414 if (!parameter_filter_ && extra_params_.empty()) | 413 if (!parameter_filter_ && extra_params_.empty()) |
415 return; | 414 return; |
416 | 415 |
417 const TemplateURLRef* t_url_ref = | 416 GURL url(is_suggest_url_ ? url_->suggestions_url() : url_->url()); |
418 is_suggest_url_ ? url_->suggestions_url() : url_->url(); | 417 if (url.is_empty()) |
419 if (!t_url_ref) | |
420 return; | 418 return; |
421 GURL url(t_url_ref->url()); | 419 |
422 // If there is a parameter filter, parse the existing URL and remove any | 420 // If there is a parameter filter, parse the existing URL and remove any |
423 // unwanted parameter. | 421 // unwanted parameter. |
424 std::string new_query; | 422 std::string new_query; |
425 bool modified = false; | 423 bool modified = false; |
426 if (parameter_filter_) { | 424 if (parameter_filter_) { |
427 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; | 425 url_parse::Component query = url.parsed_for_possibly_invalid_spec().query; |
428 url_parse::Component key, value; | 426 url_parse::Component key, value; |
429 const char* url_spec = url.spec().c_str(); | 427 const char* url_spec = url.spec().c_str(); |
430 while (url_parse::ExtractQueryKeyValue(url_spec, &query, &key, &value)) { | 428 while (url_parse::ExtractQueryKeyValue(url_spec, &query, &key, &value)) { |
431 std::string key_str(url_spec, key.begin, key.len); | 429 std::string key_str(url_spec, key.begin, key.len); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
486 xmlSAXHandler sax_handler; | 484 xmlSAXHandler sax_handler; |
487 memset(&sax_handler, 0, sizeof(sax_handler)); | 485 memset(&sax_handler, 0, sizeof(sax_handler)); |
488 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; | 486 sax_handler.startElement = &TemplateURLParsingContext::StartElementImpl; |
489 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; | 487 sax_handler.endElement = &TemplateURLParsingContext::EndElementImpl; |
490 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; | 488 sax_handler.characters = &TemplateURLParsingContext::CharactersImpl; |
491 xmlSAXUserParseMemory(&sax_handler, &context, data, static_cast<int>(length)); | 489 xmlSAXUserParseMemory(&sax_handler, &context, data, static_cast<int>(length)); |
492 xmlSubstituteEntitiesDefault(last_sub_entities_value); | 490 xmlSubstituteEntitiesDefault(last_sub_entities_value); |
493 | 491 |
494 return context.GetTemplateURL(profile); | 492 return context.GetTemplateURL(profile); |
495 } | 493 } |
OLD | NEW |