search_terms_data.cc revision bda42a81ee5f9b20d2bebedcf0bbef1e30e5b293
1// Copyright (c) 2010 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/chrome_thread.h"
10#include "chrome/browser/google/google_url_tracker.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
18SearchTermsData::SearchTermsData() {
19}
20
21SearchTermsData::~SearchTermsData() {
22}
23
24std::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
52std::string* UIThreadSearchTermsData::google_base_url_ = NULL;
53
54UIThreadSearchTermsData::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(!ChromeThread::IsWellKnownThread(ChromeThread::UI) ||
58         ChromeThread::CurrentlyOn(ChromeThread::UI));
59}
60
61std::string UIThreadSearchTermsData::GoogleBaseURLValue() const {
62  DCHECK(!ChromeThread::IsWellKnownThread(ChromeThread::UI) ||
63         ChromeThread::CurrentlyOn(ChromeThread::UI));
64  return google_base_url_ ?
65    (*google_base_url_) : GoogleURLTracker::GoogleURL().spec();
66}
67
68std::string UIThreadSearchTermsData::GetApplicationLocale() const {
69  DCHECK(!ChromeThread::IsWellKnownThread(ChromeThread::UI) ||
70         ChromeThread::CurrentlyOn(ChromeThread::UI));
71  return g_browser_process->GetApplicationLocale();
72}
73
74#if defined(OS_WIN) && defined(GOOGLE_CHROME_BUILD)
75std::wstring UIThreadSearchTermsData::GetRlzParameterValue() const {
76  DCHECK(!ChromeThread::IsWellKnownThread(ChromeThread::UI) ||
77         ChromeThread::CurrentlyOn(ChromeThread::UI));
78  std::wstring rlz_string;
79  // For organic brandcodes do not use rlz at all. Empty brandcode usually
80  // means a chromium install. This is ok.
81  std::wstring brand;
82  if (GoogleUpdateSettings::GetBrand(&brand) && !brand.empty() &&
83      !GoogleUpdateSettings::IsOrganic(brand))
84    RLZTracker::GetAccessPointRlz(rlz_lib::CHROME_OMNIBOX, &rlz_string);
85  return rlz_string;
86}
87#endif
88
89// static
90void UIThreadSearchTermsData::SetGoogleBaseURL(std::string* google_base_url) {
91  delete google_base_url_;
92  google_base_url_ = google_base_url;
93}
94