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