blob: 4f18e46c4fb0f4a00bb270ec672ac575cba8274f [file] [log] [blame]
[email protected]988394e2012-02-22 16:37:551// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]9e743cd2010-03-16 07:03:532// 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/base/net_log.h"
[email protected]f6f1bebc2011-01-07 03:04:546
[email protected]3abacd62012-06-10 20:20:327#include "base/bind.h"
[email protected]93190352010-08-13 13:55:108#include "base/logging.h"
9#include "base/string_number_conversions.h"
[email protected]5097dc82010-07-15 17:23:2310#include "base/time.h"
[email protected]f1d81922010-07-31 17:47:0911#include "base/utf_string_conversions.h"
[email protected]d13f51b2010-04-27 23:20:4512#include "base/values.h"
[email protected]f6f1bebc2011-01-07 03:04:5413#include "net/base/net_errors.h"
[email protected]9e743cd2010-03-16 07:03:5314
15namespace net {
16
[email protected]267a0d66d2011-06-01 21:15:1917namespace {
18
[email protected]3abacd62012-06-10 20:20:3219// Returns parameters for logging data transferred events. Includes number of
20// bytes transferred and, if the log level indicates bytes should be logged and
21// |byte_count| > 0, the bytes themselves. The bytes are hex-encoded, since
22// base::StringValue only supports UTF-8.
23Value* BytesTransferredCallback(int byte_count,
24 const char* bytes,
25 NetLog::LogLevel log_level) {
26 DictionaryValue* dict = new DictionaryValue();
27 dict->SetInteger("byte_count", byte_count);
28 if (NetLog::IsLoggingBytes(log_level) && byte_count > 0)
29 dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, byte_count));
30 return dict;
[email protected]267a0d66d2011-06-01 21:15:1931}
32
[email protected]3abacd62012-06-10 20:20:3233Value* EventParametersCallback(
34 const scoped_refptr<NetLog::EventParameters>& params,
35 NetLog::LogLevel /* log_level */) {
36 if (!params.get())
37 return NULL;
38 return params->ToValue();
39}
40
41Value* SourceEventParametersCallback(const NetLog::Source source,
42 NetLog::LogLevel /* log_level */) {
43 DictionaryValue* event_params = new DictionaryValue();
44 source.AddToEventParameters(event_params);
45 return event_params;
46}
47
48Value* SingleIntegerCallback(const char* name,
49 int value,
50 NetLog::LogLevel /* log_level */) {
51 if (!value)
52 return NULL;
53 DictionaryValue* event_params = new DictionaryValue();
54 event_params->SetInteger(name, value);
55 return event_params;
[email protected]267a0d66d2011-06-01 21:15:1956}
57
58} // namespace
59
[email protected]ee094b82010-08-24 15:55:5160Value* NetLog::Source::ToValue() const {
61 DictionaryValue* dict = new DictionaryValue();
62 dict->SetInteger("type", static_cast<int>(type));
63 dict->SetInteger("id", static_cast<int>(id));
64 return dict;
65}
66
[email protected]3abacd62012-06-10 20:20:3267void NetLog::Source::AddToEventParameters(DictionaryValue* event_params) const {
68 DictionaryValue* dict = new DictionaryValue();
69 dict->SetInteger("type", static_cast<int>(type));
70 dict->SetInteger("id", static_cast<int>(id));
71 event_params->Set("source_dependency", dict);
72}
73
74NetLog::ParametersCallback NetLog::Source::ToEventParametersCallback() const {
75 return base::Bind(&SourceEventParametersCallback, *this);
76}
77
78// static
79bool NetLog::Source::FromEventParameters(Value* event_params, Source* source) {
80 DictionaryValue* dict;
81 DictionaryValue* source_dict;
82 int source_id;
83 int source_type;
84 if (!event_params ||
85 !event_params->GetAsDictionary(&dict) ||
86 !dict->GetDictionary("source_dependency", &source_dict) ||
87 !source_dict->GetInteger("id", &source_id) ||
88 !source_dict->GetInteger("type", &source_type)) {
89 *source = Source();
90 return false;
91 }
92
93 DCHECK_LE(0, source_id);
94 DCHECK_LT(source_type, NetLog::SOURCE_COUNT);
95 *source = Source(static_cast<SourceType>(source_type), source_id);
96 return true;
97}
98
99Value* NetLog::Entry::ToValue() const {
100 DictionaryValue* entry_dict(new DictionaryValue());
101
102 entry_dict->SetString("time", TickCountToString(base::TimeTicks::Now()));
103
104 // Set the entry source.
105 DictionaryValue* source_dict = new DictionaryValue();
106 source_dict->SetInteger("id", source_.id);
107 source_dict->SetInteger("type", static_cast<int>(source_.type));
108 entry_dict->Set("source", source_dict);
109
110 // Set the event info.
111 entry_dict->SetInteger("type", static_cast<int>(type_));
112 entry_dict->SetInteger("phase", static_cast<int>(phase_));
113
114 // Set the event-specific parameters.
115 if (parameters_callback_) {
116 Value* value = parameters_callback_->Run(log_level_);
117 if (value)
118 entry_dict->Set("params", value);
119 }
120
121 return entry_dict;
122}
123
124Value* NetLog::Entry::ParametersToValue() const {
125 if (parameters_callback_)
126 return parameters_callback_->Run(log_level_);
127 return NULL;
128}
129
130NetLog::Entry::Entry(
131 EventType type,
132 Source source,
133 EventPhase phase,
134 const ParametersCallback* parameters_callback,
135 LogLevel log_level)
136 : type_(type),
137 source_(source),
138 phase_(phase),
139 parameters_callback_(parameters_callback),
140 log_level_(log_level) {
141};
142
143NetLog::Entry::~Entry() {
144}
145
[email protected]d5cbd92a2012-02-29 12:43:23146NetLog::ThreadSafeObserver::ThreadSafeObserver() : log_level_(LOG_BASIC),
147 net_log_(NULL) {
[email protected]ae6e9912011-07-27 01:18:28148}
149
150NetLog::ThreadSafeObserver::~ThreadSafeObserver() {
[email protected]d5cbd92a2012-02-29 12:43:23151 // Make sure we aren't watching a NetLog on destruction. Because the NetLog
152 // may pass events to each observer on multiple threads, we cannot safely
153 // stop watching a NetLog automatically from a parent class.
154 DCHECK(!net_log_);
[email protected]ae6e9912011-07-27 01:18:28155}
156
157NetLog::LogLevel NetLog::ThreadSafeObserver::log_level() const {
[email protected]d5cbd92a2012-02-29 12:43:23158 DCHECK(net_log_);
[email protected]ae6e9912011-07-27 01:18:28159 return log_level_;
160}
161
[email protected]d5cbd92a2012-02-29 12:43:23162NetLog* NetLog::ThreadSafeObserver::net_log() const {
163 return net_log_;
164}
165
[email protected]3abacd62012-06-10 20:20:32166void NetLog::AddGlobalEntry(EventType type) {
167 AddEntry(type,
168 Source(net::NetLog::SOURCE_NONE, NextID()),
169 net::NetLog::PHASE_NONE,
170 NULL);
171}
172
173void NetLog::AddGlobalEntry(
174 EventType type,
175 const NetLog::ParametersCallback& parameters_callback) {
176 AddEntry(type,
177 Source(net::NetLog::SOURCE_NONE, NextID()),
178 net::NetLog::PHASE_NONE,
179 &parameters_callback);
180}
181
[email protected]fba65f7b2012-03-15 05:22:13182void NetLog::AddGlobalEntry(EventType type,
183 const scoped_refptr<EventParameters>& params) {
[email protected]3abacd62012-06-10 20:20:32184 ParametersCallback callback = base::Bind(&EventParametersCallback, params);
[email protected]fba65f7b2012-03-15 05:22:13185 AddEntry(type,
[email protected]3abacd62012-06-10 20:20:32186 Source(net::NetLog::SOURCE_NONE, NextID()),
[email protected]fba65f7b2012-03-15 05:22:13187 net::NetLog::PHASE_NONE,
[email protected]3abacd62012-06-10 20:20:32188 &callback);
[email protected]fba65f7b2012-03-15 05:22:13189}
190
[email protected]9e743cd2010-03-16 07:03:53191// static
[email protected]1ce7b66b2010-10-12 20:32:44192std::string NetLog::TickCountToString(const base::TimeTicks& time) {
193 int64 delta_time = (time - base::TimeTicks()).InMilliseconds();
194 return base::Int64ToString(delta_time);
195}
196
197// static
[email protected]9e743cd2010-03-16 07:03:53198const char* NetLog::EventTypeToString(EventType event) {
199 switch (event) {
200#define EVENT_TYPE(label) case TYPE_ ## label: return #label;
201#include "net/base/net_log_event_type_list.h"
202#undef EVENT_TYPE
[email protected]988394e2012-02-22 16:37:55203 default:
204 NOTREACHED();
205 return NULL;
[email protected]9e743cd2010-03-16 07:03:53206 }
[email protected]9e743cd2010-03-16 07:03:53207}
208
[email protected]fd9c0d92010-03-23 17:47:49209// static
[email protected]988394e2012-02-22 16:37:55210base::Value* NetLog::GetEventTypesAsValue() {
211 DictionaryValue* dict = new DictionaryValue();
212 for (int i = 0; i < EVENT_COUNT; ++i) {
213 dict->SetInteger(EventTypeToString(static_cast<EventType>(i)), i);
214 }
215 return dict;
[email protected]fd9c0d92010-03-23 17:47:49216}
217
[email protected]93190352010-08-13 13:55:10218// static
219const char* NetLog::SourceTypeToString(SourceType source) {
220 switch (source) {
[email protected]988394e2012-02-22 16:37:55221#define SOURCE_TYPE(label) case SOURCE_ ## label: return #label;
[email protected]93190352010-08-13 13:55:10222#include "net/base/net_log_source_type_list.h"
223#undef SOURCE_TYPE
[email protected]988394e2012-02-22 16:37:55224 default:
225 NOTREACHED();
226 return NULL;
[email protected]93190352010-08-13 13:55:10227 }
[email protected]988394e2012-02-22 16:37:55228}
229
230// static
231base::Value* NetLog::GetSourceTypesAsValue() {
232 DictionaryValue* dict = new DictionaryValue();
233 for (int i = 0; i < SOURCE_COUNT; ++i) {
234 dict->SetInteger(SourceTypeToString(static_cast<SourceType>(i)), i);
235 }
236 return dict;
[email protected]93190352010-08-13 13:55:10237}
238
239// static
240const char* NetLog::EventPhaseToString(EventPhase phase) {
241 switch (phase) {
242 case PHASE_BEGIN:
243 return "PHASE_BEGIN";
244 case PHASE_END:
245 return "PHASE_END";
246 case PHASE_NONE:
247 return "PHASE_NONE";
248 }
249 NOTREACHED();
250 return NULL;
251}
252
253// static
[email protected]3abacd62012-06-10 20:20:32254bool NetLog::IsLoggingBytes(LogLevel log_level) {
255 return log_level == NetLog::LOG_ALL;
256}
[email protected]93190352010-08-13 13:55:10257
[email protected]3abacd62012-06-10 20:20:32258// static
259bool NetLog::IsLoggingAllEvents(LogLevel log_level) {
260 return log_level <= NetLog::LOG_ALL_BUT_BYTES;
261}
[email protected]93190352010-08-13 13:55:10262
[email protected]3abacd62012-06-10 20:20:32263// static
264NetLog::ParametersCallback NetLog::IntegerCallback(const char* name,
265 int value) {
266 return base::Bind(&SingleIntegerCallback, name, value);
[email protected]93190352010-08-13 13:55:10267}
268
[email protected]d5cbd92a2012-02-29 12:43:23269void NetLog::OnAddObserver(ThreadSafeObserver* observer, LogLevel log_level) {
270 DCHECK(!observer->net_log_);
271 observer->net_log_ = this;
272 observer->log_level_ = log_level;
273}
274
275void NetLog::OnSetObserverLogLevel(ThreadSafeObserver* observer,
276 LogLevel log_level) {
277 DCHECK_EQ(this, observer->net_log_);
278 observer->log_level_ = log_level;
279}
280
281void NetLog::OnRemoveObserver(ThreadSafeObserver* observer) {
282 DCHECK_EQ(this, observer->net_log_);
283 observer->net_log_ = NULL;
284}
285
[email protected]3abacd62012-06-10 20:20:32286void NetLog::AddEntry(EventType type,
287 const Source& source,
288 EventPhase phase,
289 const NetLog::ParametersCallback* parameters_callback) {
290 Entry entry(type, source, phase, parameters_callback, GetLogLevel());
291 OnAddEntry(entry);
292}
293
294void BoundNetLog::AddEntry(NetLog::EventType type,
295 NetLog::EventPhase phase) const {
296 if (!net_log_)
297 return;
298 net_log_->AddEntry(type, source_, phase, NULL);
299}
300
301void BoundNetLog::AddEntry(
302 NetLog::EventType type,
303 NetLog::EventPhase phase,
304 const NetLog::ParametersCallback& get_parameters) const {
305 if (!net_log_)
306 return;
307 net_log_->AddEntry(type, source_, phase, &get_parameters);
308}
309
310void BoundNetLog::AddEvent(NetLog::EventType type) const {
311 AddEntry(type, NetLog::PHASE_NONE);
312}
313
314void BoundNetLog::AddEvent(
315 NetLog::EventType type,
316 const NetLog::ParametersCallback& get_parameters) const {
317 AddEntry(type, NetLog::PHASE_NONE, get_parameters);
318}
319
320void BoundNetLog::BeginEvent(NetLog::EventType type) const {
321 AddEntry(type, NetLog::PHASE_BEGIN);
322}
323
324void BoundNetLog::BeginEvent(
325 NetLog::EventType type,
326 const NetLog::ParametersCallback& get_parameters) const {
327 AddEntry(type, NetLog::PHASE_BEGIN, get_parameters);
328}
329
330void BoundNetLog::EndEvent(NetLog::EventType type) const {
331 AddEntry(type, NetLog::PHASE_END);
332}
333
334void BoundNetLog::EndEvent(
335 NetLog::EventType type,
336 const NetLog::ParametersCallback& get_parameters) const {
337 AddEntry(type, NetLog::PHASE_END, get_parameters);
338}
339
[email protected]ec11be62010-04-28 19:28:09340void BoundNetLog::AddEntry(
341 NetLog::EventType type,
342 NetLog::EventPhase phase,
343 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]3abacd62012-06-10 20:20:32344 if (!net_log_)
345 return;
346 NetLog::ParametersCallback callback =
347 base::Bind(&EventParametersCallback, params);
348 net_log_->AddEntry(type, source_, phase, &callback);
[email protected]9e743cd2010-03-16 07:03:53349}
350
[email protected]ec11be62010-04-28 19:28:09351void BoundNetLog::AddEvent(
[email protected]1f0e32b2010-04-09 04:34:47352 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09353 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47354 AddEntry(event_type, NetLog::PHASE_NONE, params);
[email protected]9e743cd2010-03-16 07:03:53355}
356
[email protected]ec11be62010-04-28 19:28:09357void BoundNetLog::BeginEvent(
[email protected]1f0e32b2010-04-09 04:34:47358 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09359 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47360 AddEntry(event_type, NetLog::PHASE_BEGIN, params);
361}
362
[email protected]ec11be62010-04-28 19:28:09363void BoundNetLog::EndEvent(
[email protected]1f0e32b2010-04-09 04:34:47364 NetLog::EventType event_type,
[email protected]ec11be62010-04-28 19:28:09365 const scoped_refptr<NetLog::EventParameters>& params) const {
[email protected]1f0e32b2010-04-09 04:34:47366 AddEntry(event_type, NetLog::PHASE_END, params);
367}
368
[email protected]8866f622011-10-18 20:08:10369void BoundNetLog::AddEventWithNetErrorCode(NetLog::EventType event_type,
370 int net_error) const {
371 DCHECK_GT(0, net_error);
372 DCHECK_NE(ERR_IO_PENDING, net_error);
[email protected]3abacd62012-06-10 20:20:32373 AddEvent(event_type, NetLog::IntegerCallback("net_error", net_error));
[email protected]8866f622011-10-18 20:08:10374}
375
[email protected]f6f1bebc2011-01-07 03:04:54376void BoundNetLog::EndEventWithNetErrorCode(NetLog::EventType event_type,
377 int net_error) const {
[email protected]8866f622011-10-18 20:08:10378 DCHECK_NE(ERR_IO_PENDING, net_error);
[email protected]3abacd62012-06-10 20:20:32379 EndEvent(event_type, NetLog::IntegerCallback("net_error", net_error));
[email protected]f6f1bebc2011-01-07 03:04:54380}
381
[email protected]267a0d66d2011-06-01 21:15:19382void BoundNetLog::AddByteTransferEvent(NetLog::EventType event_type,
[email protected]8866f622011-10-18 20:08:10383 int byte_count,
384 const char* bytes) const {
[email protected]3abacd62012-06-10 20:20:32385 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes));
[email protected]267a0d66d2011-06-01 21:15:19386}
387
[email protected]be1a48b2011-01-20 00:12:13388NetLog::LogLevel BoundNetLog::GetLogLevel() const {
389 if (net_log_)
390 return net_log_->GetLogLevel();
391 return NetLog::LOG_BASIC;
392}
393
394bool BoundNetLog::IsLoggingBytes() const {
[email protected]3abacd62012-06-10 20:20:32395 return NetLog::IsLoggingBytes(GetLogLevel());
[email protected]be1a48b2011-01-20 00:12:13396}
397
398bool BoundNetLog::IsLoggingAllEvents() const {
[email protected]3abacd62012-06-10 20:20:32399 return NetLog::IsLoggingAllEvents(GetLogLevel());
[email protected]be1a48b2011-01-20 00:12:13400}
401
[email protected]9e743cd2010-03-16 07:03:53402// static
403BoundNetLog BoundNetLog::Make(NetLog* net_log,
404 NetLog::SourceType source_type) {
405 if (!net_log)
406 return BoundNetLog();
407
408 NetLog::Source source(source_type, net_log->NextID());
409 return BoundNetLog(source, net_log);
410}
411
[email protected]d13f51b2010-04-27 23:20:45412NetLogStringParameter::NetLogStringParameter(const char* name,
413 const std::string& value)
414 : name_(name), value_(value) {
[email protected]1f0e32b2010-04-09 04:34:47415}
416
[email protected]9349cfb2010-08-31 18:00:53417NetLogStringParameter::~NetLogStringParameter() {
418}
419
[email protected]d13f51b2010-04-27 23:20:45420Value* NetLogIntegerParameter::ToValue() const {
421 DictionaryValue* dict = new DictionaryValue();
[email protected]ccaff652010-07-31 06:28:20422 dict->SetInteger(name_, value_);
[email protected]d13f51b2010-04-27 23:20:45423 return dict;
[email protected]1f0e32b2010-04-09 04:34:47424}
425
[email protected]d13f51b2010-04-27 23:20:45426Value* NetLogStringParameter::ToValue() const {
427 DictionaryValue* dict = new DictionaryValue();
[email protected]ccaff652010-07-31 06:28:20428 dict->SetString(name_, value_);
[email protected]d13f51b2010-04-27 23:20:45429 return dict;
[email protected]1f0e32b2010-04-09 04:34:47430}
431
[email protected]06650c52010-06-03 00:49:17432Value* NetLogSourceParameter::ToValue() const {
433 DictionaryValue* dict = new DictionaryValue();
[email protected]68697a42011-12-19 21:47:55434 if (value_.is_valid())
435 dict->Set(name_, value_.ToValue());
[email protected]06650c52010-06-03 00:49:17436 return dict;
437}
438
[email protected]9e743cd2010-03-16 07:03:53439} // namespace net