Commit b8403fda authored by Nate Fischer's avatar Nate Fischer Committed by Commit Bot

UMA: avoid std::unique_ptr in favor of make_unique

No change to logic, this just changes explicit calls to the
std::unique_ptr constructor to instead use make_unique() or implicit
conversion from nullptr. This is to conform with a chromium presubmit
check.

This makes MetricSample's constructor public for compatibility with
make_unique(), and documents the existing static methods are still the
preferred way to instantiate the class.

This also fixes some indentation problems found by 'git-cl format'.

Bug: 1010369
Test: git cl presubmit --upload
Change-Id: I734a48629c435a8c31fe15b8a8d3e44352dcae08
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1834656
Commit-Queue: Nate Fischer <ntfschr@chromium.org>
Reviewed-by: default avatarIlya Sherman <isherman@chromium.org>
Cr-Commit-Position: refs/heads/master@{#702611}
parent c9d4cd17
......@@ -44,16 +44,14 @@ MetricsLogStore::MetricsLogStore(PrefService* local_state,
size_t max_ongoing_log_size,
const std::string& signing_key)
: unsent_logs_loaded_(false),
initial_log_queue_(std::unique_ptr<UnsentLogStoreMetricsImpl>(
new UnsentLogStoreMetricsImpl()),
initial_log_queue_(std::make_unique<UnsentLogStoreMetricsImpl>(),
local_state,
prefs::kMetricsInitialLogs,
kInitialLogsSaveLimit,
kStorageByteLimitPerLogType,
0,
signing_key),
ongoing_log_queue_(std::unique_ptr<UnsentLogStoreMetricsImpl>(
new UnsentLogStoreMetricsImpl()),
ongoing_log_queue_(std::make_unique<UnsentLogStoreMetricsImpl>(),
local_state,
prefs::kMetricsOngoingLogs,
kOngoingLogsSaveLimit,
......
......@@ -47,7 +47,7 @@ void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) {
}
std::unique_ptr<ClientInfo> ReturnNoBackup() {
return std::unique_ptr<ClientInfo>();
return nullptr;
}
class TestMetricsService : public MetricsService {
......
......@@ -281,8 +281,8 @@ MetricsStateManager::CreateDefaultEntropyProvider() {
if (enabled_state_provider_->IsConsentGiven() ||
!provisional_client_id_.empty()) {
UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_HIGH);
return std::unique_ptr<const base::FieldTrial::EntropyProvider>(
new variations::SHA1EntropyProvider(GetHighEntropySource()));
return std::make_unique<variations::SHA1EntropyProvider>(
GetHighEntropySource());
}
UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_LOW);
......
......@@ -119,7 +119,7 @@ class MetricsStateManagerTest : public testing::Test {
// Hands out a copy of |fake_client_info_backup_| if it is set.
std::unique_ptr<ClientInfo> LoadFakeClientInfoBackup() {
if (!fake_client_info_backup_)
return std::unique_ptr<ClientInfo>();
return nullptr;
std::unique_ptr<ClientInfo> backup_copy(new ClientInfo);
backup_copy->client_id = fake_client_info_backup_->client_id;
......
......@@ -99,8 +99,7 @@ int MetricSample::bucket_count() const {
// static
std::unique_ptr<MetricSample> MetricSample::CrashSample(
const std::string& crash_name) {
return std::unique_ptr<MetricSample>(
new MetricSample(CRASH, crash_name, 0, 0, 0, 0));
return std::make_unique<MetricSample>(CRASH, crash_name, 0, 0, 0, 0);
}
// static
......@@ -110,8 +109,8 @@ std::unique_ptr<MetricSample> MetricSample::HistogramSample(
int min,
int max,
int bucket_count) {
return std::unique_ptr<MetricSample>(new MetricSample(
HISTOGRAM, histogram_name, sample, min, max, bucket_count));
return std::make_unique<MetricSample>(HISTOGRAM, histogram_name, sample, min,
max, bucket_count);
}
// static
......@@ -121,13 +120,13 @@ std::unique_ptr<MetricSample> MetricSample::ParseHistogram(
serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (parts.size() != 5)
return std::unique_ptr<MetricSample>();
return nullptr;
int sample, min, max, bucket_count;
if (parts[0].empty() || !base::StringToInt(parts[1], &sample) ||
!base::StringToInt(parts[2], &min) ||
!base::StringToInt(parts[3], &max) ||
!base::StringToInt(parts[4], &bucket_count)) {
return std::unique_ptr<MetricSample>();
return nullptr;
}
return HistogramSample(parts[0].as_string(), sample, min, max, bucket_count);
......@@ -137,8 +136,8 @@ std::unique_ptr<MetricSample> MetricSample::ParseHistogram(
std::unique_ptr<MetricSample> MetricSample::SparseHistogramSample(
const std::string& histogram_name,
int sample) {
return std::unique_ptr<MetricSample>(
new MetricSample(SPARSE_HISTOGRAM, histogram_name, sample, 0, 0, 0));
return std::make_unique<MetricSample>(SPARSE_HISTOGRAM, histogram_name,
sample, 0, 0, 0);
}
// static
......@@ -147,10 +146,10 @@ std::unique_ptr<MetricSample> MetricSample::ParseSparseHistogram(
std::vector<base::StringPiece> parts = base::SplitStringPiece(
serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (parts.size() != 2)
return std::unique_ptr<MetricSample>();
return nullptr;
int sample;
if (parts[0].empty() || !base::StringToInt(parts[1], &sample))
return std::unique_ptr<MetricSample>();
return nullptr;
return SparseHistogramSample(parts[0].as_string(), sample);
}
......@@ -160,8 +159,8 @@ std::unique_ptr<MetricSample> MetricSample::LinearHistogramSample(
const std::string& histogram_name,
int sample,
int max) {
return std::unique_ptr<MetricSample>(
new MetricSample(LINEAR_HISTOGRAM, histogram_name, sample, 0, max, 0));
return std::make_unique<MetricSample>(LINEAR_HISTOGRAM, histogram_name,
sample, 0, max, 0);
}
// static
......@@ -171,10 +170,10 @@ std::unique_ptr<MetricSample> MetricSample::ParseLinearHistogram(
serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
int sample, max;
if (parts.size() != 3)
return std::unique_ptr<MetricSample>();
return nullptr;
if (parts[0].empty() || !base::StringToInt(parts[1], &sample) ||
!base::StringToInt(parts[2], &max)) {
return std::unique_ptr<MetricSample>();
return nullptr;
}
return LinearHistogramSample(parts[0].as_string(), sample, max);
......@@ -183,8 +182,7 @@ std::unique_ptr<MetricSample> MetricSample::ParseLinearHistogram(
// static
std::unique_ptr<MetricSample> MetricSample::UserActionSample(
const std::string& action_name) {
return std::unique_ptr<MetricSample>(
new MetricSample(USER_ACTION, action_name, 0, 0, 0, 0));
return std::make_unique<MetricSample>(USER_ACTION, action_name, 0, 0, 0, 0);
}
bool MetricSample::IsEqual(const MetricSample& metric) {
......
......@@ -26,6 +26,17 @@ class MetricSample {
USER_ACTION
};
// Use one of the static methods in this class instead of calling the
// constructor directly.
//
// The constructor is exposed for std::make_unique.
MetricSample(SampleType sample_type,
const std::string& metric_name,
const int sample,
const int min,
const int max,
const int bucket_count);
~MetricSample();
// Returns true if the sample is valid (can be serialized without ambiguity).
......@@ -96,13 +107,6 @@ class MetricSample {
bool IsEqual(const MetricSample& sample);
private:
MetricSample(SampleType sample_type,
const std::string& metric_name,
const int sample,
const int min,
const int max,
const int bucket_count);
const SampleType type_;
const std::string name_;
const int sample_;
......
......@@ -91,7 +91,7 @@ bool ReadMessage(int fd, std::string* message) {
std::unique_ptr<MetricSample> SerializationUtils::ParseSample(
const std::string& sample) {
if (sample.empty())
return std::unique_ptr<MetricSample>();
return nullptr;
std::vector<std::string> parts = base::SplitString(
sample, std::string(1, '\0'),
......@@ -101,7 +101,7 @@ std::unique_ptr<MetricSample> SerializationUtils::ParseSample(
if (parts.size() != 3) {
DLOG(ERROR) << "splitting message on \\0 produced " << parts.size()
<< " parts (expected 3)";
return std::unique_ptr<MetricSample>();
return nullptr;
}
const std::string& name = parts[0];
const std::string& value = parts[1];
......@@ -117,7 +117,7 @@ std::unique_ptr<MetricSample> SerializationUtils::ParseSample(
if (base::LowerCaseEqualsASCII(name, "useraction"))
return MetricSample::UserActionSample(value);
DLOG(ERROR) << "invalid event type: " << name << ", value: " << value;
return std::unique_ptr<MetricSample>();
return nullptr;
}
void SerializationUtils::ReadAndTruncateMetricsFromFile(
......
......@@ -64,25 +64,23 @@ class UnsentLogStoreTest : public testing::Test {
class TestUnsentLogStore : public UnsentLogStore {
public:
TestUnsentLogStore(PrefService* service, size_t min_log_bytes)
: UnsentLogStore(std::unique_ptr<UnsentLogStoreMetricsImpl>(
new UnsentLogStoreMetricsImpl()),
service,
kTestPrefName,
kLogCountLimit,
min_log_bytes,
0,
std::string()) {}
: UnsentLogStore(std::make_unique<UnsentLogStoreMetricsImpl>(),
service,
kTestPrefName,
kLogCountLimit,
min_log_bytes,
0,
std::string()) {}
TestUnsentLogStore(PrefService* service,
size_t min_log_bytes,
const std::string& signing_key)
: UnsentLogStore(std::unique_ptr<UnsentLogStoreMetricsImpl>(
new UnsentLogStoreMetricsImpl()),
service,
kTestPrefName,
kLogCountLimit,
min_log_bytes,
0,
signing_key) {}
size_t min_log_bytes,
const std::string& signing_key)
: UnsentLogStore(std::make_unique<UnsentLogStoreMetricsImpl>(),
service,
kTestPrefName,
kLogCountLimit,
min_log_bytes,
0,
signing_key) {}
// Stages and removes the next log, while testing it's value.
void ExpectNextLog(const std::string& expected_log) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment