summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/metrics_private/metrics_private_api.cc
blob: 141e926e543ef63658382cfcc32db41e7bf19463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/metrics_private/metrics_private_api.h"

#include <limits.h>

#include <algorithm>

#include "base/metrics/field_trial.h"
#include "base/metrics/histogram.h"
#include "base/metrics/sparse_histogram.h"
#include "chrome/browser/metrics/chrome_metrics_service_accessor.h"
#include "chrome/common/extensions/api/metrics_private.h"
#include "components/variations/variations_associated_data.h"
#include "content/public/browser/user_metrics.h"
#include "extensions/common/extension.h"

namespace extensions {

namespace GetIsCrashReportingEnabled =
    api::metrics_private::GetIsCrashReportingEnabled;
namespace GetVariationParams = api::metrics_private::GetVariationParams;
namespace GetFieldTrial = api::metrics_private::GetFieldTrial;
namespace RecordUserAction = api::metrics_private::RecordUserAction;
namespace RecordValue = api::metrics_private::RecordValue;
namespace RecordSparseValue = api::metrics_private::RecordSparseValue;
namespace RecordPercentage = api::metrics_private::RecordPercentage;
namespace RecordCount = api::metrics_private::RecordCount;
namespace RecordSmallCount = api::metrics_private::RecordSmallCount;
namespace RecordMediumCount = api::metrics_private::RecordMediumCount;
namespace RecordTime = api::metrics_private::RecordTime;
namespace RecordMediumTime = api::metrics_private::RecordMediumTime;
namespace RecordLongTime = api::metrics_private::RecordLongTime;

namespace {

const size_t kMaxBuckets = 10000; // We don't ever want more than these many
                                  // buckets; there is no real need for them
                                  // and would cause crazy memory usage
} // namespace

bool MetricsPrivateGetIsCrashReportingEnabledFunction::RunSync() {
  SetResult(new base::FundamentalValue(
      ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled()));
  return true;
}

bool MetricsPrivateGetFieldTrialFunction::RunSync() {
  std::string name;
  EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name));

  SetResult(new base::StringValue(base::FieldTrialList::FindFullName(name)));
  return true;
}

bool MetricsPrivateGetVariationParamsFunction::RunSync() {
  scoped_ptr<GetVariationParams::Params> params(
      GetVariationParams::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());

  GetVariationParams::Results::Params result;
  if (variations::GetVariationParams(params->name,
                                     &result.additional_properties)) {
    SetResult(result.ToValue().release());
  }
  return true;
}

bool MetricsPrivateRecordUserActionFunction::RunSync() {
  scoped_ptr<RecordUserAction::Params> params(
      RecordUserAction::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());

  content::RecordComputedAction(params->name);
  return true;
}

bool MetricsHistogramHelperFunction::RecordValue(
    const std::string& name,
    base::HistogramType type,
    int min, int max, size_t buckets,
    int sample) {
  // Make sure toxic values don't get to internal code.
  // Fix for maximums
  min = std::min(min, INT_MAX - 3);
  max = std::min(max, INT_MAX - 3);
  buckets = std::min(buckets, kMaxBuckets);
  // Fix for minimums.
  min = std::max(min, 1);
  max = std::max(max, min + 1);
  buckets = std::max(buckets, static_cast<size_t>(3));
  // Trim buckets down to a maximum of the given range + over/underflow buckets
  if (buckets > static_cast<size_t>(max - min + 2))
    buckets = max - min + 2;

  base::HistogramBase* counter;
  if (type == base::LINEAR_HISTOGRAM) {
    counter = base::LinearHistogram::FactoryGet(
        name, min, max, buckets,
        base::HistogramBase::kUmaTargetedHistogramFlag);
  } else {
    counter = base::Histogram::FactoryGet(
        name, min, max, buckets,
        base::HistogramBase::kUmaTargetedHistogramFlag);
  }

  // The histogram can be NULL if it is constructed with bad arguments.  Ignore
  // that data for this API.  An error message will be logged.
  if (counter)
    counter->Add(sample);
  return true;
}

bool MetricsPrivateRecordValueFunction::RunSync() {
  scoped_ptr<RecordValue::Params> params(RecordValue::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());

  // Get the histogram parameters from the metric type object.
  std::string type = api::metrics_private::ToString(params->metric.type);

  base::HistogramType histogram_type(type == "histogram-linear" ?
      base::LINEAR_HISTOGRAM : base::HISTOGRAM);
  return RecordValue(params->metric.metric_name, histogram_type,
                     params->metric.min, params->metric.max,
                     params->metric.buckets, params->value);
}

bool MetricsPrivateRecordSparseValueFunction::RunSync() {
  scoped_ptr<RecordSparseValue::Params> params(
      RecordSparseValue::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());
  // This particular UMA_HISTOGRAM_ macro is okay for
  // non-runtime-constant strings.
  UMA_HISTOGRAM_SPARSE_SLOWLY(params->metric_name, params->value);
  return true;
}

bool MetricsPrivateRecordPercentageFunction::RunSync() {
  scoped_ptr<RecordPercentage::Params> params(
      RecordPercentage::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());
  return RecordValue(params->metric_name, base::LINEAR_HISTOGRAM,
                     1, 101, 102, params->value);
}

bool MetricsPrivateRecordCountFunction::RunSync() {
  scoped_ptr<RecordCount::Params> params(RecordCount::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());
  return RecordValue(params->metric_name, base::HISTOGRAM,
                     1, 1000000, 50, params->value);
}

bool MetricsPrivateRecordSmallCountFunction::RunSync() {
  scoped_ptr<RecordSmallCount::Params> params(
      RecordSmallCount::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());
  return RecordValue(params->metric_name, base::HISTOGRAM,
                     1, 100, 50, params->value);
}

bool MetricsPrivateRecordMediumCountFunction::RunSync() {
  scoped_ptr<RecordMediumCount::Params> params(
      RecordMediumCount::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());
  return RecordValue(params->metric_name, base::HISTOGRAM,
                     1, 10000, 50, params->value);
}

bool MetricsPrivateRecordTimeFunction::RunSync() {
  scoped_ptr<RecordTime::Params> params(RecordTime::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());
  static const int kTenSecMs = 10 * 1000;
  return RecordValue(params->metric_name, base::HISTOGRAM,
                     1, kTenSecMs, 50, params->value);
}

bool MetricsPrivateRecordMediumTimeFunction::RunSync() {
  scoped_ptr<RecordMediumTime::Params> params(
      RecordMediumTime::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());
  static const int kThreeMinMs = 3 * 60 * 1000;
  return RecordValue(params->metric_name, base::HISTOGRAM,
                     1, kThreeMinMs, 50, params->value);
}

bool MetricsPrivateRecordLongTimeFunction::RunSync() {
  scoped_ptr<RecordLongTime::Params> params(
      RecordLongTime::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());
  static const int kOneHourMs = 60 * 60 * 1000;
  return RecordValue(params->metric_name, base::HISTOGRAM,
                     1, kOneHourMs, 50, params->value);
}

} // namespace extensions