| 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/net/url_fixer_upper.h" | 18 #include "chrome/common/net/url_fixer_upper.h" |
| 19 #include "chrome/installer/util/google_update_settings.h" | |
| 20 #include "components/google/core/browser/google_switches.h" | 19 #include "components/google/core/browser/google_switches.h" |
| 21 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" | 20 #include "net/base/registry_controlled_domains/registry_controlled_domain.h" |
| 22 #include "net/base/url_util.h" | 21 #include "net/base/url_util.h" |
| 23 #include "url/gurl.h" | 22 #include "url/gurl.h" |
| 24 | 23 |
| 25 #if defined(OS_MACOSX) | |
| 26 #include "chrome/browser/mac/keystone_glue.h" | |
| 27 #elif defined(OS_CHROMEOS) | |
| 28 #include "chrome/browser/google/google_util_chromeos.h" | |
| 29 #endif | |
| 30 | |
| 31 // Only use Link Doctor on official builds. It uses an API key, too, but | 24 // 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 | 25 // seems best to just disable it, for more responsive error pages and to reduce |
| 33 // server load. | 26 // server load. |
| 34 #if defined(GOOGLE_CHROME_BUILD) | 27 #if defined(GOOGLE_CHROME_BUILD) |
| 35 #define LINKDOCTOR_SERVER_REQUEST_URL "https://www.googleapis.com/rpc" | 28 #define LINKDOCTOR_SERVER_REQUEST_URL "https://www.googleapis.com/rpc" |
| 36 #else | 29 #else |
| 37 #define LINKDOCTOR_SERVER_REQUEST_URL "" | 30 #define LINKDOCTOR_SERVER_REQUEST_URL "" |
| 38 #endif | 31 #endif |
| 39 | 32 |
| 40 | 33 |
| 41 // Helpers -------------------------------------------------------------------- | 34 // Helpers -------------------------------------------------------------------- |
| 42 | 35 |
| 43 namespace { | 36 namespace { |
| 44 | 37 |
| 45 const char* brand_for_testing = NULL; | |
| 46 bool gUseMockLinkDoctorBaseURLForTesting = false; | 38 bool gUseMockLinkDoctorBaseURLForTesting = false; |
| 47 | 39 |
| 48 bool IsPathHomePageBase(const std::string& path) { | 40 bool IsPathHomePageBase(const std::string& path) { |
| 49 return (path == "/") || (path == "/webhp"); | 41 return (path == "/") || (path == "/webhp"); |
| 50 } | 42 } |
| 51 | 43 |
| 52 } // namespace | 44 } // namespace |
| 53 | 45 |
| 54 | 46 |
| 55 namespace google_util { | 47 namespace google_util { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 // To transform the homepage URL into the corresponding search URL, add the | 112 // To transform the homepage URL into the corresponding search URL, add the |
| 121 // "search" and the "q=" query string. | 113 // "search" and the "q=" query string. |
| 122 std::string search_path = "search"; | 114 std::string search_path = "search"; |
| 123 std::string query_string = "q="; | 115 std::string query_string = "q="; |
| 124 GURL::Replacements replacements; | 116 GURL::Replacements replacements; |
| 125 replacements.SetPathStr(search_path); | 117 replacements.SetPathStr(search_path); |
| 126 replacements.SetQueryStr(query_string); | 118 replacements.SetQueryStr(query_string); |
| 127 return google_homepage_url.ReplaceComponents(replacements); | 119 return google_homepage_url.ReplaceComponents(replacements); |
| 128 } | 120 } |
| 129 | 121 |
| 130 #if defined(OS_WIN) | |
| 131 | |
| 132 bool GetBrand(std::string* brand) { | |
| 133 if (brand_for_testing) { | |
| 134 brand->assign(brand_for_testing); | |
| 135 return true; | |
| 136 } | |
| 137 | |
| 138 base::string16 brand16; | |
| 139 bool ret = GoogleUpdateSettings::GetBrand(&brand16); | |
| 140 if (ret) | |
| 141 brand->assign(base::UTF16ToASCII(brand16)); | |
| 142 return ret; | |
| 143 } | |
| 144 | |
| 145 bool GetReactivationBrand(std::string* brand) { | |
| 146 base::string16 brand16; | |
| 147 bool ret = GoogleUpdateSettings::GetReactivationBrand(&brand16); | |
| 148 if (ret) | |
| 149 brand->assign(base::UTF16ToASCII(brand16)); | |
| 150 return ret; | |
| 151 } | |
| 152 | |
| 153 #else | |
| 154 | |
| 155 bool GetBrand(std::string* brand) { | |
| 156 if (brand_for_testing) { | |
| 157 brand->assign(brand_for_testing); | |
| 158 return true; | |
| 159 } | |
| 160 | |
| 161 #if defined(OS_MACOSX) | |
| 162 brand->assign(keystone_glue::BrandCode()); | |
| 163 #elif defined(OS_CHROMEOS) | |
| 164 brand->assign(google_util::chromeos::GetBrand()); | |
| 165 #else | |
| 166 brand->clear(); | |
| 167 #endif | |
| 168 return true; | |
| 169 } | |
| 170 | |
| 171 bool GetReactivationBrand(std::string* brand) { | |
| 172 brand->clear(); | |
| 173 return true; | |
| 174 } | |
| 175 | |
| 176 #endif | |
| 177 | |
| 178 GURL CommandLineGoogleBaseURL() { | 122 GURL CommandLineGoogleBaseURL() { |
| 179 // Unit tests may add command-line flags after the first call to this | 123 // Unit tests may add command-line flags after the first call to this |
| 180 // function, so we don't simply initialize a static |base_url| directly and | 124 // function, so we don't simply initialize a static |base_url| directly and |
| 181 // then unconditionally return it. | 125 // then unconditionally return it. |
| 182 CR_DEFINE_STATIC_LOCAL(std::string, switch_value, ()); | 126 CR_DEFINE_STATIC_LOCAL(std::string, switch_value, ()); |
| 183 CR_DEFINE_STATIC_LOCAL(GURL, base_url, ()); | 127 CR_DEFINE_STATIC_LOCAL(GURL, base_url, ()); |
| 184 std::string current_switch_value( | 128 std::string current_switch_value( |
| 185 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 129 CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 186 switches::kGoogleBaseURL)); | 130 switches::kGoogleBaseURL)); |
| 187 if (current_switch_value != switch_value) { | 131 if (current_switch_value != switch_value) { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 247 bool is_home_page_base = IsPathHomePageBase(path); | 191 bool is_home_page_base = IsPathHomePageBase(path); |
| 248 if (!is_home_page_base && (path != "/search")) | 192 if (!is_home_page_base && (path != "/search")) |
| 249 return false; | 193 return false; |
| 250 | 194 |
| 251 // Check for query parameter in URL parameter and hash fragment, depending on | 195 // Check for query parameter in URL parameter and hash fragment, depending on |
| 252 // the path type. | 196 // the path type. |
| 253 return HasGoogleSearchQueryParam(url.ref()) || | 197 return HasGoogleSearchQueryParam(url.ref()) || |
| 254 (!is_home_page_base && HasGoogleSearchQueryParam(url.query())); | 198 (!is_home_page_base && HasGoogleSearchQueryParam(url.query())); |
| 255 } | 199 } |
| 256 | 200 |
| 257 bool IsOrganic(const std::string& brand) { | |
| 258 #if defined(OS_MACOSX) | |
| 259 if (brand.empty()) { | |
| 260 // An empty brand string on Mac is used for channels other than stable, | |
| 261 // which are always organic. | |
| 262 return true; | |
| 263 } | |
| 264 #endif | |
| 265 | |
| 266 const char* const kBrands[] = { | |
| 267 "CHCA", "CHCB", "CHCG", "CHCH", "CHCI", "CHCJ", "CHCK", "CHCL", | |
| 268 "CHFO", "CHFT", "CHHS", "CHHM", "CHMA", "CHMB", "CHME", "CHMF", | |
| 269 "CHMG", "CHMH", "CHMI", "CHMQ", "CHMV", "CHNB", "CHNC", "CHNG", | |
| 270 "CHNH", "CHNI", "CHOA", "CHOB", "CHOC", "CHON", "CHOO", "CHOP", | |
| 271 "CHOQ", "CHOR", "CHOS", "CHOT", "CHOU", "CHOX", "CHOY", "CHOZ", | |
| 272 "CHPD", "CHPE", "CHPF", "CHPG", "ECBA", "ECBB", "ECDA", "ECDB", | |
| 273 "ECSA", "ECSB", "ECVA", "ECVB", "ECWA", "ECWB", "ECWC", "ECWD", | |
| 274 "ECWE", "ECWF", "EUBB", "EUBC", "GGLA", "GGLS" | |
| 275 }; | |
| 276 const char* const* end = &kBrands[arraysize(kBrands)]; | |
| 277 const char* const* found = std::find(&kBrands[0], end, brand); | |
| 278 if (found != end) | |
| 279 return true; | |
| 280 | |
| 281 return StartsWithASCII(brand, "EUB", true) || | |
| 282 StartsWithASCII(brand, "EUC", true) || | |
| 283 StartsWithASCII(brand, "GGR", true); | |
| 284 } | |
| 285 | |
| 286 bool IsOrganicFirstRun(const std::string& brand) { | |
| 287 #if defined(OS_MACOSX) | |
| 288 if (brand.empty()) { | |
| 289 // An empty brand string on Mac is used for channels other than stable, | |
| 290 // which are always organic. | |
| 291 return true; | |
| 292 } | |
| 293 #endif | |
| 294 | |
| 295 return StartsWithASCII(brand, "GG", true) || | |
| 296 StartsWithASCII(brand, "EU", true); | |
| 297 } | |
| 298 | |
| 299 bool IsInternetCafeBrandCode(const std::string& brand) { | |
| 300 const char* const kBrands[] = { | |
| 301 "CHIQ", "CHSG", "HLJY", "NTMO", "OOBA", "OOBB", "OOBC", "OOBD", "OOBE", | |
| 302 "OOBF", "OOBG", "OOBH", "OOBI", "OOBJ", "IDCM", | |
| 303 }; | |
| 304 const char* const* end = &kBrands[arraysize(kBrands)]; | |
| 305 const char* const* found = std::find(&kBrands[0], end, brand); | |
| 306 return found != end; | |
| 307 } | |
| 308 | |
| 309 | |
| 310 // BrandForTesting ------------------------------------------------------------ | |
| 311 | |
| 312 BrandForTesting::BrandForTesting(const std::string& brand) : brand_(brand) { | |
| 313 DCHECK(brand_for_testing == NULL); | |
| 314 brand_for_testing = brand_.c_str(); | |
| 315 } | |
| 316 | |
| 317 BrandForTesting::~BrandForTesting() { | |
| 318 brand_for_testing = NULL; | |
| 319 } | |
| 320 | |
| 321 | |
| 322 } // namespace google_util | 201 } // namespace google_util |
| OLD | NEW |