blob: 96ba8eaa9be12bfed9c297a47ed6b3489106ff33 [file] [log] [blame]
eroman87c53d62015-04-02 06:51:071// 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
vishal.b62985ca92015-04-17 08:45:515#ifndef NET_LOG_TEST_NET_LOG_H_
6#define NET_LOG_TEST_NET_LOG_H_
eroman87c53d62015-04-02 06:51:077
Avi Drissman13fc8932015-12-20 04:40:468#include <stddef.h>
9
eroman87c53d62015-04-02 06:51:0710#include <string>
11#include <vector>
12
eroman87c53d62015-04-02 06:51:0713#include "base/compiler_specific.h"
Avi Drissman13fc8932015-12-20 04:40:4614#include "base/macros.h"
eroman87c53d62015-04-02 06:51:0715#include "net/log/net_log.h"
mikecironef22f9812016-10-04 03:40:1916#include "net/log/net_log_with_source.h"
eroman87c53d62015-04-02 06:51:0717
18namespace net {
19
mikecironef22f9812016-10-04 03:40:1920struct NetLogSource;
21
Eric Roman630830c2019-07-16 21:49:4222// NetLog subclass that attaches a single observer (this) to record NetLog
23// events and their parameters into an in-memory buffer. The NetLog is observed
24// at kSensitive level by default, however can be changed with
25// SetObserverCaptureMode().
26//
27// This class is for testing only.
28class TestNetLog : public NetLog, public NetLog::ThreadSafeObserver {
eroman87c53d62015-04-02 06:51:0729 public:
vishal.b62985ca92015-04-17 08:45:5130 TestNetLog();
31 ~TestNetLog() override;
eroman87c53d62015-04-02 06:51:0732
Eric Roman630830c2019-07-16 21:49:4233 void SetObserverCaptureMode(NetLogCaptureMode capture_mode);
eroman87c53d62015-04-02 06:51:0734
Eric Roman630830c2019-07-16 21:49:4235 // ThreadSafeObserver implementation:
36 void OnAddEntry(const NetLogEntry& entry) override;
37
38 // Returns the list of all observed NetLog entries.
Eric Roman79cc7552019-07-19 02:17:5439 std::vector<NetLogEntry> GetEntries() const;
Eric Roman630830c2019-07-16 21:49:4240
Eric Roman79cc7552019-07-19 02:17:5441 // Returns all entries in the log from the specified Source.
42 std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const;
43
44 // Returns all captured entries with the specified type.
45 std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const;
Eric Roman630830c2019-07-16 21:49:4246
47 // Returns the number of entries in the log.
eroman87c53d62015-04-02 06:51:0748 size_t GetSize() const;
Eric Roman630830c2019-07-16 21:49:4249
Eric Roman79cc7552019-07-19 02:17:5450 // Clears the captured entry list.
eroman87c53d62015-04-02 06:51:0751 void Clear();
52
Eric Roman630830c2019-07-16 21:49:4253 // Returns the NetLog observer responsible for recording the NetLog event
54 // stream. For testing code that bypasses NetLogs and adds events directly to
mmenke6d23d6f2015-05-05 19:05:0855 // an observer.
Eric Roman630830c2019-07-16 21:49:4256 NetLog::ThreadSafeObserver* GetObserver();
mmenke6d23d6f2015-05-05 19:05:0857
eroman87c53d62015-04-02 06:51:0758 private:
Eric Roman630830c2019-07-16 21:49:4259 mutable base::Lock lock_;
Eric Roman79cc7552019-07-19 02:17:5460 std::vector<NetLogEntry> entry_list_;
eroman87c53d62015-04-02 06:51:0761
vishal.b62985ca92015-04-17 08:45:5162 DISALLOW_COPY_AND_ASSIGN(TestNetLog);
eroman87c53d62015-04-02 06:51:0763};
64
tfarina428341112016-09-22 13:38:2065// Helper class that exposes a similar API as NetLogWithSource, but uses a
vishal.b62985ca92015-04-17 08:45:5166// TestNetLog rather than the more generic NetLog.
eroman87c53d62015-04-02 06:51:0767//
tfarina428341112016-09-22 13:38:2068// A BoundTestNetLog can easily be converted to a NetLogWithSource using the
eroman87c53d62015-04-02 06:51:0769// bound() method.
vishal.b62985ca92015-04-17 08:45:5170class BoundTestNetLog {
eroman87c53d62015-04-02 06:51:0771 public:
vishal.b62985ca92015-04-17 08:45:5172 BoundTestNetLog();
73 ~BoundTestNetLog();
eroman87c53d62015-04-02 06:51:0774
tfarina428341112016-09-22 13:38:2075 // The returned NetLogWithSource is only valid while |this| is alive.
76 NetLogWithSource bound() const { return net_log_; }
eroman87c53d62015-04-02 06:51:0777
Eric Roman79cc7552019-07-19 02:17:5478 // Returns all captured entries.
79 std::vector<NetLogEntry> GetEntries() const;
eroman87c53d62015-04-02 06:51:0780
Eric Roman79cc7552019-07-19 02:17:5481 // Returns all captured entries for the specified Source.
82 std::vector<NetLogEntry> GetEntriesForSource(NetLogSource source) const;
83
84 // Returns all captured entries with the specified type.
85 std::vector<NetLogEntry> GetEntriesWithType(NetLogEventType type) const;
eroman87c53d62015-04-02 06:51:0786
87 // Returns number of entries in the log.
88 size_t GetSize() const;
89
90 void Clear();
91
Eric Roman630830c2019-07-16 21:49:4292 // Sets the observer capture mode of the underlying TestNetLog.
93 void SetObserverCaptureMode(NetLogCaptureMode capture_mode);
eroman87c53d62015-04-02 06:51:0794
95 private:
mmenke43758e62015-05-04 21:09:4696 TestNetLog test_net_log_;
tfarina428341112016-09-22 13:38:2097 const NetLogWithSource net_log_;
eroman87c53d62015-04-02 06:51:0798
vishal.b62985ca92015-04-17 08:45:5199 DISALLOW_COPY_AND_ASSIGN(BoundTestNetLog);
eroman87c53d62015-04-02 06:51:07100};
101
102} // namespace net
103
vishal.b62985ca92015-04-17 08:45:51104#endif // NET_LOG_TEST_NET_LOG_H_