OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/log/net_log.h" | 5 #include "net/log/net_log.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/debug/alias.h" | 8 #include "base/debug/alias.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "base/values.h" | 13 #include "base/values.h" |
14 #include "net/base/net_errors.h" | 14 #include "net/base/net_errors.h" |
15 | 15 |
16 namespace net { | 16 namespace net { |
17 | 17 |
18 namespace { | 18 namespace { |
19 | 19 |
20 // Returns parameters for logging data transferred events. At a minum includes | 20 // Returns parameters for logging data transferred events. At a minum includes |
21 // the number of bytes transferred. If the capture mode allows logging byte | 21 // the number of bytes transferred. If the capture mode allows logging byte |
22 // contents and |byte_count| > 0, then will include the actual bytes. The | 22 // contents and |byte_count| > 0, then will include the actual bytes. The |
23 // bytes are hex-encoded, since base::StringValue only supports UTF-8. | 23 // bytes are hex-encoded, since base::StringValue only supports UTF-8. |
24 base::Value* BytesTransferredCallback(int byte_count, | 24 scoped_ptr<base::Value> BytesTransferredCallback( |
25 const char* bytes, | 25 int byte_count, |
26 NetLogCaptureMode capture_mode) { | 26 const char* bytes, |
27 base::DictionaryValue* dict = new base::DictionaryValue(); | 27 NetLogCaptureMode capture_mode) { |
| 28 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
28 dict->SetInteger("byte_count", byte_count); | 29 dict->SetInteger("byte_count", byte_count); |
29 if (capture_mode.include_socket_bytes() && byte_count > 0) | 30 if (capture_mode.include_socket_bytes() && byte_count > 0) |
30 dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, byte_count)); | 31 dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, byte_count)); |
31 return dict; | 32 return dict.Pass(); |
32 } | 33 } |
33 | 34 |
34 base::Value* SourceEventParametersCallback( | 35 scoped_ptr<base::Value> SourceEventParametersCallback( |
35 const NetLog::Source source, | 36 const NetLog::Source source, |
36 NetLogCaptureMode /* capture_mode */) { | 37 NetLogCaptureMode /* capture_mode */) { |
37 if (!source.IsValid()) | 38 if (!source.IsValid()) |
38 return NULL; | 39 return NULL; |
39 base::DictionaryValue* event_params = new base::DictionaryValue(); | 40 scoped_ptr<base::DictionaryValue> event_params(new base::DictionaryValue()); |
40 source.AddToEventParameters(event_params); | 41 source.AddToEventParameters(event_params.release()); |
41 return event_params; | 42 return event_params.Pass(); |
42 } | 43 } |
43 | 44 |
44 base::Value* NetLogBoolCallback(const char* name, | 45 scoped_ptr<base::Value> NetLogBoolCallback( |
45 bool value, | 46 const char* name, |
46 NetLogCaptureMode /* capture_mode */) { | 47 bool value, |
47 base::DictionaryValue* event_params = new base::DictionaryValue(); | 48 NetLogCaptureMode /* capture_mode */) { |
| 49 scoped_ptr<base::DictionaryValue> event_params(new base::DictionaryValue()); |
48 event_params->SetBoolean(name, value); | 50 event_params->SetBoolean(name, value); |
49 return event_params; | 51 return event_params.Pass(); |
50 } | 52 } |
51 | 53 |
52 base::Value* NetLogIntegerCallback(const char* name, | 54 scoped_ptr<base::Value> NetLogIntegerCallback( |
53 int value, | 55 const char* name, |
54 NetLogCaptureMode /* capture_mode */) { | 56 int value, |
55 base::DictionaryValue* event_params = new base::DictionaryValue(); | 57 NetLogCaptureMode /* capture_mode */) { |
| 58 scoped_ptr<base::DictionaryValue> event_params(new base::DictionaryValue()); |
56 event_params->SetInteger(name, value); | 59 event_params->SetInteger(name, value); |
57 return event_params; | 60 return event_params.Pass(); |
58 } | 61 } |
59 | 62 |
60 base::Value* NetLogInt64Callback(const char* name, | 63 scoped_ptr<base::Value> NetLogInt64Callback( |
61 int64 value, | 64 const char* name, |
62 NetLogCaptureMode /* capture_mode */) { | 65 int64 value, |
63 base::DictionaryValue* event_params = new base::DictionaryValue(); | 66 NetLogCaptureMode /* capture_mode */) { |
| 67 scoped_ptr<base::DictionaryValue> event_params(new base::DictionaryValue()); |
64 event_params->SetString(name, base::Int64ToString(value)); | 68 event_params->SetString(name, base::Int64ToString(value)); |
65 return event_params; | 69 return event_params.Pass(); |
66 } | 70 } |
67 | 71 |
68 base::Value* NetLogStringCallback(const char* name, | 72 scoped_ptr<base::Value> NetLogStringCallback( |
69 const std::string* value, | 73 const char* name, |
70 NetLogCaptureMode /* capture_mode */) { | 74 const std::string* value, |
71 base::DictionaryValue* event_params = new base::DictionaryValue(); | 75 NetLogCaptureMode /* capture_mode */) { |
| 76 scoped_ptr<base::DictionaryValue> event_params(new base::DictionaryValue()); |
72 event_params->SetString(name, *value); | 77 event_params->SetString(name, *value); |
73 return event_params; | 78 return event_params.Pass(); |
74 } | 79 } |
75 | 80 |
76 base::Value* NetLogString16Callback(const char* name, | 81 scoped_ptr<base::Value> NetLogString16Callback( |
77 const base::string16* value, | 82 const char* name, |
78 NetLogCaptureMode /* capture_mode */) { | 83 const base::string16* value, |
79 base::DictionaryValue* event_params = new base::DictionaryValue(); | 84 NetLogCaptureMode /* capture_mode */) { |
| 85 scoped_ptr<base::DictionaryValue> event_params(new base::DictionaryValue()); |
80 event_params->SetString(name, *value); | 86 event_params->SetString(name, *value); |
81 return event_params; | 87 return event_params.Pass(); |
82 } | 88 } |
83 | 89 |
84 } // namespace | 90 } // namespace |
85 | 91 |
86 // LoadTimingInfo requires this be 0. | 92 // LoadTimingInfo requires this be 0. |
87 const uint32 NetLog::Source::kInvalidId = 0; | 93 const uint32 NetLog::Source::kInvalidId = 0; |
88 | 94 |
89 NetLog::Source::Source() : type(SOURCE_NONE), id(kInvalidId) { | 95 NetLog::Source::Source() : type(SOURCE_NONE), id(kInvalidId) { |
90 } | 96 } |
91 | 97 |
92 NetLog::Source::Source(SourceType type, uint32 id) : type(type), id(id) { | 98 NetLog::Source::Source(SourceType type, uint32 id) : type(type), id(id) { |
93 } | 99 } |
94 | 100 |
95 bool NetLog::Source::IsValid() const { | 101 bool NetLog::Source::IsValid() const { |
96 return id != kInvalidId; | 102 return id != kInvalidId; |
97 } | 103 } |
98 | 104 |
99 void NetLog::Source::AddToEventParameters( | 105 void NetLog::Source::AddToEventParameters( |
100 base::DictionaryValue* event_params) const { | 106 base::DictionaryValue* event_params) const { |
101 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); | 107 scoped_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
102 dict->SetInteger("type", static_cast<int>(type)); | 108 dict->SetInteger("type", static_cast<int>(type)); |
103 dict->SetInteger("id", static_cast<int>(id)); | 109 dict->SetInteger("id", static_cast<int>(id)); |
104 event_params->Set("source_dependency", dict.Pass()); | 110 event_params->Set("source_dependency", dict.Pass()); |
105 } | 111 } |
106 | 112 |
107 NetLog::ParametersCallback NetLog::Source::ToEventParametersCallback() const { | 113 NetLog::ParametersCallback NetLog::Source::ToEventParametersCallback() const { |
108 return base::Bind(&SourceEventParametersCallback, *this); | 114 return base::Bind(SourceEventParametersCallback, *this); |
109 } | 115 } |
110 | 116 |
111 // static | 117 // static |
112 bool NetLog::Source::FromEventParameters(base::Value* event_params, | 118 bool NetLog::Source::FromEventParameters(base::Value* event_params, |
113 Source* source) { | 119 Source* source) { |
114 base::DictionaryValue* dict = NULL; | 120 base::DictionaryValue* dict = NULL; |
115 base::DictionaryValue* source_dict = NULL; | 121 base::DictionaryValue* source_dict = NULL; |
116 int source_id = -1; | 122 int source_id = -1; |
117 int source_type = NetLog::SOURCE_COUNT; | 123 int source_type = NetLog::SOURCE_COUNT; |
118 if (!event_params || !event_params->GetAsDictionary(&dict) || | 124 if (!event_params || !event_params->GetAsDictionary(&dict) || |
119 !dict->GetDictionary("source_dependency", &source_dict) || | 125 !dict->GetDictionary("source_dependency", &source_dict) || |
120 !source_dict->GetInteger("id", &source_id) || | 126 !source_dict->GetInteger("id", &source_id) || |
121 !source_dict->GetInteger("type", &source_type)) { | 127 !source_dict->GetInteger("type", &source_type)) { |
122 *source = Source(); | 128 *source = Source(); |
123 return false; | 129 return false; |
124 } | 130 } |
125 | 131 |
126 DCHECK_GE(source_id, 0); | 132 DCHECK_GE(source_id, 0); |
127 DCHECK_LT(source_type, NetLog::SOURCE_COUNT); | 133 DCHECK_LT(source_type, NetLog::SOURCE_COUNT); |
128 *source = Source(static_cast<SourceType>(source_type), source_id); | 134 *source = Source(static_cast<SourceType>(source_type), source_id); |
129 return true; | 135 return true; |
130 } | 136 } |
131 | 137 |
132 base::Value* NetLog::Entry::ToValue() const { | 138 scoped_ptr<base::Value> NetLog::Entry::ToValue() const { |
133 base::DictionaryValue* entry_dict(new base::DictionaryValue()); | 139 scoped_ptr<base::DictionaryValue> entry_dict(new base::DictionaryValue()); |
134 | 140 |
135 entry_dict->SetString("time", TickCountToString(data_->time)); | 141 entry_dict->SetString("time", TickCountToString(data_->time)); |
136 | 142 |
137 // Set the entry source. | 143 // Set the entry source. |
138 scoped_ptr<base::DictionaryValue> source_dict(new base::DictionaryValue()); | 144 scoped_ptr<base::DictionaryValue> source_dict(new base::DictionaryValue()); |
139 source_dict->SetInteger("id", data_->source.id); | 145 source_dict->SetInteger("id", data_->source.id); |
140 source_dict->SetInteger("type", static_cast<int>(data_->source.type)); | 146 source_dict->SetInteger("type", static_cast<int>(data_->source.type)); |
141 entry_dict->Set("source", source_dict.Pass()); | 147 entry_dict->Set("source", source_dict.Pass()); |
142 | 148 |
143 // Set the event info. | 149 // Set the event info. |
144 entry_dict->SetInteger("type", static_cast<int>(data_->type)); | 150 entry_dict->SetInteger("type", static_cast<int>(data_->type)); |
145 entry_dict->SetInteger("phase", static_cast<int>(data_->phase)); | 151 entry_dict->SetInteger("phase", static_cast<int>(data_->phase)); |
146 | 152 |
147 // Set the event-specific parameters. | 153 // Set the event-specific parameters. |
148 if (data_->parameters_callback) { | 154 if (data_->parameters_callback) { |
149 scoped_ptr<base::Value> value( | 155 scoped_ptr<base::Value> value( |
150 data_->parameters_callback->Run(capture_mode_)); | 156 data_->parameters_callback->Run(capture_mode_)); |
151 if (value) | 157 if (value) |
152 entry_dict->Set("params", value.Pass()); | 158 entry_dict->Set("params", value.Pass()); |
153 } | 159 } |
154 | 160 |
155 return entry_dict; | 161 return entry_dict.Pass(); |
156 } | 162 } |
157 | 163 |
158 base::Value* NetLog::Entry::ParametersToValue() const { | 164 scoped_ptr<base::Value> NetLog::Entry::ParametersToValue() const { |
159 if (data_->parameters_callback) | 165 if (data_->parameters_callback) |
160 return data_->parameters_callback->Run(capture_mode_); | 166 return data_->parameters_callback->Run(capture_mode_); |
161 return NULL; | 167 return NULL; |
162 } | 168 } |
163 | 169 |
164 NetLog::EntryData::EntryData(EventType type, | 170 NetLog::EntryData::EntryData(EventType type, |
165 Source source, | 171 Source source, |
166 EventPhase phase, | 172 EventPhase phase, |
167 base::TimeTicks time, | 173 base::TimeTicks time, |
168 const ParametersCallback* parameters_callback) | 174 const ParametersCallback* parameters_callback) |
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
330 return "PHASE_END"; | 336 return "PHASE_END"; |
331 case PHASE_NONE: | 337 case PHASE_NONE: |
332 return "PHASE_NONE"; | 338 return "PHASE_NONE"; |
333 } | 339 } |
334 NOTREACHED(); | 340 NOTREACHED(); |
335 return NULL; | 341 return NULL; |
336 } | 342 } |
337 | 343 |
338 // static | 344 // static |
339 NetLog::ParametersCallback NetLog::BoolCallback(const char* name, bool value) { | 345 NetLog::ParametersCallback NetLog::BoolCallback(const char* name, bool value) { |
340 return base::Bind(&NetLogBoolCallback, name, value); | 346 return base::Bind(NetLogBoolCallback, name, value); |
341 } | 347 } |
342 | 348 |
343 // static | 349 // static |
344 NetLog::ParametersCallback NetLog::IntegerCallback(const char* name, | 350 NetLog::ParametersCallback NetLog::IntegerCallback(const char* name, |
345 int value) { | 351 int value) { |
346 return base::Bind(&NetLogIntegerCallback, name, value); | 352 return base::Bind(NetLogIntegerCallback, name, value); |
347 } | 353 } |
348 | 354 |
349 // static | 355 // static |
350 NetLog::ParametersCallback NetLog::Int64Callback(const char* name, | 356 NetLog::ParametersCallback NetLog::Int64Callback(const char* name, |
351 int64 value) { | 357 int64 value) { |
352 return base::Bind(&NetLogInt64Callback, name, value); | 358 return base::Bind(NetLogInt64Callback, name, value); |
353 } | 359 } |
354 | 360 |
355 // static | 361 // static |
356 NetLog::ParametersCallback NetLog::StringCallback(const char* name, | 362 NetLog::ParametersCallback NetLog::StringCallback(const char* name, |
357 const std::string* value) { | 363 const std::string* value) { |
358 DCHECK(value); | 364 DCHECK(value); |
359 return base::Bind(&NetLogStringCallback, name, value); | 365 return base::Bind(NetLogStringCallback, name, value); |
360 } | 366 } |
361 | 367 |
362 // static | 368 // static |
363 NetLog::ParametersCallback NetLog::StringCallback(const char* name, | 369 NetLog::ParametersCallback NetLog::StringCallback(const char* name, |
364 const base::string16* value) { | 370 const base::string16* value) { |
365 DCHECK(value); | 371 DCHECK(value); |
366 return base::Bind(&NetLogString16Callback, name, value); | 372 return base::Bind(NetLogString16Callback, name, value); |
367 } | 373 } |
368 | 374 |
369 void NetLog::AddEntry(EventType type, | 375 void NetLog::AddEntry(EventType type, |
370 const Source& source, | 376 const Source& source, |
371 EventPhase phase, | 377 EventPhase phase, |
372 const NetLog::ParametersCallback* parameters_callback) { | 378 const NetLog::ParametersCallback* parameters_callback) { |
373 if (!IsCapturing()) | 379 if (!IsCapturing()) |
374 return; | 380 return; |
375 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), | 381 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), |
376 parameters_callback); | 382 parameters_callback); |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
478 Liveness liveness = liveness_; | 484 Liveness liveness = liveness_; |
479 | 485 |
480 if (liveness == ALIVE) | 486 if (liveness == ALIVE) |
481 return; | 487 return; |
482 | 488 |
483 base::debug::Alias(&liveness); | 489 base::debug::Alias(&liveness); |
484 CHECK_EQ(ALIVE, liveness); | 490 CHECK_EQ(ALIVE, liveness); |
485 } | 491 } |
486 | 492 |
487 } // namespace net | 493 } // namespace net |
OLD | NEW |