OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/extension_metrics_module.h" | 5 #include "chrome/browser/extensions/api/metrics/metrics.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
10 #include "chrome/common/extensions/extension.h" | 10 #include "chrome/common/extensions/extension.h" |
11 #include "content/public/browser/user_metrics.h" | 11 #include "content/public/browser/user_metrics.h" |
12 | 12 |
| 13 namespace extensions { |
| 14 |
13 using base::Histogram; | 15 using base::Histogram; |
14 using base::LinearHistogram; | 16 using base::LinearHistogram; |
15 using content::UserMetricsAction; | 17 |
| 18 namespace { |
16 | 19 |
17 const size_t kMaxBuckets = 10000; // We don't ever want more than these many | 20 const size_t kMaxBuckets = 10000; // We don't ever want more than these many |
18 // buckets; there is no real need for them | 21 // buckets; there is no real need for them |
19 // and would cause crazy memory usage | 22 // and would cause crazy memory usage |
| 23 } // namespace |
20 | 24 |
21 bool MetricsRecordUserActionFunction::RunImpl() { | 25 bool MetricsRecordUserActionFunction::RunImpl() { |
22 std::string name; | 26 std::string name; |
23 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name)); | 27 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name)); |
24 | 28 |
25 content::RecordComputedAction(name); | 29 content::RecordComputedAction(name); |
26 return true; | 30 return true; |
27 } | 31 } |
28 | 32 |
29 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, | 33 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, |
30 int* sample) { | 34 int* sample) { |
31 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); | 35 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); |
32 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); | 36 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); |
33 return true; | 37 return true; |
34 } | 38 } |
35 | 39 |
36 bool MetricsHistogramHelperFunction::RecordValue(const std::string& name, | 40 bool MetricsHistogramHelperFunction::RecordValue( |
37 Histogram::ClassType type, | 41 const std::string& name, |
38 int min, | 42 Histogram::ClassType type, |
39 int max, | 43 int min, int max, size_t buckets, |
40 size_t buckets, | 44 int sample) { |
41 int sample) { | |
42 // Make sure toxic values don't get to internal code. | 45 // Make sure toxic values don't get to internal code. |
43 // Fix for maximums | 46 // Fix for maximums |
44 min = std::min(min, INT_MAX - 3); | 47 min = std::min(min, INT_MAX - 3); |
45 max = std::min(max, INT_MAX - 3); | 48 max = std::min(max, INT_MAX - 3); |
46 buckets = std::min(buckets, kMaxBuckets); | 49 buckets = std::min(buckets, kMaxBuckets); |
47 // Fix for minimums. | 50 // Fix for minimums. |
48 min = std::max(min, 1); | 51 min = std::max(min, 1); |
49 max = std::max(max, min + 1); | 52 max = std::max(max, min + 1); |
50 buckets = std::max(buckets, static_cast<size_t>(3)); | 53 buckets = std::max(buckets, static_cast<size_t>(3)); |
51 // Trim buckets down to a maximum of the given range + over/underflow buckets | 54 // Trim buckets down to a maximum of the given range + over/underflow buckets |
52 if (buckets > static_cast<size_t>(max - min + 2)) | 55 if (buckets > static_cast<size_t>(max - min + 2)) |
53 buckets = max - min + 2; | 56 buckets = max - min + 2; |
54 | 57 |
55 Histogram* counter; | 58 Histogram* counter; |
56 if (type == Histogram::LINEAR_HISTOGRAM) { | 59 if (type == Histogram::LINEAR_HISTOGRAM) { |
57 counter = LinearHistogram::FactoryGet(name, | 60 counter = LinearHistogram::FactoryGet(name, |
58 min, | 61 min, max, buckets, |
59 max, | |
60 buckets, | |
61 Histogram::kUmaTargetedHistogramFlag); | 62 Histogram::kUmaTargetedHistogramFlag); |
62 } else { | 63 } else { |
63 counter = Histogram::FactoryGet(name, | 64 counter = Histogram::FactoryGet(name, |
64 min, | 65 min, max, buckets, |
65 max, | |
66 buckets, | |
67 Histogram::kUmaTargetedHistogramFlag); | 66 Histogram::kUmaTargetedHistogramFlag); |
68 } | 67 } |
69 | 68 |
70 counter->Add(sample); | 69 counter->Add(sample); |
71 return true; | 70 return true; |
72 } | 71 } |
73 | 72 |
74 bool MetricsRecordValueFunction::RunImpl() { | 73 bool MetricsRecordValueFunction::RunImpl() { |
75 int sample; | 74 int sample; |
76 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &sample)); | 75 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &sample)); |
(...skipping 15 matching lines...) Expand all Loading... |
92 | 91 |
93 Histogram::ClassType histogram_type(type == "histogram-linear" ? | 92 Histogram::ClassType histogram_type(type == "histogram-linear" ? |
94 Histogram::LINEAR_HISTOGRAM : Histogram::HISTOGRAM); | 93 Histogram::LINEAR_HISTOGRAM : Histogram::HISTOGRAM); |
95 return RecordValue(name, histogram_type, min, max, buckets, sample); | 94 return RecordValue(name, histogram_type, min, max, buckets, sample); |
96 } | 95 } |
97 | 96 |
98 bool MetricsRecordPercentageFunction::RunImpl() { | 97 bool MetricsRecordPercentageFunction::RunImpl() { |
99 std::string name; | 98 std::string name; |
100 int sample; | 99 int sample; |
101 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 100 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
102 return RecordValue(name, Histogram::LINEAR_HISTOGRAM, 1, 101, 102, sample); | 101 return RecordValue(name, |
| 102 Histogram::LINEAR_HISTOGRAM, |
| 103 1, 101, 102, |
| 104 sample); |
103 } | 105 } |
104 | 106 |
105 bool MetricsRecordCountFunction::RunImpl() { | 107 bool MetricsRecordCountFunction::RunImpl() { |
106 std::string name; | 108 std::string name; |
107 int sample; | 109 int sample; |
108 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 110 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
109 return RecordValue(name, Histogram::HISTOGRAM, 1, 1000000, 50, sample); | 111 return RecordValue(name, Histogram::HISTOGRAM, 1, 1000000, 50, sample); |
110 } | 112 } |
111 | 113 |
112 bool MetricsRecordSmallCountFunction::RunImpl() { | 114 bool MetricsRecordSmallCountFunction::RunImpl() { |
(...skipping 26 matching lines...) Expand all Loading... |
139 return RecordValue(name, Histogram::HISTOGRAM, 1, kThreeMinMs, 50, sample); | 141 return RecordValue(name, Histogram::HISTOGRAM, 1, kThreeMinMs, 50, sample); |
140 } | 142 } |
141 | 143 |
142 bool MetricsRecordLongTimeFunction::RunImpl() { | 144 bool MetricsRecordLongTimeFunction::RunImpl() { |
143 std::string name; | 145 std::string name; |
144 int sample; | 146 int sample; |
145 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 147 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
146 static const int kOneHourMs = 60 * 60 * 1000; | 148 static const int kOneHourMs = 60 * 60 * 1000; |
147 return RecordValue(name, Histogram::HISTOGRAM, 1, kOneHourMs, 50, sample); | 149 return RecordValue(name, Histogram::HISTOGRAM, 1, kOneHourMs, 50, sample); |
148 } | 150 } |
| 151 |
| 152 } // namespace extensions |
OLD | NEW |