OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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. |
| 4 |
| 5 #include "net/log/net_log_with_source.h" |
| 6 |
| 7 #include <memory> |
| 8 #include <utility> |
| 9 |
| 10 #include "base/bind.h" |
| 11 #include "base/debug/alias.h" |
| 12 #include "base/logging.h" |
| 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/values.h" |
| 15 #include "net/base/net_errors.h" |
| 16 #include "net/log/net_log.h" |
| 17 #include "net/log/net_log_capture_mode.h" |
| 18 |
| 19 namespace net { |
| 20 |
| 21 namespace { |
| 22 |
| 23 // Returns parameters for logging data transferred events. At a minimum includes |
| 24 // the number of bytes transferred. If the capture mode allows logging byte |
| 25 // contents and |byte_count| > 0, then will include the actual bytes. The |
| 26 // bytes are hex-encoded, since base::StringValue only supports UTF-8. |
| 27 std::unique_ptr<base::Value> BytesTransferredCallback( |
| 28 int byte_count, |
| 29 const char* bytes, |
| 30 NetLogCaptureMode capture_mode) { |
| 31 std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue()); |
| 32 dict->SetInteger("byte_count", byte_count); |
| 33 if (capture_mode.include_socket_bytes() && byte_count > 0) |
| 34 dict->SetString("hex_encoded_bytes", base::HexEncode(bytes, byte_count)); |
| 35 return std::move(dict); |
| 36 } |
| 37 |
| 38 } // namespace |
| 39 |
| 40 NetLogWithSource::~NetLogWithSource() { |
| 41 liveness_ = DEAD; |
| 42 } |
| 43 |
| 44 void NetLogWithSource::AddEntry(NetLogEventType type, |
| 45 NetLogEventPhase phase) const { |
| 46 CrashIfInvalid(); |
| 47 |
| 48 if (!net_log_) |
| 49 return; |
| 50 net_log_->AddEntry(type, source_, phase, NULL); |
| 51 } |
| 52 |
| 53 void NetLogWithSource::AddEntry( |
| 54 NetLogEventType type, |
| 55 NetLogEventPhase phase, |
| 56 const NetLogParametersCallback& get_parameters) const { |
| 57 CrashIfInvalid(); |
| 58 |
| 59 if (!net_log_) |
| 60 return; |
| 61 net_log_->AddEntry(type, source_, phase, &get_parameters); |
| 62 } |
| 63 |
| 64 void NetLogWithSource::AddEvent(NetLogEventType type) const { |
| 65 AddEntry(type, NetLogEventPhase::NONE); |
| 66 } |
| 67 |
| 68 void NetLogWithSource::AddEvent( |
| 69 NetLogEventType type, |
| 70 const NetLogParametersCallback& get_parameters) const { |
| 71 AddEntry(type, NetLogEventPhase::NONE, get_parameters); |
| 72 } |
| 73 |
| 74 void NetLogWithSource::BeginEvent(NetLogEventType type) const { |
| 75 AddEntry(type, NetLogEventPhase::BEGIN); |
| 76 } |
| 77 |
| 78 void NetLogWithSource::BeginEvent( |
| 79 NetLogEventType type, |
| 80 const NetLogParametersCallback& get_parameters) const { |
| 81 AddEntry(type, NetLogEventPhase::BEGIN, get_parameters); |
| 82 } |
| 83 |
| 84 void NetLogWithSource::EndEvent(NetLogEventType type) const { |
| 85 AddEntry(type, NetLogEventPhase::END); |
| 86 } |
| 87 |
| 88 void NetLogWithSource::EndEvent( |
| 89 NetLogEventType type, |
| 90 const NetLogParametersCallback& get_parameters) const { |
| 91 AddEntry(type, NetLogEventPhase::END, get_parameters); |
| 92 } |
| 93 |
| 94 void NetLogWithSource::AddEventWithNetErrorCode(NetLogEventType event_type, |
| 95 int net_error) const { |
| 96 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 97 if (net_error >= 0) { |
| 98 AddEvent(event_type); |
| 99 } else { |
| 100 AddEvent(event_type, NetLog::IntCallback("net_error", net_error)); |
| 101 } |
| 102 } |
| 103 |
| 104 void NetLogWithSource::EndEventWithNetErrorCode(NetLogEventType event_type, |
| 105 int net_error) const { |
| 106 DCHECK_NE(ERR_IO_PENDING, net_error); |
| 107 if (net_error >= 0) { |
| 108 EndEvent(event_type); |
| 109 } else { |
| 110 EndEvent(event_type, NetLog::IntCallback("net_error", net_error)); |
| 111 } |
| 112 } |
| 113 |
| 114 void NetLogWithSource::AddByteTransferEvent(NetLogEventType event_type, |
| 115 int byte_count, |
| 116 const char* bytes) const { |
| 117 AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes)); |
| 118 } |
| 119 |
| 120 bool NetLogWithSource::IsCapturing() const { |
| 121 CrashIfInvalid(); |
| 122 return net_log_ && net_log_->IsCapturing(); |
| 123 } |
| 124 |
| 125 // static |
| 126 NetLogWithSource NetLogWithSource::Make(NetLog* net_log, |
| 127 NetLogSourceType source_type) { |
| 128 if (!net_log) |
| 129 return NetLogWithSource(); |
| 130 |
| 131 NetLogSource source(source_type, net_log->NextID()); |
| 132 return NetLogWithSource(source, net_log); |
| 133 } |
| 134 |
| 135 void NetLogWithSource::CrashIfInvalid() const { |
| 136 Liveness liveness = liveness_; |
| 137 |
| 138 if (liveness == ALIVE) |
| 139 return; |
| 140 |
| 141 base::debug::Alias(&liveness); |
| 142 CHECK_EQ(ALIVE, liveness); |
| 143 } |
| 144 |
| 145 } // namespace net |
OLD | NEW |