Remove heap allocation of base::Value from NetLog.

This CL changes NetLogParametersCallback to return a "base::Value" rather than a "std::unique_ptr<base::Value>", and updates the affected callsites.

This is a step towards removing all the deprecated calls in base/values.h. This CL does not change the use of base::DictionaryValue.

Bug: 646113
Bug: 901525
TBR: [email protected],[email protected]
Change-Id: I27465cc27578de22575f4a441f571cdaa3eda4d6
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1590407
Commit-Queue: Eric Roman <[email protected]>
Reviewed-by: David Benjamin <[email protected]>
Cr-Commit-Position: refs/heads/master@{#655454}
diff --git a/net/log/file_net_log_observer.cc b/net/log/file_net_log_observer.cc
index 1b1b7e40..f85805a 100644
--- a/net/log/file_net_log_observer.cc
+++ b/net/log/file_net_log_observer.cc
@@ -402,7 +402,7 @@
 void FileNetLogObserver::OnAddEntry(const NetLogEntry& entry) {
   std::unique_ptr<std::string> json(new std::string);
 
-  *json = SerializeNetLogValueToJson(*entry.ToValue());
+  *json = SerializeNetLogValueToJson(entry.ToValue());
 
   size_t queue_size = write_queue_->AddEntryToQueue(std::move(json));
 
diff --git a/net/log/file_net_log_observer_unittest.cc b/net/log/file_net_log_observer_unittest.cc
index 399143d..bb078fd 100644
--- a/net/log/file_net_log_observer_unittest.cc
+++ b/net/log/file_net_log_observer_unittest.cc
@@ -63,9 +63,9 @@
                                   base::TimeTicks::Now(), &callback);
   NetLogEntry base_entry(&base_entry_data,
                          NetLogCaptureMode::IncludeSocketBytes());
-  std::unique_ptr<base::Value> value(base_entry.ToValue());
+  base::Value value = base_entry.ToValue();
   std::string json;
-  base::JSONWriter::Write(*value, &json);
+  base::JSONWriter::Write(value, &json);
   size_t base_entry_size = json.size();
 
   // The maximum value of base::TimeTicks::Now() will be the maximum value of
@@ -105,7 +105,7 @@
 
   // Initializes the ParsedNetLog by parsing a JSON file.
   // Owner for the Value tree.
-  std::unique_ptr<base::Value> container;
+  base::Value container;
 
   // A dictionary for the entire netlog.
   const base::DictionaryValue* root = nullptr;
@@ -127,12 +127,13 @@
   }
 
   base::JSONReader reader;
