Drive: Add BatchableRequest subclass.
[chromium-blink-merge.git] / chromecast / browser / metrics / cast_metrics_service_client.cc
bloba367322da4e680893ec81f53d0654d27a266c989
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"
8 #include "base/guid.h"
9 #include "base/i18n/rtl.h"
10 #include "base/prefs/pref_service.h"
11 #include "chromecast/browser/metrics/cast_stability_metrics_provider.h"
12 #include "chromecast/browser/metrics/platform_metrics_providers.h"
13 #include "chromecast/common/chromecast_switches.h"
14 #include "chromecast/common/pref_names.h"
15 #include "components/metrics/client_info.h"
16 #include "components/metrics/gpu/gpu_metrics_provider.h"
17 #include "components/metrics/metrics_pref_names.h"
18 #include "components/metrics/metrics_provider.h"
19 #include "components/metrics/metrics_service.h"
20 #include "components/metrics/metrics_state_manager.h"
21 #include "components/metrics/net/net_metrics_log_uploader.h"
22 #include "components/metrics/net/network_metrics_provider.h"
23 #include "components/metrics/profiler/profiler_metrics_provider.h"
24 #include "components/metrics/url_constants.h"
25 #include "content/public/common/content_switches.h"
27 #if defined(OS_LINUX)
28 #include "chromecast/browser/metrics/external_metrics.h"
29 #endif // defined(OS_LINUX)
31 namespace chromecast {
32 namespace metrics {
34 namespace {
35 const int kStandardUploadIntervalMinutes = 5;
36 } // namespace
38 // static
39 scoped_ptr<CastMetricsServiceClient> CastMetricsServiceClient::Create(
40 base::TaskRunner* io_task_runner,
41 PrefService* pref_service,
42 net::URLRequestContextGetter* request_context) {
43 return make_scoped_ptr(new CastMetricsServiceClient(io_task_runner,
44 pref_service,
45 request_context));
48 void CastMetricsServiceClient::SetMetricsClientId(
49 const std::string& client_id) {
50 client_id_ = client_id;
51 LOG(INFO) << "Metrics client ID set: " << client_id;
52 PlatformSetClientID(cast_service_, client_id);
55 void CastMetricsServiceClient::OnRecordingDisabled() {
58 void CastMetricsServiceClient::StoreClientInfo(
59 const ::metrics::ClientInfo& client_info) {
60 const std::string& client_id = client_info.client_id;
61 DCHECK(client_id.empty() || base::IsValidGUID(client_id));
62 // backup client_id or reset to empty.
63 SetMetricsClientId(client_id);
66 scoped_ptr< ::metrics::ClientInfo> CastMetricsServiceClient::LoadClientInfo() {
67 scoped_ptr< ::metrics::ClientInfo> client_info(new ::metrics::ClientInfo);
69 // kMetricsIsNewClientID would be missing if either the device was just
70 // FDR'ed, or it is on pre-v1.2 build.
71 if (!pref_service_->GetBoolean(prefs::kMetricsIsNewClientID)) {
72 // If the old client id exists, the device must be on pre-v1.2 build,
73 // instead of just being FDR'ed.
74 if (!pref_service_->GetString(::metrics::prefs::kMetricsOldClientID)
75 .empty()) {
76 // Force old client id to be regenerated. See b/9487011.
77 client_info->client_id = base::GenerateGUID();
78 pref_service_->SetBoolean(prefs::kMetricsIsNewClientID, true);
79 return client_info.Pass();
81 // else the device was just FDR'ed, pass through.
84 const std::string client_id(GetPlatformClientID(cast_service_));
85 if (!client_id.empty() && base::IsValidGUID(client_id)) {
86 client_info->client_id = client_id;
87 return client_info.Pass();
88 } else {
89 if (client_id.empty()) {
90 LOG(WARNING) << "Empty client id from platform,"
91 << " assuming this is the first boot up of a new device.";
92 } else {
93 LOG(ERROR) << "Invalid client id " << client_id << " from platform.";
95 return scoped_ptr< ::metrics::ClientInfo>();
101 bool CastMetricsServiceClient::IsOffTheRecordSessionActive() {
102 // Chromecast behaves as "off the record" w/r/t recording browsing state,
103 // but this value is about not disabling metrics because of it.
104 return false;
107 int32_t CastMetricsServiceClient::GetProduct() {
108 // Chromecast currently uses the same product identifier as Chrome.
109 return ::metrics::ChromeUserMetricsExtension::CHROME;
112 std::string CastMetricsServiceClient::GetApplicationLocale() {
113 return base::i18n::GetConfiguredLocale();
116 bool CastMetricsServiceClient::GetBrand(std::string* brand_code) {
117 return false;
120 ::metrics::SystemProfileProto::Channel CastMetricsServiceClient::GetChannel() {
121 return GetPlatformReleaseChannel(cast_service_);
124 std::string CastMetricsServiceClient::GetVersionString() {
125 return GetPlatformVersionString(cast_service_);
128 void CastMetricsServiceClient::OnLogUploadComplete() {
129 PlatformOnLogUploadComplete(cast_service_);
132 void CastMetricsServiceClient::StartGatheringMetrics(
133 const base::Closure& done_callback) {
134 done_callback.Run();
137 void CastMetricsServiceClient::CollectFinalMetrics(
138 const base::Closure& done_callback) {
139 done_callback.Run();
142 scoped_ptr< ::metrics::MetricsLogUploader>
143 CastMetricsServiceClient::CreateUploader(
144 const base::Callback<void(int)>& on_upload_complete) {
145 std::string uma_server_url(::metrics::kDefaultMetricsServerUrl);
146 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
147 if (command_line->HasSwitch(switches::kOverrideMetricsUploadUrl)) {
148 uma_server_url.assign(
149 command_line->GetSwitchValueASCII(switches::kOverrideMetricsUploadUrl));
151 DCHECK(!uma_server_url.empty());
152 return scoped_ptr< ::metrics::MetricsLogUploader>(
153 new ::metrics::NetMetricsLogUploader(
154 request_context_,
155 uma_server_url,
156 ::metrics::kDefaultMetricsMimeType,
157 on_upload_complete));
160 base::TimeDelta CastMetricsServiceClient::GetStandardUploadInterval() {
161 return base::TimeDelta::FromMinutes(kStandardUploadIntervalMinutes);
164 void CastMetricsServiceClient::EnableMetricsService(bool enabled) {
165 if (!metrics_service_loop_->BelongsToCurrentThread()) {
166 metrics_service_loop_->PostTask(
167 FROM_HERE,
168 base::Bind(&CastMetricsServiceClient::EnableMetricsService,
169 base::Unretained(this),
170 enabled));
171 return;
174 if (enabled) {
175 metrics_service_->Start();
176 } else {
177 metrics_service_->Stop();
181 CastMetricsServiceClient::CastMetricsServiceClient(
182 base::TaskRunner* io_task_runner,
183 PrefService* pref_service,
184 net::URLRequestContextGetter* request_context)
185 : io_task_runner_(io_task_runner),
186 pref_service_(pref_service),
187 cast_service_(NULL),
188 #if !defined(OS_ANDROID)
189 external_metrics_(NULL),
190 #endif // !defined(OS_ANDROID)
191 metrics_service_loop_(base::MessageLoopProxy::current()),
192 request_context_(request_context) {
195 CastMetricsServiceClient::~CastMetricsServiceClient() {
196 #if !defined(OS_ANDROID)
197 DCHECK(!external_metrics_);
198 #endif // !defined(OS_ANDROID)
201 void CastMetricsServiceClient::Initialize(CastService* cast_service) {
202 DCHECK(cast_service);
203 DCHECK(!cast_service_);
204 cast_service_ = cast_service;
206 metrics_state_manager_ = ::metrics::MetricsStateManager::Create(
207 pref_service_,
208 base::Bind(&CastMetricsServiceClient::IsReportingEnabled,
209 base::Unretained(this)),
210 base::Bind(&CastMetricsServiceClient::StoreClientInfo,
211 base::Unretained(this)),
212 base::Bind(&CastMetricsServiceClient::LoadClientInfo,
213 base::Unretained(this)));
214 metrics_service_.reset(new ::metrics::MetricsService(
215 metrics_state_manager_.get(),
216 this,
217 pref_service_));
219 // Always create a client id as it may also be used by crash reporting,
220 // (indirectly) included in feedback, and can be queried during setup.
221 // For UMA and crash reporting, associated opt-in settings will control
222 // sending reports as directed by the user.
223 // For Setup (which also communicates the user's opt-in preferences),
224 // report the client-id and expect that setup will handle the current opt-in
225 // value.
226 metrics_state_manager_->ForceClientIdCreation();
228 CastStabilityMetricsProvider* stability_provider =
229 new CastStabilityMetricsProvider(metrics_service_.get());
230 metrics_service_->RegisterMetricsProvider(
231 scoped_ptr< ::metrics::MetricsProvider>(stability_provider));
233 base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
234 if (!command_line->HasSwitch(switches::kDisableGpu)) {
235 metrics_service_->RegisterMetricsProvider(
236 scoped_ptr< ::metrics::MetricsProvider>(
237 new ::metrics::GPUMetricsProvider));
239 metrics_service_->RegisterMetricsProvider(
240 scoped_ptr< ::metrics::MetricsProvider>(
241 new ::metrics::NetworkMetricsProvider(io_task_runner_)));
242 metrics_service_->RegisterMetricsProvider(
243 scoped_ptr< ::metrics::MetricsProvider>(
244 new ::metrics::ProfilerMetricsProvider));
245 RegisterPlatformMetricsProviders(metrics_service_.get(), cast_service_);
247 metrics_service_->InitializeMetricsRecordingState();
248 #if !defined(OS_ANDROID)
249 // Reset clean_shutdown bit after InitializeMetricsRecordingState().
250 metrics_service_->LogNeedForCleanShutdown();
251 #endif // !defined(OS_ANDROID)
253 if (IsReportingEnabled())
254 metrics_service_->Start();
256 // Start external metrics collection, which feeds data from external
257 // processes into the main external metrics.
258 #if defined(OS_LINUX)
259 external_metrics_ = new ExternalMetrics(stability_provider);
260 external_metrics_->Start();
261 #endif // defined(OS_LINUX)
264 void CastMetricsServiceClient::Finalize() {
265 #if !defined(OS_ANDROID)
266 // Set clean_shutdown bit.
267 metrics_service_->RecordCompletedSessionEnd();
268 #endif // !defined(OS_ANDROID)
270 // Stop metrics service cleanly before destructing CastMetricsServiceClient.
271 #if defined(OS_LINUX)
272 external_metrics_->StopAndDestroy();
273 external_metrics_ = NULL;
274 #endif // defined(OS_LINUX)
275 metrics_service_->Stop();
278 bool CastMetricsServiceClient::IsReportingEnabled() {
279 return PlatformIsReportingEnabled(cast_service_);
282 } // namespace metrics
283 } // namespace chromecast