Enable Enterprise enrollment on desktop builds.
[chromium-blink-merge.git] / chrome / browser / extensions / api / metrics_private / metrics_private_api.cc
blob0b344442580e4bc321f4dc996a41131c8b9a4a20
1 // Copyright (c) 2012 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 "chrome/browser/extensions/api/metrics_private/metrics_private_api.h"
7 #include <algorithm>
9 #include "base/metrics/field_trial.h"
10 #include "base/metrics/histogram.h"
11 #include "base/metrics/sparse_histogram.h"
12 #include "chrome/browser/metrics/metrics_service.h"
13 #include "chrome/common/extensions/api/metrics_private.h"
14 #include "components/variations/variations_associated_data.h"
15 #include "content/public/browser/user_metrics.h"
16 #include "extensions/common/extension.h"
18 namespace extensions {
20 namespace GetIsCrashReportingEnabled =
21 api::metrics_private::GetIsCrashReportingEnabled;
22 namespace GetVariationParams = api::metrics_private::GetVariationParams;
23 namespace GetFieldTrial = api::metrics_private::GetFieldTrial;
24 namespace RecordUserAction = api::metrics_private::RecordUserAction;
25 namespace RecordValue = api::metrics_private::RecordValue;
26 namespace RecordSparseValue = api::metrics_private::RecordSparseValue;
27 namespace RecordPercentage = api::metrics_private::RecordPercentage;
28 namespace RecordCount = api::metrics_private::RecordCount;
29 namespace RecordSmallCount = api::metrics_private::RecordSmallCount;
30 namespace RecordMediumCount = api::metrics_private::RecordMediumCount;
31 namespace RecordTime = api::metrics_private::RecordTime;
32 namespace RecordMediumTime = api::metrics_private::RecordMediumTime;
33 namespace RecordLongTime = api::metrics_private::RecordLongTime;
35 namespace {
37 const size_t kMaxBuckets = 10000; // We don't ever want more than these many
38 // buckets; there is no real need for them
39 // and would cause crazy memory usage
40 } // namespace
42 bool MetricsPrivateGetIsCrashReportingEnabledFunction::RunSync() {
43 SetResult(new base::FundamentalValue(
44 MetricsServiceHelper::IsCrashReportingEnabled()));
45 return true;
48 bool MetricsPrivateGetFieldTrialFunction::RunSync() {
49 std::string name;
50 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name));
52 SetResult(new base::StringValue(base::FieldTrialList::FindFullName(name)));
53 return true;
56 bool MetricsPrivateGetVariationParamsFunction::RunSync() {
57 scoped_ptr<GetVariationParams::Params> params(
58 GetVariationParams::Params::Create(*args_));
59 EXTENSION_FUNCTION_VALIDATE(params.get());
61 GetVariationParams::Results::Params result;
62 if (!chrome_variations::GetVariationParams(
63 params->name, &result.additional_properties)) {
64 SetError("Variation parameters are unavailable.");
65 return false;
68 SetResult(result.ToValue().release());
69 return true;
72 bool MetricsPrivateRecordUserActionFunction::RunSync() {
73 scoped_ptr<RecordUserAction::Params> params(
74 RecordUserAction::Params::Create(*args_));
75 EXTENSION_FUNCTION_VALIDATE(params.get());
77 content::RecordComputedAction(params->name);
78 return true;
81 bool MetricsHistogramHelperFunction::RecordValue(
82 const std::string& name,
83 base::HistogramType type,
84 int min, int max, size_t buckets,
85 int sample) {
86 // Make sure toxic values don't get to internal code.
87 // Fix for maximums
88 min = std::min(min, INT_MAX - 3);
89 max = std::min(max, INT_MAX - 3);
90 buckets = std::min(buckets, kMaxBuckets);
91 // Fix for minimums.
92 min = std::max(min, 1);
93 max = std::max(max, min + 1);
94 buckets = std::max(buckets, static_cast<size_t>(3));
95 // Trim buckets down to a maximum of the given range + over/underflow buckets
96 if (buckets > static_cast<size_t>(max - min + 2))
97 buckets = max - min + 2;
99 base::HistogramBase* counter;
100 if (type == base::LINEAR_HISTOGRAM) {
101 counter = base::LinearHistogram::FactoryGet(
102 name, min, max, buckets,
103 base::HistogramBase::kUmaTargetedHistogramFlag);
104 } else {
105 counter = base::Histogram::FactoryGet(
106 name, min, max, buckets,
107 base::HistogramBase::kUmaTargetedHistogramFlag);
110 // The histogram can be NULL if it is constructed with bad arguments. Ignore
111 // that data for this API. An error message will be logged.
112 if (counter)
113 counter->Add(sample);
114 return true;
117 bool MetricsPrivateRecordValueFunction::RunSync() {
118 scoped_ptr<RecordValue::Params> params(RecordValue::Params::Create(*args_));
119 EXTENSION_FUNCTION_VALIDATE(params.get());
121 // Get the histogram parameters from the metric type object.
122 std::string type = api::metrics_private::MetricType::ToString(
123 params->metric.type);
125 base::HistogramType histogram_type(type == "histogram-linear" ?
126 base::LINEAR_HISTOGRAM : base::HISTOGRAM);
127 return RecordValue(params->metric.metric_name, histogram_type,
128 params->metric.min, params->metric.max,
129 params->metric.buckets, params->value);
132 bool MetricsPrivateRecordSparseValueFunction::RunSync() {
133 scoped_ptr<RecordSparseValue::Params> params(
134 RecordSparseValue::Params::Create(*args_));
135 EXTENSION_FUNCTION_VALIDATE(params.get());
136 // This particular UMA_HISTOGRAM_ macro is okay for
137 // non-runtime-constant strings.
138 UMA_HISTOGRAM_SPARSE_SLOWLY(params->metric_name, params->value);
139 return true;
142 bool MetricsPrivateRecordPercentageFunction::RunSync() {
143 scoped_ptr<RecordPercentage::Params> params(
144 RecordPercentage::Params::Create(*args_));
145 EXTENSION_FUNCTION_VALIDATE(params.get());
146 return RecordValue(params->metric_name, base::LINEAR_HISTOGRAM,
147 1, 101, 102, params->value);
150 bool MetricsPrivateRecordCountFunction::RunSync() {
151 scoped_ptr<RecordCount::Params> params(RecordCount::Params::Create(*args_));
152 EXTENSION_FUNCTION_VALIDATE(params.get());
153 return RecordValue(params->metric_name, base::HISTOGRAM,
154 1, 1000000, 50, params->value);
157 bool MetricsPrivateRecordSmallCountFunction::RunSync() {
158 scoped_ptr<RecordSmallCount::Params> params(
159 RecordSmallCount::Params::Create(*args_));
160 EXTENSION_FUNCTION_VALIDATE(params.get());
161 return RecordValue(params->metric_name, base::HISTOGRAM,
162 1, 100, 50, params->value);
165 bool MetricsPrivateRecordMediumCountFunction::RunSync() {
166 scoped_ptr<RecordMediumCount::Params> params(
167 RecordMediumCount::Params::Create(*args_));
168 EXTENSION_FUNCTION_VALIDATE(params.get());
169 return RecordValue(params->metric_name, base::HISTOGRAM,
170 1, 10000, 50, params->value);
173 bool MetricsPrivateRecordTimeFunction::RunSync() {
174 scoped_ptr<RecordTime::Params> params(RecordTime::Params::Create(*args_));
175 EXTENSION_FUNCTION_VALIDATE(params.get());
176 static const int kTenSecMs = 10 * 1000;
177 return RecordValue(params->metric_name, base::HISTOGRAM,
178 1, kTenSecMs, 50, params->value);
181 bool MetricsPrivateRecordMediumTimeFunction::RunSync() {
182 scoped_ptr<RecordMediumTime::Params> params(
183 RecordMediumTime::Params::Create(*args_));
184 EXTENSION_FUNCTION_VALIDATE(params.get());
185 static const int kThreeMinMs = 3 * 60 * 1000;
186 return RecordValue(params->metric_name, base::HISTOGRAM,
187 1, kThreeMinMs, 50, params->value);
190 bool MetricsPrivateRecordLongTimeFunction::RunSync() {
191 scoped_ptr<RecordLongTime::Params> params(
192 RecordLongTime::Params::Create(*args_));
193 EXTENSION_FUNCTION_VALIDATE(params.get());
194 static const int kOneHourMs = 60 * 60 * 1000;
195 return RecordValue(params->metric_name, base::HISTOGRAM,
196 1, kOneHourMs, 50, params->value);
199 } // namespace extensions