summaryrefslogtreecommitdiffstats
path: root/chrome/browser/rlz/chrome_rlz_tracker_delegate.cc
blob: 5d5a0308b8a59946b89a1f88250fb8ccfe6f71a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/rlz/chrome_rlz_tracker_delegate.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "base/prefs/pref_service.h"
#include "build/build_config.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/chrome_notification_types.h"
#include "chrome/browser/google/google_brand.h"
#include "chrome/browser/prefs/session_startup_pref.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/search_engines/template_url_service_factory.h"
#include "chrome/browser/ui/startup/startup_browser_creator.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "components/google/core/browser/google_util.h"
#include "components/omnibox/browser/omnibox_log.h"
#include "components/search_engines/template_url.h"
#include "components/search_engines/template_url_service.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/navigation_controller.h"
#include "content/public/browser/navigation_details.h"
#include "content/public/browser/navigation_entry.h"
#include "content/public/browser/notification_details.h"
#include "content/public/browser/notification_service.h"
#include "content/public/browser/notification_source.h"
#include "content/public/common/content_switches.h"

#if defined(OS_WIN)
#include "chrome/installer/util/google_update_settings.h"
#endif

ChromeRLZTrackerDelegate::ChromeRLZTrackerDelegate() {
}

ChromeRLZTrackerDelegate::~ChromeRLZTrackerDelegate() {
}

// static
bool ChromeRLZTrackerDelegate::IsGoogleDefaultSearch(Profile* profile) {
  bool is_google_default_search = false;
  TemplateURLService* template_url_service =
      TemplateURLServiceFactory::GetForProfile(profile);
  if (template_url_service) {
    const TemplateURL* url_template =
        template_url_service->GetDefaultSearchProvider();
    is_google_default_search = url_template &&
                               url_template->url_ref().HasGoogleBaseURLs(
                                   template_url_service->search_terms_data());
  }
  return is_google_default_search;
}

// static
bool ChromeRLZTrackerDelegate::IsGoogleHomepage(Profile* profile) {
  return google_util::IsGoogleHomePageUrl(
      GURL(profile->GetPrefs()->GetString(prefs::kHomePage)));
}

// static
bool ChromeRLZTrackerDelegate::IsGoogleInStartpages(Profile* profile) {
  bool is_google_in_startpages = false;
  SessionStartupPref session_startup_prefs =
      StartupBrowserCreator::GetSessionStartupPref(
          *base::CommandLine::ForCurrentProcess(), profile);
  if (session_startup_prefs.type == SessionStartupPref::URLS) {
    is_google_in_startpages =
        std::count_if(session_startup_prefs.urls.begin(),
                      session_startup_prefs.urls.end(),
                      google_util::IsGoogleHomePageUrl) > 0;
  }
  return is_google_in_startpages;
}

void ChromeRLZTrackerDelegate::Cleanup() {
  registrar_.RemoveAll();
  on_omnibox_search_callback_.Reset();
  on_homepage_search_callback_.Reset();
}

bool ChromeRLZTrackerDelegate::IsOnUIThread() {
  return content::BrowserThread::CurrentlyOn(content::BrowserThread::UI);
}

base::SequencedWorkerPool* ChromeRLZTrackerDelegate::GetBlockingPool() {
  return content::BrowserThread::GetBlockingPool();
}

net::URLRequestContextGetter* ChromeRLZTrackerDelegate::GetRequestContext() {
  return g_browser_process->system_request_context();
}

bool ChromeRLZTrackerDelegate::GetBrand(std::string* brand) {
  return google_brand::GetBrand(brand);
}

bool ChromeRLZTrackerDelegate::IsBrandOrganic(const std::string& brand) {
  return brand.empty() || google_brand::IsOrganic(brand);
}

bool ChromeRLZTrackerDelegate::GetReactivationBrand(std::string* brand) {
  return google_brand::GetReactivationBrand(brand);
}

bool ChromeRLZTrackerDelegate::ShouldEnableZeroDelayForTesting() {
  return base::CommandLine::ForCurrentProcess()->HasSwitch(
      ::switches::kTestType);
}

