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" |
7 #include "base/logging.h" | 10 #include "base/logging.h" |
8 #include "chrome/browser/metrics/metrics_services_manager_client.h" | 11 #include "base/prefs/pref_service.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" |
9 #include "components/metrics/metrics_service.h" | 22 #include "components/metrics/metrics_service.h" |
10 #include "components/metrics/metrics_service_client.h" | |
11 #include "components/metrics/metrics_state_manager.h" | 23 #include "components/metrics/metrics_state_manager.h" |
12 #include "components/rappor/rappor_service.h" | 24 #include "components/rappor/rappor_service.h" |
13 #include "components/variations/service/variations_service.h" | 25 #include "components/variations/service/variations_service.h" |
| 26 #include "content/public/browser/browser_thread.h" |
14 | 27 |
15 MetricsServicesManager::MetricsServicesManager( | 28 #if defined(OS_CHROMEOS) |
16 scoped_ptr<MetricsServicesManagerClient> client) | 29 #include "chrome/browser/chromeos/settings/cros_settings.h" |
17 : client_(client.Pass()), may_upload_(false), may_record_(false) { | 30 #endif |
18 DCHECK(client_); | 31 |
| 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); |
19 } | 44 } |
20 | 45 |
21 MetricsServicesManager::~MetricsServicesManager() { | 46 MetricsServicesManager::~MetricsServicesManager() { |
22 } | 47 } |
23 | 48 |
24 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { | 49 metrics::MetricsService* MetricsServicesManager::GetMetricsService() { |
25 DCHECK(thread_checker_.CalledOnValidThread()); | 50 DCHECK(thread_checker_.CalledOnValidThread()); |
26 return GetMetricsServiceClient()->GetMetricsService(); | 51 return GetChromeMetricsServiceClient()->metrics_service(); |
27 } | 52 } |
28 | 53 |
29 rappor::RapporService* MetricsServicesManager::GetRapporService() { | 54 rappor::RapporService* MetricsServicesManager::GetRapporService() { |
30 DCHECK(thread_checker_.CalledOnValidThread()); | 55 DCHECK(thread_checker_.CalledOnValidThread()); |
31 if (!rappor_service_) { | 56 if (!rappor_service_) { |
32 rappor_service_ = client_->CreateRapporService(); | 57 rappor_service_.reset(new rappor::RapporService( |
33 rappor_service_->Initialize(client_->GetURLRequestContext()); | 58 local_state_, base::Bind(&chrome::IsOffTheRecordSessionActive))); |
| 59 rappor_service_->Initialize(g_browser_process->system_request_context()); |
34 } | 60 } |
35 return rappor_service_.get(); | 61 return rappor_service_.get(); |
36 } | 62 } |
37 | 63 |
38 variations::VariationsService* MetricsServicesManager::GetVariationsService() { | 64 variations::VariationsService* MetricsServicesManager::GetVariationsService() { |
39 DCHECK(thread_checker_.CalledOnValidThread()); | 65 DCHECK(thread_checker_.CalledOnValidThread()); |
40 if (!variations_service_) | 66 if (!variations_service_) { |
41 variations_service_ = client_->CreateVariationsService(); | 67 variations_service_ = variations::VariationsService::Create( |
| 68 make_scoped_ptr(new ChromeVariationsServiceClient()), local_state_, |
| 69 GetMetricsStateManager(), switches::kDisableBackgroundNetworking, |
| 70 chrome_variations::CreateUIStringOverrider()); |
| 71 } |
42 return variations_service_.get(); | 72 return variations_service_.get(); |
43 } | 73 } |
44 | 74 |
45 void MetricsServicesManager::OnPluginLoadingError( | 75 void MetricsServicesManager::OnPluginLoadingError( |
46 const base::FilePath& plugin_path) { | 76 const base::FilePath& plugin_path) { |
47 GetMetricsServiceClient()->OnPluginLoadingError(plugin_path); | 77 GetChromeMetricsServiceClient()->LogPluginLoadingError(plugin_path); |
48 } | 78 } |
49 | 79 |
50 metrics::MetricsServiceClient* | 80 ChromeMetricsServiceClient* |
51 MetricsServicesManager::GetMetricsServiceClient() { | 81 MetricsServicesManager::GetChromeMetricsServiceClient() { |
52 DCHECK(thread_checker_.CalledOnValidThread()); | 82 DCHECK(thread_checker_.CalledOnValidThread()); |
53 if (!metrics_service_client_) | 83 if (!metrics_service_client_) { |
54 metrics_service_client_ = client_->CreateMetricsServiceClient(); | 84 metrics_service_client_ = ChromeMetricsServiceClient::Create( |
| 85 GetMetricsStateManager(), local_state_); |
| 86 } |
55 return metrics_service_client_.get(); | 87 return metrics_service_client_.get(); |
56 } | 88 } |
57 | 89 |
| 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 |
58 void MetricsServicesManager::UpdatePermissions(bool may_record, | 119 void MetricsServicesManager::UpdatePermissions(bool may_record, |
59 bool may_upload) { | 120 bool may_upload) { |
60 DCHECK(thread_checker_.CalledOnValidThread()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
61 // Stash the current permissions so that we can update the RapporService | 122 // Stash the current permissions so that we can update the RapporService |
62 // correctly when the Rappor preference changes. The metrics recording | 123 // correctly when the Rappor preference changes. The metrics recording |
63 // preference partially determines the initial rappor setting, and also | 124 // preference partially determines the initial rappor setting, and also |
64 // controls whether FINE metrics are sent. | 125 // controls whether FINE metrics are sent. |
65 may_record_ = may_record; | 126 may_record_ = may_record; |
66 may_upload_ = may_upload; | 127 may_upload_ = may_upload; |
67 UpdateRunningServices(); | 128 UpdateRunningServices(); |
68 } | 129 } |
69 | 130 |
70 void MetricsServicesManager::UpdateRunningServices() { | 131 void MetricsServicesManager::UpdateRunningServices() { |
71 DCHECK(thread_checker_.CalledOnValidThread()); | 132 DCHECK(thread_checker_.CalledOnValidThread()); |
72 metrics::MetricsService* metrics = GetMetricsService(); | 133 metrics::MetricsService* metrics = GetMetricsService(); |
73 | 134 |
74 if (client_->OnlyDoMetricsRecording()) { | 135 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess(); |
| 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) { |
75 metrics->StartRecordingForTests(); | 142 metrics->StartRecordingForTests(); |
76 GetRapporService()->Update( | 143 GetRapporService()->Update( |
77 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, | 144 rappor::UMA_RAPPOR_GROUP | rappor::SAFEBROWSING_RAPPOR_GROUP, |
78 false); | 145 false); |
79 return; | 146 return; |
80 } | 147 } |
81 | 148 |
82 if (may_record_) { | 149 if (may_record_) { |
83 if (!metrics->recording_active()) | 150 if (!metrics->recording_active()) |
84 metrics->Start(); | 151 metrics->Start(); |
85 | 152 |
86 if (may_upload_) | 153 if (may_upload_) |
87 metrics->EnableReporting(); | 154 metrics->EnableReporting(); |
88 else | 155 else |
89 metrics->DisableReporting(); | 156 metrics->DisableReporting(); |
90 } else { | 157 } else { |
91 metrics->Stop(); | 158 metrics->Stop(); |
92 } | 159 } |
93 | 160 |
94 int recording_groups = 0; | 161 int recording_groups = 0; |
95 #if defined(GOOGLE_CHROME_BUILD) | 162 #if defined(GOOGLE_CHROME_BUILD) |
96 if (may_record_) | 163 if (may_record_) |
97 recording_groups |= rappor::UMA_RAPPOR_GROUP; | 164 recording_groups |= rappor::UMA_RAPPOR_GROUP; |
98 | 165 if (GetSafeBrowsingState()) |
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) | |
107 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; | 166 recording_groups |= rappor::SAFEBROWSING_RAPPOR_GROUP; |
108 #endif // defined(GOOGLE_CHROME_BUILD) | 167 #endif // defined(GOOGLE_CHROME_BUILD) |
109 GetRapporService()->Update(recording_groups, may_upload_); | 168 GetRapporService()->Update(recording_groups, may_upload_); |
110 } | 169 } |
111 | 170 |
112 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { | 171 void MetricsServicesManager::UpdateUploadPermissions(bool may_upload) { |
113 return UpdatePermissions(client_->IsMetricsReportingEnabled(), may_upload); | 172 return UpdatePermissions( |
| 173 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled(), |
| 174 may_upload); |
114 } | 175 } |
OLD | NEW |