Home | History | Annotate | Download | only in search_engines
      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/search_engines/search_terms_data.h"
      6 
      7 #include "base/logging.h"
      8 #include "chrome/browser/browser_process.h"
      9 #include "chrome/browser/google/google_url_tracker.h"
     10 #include "content/browser/browser_thread.h"
     11 #include "googleurl/src/gurl.h"
     12 
     13 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
     14 #include "chrome/browser/rlz/rlz.h"
     15 #include "chrome/installer/util/google_update_settings.h"
     16 #endif
     17 
     18 SearchTermsData::SearchTermsData() {
     19 }
     20 
     21 SearchTermsData::~SearchTermsData() {
     22 }
     23 
     24 std::string SearchTermsData::GoogleBaseSuggestURLValue() const {
     25   // The suggest base URL we want at the end is something like
     26   // "http://clients1.google.TLD/complete/".  The key bit we want from the
     27   // original Google base URL is the TLD.
     28 
     29   // Start with the Google base URL.
     30   const GURL base_url(GoogleBaseURLValue());
     31   DCHECK(base_url.is_valid());
     32 
     33   // Change "www." to "clients1." in the hostname.  If no "www." was found, just
     34   // prepend "clients1.".
     35   const std::string base_host(base_url.host());
     36   GURL::Replacements repl;
     37   const std::string suggest_host("clients1." +
     38       (base_host.compare(0, 4, "www.") ? base_host : base_host.substr(4)));
     39   repl.SetHostStr(suggest_host);
     40 
     41   // Replace any existing path with "/complete/".
     42   static const std::string suggest_path("/complete/");
     43   repl.SetPathStr(suggest_path);
     44 
     45   // Clear the query and ref.
     46   repl.ClearQuery();
     47   repl.ClearRef();
     48   return base_url.ReplaceComponents(repl).spec();
     49 }
     50 
     51 // static
     52 std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
     53 
     54 UIThreadSearchTermsData::UIThreadSearchTermsData() {
     55   // GoogleURLTracker::GoogleURL() DCHECKs this also, but adding it here helps
     56   // us catch bad behavior at a more common place in this code.
     57   DCHECK(!BrowserThread::IsWellKnownThread(BrowserThread::UI) ||
     58          BrowserThread::CurrentlyOn(BrowserThread::UI));
     59 }
     60 
     61 std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
     62   DCHECK(!BrowserThread::IsWellKnownThread(BrowserThread::UI) ||
     63          BrowserThread::CurrentlyOn(BrowserThread::UI));
     64   return google_base_url_ ?
     65     (*google_base_url_) : GoogleURLTracker::GoogleURL().spec();
     66 }
     67 
     68 std::string UIThreadSearchTermsData::GetApplicationLocale() const {
     69   DCHECK(!BrowserThread::IsWellKnownThread(BrowserThread::UI) ||
     70          BrowserThread::CurrentlyOn(BrowserThread::UI));
     71   return g_browser_process->GetApplicationLocale();
     72 }
     73 
     74 #if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
     75 string16 UIThreadSearchTermsData::GetRlzParameterValue() const {
     76   DCHECK(!BrowserThread::IsWellKnownThread(BrowserThread::UI) ||
     77          BrowserThread::CurrentlyOn(BrowserThread::UI));
     78   string16 rlz_string;
     79   // For organic brandcodes do not use rlz at all. Empty brandcode usually
     80   // means a chromium install. This is ok.
     81   string16 brand;
     82   if (GoogleUpdateSettings::GetBrand(&brand) && !brand.empty() &&
     83       !GoogleUpdateSettings::IsOrganic(brand)) {
     84     // This call will return false the first time(s) it is called until the
     85     // value has been cached. This normally would mean that at most one omnibox
     86     // search might not send the RLZ data but this is not really a problem.
     87     RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz_string);
     88   }
     89   return rlz_string;
     90 }
     91 #endif
     92 
     93 // static
     94 void UIThreadSearchTermsData::SetGoogleBaseURL(std::string* google_base_url) {
     95   delete google_base_url_;
     96   google_base_url_ = google_base_url;
     97 }
     98