OLD | NEW |
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/ui/browser_otr_state.h" | |
19 #include "chrome/common/chrome_switches.h" | |
20 #include "chrome/common/pref_names.h" | |
21 #include "chrome/installer/util/google_update_settings.h" | |
22 #include "components/metrics/metrics_service.h" | 9 #include "components/metrics/metrics_service.h" |
| 10 #include "components/metrics/metrics_service_client.h" |
23 #include "components/metrics/metrics_state_manager.h" | 11 #include "components/metrics/metrics_state_manager.h" |
24 #include "components/rappor/rappor_service.h" | 12 #include "components/rappor/rappor_service.h" |
25 #include "components/variations/service/variations_service.h" | 13 #include "components/variations/service/variations_service.h" |
26 #include "content/public/browser/browser_thread.h" | |
27 | 14 |
28 #if defined(OS_CHROMEOS) | 15 MetricsServicesManager::MetricsServicesManager( |
29 #include "chrome/browser/chromeos/settings/cros_settings.h" | 16 scoped_ptr<MetricsServicesManagerClient> client) |
30 #endif | 17 : client_(client.Pass()), may_upload_(false), may_record_(false) { |
31 | 18 DCHECK(client_); |
32 // Posts |GoogleUpdateSettings::StoreMetricsClientInfo| on blocking pool thread | |
33 // because it needs access to IO and cannot work from UI thread. | |
34 void PostStoreMetricsClientInfo(const metrics::ClientInfo& client_info) { | |
35 content::BrowserThread::GetBlockingPool()->PostTask(FROM_HERE, | |
36 base::Bind(&GoogleUpdateSettings::StoreMetricsClientInfo, client_info)); | |
37 } | |
38 | |
39 MetricsServicesManager::MetricsServicesManager(PrefService* local_state) | |
40 : local_state_(local_state), | |
41 may_upload_(false), | |
42 may_record_(false) { | |
43 DCHECK(local_state); | |
44 } | 19 } |
45 | 20 |
46 MetricsServicesManager::~MetricsServicesManager() { | 21 MetricsServicesManager::~MetricsServicesManager() { |
47 } | 22 } |
48 | 23 |
49 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { | 24 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { |
50 DCHECK(thread_checker_.CalledOnValidThread()); | 25 DCHECK(thread_checker_.CalledOnValidThread()); |
51 return GetChromeMetricsServiceClient()->metrics_service(); | 26 return GetMetricsServiceClient()->GetMetricsService(); |
52 } | 27 } |
53 | 28 |
54 rappor::RapporService* MetricsServicesManager::GetRapporService() { | 29 rappor::RapporService* MetricsServicesManager::GetRapporService() { |
55 DCHECK(thread_checker_.CalledOnValidThread()); | 30 DCHECK(thread_checker_.CalledOnValidThread()); |
56 if (!rappor_service_) { | 31 if (!rappor_service_) { |
57 rappor_service_.reset(new rappor::RapporService( | 32 rappor_service_ = client_->CreateRapporService(); |
58 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive))); | 33 rappor_service_->Initialize(client_->GetURLRequestContext()); |
59 rappor_service_->Initialize(g_browser_process->system_request_context()); | |
60 } | 34 } |
61 return rappor_service_.get(); | 35 return rappor_service_.get(); |
62 } | 36 } |
63 | 37 |
64 variations::VariationsService* MetricsServicesManager::GetVariationsService() { | 38 variations::VariationsService* MetricsServicesManager::GetVariationsService() { |
65 DCHECK(thread_checker_.CalledOnValidThread()); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
66 if (!variations_service_) { | 40 if (!variations_service_) |
67 variations_service_ = variations::VariationsService::Create( | 41 variations_service_ = client_->CreateVariationsService(); |
68 make_scoped_ptr(new ChromeVariationsServiceClient()), local_state_, | |
69 GetMetricsStateManager(), switches::kDisableBackgroundNetworking, | |
70 chrome_variations::CreateUIStringOverrider()); | |
71 } | |
72 return variations_service_.get(); | 42 return variations_service_.get(); |
73 } | 43 } |
74 | 44 |
75 void MetricsServicesManager::OnPluginLoadingError( | 45 void MetricsServicesManager::OnPluginLoadingError( |
76 const base::FilePath& plugin_path) { | 46 const base::FilePath& plugin_path) { |
77 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path); | 47 GetMetricsServiceClient()->OnPluginLoadingError(plugin_path); |
78 } | 48 } |
79 | 49 |
80 ChromeMetricsServiceClient* | 50 metrics::MetricsServiceClient* |
81 MetricsServicesManager::GetChromeMetricsServiceClient() { | 51 MetricsServicesManager::GetMetricsServiceClient() { |
82 DCHECK(thread_checker_.CalledOnValidThread()); | 52 DCHECK(thread_checker_.CalledOnValidThread()); |
83 if (!metrics_service_client_) { | 53 if (!metrics_service_client_) |
84 metrics_service_client_ = ChromeMetricsServiceClient::Create( | 54 metrics_service_client_ = client_->CreateMetricsServiceClient(); |
85 GetMetricsStateManager(), local_state_); | |
86 } | |
87 return metrics_service_client_.get(); | 55 return metrics_service_client_.get(); |
88 } | 56 } |
89 | 57 |
90 metrics::MetricsStateManager* MetricsServicesManager::GetMetricsStateManager() { | |
91 DCHECK(thread_checker_.CalledOnValidThread()); | |
92 if (!metrics_state_manager_) { | |
93 metrics_state_manager_ = metrics::MetricsStateManager::Create( | |
94 local_state_, | |
95 base::Bind( | |
96 &ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled), | |
97 base::Bind(&PostStoreMetricsClientInfo), | |
98 base::Bind(&GoogleUpdateSettings::LoadMetricsClientInfo)); | |
99 } | |
100 return metrics_state_manager_.get(); | |
101 } | |
102 | |
103 bool MetricsServicesManager::GetSafeBrowsingState() { | |
104 // Start listening for updates to SB service state. This is done here instead | |
105 // of in the constructor to avoid errors from trying to instantiate SB | |
106 // service before the IO thread exists. | |
107 SafeBrowsingService* sb_service = g_browser_process->safe_browsing_service(); | |
108 if (!sb_state_subscription_ && sb_service) { | |
109 // base::Unretained(this) is safe here since this object owns the | |
110 // sb_state_subscription_ which owns the pointer. | |
111 sb_state_subscription_ = sb_service->RegisterStateCallback( | |
112 base::Bind(&MetricsServicesManager::UpdateRunningServices, | |
113 base::Unretained(this))); | |
114 } | |
115 | |
116 return sb_service && sb_service->enabled_by_prefs(); | |
117 } | |
118 | |
119 void MetricsServicesManager::UpdatePermissions(bool may_record, | 58 void MetricsServicesManager::UpdatePermissions(bool may_record, |
120 bool may_upload) { | 59 bool may_upload) { |
121 DCHECK(thread_checker_.CalledOnValidThread()); | 60 DCHECK(thread_checker_.CalledOnValidThread()); |
122 // Stash the current permissions so that we can update the RapporService | 61 // Stash the current permissions so that we can update the RapporService |
123 // correctly when the Rappor preference changes. The metrics recording | 62 // correctly when the Rappor preference changes. The metrics recording |
124 // preference partially determines the initial rappor setting, and also | 63 // preference partially determines the initial rappor setting, and also |
125 // controls whether FINE metrics are sent. | 64 // controls whether FINE metrics are sent. |
126 may_record_ = may_record; | 65 may_record_ = may_record; |
127 may_upload_ = may_upload; | 66 may_upload_ = may_upload; |
128 UpdateRunningServices(); | 67 UpdateRunningServices(); |
129 } | 68 } |
130 | 69 |
131 void MetricsServicesManager::UpdateRunningServices() { | 70 void MetricsServicesManager::UpdateRunningServices() { |
132 DCHECK(thread_checker_.CalledOnValidThread()); | 71 DCHECK(thread_checker_.CalledOnValidThread()); |
133 metrics::MetricsService* metrics = GetMetricsService(); | 72 metrics::MetricsService* metrics = GetMetricsService(); |
134 | 73 |
135 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); | 74 if (client_->OnlyDoMetricsRecording()) { |
136 | |
137 const bool only_do_metrics_recording = | |
138 cmdline->HasSwitch(switches::kMetricsRecordingOnly) || | |
139 cmdline->HasSwitch(switches::kEnableBenchmarking); | |
140 | |
141 if (only_do_metrics_recording) { | |
142 metrics->StartRecordingForTests(); | 75 metrics->StartRecordingForTests(); |
143 GetRapporService()->Update( | 76 GetRapporService()->Update( |
144 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, | 77 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, |
145 false); | 78 false); |
146 return; | 79 return; |
147 } | 80 } |
148 | 81 |
149 if (may_record_) { | 82 if (may_record_) { |
150 if (!metrics->recording_active()) | 83 if (!metrics->recording_active()) |
151 metrics->Start(); | 84 metrics->Start(); |
152 | 85 |
153 if (may_upload_) | 86 if (may_upload_) |
154 metrics->EnableReporting(); | 87 metrics->EnableReporting(); |
155 else | 88 else |
156 metrics->DisableReporting(); | 89 metrics->DisableReporting(); |
157 } else { | 90 } else { |
158 metrics->Stop(); | 91 metrics->Stop(); |
159 } | 92 } |
160 | 93 |
161 int recording_groups = 0; | 94 int recording_groups = 0; |
162 #if defined(GOOGLE_CHROME_BUILD) | 95 #if defined(GOOGLE_CHROME_BUILD) |
163 if (may_record_) | 96 if (may_record_) |
164 recording_groups |= rappor::UMA_RAPPOR_GROUP; | 97 recording_groups |= rappor::UMA_RAPPOR_GROUP; |
165 if (GetSafeBrowsingState()) | 98 |
| 99 // NOTE: It is safe to use a raw pointer to |this| because this object owns |
| 100 // |client_|, and the contract of |
| 101 // MetricsServicesManagerClient::IsSafeBrowsingEnabled() states that the |
| 102 // callback passed in must not be used beyond the lifetime of the client |
| 103 // instance. |
| 104 base::Closure on_safe_browsing_update_callback = base::Bind( |
| 105 &MetricsServicesManager::UpdateRunningServices, base::Unretained(this)); |
| 106 if (client_->IsSafeBrowsingEnabled(on_safe_browsing_update_callback) |
166 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; | 107 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; |
167 #endif // defined(GOOGLE_CHROME_BUILD) | 108 #endif // defined(GOOGLE_CHROME_BUILD) |
168 GetRapporService()->Update(recording_groups, may_upload_); | 109 GetRapporService()->Update(recording_groups, may_upload_); |
169 } | 110 } |
170 | 111 |
171 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { | 112 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { |
172 return UpdatePermissions( | 113 return UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload); |
173 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(), | |
174 may_upload); | |
175 } | 114 } |
OLD | NEW |