blob: a1b383e8bce841ae6d95ce961a51fde82f62b543 [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
mmenke6d23d6f2015-05-05 19:05:087#include "base/synchronization/lock.h"
8#include "base/values.h"
9
vishal.b62985ca92015-04-17 08:45:5110namespace net {
11
mmenke6d23d6f2015-05-05 19:05:0812// TestNetLog::Observer is an implementation of NetLog::ThreadSafeObserver
13// that saves messages to a buffer.
14class TestNetLog::Observer : public NetLog::ThreadSafeObserver {
15 public:
16 Observer() {}
17 ~Observer() override {}
18
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_;
23 }
24
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_);
29 entry_list->clear();
30 for (const auto& entry : entry_list_) {
31 if (entry.source.id == source.id)
32 entry_list->push_back(entry);
33 }
34 }
35
36 // Returns the number of entries in the log.
37 size_t GetSize() const {
38 base::AutoLock lock(lock_);
39 return entry_list_.size();
40 }
41
42 void Clear() {
43 base::AutoLock lock(lock_);
44 entry_list_.clear();
45 }
46
47 private:
48 // ThreadSafeObserver implementation:
49 void OnAddEntry(const NetLog::Entry& entry) override {
50 // Using Dictionaries instead of Values makes checking values a little
51 // simpler.
52 base::DictionaryValue* param_dict = nullptr;
53 base::Value* param_value = entry.ParametersToValue();
54 if (param_value && !param_value->GetAsDictionary(&param_dict))
55 delete param_value;
56
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)));
62 }
63
64 // Needs to be "mutable" to use it in GetEntries().
65 mutable base::Lock lock_;
66
67 TestNetLogEntry::List entry_list_;
68
69 DISALLOW_COPY_AND_ASSIGN(Observer);
70};
71
72TestNetLog::TestNetLog() : observer_(new Observer()) {
73 DeprecatedAddObserver(observer_.get(),
eroman001c3742015-04-23 03:11:1774 NetLogCaptureMode::IncludeCookiesAndCredentials());
vishal.b62985ca92015-04-17 08:45:5175}
76
77TestNetLog::~TestNetLog() {
mmenke6d23d6f2015-05-05 19:05:0878 DeprecatedRemoveObserver(observer_.get());
vishal.b62985ca92015-04-17 08:45:5179}
80
eroman001c3742015-04-23 03:11:1781void TestNetLog::SetCaptureMode(NetLogCaptureMode capture_mode) {
mmenke6d23d6f2015-05-05 19:05:0882 SetObserverCaptureMode(observer_.get(), capture_mode);
vishal.b62985ca92015-04-17 08:45:5183}
84
mmenke43758e62015-05-04 21:09:4685void TestNetLog::GetEntries(TestNetLogEntry::List* entry_list) const {
mmenke6d23d6f2015-05-05 19:05:0886 observer_->GetEntries(entry_list);
vishal.b62985ca92015-04-17 08:45:5187}
88
mmenke43758e62015-05-04 21:09:4689void TestNetLog::GetEntriesForSource(NetLog::Source source,
90 TestNetLogEntry::List* entry_list) const {
mmenke6d23d6f2015-05-05 19:05:0891 observer_->GetEntriesForSource(source, entry_list);
vishal.b62985ca92015-04-17 08:45:5192}
93
94size_t TestNetLog::GetSize() const {
mmenke6d23d6f2015-05-05 19:05:0895 return observer_->GetSize();
vishal.b62985ca92015-04-17 08:45:5196}
97
98void TestNetLog::Clear() {
mmenke6d23d6f2015-05-05 19:05:0899 observer_->Clear();
100}
101
102NetLog::ThreadSafeObserver* TestNetLog::GetObserver() const {
103 return observer_.get();
vishal.b62985ca92015-04-17 08:45:51104}
105
106BoundTestNetLog::BoundTestNetLog()
mmenke43758e62015-05-04 21:09:46107 : net_log_(BoundNetLog::Make(&test_net_log_, NetLog::SOURCE_NONE)) {
vishal.b62985ca92015-04-17 08:45:51108}
109
110BoundTestNetLog::~BoundTestNetLog() {
111}
112
mmenke43758e62015-05-04 21:09:46113void BoundTestNetLog::GetEntries(TestNetLogEntry::List* entry_list) const {
114 test_net_log_.GetEntries(entry_list);
vishal.b62985ca92015-04-17 08:45:51115}
116
117void BoundTestNetLog::GetEntriesForSource(
118 NetLog::Source source,
mmenke43758e62015-05-04 21:09:46119 TestNetLogEntry::List* entry_list) const {
120 test_net_log_.GetEntriesForSource(source, entry_list);
vishal.b62985ca92015-04-17 08:45:51121}
122
123size_t BoundTestNetLog::GetSize() const {
mmenke43758e62015-05-04 21:09:46124 return test_net_log_.GetSize();
vishal.b62985ca92015-04-17 08:45:51125}
126
127void BoundTestNetLog::Clear() {
mmenke43758e62015-05-04 21:09:46128 test_net_log_.Clear();
vishal.b62985ca92015-04-17 08:45:51129}
130
eroman001c3742015-04-23 03:11:17131void BoundTestNetLog::SetCaptureMode(NetLogCaptureMode capture_mode) {
mmenke43758e62015-05-04 21:09:46132 test_net_log_.SetCaptureMode(capture_mode);
vishal.b62985ca92015-04-17 08:45:51133}
134
135} // namespace net