blob: aacb6553d3e762488af36d63388c5ec8d2c110c3 [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
mmenke6d23d6f2015-05-05 19:05:0817// TestNetLog::Observer is an implementation of NetLog::ThreadSafeObserver
18// that saves messages to a buffer.
19class TestNetLog::Observer : public NetLog::ThreadSafeObserver {
20 public:
21 Observer() {}
22 ~Observer() override {}
23
24 // Returns the list of all entries in the log.
25 void GetEntries(TestNetLogEntry::List* entry_list) const {
26 base::AutoLock lock(lock_);
27 *entry_list = entry_list_;
28 }
29
30 // Fills |entry_list| with all entries in the log from the specified Source.
mikecironef22f9812016-10-04 03:40:1931 void GetEntriesForSource(NetLogSource source,
mmenke6d23d6f2015-05-05 19:05:0832 TestNetLogEntry::List* entry_list) const {
33 base::AutoLock lock(lock_);
34 entry_list->clear();
35 for (const auto& entry : entry_list_) {
36 if (entry.source.id == source.id)
37 entry_list->push_back(entry);
38 }
39 }
40
41 // Returns the number of entries in the log.
42 size_t GetSize() const {
43 base::AutoLock lock(lock_);
44 return entry_list_.size();
45 }
46
47 void Clear() {
48 base::AutoLock lock(lock_);
49 entry_list_.clear();
50 }
51
52 private:
53 // ThreadSafeObserver implementation:
mikecironef22f9812016-10-04 03:40:1954 void OnAddEntry(const NetLogEntry& entry) override {
mmenke6d23d6f2015-05-05 19:05:0855 // Using Dictionaries instead of Values makes checking values a little
56 // simpler.
danakja9850e12016-04-18 22:28:0857 std::unique_ptr<base::DictionaryValue> param_dict =
olli.raula0a833f792015-12-29 12:49:2058 base::DictionaryValue::From(entry.ParametersToValue());
mmenke6d23d6f2015-05-05 19:05:0859
60 // Only need to acquire the lock when accessing class variables.
61 base::AutoLock lock(lock_);
olli.raula0a833f792015-12-29 12:49:2062 entry_list_.push_back(TestNetLogEntry(entry.type(), base::TimeTicks::Now(),
63 entry.source(), entry.phase(),
64 std::move(param_dict)));
mmenke6d23d6f2015-05-05 19:05:0865 }
66
67 // Needs to be "mutable" to use it in GetEntries().
68 mutable base::Lock lock_;
69
70 TestNetLogEntry::List entry_list_;
71
72 DISALLOW_COPY_AND_ASSIGN(Observer);
73};
74
75TestNetLog::TestNetLog() : observer_(new Observer()) {
76 DeprecatedAddObserver(observer_.get(),
eroman001c3742015-04-23 03:11:1777 NetLogCaptureMode::IncludeCookiesAndCredentials());
vishal.b62985ca92015-04-17 08:45:5178}
79
80TestNetLog::~TestNetLog() {
mmenke6d23d6f2015-05-05 19:05:0881 DeprecatedRemoveObserver(observer_.get());
vishal.b62985ca92015-04-17 08:45:5182}
83
eroman001c3742015-04-23 03:11:1784void TestNetLog::SetCaptureMode(NetLogCaptureMode capture_mode) {
mmenke6d23d6f2015-05-05 19:05:0885 SetObserverCaptureMode(observer_.get(), capture_mode);
vishal.b62985ca92015-04-17 08:45:5186}
87
mmenke43758e62015-05-04 21:09:4688void TestNetLog::GetEntries(TestNetLogEntry::List* entry_list) const {
mmenke6d23d6f2015-05-05 19:05:0889 observer_->GetEntries(entry_list);
vishal.b62985ca92015-04-17 08:45:5190}
91
mikecironef22f9812016-10-04 03:40:1992void TestNetLog::GetEntriesForSource(NetLogSource source,
mmenke43758e62015-05-04 21:09:4693 TestNetLogEntry::List* entry_list) const {
mmenke6d23d6f2015-05-05 19:05:0894 observer_->GetEntriesForSource(source, entry_list);
vishal.b62985ca92015-04-17 08:45:5195}
96
97size_t TestNetLog::GetSize() const {
mmenke6d23d6f2015-05-05 19:05:0898 return observer_->GetSize();
vishal.b62985ca92015-04-17 08:45:5199}
100
101void TestNetLog::Clear() {
mmenke6d23d6f2015-05-05 19:05:08102 observer_->Clear();
103}
104
105NetLog::ThreadSafeObserver* TestNetLog::GetObserver() const {
106 return observer_.get();
vishal.b62985ca92015-04-17 08:45:51107}
108
109BoundTestNetLog::BoundTestNetLog()
tfarina428341112016-09-22 13:38:20110 : net_log_(NetLogWithSource::Make(&test_net_log_, NetLogSourceType::NONE)) {
111}
vishal.b62985ca92015-04-17 08:45:51112
113BoundTestNetLog::~BoundTestNetLog() {
114}
115
mmenke43758e62015-05-04 21:09:46116void BoundTestNetLog::GetEntries(TestNetLogEntry::List* entry_list) const {
117 test_net_log_.GetEntries(entry_list);
vishal.b62985ca92015-04-17 08:45:51118}
119
120void BoundTestNetLog::GetEntriesForSource(
mikecironef22f9812016-10-04 03:40:19121 NetLogSource source,
mmenke43758e62015-05-04 21:09:46122 TestNetLogEntry::List* entry_list) const {
123 test_net_log_.GetEntriesForSource(source, entry_list);
vishal.b62985ca92015-04-17 08:45:51124}
125
126size_t BoundTestNetLog::GetSize() const {
mmenke43758e62015-05-04 21:09:46127 return test_net_log_.GetSize();
vishal.b62985ca92015-04-17 08:45:51128}
129
130void BoundTestNetLog::Clear() {
mmenke43758e62015-05-04 21:09:46131 test_net_log_.Clear();
vishal.b62985ca92015-04-17 08:45:51132}
133
eroman001c3742015-04-23 03:11:17134void BoundTestNetLog::SetCaptureMode(NetLogCaptureMode capture_mode) {
mmenke43758e62015-05-04 21:09:46135 test_net_log_.SetCaptureMode(capture_mode);
vishal.b62985ca92015-04-17 08:45:51136}
137
138} // namespace net