test_net_log.h 2.5 KB

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