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. | |
34 #if defined(GOOGLE_CHROME_BUILD) | 31 #if defined(GOOGLE_CHROME_BUILD) |
35 #define LINKDOCTOR_SERVER_REQUEST_URL "https://www.googleapis.com/rpc" | 32 #include "chrome/browser/google/linkdoctor_internal/linkdoctor_internal.h" |
36 #else | 33 #endif |
37 #define LINKDOCTOR_SERVER_REQUEST_URL "" | 34 |
| 35 #ifndef LINKDOCTOR_SERVER_REQUEST_URL |
| 36 #define LINKDOCTOR_SERVER_REQUEST_URL std::string() |
38 #endif | 37 #endif |
39 | 38 |
40 | 39 |
41 // Helpers -------------------------------------------------------------------- | 40 // Helpers -------------------------------------------------------------------- |
42 | 41 |
43 namespace { | 42 namespace { |
44 | 43 |
45 const char* brand_for_testing = NULL; | 44 const char* brand_for_testing = NULL; |
46 bool gUseMockLinkDoctorBaseURLForTesting = false; | 45 bool gUseMockLinkDoctorBaseURLForTesting = false; |
47 | 46 |
(...skipping 21 matching lines...) Expand all Loading... |
69 GURL LinkDoctorBaseURL() { | 68 GURL LinkDoctorBaseURL() { |
70 if (gUseMockLinkDoctorBaseURLForTesting) | 69 if (gUseMockLinkDoctorBaseURLForTesting) |
71 return GURL("http://mock.linkdoctor.url/for?testing"); | 70 return GURL("http://mock.linkdoctor.url/for?testing"); |
72 return GURL(LINKDOCTOR_SERVER_REQUEST_URL); | 71 return GURL(LINKDOCTOR_SERVER_REQUEST_URL); |
73 } | 72 } |
74 | 73 |
75 void SetMockLinkDoctorBaseURLForTesting() { | 74 void SetMockLinkDoctorBaseURLForTesting() { |
76 gUseMockLinkDoctorBaseURLForTesting = true; | 75 gUseMockLinkDoctorBaseURLForTesting = true; |
77 } | 76 } |
78 | 77 |
79 std::string GetGoogleLocale() { | 78 GURL AppendGoogleLocaleParam(const GURL& url) { |
80 std::string locale = g_browser_process->GetApplicationLocale(); | |
81 // Google does not yet recognize 'nb' for Norwegian Bokmal, but it uses | 79 // Google does not yet recognize 'nb' for Norwegian Bokmal, but it uses |
82 // 'no' for that. | 80 // 'no' for that. |
| 81 std::string locale = g_browser_process->GetApplicationLocale(); |
83 if (locale == "nb") | 82 if (locale == "nb") |
84 return "no"; | 83 locale = "no"; |
85 return locale; | 84 return net::AppendQueryParameter(url, "hl", locale); |
86 } | |
87 | |
88 GURL AppendGoogleLocaleParam(const GURL& url) { | |
89 return net::AppendQueryParameter(url, "hl", GetGoogleLocale()); | |
90 } | 85 } |
91 | 86 |
92 std::string StringAppendGoogleLocaleParam(const std::string& url) { | 87 std::string StringAppendGoogleLocaleParam(const std::string& url) { |
93 GURL original_url(url); | 88 GURL original_url(url); |
94 DCHECK(original_url.is_valid()); | 89 DCHECK(original_url.is_valid()); |
95 GURL localized_url = AppendGoogleLocaleParam(original_url); | 90 GURL localized_url = AppendGoogleLocaleParam(original_url); |
96 return localized_url.spec(); | 91 return localized_url.spec(); |
97 } | 92 } |
98 | 93 |
99 std::string GetGoogleCountryCode(Profile* profile) { | 94 GURL AppendGoogleTLDParam(Profile* profile, const GURL& url) { |
100 const std::string google_hostname = | 95 const std::string google_domain( |
101 GoogleURLTracker::GoogleURL(profile).host(); | 96 net::registry_controlled_domains::GetDomainAndRegistry( |
102 const size_t last_dot = google_hostname.find_last_of('.'); | 97 GoogleURLTracker::GoogleURL(profile), |
103 if (last_dot == std::string::npos) { | 98 net::registry_controlled_domains::EXCLUDE_PRIVATE_REGISTRIES)); |
| 99 const size_t first_dot = google_domain.find('.'); |
| 100 if (first_dot == std::string::npos) { |
104 NOTREACHED(); | 101 NOTREACHED(); |
| 102 return url; |
105 } | 103 } |
106 std::string country_code = google_hostname.substr(last_dot + 1); | 104 return net::AppendQueryParameter(url, "sd", |
107 // Assume the com TLD implies the US. | 105 google_domain.substr(first_dot + 1)); |
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); | |
130 } | 106 } |
131 | 107 |
132 #if defined(OS_WIN) | 108 #if defined(OS_WIN) |
133 | 109 |
134 bool GetBrand(std::string* brand) { | 110 bool GetBrand(std::string* brand) { |
135 if (brand_for_testing) { | 111 if (brand_for_testing) { |
136 brand->assign(brand_for_testing); | 112 brand->assign(brand_for_testing); |
137 return true; | 113 return true; |
138 } | 114 } |
139 | 115 |
(...skipping 175 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 DCHECK(brand_for_testing == NULL); | 291 DCHECK(brand_for_testing == NULL); |
316 brand_for_testing = brand_.c_str(); | 292 brand_for_testing = brand_.c_str(); |
317 } | 293 } |
318 | 294 |
319 BrandForTesting::~BrandForTesting() { | 295 BrandForTesting::~BrandForTesting() { |
320 brand_for_testing = NULL; | 296 brand_for_testing = NULL; |
321 } | 297 } |
322 | 298 |
323 | 299 |
324 } // namespace google_util | 300 } // namespace google_util |
OLD | NEW |