Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / google / google_util.h
1 // Copyright (c) 2012 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 CHROME_BROWSER_GOOGLE_GOOGLE_UTIL_H__
8 #define CHROME_BROWSER_GOOGLE_GOOGLE_UTIL_H__
9
10 #include <string>
11
12 #include "base/basictypes.h"
13
14 class GURL;
15 class Profile;
16
17 // This namespace provides various helpers around handling Google-related URLs
18 // and state relating to Google Chrome distributions (such as RLZ).
19 namespace google_util {
20
21 // True iff |str| contains a "q=" query parameter with a non-empty value.
22 // |str| should be a query or a hash fragment, without the ? or # (as
23 // returned by GURL::query() or GURL::ref().
24 bool HasGoogleSearchQueryParam(const std::string& str);
25
26 // The query key that identifies a Google Extended API request for Instant.
27 const char kInstantExtendedAPIParam[] = "espv";
28
29 GURL LinkDoctorBaseURL();
30 void SetMockLinkDoctorBaseURLForTesting();
31
32 // Returns the Google locale.  This is the same string as
33 // AppendGoogleLocaleParam adds to the URL, only without the leading "hl".
34 std::string GetGoogleLocale();
35
36 // Adds the Google locale string to the URL (e.g., hl=en-US).  This does not
37 // check to see if the param already exists.
38 GURL AppendGoogleLocaleParam(const GURL& url);
39
40 // String version of AppendGoogleLocaleParam.
41 std::string StringAppendGoogleLocaleParam(const std::string& url);
42
43 // Returns the Google country code string for the given profile.
44 std::string GetGoogleCountryCode(Profile* profile);
45
46 // Returns the Google search URL for the given profile.
47 GURL GetGoogleSearchURL(Profile* profile);
48
49 // Returns in |brand| the brand code or distribution tag that has been
50 // assigned to a partner. Returns false if the information is not available.
51 bool GetBrand(std::string* brand);
52
53 // Returns in |brand| the reactivation brand code or distribution tag
54 // that has been assigned to a partner for reactivating a dormant chrome
55 // install. Returns false if the information is not available.
56 bool GetReactivationBrand(std::string* brand);
57
58 // Returns the Google base URL specified on the command line, if it exists.
59 // This performs some fixup and sanity-checking to ensure that the resulting URL
60 // is valid and has no query or ref.
61 GURL CommandLineGoogleBaseURL();
62
63 // Returns true if a Google base URL was specified on the command line and |url|
64 // begins with that base URL.  This uses a simple string equality check.
65 bool StartsWithCommandLineGoogleBaseURL(const GURL& url);
66
67 // WARNING: The following IsGoogleXXX() functions use heuristics to rule out
68 // "obviously false" answers.  They do NOT guarantee that the string in question
69 // is actually on a Google-owned domain, just that it looks plausible.  If you
70 // need to restrict some behavior to only happen on Google's officially-owned
71 // domains, use TransportSecurityState::IsGooglePinnedProperty() instead.
72
73 // Designate whether or not a URL checking function also checks for specific
74 // subdomains, or only "www" and empty subdomains.
75 enum SubdomainPermission {
76   ALLOW_SUBDOMAIN,
77   DISALLOW_SUBDOMAIN,
78 };
79
80 // Designate whether or not a URL checking function also checks for standard
81 // ports (80 for http, 443 for https), or if it allows all port numbers.
82 enum PortPermission {
83   ALLOW_NON_STANDARD_PORTS,
84   DISALLOW_NON_STANDARD_PORTS,
85 };
86
87 // True if |host| is "[www.]google.<TLD>" with a valid TLD. If
88 // |subdomain_permission| is ALLOW_SUBDOMAIN, we check against host
89 // "*.google.<TLD>" instead.
90 //
91 // If the Google base URL has been overridden on the command line, this function
92 // will also return true for any URL whose hostname exactly matches the hostname
93 // of the URL specified on the command line.  In this case,
94 // |subdomain_permission| is ignored.
95 bool IsGoogleHostname(const std::string& host,
96                       SubdomainPermission subdomain_permission);
97
98 // True if |url| is a valid URL with a host that returns true for
99 // IsGoogleHostname(), and an HTTP or HTTPS scheme.  If |port_permission| is
100 // DISALLOW_NON_STANDARD_PORTS, this also requires |url| to use the standard
101 // port for its scheme (80 for HTTP, 443 for HTTPS).
102 //
103 // Note that this only checks for google.<TLD> domains, but not other Google
104 // properties. There is code in variations_http_header_provider.cc that checks
105 // for additional Google properties, which can be moved here if more callers
106 // are interested in this in the future.
107 bool IsGoogleDomainUrl(const GURL& url,
108                        SubdomainPermission subdomain_permission,
109                        PortPermission port_permission);
110
111 // True if |url| represents a valid Google home page URL.
112 bool IsGoogleHomePageUrl(const GURL& url);
113
114 // True if |url| represents a valid Google search URL.
115 bool IsGoogleSearchUrl(const GURL& url);
116
117 // True if a build is strictly organic, according to its brand code.
118 bool IsOrganic(const std::string& brand);
119
120 // True if a build should run as organic during first run. This uses
121 // a slightly different set of brand codes from the standard IsOrganic
122 // method.
123 bool IsOrganicFirstRun(const std::string& brand);
124
125 // True if |brand| is an internet cafe brand code.
126 bool IsInternetCafeBrandCode(const std::string& brand);
127
128 // This class is meant to be used only from test code, and sets the brand
129 // code returned by the function GetBrand() above while the object exists.
130 class BrandForTesting {
131  public:
132   explicit BrandForTesting(const std::string& brand);
133   ~BrandForTesting();
134
135  private:
136   std::string brand_;
137   DISALLOW_COPY_AND_ASSIGN(BrandForTesting);
138 };
139
140 }  // namespace google_util
141
142 #endif  // CHROME_BROWSER_GOOGLE_GOOGLE_UTIL_H__