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/api/metrics_private/metrics_private_api.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 namespace extensions { |
| 14 |
| 15 namespace { |
| 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 } // namespace |
| 21 |
| 22 bool MetricsPrivateRecordUserActionFunction::RunImpl() { |
| 23 std::string name; |
| 24 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name)); |
| 25 |
| 26 content::RecordComputedAction(name); |
| 27 return true; |
| 28 } |
| 29 |
| 30 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, |
| 31 int* sample) { |
| 32 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); |
| 33 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); |
| 34 return true; |
| 35 } |
| 36 |
| 37 bool MetricsHistogramHelperFunction::RecordValue( |
| 38 const std::string& name, |
| 39 base::HistogramType type, |
| 40 int min, int max, 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 base::HistogramBase* counter; |
| 56 if (type == base::LINEAR_HISTOGRAM) { |
| 57 counter = base::LinearHistogram::FactoryGet( |
| 58 name, min, max, buckets, |
| 59 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 60 } else { |
| 61 counter = base::Histogram::FactoryGet( |
| 62 name, min, max, buckets, |
| 63 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 64 } |
| 65 |
| 66 counter->Add(sample); |
| 67 return true; |
| 68 } |
| 69 |
| 70 bool MetricsPrivateRecordValueFunction::RunImpl() { |
| 71 int sample; |
| 72 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &sample)); |
| 73 |
| 74 // Get the histogram parameters from the metric type object. |
| 75 DictionaryValue* metric_type; |
| 76 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &metric_type)); |
| 77 |
| 78 std::string name; |
| 79 std::string type; |
| 80 int min; |
| 81 int max; |
| 82 int buckets; |
| 83 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString("metricName", &name)); |
| 84 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString("type", &type)); |
| 85 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("min", &min)); |
| 86 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("max", &max)); |
| 87 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger("buckets", &buckets)); |
| 88 |
| 89 base::HistogramType histogram_type(type == "histogram-linear" ? |
| 90 base::LINEAR_HISTOGRAM : base::HISTOGRAM); |
| 91 return RecordValue(name, histogram_type, min, max, buckets, sample); |
| 92 } |
| 93 |
| 94 bool MetricsPrivateRecordPercentageFunction::RunImpl() { |
| 95 std::string name; |
| 96 int sample; |
| 97 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 98 return RecordValue(name, base::LINEAR_HISTOGRAM, 1, 101, 102, sample); |
| 99 } |
| 100 |
| 101 bool MetricsPrivateRecordCountFunction::RunImpl() { |
| 102 std::string name; |
| 103 int sample; |
| 104 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 105 return RecordValue(name, base::HISTOGRAM, 1, 1000000, 50, sample); |
| 106 } |
| 107 |
| 108 bool MetricsPrivateRecordSmallCountFunction::RunImpl() { |
| 109 std::string name; |
| 110 int sample; |
| 111 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 112 return RecordValue(name, base::HISTOGRAM, 1, 100, 50, sample); |
| 113 } |
| 114 |
| 115 bool MetricsPrivateRecordMediumCountFunction::RunImpl() { |
| 116 std::string name; |
| 117 int sample; |
| 118 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 119 return RecordValue(name, base::HISTOGRAM, 1, 10000, 50, sample); |
| 120 } |
| 121 |
| 122 bool MetricsPrivateRecordTimeFunction::RunImpl() { |
| 123 std::string name; |
| 124 int sample; |
| 125 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 126 static const int kTenSecMs = 10 * 1000; |
| 127 return RecordValue(name, base::HISTOGRAM, 1, kTenSecMs, 50, sample); |
| 128 } |
| 129 |
| 130 bool MetricsPrivateRecordMediumTimeFunction::RunImpl() { |
| 131 std::string name; |
| 132 int sample; |
| 133 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 134 static const int kThreeMinMs = 3 * 60 * 1000; |
| 135 return RecordValue(name, base::HISTOGRAM, 1, kThreeMinMs, 50, sample); |
| 136 } |
| 137 |
| 138 bool MetricsPrivateRecordLongTimeFunction::RunImpl() { |
| 139 std::string name; |
| 140 int sample; |
| 141 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 142 static const int kOneHourMs = 60 * 60 * 1000; |
| 143 return RecordValue(name, base::HISTOGRAM, 1, kOneHourMs, 50, sample); |
| 144 } |
| 145 |
| 146 } // namespace extensions |
OLD | NEW |