eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 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 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 5 | #ifndef NET_LOG_TEST_NET_LOG_H_ |
| 6 | #define NET_LOG_TEST_NET_LOG_H_ |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 7 | |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 8 | #include <stddef.h> |
| 9 | |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 13 | #include "base/compiler_specific.h" |
Avi Drissman | 13fc893 | 2015-12-20 04:40:46 | [diff] [blame] | 14 | #include "base/macros.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 15 | #include "net/log/net_log.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 16 | #include "net/log/net_log_with_source.h" |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 17 | |
| 18 | namespace net { |
| 19 | |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 20 | struct NetLogSource; |
| 21 | |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 22 | // 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. |
| 28 | class TestNetLog : public NetLog, public NetLog::ThreadSafeObserver { |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 29 | public: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 30 | TestNetLog(); |
| 31 | ~TestNetLog() override; |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 32 | |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 33 | void SetObserverCaptureMode(NetLogCaptureMode capture_mode); |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 34 | |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 35 | // ThreadSafeObserver implementation: |
| 36 | void OnAddEntry(const NetLogEntry& entry) override; |
| 37 | |
| 38 | // Returns the list of all observed NetLog entries. |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 39 | std::vector<NetLogEntry> GetEntries() const; |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 40 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 41 | // 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 Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 46 | |
| 47 | // Returns the number of entries in the log. |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 48 | size_t GetSize() const; |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 49 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 50 | // Clears the captured entry list. |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 51 | void Clear(); |
| 52 | |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 53 | // Returns the NetLog observer responsible for recording the NetLog event |
| 54 | // stream. For testing code that bypasses NetLogs and adds events directly to |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame] | 55 | // an observer. |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 56 | NetLog::ThreadSafeObserver* GetObserver(); |
mmenke | 6d23d6f | 2015-05-05 19:05:08 | [diff] [blame] | 57 | |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 58 | private: |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 59 | mutable base::Lock lock_; |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 60 | std::vector<NetLogEntry> entry_list_; |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 61 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 62 | DISALLOW_COPY_AND_ASSIGN(TestNetLog); |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 63 | }; |
| 64 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 65 | // Helper class that exposes a similar API as NetLogWithSource, but uses a |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 66 | // TestNetLog rather than the more generic NetLog. |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 67 | // |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 68 | // A BoundTestNetLog can easily be converted to a NetLogWithSource using the |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 69 | // bound() method. |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 70 | class BoundTestNetLog { |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 71 | public: |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 72 | BoundTestNetLog(); |
| 73 | ~BoundTestNetLog(); |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 74 | |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 75 | // The returned NetLogWithSource is only valid while |this| is alive. |
| 76 | NetLogWithSource bound() const { return net_log_; } |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 77 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 78 | // Returns all captured entries. |
| 79 | std::vector<NetLogEntry> GetEntries() const; |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 80 | |
Eric Roman | 79cc755 | 2019-07-19 02:17:54 | [diff] [blame] | 81 | // 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; |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 86 | |
| 87 | // Returns number of entries in the log. |
| 88 | size_t GetSize() const; |
| 89 | |
| 90 | void Clear(); |
| 91 | |
Eric Roman | 630830c | 2019-07-16 21:49:42 | [diff] [blame] | 92 | // Sets the observer capture mode of the underlying TestNetLog. |
| 93 | void SetObserverCaptureMode(NetLogCaptureMode capture_mode); |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 94 | |
| 95 | private: |
mmenke | 43758e6 | 2015-05-04 21:09:46 | [diff] [blame] | 96 | TestNetLog test_net_log_; |
tfarina | 42834111 | 2016-09-22 13:38:20 | [diff] [blame] | 97 | const NetLogWithSource net_log_; |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 98 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 99 | DISALLOW_COPY_AND_ASSIGN(BoundTestNetLog); |
eroman | 87c53d6 | 2015-04-02 06:51:07 | [diff] [blame] | 100 | }; |
| 101 | |
| 102 | } // namespace net |
| 103 | |
vishal.b | 62985ca9 | 2015-04-17 08:45:51 | [diff] [blame] | 104 | #endif // NET_LOG_TEST_NET_LOG_H_ |