1 // Copyright 2019 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.
4 
5 #include "chrome/browser/tracing/background_tracing_metrics_provider.h"
6 
7 #include <utility>
8 
9 #include "base/strings/string_piece.h"
10 #include "base/time/time.h"
11 #include "components/metrics/field_trials_provider.h"
12 #include "content/public/browser/background_tracing_manager.h"
13 #include "third_party/metrics_proto/chrome_user_metrics_extension.pb.h"
14 #include "third_party/metrics_proto/trace_log.pb.h"
15 
16 namespace tracing {
17 
BackgroundTracingMetricsProvider()18 BackgroundTracingMetricsProvider::BackgroundTracingMetricsProvider() {}
~BackgroundTracingMetricsProvider()19 BackgroundTracingMetricsProvider::~BackgroundTracingMetricsProvider() {}
20 
Init()21 void BackgroundTracingMetricsProvider::Init() {
22   // TODO(ssid): SetupBackgroundTracingFieldTrial() should be called here.
23 }
24 
HasIndependentMetrics()25 bool BackgroundTracingMetricsProvider::HasIndependentMetrics() {
26   return content::BackgroundTracingManager::GetInstance()->HasTraceToUpload();
27 }
28 
ProvideIndependentMetrics(base::OnceCallback<void (bool)> done_callback,metrics::ChromeUserMetricsExtension * uma_proto,base::HistogramSnapshotManager * snapshot_manager)29 void BackgroundTracingMetricsProvider::ProvideIndependentMetrics(
30     base::OnceCallback<void(bool)> done_callback,
31     metrics::ChromeUserMetricsExtension* uma_proto,
32     base::HistogramSnapshotManager* snapshot_manager) {
33   auto* tracing_manager = content::BackgroundTracingManager::GetInstance();
34   auto serialized_trace = tracing_manager->GetLatestTraceToUpload();
35   if (serialized_trace.empty()) {
36     std::move(done_callback).Run(false);
37     return;
38   }
39   metrics::TraceLog* log = uma_proto->add_trace_log();
40   log->set_raw_data(std::move(serialized_trace));
41 
42   // TODO(ssid): Find a better way to record other system profile metrics in
43   // independent providers.
44   variations::FieldTrialsProvider provider(nullptr, base::StringPiece());
45   provider.ProvideSystemProfileMetricsWithLogCreationTime(
46       base::TimeTicks(), uma_proto->mutable_system_profile());
47 
48   std::move(done_callback).Run(true);
49 }
50 
51 }  // namespace tracing
52