| 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" |
| 11 #include "base/strings/string16.h" | 11 #include "base/strings/string16.h" |
| 12 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "chrome/browser/browser_process.h" | 16 #include "chrome/browser/browser_process.h" |
| 17 #include "chrome/browser/google/google_url_tracker.h" | 17 #include "chrome/browser/google/google_url_tracker.h" |
| 18 #include "chrome/common/chrome_switches.h" | 18 #include "chrome/common/chrome_switches.h" |
| 19 #include "chrome/installer/util/google_update_settings.h" | 19 #include "chrome/installer/util/google_update_settings.h" |
| 20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 21 #include "net/base/url_util.h" | 21 #include "net/base/url_util.h" |
| 22 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 23 | 23 |
| 24 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
| 25 #include "chrome/browser/mac/keystone_glue.h" | 25 #include "chrome/browser/mac/keystone_glue.h" |
| 26 #elif defined(OS_CHROMEOS) | 26 #elif defined(OS_CHROMEOS) |
| 27 #include "chrome/browser/google/google_util_chromeos.h" | 27 #include "chrome/browser/google/google_util_chromeos.h" |
| 28 #endif | 28 #endif |
| 29 | 29 |
| 30 // Only use Link Doctor on official builds. It uses an API key, too, but | |
| 31 // seems best to just disable it, for more responsive error pages and to reduce | |
| 32 // server load. | |
| 33 #if defined(GOOGLE_CHROME_BUILD) | 30 #if defined(GOOGLE_CHROME_BUILD) |
| 34 #define LINKDOCTOR_SERVER_REQUEST_URL "https://www.googleapis.com/rpc" | 31 #include "chrome/browser/google/linkdoctor_internal/linkdoctor_internal.h" |
| 35 #else | 32 #endif |
| 36 #define LINKDOCTOR_SERVER_REQUEST_URL "" | 33 |
| 34 #ifndef LINKDOCTOR_SERVER_REQUEST_URL |
| 35 #define LINKDOCTOR_SERVER_REQUEST_URL std::string() |
| 37 #endif | 36 #endif |
| 38 | 37 |
| 39 | 38 |
| 40 // Helpers -------------------------------------------------------------------- | 39 // Helpers -------------------------------------------------------------------- |
| 41 | 40 |
| 42 namespace { | 41 namespace { |
| 43 | 42 |
| 44 const char* brand_for_testing = NULL; | 43 const char* brand_for_testing = NULL; |
| 45 bool gUseMockLinkDoctorBaseURLForTesting = false; | 44 bool gUseMockLinkDoctorBaseURLForTesting = false; |
| 46 | 45 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 68 GURL LinkDoctorBaseURL() { | 67 GURL LinkDoctorBaseURL() { |
| 69 if (gUseMockLinkDoctorBaseURLForTesting) | 68 if (gUseMockLinkDoctorBaseURLForTesting) |
| 70 return GURL("http://mock.linkdoctor.url/for?testing"); | 69 return GURL("http://mock.linkdoctor.url/for?testing"); |
| 71 return GURL(LINKDOCTOR_SERVER_REQUEST_URL); | 70 return GURL(LINKDOCTOR_SERVER_REQUEST_URL); |
| 72 } | 71 } |
| 73 | 72 |
| 74 void SetMockLinkDoctorBaseURLForTesting() { | 73 void SetMockLinkDoctorBaseURLForTesting() { |
| 75 gUseMockLinkDoctorBaseURLForTesting = true; | 74 gUseMockLinkDoctorBaseURLForTesting = true; |
| 76 } | 75 } |
| 77 | 76 |
| 78 std::string GetGoogleLocale() { | 77 GURL AppendGoogleLocaleParam(const GURL& url) { |
| 79 std::string locale = g_browser_process->GetApplicationLocale(); | |
| 80 // 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 |
| 81 // 'no' for that. | 79 // 'no' for that. |
| 80 std::string locale = g_browser_process->GetApplicationLocale(); |
| 82 if (locale == "nb") | 81 if (locale == "nb") |
| 83 return "no"; | 82 locale = "no"; |
| 84 return locale; | 83 return net::AppendQueryParameter(url, "hl", locale); |
| 85 } | |
| 86 | |
| 87 GURL AppendGoogleLocaleParam(const GURL& url) { | |
| 88 return net::AppendQueryParameter(url, "hl", GetGoogleLocale()); | |
| 89 } | 84 } |
| 90 | 85 |
| 91 std::string StringAppendGoogleLocaleParam(const std::string& url) { | 86 std::string StringAppendGoogleLocaleParam(const std::string& url) { |
| 92 GURL original_url(url); | 87 GURL original_url(url); |
| 93 DCHECK(original_url.is_valid()); | 88 DCHECK(original_url.is_valid()); |
| 94 GURL localized_url = AppendGoogleLocaleParam(original_url); | 89 GURL localized_url = AppendGoogleLocaleParam(original_url); |
| 95 return localized_url.spec(); | 90 return localized_url.spec(); |
| 96 } | 91 } |
| 97 | 92 |
| 98 std::string GetGoogleCountryCode(Profile* profile) { | 93 GURL AppendGoogleTLDParam(Profile* profile, const GURL& url) { |
| 99 const std::string google_hostname = | 94 const std::string google_domain( |
| 100 GoogleURLTracker::GoogleURL(profile).host(); | 95 net::registry_controlled_domains::GetDomainAndRegistry( |
| 101 const size_t last_dot = google_hostname.find_last_of('.'); | 96 GoogleURLTracker::GoogleURL(profile), |
| 102 if (last_dot == std::string::npos) { | 97 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); |
| 98 const size_t first_dot = google_domain.find('.'); |
| 99 if (first_dot == std::string::npos) { |
| 103 NOTREACHED(); | 100 NOTREACHED(); |
| 101 return url; |
| 104 } | 102 } |
| 105 std::string country_code = google_hostname.substr(last_dot + 1); | 103 return net::AppendQueryParameter(url, "sd", |
| 106 // Assume the com TLD implies the US. | 104 google_domain.substr(first_dot + 1)); |
| 107 if (country_code == "com") | |
| 108 return "us"; | |
| 109 // Google uses the Unicode Common Locale Data Repository (CLDR), and the CLDR | |
| 110 // code for the UK is "gb". | |
| 111 if (country_code == "uk") | |
| 112 return "gb"; | |
| 113 // Catalonia does not have a CLDR country code, since it's a region in Spain, | |
| 114 // so use Spain instead. | |
| 115 if (country_code == "cat") | |
| 116 return "es"; | |
| 117 return country_code; | |
| 118 } | |
| 119 | |
| 120 GURL GetGoogleSearchURL(Profile* profile) { | |
| 121 // The url returned by the tracker does not include the "/search" or the | |
| 122 // "q=" query string. | |
| 123 std::string search_path = "search"; | |
| 124 std::string query_string = "q="; | |
| 125 GURL::Replacements replacements; | |
| 126 replacements.SetPathStr(search_path); | |
| 127 replacements.SetQueryStr(query_string); | |
| 128 return GoogleURLTracker::GoogleURL(profile).ReplaceComponents(replacements); | |
| 129 } | 105 } |
| 130 | 106 |
| 131 #if defined(OS_WIN) | 107 #if defined(OS_WIN) |
| 132 | 108 |
| 133 bool GetBrand(std::string* brand) { | 109 bool GetBrand(std::string* brand) { |
| 134 if (brand_for_testing) { | 110 if (brand_for_testing) { |
| 135 brand->assign(brand_for_testing); | 111 brand->assign(brand_for_testing); |
| 136 return true; | 112 return true; |
| 137 } | 113 } |
| 138 | 114 |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 310 DCHECK(brand_for_testing == NULL); | 286 DCHECK(brand_for_testing == NULL); |
| 311 brand_for_testing = brand_.c_str(); | 287 brand_for_testing = brand_.c_str(); |
| 312 } | 288 } |
| 313 | 289 |
| 314 BrandForTesting::~BrandForTesting() { | 290 BrandForTesting::~BrandForTesting() { |
| 315 brand_for_testing = NULL; | 291 brand_for_testing = NULL; |
| 316 } | 292 } |
| 317 | 293 |
| 318 | 294 |
| 319 } // namespace google_util | 295 } // namespace google_util |
| OLD | NEW |