Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: chrome/browser/google/google_util.cc

Issue 18119005: Misc. cleanup: (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « chrome/browser/google/google_util.h ('k') | chrome/browser/google/google_util_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/google/google_util.h" 5 #include "chrome/browser/google/google_util.h"
6 6
7 #include <string> 7 #include <string>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/command_line.h" 10 #include "base/command_line.h"
(...skipping 17 matching lines...) Expand all
28 #endif 28 #endif
29 29
30 #if defined(GOOGLE_CHROME_BUILD) 30 #if defined(GOOGLE_CHROME_BUILD)
31 #include "chrome/browser/google/linkdoctor_internal/linkdoctor_internal.h" 31 #include "chrome/browser/google/linkdoctor_internal/linkdoctor_internal.h"
32 #endif 32 #endif
33 33
34 #ifndef LINKDOCTOR_SERVER_REQUEST_URL 34 #ifndef LINKDOCTOR_SERVER_REQUEST_URL
35 #define LINKDOCTOR_SERVER_REQUEST_URL std::string() 35 #define LINKDOCTOR_SERVER_REQUEST_URL std::string()
36 #endif 36 #endif
37 37
38
39 // Helpers --------------------------------------------------------------------
40
38 namespace { 41 namespace {
39 42
40 const char* brand_for_testing = NULL; 43 const char* brand_for_testing = NULL;
41
42 bool gUseMockLinkDoctorBaseURLForTesting = false; 44 bool gUseMockLinkDoctorBaseURLForTesting = false;
43 45
44 } // anonymous namespace 46 bool IsPathHomePageBase(const std::string& path) {
47 return (path == "/") || (path == "/webhp");
48 }
49
50 } // namespace
51
45 52
46 namespace google_util { 53 namespace google_util {
47 54
55 // Global functions -----------------------------------------------------------
56
48 bool HasGoogleSearchQueryParam(const std::string& str) { 57 bool HasGoogleSearchQueryParam(const std::string& str) {
49 url_parse::Component query(0, str.length()), key, value; 58 url_parse::Component query(0, str.length()), key, value;
50 while (url_parse::ExtractQueryKeyValue(str.c_str(), &query, &key, 59 while (url_parse::ExtractQueryKeyValue(str.c_str(), &query, &key,
51 &value)) { 60 &value)) {
52 if ((key.len == 1) && (str[key.begin] == 'q') && value.is_nonempty()) 61 if ((key.len == 1) && (str[key.begin] == 'q') && value.is_nonempty())
53 return true; 62 return true;
54 } 63 }
55 return false; 64 return false;
56 } 65 }
57 66
58 GURL LinkDoctorBaseURL() { 67 GURL LinkDoctorBaseURL() {
59 if (gUseMockLinkDoctorBaseURLForTesting) 68 if (gUseMockLinkDoctorBaseURLForTesting)
60 return GURL("http://mock.linkdoctor.url/for?testing"); 69 return GURL("http://mock.linkdoctor.url/for?testing");
61 return GURL(LINKDOCTOR_SERVER_REQUEST_URL); 70 return GURL(LINKDOCTOR_SERVER_REQUEST_URL);
62 } 71 }
63 72
64 void SetMockLinkDoctorBaseURLForTesting() { 73 void SetMockLinkDoctorBaseURLForTesting() {
65 gUseMockLinkDoctorBaseURLForTesting = true; 74 gUseMockLinkDoctorBaseURLForTesting = true;
66 } 75 }
67 76
68 BrandForTesting::BrandForTesting(const std::string& brand) : brand_(brand) {
69 DCHECK(brand_for_testing == NULL);
70 brand_for_testing = brand_.c_str();
71 }
72
73 BrandForTesting::~BrandForTesting() {
74 brand_for_testing = NULL;
75 }
76
77 GURL AppendGoogleLocaleParam(const GURL& url) { 77 GURL AppendGoogleLocaleParam(const GURL& url) {
78 // Google does not yet recognize 'nb' for Norwegian Bokmal, but it uses 78 // Google does not yet recognize 'nb' for Norwegian Bokmal, but it uses
79 // 'no' for that. 79 // 'no' for that.
80 std::string locale = g_browser_process->GetApplicationLocale(); 80 std::string locale = g_browser_process->GetApplicationLocale();
81 if (locale == "nb") 81 if (locale == "nb")
82 locale = "no"; 82 locale = "no";
83 return net::AppendQueryParameter(url, "hl", locale); 83 return net::AppendQueryParameter(url, "hl", locale);
84 } 84 }
85 85
86 std::string StringAppendGoogleLocaleParam(const std::string& url) { 86 std::string StringAppendGoogleLocaleParam(const std::string& url) {
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
145 return true; 145 return true;
146 } 146 }
147 147
148 bool GetReactivationBrand(std::string* brand) { 148 bool GetReactivationBrand(std::string* brand) {
149 brand->clear(); 149 brand->clear();
150 return true; 150 return true;
151 } 151 }
152 152
153 #endif 153 #endif
154 154
155 bool IsGoogleDomainUrl(const GURL& url,
156 SubdomainPermission subdomain_permission,
157 PortPermission port_permission) {
158 return url.is_valid() && (url.SchemeIs("http") || url.SchemeIs("https")) &&
159 (url.port().empty() || (port_permission == ALLOW_NON_STANDARD_PORTS)) &&
160 google_util::IsGoogleHostname(url.host(), subdomain_permission);
161 }
162
163 bool IsGoogleHostname(const std::string& host, 155 bool IsGoogleHostname(const std::string& host,
164 SubdomainPermission subdomain_permission) { 156 SubdomainPermission subdomain_permission) {
165 size_t tld_length = net::registry_controlled_domains::GetRegistryLength( 157 size_t tld_length = net::registry_controlled_domains::GetRegistryLength(
166 host, 158 host,
167 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES, 159 net::registry_controlled_domains::EXCLUDE_UNKNOWN_REGISTRIES,
168 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES); 160 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES);
169 if ((tld_length == 0) || (tld_length == std::string::npos)) 161 if ((tld_length == 0) || (tld_length == std::string::npos))
170 return false; 162 return false;
171 std::string host_minus_tld(host, 0, host.length() - tld_length); 163 std::string host_minus_tld(host, 0, host.length() - tld_length);
172 if (LowerCaseEqualsASCII(host_minus_tld, "google.")) 164 if (LowerCaseEqualsASCII(host_minus_tld, "google."))
173 return true; 165 return true;
174 if (subdomain_permission == ALLOW_SUBDOMAIN) 166 if (subdomain_permission == ALLOW_SUBDOMAIN)
175 return EndsWith(host_minus_tld, ".google.", false); 167 return EndsWith(host_minus_tld, ".google.", false);
176 return LowerCaseEqualsASCII(host_minus_tld, "www.google."); 168 return LowerCaseEqualsASCII(host_minus_tld, "www.google.");
177 } 169 }
178 170
171 bool IsGoogleDomainUrl(const GURL& url,
172 SubdomainPermission subdomain_permission,
173 PortPermission port_permission) {
174 return url.is_valid() && (url.SchemeIs("http") || url.SchemeIs("https")) &&
175 (url.port().empty() || (port_permission == ALLOW_NON_STANDARD_PORTS)) &&
176 google_util::IsGoogleHostname(url.host(), subdomain_permission);
177 }
178
179 bool IsGoogleHomePageUrl(const GURL& url) { 179 bool IsGoogleHomePageUrl(const GURL& url) {
180 // First check to see if this has a Google domain. 180 // First check to see if this has a Google domain.
181 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) 181 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS))
182 return false; 182 return false;
183 183
184 // Make sure the path is a known home page path. 184 // Make sure the path is a known home page path.
185 std::string path(url.path()); 185 std::string path(url.path());
186 if (path != "/" && path != "/webhp" && 186 return IsPathHomePageBase(path) || StartsWithASCII(path, "/ig", false);
187 !StartsWithASCII(path, "/ig", false)) {
188 return false;
189 }
190
191 return true;
192 } 187 }
193 188
194 bool IsGoogleSearchUrl(const GURL& url) { 189 bool IsGoogleSearchUrl(const GURL& url) {
195 // First check to see if this has a Google domain. 190 // First check to see if this has a Google domain.
196 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) 191 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS))
197 return false; 192 return false;
198 193
199 // Make sure the path is a known search path. 194 // Make sure the path is a known search path.
200 std::string path(url.path()); 195 std::string path(url.path());
201 bool has_valid_path = false; 196 bool is_home_page_base = IsPathHomePageBase(path);
202 bool is_home_page_base = false; 197 if (!is_home_page_base && (path != "/search"))
203 if (path == "/search") {
204 has_valid_path = true;
205 } else if (path == "/webhp" || path == "/") {
206 // Note that we allow both "/" and "" paths, but GURL spits them
207 // both out as just "/".
208 has_valid_path = true;
209 is_home_page_base = true;
210 }
211 if (!has_valid_path)
212 return false; 198 return false;
213 199
214 // Check for query parameter in URL parameter and hash fragment, depending on 200 // Check for query parameter in URL parameter and hash fragment, depending on
215 // the path type. 201 // the path type.
216 std::string query(url.query()); 202 return HasGoogleSearchQueryParam(url.ref()) ||
217 std::string ref(url.ref()); 203 (!is_home_page_base && HasGoogleSearchQueryParam(url.query()));
218 return HasGoogleSearchQueryParam(ref) ||
219 (!is_home_page_base && HasGoogleSearchQueryParam(query));
220 } 204 }
221 205
222 bool IsOrganic(const std::string& brand) { 206 bool IsOrganic(const std::string& brand) {
223 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 207 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
224 if (command_line.HasSwitch(switches::kOrganicInstall)) 208 if (command_line.HasSwitch(switches::kOrganicInstall))
225 return true; 209 return true;
226 210
227 #if defined(OS_MACOSX) 211 #if defined(OS_MACOSX)
228 if (brand.empty()) { 212 if (brand.empty()) {
229 // An empty brand string on Mac is used for channels other than stable, 213 // An empty brand string on Mac is used for channels other than stable,
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
273 bool IsInternetCafeBrandCode(const std::string& brand) { 257 bool IsInternetCafeBrandCode(const std::string& brand) {
274 const char* const kBrands[] = { 258 const char* const kBrands[] = {
275 "CHIQ", "CHSG", "HLJY", "NTMO", "OOBA", "OOBB", "OOBC", "OOBD", "OOBE", 259 "CHIQ", "CHSG", "HLJY", "NTMO", "OOBA", "OOBB", "OOBC", "OOBD", "OOBE",
276 "OOBF", "OOBG", "OOBH", "OOBI", "OOBJ", "IDCM", 260 "OOBF", "OOBG", "OOBH", "OOBI", "OOBJ", "IDCM",
277 }; 261 };
278 const char* const* end = &kBrands[arraysize(kBrands)]; 262 const char* const* end = &kBrands[arraysize(kBrands)];
279 const char* const* found = std::find(&kBrands[0], end, brand); 263 const char* const* found = std::find(&kBrands[0], end, brand);
280 return found != end; 264 return found != end;
281 } 265 }
282 266
267
268 // BrandForTesting ------------------------------------------------------------
269
270 BrandForTesting::BrandForTesting(const std::string& brand) : brand_(brand) {
271 DCHECK(brand_for_testing == NULL);
272 brand_for_testing = brand_.c_str();
273 }
274
275 BrandForTesting::~BrandForTesting() {
276 brand_for_testing = NULL;
277 }
278
279
283 } // namespace google_util 280 } // namespace google_util
OLDNEW
« no previous file with comments | « chrome/browser/google/google_util.h ('k') | chrome/browser/google/google_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698