mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 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" |
Peter Kasting | 383640f5 | 2018-02-13 06:22:40 | [diff] [blame] | 11 | #include "base/callback.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 12 | #include "base/debug/alias.h" |
| 13 | #include "base/logging.h" |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 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 |
Eric Roman | 0d5fcb56a | 2018-12-15 04:46:55 | [diff] [blame] | 25 | // contents and |byte_count| > 0, then will include the actual bytes. |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 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) |
Eric Roman | 0d5fcb56a | 2018-12-15 04:46:55 | [diff] [blame] | 33 | dict->SetKey("bytes", NetLogBinaryValue(bytes, byte_count)); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 34 | return std::move(dict); |
| 35 | } |
| 36 | |
| 37 | } // namespace |
| 38 | |
| 39 | NetLogWithSource::~NetLogWithSource() { |
| 40 | liveness_ = DEAD; |
| 41 | } |
| 42 | |
| 43 | void NetLogWithSource::AddEntry(NetLogEventType type, |
| 44 | NetLogEventPhase phase) const { |
| 45 | CrashIfInvalid(); |
| 46 | |
| 47 | if (!net_log_) |
| 48 | return; |
Raul Tambre | 94493c65 | 2019-03-11 17:18:35 | [diff] [blame] | 49 | net_log_->AddEntry(type, source_, phase, nullptr); |
mikecirone | f22f981 | 2016-10-04 03:40:19 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | void NetLogWithSource::AddEntry( |
| 53 | NetLogEventType type, |
| 54 | NetLogEventPhase phase, |
| 55 | const NetLogParametersCallback& get_parameters) const { |
| 56 | CrashIfInvalid(); |
| 57 | |
| 58 | if (!net_log_) |
| 59 | return; |
| 60 | net_log_->AddEntry(type, source_, phase, &get_parameters); |
| 61 | } |
| 62 | |
| 63 | void NetLogWithSource::AddEvent(NetLogEventType type) const { |
| 64 | AddEntry(type, NetLogEventPhase::NONE); |
| 65 | } |
| 66 | |
| 67 | void NetLogWithSource::AddEvent( |
| 68 | NetLogEventType type, |
| 69 | const NetLogParametersCallback& get_parameters) const { |
| 70 | AddEntry(type, NetLogEventPhase::NONE, get_parameters); |
| 71 | } |
| 72 | |
| 73 | void NetLogWithSource::BeginEvent(NetLogEventType type) const { |
| 74 | AddEntry(type, NetLogEventPhase::BEGIN); |
| 75 | } |
| 76 | |
| 77 | void NetLogWithSource::BeginEvent( |
| 78 | NetLogEventType type, |
| 79 | const NetLogParametersCallback& get_parameters) const { |
| 80 | AddEntry(type, NetLogEventPhase::BEGIN, get_parameters); |
| 81 | } |
| 82 | |
| 83 | void NetLogWithSource::EndEvent(NetLogEventType type) const { |
| 84 | AddEntry(type, NetLogEventPhase::END); |
| 85 | } |
| 86 | |
| 87 | void NetLogWithSource::EndEvent( |
| 88 | NetLogEventType type, |
| 89 | const NetLogParametersCallback& get_parameters) const { |
| 90 | AddEntry(type, NetLogEventPhase::END, get_parameters); |
| 91 | } |
| 92 | |
| 93 | void NetLogWithSource::AddEventWithNetErrorCode(NetLogEventType event_type, |
| 94 | int net_error) const { |
| 95 | DCHECK_NE(ERR_IO_PENDING, net_error); |
| 96 | if (net_error >= 0) { |
| 97 | AddEvent(event_type); |
| 98 | } else { |
| 99 | AddEvent(event_type, NetLog::IntCallback("net_error", net_error)); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | void NetLogWithSource::EndEventWithNetErrorCode(NetLogEventType event_type, |
| 104 | int net_error) const { |
| 105 | DCHECK_NE(ERR_IO_PENDING, net_error); |
| 106 | if (net_error >= 0) { |
| 107 | EndEvent(event_type); |
| 108 | } else { |
| 109 | EndEvent(event_type, NetLog::IntCallback("net_error", net_error)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | void NetLogWithSource::AddByteTransferEvent(NetLogEventType event_type, |
| 114 | int byte_count, |
| 115 | const char* bytes) const { |
| 116 | AddEvent(event_type, base::Bind(BytesTransferredCallback, byte_count, bytes)); |
| 117 | } |
| 118 | |
| 119 | bool NetLogWithSource::IsCapturing() const { |
| 120 | CrashIfInvalid(); |
| 121 | return net_log_ && net_log_->IsCapturing(); |
| 122 | } |
| 123 | |
| 124 | // static |
| 125 | NetLogWithSource NetLogWithSource::Make(NetLog* net_log, |
| 126 | NetLogSourceType source_type) { |
| 127 | if (!net_log) |
| 128 | return NetLogWithSource(); |
| 129 | |
| 130 | NetLogSource source(source_type, net_log->NextID()); |
| 131 | return NetLogWithSource(source, net_log); |
| 132 | } |
| 133 | |
| 134 | void NetLogWithSource::CrashIfInvalid() const { |
| 135 | Liveness liveness = liveness_; |
| 136 | |
| 137 | if (liveness == ALIVE) |
| 138 | return; |
| 139 | |
| 140 | base::debug::Alias(&liveness); |
| 141 | CHECK_EQ(ALIVE, liveness); |
| 142 | } |
| 143 | |
| 144 | } // namespace net |