Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Side by Side Diff: chrome/browser/metrics/metrics_services_manager.cc

Issue 1411593004: Eliminate //chrome dependencies from MetricsServicesManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/browser/metrics/metrics_services_manager.h" 5 #include "chrome/browser/metrics/metrics_services_manager.h"
6 6
7 #include <string>
8
9 #include "base/command_line.h"
10 #include "base/logging.h" 7 #include "base/logging.h"
11 #include "base/prefs/pref_service.h" 8 #include "chrome/browser/metrics/metrics_services_manager_client.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
14 #include "chrome/browser/metrics/chrome_metrics_service_client.h"
15 #include "chrome/browser/metrics/metrics_reporting_state.h"
16 #include "chrome/browser/metrics/variations/chrome_variations_service_client.h"
17 #include "chrome/browser/metrics/variations/ui_string_overrider_factory.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/profiles/profile_manager.h"
20 #include "chrome/browser/ui/browser_otr_state.h"
21 #include "chrome/common/chrome_switches.h"
22 #include "chrome/common/pref_names.h"
23 #include "chrome/installer/util/google_update_settings.h"
24 #include "components/metrics/metrics_service.h" 9 #include "components/metrics/metrics_service.h"
10 #include "components/metrics/metrics_service_client.h"
25 #include "components/metrics/metrics_state_manager.h" 11 #include "components/metrics/metrics_state_manager.h"
26 #include "components/rappor/rappor_service.h" 12 #include "components/rappor/rappor_service.h"
27 #include "components/variations/service/variations_service.h" 13 #include "components/variations/service/variations_service.h"
28 #include "content/public/browser/browser_thread.h"
29 14
30 #if defined(OS_CHROMEOS) 15 MetricsServicesManager::MetricsServicesManager(
31 #include "chrome/browser/chromeos/settings/cros_settings.h" 16 scoped_ptr<MetricsServicesManagerClient> client)
32 #endif 17 : client_(client.Pass()), may_upload_(false), may_record_(false) {
33 18 DCHECK(client_);
34 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread
35 // because it needs access to IO and cannot work from UI thread.
36 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) {
37 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE,
38 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info));
39 }
40
41 MetricsServicesManager::MetricsServicesManager(PrefService* local_state)
42 : local_state_(local_state),
43 may_upload_(false),
44 may_record_(false) {
45 DCHECK(local_state);
46 } 19 }
47 20
48 MetricsServicesManager::~MetricsServicesManager() { 21 MetricsServicesManager::~MetricsServicesManager() {
49 } 22 }
50 23
51 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { 24 metrics::MetricsService* MetricsServicesManager::GetMetricsService() {
52 DCHECK(thread_checker_.CalledOnValidThread()); 25 DCHECK(thread_checker_.CalledOnValidThread());
53 return GetChromeMetricsServiceClient()->metrics_service(); 26 return client_->GetMetricsService(GetMetricsServiceClient());
Alexei Svitkine (slow) 2015/10/19 16:47:19 This is a bit convoluted. Can we just expose metri
blundell 2015/10/20 10:36:45 Yep! I meant to ask you about whether you thought
54 } 27 }
55 28
56 rappor::RapporService* MetricsServicesManager::GetRapporService() { 29 rappor::RapporService* MetricsServicesManager::GetRapporService() {
57 DCHECK(thread_checker_.CalledOnValidThread()); 30 DCHECK(thread_checker_.CalledOnValidThread());
58 if (!rappor_service_) { 31 if (!rappor_service_) {
59 rappor_service_.reset(new rappor::RapporService( 32 rappor_service_ = client_->CreateRapporService();
60 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive))); 33 rappor_service_->Initialize(client_->GetURLRequestContext());
61 rappor_service_->Initialize(g_browser_process->system_request_context());
62 } 34 }
63 return rappor_service_.get(); 35 return rappor_service_.get();
64 } 36 }
65 37
66 variations::VariationsService* MetricsServicesManager::GetVariationsService() { 38 variations::VariationsService* MetricsServicesManager::GetVariationsService() {
67 DCHECK(thread_checker_.CalledOnValidThread()); 39 DCHECK(thread_checker_.CalledOnValidThread());
68 if (!variations_service_) { 40 if (!variations_service_)
69 variations_service_ = variations::VariationsService::Create( 41 variations_service_ = client_->CreateVariationsService();
70 make_scoped_ptr(new ChromeVariationsServiceClient()), local_state_,
71 GetMetricsStateManager(), switches::kDisableBackgroundNetworking,
72 chrome_variations::CreateUIStringOverrider());
73 }
74 return variations_service_.get(); 42 return variations_service_.get();
75 } 43 }
76 44
77 void MetricsServicesManager::OnPluginLoadingError( 45 void MetricsServicesManager::OnPluginLoadingError(
78 const base::FilePath& plugin_path) { 46 const base::FilePath& plugin_path) {
79 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path); 47 GetMetricsServiceClient()->OnPluginLoadingError(plugin_path);
Alexei Svitkine (slow) 2015/10/19 16:47:19 Hmm, I wonder if we should just plumb this through
blundell 2015/10/20 10:36:45 The functionality here is implemented at the //chr
80 } 48 }
81 49
82 ChromeMetricsServiceClient* 50 metrics::MetricsServiceClient*
83 MetricsServicesManager::GetChromeMetricsServiceClient() { 51 MetricsServicesManager::GetMetricsServiceClient() {
84 DCHECK(thread_checker_.CalledOnValidThread()); 52 DCHECK(thread_checker_.CalledOnValidThread());
85 if (!metrics_service_client_) { 53 if (!metrics_service_client_) {
Alexei Svitkine (slow) 2015/10/19 16:47:19 Nit: No {}'s
blundell 2015/10/20 10:36:45 Done.
86 metrics_service_client_ = ChromeMetricsServiceClient::Create( 54 metrics_service_client_ = client_->CreateMetricsServiceClient();
87 GetMetricsStateManager(), local_state_);
88 } 55 }
89 return metrics_service_client_.get(); 56 return metrics_service_client_.get();
90 } 57 }
91 58
92 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() {
93 DCHECK(thread_checker_.CalledOnValidThread());
94 if (!metrics_state_manager_) {
95 metrics_state_manager_ = metrics::MetricsStateManager::Create(
96 local_state_,
97 base::Bind(
98 &ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled),
99 base::Bind(&PostStoreMetricsClientInfo),
100 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo));
101 }
102 return metrics_state_manager_.get();
103 }
104
105 bool MetricsServicesManager::GetSafeBrowsingState() {
106 // Start listening for updates to SB service state. This is done here instead
107 // of in the constructor to avoid errors from trying to instantiate SB
108 // service before the IO thread exists.
109 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service();
110 if (!sb_state_subscription_ && sb_service) {
111 // base::Unretained(this) is safe here since this object owns the
112 // sb_state_subscription_ which owns the pointer.
113 sb_state_subscription_ = sb_service->RegisterStateCallback(
114 base::Bind(&MetricsServicesManager::UpdateRunningServices,
115 base::Unretained(this)));
116 }
117
118 return sb_service && sb_service->enabled_by_prefs();
119 }
120
121 void MetricsServicesManager::UpdatePermissions(bool may_record, 59 void MetricsServicesManager::UpdatePermissions(bool may_record,
122 bool may_upload) { 60 bool may_upload) {
123 DCHECK(thread_checker_.CalledOnValidThread()); 61 DCHECK(thread_checker_.CalledOnValidThread());
124 // Stash the current permissions so that we can update the RapporService 62 // Stash the current permissions so that we can update the RapporService
125 // correctly when the Rappor preference changes. The metrics recording 63 // correctly when the Rappor preference changes. The metrics recording
126 // preference partially determines the initial rappor setting, and also 64 // preference partially determines the initial rappor setting, and also
127 // controls whether FINE metrics are sent. 65 // controls whether FINE metrics are sent.
128 may_record_ = may_record; 66 may_record_ = may_record;
129 may_upload_ = may_upload; 67 may_upload_ = may_upload;
130 UpdateRunningServices(); 68 UpdateRunningServices();
131 } 69 }
132 70
133 void MetricsServicesManager::UpdateRunningServices() { 71 void MetricsServicesManager::UpdateRunningServices() {
134 DCHECK(thread_checker_.CalledOnValidThread()); 72 DCHECK(thread_checker_.CalledOnValidThread());
135 metrics::MetricsService* metrics = GetMetricsService(); 73 metrics::MetricsService* metrics = GetMetricsService();
136 74
137 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); 75 if (client_->OnlyDoMetricsRecording()) {
138
139 const bool only_do_metrics_recording =
140 cmdline->HasSwitch(switches::kMetricsRecordingOnly) ||
141 cmdline->HasSwitch(switches::kEnableBenchmarking);
142
143 if (only_do_metrics_recording) {
144 metrics->StartRecordingForTests(); 76 metrics->StartRecordingForTests();
145 GetRapporService()->Update( 77 GetRapporService()->Update(
146 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, 78 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP,
147 false); 79 false);
148 return; 80 return;
149 } 81 }
150 82
151 if (may_record_) { 83 if (may_record_) {
152 if (!metrics->recording_active()) 84 if (!metrics->recording_active())
153 metrics->Start(); 85 metrics->Start();
154 86
155 if (may_upload_) 87 if (may_upload_)
156 metrics->EnableReporting(); 88 metrics->EnableReporting();
157 else 89 else
158 metrics->DisableReporting(); 90 metrics->DisableReporting();
159 } else { 91 } else {
160 metrics->Stop(); 92 metrics->Stop();
161 } 93 }
162 94
163 int recording_groups = 0; 95 int recording_groups = 0;
164 #if defined(GOOGLE_CHROME_BUILD) 96 #if defined(GOOGLE_CHROME_BUILD)
165 if (may_record_) 97 if (may_record_)
166 recording_groups |= rappor::UMA_RAPPOR_GROUP; 98 recording_groups |= rappor::UMA_RAPPOR_GROUP;
167 if (GetSafeBrowsingState()) 99 if (client_->IsSafeBrowsingEnabled(
100 base::Bind(&MetricsServicesManager::UpdateRunningServices,
101 base::Unretained(this)))
Alexei Svitkine (slow) 2015/10/19 16:47:19 Nit: Make a local var for this callback to make th
blundell 2015/10/20 10:36:45 Done.
168 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; 102 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP;
169 #endif // defined(GOOGLE_CHROME_BUILD) 103 #endif // defined(GOOGLE_CHROME_BUILD)
170 GetRapporService()->Update(recording_groups, may_upload_); 104 GetRapporService()->Update(recording_groups, may_upload_);
171 } 105 }
172 106
173 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { 107 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) {
174 return UpdatePermissions( 108 return UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload);
175 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(),
176 may_upload);
177 } 109 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698