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, ...@@ -44,16 +44,14 @@ MetricsLogStore::MetricsLogStore(PrefService* local_state,
size_t max_ongoing_log_size, size_t max_ongoing_log_size,
const std::string& signing_key) const std::string& signing_key)
: unsent_logs_loaded_(false), : unsent_logs_loaded_(false),
initial_log_queue_(std::unique_ptr<UnsentLogStoreMetricsImpl>( initial_log_queue_(std::make_unique<UnsentLogStoreMetricsImpl>(),
new UnsentLogStoreMetricsImpl()),
local_state, local_state,
prefs::kMetricsInitialLogs, prefs::kMetricsInitialLogs,
kInitialLogsSaveLimit, kInitialLogsSaveLimit,
kStorageByteLimitPerLogType, kStorageByteLimitPerLogType,
0, 0,
signing_key), signing_key),
ongoing_log_queue_(std::unique_ptr<UnsentLogStoreMetricsImpl>( ongoing_log_queue_(std::make_unique<UnsentLogStoreMetricsImpl>(),
new UnsentLogStoreMetricsImpl()),
local_state, local_state,
prefs::kMetricsOngoingLogs, prefs::kMetricsOngoingLogs,
kOngoingLogsSaveLimit, kOngoingLogsSaveLimit,
......
...@@ -47,7 +47,7 @@ void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) { ...@@ -47,7 +47,7 @@ void StoreNoClientInfoBackup(const ClientInfo& /* client_info */) {
} }
std::unique_ptr<ClientInfo> ReturnNoBackup() { std::unique_ptr<ClientInfo> ReturnNoBackup() {
return std::unique_ptr<ClientInfo>(); return nullptr;
} }
class TestMetricsService : public MetricsService { class TestMetricsService : public MetricsService {
......
...@@ -281,8 +281,8 @@ MetricsStateManager::CreateDefaultEntropyProvider() { ...@@ -281,8 +281,8 @@ MetricsStateManager::CreateDefaultEntropyProvider() {
if (enabled_state_provider_->IsConsentGiven() || if (enabled_state_provider_->IsConsentGiven() ||
!provisional_client_id_.empty()) { !provisional_client_id_.empty()) {
UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_HIGH); UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_HIGH);
return std::unique_ptr<const base::FieldTrial::EntropyProvider>( return std::make_unique<variations::SHA1EntropyProvider>(
new variations::SHA1EntropyProvider(GetHighEntropySource())); GetHighEntropySource());
} }
UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_LOW); UpdateEntropySourceReturnedValue(ENTROPY_SOURCE_LOW);
......
...@@ -119,7 +119,7 @@ class MetricsStateManagerTest : public testing::Test { ...@@ -119,7 +119,7 @@ class MetricsStateManagerTest : public testing::Test {
// Hands out a copy of |fake_client_info_backup_| if it is set. // Hands out a copy of |fake_client_info_backup_| if it is set.
std::unique_ptr<ClientInfo> LoadFakeClientInfoBackup() { std::unique_ptr<ClientInfo> LoadFakeClientInfoBackup() {
if (!fake_client_info_backup_) if (!fake_client_info_backup_)
return std::unique_ptr<ClientInfo>(); return nullptr;
std::unique_ptr<ClientInfo> backup_copy(new ClientInfo); std::unique_ptr<ClientInfo> backup_copy(new ClientInfo);
backup_copy->client_id = fake_client_info_backup_->client_id; backup_copy->client_id = fake_client_info_backup_->client_id;
......
...@@ -99,8 +99,7 @@ int MetricSample::bucket_count() const { ...@@ -99,8 +99,7 @@ int MetricSample::bucket_count() const {
// static // static
std::unique_ptr<MetricSample> MetricSample::CrashSample( std::unique_ptr<MetricSample> MetricSample::CrashSample(
const std::string& crash_name) { const std::string& crash_name) {
return std::unique_ptr<MetricSample>( return std::make_unique<MetricSample>(CRASH, crash_name, 0, 0, 0, 0);
new MetricSample(CRASH, crash_name, 0, 0, 0, 0));
} }
// static // static
...@@ -110,8 +109,8 @@ std::unique_ptr<MetricSample> MetricSample::HistogramSample( ...@@ -110,8 +109,8 @@ std::unique_ptr<MetricSample> MetricSample::HistogramSample(
int min, int min,
int max, int max,
int bucket_count) { int bucket_count) {
return std::unique_ptr<MetricSample>(new MetricSample( return std::make_unique<MetricSample>(HISTOGRAM, histogram_name, sample, min,
HISTOGRAM, histogram_name, sample, min, max, bucket_count)); max, bucket_count);
} }
// static // static
...@@ -121,13 +120,13 @@ std::unique_ptr<MetricSample> MetricSample::ParseHistogram( ...@@ -121,13 +120,13 @@ std::unique_ptr<MetricSample> MetricSample::ParseHistogram(
serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (parts.size() != 5) if (parts.size() != 5)
return std::unique_ptr<MetricSample>(); return nullptr;
int sample, min, max, bucket_count; int sample, min, max, bucket_count;
if (parts[0].empty() || !base::StringToInt(parts[1], &sample) || if (parts[0].empty() || !base::StringToInt(parts[1], &sample) ||
!base::StringToInt(parts[2], &min) || !base::StringToInt(parts[2], &min) ||
!base::StringToInt(parts[3], &max) || !base::StringToInt(parts[3], &max) ||
!base::StringToInt(parts[4], &bucket_count)) { !base::StringToInt(parts[4], &bucket_count)) {
return std::unique_ptr<MetricSample>(); return nullptr;
} }
return HistogramSample(parts[0].as_string(), sample, min, max, bucket_count); return HistogramSample(parts[0].as_string(), sample, min, max, bucket_count);
...@@ -137,8 +136,8 @@ std::unique_ptr<MetricSample> MetricSample::ParseHistogram( ...@@ -137,8 +136,8 @@ std::unique_ptr<MetricSample> MetricSample::ParseHistogram(
std::unique_ptr<MetricSample> MetricSample::SparseHistogramSample( std::unique_ptr<MetricSample> MetricSample::SparseHistogramSample(
const std::string& histogram_name, const std::string& histogram_name,
int sample) { int sample) {
return std::unique_ptr<MetricSample>( return std::make_unique<MetricSample>(SPARSE_HISTOGRAM, histogram_name,
new MetricSample(SPARSE_HISTOGRAM, histogram_name, sample, 0, 0, 0)); sample, 0, 0, 0);
} }
// static // static
...@@ -147,10 +146,10 @@ std::unique_ptr<MetricSample> MetricSample::ParseSparseHistogram( ...@@ -147,10 +146,10 @@ std::unique_ptr<MetricSample> MetricSample::ParseSparseHistogram(
std::vector<base::StringPiece> parts = base::SplitStringPiece( std::vector<base::StringPiece> parts = base::SplitStringPiece(
serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
if (parts.size() != 2) if (parts.size() != 2)
return std::unique_ptr<MetricSample>(); return nullptr;
int sample; int sample;
if (parts[0].empty() || !base::StringToInt(parts[1], &sample)) if (parts[0].empty() || !base::StringToInt(parts[1], &sample))
return std::unique_ptr<MetricSample>(); return nullptr;
return SparseHistogramSample(parts[0].as_string(), sample); return SparseHistogramSample(parts[0].as_string(), sample);
} }
...@@ -160,8 +159,8 @@ std::unique_ptr<MetricSample> MetricSample::LinearHistogramSample( ...@@ -160,8 +159,8 @@ std::unique_ptr<MetricSample> MetricSample::LinearHistogramSample(
const std::string& histogram_name, const std::string& histogram_name,
int sample, int sample,
int max) { int max) {
return std::unique_ptr<MetricSample>( return std::make_unique<MetricSample>(LINEAR_HISTOGRAM, histogram_name,
new MetricSample(LINEAR_HISTOGRAM, histogram_name, sample, 0, max, 0)); sample, 0, max, 0);
} }
// static // static
...@@ -171,10 +170,10 @@ std::unique_ptr<MetricSample> MetricSample::ParseLinearHistogram( ...@@ -171,10 +170,10 @@ std::unique_ptr<MetricSample> MetricSample::ParseLinearHistogram(
serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); serialized_histogram, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
int sample, max; int sample, max;
if (parts.size() != 3) if (parts.size() != 3)
return std::unique_ptr<MetricSample>(); return nullptr;
if (parts[0].empty() || !base::StringToInt(parts[1], &sample) || if (parts[0].empty() || !base::StringToInt(parts[1], &sample) ||
!base::StringToInt(parts[2], &max)) { !base::StringToInt(parts[2], &max)) {
return std::unique_ptr<MetricSample>(); return nullptr;
} }
return LinearHistogramSample(parts[0].as_string(), sample, max); return LinearHistogramSample(parts[0].as_string(), sample, max);
...@@ -183,8 +182,7 @@ std::unique_ptr<MetricSample> MetricSample::ParseLinearHistogram( ...@@ -183,8 +182,7 @@ std::unique_ptr<MetricSample> MetricSample::ParseLinearHistogram(
// static // static
std::unique_ptr<MetricSample> MetricSample::UserActionSample( std::unique_ptr<MetricSample> MetricSample::UserActionSample(
const std::string& action_name) { const std::string& action_name) {
return std::unique_ptr<MetricSample>( return std::make_unique<MetricSample>(USER_ACTION, action_name, 0, 0, 0, 0);
new MetricSample(USER_ACTION, action_name, 0, 0, 0, 0));
} }
bool MetricSample::IsEqual(const MetricSample& metric) { bool MetricSample::IsEqual(const MetricSample& metric) {
......
...@@ -26,6 +26,17 @@ class MetricSample { ...@@ -26,6 +26,17 @@ class MetricSample {
USER_ACTION 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(); ~MetricSample();
// Returns true if the sample is valid (can be serialized without ambiguity). // Returns true if the sample is valid (can be serialized without ambiguity).
...@@ -96,13 +107,6 @@ class MetricSample { ...@@ -96,13 +107,6 @@ class MetricSample {
bool IsEqual(const MetricSample& sample); bool IsEqual(const MetricSample& sample);
private: 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 SampleType type_;
const std::string name_; const std::string name_;
const int sample_; const int sample_;
......
...@@ -91,7 +91,7 @@ bool ReadMessage(int fd, std::string* message) { ...@@ -91,7 +91,7 @@ bool ReadMessage(int fd, std::string* message) {
std::unique_ptr<MetricSample> SerializationUtils::ParseSample( std::unique_ptr<MetricSample> SerializationUtils::ParseSample(
const std::string& sample) { const std::string& sample) {
if (sample.empty()) if (sample.empty())
return std::unique_ptr<MetricSample>(); return nullptr;
std::vector<std::string> parts = base::SplitString( std::vector<std::string> parts = base::SplitString(
sample, std::string(1, '\0'), sample, std::string(1, '\0'),
...@@ -101,7 +101,7 @@ std::unique_ptr<MetricSample> SerializationUtils::ParseSample( ...@@ -101,7 +101,7 @@ std::unique_ptr<MetricSample> SerializationUtils::ParseSample(
if (parts.size() != 3) { if (parts.size() != 3) {
DLOG(ERROR) << "splitting message on \\0 produced " << parts.size() DLOG(ERROR) << "splitting message on \\0 produced " << parts.size()
<< " parts (expected 3)"; << " parts (expected 3)";
return std::unique_ptr<MetricSample>(); return nullptr;
} }
const std::string& name = parts[0]; const std::string& name = parts[0];
const std::string& value = parts[1]; const std::string& value = parts[1];
...@@ -117,7 +117,7 @@ std::unique_ptr<MetricSample> SerializationUtils::ParseSample( ...@@ -117,7 +117,7 @@ std::unique_ptr<MetricSample> SerializationUtils::ParseSample(
if (base::LowerCaseEqualsASCII(name, "useraction")) if (base::LowerCaseEqualsASCII(name, "useraction"))
return MetricSample::UserActionSample(value); return MetricSample::UserActionSample(value);
DLOG(ERROR) << "invalid event type: " << name << ", value: " << value; DLOG(ERROR) << "invalid event type: " << name << ", value: " << value;
return std::unique_ptr<MetricSample>(); return nullptr;
} }
void SerializationUtils::ReadAndTruncateMetricsFromFile( void SerializationUtils::ReadAndTruncateMetricsFromFile(
......
...@@ -64,25 +64,23 @@ class UnsentLogStoreTest : public testing::Test { ...@@ -64,25 +64,23 @@ class UnsentLogStoreTest : public testing::Test {
class TestUnsentLogStore : public UnsentLogStore { class TestUnsentLogStore : public UnsentLogStore {
public: public:
TestUnsentLogStore(PrefService* service, size_t min_log_bytes) TestUnsentLogStore(PrefService* service, size_t min_log_bytes)
: UnsentLogStore(std::unique_ptr<UnsentLogStoreMetricsImpl>( : UnsentLogStore(std::make_unique<UnsentLogStoreMetricsImpl>(),
new UnsentLogStoreMetricsImpl()), service,
service, kTestPrefName,
kTestPrefName, kLogCountLimit,
kLogCountLimit, min_log_bytes,
min_log_bytes, 0,
0, std::string()) {}
std::string()) {}
TestUnsentLogStore(PrefService* service, TestUnsentLogStore(PrefService* service,
size_t min_log_bytes, size_t min_log_bytes,
const std::string& signing_key) const std::string& signing_key)
: UnsentLogStore(std::unique_ptr<UnsentLogStoreMetricsImpl>( : UnsentLogStore(std::make_unique<UnsentLogStoreMetricsImpl>(),
new UnsentLogStoreMetricsImpl()), service,
service, kTestPrefName,
kTestPrefName, kLogCountLimit,
kLogCountLimit, min_log_bytes,
min_log_bytes, 0,
0, signing_key) {}
signing_key) {}
// Stages and removes the next log, while testing it's value. // Stages and removes the next log, while testing it's value.
void ExpectNextLog(const std::string& expected_log) { 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