Chromium Code Reviews| Index: components/search_engines/template_url.cc |
| diff --git a/components/search_engines/template_url.cc b/components/search_engines/template_url.cc |
| index 2a1cb2109533d7cead63dc5a25e9faf54a088da7..fb3f7542256957180d134dbf442acdf02129cc1c 100644 |
| --- a/components/search_engines/template_url.cc |
| +++ b/components/search_engines/template_url.cc |
| @@ -141,17 +141,32 @@ SearchTermsKeyResult FindSearchTermsKey(const std::string& params) { |
| return result; |
| } |
| +struct SearchTermsInPathResult { |
| + std::string value_prefix; |
| + std::string value_suffix; |
| + bool search_terms_found; |
| + SearchTermsInPathResult() : search_terms_found(false) {} |
| + bool found() const { return search_terms_found; } |
| +}; |
| + |
| // Extract the position of the search terms' parameter in the URL path. |
| -bool FindSearchTermsInPath(const std::string& path, |
| - url::Component* parameter_position) { |
| - DCHECK(parameter_position); |
| - parameter_position->reset(); |
| - const size_t begin = path.find(kSearchTermsParameterFullEscaped); |
| - if (begin == std::string::npos) |
| - return false; |
| - parameter_position->begin = begin; |
| - parameter_position->len = arraysize(kSearchTermsParameterFullEscaped) - 1; |
| - return true; |
| +SearchTermsInPathResult FindSearchTermsInPath(const base::StringPiece& path) { |
|
Vitaly Baranov
2016/05/12 15:24:12
Changed for consistency with https://codereview.ch
|
| + DCHECK(path.starts_with("/")); |
| + SearchTermsInPathResult result; |
| + const base::StringPiece search_terms_parameter( |
| + kSearchTermsParameterFullEscaped); |
| + const size_t search_terms_pos = path.find(search_terms_parameter); |
| + result.search_terms_found = (search_terms_pos != std::string::npos); |
| + if (result.search_terms_found) { |
| + path.substr(0, search_terms_pos).CopyToString(&result.value_prefix); |
| + path.substr(search_terms_pos + search_terms_parameter.length()). |
| + CopyToString(&result.value_suffix); |
| + } else { |
| + path.CopyToString(&result.value_prefix); |
| + } |
| + DCHECK(base::StartsWith(result.value_prefix, "/", |
| + base::CompareCase::SENSITIVE)); |
| + return result; |
| } |
| bool IsTemplateParameterString(const std::string& param) { |
| @@ -240,7 +255,6 @@ TemplateURLRef::TemplateURLRef(const TemplateURL* owner, Type type) |
| parsed_(false), |
| valid_(false), |
| supports_replacements_(false), |
| - search_term_position_in_path_(std::string::npos), |
| search_term_key_location_(url::Parsed::QUERY), |
| prepopulated_(false) { |
| DCHECK(owner_); |
| @@ -254,7 +268,6 @@ TemplateURLRef::TemplateURLRef(const TemplateURL* owner, size_t index_in_owner) |
| parsed_(false), |
| valid_(false), |
| supports_replacements_(false), |
| - search_term_position_in_path_(std::string::npos), |
| search_term_key_location_(url::Parsed::QUERY), |
| prepopulated_(false) { |
| DCHECK(owner_); |
| @@ -421,16 +434,22 @@ const std::string& TemplateURLRef::GetSearchTermKey( |
| return search_term_key_; |
| } |
| -size_t TemplateURLRef::GetSearchTermPositionInPath( |
| +url::Parsed::ComponentType TemplateURLRef::GetSearchTermKeyLocation( |
| const SearchTermsData& search_terms_data) const { |
| ParseIfNecessary(search_terms_data); |
| - return search_term_position_in_path_; |
| + return search_term_key_location_; |
| } |
| -url::Parsed::ComponentType TemplateURLRef::GetSearchTermKeyLocation( |
| +const std::string& TemplateURLRef::GetSearchTermValuePrefix( |
| const SearchTermsData& search_terms_data) const { |
| ParseIfNecessary(search_terms_data); |
| - return search_term_key_location_; |
| + return search_term_value_prefix_; |
| +} |
| + |
| +const std::string& TemplateURLRef::GetSearchTermValueSuffix( |
| + const SearchTermsData& search_terms_data) const { |
| + ParseIfNecessary(search_terms_data); |
| + return search_term_value_suffix_; |
| } |
|
Vitaly Baranov
2016/05/12 15:24:12
With these two function we can check the prefix an
|
| base::string16 TemplateURLRef::SearchTermToString16( |
| @@ -495,8 +514,8 @@ bool TemplateURLRef::ExtractSearchTermsFromURL( |
| // Host, port, and path must match. |
| if ((url.host() != host_) || |
| (url.port() != port_) || |
| - ((url.path() != path_) && |
| - (search_term_key_location_ != url::Parsed::PATH))) { |
| + ((search_term_key_location_ != url::Parsed::PATH) && |
| + (url.path() != path_))) { |
|
Vitaly Baranov
2016/05/12 15:24:12
Optimization.
|
| return false; |
| } |
| @@ -505,14 +524,7 @@ bool TemplateURLRef::ExtractSearchTermsFromURL( |
| if (search_term_key_location_ == url::Parsed::PATH) { |
| source = url.path(); |
| - |
| - // Characters in the path before and after search terms must match. |
| - if (source.length() < path_.length()) |
| - return false; |
| - position.begin = search_term_position_in_path_; |
| - position.len = source.length() - path_.length(); |
| - if (source.substr(0, position.begin) + source.substr(position.end()) != |
| - path_) |
| + if (!ExtractSearchTermsFromPath(source, &position)) |
| return false; |
| } else { |
| DCHECK(search_term_key_location_ == url::Parsed::QUERY || |
| @@ -559,14 +571,37 @@ bool TemplateURLRef::ExtractSearchTermsFromURL( |
| return true; |
| } |
| +bool TemplateURLRef::ExtractSearchTermsFromPath( |
| + const std::string& path, |
| + url::Component* search_terms_position) const { |
| + if (search_term_key_location_ != url::Parsed::PATH) |
| + return false; |
| + if (path.length() < search_term_value_prefix_.length() + |
| + search_term_value_suffix_.length()) |
| + return false; |
| + if (!base::StartsWith(path, search_term_value_prefix_, |
| + base::CompareCase::SENSITIVE)) |
| + return false; |
| + const size_t search_terms_pos = search_term_value_prefix_.length(); |
| + size_t search_terms_end = std::string::npos; |
| + if (!base::EndsWith(path, search_term_value_suffix_, |
| + base::CompareCase::SENSITIVE)) |
| + return false; |
| + search_terms_end = path.length() - search_term_value_suffix_.length(); |
| + DCHECK_NE(std::string::npos, search_terms_end); |
| + *search_terms_position = url::MakeRange(search_terms_pos, search_terms_end); |
| + return true; |
| +} |
| + |
| void TemplateURLRef::InvalidateCachedValues() const { |
| supports_replacements_ = valid_ = parsed_ = false; |
| host_.clear(); |
| port_.clear(); |
| path_.clear(); |
| search_term_key_.clear(); |
| - search_term_position_in_path_ = std::string::npos; |
| search_term_key_location_ = url::Parsed::QUERY; |
| + search_term_value_prefix_.clear(); |
| + search_term_value_suffix_.clear(); |
|
Vitaly Baranov
2016/05/12 15:24:12
Minor fix.
|
| replacements_.clear(); |
| post_params_.clear(); |
| } |
| @@ -819,16 +854,16 @@ void TemplateURLRef::ParseHostAndSearchTermKey( |
| auto query_result = FindSearchTermsKey(url.query()); |
| auto ref_result = FindSearchTermsKey(url.ref()); |
| - url::Component parameter_position; |
| + auto path_result = FindSearchTermsInPath(url.path()); |
| const bool in_query = query_result.found(); |
| const bool in_ref = ref_result.found(); |
| - const bool in_path = FindSearchTermsInPath(url.path(), ¶meter_position); |
| + const bool in_path = path_result.found(); |
| if (in_query ? (in_ref || in_path) : (in_ref == in_path)) |
| return; // No key or multiple keys found. We only handle having one key. |
| host_ = url.host(); |
| port_ = url.port(); |
| - path_ = url.path(); |
| + path_ = path_result.value_prefix + path_result.value_suffix; |
| if (in_query) { |
| search_term_key_ = query_result.key; |
| search_term_key_location_ = url::Parsed::QUERY; |
| @@ -841,11 +876,9 @@ void TemplateURLRef::ParseHostAndSearchTermKey( |
| search_term_value_suffix_ = ref_result.value_suffix; |
| } else { |
| DCHECK(in_path); |
| - DCHECK_GE(parameter_position.begin, 1); // Path must start with '/'. |
| search_term_key_location_ = url::Parsed::PATH; |
| - search_term_position_in_path_ = parameter_position.begin; |
| - // Remove the "{searchTerms}" itself from |path_|. |
| - path_.erase(parameter_position.begin, parameter_position.len); |
| + search_term_value_prefix_ = path_result.value_prefix; |
| + search_term_value_suffix_ = path_result.value_suffix; |
| } |
| } |