net: split RecordingNetLogObserver class out of RecordingTestNetLog

Bug: 1040681
Change-Id: Iaadc7d1b99668c8287eab5a181838f744f767e86
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2036271
Commit-Queue: Matt Mueller <[email protected]>
Reviewed-by: Matt Menke <[email protected]>
Cr-Commit-Position: refs/heads/master@{#738005}
diff --git a/net/log/test_net_log.cc b/net/log/test_net_log.cc
index f38e4d10..a238e9d 100644
--- a/net/log/test_net_log.cc
+++ b/net/log/test_net_log.cc
@@ -14,18 +14,23 @@
 
 namespace net {
 
-TestNetLog::TestNetLog() : NetLog(util::PassKey<TestNetLog>()) {}
-TestNetLog::~TestNetLog() = default;
+RecordingNetLogObserver::RecordingNetLogObserver()
+    : RecordingNetLogObserver(NetLogCaptureMode::kIncludeSensitive) {}
 
-RecordingTestNetLog::RecordingTestNetLog() {
-  AddObserver(this, NetLogCaptureMode::kIncludeSensitive);
+RecordingNetLogObserver::RecordingNetLogObserver(NetLogCaptureMode capture_mode)
+    : RecordingNetLogObserver(NetLog::Get(), capture_mode) {}
+
+RecordingNetLogObserver::RecordingNetLogObserver(NetLog* net_log,
+                                                 NetLogCaptureMode capture_mode)
+    : net_log_(net_log) {
+  net_log_->AddObserver(this, capture_mode);
 }
 
-RecordingTestNetLog::~RecordingTestNetLog() {
-  RemoveObserver(this);
+RecordingNetLogObserver::~RecordingNetLogObserver() {
+  net_log_->RemoveObserver(this);
 }
 
-std::vector<NetLogEntry> RecordingTestNetLog::GetEntries() const {
+std::vector<NetLogEntry> RecordingNetLogObserver::GetEntries() const {
   base::AutoLock lock(lock_);
   std::vector<NetLogEntry> result;
   for (const auto& entry : entry_list_)
@@ -33,7 +38,7 @@
   return result;
 }
 
-std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesForSource(
+std::vector<NetLogEntry> RecordingNetLogObserver::GetEntriesForSource(
     NetLogSource source) const {
   base::AutoLock lock(lock_);
   std::vector<NetLogEntry> result;
@@ -44,7 +49,7 @@
   return result;
 }
 
-std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesWithType(
+std::vector<NetLogEntry> RecordingNetLogObserver::GetEntriesWithType(
     NetLogEventType type) const {
   base::AutoLock lock(lock_);
   std::vector<NetLogEntry> result;
@@ -55,33 +60,78 @@
   return result;
 }
 
-size_t RecordingTestNetLog::GetSize() const {
+size_t RecordingNetLogObserver::GetSize() const {
   base::AutoLock lock(lock_);
   return entry_list_.size();
 }
 
-void RecordingTestNetLog::Clear() {
+void RecordingNetLogObserver::Clear() {
   base::AutoLock lock(lock_);
   entry_list_.clear();
 }
 
-void RecordingTestNetLog::OnAddEntry(const NetLogEntry& entry) {
+void RecordingNetLogObserver::OnAddEntry(const NetLogEntry& entry) {
   base::Value params = entry.params.Clone();
+  base::RepeatingClosure add_entry_callback;
+  {
+    // Only need to acquire the lock when accessing class variables.
+    base::AutoLock lock(lock_);
+    entry_list_.emplace_back(entry.type, entry.source, entry.phase, entry.time,
+                             std::move(params));
+    add_entry_callback = add_entry_callback_;
+  }
+  if (!add_entry_callback.is_null())
+    add_entry_callback.Run();
+}
 
-  // Only need to acquire the lock when accessing class variables.
+void RecordingNetLogObserver::SetObserverCaptureMode(
+    NetLogCaptureMode capture_mode) {
+  net_log_->RemoveObserver(this);
+  net_log_->AddObserver(this, capture_mode);
+}
+
+void RecordingNetLogObserver::SetThreadsafeAddEntryCallback(
+    base::RepeatingClosure add_entry_callback) {
   base::AutoLock lock(lock_);
-  entry_list_.emplace_back(entry.type, entry.source, entry.phase, entry.time,
-                           std::move(params));
+  add_entry_callback_ = add_entry_callback;
+}
+
+TestNetLog::TestNetLog() : NetLog(util::PassKey<TestNetLog>()) {}
+TestNetLog::~TestNetLog() = default;
+
+RecordingTestNetLog::RecordingTestNetLog()
+    : observer_(this, NetLogCaptureMode::kIncludeSensitive) {}
+RecordingTestNetLog::~RecordingTestNetLog() = default;
+
+std::vector<NetLogEntry> RecordingTestNetLog::GetEntries() const {
+  return observer_.GetEntries();
+}
+
+std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesForSource(
+    NetLogSource source) const {
+  return observer_.GetEntriesForSource(source);
+}
+
+std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesWithType(
+    NetLogEventType type) const {
+  return observer_.GetEntriesWithType(type);
+}
+
+size_t RecordingTestNetLog::GetSize() const {
+  return observer_.GetSize();
+}
+
+void RecordingTestNetLog::Clear() {
+  return observer_.Clear();
 }
 
 NetLog::ThreadSafeObserver* RecordingTestNetLog::GetObserver() {
-  return this;
+  return &observer_;
 }
 
 void RecordingTestNetLog::SetObserverCaptureMode(
     NetLogCaptureMode capture_mode) {
-  RemoveObserver(this);
-  AddObserver(this, capture_mode);
+  observer_.SetObserverCaptureMode(capture_mode);
 }
 
 RecordingBoundTestNetLog::RecordingBoundTestNetLog()
diff --git a/net/log/test_net_log.h b/net/log/test_net_log.h
index 89325f92..e8eb239 100644
--- a/net/log/test_net_log.h
+++ b/net/log/test_net_log.h
@@ -10,6 +10,7 @@
 #include <string>
 #include <vector>
 
+#include "base/callback.h"
 #include "base/compiler_specific.h"
 #include "base/macros.h"
 #include "net/log/net_log.h"
@@ -19,6 +20,56 @@
 
 struct NetLogSource;
 
+// NetLog observer that record NetLogs events and their parameters into an
+// in-memory buffer.
+//
+// This class is for testing only.
+class RecordingNetLogObserver : public NetLog::ThreadSafeObserver {
+ public:
+  // Observe the global singleton netlog with kIncludeSensitive capture mode.
+  RecordingNetLogObserver();
+
+  // Observe the global singleton netlog with |capture_mode|.
+  explicit RecordingNetLogObserver(NetLogCaptureMode capture_mode);
+
+  // Observe the specified |net_log| object with |capture_mode|.
+  RecordingNetLogObserver(NetLog* net_log, NetLogCaptureMode capture_mode);
+
+  ~RecordingNetLogObserver() override;
+
+  // Change the |capture_mode|.
+  void SetObserverCaptureMode(NetLogCaptureMode capture_mode);
+
+  // |add_entry_callback| may be called on any thread.
+  void SetThreadsafeAddEntryCallback(base::RepeatingClosure add_entry_callback);
+
+  // ThreadSafeObserver implementation:
+  void OnAddEntry(const NetLogEntry& entry) override;
+
+  // Returns the list of all observed NetLog entries.
+  std::vector<NetLogEntry> GetEntries() const;
+
+  // Returns all entries in the log from the specified Source.
+  std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const;
+
+  // Returns all captured entries with the specified type.
+  std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const;
+
+  // Returns the number of entries in the log.
+  size_t GetSize() const;
+
+  // Clears the captured entry list.
+  void Clear();
+
+ private:
+  mutable base::Lock lock_;
+  std::vector<NetLogEntry> entry_list_;
+  NetLog* const net_log_;
+  base::RepeatingClosure add_entry_callback_;
+
+  DISALLOW_COPY_AND_ASSIGN(RecordingNetLogObserver);
+};
+
 // NetLog subclass that follows normal lifetime rules (has a public
 // destructor.)
 //
@@ -39,30 +90,19 @@
 // SetObserverCaptureMode().
 //
 // This class is for testing only.
-class RecordingTestNetLog : public TestNetLog,
-                            public NetLog::ThreadSafeObserver {
+// RecordingNetLogObserver is preferred for new tests.
+class RecordingTestNetLog : public TestNetLog {
  public:
   RecordingTestNetLog();
   ~RecordingTestNetLog() override;
 
+  // These methods all delegate to the underlying RecordingNetLogObserver,
+  // see the comments in that class for documentation.
   void SetObserverCaptureMode(NetLogCaptureMode capture_mode);
-
-  // ThreadSafeObserver implementation:
-  void OnAddEntry(const NetLogEntry& entry) override;
-
-  // Returns the list of all observed NetLog entries.
   std::vector<NetLogEntry> GetEntries() const;
-
-  // Returns all entries in the log from the specified Source.
   std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const;
-
-  // Returns all captured entries with the specified type.
   std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const;
-
-  // Returns the number of entries in the log.
   size_t GetSize() const;
-
-  // Clears the captured entry list.
   void Clear();
 
   // Returns the NetLog observer responsible for recording the NetLog event
@@ -71,8 +111,7 @@
   NetLog::ThreadSafeObserver* GetObserver();
 
  private:
-  mutable base::Lock lock_;
-  std::vector<NetLogEntry> entry_list_;
+  RecordingNetLogObserver observer_;
 
   DISALLOW_COPY_AND_ASSIGN(RecordingTestNetLog);
 };
@@ -90,22 +129,14 @@
   // The returned NetLogWithSource is only valid while |this| is alive.
   NetLogWithSource bound() const { return net_log_; }
 
-  // Returns all captured entries.
-  std::vector<NetLogEntry> GetEntries() const;
-
-  // Returns all captured entries for the specified Source.
-  std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const;
-
-  // Returns all captured entries with the specified type.
-  std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const;
-
-  // Returns number of entries in the log.
-  size_t GetSize() const;
-
-  void Clear();
-
-  // Sets the observer capture mode of the underlying RecordingTestNetLog.
+  // These methods all delegate to the underlying RecordingNetLogObserver,
+  // see the comments in that class for documentation.
   void SetObserverCaptureMode(NetLogCaptureMode capture_mode);
+  std::vector<NetLogEntry> GetEntries() const;
+  std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const;
+  std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const;
+  size_t GetSize() const;
+  void Clear();
 
  private:
   RecordingTestNetLog test_net_log_;