OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "chrome/browser/extensions/extension_metrics_module.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/metrics/histogram.h" | |
10 #include "chrome/common/extensions/extension.h" | |
11 #include "content/public/browser/user_metrics.h" | |
12 | |
13 using base::Histogram; | |
14 using base::LinearHistogram; | |
15 using content::UserMetricsAction; | |
16 | |
17 const size_t kMaxBuckets = 10000; // We don't ever want more than these many | |
18 // buckets; there is no real need for them | |
19 // and would cause crazy memory usage | |
20 | |
21 bool MetricsRecordUserActionFunction::RunImpl() { | |
22 std::string name; | |
23 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name)); | |
24 | |
25 content::RecordComputedAction(name); | |
26 return true; | |
27 } | |
28 | |
29 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, | |
30 int* sample) { | |
31 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); | |
32 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); | |
33 return true; | |
34 } | |
35 | |
36 bool MetricsHistogramHelperFunction::RecordValue(const std::string& name, | |
37 Histogram::ClassType type, | |
38 int min, | |
39 int max, | |
40 size_t buckets, | |
41 int sample) { | |
42 // Make sure toxic values don't get to internal code. | |
43 // Fix for maximums | |
44 min = std::min(min, INT_MAX - 3); | |
45 max = std::min(max, INT_MAX - 3); | |
46 buckets = std::min(buckets, kMaxBuckets); | |
47 // Fix for minimums. | |
48 min = std::max(min, 1); | |
49 max = std::max(max, min + 1); | |
50 buckets = std::max(buckets, static_cast<size_t>(3)); | |
51 // Trim buckets down to a maximum of the given range + over/underflow buckets | |
52 if (buckets > static_cast<size_t>(max - min + 2)) | |
53 buckets = max - min + 2; | |
54 | |
55 Histogram* counter; | |
56 if (type == Histogram::LINEAR_HISTOGRAM) { | |
57 counter = LinearHistogram::FactoryGet(name, | |
58 min, | |
59 max, | |
60 buckets, | |
61 Histogram::kUmaTargetedHistogramFlag); | |
62 } else { | |
63 counter = Histogram::FactoryGet(name, | |
64 min, | |
65 max, | |
66 buckets, | |
67 Histogram::kUmaTargetedHistogramFlag); | |
68 } | |
69 | |
70 counter->Add(sample); | |
71 return true; | |
72 } | |
73 | |
74 bool MetricsRecordValueFunction::RunImpl() { | |
75 int sample; | |
76 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &sample)); | |
77 | |
78 // Get the histogram parameters from the metric type object. | |
79 DictionaryValue* metric_type; | |
80 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &metric_type)); | |
81 | |
82 std::string name; | |
83 std::string type; | |
84 int min; | |
85 int max; | |
86 int buckets; | |
87 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString("metricName", &name)); | |
88 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString("type", &type)); | |
89 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("min", &min)); | |
90 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("max", &max)); | |
91 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("buckets", &buckets)); | |
92 | |
93 Histogram::ClassType histogram_type(type == "histogram-linear" ? | |
94 Histogram::LINEAR_HISTOGRAM : Histogram::HISTOGRAM); | |
95 return RecordValue(name, histogram_type, min, max, buckets, sample); | |
96 } | |
97 | |
98 bool MetricsRecordPercentageFunction::RunImpl() { | |
99 std::string name; | |
100 int sample; | |
101 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | |
102 return RecordValue(name, Histogram::LINEAR_HISTOGRAM, 1, 101, 102, sample); | |
103 } | |
104 | |
105 bool MetricsRecordCountFunction::RunImpl() { | |
106 std::string name; | |
107 int sample; | |
108 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | |
109 return RecordValue(name, Histogram::HISTOGRAM, 1, 1000000, 50, sample); | |
110 } | |
111 | |
112 bool MetricsRecordSmallCountFunction::RunImpl() { | |
113 std::string name; | |
114 int sample; | |
115 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | |
116 return RecordValue(name, Histogram::HISTOGRAM, 1, 100, 50, sample); | |
117 } | |
118 | |
119 bool MetricsRecordMediumCountFunction::RunImpl() { | |
120 std::string name; | |
121 int sample; | |
122 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | |
123 return RecordValue(name, Histogram::HISTOGRAM, 1, 10000, 50, sample); | |
124 } | |
125 | |
126 bool MetricsRecordTimeFunction::RunImpl() { | |
127 std::string name; | |
128 int sample; | |
129 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | |
130 static const int kTenSecMs = 10 * 1000; | |
131 return RecordValue(name, Histogram::HISTOGRAM, 1, kTenSecMs, 50, sample); | |
132 } | |
133 | |
134 bool MetricsRecordMediumTimeFunction::RunImpl() { | |
135 std::string name; | |
136 int sample; | |
137 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | |
138 static const int kThreeMinMs = 3 * 60 * 1000; | |
139 return RecordValue(name, Histogram::HISTOGRAM, 1, kThreeMinMs, 50, sample); | |
140 } | |
141 | |
142 bool MetricsRecordLongTimeFunction::RunImpl() { | |
143 std::string name; | |
144 int sample; | |
145 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | |
146 static const int kOneHourMs = 60 * 60 * 1000; | |
147 return RecordValue(name, Histogram::HISTOGRAM, 1, kOneHourMs, 50, sample); | |
148 } | |
OLD | NEW |