| 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_private/metrics_private_api.h" | 5 #include "chrome/browser/extensions/api/metrics_private/metrics_private_api.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/api/metrics_private.h" |
| 10 #include "chrome/common/extensions/extension.h" | 11 #include "chrome/common/extensions/extension.h" |
| 11 #include "content/public/browser/user_metrics.h" | 12 #include "content/public/browser/user_metrics.h" |
| 12 | 13 |
| 13 namespace extensions { | 14 namespace extensions { |
| 14 | 15 |
| 16 namespace RecordUserAction = api::metrics_private::RecordUserAction; |
| 17 namespace RecordValue = api::metrics_private::RecordValue; |
| 18 namespace RecordPercentage = api::metrics_private::RecordPercentage; |
| 19 namespace RecordCount = api::metrics_private::RecordCount; |
| 20 namespace RecordSmallCount = api::metrics_private::RecordSmallCount; |
| 21 namespace RecordMediumCount = api::metrics_private::RecordMediumCount; |
| 22 namespace RecordTime = api::metrics_private::RecordTime; |
| 23 namespace RecordMediumTime = api::metrics_private::RecordMediumTime; |
| 24 namespace RecordLongTime = api::metrics_private::RecordLongTime; |
| 25 |
| 15 namespace { | 26 namespace { |
| 16 | 27 |
| 17 const size_t kMaxBuckets = 10000; // We don't ever want more than these many | 28 const size_t kMaxBuckets = 10000; // We don't ever want more than these many |
| 18 // buckets; there is no real need for them | 29 // buckets; there is no real need for them |
| 19 // and would cause crazy memory usage | 30 // and would cause crazy memory usage |
| 20 } // namespace | 31 } // namespace |
| 21 | 32 |
| 22 bool MetricsPrivateRecordUserActionFunction::RunImpl() { | 33 bool MetricsPrivateRecordUserActionFunction::RunImpl() { |
| 23 std::string name; | 34 scoped_ptr<RecordUserAction::Params> params( |
| 24 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name)); | 35 RecordUserAction::Params::Create(*args_)); |
| 36 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 25 | 37 |
| 26 content::RecordComputedAction(name); | 38 content::RecordComputedAction(params->name); |
| 27 return true; | 39 return true; |
| 28 } | 40 } |
| 29 | 41 |
| 30 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, | 42 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, |
| 31 int* sample) { | 43 int* sample) { |
| 32 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); | 44 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); |
| 33 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); | 45 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); |
| 34 return true; | 46 return true; |
| 35 } | 47 } |
| 36 | 48 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 61 counter = base::Histogram::FactoryGet( | 73 counter = base::Histogram::FactoryGet( |
| 62 name, min, max, buckets, | 74 name, min, max, buckets, |
| 63 base::HistogramBase::kUmaTargetedHistogramFlag); | 75 base::HistogramBase::kUmaTargetedHistogramFlag); |
| 64 } | 76 } |
| 65 | 77 |
| 66 counter->Add(sample); | 78 counter->Add(sample); |
| 67 return true; | 79 return true; |
| 68 } | 80 } |
| 69 | 81 |
| 70 bool MetricsPrivateRecordValueFunction::RunImpl() { | 82 bool MetricsPrivateRecordValueFunction::RunImpl() { |
| 71 int sample; | 83 scoped_ptr<RecordValue::Params> params(RecordValue::Params::Create(*args_)); |
| 72 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &sample)); | 84 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 73 | 85 |
| 74 // Get the histogram parameters from the metric type object. | 86 // Get the histogram parameters from the metric type object. |
| 75 DictionaryValue* metric_type; | 87 std::string type = api::metrics_private::MetricType::ToString( |
| 76 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &metric_type)); | 88 params->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 |
| 89 base::HistogramType histogram_type(type == "histogram-linear" ? | 90 base::HistogramType histogram_type(type == "histogram-linear" ? |
| 90 base::LINEAR_HISTOGRAM : base::HISTOGRAM); | 91 base::LINEAR_HISTOGRAM : base::HISTOGRAM); |
| 91 return RecordValue(name, histogram_type, min, max, buckets, sample); | 92 return RecordValue(params->metric.metric_name, histogram_type, |
| 93 params->metric.min, params->metric.max, |
| 94 params->metric.buckets, params->value); |
| 92 } | 95 } |
| 93 | 96 |
| 94 bool MetricsPrivateRecordPercentageFunction::RunImpl() { | 97 bool MetricsPrivateRecordPercentageFunction::RunImpl() { |
| 95 std::string name; | 98 scoped_ptr<RecordPercentage::Params> params( |
| 96 int sample; | 99 RecordPercentage::Params::Create(*args_)); |
| 97 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 100 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 98 return RecordValue(name, base::LINEAR_HISTOGRAM, 1, 101, 102, sample); | 101 return RecordValue(params->metric_name, base::LINEAR_HISTOGRAM, |
| 102 1, 101, 102, params->value); |
| 99 } | 103 } |
| 100 | 104 |
| 101 bool MetricsPrivateRecordCountFunction::RunImpl() { | 105 bool MetricsPrivateRecordCountFunction::RunImpl() { |
| 102 std::string name; | 106 scoped_ptr<RecordCount::Params> params(RecordCount::Params::Create(*args_)); |
| 103 int sample; | 107 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 104 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 108 return RecordValue(params->metric_name, base::HISTOGRAM, |
| 105 return RecordValue(name, base::HISTOGRAM, 1, 1000000, 50, sample); | 109 1, 1000000, 50, params->value); |
| 106 } | 110 } |
| 107 | 111 |
| 108 bool MetricsPrivateRecordSmallCountFunction::RunImpl() { | 112 bool MetricsPrivateRecordSmallCountFunction::RunImpl() { |
| 109 std::string name; | 113 scoped_ptr<RecordSmallCount::Params> params( |
| 110 int sample; | 114 RecordSmallCount::Params::Create(*args_)); |
| 111 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 115 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 112 return RecordValue(name, base::HISTOGRAM, 1, 100, 50, sample); | 116 return RecordValue(params->metric_name, base::HISTOGRAM, |
| 117 1, 100, 50, params->value); |
| 113 } | 118 } |
| 114 | 119 |
| 115 bool MetricsPrivateRecordMediumCountFunction::RunImpl() { | 120 bool MetricsPrivateRecordMediumCountFunction::RunImpl() { |
| 116 std::string name; | 121 scoped_ptr<RecordMediumCount::Params> params( |
| 117 int sample; | 122 RecordMediumCount::Params::Create(*args_)); |
| 118 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 123 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 119 return RecordValue(name, base::HISTOGRAM, 1, 10000, 50, sample); | 124 return RecordValue(params->metric_name, base::HISTOGRAM, |
| 125 1, 10000, 50, params->value); |
| 120 } | 126 } |
| 121 | 127 |
| 122 bool MetricsPrivateRecordTimeFunction::RunImpl() { | 128 bool MetricsPrivateRecordTimeFunction::RunImpl() { |
| 123 std::string name; | 129 scoped_ptr<RecordTime::Params> params(RecordTime::Params::Create(*args_)); |
| 124 int sample; | 130 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 125 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | |
| 126 static const int kTenSecMs = 10 * 1000; | 131 static const int kTenSecMs = 10 * 1000; |
| 127 return RecordValue(name, base::HISTOGRAM, 1, kTenSecMs, 50, sample); | 132 return RecordValue(params->metric_name, base::HISTOGRAM, |
| 133 1, kTenSecMs, 50, params->value); |
| 128 } | 134 } |
| 129 | 135 |
| 130 bool MetricsPrivateRecordMediumTimeFunction::RunImpl() { | 136 bool MetricsPrivateRecordMediumTimeFunction::RunImpl() { |
| 131 std::string name; | 137 scoped_ptr<RecordMediumTime::Params> params( |
| 132 int sample; | 138 RecordMediumTime::Params::Create(*args_)); |
| 133 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 139 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 134 static const int kThreeMinMs = 3 * 60 * 1000; | 140 static const int kThreeMinMs = 3 * 60 * 1000; |
| 135 return RecordValue(name, base::HISTOGRAM, 1, kThreeMinMs, 50, sample); | 141 return RecordValue(params->metric_name, base::HISTOGRAM, |
| 142 1, kThreeMinMs, 50, params->value); |
| 136 } | 143 } |
| 137 | 144 |
| 138 bool MetricsPrivateRecordLongTimeFunction::RunImpl() { | 145 bool MetricsPrivateRecordLongTimeFunction::RunImpl() { |
| 139 std::string name; | 146 scoped_ptr<RecordLongTime::Params> params( |
| 140 int sample; | 147 RecordLongTime::Params::Create(*args_)); |
| 141 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 148 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 142 static const int kOneHourMs = 60 * 60 * 1000; | 149 static const int kOneHourMs = 60 * 60 * 1000; |
| 143 return RecordValue(name, base::HISTOGRAM, 1, kOneHourMs, 50, sample); | 150 return RecordValue(params->metric_name, base::HISTOGRAM, |
| 151 1, kOneHourMs, 50, params->value); |
| 144 } | 152 } |
| 145 | 153 |
| 146 } // namespace extensions | 154 } // namespace extensions |
| OLD | NEW |