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::GetListValue(
59 const std::string
& name
,
60 base::ListValue
** value
) const {
63 return params
->GetList(name
, value
);
66 bool CapturingNetLog::CapturedEntry::GetNetErrorCode(int* value
) const {
67 return GetIntegerValue("net_error", value
);
70 std::string
CapturingNetLog::CapturedEntry::GetParamsJson() const {
74 base::JSONWriter::Write(params
.get(), &json
);
78 CapturingNetLog::Observer::Observer() {}
80 CapturingNetLog::Observer::~Observer() {}
82 void CapturingNetLog::Observer::GetEntries(
83 CapturedEntryList
* entry_list
) const {
84 base::AutoLock
lock(lock_
);
85 *entry_list
= captured_entries_
;
88 void CapturingNetLog::Observer::GetEntriesForSource(
89 NetLog::Source source
,
90 CapturedEntryList
* entry_list
) const {
91 base::AutoLock
lock(lock_
);
93 for (CapturedEntryList::const_iterator entry
= captured_entries_
.begin();
94 entry
!= captured_entries_
.end(); ++entry
) {
95 if (entry
->source
.id
== source
.id
)
96 entry_list
->push_back(*entry
);
100 size_t CapturingNetLog::Observer::GetSize() const {
101 base::AutoLock
lock(lock_
);
102 return captured_entries_
.size();
105 void CapturingNetLog::Observer::Clear() {
106 base::AutoLock
lock(lock_
);
107 captured_entries_
.clear();
110 void CapturingNetLog::Observer::OnAddEntry(const net::NetLog::Entry
& entry
) {
111 // Only BoundNetLogs without a NetLog should have an invalid source.
112 CHECK(entry
.source().IsValid());
114 // Using Dictionaries instead of Values makes checking values a little
116 base::DictionaryValue
* param_dict
= NULL
;
117 base::Value
* param_value
= entry
.ParametersToValue();
118 if (param_value
&& !param_value
->GetAsDictionary(¶m_dict
))
121 // Only need to acquire the lock when accessing class variables.
122 base::AutoLock
lock(lock_
);
123 captured_entries_
.push_back(
124 CapturedEntry(entry
.type(),
125 base::TimeTicks::Now(),
128 scoped_ptr
<base::DictionaryValue
>(param_dict
)));
131 CapturingNetLog::CapturingNetLog() {
132 AddThreadSafeObserver(&capturing_net_log_observer_
, LOG_ALL_BUT_BYTES
);
135 CapturingNetLog::~CapturingNetLog() {
136 RemoveThreadSafeObserver(&capturing_net_log_observer_
);
139 void CapturingNetLog::SetLogLevel(NetLog::LogLevel log_level
) {
140 SetObserverLogLevel(&capturing_net_log_observer_
, log_level
);
143 void CapturingNetLog::GetEntries(
144 CapturingNetLog::CapturedEntryList
* entry_list
) const {
145 capturing_net_log_observer_
.GetEntries(entry_list
);
148 void CapturingNetLog::GetEntriesForSource(
149 NetLog::Source source
,
150 CapturedEntryList
* entry_list
) const {
151 capturing_net_log_observer_
.GetEntriesForSource(source
, entry_list
);
154 size_t CapturingNetLog::GetSize() const {
155 return capturing_net_log_observer_
.GetSize();
158 void CapturingNetLog::Clear() {
159 capturing_net_log_observer_
.Clear();
162 CapturingBoundNetLog::CapturingBoundNetLog()
163 : net_log_(BoundNetLog::Make(&capturing_net_log_
,
164 net::NetLog::SOURCE_NONE
)) {
167 CapturingBoundNetLog::~CapturingBoundNetLog() {}
169 void CapturingBoundNetLog::GetEntries(
170 CapturingNetLog::CapturedEntryList
* entry_list
) const {
171 capturing_net_log_
.GetEntries(entry_list
);
174 void CapturingBoundNetLog::GetEntriesForSource(
175 NetLog::Source source
,
176 CapturingNetLog::CapturedEntryList
* entry_list
) const {
177 capturing_net_log_
.GetEntriesForSource(source
, entry_list
);
180 size_t CapturingBoundNetLog::GetSize() const {
181 return capturing_net_log_
.GetSize();
184 void CapturingBoundNetLog::Clear() {
185 capturing_net_log_
.Clear();
188 void CapturingBoundNetLog::SetLogLevel(NetLog::LogLevel log_level
) {
189 capturing_net_log_
.SetLogLevel(log_level
);