1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef NET_LOG_TEST_NET_LOG_H_ 6 #define NET_LOG_TEST_NET_LOG_H_ 7 8 #include <stddef.h> 9 10 #include <vector> 11 12 #include "base/compiler_specific.h" 13 #include "base/functional/callback.h" 14 #include "base/memory/raw_ptr.h" 15 #include "net/log/net_log.h" 16 #include "net/log/net_log_event_type.h" 17 18 namespace net { 19 20 struct NetLogSource; 21 22 // NetLog observer that record NetLogs events and their parameters into an 23 // in-memory buffer. 24 // 25 // This class is for testing only. 26 class RecordingNetLogObserver : public NetLog::ThreadSafeObserver { 27 public: 28 // Observe the global singleton netlog with kIncludeSensitive capture mode. 29 RecordingNetLogObserver(); 30 31 // Observe the global singleton netlog with |capture_mode|. 32 explicit RecordingNetLogObserver(NetLogCaptureMode capture_mode); 33 34 // Observe the specified |net_log| object with |capture_mode|. 35 RecordingNetLogObserver(NetLog* net_log, NetLogCaptureMode capture_mode); 36 37 RecordingNetLogObserver(const RecordingNetLogObserver&) = delete; 38 RecordingNetLogObserver& operator=(const RecordingNetLogObserver&) = delete; 39 40 ~RecordingNetLogObserver() override; 41 42 // Change the |capture_mode|. 43 void SetObserverCaptureMode(NetLogCaptureMode capture_mode); 44 45 // |add_entry_callback| may be called on any thread. 46 void SetThreadsafeAddEntryCallback(base::RepeatingClosure add_entry_callback); 47 48 // ThreadSafeObserver implementation: 49 void OnAddEntry(const NetLogEntry& entry) override; 50 51 // Returns the list of all observed NetLog entries. 52 std::vector<NetLogEntry> GetEntries() const; 53 54 // Returns all entries in the log from the specified Source. 55 std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const; 56 57 // Returns all captured entries with the specified type. 58 std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const; 59 60 // Returns all captured entries with the specified values. 61 std::vector<NetLogEntry> GetEntriesForSourceWithType( 62 NetLogSource source, 63 NetLogEventType type, 64 NetLogEventPhase phase) const; 65 66 // Returns the number of entries in the log. 67 size_t GetSize() const; 68 69 // Clears the captured entry list. 70 void Clear(); 71 72 private: 73 mutable base::Lock lock_; 74 std::vector<NetLogEntry> entry_list_; 75 const raw_ptr<NetLog> net_log_; 76 base::RepeatingClosure add_entry_callback_; 77 }; 78 79 } // namespace net 80 81 #endif // NET_LOG_TEST_NET_LOG_H_ 82