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