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