blob: 0bb9ab11a39328e436a6defc678b19aa96a4c35c [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
Eric Roman630830c2019-07-16 21:49:4217TestNetLog::TestNetLog() {
18 AddObserver(this, NetLogCaptureMode::kIncludeSensitive);
vishal.b62985ca92015-04-17 08:45:5119}
20
21TestNetLog::~TestNetLog() {
Eric Roman630830c2019-07-16 21:49:4222 RemoveObserver(this);
vishal.b62985ca92015-04-17 08:45:5123}
24
Eric Roman79cc7552019-07-19 02:17:5425std::vector<NetLogEntry> TestNetLog::GetEntries() const {
Eric Roman630830c2019-07-16 21:49:4226 base::AutoLock lock(lock_);
Eric Roman79cc7552019-07-19 02:17:5427 std::vector<NetLogEntry> result;
28 for (const auto& entry : entry_list_)
29 result.push_back(entry.Clone());
30 return result;
vishal.b62985ca92015-04-17 08:45:5131}
32
Eric Roman79cc7552019-07-19 02:17:5433std::vector<NetLogEntry> TestNetLog::GetEntriesForSource(
34 NetLogSource source) const {
Eric Roman630830c2019-07-16 21:49:4235 base::AutoLock lock(lock_);
Eric Roman79cc7552019-07-19 02:17:5436 std::vector<NetLogEntry> result;
Eric Roman630830c2019-07-16 21:49:4237 for (const auto& entry : entry_list_) {
38 if (entry.source.id == source.id)
Eric Roman79cc7552019-07-19 02:17:5439 result.push_back(entry.Clone());
Eric Roman630830c2019-07-16 21:49:4240 }
Eric Roman79cc7552019-07-19 02:17:5441 return result;
42}
43
44std::vector<NetLogEntry> TestNetLog::GetEntriesWithType(
45 NetLogEventType type) const {
46 base::AutoLock lock(lock_);
47 std::vector<NetLogEntry> result;
48 for (const auto& entry : entry_list_) {
49 if (entry.type == type)
50 result.push_back(entry.Clone());
51 }
52 return result;
vishal.b62985ca92015-04-17 08:45:5153}
54
55size_t TestNetLog::GetSize() const {
Eric Roman630830c2019-07-16 21:49:4256 base::AutoLock lock(lock_);
57 return entry_list_.size();
vishal.b62985ca92015-04-17 08:45:5158}
59
60void TestNetLog::Clear() {
Eric Roman630830c2019-07-16 21:49:4261 base::AutoLock lock(lock_);
62 entry_list_.clear();
mmenke6d23d6f2015-05-05 19:05:0863}
64
Eric Roman630830c2019-07-16 21:49:4265void TestNetLog::OnAddEntry(const NetLogEntry& entry) {
Eric Roman79cc7552019-07-19 02:17:5466 base::Value params = entry.params.Clone();
67 auto time = base::TimeTicks::Now();
Eric Roman630830c2019-07-16 21:49:4268
69 // Only need to acquire the lock when accessing class variables.
70 base::AutoLock lock(lock_);
Eric Roman79cc7552019-07-19 02:17:5471 entry_list_.emplace_back(entry.type, entry.source, entry.phase, time,
72 std::move(params));
Eric Roman630830c2019-07-16 21:49:4273}
74
75NetLog::ThreadSafeObserver* TestNetLog::GetObserver() {
76 return this;
77}
78
79void TestNetLog::SetObserverCaptureMode(NetLogCaptureMode capture_mode) {
80 RemoveObserver(this);
81 AddObserver(this, capture_mode);
vishal.b62985ca92015-04-17 08:45:5182}
83
84BoundTestNetLog::BoundTestNetLog()
tfarina428341112016-09-22 13:38:2085 : net_log_(NetLogWithSource::Make(&test_net_log_, NetLogSourceType::NONE)) {
86}
vishal.b62985ca92015-04-17 08:45:5187
Chris Watkins6a33f762017-12-04 03:39:2588BoundTestNetLog::~BoundTestNetLog() = default;
vishal.b62985ca92015-04-17 08:45:5189
Eric Roman79cc7552019-07-19 02:17:5490std::vector<NetLogEntry> BoundTestNetLog::GetEntries() const {
91 return test_net_log_.GetEntries();
vishal.b62985ca92015-04-17 08:45:5192}
93
Eric Roman79cc7552019-07-19 02:17:5494std::vector<NetLogEntry> BoundTestNetLog::GetEntriesForSource(
95 NetLogSource source) const {
96 return test_net_log_.GetEntriesForSource(source);
97}
98
99std::vector<NetLogEntry> BoundTestNetLog::GetEntriesWithType(
100 NetLogEventType type) const {
101 return test_net_log_.GetEntriesWithType(type);
vishal.b62985ca92015-04-17 08:45:51102}
103
104size_t BoundTestNetLog::GetSize() const {
mmenke43758e62015-05-04 21:09:46105 return test_net_log_.GetSize();
vishal.b62985ca92015-04-17 08:45:51106}
107
108void BoundTestNetLog::Clear() {
mmenke43758e62015-05-04 21:09:46109 test_net_log_.Clear();
vishal.b62985ca92015-04-17 08:45:51110}
111
Eric Roman630830c2019-07-16 21:49:42112void BoundTestNetLog::SetObserverCaptureMode(NetLogCaptureMode capture_mode) {
113 test_net_log_.SetObserverCaptureMode(capture_mode);
vishal.b62985ca92015-04-17 08:45:51114}
115
116} // namespace net