Chromium Code Reviews| 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.h" | 5 #include "components/search_engines/template_url.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/command_line.h" | 10 #include "base/command_line.h" |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 89 return true; | 89 return true; |
| 90 std::string encoded_original_query; | 90 std::string encoded_original_query; |
| 91 if (!base::UTF16ToCodepage(original_query, encoding, | 91 if (!base::UTF16ToCodepage(original_query, encoding, |
| 92 base::OnStringConversionError::SKIP, &encoded_original_query)) | 92 base::OnStringConversionError::SKIP, &encoded_original_query)) |
| 93 return false; | 93 return false; |
| 94 *escaped_original_query = base::UTF8ToUTF16( | 94 *escaped_original_query = base::UTF8ToUTF16( |
| 95 net::EscapeQueryParamValue(encoded_original_query, true)); | 95 net::EscapeQueryParamValue(encoded_original_query, true)); |
| 96 return true; | 96 return true; |
| 97 } | 97 } |
| 98 | 98 |
| 99 // Returns true if the search term placeholder is present, and also produces | |
| 100 // the constant prefix/suffix found. | |
| 101 bool TryMatchSearchParam(base::StringPiece haystack, | |
| 102 base::StringPiece needle, | |
|
Peter Kasting
2016/04/21 19:48:57
Nit: I'd avoid references to English idioms like t
jbroman
2016/04/21 21:01:41
I'm used to variables being nouns; how about "text
Peter Kasting
2016/04/21 22:29:46
Works for me. I don't think anything is perfectly
| |
| 103 std::string* prefix, | |
| 104 std::string* suffix) { | |
|
Peter Kasting
2016/04/21 19:48:57
Since this is only ever called with the two string
jbroman
2016/04/21 21:01:42
I had some preference for keeping this method as e
Peter Kasting
2016/04/21 22:29:46
I think it's OK either way. If you like it the wa
| |
| 105 auto pos = haystack.find(needle); | |
| 106 if (pos == base::StringPiece::npos) | |
| 107 return false; | |
| 108 haystack.substr(0, pos).CopyToString(prefix); | |
| 109 haystack.substr(pos + needle.length()).CopyToString(suffix); | |
| 110 return true; | |
| 111 } | |
| 112 | |
| 99 // Extract query key and host given a list of parameters coming from the URL | 113 // Extract query key and host given a list of parameters coming from the URL |
| 100 // query or ref. | 114 // query or ref. |
| 101 std::string FindSearchTermsKey(const std::string& params) { | 115 struct SearchTermsKeyResult { |
| 116 std::string key; | |
| 117 std::string value_prefix; | |
| 118 std::string value_suffix; | |
| 119 bool found() const { return !key.empty(); } | |
| 120 }; | |
| 121 SearchTermsKeyResult FindSearchTermsKey(const std::string& params) { | |
| 122 SearchTermsKeyResult result; | |
| 102 if (params.empty()) | 123 if (params.empty()) |
| 103 return std::string(); | 124 return result; |
| 104 url::Component query, key, value; | 125 url::Component query, key, value; |
| 105 query.len = static_cast<int>(params.size()); | 126 query.len = static_cast<int>(params.size()); |
| 106 while (url::ExtractQueryKeyValue(params.c_str(), &query, &key, &value)) { | 127 while (url::ExtractQueryKeyValue(params.c_str(), &query, &key, &value)) { |
| 107 if (key.is_nonempty() && value.is_nonempty()) { | 128 if (key.is_nonempty() && value.is_nonempty()) { |
| 108 const base::StringPiece value_string(params.c_str() + value.begin, | 129 const base::StringPiece value_string(params.c_str() + value.begin, |
| 109 value.len); | 130 value.len); |
| 110 if (value_string.find(kSearchTermsParameterFull, 0) != | 131 if (TryMatchSearchParam(value_string, kSearchTermsParameterFull, |
| 111 base::StringPiece::npos || | 132 &result.value_prefix, &result.value_suffix) || |
| 112 value_string.find(kGoogleUnescapedSearchTermsParameterFull, 0) != | 133 TryMatchSearchParam(value_string, |
| 113 base::StringPiece::npos) { | 134 kGoogleUnescapedSearchTermsParameterFull, |
| 114 return params.substr(key.begin, key.len); | 135 &result.value_prefix, &result.value_suffix)) { |
| 136 result.key = params.substr(key.begin, key.len); | |
| 137 break; | |
| 115 } | 138 } |
| 116 } | 139 } |
| 117 } | 140 } |
| 118 return std::string(); | 141 return result; |
| 119 } | 142 } |
| 120 | 143 |
| 121 // Extract the position of the search terms' parameter in the URL path. | 144 // Extract the position of the search terms' parameter in the URL path. |
| 122 bool FindSearchTermsInPath(const std::string& path, | 145 bool FindSearchTermsInPath(const std::string& path, |
| 123 url::Component* parameter_position) { | 146 url::Component* parameter_position) { |
| 124 DCHECK(parameter_position); | 147 DCHECK(parameter_position); |
| 125 parameter_position->reset(); | 148 parameter_position->reset(); |
| 126 const size_t begin = path.find(kSearchTermsParameterFullEscaped); | 149 const size_t begin = path.find(kSearchTermsParameterFullEscaped); |
| 127 if (begin == std::string::npos) | 150 if (begin == std::string::npos) |
| 128 return false; | 151 return false; |
| (...skipping 380 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 key_found = true; | 532 key_found = true; |
| 510 position = value; | 533 position = value; |
| 511 } | 534 } |
| 512 } | 535 } |
| 513 } | 536 } |
| 514 if (!key_found) | 537 if (!key_found) |
| 515 return false; | 538 return false; |
| 516 } | 539 } |
| 517 | 540 |
| 518 // Extract the search term. | 541 // Extract the search term. |
| 519 *search_terms = SearchTermToString16( | 542 base::StringPiece search_term = |
| 520 source.substr(position.begin, position.len)); | 543 base::StringPiece(source).substr(position.begin, position.len); |
| 544 if (search_term.starts_with(search_term_value_prefix_)) | |
| 545 search_term.remove_prefix(search_term_value_prefix_.size()); | |
| 546 if (search_term.ends_with(search_term_value_suffix_)) | |
| 547 search_term.remove_suffix(search_term_value_suffix_.size()); | |
| 548 | |
| 549 *search_terms = SearchTermToString16(search_term.as_string()); | |
| 521 if (search_terms_component) | 550 if (search_terms_component) |
| 522 *search_terms_component = search_term_key_location_; | 551 *search_terms_component = search_term_key_location_; |
| 523 if (search_terms_position) | 552 if (search_terms_position) |
|
Peter Kasting
2016/04/21 19:48:57
Nit: {}
jbroman
2016/04/21 21:01:41
Obsoleted by addressing another comment.
| |
| 524 *search_terms_position = position; | 553 *search_terms_position = url::Component(search_term.begin() - source.data(), |
| 554 search_term.length()); | |
| 525 return true; | 555 return true; |
| 526 } | 556 } |
| 527 | 557 |
| 528 void TemplateURLRef::InvalidateCachedValues() const { | 558 void TemplateURLRef::InvalidateCachedValues() const { |
| 529 supports_replacements_ = valid_ = parsed_ = false; | 559 supports_replacements_ = valid_ = parsed_ = false; |
| 530 host_.clear(); | 560 host_.clear(); |
| 531 port_.clear(); | 561 port_.clear(); |
| 532 path_.clear(); | 562 path_.clear(); |
| 533 search_term_key_.clear(); | 563 search_term_key_.clear(); |
| 534 search_term_position_in_path_ = std::string::npos; | 564 search_term_position_in_path_ = std::string::npos; |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 776 &url_string, 0, "{google:baseURL}", | 806 &url_string, 0, "{google:baseURL}", |
| 777 search_terms_data.GoogleBaseURLValue()); | 807 search_terms_data.GoogleBaseURLValue()); |
| 778 base::ReplaceSubstringsAfterOffset( | 808 base::ReplaceSubstringsAfterOffset( |
| 779 &url_string, 0, "{google:baseSuggestURL}", | 809 &url_string, 0, "{google:baseSuggestURL}", |
| 780 search_terms_data.GoogleBaseSuggestURLValue()); | 810 search_terms_data.GoogleBaseSuggestURLValue()); |
| 781 | 811 |
| 782 GURL url(url_string); | 812 GURL url(url_string); |
| 783 if (!url.is_valid()) | 813 if (!url.is_valid()) |
| 784 return; | 814 return; |
| 785 | 815 |
| 786 std::string query_key = FindSearchTermsKey(url.query()); | 816 auto query_result = FindSearchTermsKey(url.query()); |
| 787 std::string ref_key = FindSearchTermsKey(url.ref()); | 817 auto ref_result = FindSearchTermsKey(url.ref()); |
| 788 url::Component parameter_position; | 818 url::Component parameter_position; |
| 789 const bool in_query = !query_key.empty(); | 819 const bool in_query = query_result.found(); |
| 790 const bool in_ref = !ref_key.empty(); | 820 const bool in_ref = ref_result.found(); |
| 791 const bool in_path = FindSearchTermsInPath(url.path(), ¶meter_position); | 821 const bool in_path = FindSearchTermsInPath(url.path(), ¶meter_position); |
| 792 if (in_query ? (in_ref || in_path) : (in_ref == in_path)) | 822 if (in_query ? (in_ref || in_path) : (in_ref == in_path)) |
| 793 return; // No key or multiple keys found. We only handle having one key. | 823 return; // No key or multiple keys found. We only handle having one key. |
| 794 | 824 |
| 795 host_ = url.host(); | 825 host_ = url.host(); |
| 796 port_ = url.port(); | 826 port_ = url.port(); |
| 797 path_ = url.path(); | 827 path_ = url.path(); |
| 798 if (in_query) { | 828 if (in_query) { |
| 799 search_term_key_ = query_key; | 829 search_term_key_ = query_result.key; |
| 800 search_term_key_location_ = url::Parsed::QUERY; | 830 search_term_key_location_ = url::Parsed::QUERY; |
| 831 search_term_value_prefix_ = query_result.value_prefix; | |
| 832 search_term_value_suffix_ = query_result.value_suffix; | |
| 801 } else if (in_ref) { | 833 } else if (in_ref) { |
| 802 search_term_key_ = ref_key; | 834 search_term_key_ = ref_result.key; |
| 803 search_term_key_location_ = url::Parsed::REF; | 835 search_term_key_location_ = url::Parsed::REF; |
| 836 search_term_value_prefix_ = ref_result.value_prefix; | |
| 837 search_term_value_suffix_ = ref_result.value_suffix; | |
| 804 } else { | 838 } else { |
| 805 DCHECK(in_path); | 839 DCHECK(in_path); |
| 806 DCHECK_GE(parameter_position.begin, 1); // Path must start with '/'. | 840 DCHECK_GE(parameter_position.begin, 1); // Path must start with '/'. |
| 807 search_term_key_location_ = url::Parsed::PATH; | 841 search_term_key_location_ = url::Parsed::PATH; |
| 808 search_term_position_in_path_ = parameter_position.begin; | 842 search_term_position_in_path_ = parameter_position.begin; |
| 809 // Remove the "{searchTerms}" itself from |path_|. | 843 // Remove the "{searchTerms}" itself from |path_|. |
| 810 path_.erase(parameter_position.begin, parameter_position.len); | 844 path_.erase(parameter_position.begin, parameter_position.len); |
| 811 } | 845 } |
| 812 } | 846 } |
| 813 | 847 |
| (...skipping 683 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1497 // patterns. This means that given patterns | 1531 // patterns. This means that given patterns |
| 1498 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], | 1532 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], |
| 1499 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would | 1533 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would |
| 1500 // return false. This is important for at least Google, where such URLs | 1534 // return false. This is important for at least Google, where such URLs |
| 1501 // are invalid. | 1535 // are invalid. |
| 1502 return !search_terms->empty(); | 1536 return !search_terms->empty(); |
| 1503 } | 1537 } |
| 1504 } | 1538 } |
| 1505 return false; | 1539 return false; |
| 1506 } | 1540 } |
| OLD | NEW |