Commit 30329535 authored by Matt Mueller's avatar Matt Mueller Committed by Commit Bot

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 <mattm@chromium.org>
Reviewed-by: default avatarMatt Menke <mmenke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#738005}
parent a29bbe70
...@@ -14,18 +14,23 @@ ...@@ -14,18 +14,23 @@
namespace net { namespace net {
TestNetLog::TestNetLog() : NetLog(util::PassKey<TestNetLog>()) {} RecordingNetLogObserver::RecordingNetLogObserver()
TestNetLog::~TestNetLog() = default; : RecordingNetLogObserver(NetLogCaptureMode::kIncludeSensitive) {}
RecordingNetLogObserver::RecordingNetLogObserver(NetLogCaptureMode capture_mode)
: RecordingNetLogObserver(NetLog::Get(), capture_mode) {}
RecordingTestNetLog::RecordingTestNetLog() { RecordingNetLogObserver::RecordingNetLogObserver(NetLog* net_log,
AddObserver(this, NetLogCaptureMode::kIncludeSensitive); NetLogCaptureMode capture_mode)
: net_log_(net_log) {
net_log_->AddObserver(this, capture_mode);
} }
RecordingTestNetLog::~RecordingTestNetLog() { RecordingNetLogObserver::~RecordingNetLogObserver() {
RemoveObserver(this); net_log_->RemoveObserver(this);
} }
std::vector<NetLogEntry> RecordingTestNetLog::GetEntries() const { std::vector<NetLogEntry> RecordingNetLogObserver::GetEntries() const {
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
std::vector<NetLogEntry> result; std::vector<NetLogEntry> result;
for (const auto& entry : entry_list_) for (const auto& entry : entry_list_)
...@@ -33,7 +38,7 @@ std::vector<NetLogEntry> RecordingTestNetLog::GetEntries() const { ...@@ -33,7 +38,7 @@ std::vector<NetLogEntry> RecordingTestNetLog::GetEntries() const {
return result; return result;
} }
std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesForSource( std::vector<NetLogEntry> RecordingNetLogObserver::GetEntriesForSource(
NetLogSource source) const { NetLogSource source) const {
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
std::vector<NetLogEntry> result; std::vector<NetLogEntry> result;
...@@ -44,7 +49,7 @@ std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesForSource( ...@@ -44,7 +49,7 @@ std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesForSource(
return result; return result;
} }
std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesWithType( std::vector<NetLogEntry> RecordingNetLogObserver::GetEntriesWithType(
NetLogEventType type) const { NetLogEventType type) const {
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
std::vector<NetLogEntry> result; std::vector<NetLogEntry> result;
...@@ -55,33 +60,78 @@ std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesWithType( ...@@ -55,33 +60,78 @@ std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesWithType(
return result; return result;
} }
size_t RecordingTestNetLog::GetSize() const { size_t RecordingNetLogObserver::GetSize() const {
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
return entry_list_.size(); return entry_list_.size();
} }
void RecordingTestNetLog::Clear() { void RecordingNetLogObserver::Clear() {
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
entry_list_.clear(); entry_list_.clear();
} }
void RecordingTestNetLog::OnAddEntry(const NetLogEntry& entry) { void RecordingNetLogObserver::OnAddEntry(const NetLogEntry& entry) {
base::Value params = entry.params.Clone(); 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();
}
void RecordingNetLogObserver::SetObserverCaptureMode(
NetLogCaptureMode capture_mode) {
net_log_->RemoveObserver(this);
net_log_->AddObserver(this, capture_mode);
}
// Only need to acquire the lock when accessing class variables. void RecordingNetLogObserver::SetThreadsafeAddEntryCallback(
base::RepeatingClosure add_entry_callback) {
base::AutoLock lock(lock_); base::AutoLock lock(lock_);
entry_list_.emplace_back(entry.type, entry.source, entry.phase, entry.time, add_entry_callback_ = add_entry_callback;
std::move(params)); }
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() { NetLog::ThreadSafeObserver* RecordingTestNetLog::GetObserver() {
return this; return &observer_;
} }
void RecordingTestNetLog::SetObserverCaptureMode( void RecordingTestNetLog::SetObserverCaptureMode(
NetLogCaptureMode capture_mode) { NetLogCaptureMode capture_mode) {
RemoveObserver(this); observer_.SetObserverCaptureMode(capture_mode);
AddObserver(this, capture_mode);
} }
RecordingBoundTestNetLog::RecordingBoundTestNetLog() RecordingBoundTestNetLog::RecordingBoundTestNetLog()
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/callback.h"
#include "base/compiler_specific.h" #include "base/compiler_specific.h"
#include "base/macros.h" #include "base/macros.h"
#include "net/log/net_log.h" #include "net/log/net_log.h"
...@@ -19,6 +20,56 @@ namespace net { ...@@ -19,6 +20,56 @@ namespace net {
struct NetLogSource; 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 // NetLog subclass that follows normal lifetime rules (has a public
// destructor.) // destructor.)
// //
...@@ -39,30 +90,19 @@ class TestNetLog : public NetLog { ...@@ -39,30 +90,19 @@ class TestNetLog : public NetLog {
// SetObserverCaptureMode(). // SetObserverCaptureMode().
// //
// This class is for testing only. // This class is for testing only.
class RecordingTestNetLog : public TestNetLog, // RecordingNetLogObserver is preferred for new tests.
public NetLog::ThreadSafeObserver { class RecordingTestNetLog : public TestNetLog {
public: public:
RecordingTestNetLog(); RecordingTestNetLog();
~RecordingTestNetLog() override; ~RecordingTestNetLog() override;
// These methods all delegate to the underlying RecordingNetLogObserver,
// see the comments in that class for documentation.
void SetObserverCaptureMode(NetLogCaptureMode capture_mode); 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; std::vector<NetLogEntry> GetEntries() const;
// Returns all entries in the log from the specified Source.
std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const; std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const;
// Returns all captured entries with the specified type.
std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const; std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const;
// Returns the number of entries in the log.
size_t GetSize() const; size_t GetSize() const;
// Clears the captured entry list.
void Clear(); void Clear();
// Returns the NetLog observer responsible for recording the NetLog event // Returns the NetLog observer responsible for recording the NetLog event
...@@ -71,8 +111,7 @@ class RecordingTestNetLog : public TestNetLog, ...@@ -71,8 +111,7 @@ class RecordingTestNetLog : public TestNetLog,
NetLog::ThreadSafeObserver* GetObserver(); NetLog::ThreadSafeObserver* GetObserver();
private: private:
mutable base::Lock lock_; RecordingNetLogObserver observer_;
std::vector<NetLogEntry> entry_list_;
DISALLOW_COPY_AND_ASSIGN(RecordingTestNetLog); DISALLOW_COPY_AND_ASSIGN(RecordingTestNetLog);
}; };
...@@ -90,23 +129,15 @@ class RecordingBoundTestNetLog { ...@@ -90,23 +129,15 @@ class RecordingBoundTestNetLog {
// The returned NetLogWithSource is only valid while |this| is alive. // The returned NetLogWithSource is only valid while |this| is alive.
NetLogWithSource bound() const { return net_log_; } NetLogWithSource bound() const { return net_log_; }
// Returns all captured entries. // 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> GetEntries() const;
// Returns all captured entries for the specified Source.
std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const; std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const;
// Returns all captured entries with the specified type.
std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const; std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const;
// Returns number of entries in the log.
size_t GetSize() const; size_t GetSize() const;
void Clear(); void Clear();
// Sets the observer capture mode of the underlying RecordingTestNetLog.
void SetObserverCaptureMode(NetLogCaptureMode capture_mode);
private: private:
RecordingTestNetLog test_net_log_; RecordingTestNetLog test_net_log_;
const NetLogWithSource net_log_; const NetLogWithSource net_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