Add CryptAuthDeviceManager for syncing the user's devices from CryptAuth.
[chromium-blink-merge.git] / chromecast / browser / metrics / external_metrics.cc
blobf26e0ccd9606e5c5d09e28b5ce8bdadfcbfe81ca
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/external_metrics.h"
7 #include <string>
9 #include "base/bind.h"
10 #include "base/files/file_path.h"
11 #include "base/files/file_util.h"
12 #include "base/metrics/histogram.h"
13 #include "base/metrics/sparse_histogram.h"
14 #include "base/metrics/statistics_recorder.h"
15 #include "base/timer/elapsed_timer.h"
16 #include "chromecast/base/metrics/cast_histograms.h"
17 #include "chromecast/base/metrics/cast_metrics_helper.h"
18 #include "chromecast/browser/metrics/cast_stability_metrics_provider.h"
19 #include "components/metrics/metrics_service.h"
20 #include "components/metrics/serialization/metric_sample.h"
21 #include "components/metrics/serialization/serialization_utils.h"
22 #include "content/public/browser/browser_thread.h"
24 namespace chromecast {
25 namespace metrics {
27 namespace {
29 bool CheckValues(const std::string& name,
30 int minimum,
31 int maximum,
32 size_t bucket_count) {
33 if (!base::Histogram::InspectConstructionArguments(
34 name, &minimum, &maximum, &bucket_count))
35 return false;
36 base::HistogramBase* histogram =
37 base::StatisticsRecorder::FindHistogram(name);
38 if (!histogram)
39 return true;
40 return histogram->HasConstructionArguments(minimum, maximum, bucket_count);
43 bool CheckLinearValues(const std::string& name, int maximum) {
44 return CheckValues(name, 1, maximum, maximum + 1);
47 } // namespace
49 // The interval between external metrics collections in seconds
50 static const int kExternalMetricsCollectionIntervalSeconds = 30;
51 const char kEventsFilePath[] = "/data/share/chrome/metrics/uma-events";
53 ExternalMetrics::ExternalMetrics(
54 CastStabilityMetricsProvider* stability_provider)
55 : stability_provider_(stability_provider),
56 uma_events_file_(kEventsFilePath),
57 weak_factory_(this) {
58 DCHECK(stability_provider);
61 ExternalMetrics::~ExternalMetrics() {
64 void ExternalMetrics::StopAndDestroy() {
65 content::BrowserThread::DeleteSoon(
66 content::BrowserThread::FILE, FROM_HERE, this);
69 void ExternalMetrics::Start() {
70 ScheduleCollector();
73 void ExternalMetrics::RecordCrash(const std::string& crash_kind) {
74 content::BrowserThread::PostTask(
75 content::BrowserThread::UI, FROM_HERE,
76 base::Bind(&CastStabilityMetricsProvider::LogExternalCrash,
77 base::Unretained(stability_provider_),
78 crash_kind));
81 void ExternalMetrics::RecordSparseHistogram(
82 const ::metrics::MetricSample& sample) {
83 CHECK_EQ(::metrics::MetricSample::SPARSE_HISTOGRAM, sample.type());
84 base::HistogramBase* counter = base::SparseHistogram::FactoryGet(
85 sample.name(), base::HistogramBase::kUmaTargetedHistogramFlag);
86 counter->Add(sample.sample());
89 int ExternalMetrics::CollectEvents() {
90 ScopedVector< ::metrics::MetricSample> samples;
91 ::metrics::SerializationUtils::ReadAndTruncateMetricsFromFile(
92 uma_events_file_, &samples);
94 for (ScopedVector< ::metrics::MetricSample>::iterator it = samples.begin();
95 it != samples.end();
96 ++it) {
97 const ::metrics::MetricSample& sample = **it;
99 switch (sample.type()) {
100 case ::metrics::MetricSample::CRASH:
101 RecordCrash(sample.name());
102 break;
103 case ::metrics::MetricSample::USER_ACTION:
104 CastMetricsHelper::GetInstance()->RecordSimpleAction(sample.name());
105 break;
106 case ::metrics::MetricSample::HISTOGRAM:
107 if (!CheckValues(sample.name(), sample.min(), sample.max(),
108 sample.bucket_count())) {
109 DLOG(ERROR) << "Invalid histogram: " << sample.name();
110 break;
112 UMA_HISTOGRAM_CUSTOM_COUNTS_NO_CACHE(
113 sample.name(), sample.sample(), sample.min(), sample.max(),
114 sample.bucket_count());
115 break;
116 case ::metrics::MetricSample::LINEAR_HISTOGRAM:
117 if (!CheckLinearValues(sample.name(), sample.max())) {
118 DLOG(ERROR) << "Invalid linear histogram: " << sample.name();
119 break;
121 UMA_HISTOGRAM_ENUMERATION_NO_CACHE(
122 sample.name(), sample.sample(), sample.max());
123 break;
124 case ::metrics::MetricSample::SPARSE_HISTOGRAM:
125 RecordSparseHistogram(sample);
126 break;
130 return samples.size();
133 void ExternalMetrics::CollectEventsAndReschedule() {
134 base::ElapsedTimer timer;
135 CollectEvents();
136 UMA_HISTOGRAM_TIMES("UMA.CollectExternalEventsTime", timer.Elapsed());
137 ScheduleCollector();
140 void ExternalMetrics::ScheduleCollector() {
141 bool result = content::BrowserThread::PostDelayedTask(
142 content::BrowserThread::FILE,
143 FROM_HERE,
144 base::Bind(&ExternalMetrics::CollectEventsAndReschedule,
145 weak_factory_.GetWeakPtr()),
146 base::TimeDelta::FromSeconds(kExternalMetricsCollectionIntervalSeconds));
147 DCHECK(result);
150 } // namespace metrics
151 } // namespace chromecast