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/base/capturing_net_log.h"
7 #include "base/json/json_writer.h"
8 #include "base/logging.h"
9 #include "base/values.h"
13 CapturingNetLog::CapturedEntry::CapturedEntry(
15 const base::TimeTicks
& time
,
18 scoped_ptr
<DictionaryValue
> params
)
23 params(params
.Pass()) {
26 CapturingNetLog::CapturedEntry::CapturedEntry(const CapturedEntry
& entry
) {
30 CapturingNetLog::CapturedEntry::~CapturedEntry() {}
32 CapturingNetLog::CapturedEntry
&
33 CapturingNetLog::CapturedEntry::operator=(const CapturedEntry
& entry
) {
36 source
= entry
.source
;
38 params
.reset(entry
.params
? entry
.params
->DeepCopy() : NULL
);
42 bool CapturingNetLog::CapturedEntry::GetStringValue(
43 const std::string
& name
,
44 std::string
* value
) const {
47 return params
->GetString(name
, value
);
50 bool CapturingNetLog::CapturedEntry::GetIntegerValue(
51 const std::string
& name
,
55 return params
->GetInteger(name
, value
);
58 bool CapturingNetLog::CapturedEntry::GetNetErrorCode(int* value
) const {
59 return GetIntegerValue("net_error", value
);
62 std::string
CapturingNetLog::CapturedEntry::GetParamsJson() const {
66 base::JSONWriter::Write(params
.get(), &json
);
70 CapturingNetLog::CapturingNetLog()
72 log_level_(LOG_ALL_BUT_BYTES
) {
75 CapturingNetLog::~CapturingNetLog() {}
77 void CapturingNetLog::GetEntries(CapturedEntryList
* entry_list
) const {
78 base::AutoLock
lock(lock_
);
79 *entry_list
= captured_entries_
;
82 void CapturingNetLog::GetEntriesForSource(NetLog::Source source
,
83 CapturedEntryList
* entry_list
) const {
84 base::AutoLock
lock(lock_
);
86 for (CapturedEntryList::const_iterator entry
= captured_entries_
.begin();
87 entry
!= captured_entries_
.end(); ++entry
) {
88 if (entry
->source
.id
== source
.id
)
89 entry_list
->push_back(*entry
);
93 size_t CapturingNetLog::GetSize() const {
94 base::AutoLock
lock(lock_
);
95 return captured_entries_
.size();
98 void CapturingNetLog::Clear() {
99 base::AutoLock
lock(lock_
);
100 captured_entries_
.clear();
103 void CapturingNetLog::SetLogLevel(NetLog::LogLevel log_level
) {
104 base::AutoLock
lock(lock_
);
105 log_level_
= log_level
;
108 void CapturingNetLog::OnAddEntry(const net::NetLog::Entry
& entry
) {
109 // Only BoundNetLogs without a NetLog should have an invalid source.
110 CHECK(entry
.source().IsValid());
112 // Using Dictionaries instead of Values makes checking values a little
114 DictionaryValue
* param_dict
= NULL
;
115 Value
* param_value
= entry
.ParametersToValue();
116 if (param_value
&& !param_value
->GetAsDictionary(¶m_dict
))
119 // Only need to acquire the lock when accessing class variables.
120 base::AutoLock
lock(lock_
);
121 captured_entries_
.push_back(
122 CapturedEntry(entry
.type(),
123 base::TimeTicks::Now(),
126 scoped_ptr
<DictionaryValue
>(param_dict
)));
129 uint32
CapturingNetLog::NextID() {
130 return base::subtle::NoBarrier_AtomicIncrement(&last_id_
, 1);
133 NetLog::LogLevel
CapturingNetLog::GetLogLevel() const {
134 base::AutoLock
lock(lock_
);
138 void CapturingNetLog::AddThreadSafeObserver(
139 NetLog::ThreadSafeObserver
* observer
,
140 NetLog::LogLevel log_level
) {
141 NOTIMPLEMENTED() << "Not currently used by net unit tests.";
144 void CapturingNetLog::SetObserverLogLevel(ThreadSafeObserver
* observer
,
145 LogLevel log_level
) {
146 NOTIMPLEMENTED() << "Not currently used by net unit tests.";
149 void CapturingNetLog::RemoveThreadSafeObserver(
150 NetLog::ThreadSafeObserver
* observer
) {
151 NOTIMPLEMENTED() << "Not currently used by net unit tests.";
154 CapturingBoundNetLog::CapturingBoundNetLog()
155 : net_log_(BoundNetLog::Make(&capturing_net_log_
,
156 net::NetLog::SOURCE_NONE
)) {
159 CapturingBoundNetLog::~CapturingBoundNetLog() {}
161 void CapturingBoundNetLog::GetEntries(
162 CapturingNetLog::CapturedEntryList
* entry_list
) const {
163 capturing_net_log_
.GetEntries(entry_list
);
166 void CapturingBoundNetLog::GetEntriesForSource(
167 NetLog::Source source
,
168 CapturingNetLog::CapturedEntryList
* entry_list
) const {
169 capturing_net_log_
.GetEntriesForSource(source
, entry_list
);
172 size_t CapturingBoundNetLog::GetSize() const {
173 return capturing_net_log_
.GetSize();
176 void CapturingBoundNetLog::Clear() {
177 capturing_net_log_
.Clear();
180 void CapturingBoundNetLog::SetLogLevel(NetLog::LogLevel log_level
) {
181 capturing_net_log_
.SetLogLevel(log_level
);