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.
5 #include "net/log/test_net_log.h"
7 #include "base/synchronization/lock.h"
8 #include "base/values.h"
12 // TestNetLog::Observer is an implementation of NetLog::ThreadSafeObserver
13 // that saves messages to a buffer.
14 class TestNetLog::Observer
: public NetLog::ThreadSafeObserver
{
17 ~Observer() override
{}
19 // Returns the list of all entries in the log.
20 void GetEntries(TestNetLogEntry::List
* entry_list
) const {
21 base::AutoLock
lock(lock_
);
22 *entry_list
= entry_list_
;
25 // Fills |entry_list| with all entries in the log from the specified Source.
26 void GetEntriesForSource(NetLog::Source source
,
27 TestNetLogEntry::List
* entry_list
) const {
28 base::AutoLock
lock(lock_
);
30 for (const auto& entry
: entry_list_
) {
31 if (entry
.source
.id
== source
.id
)
32 entry_list
->push_back(entry
);
36 // Returns the number of entries in the log.
37 size_t GetSize() const {
38 base::AutoLock
lock(lock_
);
39 return entry_list_
.size();
43 base::AutoLock
lock(lock_
);
48 // ThreadSafeObserver implementation:
49 void OnAddEntry(const NetLog::Entry
& entry
) override
{
50 // Using Dictionaries instead of Values makes checking values a little
52 base::DictionaryValue
* param_dict
= nullptr;
53 base::Value
* param_value
= entry
.ParametersToValue();
54 if (param_value
&& !param_value
->GetAsDictionary(¶m_dict
))
57 // Only need to acquire the lock when accessing class variables.
58 base::AutoLock
lock(lock_
);
59 entry_list_
.push_back(TestNetLogEntry(
60 entry
.type(), base::TimeTicks::Now(), entry
.source(), entry
.phase(),
61 scoped_ptr
<base::DictionaryValue
>(param_dict
)));
64 // Needs to be "mutable" to use it in GetEntries().
65 mutable base::Lock lock_
;
67 TestNetLogEntry::List entry_list_
;
69 DISALLOW_COPY_AND_ASSIGN(Observer
);
72 TestNetLog::TestNetLog() : observer_(new Observer()) {
73 DeprecatedAddObserver(observer_
.get(),
74 NetLogCaptureMode::IncludeCookiesAndCredentials());
77 TestNetLog::~TestNetLog() {
78 DeprecatedRemoveObserver(observer_
.get());
81 void TestNetLog::SetCaptureMode(NetLogCaptureMode capture_mode
) {
82 SetObserverCaptureMode(observer_
.get(), capture_mode
);
85 void TestNetLog::GetEntries(TestNetLogEntry::List
* entry_list
) const {
86 observer_
->GetEntries(entry_list
);
89 void TestNetLog::GetEntriesForSource(NetLog::Source source
,
90 TestNetLogEntry::List
* entry_list
) const {
91 observer_
->GetEntriesForSource(source
, entry_list
);
94 size_t TestNetLog::GetSize() const {
95 return observer_
->GetSize();
98 void TestNetLog::Clear() {
102 NetLog::ThreadSafeObserver
* TestNetLog::GetObserver() const {
103 return observer_
.get();
106 BoundTestNetLog::BoundTestNetLog()
107 : net_log_(BoundNetLog::Make(&test_net_log_
, NetLog::SOURCE_NONE
)) {
110 BoundTestNetLog::~BoundTestNetLog() {
113 void BoundTestNetLog::GetEntries(TestNetLogEntry::List
* entry_list
) const {
114 test_net_log_
.GetEntries(entry_list
);
117 void BoundTestNetLog::GetEntriesForSource(
118 NetLog::Source source
,
119 TestNetLogEntry::List
* entry_list
) const {
120 test_net_log_
.GetEntriesForSource(source
, entry_list
);
123 size_t BoundTestNetLog::GetSize() const {
124 return test_net_log_
.GetSize();
127 void BoundTestNetLog::Clear() {
128 test_net_log_
.Clear();
131 void BoundTestNetLog::SetCaptureMode(NetLogCaptureMode capture_mode
) {
132 test_net_log_
.SetCaptureMode(capture_mode
);