Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(405)

Side by Side Diff: net/log/net_log.cc

Issue 2333923004: Extracting NetLog inner classes into their own classes. (Closed)
Patch Set: Some nit fixes and better, impl-agnostic naming of net_log_parameters_callback_typedef.h -> net/log… Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <utility> 7 #include <utility>
8 8
9 #include "base/bind.h" 9 #include "base/bind.h"
10 #include "base/debug/alias.h"
11 #include "base/logging.h" 10 #include "base/logging.h"
12 #include "base/strings/string_number_conversions.h" 11 #include "base/strings/string_number_conversions.h"
13 #include "base/strings/utf_string_conversions.h" 12 #include "base/strings/utf_string_conversions.h"
14 #include "base/time/time.h"
15 #include "base/values.h" 13 #include "base/values.h"
16 #include "net/base/net_errors.h"
17 14
18 namespace net { 15 namespace net {
19 16
20 namespace { 17 namespace {
21 18
22 // Returns parameters for logging data transferred events. At a minimum includes
23 // the number of bytes transferred. If the capture mode allows logging byte
24 // contents and |byte_count| > 0, then will include the actual bytes. The
25 // bytes are hex-encoded, since base::StringValue only supports UTF-8.
26 std::unique_ptr<base::Value> BytesTransferredCallback(
27 int byte_count,
28 const char* bytes,
29 NetLogCaptureMode capture_mode) {
30 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
31 dict->SetInteger("byte_count", byte_count);
32 if (capture_mode.include_socket_bytes() && byte_count > 0)
33 dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, byte_count));
34 return std::move(dict);
35 }
36
37 std::unique_ptr<base::Value> SourceEventParametersCallback(
38 const NetLog::Source source,
39 NetLogCaptureMode /* capture_mode */) {
40 if (!source.IsValid())
41 return std::unique_ptr<base::Value>();
42 std::unique_ptr<base::DictionaryValue> event_params(
43 new base::DictionaryValue());
44 source.AddToEventParameters(event_params.get());
45 return std::move(event_params);
46 }
47
48 std::unique_ptr<base::Value> NetLogBoolCallback( 19 std::unique_ptr<base::Value> NetLogBoolCallback(
49 const char* name, 20 const char* name,
50 bool value, 21 bool value,
51 NetLogCaptureMode /* capture_mode */) { 22 NetLogCaptureMode /* capture_mode */) {
52 std::unique_ptr<base::DictionaryValue> event_params( 23 std::unique_ptr<base::DictionaryValue> event_params(
53 new base::DictionaryValue()); 24 new base::DictionaryValue());
54 event_params->SetBoolean(name, value); 25 event_params->SetBoolean(name, value);
55 return std::move(event_params); 26 return std::move(event_params);
56 } 27 }
57 28
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
100 const base::string16* value, 71 const base::string16* value,
101 NetLogCaptureMode /* capture_mode */) { 72 NetLogCaptureMode /* capture_mode */) {
102 std::unique_ptr<base::DictionaryValue> event_params( 73 std::unique_ptr<base::DictionaryValue> event_params(
103 new base::DictionaryValue()); 74 new base::DictionaryValue());
104 event_params->SetString(name, *value); 75 event_params->SetString(name, *value);
105 return std::move(event_params); 76 return std::move(event_params);
106 } 77 }
107 78
108 } // namespace 79 } // namespace
109 80
110 // LoadTimingInfo requires this be 0.
111 const uint32_t NetLog::Source::kInvalidId = 0;
112
113 NetLog::Source::Source() : type(NetLogSourceType::NONE), id(kInvalidId) {}
114
115 NetLog::Source::Source(NetLogSourceType type, uint32_t id)
116 : type(type), id(id) {}
117
118 bool NetLog::Source::IsValid() const {
119 return id != kInvalidId;
120 }
121
122 void NetLog::Source::AddToEventParameters(
123 base::DictionaryValue* event_params) const {
124 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
125 dict->SetInteger("type", static_cast<int>(type));
126 dict->SetInteger("id", static_cast<int>(id));
127 event_params->Set("source_dependency", std::move(dict));
128 }
129
130 NetLog::ParametersCallback NetLog::Source::ToEventParametersCallback() const {
131 return base::Bind(&SourceEventParametersCallback, *this);
132 }
133
134 // static
135 bool NetLog::Source::FromEventParameters(base::Value* event_params,
136 Source* source) {
137 base::DictionaryValue* dict = NULL;
138 base::DictionaryValue* source_dict = NULL;
139 int source_id = -1;
140 int source_type = static_cast<int>(NetLogSourceType::COUNT);
141 if (!event_params || !event_params->GetAsDictionary(&dict) ||
142 !dict->GetDictionary("source_dependency", &source_dict) ||
143 !source_dict->GetInteger("id", &source_id) ||
144 !source_dict->GetInteger("type", &source_type)) {
145 *source = Source();
146 return false;
147 }
148
149 DCHECK_GE(source_id, 0);
150 DCHECK_LT(source_type, static_cast<int>(NetLogSourceType::COUNT));
151 *source = Source(static_cast<NetLogSourceType>(source_type), source_id);
152 return true;
153 }
154
155 std::unique_ptr<base::Value> NetLog::Entry::ToValue() const {
156 std::unique_ptr<base::DictionaryValue> entry_dict(
157 new base::DictionaryValue());
158
159 entry_dict->SetString("time", TickCountToString(data_->time));
160
161 // Set the entry source.
162 std::unique_ptr<base::DictionaryValue> source_dict(
163 new base::DictionaryValue());
164 source_dict->SetInteger("id", data_->source.id);
165 source_dict->SetInteger("type", static_cast<int>(data_->source.type));
166 entry_dict->Set("source", std::move(source_dict));
167
168 // Set the event info.
169 entry_dict->SetInteger("type", static_cast<int>(data_->type));
170 entry_dict->SetInteger("phase", static_cast<int>(data_->phase));
171
172 // Set the event-specific parameters.
173 if (data_->parameters_callback) {
174 std::unique_ptr<base::Value> value(
175 data_->parameters_callback->Run(capture_mode_));
176 if (value)
177 entry_dict->Set("params", std::move(value));
178 }
179
180 return std::move(entry_dict);
181 }
182
183 std::unique_ptr<base::Value> NetLog::Entry::ParametersToValue() const {
184 if (data_->parameters_callback)
185 return data_->parameters_callback->Run(capture_mode_);
186 return nullptr;
187 }
188
189 NetLog::EntryData::EntryData(NetLogEventType type,
190 Source source,
191 NetLogEventPhase phase,
192 base::TimeTicks time,
193 const ParametersCallback* parameters_callback)
194 : type(type),
195 source(source),
196 phase(phase),
197 time(time),
198 parameters_callback(parameters_callback) {}
199
200 NetLog::EntryData::~EntryData() {
201 }
202
203 NetLog::Entry::Entry(const EntryData* data, NetLogCaptureMode capture_mode)
204 : data_(data), capture_mode_(capture_mode) {
205 }
206
207 NetLog::Entry::~Entry() {
208 }
209
210 NetLog::ThreadSafeObserver::ThreadSafeObserver() : net_log_(NULL) { 81 NetLog::ThreadSafeObserver::ThreadSafeObserver() : net_log_(NULL) {
211 } 82 }
212 83
213 NetLog::ThreadSafeObserver::~ThreadSafeObserver() { 84 NetLog::ThreadSafeObserver::~ThreadSafeObserver() {
214 // Make sure we aren't watching a NetLog on destruction. Because the NetLog 85 // Make sure we aren't watching a NetLog on destruction. Because the NetLog
215 // may pass events to each observer on multiple threads, we cannot safely 86 // may pass events to each observer on multiple threads, we cannot safely
216 // stop watching a NetLog automatically from a parent class. 87 // stop watching a NetLog automatically from a parent class.
217 DCHECK(!net_log_); 88 DCHECK(!net_log_);
218 } 89 }
219 90
220 NetLogCaptureMode NetLog::ThreadSafeObserver::capture_mode() const { 91 NetLogCaptureMode NetLog::ThreadSafeObserver::capture_mode() const {
221 DCHECK(net_log_); 92 DCHECK(net_log_);
222 return capture_mode_; 93 return capture_mode_;
223 } 94 }
224 95
225 NetLog* NetLog::ThreadSafeObserver::net_log() const { 96 NetLog* NetLog::ThreadSafeObserver::net_log() const {
226 return net_log_; 97 return net_log_;
227 } 98 }
228 99
229 void NetLog::ThreadSafeObserver::OnAddEntryData(const EntryData& entry_data) { 100 void NetLog::ThreadSafeObserver::OnAddEntryData(
230 OnAddEntry(Entry(&entry_data, capture_mode())); 101 const NetLogEntryData& entry_data) {
102 OnAddEntry(NetLogEntry(&entry_data, capture_mode()));
231 } 103 }
232 104
233 NetLog::NetLog() : last_id_(0), is_capturing_(0) { 105 NetLog::NetLog() : last_id_(0), is_capturing_(0) {
234 } 106 }
235 107
236 NetLog::~NetLog() { 108 NetLog::~NetLog() {
237 } 109 }
238 110
239 void NetLog::AddGlobalEntry(NetLogEventType type) { 111 void NetLog::AddGlobalEntry(NetLogEventType type) {
240 AddEntry(type, Source(NetLogSourceType::NONE, NextID()), 112 AddEntry(type, NetLogSource(NetLogSourceType::NONE, NextID()),
241 NetLogEventPhase::NONE, NULL); 113 NetLogEventPhase::NONE, NULL);
242 } 114 }
243 115
244 void NetLog::AddGlobalEntry( 116 void NetLog::AddGlobalEntry(
245 NetLogEventType type, 117 NetLogEventType type,
246 const NetLog::ParametersCallback& parameters_callback) { 118 const NetLogParametersCallback& parameters_callback) {
247 AddEntry(type, Source(NetLogSourceType::NONE, NextID()), 119 AddEntry(type, NetLogSource(NetLogSourceType::NONE, NextID()),
248 NetLogEventPhase::NONE, &parameters_callback); 120 NetLogEventPhase::NONE, &parameters_callback);
249 } 121 }
250 122
251 uint32_t NetLog::NextID() { 123 uint32_t NetLog::NextID() {
252 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1); 124 return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
253 } 125 }
254 126
255 bool NetLog::IsCapturing() const { 127 bool NetLog::IsCapturing() const {
256 return base::subtle::NoBarrier_Load(&is_capturing_) != 0; 128 return base::subtle::NoBarrier_Load(&is_capturing_) != 0;
257 } 129 }
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
353 case NetLogEventPhase::END: 225 case NetLogEventPhase::END:
354 return "PHASE_END"; 226 return "PHASE_END";
355 case NetLogEventPhase::NONE: 227 case NetLogEventPhase::NONE:
356 return "PHASE_NONE"; 228 return "PHASE_NONE";
357 } 229 }
358 NOTREACHED(); 230 NOTREACHED();
359 return NULL; 231 return NULL;
360 } 232 }
361 233
362 // static 234 // static
363 NetLog::ParametersCallback NetLog::BoolCallback(const char* name, bool value) { 235 NetLogParametersCallback NetLog::BoolCallback(const char* name, bool value) {
364 return base::Bind(&NetLogBoolCallback, name, value); 236 return base::Bind(&NetLogBoolCallback, name, value);
365 } 237 }
366 238
367 // static 239 // static
368 NetLog::ParametersCallback NetLog::IntCallback(const char* name, int value) { 240 NetLogParametersCallback NetLog::IntCallback(const char* name, int value) {
369 return base::Bind(&NetLogIntCallback, name, value); 241 return base::Bind(&NetLogIntCallback, name, value);
370 } 242 }
371 243
372 // static 244 // static
373 NetLog::ParametersCallback NetLog::Int64Callback(const char* name, 245 NetLogParametersCallback NetLog::Int64Callback(const char* name,
374 int64_t value) { 246 int64_t value) {
375 return base::Bind(&NetLogInt64Callback, name, value); 247 return base::Bind(&NetLogInt64Callback, name, value);
376 } 248 }
377 249
378 // static 250 // static
379 NetLog::ParametersCallback NetLog::StringCallback(const char* name, 251 NetLogParametersCallback NetLog::StringCallback(const char* name,
380 const std::string* value) { 252 const std::string* value) {
381 DCHECK(value); 253 DCHECK(value);
382 return base::Bind(&NetLogStringCallback, name, value); 254 return base::Bind(&NetLogStringCallback, name, value);
383 } 255 }
384 256
385 // static 257 // static
386 NetLog::ParametersCallback NetLog::StringCallback(const char* name, 258 NetLogParametersCallback NetLog::StringCallback(const char* name,
387 const char* value) { 259 const char* value) {
388 DCHECK(value); 260 DCHECK(value);
389 return base::Bind(&NetLogCharStringCallback, name, value); 261 return base::Bind(&NetLogCharStringCallback, name, value);
390 } 262 }
391 263
392 // static 264 // static
393 NetLog::ParametersCallback NetLog::StringCallback(const char* name, 265 NetLogParametersCallback NetLog::StringCallback(const char* name,
394 const base::string16* value) { 266 const base::string16* value) {
395 DCHECK(value); 267 DCHECK(value);
396 return base::Bind(&NetLogString16Callback, name, value); 268 return base::Bind(&NetLogString16Callback, name, value);
397 } 269 }
398 270
399 void NetLog::AddEntry(NetLogEventType type, 271 void NetLog::AddEntry(NetLogEventType type,
400 const Source& source, 272 const NetLogSource& source,
401 NetLogEventPhase phase, 273 NetLogEventPhase phase,
402 const NetLog::ParametersCallback* parameters_callback) { 274 const NetLogParametersCallback* parameters_callback) {
403 if (!IsCapturing()) 275 if (!IsCapturing())
404 return; 276 return;
405 EntryData entry_data(type, source, phase, base::TimeTicks::Now(), 277 NetLogEntryData entry_data(type, source, phase, base::TimeTicks::Now(),
406 parameters_callback); 278 parameters_callback);
407 279
408 // Notify all of the log observers. 280 // Notify all of the log observers.
409 base::AutoLock lock(lock_); 281 base::AutoLock lock(lock_);
410 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data)); 282 FOR_EACH_OBSERVER(ThreadSafeObserver, observers_, OnAddEntryData(entry_data));
411 } 283 }
412 284
413 NetLogWithSource::~NetLogWithSource() {
414 liveness_ = DEAD;
415 }
416
417 void NetLogWithSource::AddEntry(NetLogEventType type,
418 NetLogEventPhase phase) const {
419 CrashIfInvalid();
420
421 if (!net_log_)
422 return;
423 net_log_->AddEntry(type, source_, phase, NULL);
424 }
425
426 void NetLogWithSource::AddEntry(
427 NetLogEventType type,
428 NetLogEventPhase phase,
429 const NetLog::ParametersCallback& get_parameters) const {
430 CrashIfInvalid();
431
432 if (!net_log_)
433 return;
434 net_log_->AddEntry(type, source_, phase, &get_parameters);
435 }
436
437 void NetLogWithSource::AddEvent(NetLogEventType type) const {
438 AddEntry(type, NetLogEventPhase::NONE);
439 }
440
441 void NetLogWithSource::AddEvent(
442 NetLogEventType type,
443 const NetLog::ParametersCallback& get_parameters) const {
444 AddEntry(type, NetLogEventPhase::NONE, get_parameters);
445 }
446
447 void NetLogWithSource::BeginEvent(NetLogEventType type) const {
448 AddEntry(type, NetLogEventPhase::BEGIN);
449 }
450
451 void NetLogWithSource::BeginEvent(
452 NetLogEventType type,
453 const NetLog::ParametersCallback& get_parameters) const {
454 AddEntry(type, NetLogEventPhase::BEGIN, get_parameters);
455 }
456
457 void NetLogWithSource::EndEvent(NetLogEventType type) const {
458 AddEntry(type, NetLogEventPhase::END);
459 }
460
461 void NetLogWithSource::EndEvent(
462 NetLogEventType type,
463 const NetLog::ParametersCallback& get_parameters) const {
464 AddEntry(type, NetLogEventPhase::END, get_parameters);
465 }
466
467 void NetLogWithSource::AddEventWithNetErrorCode(NetLogEventType event_type,
468 int net_error) const {
469 DCHECK_NE(ERR_IO_PENDING, net_error);
470 if (net_error >= 0) {
471 AddEvent(event_type);
472 } else {
473 AddEvent(event_type, NetLog::IntCallback("net_error", net_error));
474 }
475 }
476
477 void NetLogWithSource::EndEventWithNetErrorCode(NetLogEventType event_type,
478 int net_error) const {
479 DCHECK_NE(ERR_IO_PENDING, net_error);
480 if (net_error >= 0) {
481 EndEvent(event_type);
482 } else {
483 EndEvent(event_type, NetLog::IntCallback("net_error", net_error));
484 }
485 }
486
487 void NetLogWithSource::AddByteTransferEvent(NetLogEventType event_type,
488 int byte_count,
489 const char* bytes) const {
490 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes));
491 }
492
493 bool NetLogWithSource::IsCapturing() const {
494 CrashIfInvalid();
495 return net_log_ && net_log_->IsCapturing();
496 }
497
498 // static
499 NetLogWithSource NetLogWithSource::Make(NetLog* net_log,
500 NetLogSourceType source_type) {
501 if (!net_log)
502 return NetLogWithSource();
503
504 NetLog::Source source(source_type, net_log->NextID());
505 return NetLogWithSource(source, net_log);
506 }
507
508 void NetLogWithSource::CrashIfInvalid() const {
509 Liveness liveness = liveness_;
510
511 if (liveness == ALIVE)
512 return;
513
514 base::debug::Alias(&liveness);
515 CHECK_EQ(ALIVE, liveness);
516 }
517
518 } // namespace net 285 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698