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
3// found in the LICENSE file.
4//
5// Some Google related utility functions.
6
7#ifndef COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_UTIL_H__
8#define COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_UTIL_H__
9
10#include <string>
11
12#include "base/basictypes.h"
13
14class GURL;
15
16// This namespace provides various helpers around handling Google-related URLs.
17namespace google_util {
18
19// True iff |str| contains a "q=" query parameter with a non-empty value.
20// |str| should be a query or a hash fragment, without the ? or # (as
21// returned by GURL::query() or GURL::ref().
22bool HasGoogleSearchQueryParam(const std::string& str);
23
24// The query key that identifies a Google Extended API request for Instant.
25const char kInstantExtendedAPIParam[] = "espv";
26
27GURL LinkDoctorBaseURL();
28void SetMockLinkDoctorBaseURLForTesting();
29
30// Returns the Google locale corresponding to |application_locale|.  This is
31// the same string as AppendGoogleLocaleParam adds to the URL, only without the
32// leading "hl".
33std::string GetGoogleLocale(const std::string& application_locale);
34
35// Adds the Google locale string to the URL (e.g., hl=en-US).  This does not
36// check to see if the param already exists.
37GURL AppendGoogleLocaleParam(const GURL& url,
38                             const std::string& application_locale);
39
40// Returns the Google country code string for the given Google homepage URL.
41std::string GetGoogleCountryCode(GURL google_homepage_url);
42
43// Returns the Google search URL for the given Google homepage URL.
44GURL GetGoogleSearchURL(GURL google_homepage_url);
45
46// Returns the Google base URL specified on the command line, if it exists.
47// This performs some fixup and sanity-checking to ensure that the resulting URL
48// is valid and has no query or ref.
49GURL CommandLineGoogleBaseURL();
50
51// Returns true if a Google base URL was specified on the command line and |url|
52// begins with that base URL.  This uses a simple string equality check.
53bool StartsWithCommandLineGoogleBaseURL(const GURL& url);
54
55// WARNING: The following IsGoogleXXX() functions use heuristics to rule out
56// "obviously false" answers.  They do NOT guarantee that the string in question
57// is actually on a Google-owned domain, just that it looks plausible.  If you
58// need to restrict some behavior to only happen on Google's officially-owned
59// domains, use TransportSecurityState::IsGooglePinnedProperty() instead.
60
61// Designate whether or not a URL checking function also checks for specific
62// subdomains, or only "www" and empty subdomains.
63enum SubdomainPermission {
64  ALLOW_SUBDOMAIN,
65  DISALLOW_SUBDOMAIN,
66};
67
68// Designate whether or not a URL checking function also checks for standard
69// ports (80 for http, 443 for https), or if it allows all port numbers.
70enum PortPermission {
71  ALLOW_NON_STANDARD_PORTS,
72  DISALLOW_NON_STANDARD_PORTS,
73};
74
75// True if |host| is "[www.]google.<TLD>" with a valid TLD. If
76// |subdomain_permission| is ALLOW_SUBDOMAIN, we check against host
77// "*.google.<TLD>" instead.
78//
79// If the Google base URL has been overridden on the command line, this function
80// will also return true for any URL whose hostname exactly matches the hostname
81// of the URL specified on the command line.  In this case,
82// |subdomain_permission| is ignored.
83bool IsGoogleHostname(const std::string& host,
84                      SubdomainPermission subdomain_permission);
85
86// True if |url| is a valid URL with a host that returns true for
87// IsGoogleHostname(), and an HTTP or HTTPS scheme.  If |port_permission| is
88// DISALLOW_NON_STANDARD_PORTS, this also requires |url| to use the standard
89// port for its scheme (80 for HTTP, 443 for HTTPS).
90//
91// Note that this only checks for google.<TLD> domains, but not other Google
92// properties. There is code in variations_http_header_provider.cc that checks
93// for additional Google properties, which can be moved here if more callers
94// are interested in this in the future.
95bool IsGoogleDomainUrl(const GURL& url,
96                       SubdomainPermission subdomain_permission,
97                       PortPermission port_permission);
98
99// True if |url| represents a valid Google home page URL.
100bool IsGoogleHomePageUrl(const GURL& url);
101
102// True if |url| represents a valid Google search URL.
103bool IsGoogleSearchUrl(const GURL& url);
104
105// True if |url| is a valid youtube.<TLD> URL.  If |port_permission| is
106// DISALLOW_NON_STANDARD_PORTS, this also requires |url| to use the standard
107// port for its scheme (80 for HTTP, 443 for HTTPS).
108bool IsYoutubeDomainUrl(const GURL& url,
109                        SubdomainPermission subdomain_permission,
110                        PortPermission port_permission);
111
112}  // namespace google_util
113
114#endif  // COMPONENTS_GOOGLE_CORE_BROWSER_GOOGLE_UTIL_H__
115