| 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/api/metrics/metrics.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" |
| (...skipping 21 matching lines...) Expand all Loading... |
| 32 | 32 |
| 33 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, | 33 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, |
| 34 int* sample) { | 34 int* sample) { |
| 35 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); | 35 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); |
| 36 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); | 36 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); |
| 37 return true; | 37 return true; |
| 38 } | 38 } |
| 39 | 39 |
| 40 bool MetricsHistogramHelperFunction::RecordValue( | 40 bool MetricsHistogramHelperFunction::RecordValue( |
| 41 const std::string& name, | 41 const std::string& name, |
| 42 Histogram::ClassType type, | 42 base::HistogramType type, |
| 43 int min, int max, size_t buckets, | 43 int min, int max, size_t buckets, |
| 44 int sample) { | 44 int sample) { |
| 45 // Make sure toxic values don't get to internal code. | 45 // Make sure toxic values don't get to internal code. |
| 46 // Fix for maximums | 46 // Fix for maximums |
| 47 min = std::min(min, INT_MAX - 3); | 47 min = std::min(min, INT_MAX - 3); |
| 48 max = std::min(max, INT_MAX - 3); | 48 max = std::min(max, INT_MAX - 3); |
| 49 buckets = std::min(buckets, kMaxBuckets); | 49 buckets = std::min(buckets, kMaxBuckets); |
| 50 // Fix for minimums. | 50 // Fix for minimums. |
| 51 min = std::max(min, 1); | 51 min = std::max(min, 1); |
| 52 max = std::max(max, min + 1); | 52 max = std::max(max, min + 1); |
| 53 buckets = std::max(buckets, static_cast<size_t>(3)); | 53 buckets = std::max(buckets, static_cast<size_t>(3)); |
| 54 // 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 |
| 55 if (buckets > static_cast<size_t>(max - min + 2)) | 55 if (buckets > static_cast<size_t>(max - min + 2)) |
| 56 buckets = max - min + 2; | 56 buckets = max - min + 2; |
| 57 | 57 |
| 58 Histogram* counter; | 58 Histogram* counter; |
| 59 if (type == Histogram::LINEAR_HISTOGRAM) { | 59 if (type == base::LINEAR_HISTOGRAM) { |
| 60 counter = LinearHistogram::FactoryGet(name, | 60 counter = LinearHistogram::FactoryGet(name, |
| 61 min, max, buckets, | 61 min, max, buckets, |
| 62 Histogram::kUmaTargetedHistogramFlag); | 62 Histogram::kUmaTargetedHistogramFlag); |
| 63 } else { | 63 } else { |
| 64 counter = Histogram::FactoryGet(name, | 64 counter = Histogram::FactoryGet(name, |
| 65 min, max, buckets, | 65 min, max, buckets, |
| 66 Histogram::kUmaTargetedHistogramFlag); | 66 Histogram::kUmaTargetedHistogramFlag); |
| 67 } | 67 } |
| 68 | 68 |
| 69 counter->Add(sample); | 69 counter->Add(sample); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 82 std::string type; | 82 std::string type; |
| 83 int min; | 83 int min; |
| 84 int max; | 84 int max; |
| 85 int buckets; | 85 int buckets; |
| 86 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString("metricName", &name)); | 86 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString("metricName", &name)); |
| 87 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString("type", &type)); | 87 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString("type", &type)); |
| 88 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("min", &min)); | 88 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("min", &min)); |
| 89 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("max", &max)); | 89 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("max", &max)); |
| 90 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("buckets", &buckets)); | 90 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("buckets", &buckets)); |
| 91 | 91 |
| 92 Histogram::ClassType histogram_type(type == "histogram-linear" ? | 92 base::HistogramType histogram_type(type == "histogram-linear" ? |
| 93 Histogram::LINEAR_HISTOGRAM : Histogram::HISTOGRAM); | 93 base::LINEAR_HISTOGRAM : base::HISTOGRAM); |
| 94 return RecordValue(name, histogram_type, min, max, buckets, sample); | 94 return RecordValue(name, histogram_type, min, max, buckets, sample); |
| 95 } | 95 } |
| 96 | 96 |
| 97 bool MetricsRecordPercentageFunction::RunImpl() { | 97 bool MetricsRecordPercentageFunction::RunImpl() { |
| 98 std::string name; | 98 std::string name; |
| 99 int sample; | 99 int sample; |
| 100 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 100 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 101 return RecordValue(name, | 101 return RecordValue(name, base::LINEAR_HISTOGRAM, 1, 101, 102, sample); |
| 102 Histogram::LINEAR_HISTOGRAM, | |
| 103 1, 101, 102, | |
| 104 sample); | |
| 105 } | 102 } |
| 106 | 103 |
| 107 bool MetricsRecordCountFunction::RunImpl() { | 104 bool MetricsRecordCountFunction::RunImpl() { |
| 108 std::string name; | 105 std::string name; |
| 109 int sample; | 106 int sample; |
| 110 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 107 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 111 return RecordValue(name, Histogram::HISTOGRAM, 1, 1000000, 50, sample); | 108 return RecordValue(name, base::HISTOGRAM, 1, 1000000, 50, sample); |
| 112 } | 109 } |
| 113 | 110 |
| 114 bool MetricsRecordSmallCountFunction::RunImpl() { | 111 bool MetricsRecordSmallCountFunction::RunImpl() { |
| 115 std::string name; | 112 std::string name; |
| 116 int sample; | 113 int sample; |
| 117 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 114 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 118 return RecordValue(name, Histogram::HISTOGRAM, 1, 100, 50, sample); | 115 return RecordValue(name, base::HISTOGRAM, 1, 100, 50, sample); |
| 119 } | 116 } |
| 120 | 117 |
| 121 bool MetricsRecordMediumCountFunction::RunImpl() { | 118 bool MetricsRecordMediumCountFunction::RunImpl() { |
| 122 std::string name; | 119 std::string name; |
| 123 int sample; | 120 int sample; |
| 124 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 121 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 125 return RecordValue(name, Histogram::HISTOGRAM, 1, 10000, 50, sample); | 122 return RecordValue(name, base::HISTOGRAM, 1, 10000, 50, sample); |
| 126 } | 123 } |
| 127 | 124 |
| 128 bool MetricsRecordTimeFunction::RunImpl() { | 125 bool MetricsRecordTimeFunction::RunImpl() { |
| 129 std::string name; | 126 std::string name; |
| 130 int sample; | 127 int sample; |
| 131 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 128 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 132 static const int kTenSecMs = 10 * 1000; | 129 static const int kTenSecMs = 10 * 1000; |
| 133 return RecordValue(name, Histogram::HISTOGRAM, 1, kTenSecMs, 50, sample); | 130 return RecordValue(name, base::HISTOGRAM, 1, kTenSecMs, 50, sample); |
| 134 } | 131 } |
| 135 | 132 |
| 136 bool MetricsRecordMediumTimeFunction::RunImpl() { | 133 bool MetricsRecordMediumTimeFunction::RunImpl() { |
| 137 std::string name; | 134 std::string name; |
| 138 int sample; | 135 int sample; |
| 139 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 136 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 140 static const int kThreeMinMs = 3 * 60 * 1000; | 137 static const int kThreeMinMs = 3 * 60 * 1000; |
| 141 return RecordValue(name, Histogram::HISTOGRAM, 1, kThreeMinMs, 50, sample); | 138 return RecordValue(name, base::HISTOGRAM, 1, kThreeMinMs, 50, sample); |
| 142 } | 139 } |
| 143 | 140 |
| 144 bool MetricsRecordLongTimeFunction::RunImpl() { | 141 bool MetricsRecordLongTimeFunction::RunImpl() { |
| 145 std::string name; | 142 std::string name; |
| 146 int sample; | 143 int sample; |
| 147 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 144 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 148 static const int kOneHourMs = 60 * 60 * 1000; | 145 static const int kOneHourMs = 60 * 60 * 1000; |
| 149 return RecordValue(name, Histogram::HISTOGRAM, 1, kOneHourMs, 50, sample); | 146 return RecordValue(name, base::HISTOGRAM, 1, kOneHourMs, 50, sample); |
| 150 } | 147 } |
| 151 | 148 |
| 152 } // namespace extensions | 149 } // namespace extensions |
| OLD | NEW |