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