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
3 // found in the LICENSE file.
5 #include "chromecast/browser/metrics/cast_metrics_service_client.h"
7 #include "base/command_line.h"
9 #include "base/i18n/rtl.h"
10 #include "base/prefs/pref_registry_simple.h"
11 #include "base/prefs/pref_service.h"
12 #include "base/thread_task_runner_handle.h"
13 #include "chromecast/base/chromecast_switches.h"
14 #include "chromecast/browser/metrics/cast_stability_metrics_provider.h"
15 #include "chromecast/browser/metrics/platform_metrics_providers.h"
16 #include "chromecast/common/pref_names.h"
17 #include "components/metrics/client_info.h"
18 #include "components/metrics/gpu/gpu_metrics_provider.h"
19 #include "components/metrics/metrics_provider.h"
20 #include "components/metrics/metrics_service.h"
21 #include "components/metrics/metrics_state_manager.h"
22 #include "components/metrics/net/net_metrics_log_uploader.h"
23 #include "components/metrics/net/network_metrics_provider.h"
24 #include "components/metrics/profiler/profiler_metrics_provider.h"
25 #include "components/metrics/url_constants.h"
26 #include "content/public/common/content_switches.h"
29 #include "chromecast/browser/metrics/external_metrics.h"
30 #endif // defined(OS_LINUX)
32 namespace chromecast
{
37 const int kStandardUploadIntervalMinutes
= 5;
39 const char kMetricsOldClientID
[] = "user_experience_metrics.client_id";
44 scoped_ptr
<CastMetricsServiceClient
> CastMetricsServiceClient::Create(
45 base::TaskRunner
* io_task_runner
,
46 PrefService
* pref_service
,
47 net::URLRequestContextGetter
* request_context
) {
48 return make_scoped_ptr(new CastMetricsServiceClient(io_task_runner
,
53 void CastMetricsServiceClient::RegisterPrefs(PrefRegistrySimple
* registry
) {
54 registry
->RegisterStringPref(kMetricsOldClientID
, std::string());
57 void CastMetricsServiceClient::SetMetricsClientId(
58 const std::string
& client_id
) {
59 client_id_
= client_id
;
60 LOG(INFO
) << "Metrics client ID set: " << client_id
;
61 PlatformSetClientID(cast_service_
, client_id
);
64 void CastMetricsServiceClient::OnRecordingDisabled() {
67 void CastMetricsServiceClient::StoreClientInfo(
68 const ::metrics::ClientInfo
& client_info
) {
69 const std::string
& client_id
= client_info
.client_id
;
70 DCHECK(client_id
.empty() || base::IsValidGUID(client_id
));
71 // backup client_id or reset to empty.
72 SetMetricsClientId(client_id
);
75 scoped_ptr
< ::metrics::ClientInfo
> CastMetricsServiceClient::LoadClientInfo() {
76 scoped_ptr
< ::metrics::ClientInfo
> client_info(new ::metrics::ClientInfo
);
78 // kMetricsIsNewClientID would be missing if either the device was just
79 // FDR'ed, or it is on pre-v1.2 build.
80 if (!pref_service_
->GetBoolean(prefs::kMetricsIsNewClientID
)) {
81 // If the old client id exists, the device must be on pre-v1.2 build,
82 // instead of just being FDR'ed.
83 if (!pref_service_
->GetString(kMetricsOldClientID
).empty()) {
84 // Force old client id to be regenerated. See b/9487011.
85 client_info
->client_id
= base::GenerateGUID();
86 pref_service_
->SetBoolean(prefs::kMetricsIsNewClientID
, true);
87 return client_info
.Pass();
89 // else the device was just FDR'ed, pass through.
92 const std::string
client_id(GetPlatformClientID(cast_service_
));
93 if (!client_id
.empty() && base::IsValidGUID(client_id
)) {
94 client_info
->client_id
= client_id
;
95 return client_info
.Pass();
97 if (client_id
.empty()) {
98 LOG(WARNING
) << "Empty client id from platform,"
99 << " assuming this is the first boot up of a new device.";
101 LOG(ERROR
) << "Invalid client id " << client_id
<< " from platform.";
103 return scoped_ptr
< ::metrics::ClientInfo
>();
109 bool CastMetricsServiceClient::IsOffTheRecordSessionActive() {
110 // Chromecast behaves as "off the record" w/r/t recording browsing state,
111 // but this value is about not disabling metrics because of it.
115 int32_t CastMetricsServiceClient::GetProduct() {
116 // Chromecast currently uses the same product identifier as Chrome.
117 return ::metrics::ChromeUserMetricsExtension::CHROME
;
120 std::string
CastMetricsServiceClient::GetApplicationLocale() {
121 return base::i18n::GetConfiguredLocale();
124 bool CastMetricsServiceClient::GetBrand(std::string
* brand_code
) {
128 ::metrics::SystemProfileProto::Channel
CastMetricsServiceClient::GetChannel() {
129 return GetPlatformReleaseChannel(cast_service_
);
132 std::string
CastMetricsServiceClient::GetVersionString() {
133 return GetPlatformVersionString(cast_service_
);
136 void CastMetricsServiceClient::OnLogUploadComplete() {
137 PlatformOnLogUploadComplete(cast_service_
);
140 void CastMetricsServiceClient::StartGatheringMetrics(
141 const base::Closure
& done_callback
) {
145 void CastMetricsServiceClient::CollectFinalMetrics(
146 const base::Closure
& done_callback
) {
150 scoped_ptr
< ::metrics::MetricsLogUploader
>
151 CastMetricsServiceClient::CreateUploader(
152 const base::Callback
<void(int)>& on_upload_complete
) {
153 std::string
uma_server_url(::metrics::kDefaultMetricsServerUrl
);
154 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
155 if (command_line
->HasSwitch(switches::kOverrideMetricsUploadUrl
)) {
156 uma_server_url
.assign(
157 command_line
->GetSwitchValueASCII(switches::kOverrideMetricsUploadUrl
));
159 DCHECK(!uma_server_url
.empty());
160 return scoped_ptr
< ::metrics::MetricsLogUploader
>(
161 new ::metrics::NetMetricsLogUploader(
164 ::metrics::kDefaultMetricsMimeType
,
165 on_upload_complete
));
168 base::TimeDelta
CastMetricsServiceClient::GetStandardUploadInterval() {
169 return base::TimeDelta::FromMinutes(kStandardUploadIntervalMinutes
);
172 void CastMetricsServiceClient::EnableMetricsService(bool enabled
) {
173 if (!task_runner_
->BelongsToCurrentThread()) {
174 task_runner_
->PostTask(
175 FROM_HERE
, base::Bind(&CastMetricsServiceClient::EnableMetricsService
,
176 base::Unretained(this), enabled
));
181 metrics_service_
->Start();
183 metrics_service_
->Stop();
187 CastMetricsServiceClient::CastMetricsServiceClient(
188 base::TaskRunner
* io_task_runner
,
189 PrefService
* pref_service
,
190 net::URLRequestContextGetter
* request_context
)
191 : io_task_runner_(io_task_runner
),
192 pref_service_(pref_service
),
194 #if !defined(OS_ANDROID)
195 external_metrics_(NULL
),
196 #endif // !defined(OS_ANDROID)
197 task_runner_(base::ThreadTaskRunnerHandle::Get()),
198 request_context_(request_context
) {
201 CastMetricsServiceClient::~CastMetricsServiceClient() {
202 #if !defined(OS_ANDROID)
203 DCHECK(!external_metrics_
);
204 #endif // !defined(OS_ANDROID)
207 void CastMetricsServiceClient::Initialize(CastService
* cast_service
) {
208 DCHECK(cast_service
);
209 DCHECK(!cast_service_
);
210 cast_service_
= cast_service
;
212 metrics_state_manager_
= ::metrics::MetricsStateManager::Create(
214 base::Bind(&CastMetricsServiceClient::IsReportingEnabled
,
215 base::Unretained(this)),
216 base::Bind(&CastMetricsServiceClient::StoreClientInfo
,
217 base::Unretained(this)),
218 base::Bind(&CastMetricsServiceClient::LoadClientInfo
,
219 base::Unretained(this)));
220 metrics_service_
.reset(new ::metrics::MetricsService(
221 metrics_state_manager_
.get(),
225 // Always create a client id as it may also be used by crash reporting,
226 // (indirectly) included in feedback, and can be queried during setup.
227 // For UMA and crash reporting, associated opt-in settings will control
228 // sending reports as directed by the user.
229 // For Setup (which also communicates the user's opt-in preferences),
230 // report the client-id and expect that setup will handle the current opt-in
232 metrics_state_manager_
->ForceClientIdCreation();
234 CastStabilityMetricsProvider
* stability_provider
=
235 new CastStabilityMetricsProvider(metrics_service_
.get());
236 metrics_service_
->RegisterMetricsProvider(
237 scoped_ptr
< ::metrics::MetricsProvider
>(stability_provider
));
239 base::CommandLine
* command_line
= base::CommandLine::ForCurrentProcess();
240 if (!command_line
->HasSwitch(switches::kDisableGpu
)) {
241 metrics_service_
->RegisterMetricsProvider(
242 scoped_ptr
< ::metrics::MetricsProvider
>(
243 new ::metrics::GPUMetricsProvider
));
245 metrics_service_
->RegisterMetricsProvider(
246 scoped_ptr
< ::metrics::MetricsProvider
>(
247 new ::metrics::NetworkMetricsProvider(io_task_runner_
)));
248 metrics_service_
->RegisterMetricsProvider(
249 scoped_ptr
< ::metrics::MetricsProvider
>(
250 new ::metrics::ProfilerMetricsProvider
));
251 RegisterPlatformMetricsProviders(metrics_service_
.get(), cast_service_
);
253 metrics_service_
->InitializeMetricsRecordingState();
254 #if !defined(OS_ANDROID)
255 // Reset clean_shutdown bit after InitializeMetricsRecordingState().
256 metrics_service_
->LogNeedForCleanShutdown();
257 #endif // !defined(OS_ANDROID)
259 if (IsReportingEnabled())
260 metrics_service_
->Start();
262 // Start external metrics collection, which feeds data from external
263 // processes into the main external metrics.
264 #if defined(OS_LINUX)
265 external_metrics_
= new ExternalMetrics(stability_provider
);
266 external_metrics_
->Start();
267 #endif // defined(OS_LINUX)
270 void CastMetricsServiceClient::Finalize() {
271 #if !defined(OS_ANDROID)
272 // Set clean_shutdown bit.
273 metrics_service_
->RecordCompletedSessionEnd();
274 #endif // !defined(OS_ANDROID)
276 // Stop metrics service cleanly before destructing CastMetricsServiceClient.
277 #if defined(OS_LINUX)
278 external_metrics_
->StopAndDestroy();
279 external_metrics_
= NULL
;
280 #endif // defined(OS_LINUX)
281 metrics_service_
->Stop();
284 bool CastMetricsServiceClient::IsReportingEnabled() {
285 return PlatformIsReportingEnabled(cast_service_
);
288 } // namespace metrics
289 } // namespace chromecast