OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 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/extension_metrics_module.h" |
| 6 |
| 7 #include "base/histogram.h" |
| 8 #include "chrome/common/extensions/extension.h" |
| 9 #include "chrome/browser/metrics/user_metrics.h" |
| 10 |
| 11 namespace { |
| 12 |
| 13 // Build the full name of a metrics for the given extension. Each metric |
| 14 // is made up of the unique name within the extension followed by the |
| 15 // extension's id. This keeps the metrics from one extension unique from |
| 16 // other extensions, as well as those metrics from chrome itself. |
| 17 std::string BuildMetricName(const std::string& name, |
| 18 const Extension* extension) { |
| 19 std::string full_name(name); |
| 20 full_name += extension->id(); |
| 21 return full_name; |
| 22 } |
| 23 |
| 24 } // anonymous namespace |
| 25 |
| 26 // These extension function classes are enabled only if the |
| 27 // enable-metrics-extension-api command line switch is used. Refer to |
| 28 // extension_function_dispatcher.cc to see how they are enabled. |
| 29 |
| 30 bool MetricsRecordUserActionFunction::RunImpl() { |
| 31 std::string name; |
| 32 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_STRING)); |
| 33 EXTENSION_FUNCTION_VALIDATE(args_->GetAsString(&name)); |
| 34 |
| 35 name = BuildMetricName(name, GetExtension()); |
| 36 UserMetrics::RecordComputedAction(name, profile()); |
| 37 return true; |
| 38 } |
| 39 |
| 40 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, |
| 41 int* sample) { |
| 42 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 43 const ListValue* args = args_as_list(); |
| 44 |
| 45 EXTENSION_FUNCTION_VALIDATE(args->GetString(0, name)); |
| 46 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(1, sample)); |
| 47 return true; |
| 48 } |
| 49 |
| 50 bool MetricsHistogramHelperFunction::RecordValue(const std::string& name, |
| 51 Histogram::ClassType type, |
| 52 int min, |
| 53 int max, |
| 54 size_t buckets, |
| 55 int sample) { |
| 56 std::string full_name = BuildMetricName(name, GetExtension()); |
| 57 scoped_refptr<Histogram> counter; |
| 58 if (type == Histogram::LINEAR_HISTOGRAM) { |
| 59 counter = LinearHistogram::FactoryGet(full_name, |
| 60 min, |
| 61 max, |
| 62 buckets, |
| 63 Histogram::kUmaTargetedHistogramFlag); |
| 64 } else { |
| 65 counter = Histogram::FactoryGet(full_name, |
| 66 min, |
| 67 max, |
| 68 buckets, |
| 69 Histogram::kUmaTargetedHistogramFlag); |
| 70 } |
| 71 |
| 72 counter->Add(sample); |
| 73 return true; |
| 74 } |
| 75 |
| 76 bool MetricsRecordValueFunction::RunImpl() { |
| 77 EXTENSION_FUNCTION_VALIDATE(args_->IsType(Value::TYPE_LIST)); |
| 78 const ListValue* args = args_as_list(); |
| 79 |
| 80 int sample; |
| 81 EXTENSION_FUNCTION_VALIDATE(args->GetInteger(1, &sample)); |
| 82 |
| 83 // Get the histogram parameters from the metric type object. |
| 84 DictionaryValue* metric_type; |
| 85 EXTENSION_FUNCTION_VALIDATE(args->GetDictionary(0, &metric_type)); |
| 86 |
| 87 std::string name; |
| 88 std::string type; |
| 89 int min; |
| 90 int max; |
| 91 int buckets; |
| 92 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString(L"metricName", &name)); |
| 93 EXTENSION_FUNCTION_VALIDATE(metric_type->GetString(L"type", &type)); |
| 94 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger(L"min", &min)); |
| 95 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger(L"max", &max)); |
| 96 EXTENSION_FUNCTION_VALIDATE(metric_type->GetInteger(L"buckets", &buckets)); |
| 97 |
| 98 Histogram::ClassType histogram_type(type == "histogram-linear" ? |
| 99 Histogram::LINEAR_HISTOGRAM : Histogram::HISTOGRAM); |
| 100 return RecordValue(name, histogram_type, min, max, buckets, sample); |
| 101 } |
| 102 |
| 103 bool MetricsRecordPercentageFunction::RunImpl() { |
| 104 std::string name; |
| 105 int sample; |
| 106 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 107 return RecordValue(name, Histogram::LINEAR_HISTOGRAM, 1, 101, 102, sample); |
| 108 } |
| 109 |
| 110 bool MetricsRecordCountFunction::RunImpl() { |
| 111 std::string name; |
| 112 int sample; |
| 113 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 114 return RecordValue(name, Histogram::HISTOGRAM, 1, 1000000, 50, sample); |
| 115 } |
| 116 |
| 117 bool MetricsRecordSmallCountFunction::RunImpl() { |
| 118 std::string name; |
| 119 int sample; |
| 120 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 121 return RecordValue(name, Histogram::HISTOGRAM, 1, 100, 50, sample); |
| 122 } |
| 123 |
| 124 bool MetricsRecordMediumCountFunction::RunImpl() { |
| 125 std::string name; |
| 126 int sample; |
| 127 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 128 return RecordValue(name, Histogram::HISTOGRAM, 1, 10000, 50, sample); |
| 129 } |
| 130 |
| 131 bool MetricsRecordTimeFunction::RunImpl() { |
| 132 std::string name; |
| 133 int sample; |
| 134 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 135 static const int kTenSecMs = 10 * 1000; |
| 136 return RecordValue(name, Histogram::HISTOGRAM, 1, kTenSecMs, 50, sample); |
| 137 } |
| 138 |
| 139 bool MetricsRecordMediumTimeFunction::RunImpl() { |
| 140 std::string name; |
| 141 int sample; |
| 142 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 143 static const int kThreeMinMs = 3 * 60 * 1000; |
| 144 return RecordValue(name, Histogram::HISTOGRAM, 1, kThreeMinMs, 50, sample); |
| 145 } |
| 146 |
| 147 bool MetricsRecordLongTimeFunction::RunImpl() { |
| 148 std::string name; |
| 149 int sample; |
| 150 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
| 151 static const int kOneHourMs = 60 * 60 * 1000; |
| 152 return RecordValue(name, Histogram::HISTOGRAM, 1, kOneHourMs, 50, sample); |
| 153 } |
OLD | NEW |