diff options
author | brettw <brettw@chromium.org> | 2016-01-22 16:46:38 -0800 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2016-01-23 00:48:00 +0000 |
commit | b65cd5c1a3b0d3fa5700d808afeb111d2f0ef2d0 (patch) | |
tree | 8a8616add122edddfb82cc8fcedb865e31fffc77 /components/google/core/browser/google_util.cc | |
parent | b47b47617273a91db8fd74c357a756cd189a4342 (diff) | |
download | chromium_src-b65cd5c1a3b0d3fa5700d808afeb111d2f0ef2d0.zip chromium_src-b65cd5c1a3b0d3fa5700d808afeb111d2f0ef2d0.tar.gz chromium_src-b65cd5c1a3b0d3fa5700d808afeb111d2f0ef2d0.tar.bz2 |
Convert some URL accessors to use StringPieces
Updates some google util, registry controlled domain, and net util code to
use string pieces in their APIs.
Convert some google util functions to take a const ref GURL instead of a copy.
Review URL: https://codereview.chromium.org/1614223003
Cr-Commit-Position: refs/heads/master@{#371101}
Diffstat (limited to 'components/google/core/browser/google_util.cc')
-rw-r--r-- | components/google/core/browser/google_util.cc | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/components/google/core/browser/google_util.cc b/components/google/core/browser/google_util.cc index 581363f..fc253a3 100644 --- a/components/google/core/browser/google_util.cc +++ b/components/google/core/browser/google_util.cc @@ -39,15 +39,15 @@ namespace { bool gUseMockLinkDoctorBaseURLForTesting = false; -bool IsPathHomePageBase(const std::string& path) { +bool IsPathHomePageBase(base::StringPiece path) { return (path == "/") || (path == "/webhp"); } // True if |host| is "[www.]<domain_in_lower_case>.<TLD>" with a valid TLD. If // |subdomain_permission| is ALLOW_SUBDOMAIN, we check against host // "*.<domain_in_lower_case>.<TLD>" instead. -bool IsValidHostName(const std::string& host, - const std::string& domain_in_lower_case, +bool IsValidHostName(base::StringPiece host, + base::StringPiece domain_in_lower_case, google_util::SubdomainPermission subdomain_permission) { size_t tld_length = net::registry_controlled_domains::GetRegistryLength( host, @@ -55,15 +55,23 @@ bool IsValidHostName(const std::string& host, net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); if ((tld_length == 0) || (tld_length == std::string::npos)) return false; + // Removes the tld and the preceding dot. - std::string host_minus_tld(host, 0, host.length() - tld_length - 1); - if (base::LowerCaseEqualsASCII(host_minus_tld, domain_in_lower_case.c_str())) + base::StringPiece host_minus_tld = + host.substr(0, host.length() - tld_length - 1); + if (base::LowerCaseEqualsASCII(host_minus_tld, domain_in_lower_case)) return true; - if (subdomain_permission == google_util::ALLOW_SUBDOMAIN) - return base::EndsWith(host_minus_tld, "." + domain_in_lower_case, + + if (subdomain_permission == google_util::ALLOW_SUBDOMAIN) { + std::string dot_domain("."); + domain_in_lower_case.AppendToString(&dot_domain); + return base::EndsWith(host_minus_tld, dot_domain, base::CompareCase::INSENSITIVE_ASCII); - return base::LowerCaseEqualsASCII(host_minus_tld, - ("www." + domain_in_lower_case).c_str()); + } + + std::string www_domain("www."); + domain_in_lower_case.AppendToString(&www_domain); + return base::LowerCaseEqualsASCII(host_minus_tld, www_domain); } // True if |url| is a valid URL with HTTP or HTTPS scheme. If |port_permission| @@ -82,11 +90,11 @@ namespace google_util { // Global functions ----------------------------------------------------------- -bool HasGoogleSearchQueryParam(const std::string& str) { +bool HasGoogleSearchQueryParam(base::StringPiece str) { url::Component query(0, static_cast<int>(str.length())), key, value; - while (url::ExtractQueryKeyValue(str.c_str(), &query, &key, &value)) { + while (url::ExtractQueryKeyValue(str.data(), &query, &key, &value)) { if (value.is_nonempty()) { - base::StringPiece key_str(&str[key.begin], key.len); + base::StringPiece key_str = str.substr(key.begin, key.len); if (key_str == "q" || key_str == "as_q") return true; } @@ -115,13 +123,13 @@ GURL AppendGoogleLocaleParam(const GURL& url, url, "hl", GetGoogleLocale(application_locale)); } -std::string GetGoogleCountryCode(GURL google_homepage_url) { - const std::string google_hostname = google_homepage_url.host(); +std::string GetGoogleCountryCode(const GURL& google_homepage_url) { + base::StringPiece google_hostname = google_homepage_url.host_piece(); const size_t last_dot = google_hostname.find_last_of('.'); if (last_dot == std::string::npos) { NOTREACHED(); } - std::string country_code = google_hostname.substr(last_dot + 1); + base::StringPiece country_code = google_hostname.substr(last_dot + 1); // Assume the com TLD implies the US. if (country_code == "com") return "us"; @@ -133,10 +141,10 @@ std::string GetGoogleCountryCode(GURL google_homepage_url) { // so use Spain instead. if (country_code == "cat") return "es"; - return country_code; + return country_code.as_string(); } -GURL GetGoogleSearchURL(GURL google_homepage_url) { +GURL GetGoogleSearchURL(const GURL& google_homepage_url) { // To transform the homepage URL into the corresponding search URL, add the // "search" and the "q=" query string. GURL::Replacements replacements; @@ -145,7 +153,7 @@ GURL GetGoogleSearchURL(GURL google_homepage_url) { return google_homepage_url.ReplaceComponents(replacements); } -GURL CommandLineGoogleBaseURL() { +const GURL& CommandLineGoogleBaseURL() { // Unit tests may add command-line flags after the first call to this // function, so we don't simply initialize a static |base_url| directly and // then unconditionally return it. @@ -164,16 +172,16 @@ GURL CommandLineGoogleBaseURL() { } bool StartsWithCommandLineGoogleBaseURL(const GURL& url) { - GURL base_url(CommandLineGoogleBaseURL()); + const GURL& base_url(CommandLineGoogleBaseURL()); return base_url.is_valid() && base::StartsWith(url.possibly_invalid_spec(), base_url.spec(), base::CompareCase::SENSITIVE); } -bool IsGoogleHostname(const std::string& host, +bool IsGoogleHostname(base::StringPiece host, SubdomainPermission subdomain_permission) { - GURL base_url(CommandLineGoogleBaseURL()); - if (base_url.is_valid() && (host == base_url.host())) + const GURL& base_url(CommandLineGoogleBaseURL()); + if (base_url.is_valid() && (host == base_url.host_piece())) return true; return IsValidHostName(host, "google", subdomain_permission); @@ -192,7 +200,7 @@ bool IsGoogleHomePageUrl(const GURL& url) { return false; // Make sure the path is a known home page path. - std::string path(url.path()); + base::StringPiece path(url.path_piece()); return IsPathHomePageBase(path) || base::StartsWith(path, "/ig", base::CompareCase::INSENSITIVE_ASCII); } @@ -203,22 +211,22 @@ bool IsGoogleSearchUrl(const GURL& url) { return false; // Make sure the path is a known search path. - std::string path(url.path()); + base::StringPiece path(url.path_piece()); bool is_home_page_base = IsPathHomePageBase(path); if (!is_home_page_base && (path != "/search")) return false; // Check for query parameter in URL parameter and hash fragment, depending on // the path type. - return HasGoogleSearchQueryParam(url.ref()) || - (!is_home_page_base && HasGoogleSearchQueryParam(url.query())); + return HasGoogleSearchQueryParam(url.ref_piece()) || + (!is_home_page_base && HasGoogleSearchQueryParam(url.query_piece())); } bool IsYoutubeDomainUrl(const GURL& url, SubdomainPermission subdomain_permission, PortPermission port_permission) { return IsValidURL(url, port_permission) && - IsValidHostName(url.host(), "youtube", subdomain_permission); + IsValidHostName(url.host_piece(), "youtube", subdomain_permission); } } // namespace google_util |