Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(209)

Side by Side Diff: chrome/browser/extensions/api/metrics_private/metrics_private_api.cc

Issue 2261313002: [Extensions] Convert some SyncExtensionFunctions (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ready Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <limits.h> 7 #include <limits.h>
8 8
9 #include <algorithm> 9 #include <algorithm>
10 10
(...skipping 24 matching lines...) Expand all
35 namespace RecordMediumTime = api::metrics_private::RecordMediumTime; 35 namespace RecordMediumTime = api::metrics_private::RecordMediumTime;
36 namespace RecordLongTime = api::metrics_private::RecordLongTime; 36 namespace RecordLongTime = api::metrics_private::RecordLongTime;
37 37
38 namespace { 38 namespace {
39 39
40 const size_t kMaxBuckets = 10000; // We don't ever want more than these many 40 const size_t kMaxBuckets = 10000; // We don't ever want more than these many
41 // buckets; there is no real need for them 41 // buckets; there is no real need for them
42 // and would cause crazy memory usage 42 // and would cause crazy memory usage
43 } // namespace 43 } // namespace
44 44
45 bool MetricsPrivateGetIsCrashReportingEnabledFunction::RunSync() { 45 ExtensionFunction::ResponseAction
46 SetResult(base::MakeUnique<base::FundamentalValue>( 46 MetricsPrivateGetIsCrashReportingEnabledFunction::Run() {
47 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled())); 47 return RespondNow(OneArgument(base::MakeUnique<base::FundamentalValue>(
48 return true; 48 ChromeMetricsServiceAccessor::IsMetricsAndCrashReportingEnabled())));
49 } 49 }
50 50
51 bool MetricsPrivateGetFieldTrialFunction::RunSync() { 51 ExtensionFunction::ResponseAction MetricsPrivateGetFieldTrialFunction::Run() {
52 std::string name; 52 std::string name;
53 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name)); 53 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &name));
54 54
55 SetResult(base::MakeUnique<base::StringValue>( 55 return RespondNow(OneArgument(base::MakeUnique<base::StringValue>(
56 base::FieldTrialList::FindFullName(name))); 56 base::FieldTrialList::FindFullName(name))));
57 return true;
58 } 57 }
59 58
60 bool MetricsPrivateGetVariationParamsFunction::RunSync() { 59 ExtensionFunction::ResponseAction
60 MetricsPrivateGetVariationParamsFunction::Run() {
61 std::unique_ptr<GetVariationParams::Params> params( 61 std::unique_ptr<GetVariationParams::Params> params(
62 GetVariationParams::Params::Create(*args_)); 62 GetVariationParams::Params::Create(*args_));
63 EXTENSION_FUNCTION_VALIDATE(params.get()); 63 EXTENSION_FUNCTION_VALIDATE(params.get());
64 64
65 GetVariationParams::Results::Params result; 65 GetVariationParams::Results::Params result;
66 std::unique_ptr<base::DictionaryValue> dict;
66 if (variations::GetVariationParams(params->name, 67 if (variations::GetVariationParams(params->name,
67 &result.additional_properties)) { 68 &result.additional_properties)) {
68 SetResult(result.ToValue()); 69 dict = result.ToValue();
69 } 70 }
70 return true; 71 return RespondNow(dict ? OneArgument(std::move(dict)) : NoArguments());
71 } 72 }
72 73
73 bool MetricsPrivateRecordUserActionFunction::RunSync() { 74 ExtensionFunction::ResponseAction
75 MetricsPrivateRecordUserActionFunction::Run() {
74 std::unique_ptr<RecordUserAction::Params> params( 76 std::unique_ptr<RecordUserAction::Params> params(
75 RecordUserAction::Params::Create(*args_)); 77 RecordUserAction::Params::Create(*args_));
76 EXTENSION_FUNCTION_VALIDATE(params.get()); 78 EXTENSION_FUNCTION_VALIDATE(params.get());
77 79
78 content::RecordComputedAction(params->name); 80 content::RecordComputedAction(params->name);
79 return true; 81 return RespondNow(NoArguments());
80 } 82 }
81 83
82 bool MetricsHistogramHelperFunction::RecordValue( 84 void MetricsHistogramHelperFunction::RecordValue(const std::string& name,
83 const std::string& name, 85 base::HistogramType type,
84 base::HistogramType type, 86 int min,
85 int min, int max, size_t buckets, 87 int max,
86 int sample) { 88 size_t buckets,
89 int sample) {
87 // Make sure toxic values don't get to internal code. 90 // Make sure toxic values don't get to internal code.
88 // Fix for maximums 91 // Fix for maximums
89 min = std::min(min, INT_MAX - 3); 92 min = std::min(min, INT_MAX - 3);
90 max = std::min(max, INT_MAX - 3); 93 max = std::min(max, INT_MAX - 3);
91 buckets = std::min(buckets, kMaxBuckets); 94 buckets = std::min(buckets, kMaxBuckets);
92 // Fix for minimums. 95 // Fix for minimums.
93 min = std::max(min, 1); 96 min = std::max(min, 1);
94 max = std::max(max, min + 1); 97 max = std::max(max, min + 1);
95 buckets = std::max(buckets, static_cast<size_t>(3)); 98 buckets = std::max(buckets, static_cast<size_t>(3));
96 // Trim buckets down to a maximum of the given range + over/underflow buckets 99 // Trim buckets down to a maximum of the given range + over/underflow buckets
97 if (buckets > static_cast<size_t>(max - min + 2)) 100 if (buckets > static_cast<size_t>(max - min + 2))
98 buckets = max - min + 2; 101 buckets = max - min + 2;
99 102
100 base::HistogramBase* counter; 103 base::HistogramBase* counter;
101 if (type == base::LINEAR_HISTOGRAM) { 104 if (type == base::LINEAR_HISTOGRAM) {
102 counter = base::LinearHistogram::FactoryGet( 105 counter = base::LinearHistogram::FactoryGet(
103 name, min, max, buckets, 106 name, min, max, buckets,
104 base::HistogramBase::kUmaTargetedHistogramFlag); 107 base::HistogramBase::kUmaTargetedHistogramFlag);
105 } else { 108 } else {
106 counter = base::Histogram::FactoryGet( 109 counter = base::Histogram::FactoryGet(
107 name, min, max, buckets, 110 name, min, max, buckets,
108 base::HistogramBase::kUmaTargetedHistogramFlag); 111 base::HistogramBase::kUmaTargetedHistogramFlag);
109 } 112 }
110 113
111 // The histogram can be NULL if it is constructed with bad arguments. Ignore 114 // The histogram can be NULL if it is constructed with bad arguments. Ignore
112 // that data for this API. An error message will be logged. 115 // that data for this API. An error message will be logged.
113 if (counter) 116 if (counter)
114 counter->Add(sample); 117 counter->Add(sample);
115 return true;
116 } 118 }
117 119
118 bool MetricsPrivateRecordValueFunction::RunSync() { 120 ExtensionFunction::ResponseAction MetricsPrivateRecordValueFunction::Run() {
119 std::unique_ptr<RecordValue::Params> params( 121 std::unique_ptr<RecordValue::Params> params(
120 RecordValue::Params::Create(*args_)); 122 RecordValue::Params::Create(*args_));
121 EXTENSION_FUNCTION_VALIDATE(params.get()); 123 EXTENSION_FUNCTION_VALIDATE(params.get());
122 124
123 // Get the histogram parameters from the metric type object. 125 // Get the histogram parameters from the metric type object.
124 std::string type = api::metrics_private::ToString(params->metric.type); 126 std::string type = api::metrics_private::ToString(params->metric.type);
125 127
126 base::HistogramType histogram_type(type == "histogram-linear" ? 128 base::HistogramType histogram_type(type == "histogram-linear" ?
127 base::LINEAR_HISTOGRAM : base::HISTOGRAM); 129 base::LINEAR_HISTOGRAM : base::HISTOGRAM);
128 return RecordValue(params->metric.metric_name, histogram_type, 130 RecordValue(params->metric.metric_name, histogram_type, params->metric.min,
129 params->metric.min, params->metric.max, 131 params->metric.max, params->metric.buckets, params->value);
130 params->metric.buckets, params->value); 132 return RespondNow(NoArguments());
131 } 133 }
132 134
133 bool MetricsPrivateRecordSparseValueFunction::RunSync() { 135 ExtensionFunction::ResponseAction
136 MetricsPrivateRecordSparseValueFunction::Run() {
134 std::unique_ptr<RecordSparseValue::Params> params( 137 std::unique_ptr<RecordSparseValue::Params> params(
135 RecordSparseValue::Params::Create(*args_)); 138 RecordSparseValue::Params::Create(*args_));
136 EXTENSION_FUNCTION_VALIDATE(params.get()); 139 EXTENSION_FUNCTION_VALIDATE(params.get());
137 // This particular UMA_HISTOGRAM_ macro is okay for 140 // This particular UMA_HISTOGRAM_ macro is okay for
138 // non-runtime-constant strings. 141 // non-runtime-constant strings.
139 UMA_HISTOGRAM_SPARSE_SLOWLY(params->metric_name, params->value); 142 UMA_HISTOGRAM_SPARSE_SLOWLY(params->metric_name, params->value);
140 return true; 143 return RespondNow(NoArguments());
141 } 144 }
142 145
143 bool MetricsPrivateRecordPercentageFunction::RunSync() { 146 ExtensionFunction::ResponseAction
147 MetricsPrivateRecordPercentageFunction::Run() {
144 std::unique_ptr<RecordPercentage::Params> params( 148 std::unique_ptr<RecordPercentage::Params> params(
145 RecordPercentage::Params::Create(*args_)); 149 RecordPercentage::Params::Create(*args_));
146 EXTENSION_FUNCTION_VALIDATE(params.get()); 150 EXTENSION_FUNCTION_VALIDATE(params.get());
147 return RecordValue(params->metric_name, base::LINEAR_HISTOGRAM, 151 RecordValue(params->metric_name, base::LINEAR_HISTOGRAM, 1, 101, 102,
148 1, 101, 102, params->value); 152 params->value);
153 return RespondNow(NoArguments());
149 } 154 }
150 155
151 bool MetricsPrivateRecordCountFunction::RunSync() { 156 ExtensionFunction::ResponseAction MetricsPrivateRecordCountFunction::Run() {
152 std::unique_ptr<RecordCount::Params> params( 157 std::unique_ptr<RecordCount::Params> params(
153 RecordCount::Params::Create(*args_)); 158 RecordCount::Params::Create(*args_));
154 EXTENSION_FUNCTION_VALIDATE(params.get()); 159 EXTENSION_FUNCTION_VALIDATE(params.get());
155 return RecordValue(params->metric_name, base::HISTOGRAM, 160 RecordValue(params->metric_name, base::HISTOGRAM, 1, 1000000, 50,
156 1, 1000000, 50, params->value); 161 params->value);
162 return RespondNow(NoArguments());
157 } 163 }
158 164
159 bool MetricsPrivateRecordSmallCountFunction::RunSync() { 165 ExtensionFunction::ResponseAction
166 MetricsPrivateRecordSmallCountFunction::Run() {
160 std::unique_ptr<RecordSmallCount::Params> params( 167 std::unique_ptr<RecordSmallCount::Params> params(
161 RecordSmallCount::Params::Create(*args_)); 168 RecordSmallCount::Params::Create(*args_));
162 EXTENSION_FUNCTION_VALIDATE(params.get()); 169 EXTENSION_FUNCTION_VALIDATE(params.get());
163 return RecordValue(params->metric_name, base::HISTOGRAM, 170 RecordValue(params->metric_name, base::HISTOGRAM, 1, 100, 50, params->value);
164 1, 100, 50, params->value); 171 return RespondNow(NoArguments());
165 } 172 }
166 173
167 bool MetricsPrivateRecordMediumCountFunction::RunSync() { 174 ExtensionFunction::ResponseAction
175 MetricsPrivateRecordMediumCountFunction::Run() {
168 std::unique_ptr<RecordMediumCount::Params> params( 176 std::unique_ptr<RecordMediumCount::Params> params(
169 RecordMediumCount::Params::Create(*args_)); 177 RecordMediumCount::Params::Create(*args_));
170 EXTENSION_FUNCTION_VALIDATE(params.get()); 178 EXTENSION_FUNCTION_VALIDATE(params.get());
171 return RecordValue(params->metric_name, base::HISTOGRAM, 179 RecordValue(params->metric_name, base::HISTOGRAM, 1, 10000, 50,
172 1, 10000, 50, params->value); 180 params->value);
181 return RespondNow(NoArguments());
173 } 182 }
174 183
175 bool MetricsPrivateRecordTimeFunction::RunSync() { 184 ExtensionFunction::ResponseAction MetricsPrivateRecordTimeFunction::Run() {
176 std::unique_ptr<RecordTime::Params> params( 185 std::unique_ptr<RecordTime::Params> params(
177 RecordTime::Params::Create(*args_)); 186 RecordTime::Params::Create(*args_));
178 EXTENSION_FUNCTION_VALIDATE(params.get()); 187 EXTENSION_FUNCTION_VALIDATE(params.get());
179 static const int kTenSecMs = 10 * 1000; 188 static const int kTenSecMs = 10 * 1000;
180 return RecordValue(params->metric_name, base::HISTOGRAM, 189 RecordValue(params->metric_name, base::HISTOGRAM, 1, kTenSecMs, 50,
181 1, kTenSecMs, 50, params->value); 190 params->value);
191 return RespondNow(NoArguments());
182 } 192 }
183 193
184 bool MetricsPrivateRecordMediumTimeFunction::RunSync() { 194 ExtensionFunction::ResponseAction
195 MetricsPrivateRecordMediumTimeFunction::Run() {
185 std::unique_ptr<RecordMediumTime::Params> params( 196 std::unique_ptr<RecordMediumTime::Params> params(
186 RecordMediumTime::Params::Create(*args_)); 197 RecordMediumTime::Params::Create(*args_));
187 EXTENSION_FUNCTION_VALIDATE(params.get()); 198 EXTENSION_FUNCTION_VALIDATE(params.get());
188 static const int kThreeMinMs = 3 * 60 * 1000; 199 static const int kThreeMinMs = 3 * 60 * 1000;
189 return RecordValue(params->metric_name, base::HISTOGRAM, 200 RecordValue(params->metric_name, base::HISTOGRAM, 1, kThreeMinMs, 50,
190 1, kThreeMinMs, 50, params->value); 201 params->value);
202 return RespondNow(NoArguments());
191 } 203 }
192 204
193 bool MetricsPrivateRecordLongTimeFunction::RunSync() { 205 ExtensionFunction::ResponseAction MetricsPrivateRecordLongTimeFunction::Run() {
194 std::unique_ptr<RecordLongTime::Params> params( 206 std::unique_ptr<RecordLongTime::Params> params(
195 RecordLongTime::Params::Create(*args_)); 207 RecordLongTime::Params::Create(*args_));
196 EXTENSION_FUNCTION_VALIDATE(params.get()); 208 EXTENSION_FUNCTION_VALIDATE(params.get());
197 static const int kOneHourMs = 60 * 60 * 1000; 209 static const int kOneHourMs = 60 * 60 * 1000;
198 return RecordValue(params->metric_name, base::HISTOGRAM, 210 RecordValue(params->metric_name, base::HISTOGRAM, 1, kOneHourMs, 50,
199 1, kOneHourMs, 50, params->value); 211 params->value);
212 return RespondNow(NoArguments());
200 } 213 }
201 214
202 } // namespace extensions 215 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698