OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/extension_metrics_module.h" | 5 #include "chrome/browser/extensions/extension_metrics_module.h" |
6 | 6 |
7 #include "base/metrics/histogram.h" | 7 #include "base/metrics/histogram.h" |
8 #include "base/values.h" | |
9 #include "chrome/common/extensions/extension.h" | 8 #include "chrome/common/extensions/extension.h" |
10 #include "chrome/browser/ui/options/options_util.h" | |
11 #include "chrome/installer/util/google_update_settings.h" | |
12 #include "content/browser/user_metrics.h" | 9 #include "content/browser/user_metrics.h" |
13 | 10 |
14 using base::Histogram; | 11 using base::Histogram; |
15 using base::LinearHistogram; | 12 using base::LinearHistogram; |
16 | 13 |
17 namespace { | |
18 | |
19 // Build the full name of a metrics for the given extension. | |
20 // For a non-component extension the metric name is made up of the unique name | |
21 // within the extension followed by the extension's id. This keeps the metrics | |
22 // from one extension unique from other extensions, as well as those metrics | |
23 // from chrome itself. | |
24 // For a component extension the metric name is used as is. There are not so | |
25 // many of them and it is easy enough to prevent name clashes. | |
26 std::string BuildMetricName(const std::string& name, | |
27 const Extension* extension) { | |
28 std::string full_name(name); | |
29 if (extension->location() != Extension::COMPONENT) | |
30 full_name += extension->id(); | |
31 return full_name; | |
32 } | |
33 | |
34 } // anonymous namespace | |
35 | |
36 bool MetricsSetEnabledFunction::RunImpl() { | |
37 bool enabled = false; | |
38 EXTENSION_FUNCTION_VALIDATE(args_->GetBoolean(0, &enabled)); | |
39 | |
40 // Using OptionsUtil is better because it actually ensures we reset all the | |
41 // necessary threads. This is the main way for starting / stopping UMA and | |
42 // crash reporting. | |
43 // This method will return the resulting enabled, which we send to JS. | |
44 bool result = OptionsUtil::ResolveMetricsReportingEnabled(enabled); | |
45 result_.reset(Value::CreateBooleanValue(result)); | |
46 return true; | |
47 } | |
48 | |
49 bool MetricsGetEnabledFunction::RunImpl() { | |
50 bool enabled = GoogleUpdateSettings::GetCollectStatsConsent(); | |
51 result_.reset(Value::CreateBooleanValue(enabled)); | |
52 return true; | |
53 } | |
54 | |
55 bool MetricsRecordUserActionFunction::RunImpl() { | 14 bool MetricsRecordUserActionFunction::RunImpl() { |
56 std::string name; | 15 std::string name; |
57 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name)); | 16 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name)); |
58 | 17 |
59 name = BuildMetricName(name, GetExtension()); | |
60 UserMetrics::RecordComputedAction(name); | 18 UserMetrics::RecordComputedAction(name); |
61 return true; | 19 return true; |
62 } | 20 } |
63 | 21 |
64 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, | 22 bool MetricsHistogramHelperFunction::GetNameAndSample(std::string* name, |
65 int* sample) { | 23 int* sample) { |
66 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); | 24 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, name)); |
67 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); | 25 EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, sample)); |
68 return true; | 26 return true; |
69 } | 27 } |
70 | 28 |
71 bool MetricsHistogramHelperFunction::RecordValue(const std::string& name, | 29 bool MetricsHistogramHelperFunction::RecordValue(const std::string& name, |
72 Histogram::ClassType type, | 30 Histogram::ClassType type, |
73 int min, | 31 int min, |
74 int max, | 32 int max, |
75 size_t buckets, | 33 size_t buckets, |
76 int sample) { | 34 int sample) { |
77 std::string full_name = BuildMetricName(name, GetExtension()); | |
78 Histogram* counter; | 35 Histogram* counter; |
79 if (type == Histogram::LINEAR_HISTOGRAM) { | 36 if (type == Histogram::LINEAR_HISTOGRAM) { |
80 counter = LinearHistogram::FactoryGet(full_name, | 37 counter = LinearHistogram::FactoryGet(name, |
81 min, | 38 min, |
82 max, | 39 max, |
83 buckets, | 40 buckets, |
84 Histogram::kUmaTargetedHistogramFlag); | 41 Histogram::kUmaTargetedHistogramFlag); |
85 } else { | 42 } else { |
86 counter = Histogram::FactoryGet(full_name, | 43 counter = Histogram::FactoryGet(name, |
87 min, | 44 min, |
88 max, | 45 max, |
89 buckets, | 46 buckets, |
90 Histogram::kUmaTargetedHistogramFlag); | 47 Histogram::kUmaTargetedHistogramFlag); |
91 } | 48 } |
92 | 49 |
93 counter->Add(sample); | 50 counter->Add(sample); |
94 return true; | 51 return true; |
95 } | 52 } |
96 | 53 |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 return RecordValue(name, Histogram::HISTOGRAM, 1, kThreeMinMs, 50, sample); | 119 return RecordValue(name, Histogram::HISTOGRAM, 1, kThreeMinMs, 50, sample); |
163 } | 120 } |
164 | 121 |
165 bool MetricsRecordLongTimeFunction::RunImpl() { | 122 bool MetricsRecordLongTimeFunction::RunImpl() { |
166 std::string name; | 123 std::string name; |
167 int sample; | 124 int sample; |
168 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); | 125 EXTENSION_FUNCTION_VALIDATE(GetNameAndSample(&name, &sample)); |
169 static const int kOneHourMs = 60 * 60 * 1000; | 126 static const int kOneHourMs = 60 * 60 * 1000; |
170 return RecordValue(name, Histogram::HISTOGRAM, 1, kOneHourMs, 50, sample); | 127 return RecordValue(name, Histogram::HISTOGRAM, 1, kOneHourMs, 50, sample); |
171 } | 128 } |
OLD | NEW |