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.h" | 5 #include "chrome/browser/search_engines/template_url.h" |
6 | 6 |
7 #include "base/format_macros.h" | 7 #include "base/format_macros.h" |
8 #include "base/guid.h" | 8 #include "base/guid.h" |
9 #include "base/i18n/case_conversion.h" | 9 #include "base/i18n/case_conversion.h" |
10 #include "base/i18n/icu_string_conversions.h" | 10 #include "base/i18n/icu_string_conversions.h" |
(...skipping 398 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
409 string16 result = display_url; | 409 string16 result = display_url; |
410 ReplaceSubstringsAfterOffset(&result, 0, ASCIIToUTF16(kDisplaySearchTerms), | 410 ReplaceSubstringsAfterOffset(&result, 0, ASCIIToUTF16(kDisplaySearchTerms), |
411 ASCIIToUTF16(kSearchTermsParameterFull)); | 411 ASCIIToUTF16(kSearchTermsParameterFull)); |
412 ReplaceSubstringsAfterOffset( | 412 ReplaceSubstringsAfterOffset( |
413 &result, 0, | 413 &result, 0, |
414 ASCIIToUTF16(kDisplayUnescapedSearchTerms), | 414 ASCIIToUTF16(kDisplayUnescapedSearchTerms), |
415 ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull)); | 415 ASCIIToUTF16(kGoogleUnescapedSearchTermsParameterFull)); |
416 return UTF16ToUTF8(result); | 416 return UTF16ToUTF8(result); |
417 } | 417 } |
418 | 418 |
419 const std::string& TemplateURLRef::GetScheme() const { | |
420 ParseIfNecessary(); | |
421 return scheme_; | |
422 } | |
423 | |
424 const std::string& TemplateURLRef::GetHost() const { | 419 const std::string& TemplateURLRef::GetHost() const { |
425 ParseIfNecessary(); | 420 ParseIfNecessary(); |
426 return host_; | 421 return host_; |
427 } | 422 } |
428 | 423 |
429 const std::string& TemplateURLRef::GetPath() const { | 424 const std::string& TemplateURLRef::GetPath() const { |
430 ParseIfNecessary(); | 425 ParseIfNecessary(); |
431 return path_; | 426 return path_; |
432 } | 427 } |
433 | 428 |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
517 NULL); | 512 NULL); |
518 return true; | 513 return true; |
519 } | 514 } |
520 } | 515 } |
521 } | 516 } |
522 return false; | 517 return false; |
523 } | 518 } |
524 | 519 |
525 void TemplateURLRef::InvalidateCachedValues() const { | 520 void TemplateURLRef::InvalidateCachedValues() const { |
526 supports_replacements_ = valid_ = parsed_ = false; | 521 supports_replacements_ = valid_ = parsed_ = false; |
527 scheme_.clear(); | |
528 host_.clear(); | 522 host_.clear(); |
529 path_.clear(); | 523 path_.clear(); |
530 search_term_key_.clear(); | 524 search_term_key_.clear(); |
531 replacements_.clear(); | 525 replacements_.clear(); |
532 } | 526 } |
533 | 527 |
534 bool TemplateURLRef::ParseParameter(size_t start, | 528 bool TemplateURLRef::ParseParameter(size_t start, |
535 size_t end, | 529 size_t end, |
536 std::string* url, | 530 std::string* url, |
537 Replacements* replacements) const { | 531 Replacements* replacements) const { |
(...skipping 146 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 const SearchTermsData& search_terms_data) const { | 678 const SearchTermsData& search_terms_data) const { |
685 std::string url_string(GetURL()); | 679 std::string url_string(GetURL()); |
686 ReplaceSubstringsAfterOffset(&url_string, 0, | 680 ReplaceSubstringsAfterOffset(&url_string, 0, |
687 kGoogleBaseURLParameterFull, | 681 kGoogleBaseURLParameterFull, |
688 search_terms_data.GoogleBaseURLValue()); | 682 search_terms_data.GoogleBaseURLValue()); |
689 ReplaceSubstringsAfterOffset(&url_string, 0, | 683 ReplaceSubstringsAfterOffset(&url_string, 0, |
690 kGoogleBaseSuggestURLParameterFull, | 684 kGoogleBaseSuggestURLParameterFull, |
691 search_terms_data.GoogleBaseSuggestURLValue()); | 685 search_terms_data.GoogleBaseSuggestURLValue()); |
692 | 686 |
693 search_term_key_.clear(); | 687 search_term_key_.clear(); |
694 scheme_.clear(); | |
695 host_.clear(); | 688 host_.clear(); |
696 path_.clear(); | 689 path_.clear(); |
697 search_term_key_location_ = url_parse::Parsed::REF; | 690 search_term_key_location_ = url_parse::Parsed::REF; |
698 | 691 |
699 GURL url(url_string); | 692 GURL url(url_string); |
700 if (!url.is_valid()) | 693 if (!url.is_valid()) |
701 return; | 694 return; |
702 | 695 |
703 std::string query_key = FindSearchTermsKey(url.query()); | 696 std::string query_key = FindSearchTermsKey(url.query()); |
704 std::string ref_key = FindSearchTermsKey(url.ref()); | 697 std::string ref_key = FindSearchTermsKey(url.ref()); |
705 if (query_key.empty() == ref_key.empty()) | 698 if (query_key.empty() == ref_key.empty()) |
706 return; // No key or multiple keys found. We only handle having one key. | 699 return; // No key or multiple keys found. We only handle having one key. |
707 search_term_key_ = query_key.empty() ? ref_key : query_key; | 700 search_term_key_ = query_key.empty() ? ref_key : query_key; |
708 search_term_key_location_ = query_key.empty() ? | 701 search_term_key_location_ = query_key.empty() ? |
709 url_parse::Parsed::REF : url_parse::Parsed::QUERY; | 702 url_parse::Parsed::REF : url_parse::Parsed::QUERY; |
710 scheme_ = url.scheme(); | |
711 host_ = url.host(); | 703 host_ = url.host(); |
712 path_ = url.path(); | 704 path_ = url.path(); |
713 } | 705 } |
714 | 706 |
715 | 707 |
716 // TemplateURLData ------------------------------------------------------------ | 708 // TemplateURLData ------------------------------------------------------------ |
717 | 709 |
718 TemplateURLData::TemplateURLData() | 710 TemplateURLData::TemplateURLData() |
719 : show_in_default_list(false), | 711 : show_in_default_list(false), |
720 safe_for_autoreplace(false), | 712 safe_for_autoreplace(false), |
(...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
871 if (key.is_nonempty() && | 863 if (key.is_nonempty() && |
872 params[i].substr(key.begin, key.len) == | 864 params[i].substr(key.begin, key.len) == |
873 search_terms_replacement_key()) { | 865 search_terms_replacement_key()) { |
874 return true; | 866 return true; |
875 } | 867 } |
876 } | 868 } |
877 } | 869 } |
878 return false; | 870 return false; |
879 } | 871 } |
880 | 872 |
881 bool TemplateURL::IsInstantURL(const GURL& url) { | |
882 // If the url matches the Instant ref, there's no need to | |
883 // check the replacement-key parameter, since we know this | |
884 // is instant. | |
885 // TODO(dhollowa): http://crbug.com/170390. Consolidate Instant URL checks. | |
886 TemplateURLRef ref(this, TemplateURLRef::INSTANT); | |
887 GURL instant_url(ref.ReplaceSearchTerms( | |
888 TemplateURLRef::SearchTermsArgs(string16()))); | |
889 if (instant_url.scheme() == url.scheme() && | |
890 instant_url.host() == url.host() && | |
891 instant_url.path() == url.path()) | |
892 return true; | |
893 | |
894 // Anything else requires the existence of the replacement-key. | |
895 if (!HasSearchTermsReplacementKey(url)) | |
896 return false; | |
897 | |
898 for (size_t i = 0; i < URLCount(); ++i) { | |
899 TemplateURLRef ref(this, i); | |
900 if (ref.GetScheme() == url.scheme() && | |
901 ref.GetHost() == url.host() && | |
902 ref.GetPath() == url.path()) | |
903 return true; | |
904 } | |
905 | |
906 return false; | |
907 } | |
908 | |
909 void TemplateURL::CopyFrom(const TemplateURL& other) { | 873 void TemplateURL::CopyFrom(const TemplateURL& other) { |
910 if (this == &other) | 874 if (this == &other) |
911 return; | 875 return; |
912 | 876 |
913 profile_ = other.profile_; | 877 profile_ = other.profile_; |
914 data_ = other.data_; | 878 data_ = other.data_; |
915 url_ref_.InvalidateCachedValues(); | 879 url_ref_.InvalidateCachedValues(); |
916 suggestions_url_ref_.InvalidateCachedValues(); | 880 suggestions_url_ref_.InvalidateCachedValues(); |
917 instant_url_ref_.InvalidateCachedValues(); | 881 instant_url_ref_.InvalidateCachedValues(); |
918 SetPrepopulateId(other.data_.prepopulate_id); | 882 SetPrepopulateId(other.data_.prepopulate_id); |
(...skipping 13 matching lines...) Expand all Loading... |
932 } | 896 } |
933 | 897 |
934 void TemplateURL::ResetKeywordIfNecessary(bool force) { | 898 void TemplateURL::ResetKeywordIfNecessary(bool force) { |
935 if (IsGoogleSearchURLWithReplaceableKeyword() || force) { | 899 if (IsGoogleSearchURLWithReplaceableKeyword() || force) { |
936 DCHECK(!IsExtensionKeyword()); | 900 DCHECK(!IsExtensionKeyword()); |
937 GURL url(TemplateURLService::GenerateSearchURL(this)); | 901 GURL url(TemplateURLService::GenerateSearchURL(this)); |
938 if (url.is_valid()) | 902 if (url.is_valid()) |
939 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); | 903 data_.SetKeyword(TemplateURLService::GenerateKeyword(url)); |
940 } | 904 } |
941 } | 905 } |
OLD | NEW |