blob: 0a4d666bebdec8688a75ed9a48041af75c61a904 [file] [log] [blame]
Avi Drissman64595482022-09-14 20:52:291// Copyright 2012 The Chromium Authors
vishal.b62985ca92015-04-17 08:45:512// 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
mmenke6d23d6f2015-05-05 19:05:087#include "base/synchronization/lock.h"
8#include "base/values.h"
mikecironef22f9812016-10-04 03:40:199#include "net/log/net_log_capture_mode.h"
10#include "net/log/net_log_entry.h"
11#include "net/log/net_log_source.h"
mikecirone8b85c432016-09-08 19:11:0012#include "net/log/net_log_source_type.h"
mmenke6d23d6f2015-05-05 19:05:0813
vishal.b62985ca92015-04-17 08:45:5114namespace net {
15
Matt Mueller30329532020-02-04 00:37:2216RecordingNetLogObserver::RecordingNetLogObserver()
17 : RecordingNetLogObserver(NetLogCaptureMode::kIncludeSensitive) {}
Matt Muellerde5dadf2019-11-27 20:11:5818
Matt Mueller30329532020-02-04 00:37:2219RecordingNetLogObserver::RecordingNetLogObserver(NetLogCaptureMode capture_mode)
20 : RecordingNetLogObserver(NetLog::Get(), capture_mode) {}
21
22RecordingNetLogObserver::RecordingNetLogObserver(NetLog* net_log,
23 NetLogCaptureMode capture_mode)
24 : net_log_(net_log) {
25 net_log_->AddObserver(this, capture_mode);
vishal.b62985ca92015-04-17 08:45:5126}
27
Matt Mueller30329532020-02-04 00:37:2228RecordingNetLogObserver::~RecordingNetLogObserver() {
29 net_log_->RemoveObserver(this);
vishal.b62985ca92015-04-17 08:45:5130}
31
Matt Mueller30329532020-02-04 00:37:2232std::vector<NetLogEntry> RecordingNetLogObserver::GetEntries() const {
Eric Roman630830c2019-07-16 21:49:4233 base::AutoLock lock(lock_);
Eric Roman79cc7552019-07-19 02:17:5434 std::vector<NetLogEntry> result;
35 for (const auto& entry : entry_list_)
36 result.push_back(entry.Clone());
37 return result;
vishal.b62985ca92015-04-17 08:45:5138}
39
Matt Mueller30329532020-02-04 00:37:2240std::vector<NetLogEntry> RecordingNetLogObserver::GetEntriesForSource(
Eric Roman79cc7552019-07-19 02:17:5441 NetLogSource source) const {
Eric Roman630830c2019-07-16 21:49:4242 base::AutoLock lock(lock_);
Eric Roman79cc7552019-07-19 02:17:5443 std::vector<NetLogEntry> result;
Eric Roman630830c2019-07-16 21:49:4244 for (const auto& entry : entry_list_) {
45 if (entry.source.id == source.id)
Eric Roman79cc7552019-07-19 02:17:5446 result.push_back(entry.Clone());
Eric Roman630830c2019-07-16 21:49:4247 }
Eric Roman79cc7552019-07-19 02:17:5448 return result;
49}
50
Matt Mueller30329532020-02-04 00:37:2251std::vector<NetLogEntry> RecordingNetLogObserver::GetEntriesWithType(
Eric Roman79cc7552019-07-19 02:17:5452 NetLogEventType type) const {
53 base::AutoLock lock(lock_);
54 std::vector<NetLogEntry> result;
55 for (const auto& entry : entry_list_) {
56 if (entry.type == type)
57 result.push_back(entry.Clone());
58 }
59 return result;
vishal.b62985ca92015-04-17 08:45:5160}
61
Matt Menke433de6d2020-03-04 00:24:1162std::vector<NetLogEntry> RecordingNetLogObserver::GetEntriesForSourceWithType(
63 NetLogSource source,
64 NetLogEventType type,
65 NetLogEventPhase phase) const {
66 base::AutoLock lock(lock_);
67 std::vector<NetLogEntry> result;
68 for (const auto& entry : entry_list_) {
69 if (entry.source.id == source.id && entry.type == type &&
70 entry.phase == phase) {
71 result.push_back(entry.Clone());
72 }
73 }
74 return result;
75}
76
Matt Mueller30329532020-02-04 00:37:2277size_t RecordingNetLogObserver::GetSize() const {
Eric Roman630830c2019-07-16 21:49:4278 base::AutoLock lock(lock_);
79 return entry_list_.size();
vishal.b62985ca92015-04-17 08:45:5180}
81
Matt Mueller30329532020-02-04 00:37:2282void RecordingNetLogObserver::Clear() {
Eric Roman630830c2019-07-16 21:49:4283 base::AutoLock lock(lock_);
84 entry_list_.clear();
mmenke6d23d6f2015-05-05 19:05:0885}
86
Matt Mueller30329532020-02-04 00:37:2287void RecordingNetLogObserver::OnAddEntry(const NetLogEntry& entry) {
Liam Brady0c651722023-02-24 23:48:2088 base::Value::Dict params = entry.params.Clone();
Matt Mueller30329532020-02-04 00:37:2289 base::RepeatingClosure add_entry_callback;
90 {
91 // Only need to acquire the lock when accessing class variables.
92 base::AutoLock lock(lock_);
93 entry_list_.emplace_back(entry.type, entry.source, entry.phase, entry.time,
94 std::move(params));
95 add_entry_callback = add_entry_callback_;
96 }
97 if (!add_entry_callback.is_null())
98 add_entry_callback.Run();
99}
Eric Roman630830c2019-07-16 21:49:42100
Matt Mueller30329532020-02-04 00:37:22101void RecordingNetLogObserver::SetObserverCaptureMode(
102 NetLogCaptureMode capture_mode) {
103 net_log_->RemoveObserver(this);
104 net_log_->AddObserver(this, capture_mode);
105}
106
107void RecordingNetLogObserver::SetThreadsafeAddEntryCallback(
108 base::RepeatingClosure add_entry_callback) {
Eric Roman630830c2019-07-16 21:49:42109 base::AutoLock lock(lock_);
Matt Mueller30329532020-02-04 00:37:22110 add_entry_callback_ = add_entry_callback;
111}
112
vishal.b62985ca92015-04-17 08:45:51113} // namespace net