1// Copyright (c) 2014 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_search_counter_android.h"
6
7#include "base/logging.h"
8#include "chrome/browser/google/google_search_counter.h"
9#include "chrome/browser/prerender/prerender_manager.h"
10#include "chrome/browser/prerender/prerender_manager_factory.h"
11#include "components/google/core/browser/google_search_metrics.h"
12#include "content/public/browser/navigation_details.h"
13#include "content/public/browser/navigation_entry.h"
14#include "content/public/browser/notification_service.h"
15#include "content/public/browser/notification_types.h"
16
17GoogleSearchCounterAndroid::GoogleSearchCounterAndroid(Profile* profile)
18    : profile_(profile) {
19  // We always listen for all COMMITTED navigations from all sources, as any
20  // one of them could be a navigation of interest.
21  registrar_.Add(this, content::NOTIFICATION_NAV_ENTRY_COMMITTED,
22                 content::NotificationService::AllSources());
23}
24
25GoogleSearchCounterAndroid::~GoogleSearchCounterAndroid() {
26}
27
28void GoogleSearchCounterAndroid::ProcessCommittedEntry(
29    const content::NotificationSource& source,
30    const content::NotificationDetails& details) {
31  GoogleSearchCounter* counter = GoogleSearchCounter::GetInstance();
32  DCHECK(counter);
33  if (!counter->ShouldRecordCommittedDetails(details))
34    return;
35
36  const content::NavigationEntry& entry =
37      *content::Details<content::LoadCommittedDetails>(details)->entry;
38  prerender::PrerenderManager* prerender_manager =
39      prerender::PrerenderManagerFactory::GetForProfile(profile_);
40  // |prerender_manager| is NULL when prerendering is disabled.
41  bool prerender_enabled =
42      prerender_manager ? prerender_manager->IsEnabled() : false;
43  counter->search_metrics()->RecordAndroidGoogleSearch(
44      counter->GetGoogleSearchAccessPointForSearchNavEntry(entry),
45      prerender_enabled);
46}
47
48void GoogleSearchCounterAndroid::Observe(
49    int type,
50    const content::NotificationSource& source,
51    const content::NotificationDetails& details) {
52  DCHECK_EQ(content::NOTIFICATION_NAV_ENTRY_COMMITTED, type);
53  ProcessCommittedEntry(source, details);
54}
55