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
<base::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::Observer::Observer() {}
72 CapturingNetLog::Observer::~Observer() {}
74 void CapturingNetLog::Observer::GetEntries(
75 CapturedEntryList
* entry_list
) const {
76 base::AutoLock
lock(lock_
);
77 *entry_list
= captured_entries_
;
80 void CapturingNetLog::Observer::GetEntriesForSource(
81 NetLog::Source source
,
82 CapturedEntryList
* entry_list
) const {
83 base::AutoLock
lock(lock_
);
85 for (CapturedEntryList::const_iterator entry
= captured_entries_
.begin();
86 entry
!= captured_entries_
.end(); ++entry
) {
87 if (entry
->source
.id
== source
.id
)
88 entry_list
->push_back(*entry
);
92 size_t CapturingNetLog::Observer::GetSize() const {
93 base::AutoLock
lock(lock_
);
94 return captured_entries_
.size();
97 void CapturingNetLog::Observer::Clear() {
98 base::AutoLock
lock(lock_
);
99 captured_entries_
.clear();
102 void CapturingNetLog::Observer::OnAddEntry(const net::NetLog::Entry
& entry
) {
103 // Only BoundNetLogs without a NetLog should have an invalid source.
104 CHECK(entry
.source().IsValid());
106 // Using Dictionaries instead of Values makes checking values a little
108 base::DictionaryValue
* param_dict
= NULL
;
109 Value
* param_value
= entry
.ParametersToValue();
110 if (param_value
&& !param_value
->GetAsDictionary(¶m_dict
))
113 // Only need to acquire the lock when accessing class variables.
114 base::AutoLock
lock(lock_
);
115 captured_entries_
.push_back(
116 CapturedEntry(entry
.type(),
117 base::TimeTicks::Now(),
120 scoped_ptr
<base::DictionaryValue
>(param_dict
)));
123 CapturingNetLog::CapturingNetLog() {
124 AddThreadSafeObserver(&capturing_net_log_observer_
, LOG_ALL_BUT_BYTES
);
127 CapturingNetLog::~CapturingNetLog() {
128 RemoveThreadSafeObserver(&capturing_net_log_observer_
);
131 void CapturingNetLog::SetLogLevel(NetLog::LogLevel log_level
) {
132 SetObserverLogLevel(&capturing_net_log_observer_
, log_level
);
135 void CapturingNetLog::GetEntries(
136 CapturingNetLog::CapturedEntryList
* entry_list
) const {
137 capturing_net_log_observer_
.GetEntries(entry_list
);
140 void CapturingNetLog::GetEntriesForSource(
141 NetLog::Source source
,
142 CapturedEntryList
* entry_list
) const {
143 capturing_net_log_observer_
.GetEntriesForSource(source
, entry_list
);
146 size_t CapturingNetLog::GetSize() const {
147 return capturing_net_log_observer_
.GetSize();
150 void CapturingNetLog::Clear() {
151 capturing_net_log_observer_
.Clear();
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
);