Home | History | Annotate | Download | only in google
      1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include "chrome/browser/google/google_util.h"
      6 
      7 #include <string>
      8 
      9 #include "base/string_util.h"
     10 #include "chrome/browser/browser_process.h"
     11 #include "chrome/browser/google/google_url_tracker.h"
     12 #include "googleurl/src/gurl.h"
     13 #include "net/base/registry_controlled_domain.h"
     14 
     15 namespace {
     16 
     17 // A helper method for adding a query param to |url|.
     18 GURL AppendParam(const GURL& url,
     19                  const std::string& param_name,
     20                  const std::string& param_value) {
     21   std::string query(url.query());
     22   if (!query.empty())
     23     query += "&";
     24   query += param_name + "=" + param_value;
     25   GURL::Replacements repl;
     26   repl.SetQueryStr(query);
     27   return url.ReplaceComponents(repl);
     28 }
     29 
     30 }  // anonymous namespace
     31 
     32 namespace google_util {
     33 
     34 const char kLinkDoctorBaseURL[] =
     35     "http://linkhelp.clients.google.com/tbproxy/lh/fixurl";
     36 
     37 GURL AppendGoogleLocaleParam(const GURL& url) {
     38   // Google does not yet recognize 'nb' for Norwegian Bokmal, but it uses
     39   // 'no' for that.
     40   std::string locale = g_browser_process->GetApplicationLocale();
     41   if (locale == "nb")
     42     locale = "no";
     43   return AppendParam(url, "hl", locale);
     44 }
     45 
     46 std::string StringAppendGoogleLocaleParam(const std::string& url) {
     47   GURL original_url(url);
     48   DCHECK(original_url.is_valid());
     49   GURL localized_url = AppendGoogleLocaleParam(original_url);
     50   return localized_url.spec();
     51 }
     52 
     53 GURL AppendGoogleTLDParam(const GURL& url) {
     54   const std::string google_domain(
     55       net::RegistryControlledDomainService::GetDomainAndRegistry(
     56           GoogleURLTracker::GoogleURL()));
     57   const size_t first_dot = google_domain.find('.');
     58   if (first_dot == std::string::npos) {
     59     NOTREACHED();
     60     return url;
     61   }
     62   return AppendParam(url, "sd", google_domain.substr(first_dot + 1));
     63 }
     64 
     65 }  // namespace google_util
     66