11320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Copyright 2014 The Chromium Authors. All rights reserved.
21320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// Use of this source code is governed by a BSD-style license that can be
31320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// found in the LICENSE file.
41320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/metrics/cast_metrics_service_client.h"
61320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
71320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/command_line.h"
81320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "base/i18n/rtl.h"
91320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/common/chromecast_config.h"
101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/common/chromecast_switches.h"
111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "chromecast/metrics/platform_metrics_providers.h"
121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/client_info.h"
131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/gpu/gpu_metrics_provider.h"
141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/metrics_provider.h"
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/metrics_service.h"
161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/metrics_state_manager.h"
171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/net/net_metrics_log_uploader.h"
181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/net/network_metrics_provider.h"
191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/metrics/profiler/profiler_metrics_provider.h"
201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace chromecast {
221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace metrics {
231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace {
251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid StoreClientInfo(const ::metrics::ClientInfo& client_info) {
271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciscoped_ptr<::metrics::ClientInfo> LoadClientInfo() {
301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return scoped_ptr<::metrics::ClientInfo>();
311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace
341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci// static
361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciCastMetricsServiceClient* CastMetricsServiceClient::Create(
371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::TaskRunner* io_task_runner,
381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PrefService* pref_service,
391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    net::URLRequestContextGetter* request_context) {
401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return new CastMetricsServiceClient(io_task_runner,
411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                      pref_service,
421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                                      request_context);
431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CastMetricsServiceClient::SetMetricsClientId(
461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& client_id) {
471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  LOG(INFO) << "Metrics client ID set: " << client_id;
481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PlatformSetClientID(client_id);
491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool CastMetricsServiceClient::IsOffTheRecordSessionActive() {
521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Chromecast behaves as "off the record" w/r/t recording browsing state,
531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // but this value is about not disabling metrics because of it.
541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return false;
551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccistd::string CastMetricsServiceClient::GetApplicationLocale() {
581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return base::i18n::GetConfiguredLocale();
591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool CastMetricsServiceClient::GetBrand(std::string* brand_code) {
621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return false;
631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
651320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci::metrics::SystemProfileProto::Channel CastMetricsServiceClient::GetChannel() {
661320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return GetPlatformReleaseChannel();
671320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
681320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
691320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccistd::string CastMetricsServiceClient::GetVersionString() {
701320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return GetPlatformVersionString();
711320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
721320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
731320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CastMetricsServiceClient::OnLogUploadComplete() {
741320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  PlatformOnLogUploadComplete();
751320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
761320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
771320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CastMetricsServiceClient::StartGatheringMetrics(
781320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const base::Closure& done_callback) {
791320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  done_callback.Run();
801320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
811320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
821320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CastMetricsServiceClient::CollectFinalMetrics(
831320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const base::Closure& done_callback) {
841320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  done_callback.Run();
851320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
861320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
871320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciscoped_ptr< ::metrics::MetricsLogUploader>
881320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciCastMetricsServiceClient::CreateUploader(
891320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& server_url,
901320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const std::string& mime_type,
911320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    const base::Callback<void(int)>& on_upload_complete) {
921320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  std::string uma_server_url(server_url);
931320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
941320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (command_line->HasSwitch(switches::kOverrideMetricsUploadUrl)) {
951320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    uma_server_url.assign(
961320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci        command_line->GetSwitchValueASCII(switches::kOverrideMetricsUploadUrl));
971320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
981320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  DCHECK(!uma_server_url.empty());
991320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return scoped_ptr< ::metrics::MetricsLogUploader>(
1001320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      new ::metrics::NetMetricsLogUploader(
1011320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          request_context_,
1021320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          uma_server_url,
1031320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          mime_type,
1041320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          on_upload_complete));
1051320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1061320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1071320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccivoid CastMetricsServiceClient::EnableMetricsService(bool enabled) {
1081320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (enabled) {
1091320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    metrics_service_->Start();
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  } else {
1111320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    metrics_service_->Stop();
1121320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  }
1131320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1141320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciCastMetricsServiceClient::CastMetricsServiceClient(
1161320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    base::TaskRunner* io_task_runner,
1171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    PrefService* pref_service,
1181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    net::URLRequestContextGetter* request_context)
1191320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    : metrics_state_manager_(::metrics::MetricsStateManager::Create(
1201320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          pref_service,
1211320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          base::Bind(&CastMetricsServiceClient::IsReportingEnabled,
1221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     base::Unretained(this)),
1231320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     base::Bind(&StoreClientInfo),
1241320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci                     base::Bind(&LoadClientInfo))),
1251320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      metrics_service_(new ::metrics::MetricsService(
1261320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          metrics_state_manager_.get(),
1271320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          this,
1281320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          pref_service)),
1291320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      request_context_(request_context) {
1301320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // Always create a client id as it may also be used by crash reporting,
1311320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // (indirectly) included in feedback, and can be queried during setup.
1321320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // For UMA and crash reporting, associated opt-in settings will control
1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // sending reports as directed by the user.
1341320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // For Setup (which also communicates the user's opt-in preferences),
1351320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // report the client-id and expect that setup will handle the current opt-in
1361320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  // value.
1371320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  metrics_state_manager_->ForceClientIdCreation();
1381320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1391320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  metrics_service_->RegisterMetricsProvider(
1401320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_ptr< ::metrics::MetricsProvider>(
1411320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          new ::metrics::GPUMetricsProvider));
1421320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  metrics_service_->RegisterMetricsProvider(
1431320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_ptr< ::metrics::MetricsProvider>(
1441320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          new NetworkMetricsProvider(io_task_runner)));
1451320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  metrics_service_->RegisterMetricsProvider(
1461320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci      scoped_ptr< ::metrics::MetricsProvider>(
1471320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci          new ::metrics::ProfilerMetricsProvider));
1481320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  RegisterPlatformMetricsProviders(metrics_service_.get());
1491320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1501320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  metrics_service_->InitializeMetricsRecordingState();
1511320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1521320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  if (IsReportingEnabled())
1531320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci    metrics_service_->Start();
1541320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1551320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1561320f92c476a1ad9d19dba2a48c72b75566198e9Primiano TucciCastMetricsServiceClient::~CastMetricsServiceClient() {
1571320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1581320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1591320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccibool CastMetricsServiceClient::IsReportingEnabled() {
1601320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci  return PlatformIsReportingEnabled();
1611320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}
1621320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci
1631320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace metrics
1641320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace chromecast
165