| OLD | NEW |
| 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 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 // instance, in a regular Google search they'll be in the query but in a | 191 // instance, in a regular Google search they'll be in the query but in a |
| 192 // Google Instant search they can be in both. The ref is the correct one to | 192 // Google Instant search they can be in both. The ref is the correct one to |
| 193 // return in this case, so test the ref component first. | 193 // return in this case, so test the ref component first. |
| 194 if (ExtractSearchTermsFromComponent(url, &parsed_url.ref, &search_terms) || | 194 if (ExtractSearchTermsFromComponent(url, &parsed_url.ref, &search_terms) || |
| 195 ExtractSearchTermsFromComponent(url, &parsed_url.query, &search_terms)) { | 195 ExtractSearchTermsFromComponent(url, &parsed_url.query, &search_terms)) { |
| 196 return search_terms; | 196 return search_terms; |
| 197 } | 197 } |
| 198 return string16(); | 198 return string16(); |
| 199 } | 199 } |
| 200 | 200 |
| 201 bool IsGoogleDomainUrl(const std::string& url, SubdomainPermission permission) { | 201 bool IsGoogleDomainUrl(const std::string& url, |
| 202 SubdomainPermission subdomain_permission, |
| 203 PortPermission port_permission) { |
| 202 GURL original_url(url); | 204 GURL original_url(url); |
| 203 if (!original_url.is_valid() || | 205 if (!original_url.is_valid() || |
| 204 !(original_url.SchemeIs("http") || original_url.SchemeIs("https"))) | 206 !(original_url.SchemeIs("http") || original_url.SchemeIs("https"))) |
| 205 return false; | 207 return false; |
| 206 | 208 |
| 207 // If we have the Instant URL overridden with a command line flag, accept | 209 // If we have the Instant URL overridden with a command line flag, accept |
| 208 // its domain/port combination as well. | 210 // its domain/port combination as well. |
| 209 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 211 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
| 210 if (command_line.HasSwitch(switches::kInstantURL)) { | 212 if (command_line.HasSwitch(switches::kInstantURL)) { |
| 211 GURL custom_instant_url( | 213 GURL custom_instant_url( |
| 212 command_line.GetSwitchValueASCII(switches::kInstantURL)); | 214 command_line.GetSwitchValueASCII(switches::kInstantURL)); |
| 213 if (original_url.host() == custom_instant_url.host() && | 215 if (original_url.host() == custom_instant_url.host() && |
| 214 original_url.port() == custom_instant_url.port()) | 216 original_url.port() == custom_instant_url.port()) |
| 215 return true; | 217 return true; |
| 216 } | 218 } |
| 217 | 219 |
| 218 return original_url.port().empty() && | 220 return (original_url.port().empty() || |
| 219 google_util::IsGoogleHostname(original_url.host(), permission); | 221 port_permission == ALLOW_NON_STANDARD_PORTS) && |
| 222 google_util::IsGoogleHostname(original_url.host(), subdomain_permission); |
| 220 } | 223 } |
| 221 | 224 |
| 222 bool IsGoogleHostname(const std::string& host, | 225 bool IsGoogleHostname(const std::string& host, |
| 223 SubdomainPermission permission) { | 226 SubdomainPermission subdomain_permission) { |
| 224 size_t tld_length = | 227 size_t tld_length = |
| 225 net::RegistryControlledDomainService::GetRegistryLength(host, false); | 228 net::RegistryControlledDomainService::GetRegistryLength(host, false); |
| 226 if ((tld_length == 0) || (tld_length == std::string::npos)) | 229 if ((tld_length == 0) || (tld_length == std::string::npos)) |
| 227 return false; | 230 return false; |
| 228 std::string host_minus_tld(host, 0, host.length() - tld_length); | 231 std::string host_minus_tld(host, 0, host.length() - tld_length); |
| 229 if (LowerCaseEqualsASCII(host_minus_tld, "google.")) | 232 if (LowerCaseEqualsASCII(host_minus_tld, "google.")) |
| 230 return true; | 233 return true; |
| 231 if (permission == ALLOW_SUBDOMAIN) | 234 if (subdomain_permission == ALLOW_SUBDOMAIN) |
| 232 return EndsWith(host_minus_tld, ".google.", false); | 235 return EndsWith(host_minus_tld, ".google.", false); |
| 233 return LowerCaseEqualsASCII(host_minus_tld, "www.google."); | 236 return LowerCaseEqualsASCII(host_minus_tld, "www.google."); |
| 234 } | 237 } |
| 235 | 238 |
| 236 bool IsGoogleHomePageUrl(const std::string& url) { | 239 bool IsGoogleHomePageUrl(const std::string& url) { |
| 237 GURL original_url(url); | 240 GURL original_url(url); |
| 238 | 241 |
| 239 // First check to see if this has a Google domain. | 242 // First check to see if this has a Google domain. |
| 240 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN)) | 243 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) |
| 241 return false; | 244 return false; |
| 242 | 245 |
| 243 // Make sure the path is a known home page path. | 246 // Make sure the path is a known home page path. |
| 244 std::string path(original_url.path()); | 247 std::string path(original_url.path()); |
| 245 if (path != "/" && path != "/webhp" && | 248 if (path != "/" && path != "/webhp" && |
| 246 !StartsWithASCII(path, "/ig", false)) { | 249 !StartsWithASCII(path, "/ig", false)) { |
| 247 return false; | 250 return false; |
| 248 } | 251 } |
| 249 | 252 |
| 250 return true; | 253 return true; |
| 251 } | 254 } |
| 252 | 255 |
| 253 bool IsGoogleSearchUrl(const std::string& url) { | 256 bool IsGoogleSearchUrl(const std::string& url) { |
| 254 GURL original_url(url); | 257 GURL original_url(url); |
| 255 | 258 |
| 256 // First check to see if this has a Google domain. | 259 // First check to see if this has a Google domain. |
| 257 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN)) | 260 if (!IsGoogleDomainUrl(url, DISALLOW_SUBDOMAIN, DISALLOW_NON_STANDARD_PORTS)) |
| 258 return false; | 261 return false; |
| 259 | 262 |
| 260 // Make sure the path is a known search path. | 263 // Make sure the path is a known search path. |
| 261 std::string path(original_url.path()); | 264 std::string path(original_url.path()); |
| 262 bool has_valid_path = false; | 265 bool has_valid_path = false; |
| 263 bool is_home_page_base = false; | 266 bool is_home_page_base = false; |
| 264 if (path == "/search") { | 267 if (path == "/search") { |
| 265 has_valid_path = true; | 268 has_valid_path = true; |
| 266 } else if (path == "/webhp" || path == "/") { | 269 } else if (path == "/webhp" || path == "/") { |
| 267 // Note that we allow both "/" and "" paths, but GURL spits them | 270 // Note that we allow both "/" and "" paths, but GURL spits them |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 358 const char* const kBrands[] = { | 361 const char* const kBrands[] = { |
| 359 "CHIQ", "CHSG", "HLJY", "NTMO", "OOBA", "OOBB", "OOBC", "OOBD", "OOBE", | 362 "CHIQ", "CHSG", "HLJY", "NTMO", "OOBA", "OOBB", "OOBC", "OOBD", "OOBE", |
| 360 "OOBF", "OOBG", "OOBH", "OOBI", "OOBJ", "IDCM", | 363 "OOBF", "OOBG", "OOBH", "OOBI", "OOBJ", "IDCM", |
| 361 }; | 364 }; |
| 362 const char* const* end = &kBrands[arraysize(kBrands)]; | 365 const char* const* end = &kBrands[arraysize(kBrands)]; |
| 363 const char* const* found = std::find(&kBrands[0], end, brand); | 366 const char* const* found = std::find(&kBrands[0], end, brand); |
| 364 return found != end; | 367 return found != end; |
| 365 } | 368 } |
| 366 | 369 |
| 367 } // namespace google_util | 370 } // namespace google_util |
| OLD | NEW |