bool ChromeRLZTrackerDelegate::GetLanguage(base::string16* language) {
#if defined(OS_WIN)
  return GoogleUpdateSettings::GetLanguage(language);
#else
  // TODO(thakis): Implement.
  NOTIMPLEMENTED();
  return false;
#endif
}

bool ChromeRLZTrackerDelegate::GetReferral(base::string16* referral) {
#if defined(OS_WIN)
  return GoogleUpdateSettings::GetReferral(referral);
#else
  // The referral program is defunct and not used. No need to implement this
  // function on non-Win platforms.
  return true;
#endif
}

bool ChromeRLZTrackerDelegate::ClearReferral() {
#if defined(OS_WIN)
  return GoogleUpdateSettings::ClearReferral();
#else
  // The referral program is defunct and not used. No need to implement this
  // function on non-Win platforms.
  return true;
#endif
}

void ChromeRLZTrackerDelegate::SetOmniboxSearchCallback(
    const base::Closure& callback) {
  DCHECK(!callback.is_null());
  omnibox_url_opened_subscription_ =
      OmniboxEventGlobalTracker::GetInstance()->RegisterCallback(
          base::Bind(&ChromeRLZTrackerDelegate::OnURLOpenedFromOmnibox,
                     base::Unretained(this)));
  on_omnibox_search_callback_ = callback;
}

void ChromeRLZTrackerDelegate::SetHomepageSearchCallback(
    const base::Closure& callback) {
  DCHECK(!callback.is_null());
  registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
                 content::NotificationService::AllSources());
  on_homepage_search_callback_ = callback;
}

void ChromeRLZTrackerDelegate::Observe(
    int type,
    const content::NotificationSource& source,
    const content::NotificationDetails& details) {
  using std::swap;
  base::Closure callback_to_run;
  switch (type) {
    case content::NOTIFICATION_NAV_ENTRY_COMMITTED: {
      // Firstly check if it is a Google search.
      content::LoadCommittedDetails* load_details =
          content::Details<content::LoadCommittedDetails>(details).ptr();
      if (load_details == nullptr)
        break;

      content::NavigationEntry* entry = load_details->entry;
      if (entry == nullptr)
        break;

      if (google_util::IsGoogleSearchUrl(entry->GetURL())) {
        // If it is a Google search, check if it originates from HOMEPAGE by
        // getting the previous NavigationEntry.
        content::NavigationController* controller =
            content::Source<content::NavigationController>(source).ptr();
        if (controller == nullptr)
          break;

        int entry_index = controller->GetLastCommittedEntryIndex();
        if (entry_index < 1)
          break;

        const content::NavigationEntry* previous_entry =
            controller->GetEntryAtIndex(entry_index - 1);

        if (previous_entry == nullptr)
          break;

        // Make sure it is a Google web page originated from HOMEPAGE.
        if (google_util::IsGoogleHomePageUrl(previous_entry->GetURL()) &&
            ((previous_entry->GetTransitionType() &
              ui::PAGE_TRANSITION_HOME_PAGE) != 0)) {
          registrar_.Remove(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
                            content::NotificationService::AllSources());
          swap(callback_to_run, on_homepage_search_callback_);
        }
      }
      break;
    }

    default:
      NOTREACHED();
      break;
  }

  if (!callback_to_run.is_null())
    callback_to_run.Run();
}

void ChromeRLZTrackerDelegate::OnURLOpenedFromOmnibox(OmniboxLog* log) {
  using std::swap;

  // In M-36, we made NOTIFICATION_OMNIBOX_OPENED_URL fire more often than
  // it did previously.  The RLZ folks want RLZ's "first search" detection
  // to remain as unaffected as possible by this change.  This test is
  // there to keep the old behavior.
  if (!log->is_popup_open)
    return;

  omnibox_url_opened_subscription_.reset();
  base::Closure omnibox_callback;
  swap(omnibox_callback, on_omnibox_search_callback_);
  omnibox_callback.Run();
}