Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(150)

Side by Side Diff: components/search_engines/template_url.cc

Issue 2481923002: [WIP] make GURL::path() return a StringPiece (Closed)
Patch Set: thanks asan Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 &result.value_prefix, &result.value_suffix)) { 136 &result.value_prefix, &result.value_suffix)) {
137 result.key = params.substr(key.begin, key.len); 137 result.key = params.substr(key.begin, key.len);
138 break; 138 break;
139 } 139 }
140 } 140 }
141 } 141 }
142 return result; 142 return result;
143 } 143 }
144 144
145 // Extract the position of the search terms' parameter in the URL path. 145 // Extract the position of the search terms' parameter in the URL path.
146 bool FindSearchTermsInPath(const std::string& path, 146 bool FindSearchTermsInPath(const base::StringPiece& path,
147 url::Component* parameter_position) { 147 url::Component* parameter_position) {
148 DCHECK(parameter_position); 148 DCHECK(parameter_position);
149 parameter_position->reset(); 149 parameter_position->reset();
150 const size_t begin = path.find(kSearchTermsParameterFullEscaped); 150 const size_t begin = path.find(kSearchTermsParameterFullEscaped);
151 if (begin == std::string::npos) 151 if (begin == std::string::npos)
152 return false; 152 return false;
153 parameter_position->begin = begin; 153 parameter_position->begin = begin;
154 parameter_position->len = arraysize(kSearchTermsParameterFullEscaped) - 1; 154 parameter_position->len = arraysize(kSearchTermsParameterFullEscaped) - 1;
155 return true; 155 return true;
156 } 156 }
(...skipping 336 matching lines...) Expand 10 before | Expand all | Expand 10 after
493 (url.port() != port_) || 493 (url.port() != port_) ||
494 ((url.path() != path_) && 494 ((url.path() != path_) &&
495 (search_term_key_location_ != url::Parsed::PATH))) { 495 (search_term_key_location_ != url::Parsed::PATH))) {
496 return false; 496 return false;
497 } 497 }
498 498
499 std::string source; 499 std::string source;
500 url::Component position; 500 url::Component position;
501 501
502 if (search_term_key_location_ == url::Parsed::PATH) { 502 if (search_term_key_location_ == url::Parsed::PATH) {
503 source = url.path(); 503 source = url.path().as_string();
504 504
505 // Characters in the path before and after search terms must match. 505 // Characters in the path before and after search terms must match.
506 if (source.length() < path_.length()) 506 if (source.length() < path_.length())
507 return false; 507 return false;
508 position.begin = search_term_position_in_path_; 508 position.begin = search_term_position_in_path_;
509 position.len = source.length() - path_.length(); 509 position.len = source.length() - path_.length();
510 if (source.substr(0, position.begin) + source.substr(position.end()) != 510 if (source.substr(0, position.begin) + source.substr(position.end()) !=
511 path_) 511 path_)
512 return false; 512 return false;
513 } else { 513 } else {
(...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
817 auto ref_result = FindSearchTermsKey(url.ref()); 817 auto ref_result = FindSearchTermsKey(url.ref());
818 url::Component parameter_position; 818 url::Component parameter_position;
819 const bool in_query = query_result.found(); 819 const bool in_query = query_result.found();
820 const bool in_ref = ref_result.found(); 820 const bool in_ref = ref_result.found();
821 const bool in_path = FindSearchTermsInPath(url.path(), &parameter_position); 821 const bool in_path = FindSearchTermsInPath(url.path(), &parameter_position);
822 if (in_query ? (in_ref || in_path) : (in_ref == in_path)) 822 if (in_query ? (in_ref || in_path) : (in_ref == in_path))
823 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.
824 824
825 host_ = url.host(); 825 host_ = url.host();
826 port_ = url.port(); 826 port_ = url.port();
827 path_ = url.path(); 827 path_ = url.path().as_string();
828 if (in_query) { 828 if (in_query) {
829 search_term_key_ = query_result.key; 829 search_term_key_ = query_result.key;
830 search_term_key_location_ = url::Parsed::QUERY; 830 search_term_key_location_ = url::Parsed::QUERY;
831 search_term_value_prefix_ = query_result.value_prefix; 831 search_term_value_prefix_ = query_result.value_prefix;
832 search_term_value_suffix_ = query_result.value_suffix; 832 search_term_value_suffix_ = query_result.value_suffix;
833 } else if (in_ref) { 833 } else if (in_ref) {
834 search_term_key_ = ref_result.key; 834 search_term_key_ = ref_result.key;
835 search_term_key_location_ = url::Parsed::REF; 835 search_term_key_location_ = url::Parsed::REF;
836 search_term_value_prefix_ = ref_result.value_prefix; 836 search_term_value_prefix_ = ref_result.value_prefix;
837 search_term_value_suffix_ = ref_result.value_suffix; 837 search_term_value_suffix_ = ref_result.value_suffix;
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after
1379 EncodeSearchTerms(search_terms_args, is_in_query, &input_encoding, 1379 EncodeSearchTerms(search_terms_args, is_in_query, &input_encoding,
1380 &encoded_terms, &encoded_original_query); 1380 &encoded_terms, &encoded_original_query);
1381 1381
1382 std::string old_params; 1382 std::string old_params;
1383 if (search_term_component == url::Parsed::QUERY) { 1383 if (search_term_component == url::Parsed::QUERY) {
1384 old_params = url.query(); 1384 old_params = url.query();
1385 } else if (search_term_component == url::Parsed::REF) { 1385 } else if (search_term_component == url::Parsed::REF) {
1386 old_params = url.ref(); 1386 old_params = url.ref();
1387 } else { 1387 } else {
1388 DCHECK_EQ(search_term_component, url::Parsed::PATH); 1388 DCHECK_EQ(search_term_component, url::Parsed::PATH);
1389 old_params = url.path(); 1389 old_params = url.path().as_string();
1390 } 1390 }
1391 1391
1392 std::string new_params(old_params, 0, search_terms_position.begin); 1392 std::string new_params(old_params, 0, search_terms_position.begin);
1393 new_params += base::UTF16ToUTF8(encoded_terms); 1393 new_params += base::UTF16ToUTF8(encoded_terms);
1394 new_params += old_params.substr(search_terms_position.end()); 1394 new_params += old_params.substr(search_terms_position.end());
1395 GURL::Replacements replacements; 1395 GURL::Replacements replacements;
1396 1396
1397 if (search_term_component == url::Parsed::QUERY) { 1397 if (search_term_component == url::Parsed::QUERY) {
1398 replacements.SetQueryStr(new_params); 1398 replacements.SetQueryStr(new_params);
1399 } else if (search_term_component == url::Parsed::REF) { 1399 } else if (search_term_component == url::Parsed::REF) {
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
1531 // patterns. This means that given patterns 1531 // patterns. This means that given patterns
1532 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ], 1532 // [ "http://foo/#q={searchTerms}", "http://foo/?q={searchTerms}" ],
1533 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would 1533 // calling ExtractSearchTermsFromURL() on "http://foo/?q=bar#q=' would
1534 // 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
1535 // are invalid. 1535 // are invalid.
1536 return !search_terms->empty(); 1536 return !search_terms->empty();
1537 } 1537 }
1538 } 1538 }
1539 return false; 1539 return false;
1540 } 1540 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698