-  container = reader.ReadToValueDeprecated(input);
-  if (!container) {
+  base::Optional<base::Value> container_optional = reader.Read(input);
+  if (!container_optional) {
     return ::testing::AssertionFailure() << reader.GetErrorMessage();
   }
+  container = std::move(*container_optional);
 
-  if (!container->GetAsDictionary(&root)) {
+  if (!container.GetAsDictionary(&root)) {
     return ::testing::AssertionFailure() << "Not a dictionary";
   }
 
diff --git a/net/log/net_log.cc b/net/log/net_log.cc
index afb760e..88b3fa8 100644
--- a/net/log/net_log.cc
+++ b/net/log/net_log.cc
@@ -23,63 +23,51 @@
 
 namespace {
 
-std::unique_ptr<base::Value> NetLogBoolCallback(
-    const char* name,
-    bool value,
-    NetLogCaptureMode /* capture_mode */) {
-  std::unique_ptr<base::DictionaryValue> event_params(
-      new base::DictionaryValue());
-  event_params->SetBoolean(name, value);
+base::Value NetLogBoolCallback(const char* name,
+                               bool value,
+                               NetLogCaptureMode /* capture_mode */) {
+  base::DictionaryValue event_params;
+  event_params.SetBoolean(name, value);
   return std::move(event_params);
 }
 
-std::unique_ptr<base::Value> NetLogIntCallback(
-    const char* name,
-    int value,
-    NetLogCaptureMode /* capture_mode */) {
-  std::unique_ptr<base::DictionaryValue> event_params(
-      new base::DictionaryValue());
-  event_params->SetInteger(name, value);
+base::Value NetLogIntCallback(const char* name,
+                              int value,
+                              NetLogCaptureMode /* capture_mode */) {
+  base::DictionaryValue event_params;
+  event_params.SetInteger(name, value);
   return std::move(event_params);
 }
 
-std::unique_ptr<base::Value> NetLogInt64Callback(
-    const char* name,
-    int64_t value,
-    NetLogCaptureMode /* capture_mode */) {
-  std::unique_ptr<base::DictionaryValue> event_params(
-      new base::DictionaryValue());
-  event_params->SetKey(name, NetLogNumberValue(value));
+base::Value NetLogInt64Callback(const char* name,
+                                int64_t value,
+                                NetLogCaptureMode /* capture_mode */) {
+  base::DictionaryValue event_params;
+  event_params.SetKey(name, NetLogNumberValue(value));
   return std::move(event_params);
 }
 
-std::unique_ptr<base::Value> NetLogStringCallback(
-    const char* name,
-    const std::string* value,
-    NetLogCaptureMode /* capture_mode */) {
-  std::unique_ptr<base::DictionaryValue> event_params(
-      new base::DictionaryValue());
-  event_params->SetString(name, *value);
+base::Value NetLogStringCallback(const char* name,
+                                 const std::string* value,
+                                 NetLogCaptureMode /* capture_mode */) {
+  base::DictionaryValue event_params;
+  event_params.SetString(name, *value);
   return std::move(event_params);
 }
 
-std::unique_ptr<base::Value> NetLogCharStringCallback(
-    const char* name,
-    const char* value,
-    NetLogCaptureMode /* capture_mode */) {
-  std::unique_ptr<base::DictionaryValue> event_params(
-      new base::DictionaryValue());
-  event_params->SetString(name, value);
+base::Value NetLogCharStringCallback(const char* name,
+                                     const char* value,
+                                     NetLogCaptureMode /* capture_mode */) {
+  base::DictionaryValue event_params;
+  event_params.SetString(name, value);
   return std::move(event_params);
 }
 
-std::unique_ptr<base::Value> NetLogString16Callback(
-    const char* name,
-    const base::string16* value,
-    NetLogCaptureMode /* capture_mode */) {
-  std::unique_ptr<base::DictionaryValue> event_params(
-      new base::DictionaryValue());
-  event_params->SetString(name, *value);
+base::Value NetLogString16Callback(const char* name,
+                                   const base::string16* value,
+                                   NetLogCaptureMode /* capture_mode */) {
+  base::DictionaryValue event_params;
+  event_params.SetString(name, *value);
   return std::move(event_params);
 }
 
@@ -236,10 +224,10 @@
 }
 
 // static
-std::unique_ptr<base::Value> NetLog::GetEventTypesAsValue() {
-  auto dict = std::make_unique<base::DictionaryValue>();
+base::Value NetLog::GetEventTypesAsValue() {
+  base::DictionaryValue dict;
   for (int i = 0; i < static_cast<int>(NetLogEventType::COUNT); ++i) {
-    dict->SetInteger(EventTypeToString(static_cast<NetLogEventType>(i)), i);
+    dict.SetInteger(EventTypeToString(static_cast<NetLogEventType>(i)), i);
   }
   return std::move(dict);
 }
@@ -259,10 +247,10 @@
 }
 
 // static
-std::unique_ptr<base::Value> NetLog::GetSourceTypesAsValue() {
-  auto dict = std::make_unique<base::DictionaryValue>();
+base::Value NetLog::GetSourceTypesAsValue() {
+  base::DictionaryValue dict;
   for (int i = 0; i < static_cast<int>(NetLogSourceType::COUNT); ++i) {
-    dict->SetInteger(SourceTypeToString(static_cast<NetLogSourceType>(i)), i);
+    dict.SetInteger(SourceTypeToString(static_cast<NetLogSourceType>(i)), i);
   }
   return std::move(dict);
 }
diff --git a/net/log/net_log.h b/net/log/net_log.h
index 0dfba99..14c9a74 100644
--- a/net/log/net_log.h
+++ b/net/log/net_log.h
@@ -165,14 +165,14 @@
 
   // Returns a dictionary that maps event type symbolic names to their enum
   // values.
-  static std::unique_ptr<base::Value> GetEventTypesAsValue();
+  static base::Value GetEventTypesAsValue();
 
   // Returns a C-String symbolic name for |source_type|.
   static const char* SourceTypeToString(NetLogSourceType source_type);
 
   // Returns a dictionary that maps source type symbolic names to their enum
   // values.
-  static std::unique_ptr<base::Value> GetSourceTypesAsValue();
+  static base::Value GetSourceTypesAsValue();
 
   // Returns a C-String symbolic name for |event_phase|.
   static const char* EventPhaseToString(NetLogEventPhase event_phase);
diff --git a/net/log/net_log_entry.cc b/net/log/net_log_entry.cc
index 3cdfc452..da6329ac 100644
--- a/net/log/net_log_entry.cc
+++ b/net/log/net_log_entry.cc
@@ -12,38 +12,33 @@
 
 namespace net {
 
-std::unique_ptr<base::Value> NetLogEntry::ToValue() const {
-  std::unique_ptr<base::DictionaryValue> entry_dict(
-      new base::DictionaryValue());
+base::Value NetLogEntry::ToValue() const {
+  base::DictionaryValue entry_dict;
 
-  entry_dict->SetString("time", NetLog::TickCountToString(data_->time));
+  entry_dict.SetString("time", NetLog::TickCountToString(data_->time));
 
   // Set the entry source.
-  std::unique_ptr<base::DictionaryValue> source_dict(
-      new base::DictionaryValue());
-  source_dict->SetInteger("id", data_->source.id);
-  source_dict->SetInteger("type", static_cast<int>(data_->source.type));
-  entry_dict->Set("source", std::move(source_dict));
+  base::DictionaryValue source_dict;
+  source_dict.SetInteger("id", data_->source.id);
+  source_dict.SetInteger("type", static_cast<int>(data_->source.type));
+  entry_dict.SetKey("source", std::move(source_dict));
 
   // Set the event info.
-  entry_dict->SetInteger("type", static_cast<int>(data_->type));
-  entry_dict->SetInteger("phase", static_cast<int>(data_->phase));
+  entry_dict.SetInteger("type", static_cast<int>(data_->type));
+  entry_dict.SetInteger("phase", static_cast<int>(data_->phase));
 
   // Set the event-specific parameters.
   if (data_->parameters_callback) {
-    std::unique_ptr<base::Value> value(
-        data_->parameters_callback->Run(capture_mode_));
-    if (value)
-      entry_dict->Set("params", std::move(value));
+    entry_dict.SetKey("params", data_->parameters_callback->Run(capture_mode_));
   }
 
   return std::move(entry_dict);
 }
 
-std::unique_ptr<base::Value> NetLogEntry::ParametersToValue() const {
+base::Value NetLogEntry::ParametersToValue() const {
   if (data_->parameters_callback)
     return data_->parameters_callback->Run(capture_mode_);
-  return nullptr;
+  return base::Value();
 }
 
 NetLogEntryData::NetLogEntryData(
diff --git a/net/log/net_log_entry.h b/net/log/net_log_entry.h
index 48127041..b9280844 100644
--- a/net/log/net_log_entry.h
+++ b/net/log/net_log_entry.h
@@ -50,11 +50,12 @@
 
   // Serializes the specified event to a Value.  The Value also includes the
   // current time.  Takes in a time to allow back-dating entries.
-  std::unique_ptr<base::Value> ToValue() const;
+  base::Value ToValue() const;
 
-  // Returns the parameters as a Value.  Returns nullptr if there are no
+  // Returns the parameters as a Value.  Returns a none value if there are no
   // parameters.
-  std::unique_ptr<base::Value> ParametersToValue() const;
+  // TODO(eroman): Make this base::Optional instead?
+  base::Value ParametersToValue() const;
 
  private:
   const NetLogEntryData* const data_;
diff --git a/net/log/net_log_parameters_callback.h b/net/log/net_log_parameters_callback.h
index e472a330..05ff3ff 100644
--- a/net/log/net_log_parameters_callback.h
+++ b/net/log/net_log_parameters_callback.h
@@ -19,9 +19,9 @@
 // A callback that returns a Value representation of the parameters
 // associated with an event.  If called, it will be called synchronously,
 // so it need not have owning references.  May be called more than once, or
-// not at all.  May return nullptr.
-typedef base::Callback<std::unique_ptr<base::Value>(NetLogCaptureMode)>
-    NetLogParametersCallback;
+// not at all.  May return a none value to indicate no parameters.
+using NetLogParametersCallback =
+    base::RepeatingCallback<base::Value(NetLogCaptureMode)>;
 
 }  // namespace net
 
diff --git a/net/log/net_log_source.cc b/net/log/net_log_source.cc
index a427f68..255caf4 100644
--- a/net/log/net_log_source.cc
+++ b/net/log/net_log_source.cc
@@ -17,14 +17,13 @@
 
 namespace {
 
-std::unique_ptr<base::Value> SourceEventParametersCallback(
+base::Value SourceEventParametersCallback(
     const NetLogSource source,
     NetLogCaptureMode /* capture_mode */) {
   if (!source.IsValid())
-    return std::unique_ptr<base::Value>();
-  std::unique_ptr<base::DictionaryValue> event_params(
-      new base::DictionaryValue());
-  source.AddToEventParameters(event_params.get());
+    return base::Value();
+  base::DictionaryValue event_params;
+  source.AddToEventParameters(&event_params);
   return std::move(event_params);
 }
 
diff --git a/net/log/net_log_unittest.cc b/net/log/net_log_unittest.cc
index 21a14096..6ff65a3 100644
--- a/net/log/net_log_unittest.cc
+++ b/net/log/net_log_unittest.cc
@@ -44,15 +44,13 @@
   return -1;
 }
 
-std::unique_ptr<base::Value> CaptureModeToValue(
-    NetLogCaptureMode capture_mode) {
-  return std::make_unique<base::Value>(CaptureModeToInt(capture_mode));
+base::Value CaptureModeToValue(NetLogCaptureMode capture_mode) {
+  return base::Value(CaptureModeToInt(capture_mode));
 }
 
-std::unique_ptr<base::Value> NetCaptureModeCallback(
-    NetLogCaptureMode capture_mode) {
-  std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
-  dict->Set("capture_mode", CaptureModeToValue(capture_mode));
+base::Value NetCaptureModeCallback(NetLogCaptureMode capture_mode) {
+  base::DictionaryValue dict;
+  dict.SetKey("capture_mode", CaptureModeToValue(capture_mode));
   return std::move(dict);
 }
 
@@ -137,8 +135,8 @@
   }
 
   void OnAddEntry(const NetLogEntry& entry) override {
-    std::unique_ptr<base::DictionaryValue> dict =
-        base::DictionaryValue::From(entry.ToValue());
+    std::unique_ptr<base::DictionaryValue> dict = base::DictionaryValue::From(
+        base::Value::ToUniquePtrValue(entry.ToValue()));
     ASSERT_TRUE(dict);
     values_.push_back(std::move(dict));
   }
diff --git a/net/log/net_log_util.cc b/net/log/net_log_util.cc
index 59bd8b04..7ec226f7 100644
--- a/net/log/net_log_util.cc
+++ b/net/log/net_log_util.cc
@@ -130,9 +130,8 @@
 
 // Returns a Value representing the state of a pre-existing URLRequest when
 // net-internals was opened.
-std::unique_ptr<base::Value> GetRequestStateAsValue(
-    const net::URLRequest* request,
-    NetLogCaptureMode capture_mode) {
+base::Value GetRequestStateAsValue(const net::URLRequest* request,
+                                   NetLogCaptureMode capture_mode) {
   return request->GetStateAsValue();
 }
 
@@ -147,7 +146,7 @@
 
   // Add a dictionary with information on the relationship between event type
   // enums and their symbolic names.
-  constants_dict->Set("logEventTypes", NetLog::GetEventTypesAsValue());
+  constants_dict->SetKey("logEventTypes", NetLog::GetEventTypesAsValue());
 
   // Add a dictionary with information about the relationship between CertStatus
   // flags and their symbolic names.
@@ -245,7 +244,7 @@
 
   // Information about the relationship between source type enums and
   // their symbolic names.
-  constants_dict->Set("logSourceType", NetLog::GetSourceTypesAsValue());
+  constants_dict->SetKey("logSourceType", NetLog::GetSourceTypesAsValue());
 
   // TODO(eroman): This is here for compatibility in loading new log files with
   // older builds of Chrome. Safe to remove this once M45 is on the stable
diff --git a/net/log/net_log_with_source.cc b/net/log/net_log_with_source.cc
index 8997f64c..bdaa42e 100644
--- a/net/log/net_log_with_source.cc
+++ b/net/log/net_log_with_source.cc
@@ -23,14 +23,13 @@
 // Returns parameters for logging data transferred events. At a minimum includes
 // the number of bytes transferred. If the capture mode allows logging byte
 // contents and |byte_count| > 0, then will include the actual bytes.
-std::unique_ptr<base::Value> BytesTransferredCallback(
-    int byte_count,
-    const char* bytes,
-    NetLogCaptureMode capture_mode) {
-  std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
-  dict->SetInteger("byte_count", byte_count);
+base::Value BytesTransferredCallback(int byte_count,
+                                     const char* bytes,
+                                     NetLogCaptureMode capture_mode) {
+  base::DictionaryValue dict;
+  dict.SetInteger("byte_count", byte_count);
   if (capture_mode.include_socket_bytes() && byte_count > 0)
-    dict->SetKey("bytes", NetLogBinaryValue(bytes, byte_count));
+    dict.SetKey("bytes", NetLogBinaryValue(bytes, byte_count));
   return std::move(dict);
 }
 
diff --git a/net/log/test_net_log.cc b/net/log/test_net_log.cc
index 36ef00d..6814f15 100644
--- a/net/log/test_net_log.cc
+++ b/net/log/test_net_log.cc
@@ -55,7 +55,8 @@
     // Using Dictionaries instead of Values makes checking values a little
     // simpler.
     std::unique_ptr<base::DictionaryValue> param_dict =
-        base::DictionaryValue::From(entry.ParametersToValue());
+        base::DictionaryValue::From(
+            base::Value::ToUniquePtrValue(entry.ParametersToValue()));
 
     // Only need to acquire the lock when accessing class variables.
     base::AutoLock lock(lock_);
diff --git a/net/log/trace_net_log_observer.cc b/net/log/trace_net_log_observer.cc
index 5129deb5..a6ecb034 100644
--- a/net/log/trace_net_log_observer.cc
+++ b/net/log/trace_net_log_observer.cc
@@ -26,16 +26,15 @@
 
 class TracedValue : public base::trace_event::ConvertableToTraceFormat {
  public:
-  explicit TracedValue(std::unique_ptr<base::Value> value)
-      : value_(std::move(value)) {}
+  explicit TracedValue(base::Value value) : value_(std::move(value)) {}
 
  private:
   ~TracedValue() override = default;
 
   void AppendAsTraceFormat(std::string* out) const override {
-    if (value_) {
+    if (!value_.is_none()) {
       std::string tmp;
-      base::JSONWriter::Write(*value_, &tmp);
+      base::JSONWriter::Write(value_, &tmp);
       *out += tmp;
     } else {
       *out += "\"\"";
@@ -43,7 +42,7 @@
   }
 
  private:
-  std::unique_ptr<base::Value> value_;
+  base::Value value_;
 };
 
 }  // namespace
@@ -57,7 +56,7 @@
 }
 
 void TraceNetLogObserver::OnAddEntry(const NetLogEntry& entry) {
-  std::unique_ptr<base::Value> params(entry.ParametersToValue());
+  base::Value params(entry.ParametersToValue());
   switch (entry.phase()) {
     case NetLogEventPhase::BEGIN:
       TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(