1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/browser/api/metrics_private/metrics_private_api.h"
#include <limits.h>
#include <algorithm>
#include <memory>
#include <utility>
#include "base/hash/hash.h"
#include "base/metrics/field_trial.h"
#include "base/metrics/field_trial_params.h"
#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_samples.h"
#include "base/metrics/metrics_hashes.h"
#include "base/metrics/statistics_recorder.h"
#include "base/metrics/user_metrics.h"
#include "base/strings/strcat.h"
#include "base/task/single_thread_task_runner.h"
#include "content/public/browser/histogram_fetcher.h"
#include "extensions/browser/api/extensions_api_client.h"
#include "extensions/browser/api/metrics_private/metrics_private_delegate.h"
#include "extensions/common/api/metrics_private.h"
namespace extensions {
namespace GetVariationParams = api::metrics_private::GetVariationParams;
namespace RecordUserAction = api::metrics_private::RecordUserAction;
namespace RecordValue = api::metrics_private::RecordValue;
namespace RecordBoolean = api::metrics_private::RecordBoolean;
namespace RecordEnumerationValue = api::metrics_private::RecordEnumerationValue;
namespace RecordSparseValueWithHashMetricName =
api::metrics_private::RecordSparseValueWithHashMetricName;
namespace RecordSparseValueWithPersistentHash =
api::metrics_private::RecordSparseValueWithPersistentHash;
namespace RecordSparseValue = api::metrics_private::RecordSparseValue;
namespace RecordPercentage = api::metrics_private::RecordPercentage;
namespace RecordCount = api::metrics_private::RecordCount;
namespace RecordSmallCount = api::metrics_private::RecordSmallCount;
namespace RecordMediumCount = api::metrics_private::RecordMediumCount;
namespace RecordTime = api::metrics_private::RecordTime;
namespace RecordMediumTime = api::metrics_private::RecordMediumTime;
namespace RecordLongTime = api::metrics_private::RecordLongTime;
namespace {
const size_t kMaxBuckets = 10000; // We don't ever want more than these many
// buckets; there is no real need for them
// and would cause crazy memory usage
// Amount of time to give other processes to report their histograms.
constexpr base::TimeDelta kHistogramsRefreshTimeout = base::Seconds(10);
} // namespace
ExtensionFunction::ResponseAction
MetricsPrivateGetIsCrashReportingEnabledFunction::Run() {
MetricsPrivateDelegate* delegate =
ExtensionsAPIClient::Get()->GetMetricsPrivateDelegate();
return RespondNow(
WithArguments(delegate && delegate->IsCrashReportingEnabled()));
}
ExtensionFunction::ResponseAction MetricsPrivateGetFieldTrialFunction::Run() {
EXTENSION_FUNCTION_VALIDATE(args().size() >= 1);
EXTENSION_FUNCTION_VALIDATE(args()[0].is_string());
const std::string& name = args()[0].GetString();
return RespondNow(WithArguments(base::FieldTrialList::FindFullName(name)));
}
ExtensionFunction::ResponseAction
MetricsPrivateGetVariationParamsFunction::Run() {
std::optional<GetVariationParams::Params> params =
GetVariationParams::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
GetVariationParams::Results::Params result;
if (base::GetFieldTrialParams(params->name, &result.additional_properties)) {
return RespondNow(WithArguments(result.ToValue()));
}
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordUserActionFunction::Run() {
std::optional<RecordUserAction::Params> params =
RecordUserAction::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
base::RecordComputedAction(params->name);
return RespondNow(NoArguments());
}
void MetricsHistogramHelperFunction::RecordValue(const std::string& name,
base::HistogramType type,
int min,
int max,
size_t buckets,
int sample) {
// Make sure toxic values don't get to internal code.
// Fix for maximums
min = std::min(min, INT_MAX - 3);
max = std::min(max, INT_MAX - 3);
buckets = std::min(buckets, kMaxBuckets);
// Fix for minimums.
min = std::max(min, 1);
max = std::max(max, min + 1);
buckets = std::max(buckets, static_cast<size_t>(3));
// Trim buckets down to a maximum of the given range + over/underflow buckets
if (buckets > static_cast<size_t>(max - min + 2)) {
buckets = max - min + 2;
}
base::HistogramBase* counter;
if (type == base::LINEAR_HISTOGRAM) {
counter = base::LinearHistogram::FactoryGet(
name, min, max, buckets,
base::HistogramBase::kUmaTargetedHistogramFlag);
} else {
counter = base::Histogram::FactoryGet(
name, min, max, buckets,
base::HistogramBase::kUmaTargetedHistogramFlag);
}
// The histogram can be NULL if it is constructed with bad arguments. Ignore
// that data for this API. An error message will be logged.
if (counter) {
counter->Add(sample);
}
}
ExtensionFunction::ResponseAction MetricsPrivateRecordValueFunction::Run() {
std::optional<RecordValue::Params> params =
RecordValue::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
// Get the histogram parameters from the metric type object.
std::string type = api::metrics_private::ToString(params->metric.type);
base::HistogramType histogram_type(
type == "histogram-linear" ? base::LINEAR_HISTOGRAM : base::HISTOGRAM);
RecordValue(params->metric.metric_name, histogram_type, params->metric.min,
params->metric.max, params->metric.buckets, params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordSparseValueWithHashMetricNameFunction::Run() {
auto params = RecordSparseValueWithHashMetricName::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
base::UmaHistogramSparse(params->metric_name,
base::HashMetricName(params->value));
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordSparseValueWithPersistentHashFunction::Run() {
auto params = RecordSparseValueWithPersistentHash::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
base::UmaHistogramSparse(params->metric_name,
base::PersistentHash(params->value));
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordSparseValueFunction::Run() {
std::optional<RecordSparseValue::Params> params =
RecordSparseValue::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
base::UmaHistogramSparse(params->metric_name, params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction MetricsPrivateRecordBooleanFunction::Run() {
std::optional<RecordBoolean::Params> params =
RecordBoolean::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
base::UmaHistogramBoolean(params->metric_name, params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordEnumerationValueFunction::Run() {
std::optional<RecordEnumerationValue::Params> params =
RecordEnumerationValue::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
// Uses UmaHistogramExactLinear instead of UmaHistogramEnumeration
// because we don't have an enum type on params->value.
base::UmaHistogramExactLinear(params->metric_name, params->value,
params->enum_size);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordPercentageFunction::Run() {
std::optional<RecordPercentage::Params> params =
RecordPercentage::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
RecordValue(params->metric_name, base::LINEAR_HISTOGRAM, 1, 101, 102,
params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction MetricsPrivateRecordCountFunction::Run() {
std::optional<RecordCount::Params> params =
RecordCount::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
RecordValue(params->metric_name, base::HISTOGRAM, 1, 1000000, 50,
params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordSmallCountFunction::Run() {
std::optional<RecordSmallCount::Params> params =
RecordSmallCount::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
RecordValue(params->metric_name, base::HISTOGRAM, 1, 100, 50, params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordMediumCountFunction::Run() {
std::optional<RecordMediumCount::Params> params =
RecordMediumCount::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
RecordValue(params->metric_name, base::HISTOGRAM, 1, 10000, 50,
params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction MetricsPrivateRecordTimeFunction::Run() {
std::optional<RecordTime::Params> params = RecordTime::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
static const int kTenSecMs = 10 * 1000;
RecordValue(params->metric_name, base::HISTOGRAM, 1, kTenSecMs, 50,
params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction
MetricsPrivateRecordMediumTimeFunction::Run() {
std::optional<RecordMediumTime::Params> params =
RecordMediumTime::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
static const int kThreeMinMs = 3 * 60 * 1000;
RecordValue(params->metric_name, base::HISTOGRAM, 1, kThreeMinMs, 50,
params->value);
return RespondNow(NoArguments());
}
ExtensionFunction::ResponseAction MetricsPrivateRecordLongTimeFunction::Run() {
std::optional<RecordLongTime::Params> params =
RecordLongTime::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
static const int kOneHourMs = 60 * 60 * 1000;
RecordValue(params->metric_name, base::HISTOGRAM, 1, kOneHourMs, 50,
params->value);
return RespondNow(NoArguments());
}
MetricsPrivateGetHistogramFunction::~MetricsPrivateGetHistogramFunction() =
default;
ExtensionFunction::ResponseAction MetricsPrivateGetHistogramFunction::Run() {
std::optional<api::metrics_private::GetHistogram::Params> params =
api::metrics_private::GetHistogram::Params::Create(args());
EXTENSION_FUNCTION_VALIDATE(params);
// Collect histogram data from other processes before responding. Otherwise,
// we'd report stale data for histograms that are e.g. recorded by renderers.
content::FetchHistogramsAsynchronously(
base::SingleThreadTaskRunner::GetCurrentDefault(),
base::BindOnce(
&MetricsPrivateGetHistogramFunction::RespondOnHistogramsFetched, this,
params->name),
kHistogramsRefreshTimeout);
return RespondLater();
}
void MetricsPrivateGetHistogramFunction::RespondOnHistogramsFetched(
const std::string& name) {
// Incorporate the data collected by content::FetchHistogramsAsynchronously().
base::StatisticsRecorder::ImportProvidedHistogramsSync();
Respond(GetHistogram(name));
}
ExtensionFunction::ResponseValue
MetricsPrivateGetHistogramFunction::GetHistogram(const std::string& name) {
const base::HistogramBase* histogram =
base::StatisticsRecorder::FindHistogram(name);
if (!histogram) {
return Error(base::StrCat({"Histogram ", name, " not found"}));
}
std::unique_ptr<base::HistogramSamples> samples =
histogram->SnapshotSamples();
api::metrics_private::Histogram result;
result.sum = samples->sum();
for (std::unique_ptr<base::SampleCountIterator> it = samples->Iterator();
!it->Done(); it->Next()) {
base::HistogramBase::Sample32 min = 0;
int64_t max = 0;
base::HistogramBase::Count32 count = 0;
it->Get(&min, &max, &count);
api::metrics_private::HistogramBucket bucket;
bucket.min = min;
bucket.max = max;
bucket.count = count;
result.buckets.push_back(std::move(bucket));
}
return WithArguments(result.ToValue());
}
} // namespace extensions
|