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