Chromium Code Reviews| Index: chrome/browser/search_engines/template_url.cc |
| diff --git a/chrome/browser/search_engines/template_url.cc b/chrome/browser/search_engines/template_url.cc |
| index ad96e0e24ed19d7c099a93a3ff2513adce567214..3c8d54240c7b1157470189ac3ab4ec2e18590797 100644 |
| --- a/chrome/browser/search_engines/template_url.cc |
| +++ b/chrome/browser/search_engines/template_url.cc |
| @@ -11,6 +11,7 @@ |
| #include "base/logging.h" |
| #include "base/metrics/field_trial.h" |
| #include "base/string_number_conversions.h" |
| +#include "base/string_split.h" |
| #include "base/string_util.h" |
| #include "base/stringprintf.h" |
| #include "base/utf_string_conversions.h" |
| @@ -121,11 +122,27 @@ TemplateURLRef::SearchTermsArgs::SearchTermsArgs(const string16& search_terms) |
| TemplateURLRef::TemplateURLRef(TemplateURL* owner, Type type) |
| : owner_(owner), |
| type_(type), |
| + index_in_owner_(-1), |
| parsed_(false), |
| valid_(false), |
| supports_replacements_(false), |
| + search_term_key_location_(url_parse::Parsed::QUERY), |
| prepopulated_(false) { |
| DCHECK(owner_); |
| + DCHECK(type_ != INDEXED); |
| +} |
| + |
| +TemplateURLRef::TemplateURLRef(TemplateURL* owner, size_t index_in_owner) |
| + : owner_(owner), |
| + type_(INDEXED), |
| + index_in_owner_(index_in_owner), |
| + parsed_(false), |
| + valid_(false), |
| + supports_replacements_(false), |
| + search_term_key_location_(url_parse::Parsed::QUERY), |
| + prepopulated_(false) { |
| + DCHECK(owner_); |
| + DCHECK(index_in_owner_ >= 0L && index_in_owner_ < owner_->URLCount()); |
| } |
| TemplateURLRef::~TemplateURLRef() { |
| @@ -136,7 +153,10 @@ std::string TemplateURLRef::GetURL() const { |
| case SEARCH: return owner_->url(); |
| case SUGGEST: return owner_->suggestions_url(); |
| case INSTANT: return owner_->instant_url(); |
| - default: NOTREACHED(); return std::string(); |
| + case INDEXED: return owner_->GetURL(index_in_owner_); |
| + default: |
| + NOTREACHED(); |
| + return std::string(); |
| } |
| } |
| @@ -402,6 +422,59 @@ bool TemplateURLRef::HasGoogleBaseURLs() const { |
| return false; |
| } |
| +string16 TemplateURLRef::ExtractSearchTermsFromURL(const GURL& url) const { |
| + ParseIfNecessary(); |
| + |
| + // We need a search term in the template URL to extract something. |
| + if (search_term_key_.empty()) |
| + return string16(); |
| + |
| + // TODO(beaudoin): Support {Anything} parameter to act as a path wildcard. |
| + // See crbug/139176 |
| + |
| + // Fill-in the replacements. We don't care about search terms in the pattern, |
| + // so we use the empty string. |
| + GURL pattern(ReplaceSearchTerms(SearchTermsArgs(string16()))); |
| + // Scheme, host, path and port must match. |
| + if (!url.SchemeIs(pattern.scheme().c_str()) || |
| + url.port() != pattern.port() || |
| + url.host() != host_ || |
| + url.path() != path_) |
|
dominich
2012/09/12 22:23:12
nit: add braces here
beaudoin
2012/09/13 18:16:45
Done.
|
| + return string16(); |
| + |
| + // Parameter must be present either in the query or the ref. |
| + std::string params; |
| + switch (search_term_key_location_) { |
| + case url_parse::Parsed::QUERY: |
| + params = url.query(); |
| + break; |
| + case url_parse::Parsed::REF: |
| + params = url.ref(); |
| + break; |
| + default: |
| + NOTREACHED(); |
| + return string16(); |
| + } |
| + |
| + url_parse::Component query, key, value; |
| + query.len = static_cast<int>(params.size()); |
| + while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key, |
| + &value)) { |
| + if (key.is_nonempty() && value.is_nonempty()) { |
| + if (params.substr(key.begin, key.len) == search_term_key_) { |
| + // Extract the search term. |
| + return net::UnescapeAndDecodeUTF8URLComponent( |
| + params.substr(value.begin, value.len), |
| + net::UnescapeRule::SPACES | |
| + net::UnescapeRule::URL_SPECIAL_CHARS | |
| + net::UnescapeRule::REPLACE_PLUS_WITH_SPACE, |
| + NULL); |
| + } |
| + } |
| + } |
| + return string16(); |
| +} |
| + |
| void TemplateURLRef::InvalidateCachedValues() const { |
| supports_replacements_ = valid_ = parsed_ = false; |
| host_.clear(); |
| @@ -551,27 +624,44 @@ void TemplateURLRef::ParseHostAndSearchTermKey( |
| kGoogleBaseSuggestURLParameterFull, |
| search_terms_data.GoogleBaseSuggestURLValue()); |
| + search_term_key_.clear(); |
| + host_.clear(); |
| + path_.clear(); |
| + search_term_key_location_ = url_parse::Parsed::REF; |
| + |
| GURL url(url_string); |
| if (!url.is_valid()) |
| return; |
| - std::string query_string = url.query(); |
| - if (query_string.empty()) |
| - return; |
| + // We want to prioritize search terms in the ref rather than ones in the |
| + // query. |
| + if (!url.ref().empty()) |
| + FindSearchTermsKey(url.ref()); |
| + // If not found in ref string, look for them in query. |
| + if (search_term_key_.empty() && !url.query().empty()) { |
| + search_term_key_location_ = url_parse::Parsed::QUERY; |
| + FindSearchTermsKey(url.query()); |
| + } |
| + |
| + if (!search_term_key_.empty()) { |
| + host_ = url.host(); |
| + path_ = url.path(); |
| + } |
| +} |
| + |
| +void TemplateURLRef::FindSearchTermsKey(const std::string& params) const { |
| url_parse::Component query, key, value; |
| - query.len = static_cast<int>(query_string.size()); |
| - while (url_parse::ExtractQueryKeyValue(query_string.c_str(), &query, &key, |
| + query.len = static_cast<int>(params.size()); |
| + while (url_parse::ExtractQueryKeyValue(params.c_str(), &query, &key, |
| &value)) { |
| if (key.is_nonempty() && value.is_nonempty()) { |
| - std::string value_string = query_string.substr(value.begin, value.len); |
| + std::string value_string = params.substr(value.begin, value.len); |
| if (value_string.find(kSearchTermsParameterFull, 0) != |
| std::string::npos || |
| value_string.find(kGoogleUnescapedSearchTermsParameterFull, 0) != |
| std::string::npos) { |
| - search_term_key_ = query_string.substr(key.begin, key.len); |
| - host_ = url.host(); |
| - path_ = url.path(); |
| + search_term_key_ = params.substr(key.begin, key.len); |
| break; |
| } |
| } |
| @@ -611,6 +701,22 @@ void TemplateURLData::SetURL(const std::string& url) { |
| url_ = url; |
| } |
| +std::string TemplateURLData::SerializeAlternateURLs() const { |
| + std::string result; |
| + for (size_t i = 0; i < alternate_urls_.size(); ++i) { |
| + // Sanity check that the URL doesn't contain a comma. |
| + DCHECK(alternate_urls_[i].find(',') == std::string::npos); |
| + if (result.length() != 0) |
| + result.append(","); |
| + result.append(alternate_urls_[i]); |
| + } |
| + return result; |
| +} |
| + |
| +void TemplateURLData::DeserializeAndSetAlternateURLs( |
| + const std::string& alternate_urls) { |
| + base::SplitString(alternate_urls, ',', &alternate_urls_); |
| +} |
| // TemplateURL ---------------------------------------------------------------- |
| @@ -685,6 +791,43 @@ bool TemplateURL::IsExtensionKeyword() const { |
| return GURL(data_.url()).SchemeIs(chrome::kExtensionScheme); |
| } |
| +size_t TemplateURL::URLCount() const { |
| + DCHECK(!url().empty()); |
| + |
| + int count = 1; // At least one for url(). |
| + if (!instant_url().empty()) |
| + count++; |
| + count += data_.alternate_urls().size(); |
| + return count; |
| +} |
| + |
| +const std::string& TemplateURL::GetURL(size_t index) const { |
| + DCHECK(!url().empty()); |
| + DCHECK(index >= 0); |
| + |
| + if (index < data_.alternate_urls().size()) |
| + return data_.alternate_urls()[index]; |
| + |
| + index -= data_.alternate_urls().size(); |
| + if (!instant_url().empty()) { |
| + if (index == 0) |
| + return instant_url(); |
| + index--; |
| + } |
| + DCHECK(index == 0); |
| + return url(); |
| +} |
| + |
| +string16 TemplateURL::ExtractSearchTermsFromURL(const GURL& url) { |
| + for (size_t i = 0; i < URLCount(); ++i) { |
| + TemplateURLRef ref(this, i); |
| + string16 result(ref.ExtractSearchTermsFromURL(url)); |
| + if (!result.empty()) |
| + return result; |
| + } |
| + return string16(); |
| +} |
| + |
| void TemplateURL::CopyFrom(const TemplateURL& other) { |
| if (this == &other) |
| return; |