summaryrefslogtreecommitdiffstats
path: root/chrome/browser/metrics/chrome_metrics_services_manager_client.cc
blob: 5e6b5a3063c428ade91d94477c6d45de369695b3 (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
// 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/metrics/chrome_metrics_services_manager_client.h"

#include "base/command_line.h"
#include "base/logging.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/browser/metrics/chrome_metrics_service_client.h"
#include "chrome/browser/metrics/variations/chrome_variations_service_client.h"
#include "chrome/browser/metrics/variations/ui_string_overrider_factory.h"
#include "chrome/browser/ui/browser_otr_state.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/installer/util/google_update_settings.h"
#include "components/metrics/metrics_state_manager.h"
#include "components/prefs/pref_service.h"
#include "components/rappor/rappor_service.h"
#include "components/variations/service/variations_service.h"
#include "content/public/browser/browser_thread.h"

namespace {

// Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread
// because it needs access to IO and cannot work from UI thread.
void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) {
  content::BrowserThread::GetBlockingPool()->PostTask(
      FROM_HERE,
      base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info));
}

}  // namespace

ChromeMetricsServicesManagerClient::ChromeMetricsServicesManagerClient(
    PrefService* local_state)
    : local_state_(local_state) {
  DCHECK(local_state);

  SetupMetricsStateForChromeOS();
}

ChromeMetricsServicesManagerClient::~ChromeMetricsServicesManagerClient() {}

scoped_ptr<rappor::RapporService>
ChromeMetricsServicesManagerClient::CreateRapporService() {
  DCHECK(thread_checker_.CalledOnValidThread());
  return make_scoped_ptr(new rappor::RapporService(
      local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive)));
}

scoped_ptr<variations::VariationsService>
ChromeMetricsServicesManagerClient::CreateVariationsService() {
  DCHECK(thread_checker_.CalledOnValidThread());
  return variations::VariationsService::Create(
      make_scoped_ptr(new ChromeVariationsServiceClient()), local_state_,
      GetMetricsStateManager(), switches::kDisableBackgroundNetworking,
      chrome_variations::CreateUIStringOverrider());
}

scoped_ptr<metrics::MetricsServiceClient>
ChromeMetricsServicesManagerClient::CreateMetricsServiceClient() {
  DCHECK(thread_checker_.CalledOnValidThread());
  return ChromeMetricsServiceClient::Create(GetMetricsStateManager(),
                                            local_state_);
}

net::URLRequestContextGetter*
ChromeMetricsServicesManagerClient::GetURLRequestContext() {
  return g_browser_process->system_request_context();
}

bool ChromeMetricsServicesManagerClient::IsSafeBrowsingEnabled(
    const base::Closure& on_update_callback) {
  // Start listening for updates to SB service state. This is done here instead
  // of in the constructor to avoid errors from trying to instantiate SB
  // service before the IO thread exists.
  safe_browsing::SafeBrowsingService* sb_service =
      g_browser_process->safe_browsing_service();
  if (!sb_state_subscription_ && sb_service) {
    // It is safe to pass the callback received from the
    // MetricsServicesManager here since the MetricsServicesManager owns
    // this object, which owns the sb_state_subscription_, which owns the
    // pointer to the MetricsServicesManager.
    sb_state_subscription_ =
        sb_service->RegisterStateCallback(on_update_callback);
  }

  return sb_service && sb_service->enabled_by_prefs();
}

bool ChromeMetricsServicesManagerClient::IsMetricsReportingEnabled() {
  return ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled();
}

bool ChromeMetricsServicesManagerClient::OnlyDoMetricsRecording() {
  const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
  return cmdline->HasSwitch(switches::kMetricsRecordingOnly) ||
         cmdline->HasSwitch(switches::kEnableBenchmarking);
}

metrics::MetricsStateManager*
ChromeMetricsServicesManagerClient::GetMetricsStateManager() {
  DCHECK(thread_checker_.CalledOnValidThread());
  if (!metrics_state_manager_) {
    metrics_state_manager_ = metrics::MetricsStateManager::Create(
        local_state_,
        base::Bind(
            &ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled),
        base::Bind(&PostStoreMetricsClientInfo),
        base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo));
  }
  return metrics_state_manager_.get();
}