blob: f38e4d103070679fac7be1ba7493586502bf5d63 [file] [log] [blame]
vishal.b62985ca92015-04-17 08:45:511// 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
5#include "net/log/test_net_log.h"
6
Avi Drissman13fc8932015-12-20 04:40:467#include "base/macros.h"
mmenke6d23d6f2015-05-05 19:05:088#include "base/synchronization/lock.h"
9#include "base/values.h"
mikecironef22f9812016-10-04 03:40:1910#include "net/log/net_log_capture_mode.h"
11#include "net/log/net_log_entry.h"
12#include "net/log/net_log_source.h"
mikecirone8b85c432016-09-08 19:11:0013#include "net/log/net_log_source_type.h"
mmenke6d23d6f2015-05-05 19:05:0814
vishal.b62985ca92015-04-17 08:45:5115namespace net {
16
Matt Muellerde5dadf2019-11-27 20:11:5817TestNetLog::TestNetLog() : NetLog(util::PassKey<TestNetLog>()) {}
18TestNetLog::~TestNetLog() = default;
19
Matt Muellerd9342e3a2019-11-26 01:41:1420RecordingTestNetLog::RecordingTestNetLog() {
Eric Roman630830c2019-07-16 21:49:4221 AddObserver(this, NetLogCaptureMode::kIncludeSensitive);
vishal.b62985ca92015-04-17 08:45:5122}
23
Matt Muellerd9342e3a2019-11-26 01:41:1424RecordingTestNetLog::~RecordingTestNetLog() {
Eric Roman630830c2019-07-16 21:49:4225 RemoveObserver(this);
vishal.b62985ca92015-04-17 08:45:5126}
27
Matt Muellerd9342e3a2019-11-26 01:41:1428std::vector<NetLogEntry> RecordingTestNetLog::GetEntries() const {
Eric Roman630830c2019-07-16 21:49:4229 base::AutoLock lock(lock_);
Eric Roman79cc7552019-07-19 02:17:5430 std::vector<NetLogEntry> result;
31 for (const auto& entry : entry_list_)
32 result.push_back(entry.Clone());
33 return result;
vishal.b62985ca92015-04-17 08:45:5134}
35
Matt Muellerd9342e3a2019-11-26 01:41:1436std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesForSource(
Eric Roman79cc7552019-07-19 02:17:5437 NetLogSource source) const {
Eric Roman630830c2019-07-16 21:49:4238 base::AutoLock lock(lock_);
Eric Roman79cc7552019-07-19 02:17:5439 std::vector<NetLogEntry> result;
Eric Roman630830c2019-07-16 21:49:4240 for (const auto& entry : entry_list_) {
41 if (entry.source.id == source.id)
Eric Roman79cc7552019-07-19 02:17:5442 result.push_back(entry.Clone());
Eric Roman630830c2019-07-16 21:49:4243 }
Eric Roman79cc7552019-07-19 02:17:5444 return result;
45}
46
Matt Muellerd9342e3a2019-11-26 01:41:1447std::vector<NetLogEntry> RecordingTestNetLog::GetEntriesWithType(
Eric Roman79cc7552019-07-19 02:17:5448 NetLogEventType type) const {
49 base::AutoLock lock(lock_);
50 std::vector<NetLogEntry> result;
51 for (const auto& entry : entry_list_) {
52 if (entry.type == type)
53 result.push_back(entry.Clone());
54 }
55 return result;
vishal.b62985ca92015-04-17 08:45:5156}
57
Matt Muellerd9342e3a2019-11-26 01:41:1458size_t RecordingTestNetLog::GetSize() const {
Eric Roman630830c2019-07-16 21:49:4259 base::AutoLock lock(lock_);
60 return entry_list_.size();
vishal.b62985ca92015-04-17 08:45:5161}
62
Matt Muellerd9342e3a2019-11-26 01:41:1463void RecordingTestNetLog::Clear() {
Eric Roman630830c2019-07-16 21:49:4264 base::AutoLock lock(lock_);
65 entry_list_.clear();
mmenke6d23d6f2015-05-05 19:05:0866}
67
Matt Muellerd9342e3a2019-11-26 01:41:1468void RecordingTestNetLog::OnAddEntry(const NetLogEntry& entry) {
Eric Roman79cc7552019-07-19 02:17:5469 base::Value params = entry.params.Clone();
Eric Roman630830c2019-07-16 21:49:4270
71 // Only need to acquire the lock when accessing class variables.
72 base::AutoLock lock(lock_);
Matt Muellera9a15312020-01-29 22:45:1273 entry_list_.emplace_back(entry.type, entry.source, entry.phase, entry.time,
Eric Roman79cc7552019-07-19 02:17:5474 std::move(params));
Eric Roman630830c2019-07-16 21:49:4275}
76
Matt Muellerd9342e3a2019-11-26 01:41:1477NetLog::ThreadSafeObserver* RecordingTestNetLog::GetObserver() {
Eric Roman630830c2019-07-16 21:49:4278 return this;
79}
80
Matt Muellerd9342e3a2019-11-26 01:41:1481void RecordingTestNetLog::SetObserverCaptureMode(
82 NetLogCaptureMode capture_mode) {
Eric Roman630830c2019-07-16 21:49:4283 RemoveObserver(this);
84 AddObserver(this, capture_mode);
vishal.b62985ca92015-04-17 08:45:5185}
86
Matt Muellerd9342e3a2019-11-26 01:41:1487RecordingBoundTestNetLog::RecordingBoundTestNetLog()
tfarina42834112016-09-22 13:38:2088 : net_log_(NetLogWithSource::Make(&test_net_log_, NetLogSourceType::NONE)) {
89}
vishal.b62985ca92015-04-17 08:45:5190
Matt Muellerd9342e3a2019-11-26 01:41:1491RecordingBoundTestNetLog::~RecordingBoundTestNetLog() = default;
vishal.b62985ca92015-04-17 08:45:5192
Matt Muellerd9342e3a2019-11-26 01:41:1493std::vector<NetLogEntry> RecordingBoundTestNetLog::GetEntries() const {
Eric Roman79cc7552019-07-19 02:17:5494 return test_net_log_.GetEntries();
vishal.b62985ca92015-04-17 08:45:5195}
96
Matt Muellerd9342e3a2019-11-26 01:41:1497std::vector<NetLogEntry> RecordingBoundTestNetLog::GetEntriesForSource(
Eric Roman79cc7552019-07-19 02:17:5498 NetLogSource source) const {
99 return test_net_log_.GetEntriesForSource(source);
100}
101
Matt Muellerd9342e3a2019-11-26 01:41:14102std::vector<NetLogEntry> RecordingBoundTestNetLog::GetEntriesWithType(
Eric Roman79cc7552019-07-19 02:17:54103 NetLogEventType type) const {
104 return test_net_log_.GetEntriesWithType(type);
vishal.b62985ca92015-04-17 08:45:51105}
106
Matt Muellerd9342e3a2019-11-26 01:41:14107size_t RecordingBoundTestNetLog::GetSize() const {
mmenke43758e62015-05-04 21:09:46108 return test_net_log_.GetSize();
vishal.b62985ca92015-04-17 08:45:51109}
110
Matt Muellerd9342e3a2019-11-26 01:41:14111void RecordingBoundTestNetLog::Clear() {
mmenke43758e62015-05-04 21:09:46112 test_net_log_.Clear();
vishal.b62985ca92015-04-17 08:45:51113}
114
Matt Muellerd9342e3a2019-11-26 01:41:14115void RecordingBoundTestNetLog::SetObserverCaptureMode(
116 NetLogCaptureMode capture_mode) {
Eric Roman630830c2019-07-16 21:49:42117 test_net_log_.SetObserverCaptureMode(capture_mode);
vishal.b62985ca92015-04-17 08:45:51118}
119
120} // namespace net