• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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/check_op.h"
11 #include "base/no_destructor.h"
12 #include "base/values.h"
13 #include "net/base/net_errors.h"
14 #include "net/log/net_log.h"
15 #include "net/log/net_log_capture_mode.h"
16 #include "net/log/net_log_values.h"
17 
18 namespace net {
19 
20 namespace {
21 
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.
BytesTransferredParams(int byte_count,const char * bytes,NetLogCaptureMode capture_mode)25 base::Value::Dict BytesTransferredParams(int byte_count,
26                                          const char* bytes,
27                                          NetLogCaptureMode capture_mode) {
28   base::Value::Dict dict;
29   dict.Set("byte_count", byte_count);
30   if (NetLogCaptureIncludesSocketBytes(capture_mode) && byte_count > 0)
31     dict.Set("bytes", NetLogBinaryValue(bytes, byte_count));
32   return dict;
33 }
34 
35 }  // namespace
36 
NetLogWithSource()37 NetLogWithSource::NetLogWithSource() {
38   // Conceptually, default NetLogWithSource have no NetLog*, and will return
39   // nullptr when calling |net_log()|. However for performance reasons, we
40   // always store a non-null member to the NetLog in order to avoid needing
41   // null checks for critical codepaths.
42   //
43   // The "dummy" net log used here will always return false for IsCapturing(),
44   // and have no sideffects should its method be called. In practice the only
45   // method that will get called on it is IsCapturing().
46   static base::NoDestructor<NetLog> dummy{base::PassKey<NetLogWithSource>()};
47   DCHECK(!dummy->IsCapturing());
48   non_null_net_log_ = dummy.get();
49 }
50 
AddEntry(NetLogEventType type,NetLogEventPhase phase) const51 void NetLogWithSource::AddEntry(NetLogEventType type,
52                                 NetLogEventPhase phase) const {
53   non_null_net_log_->AddEntry(type, source_, phase);
54 }
55 
AddEvent(NetLogEventType type) const56 void NetLogWithSource::AddEvent(NetLogEventType type) const {
57   AddEntry(type, NetLogEventPhase::NONE);
58 }
59 
AddEventWithStringParams(NetLogEventType type,std::string_view name,std::string_view value) const60 void NetLogWithSource::AddEventWithStringParams(NetLogEventType type,
61                                                 std::string_view name,
62                                                 std::string_view value) const {
63   AddEvent(type, [&] { return NetLogParamsWithString(name, value); });
64 }
65 
AddEventWithIntParams(NetLogEventType type,std::string_view name,int value) const66 void NetLogWithSource::AddEventWithIntParams(NetLogEventType type,
67                                              std::string_view name,
68                                              int value) const {
69   AddEvent(type, [&] { return NetLogParamsWithInt(name, value); });
70 }
71 
BeginEventWithIntParams(NetLogEventType type,std::string_view name,int value) const72 void NetLogWithSource::BeginEventWithIntParams(NetLogEventType type,
73                                                std::string_view name,
74                                                int value) const {
75   BeginEvent(type, [&] { return NetLogParamsWithInt(name, value); });
76 }
77 
EndEventWithIntParams(NetLogEventType type,std::string_view name,int value) const78 void NetLogWithSource::EndEventWithIntParams(NetLogEventType type,
79                                              std::string_view name,
80                                              int value) const {
81   EndEvent(type, [&] { return NetLogParamsWithInt(name, value); });
82 }
83 
AddEventWithInt64Params(NetLogEventType type,std::string_view name,int64_t value) const84 void NetLogWithSource::AddEventWithInt64Params(NetLogEventType type,
85                                                std::string_view name,
86                                                int64_t value) const {
87   AddEvent(type, [&] { return NetLogParamsWithInt64(name, value); });
88 }
89 
BeginEventWithStringParams(NetLogEventType type,std::string_view name,std::string_view value) const90 void NetLogWithSource::BeginEventWithStringParams(
91     NetLogEventType type,
92     std::string_view name,
93     std::string_view value) const {
94   BeginEvent(type, [&] { return NetLogParamsWithString(name, value); });
95 }
96 
AddEventReferencingSource(NetLogEventType type,const NetLogSource & source) const97 void NetLogWithSource::AddEventReferencingSource(
98     NetLogEventType type,
99     const NetLogSource& source) const {
100   AddEvent(type, [&] { return source.ToEventParameters(); });
101 }
102 
BeginEventReferencingSource(NetLogEventType type,const NetLogSource & source) const103 void NetLogWithSource::BeginEventReferencingSource(
104     NetLogEventType type,
105     const NetLogSource& source) const {
106   BeginEvent(type, [&] { return source.ToEventParameters(); });
107 }
108 
BeginEvent(NetLogEventType type) const109 void NetLogWithSource::BeginEvent(NetLogEventType type) const {
110   AddEntry(type, NetLogEventPhase::BEGIN);
111 }
112 
EndEvent(NetLogEventType type) const113 void NetLogWithSource::EndEvent(NetLogEventType type) const {
114   AddEntry(type, NetLogEventPhase::END);
115 }
116 
AddEventWithNetErrorCode(NetLogEventType event_type,int net_error) const117 void NetLogWithSource::AddEventWithNetErrorCode(NetLogEventType event_type,
118                                                 int net_error) const {
119   DCHECK_NE(ERR_IO_PENDING, net_error);
120   if (net_error >= 0) {
121     AddEvent(event_type);
122   } else {
123     AddEventWithIntParams(event_type, "net_error", net_error);
124   }
125 }
126 
EndEventWithNetErrorCode(NetLogEventType event_type,int net_error) const127 void NetLogWithSource::EndEventWithNetErrorCode(NetLogEventType event_type,
128                                                 int net_error) const {
129   DCHECK_NE(ERR_IO_PENDING, net_error);
130   if (net_error >= 0) {
131     EndEvent(event_type);
132   } else {
133     EndEventWithIntParams(event_type, "net_error", net_error);
134   }
135 }
136 
AddEntryWithBoolParams(NetLogEventType type,NetLogEventPhase phase,std::string_view name,bool value) const137 void NetLogWithSource::AddEntryWithBoolParams(NetLogEventType type,
138                                               NetLogEventPhase phase,
139                                               std::string_view name,
140                                               bool value) const {
141   AddEntry(type, phase, [&] { return NetLogParamsWithBool(name, value); });
142 }
143 
AddByteTransferEvent(NetLogEventType event_type,int byte_count,const char * bytes) const144 void NetLogWithSource::AddByteTransferEvent(NetLogEventType event_type,
145                                             int byte_count,
146                                             const char* bytes) const {
147   AddEvent(event_type, [&](NetLogCaptureMode capture_mode) {
148     return BytesTransferredParams(byte_count, bytes, capture_mode);
149   });
150 }
151 
152 // static
Make(NetLog * net_log,NetLogSourceType source_type)153 NetLogWithSource NetLogWithSource::Make(NetLog* net_log,
154                                         NetLogSourceType source_type) {
155   if (!net_log)
156     return NetLogWithSource();
157 
158   NetLogSource source(source_type, net_log->NextID());
159   return NetLogWithSource(source, net_log);
160 }
161 
162 // static
Make(NetLogSourceType source_type)163 NetLogWithSource NetLogWithSource::Make(NetLogSourceType source_type) {
164   return NetLogWithSource::Make(NetLog::Get(), source_type);
165 }
166 
167 // static
Make(NetLog * net_log,const NetLogSource & source)168 NetLogWithSource NetLogWithSource::Make(NetLog* net_log,
169                                         const NetLogSource& source) {
170   if (!net_log || !source.IsValid())
171     return NetLogWithSource();
172   return NetLogWithSource(source, net_log);
173 }
174 
175 // static
Make(const NetLogSource & source)176 NetLogWithSource NetLogWithSource::Make(const NetLogSource& source) {
177   return NetLogWithSource::Make(NetLog::Get(), source);
178 }
179 
net_log() const180 NetLog* NetLogWithSource::net_log() const {
181   if (source_.IsValid())
182     return non_null_net_log_;
183   return nullptr;
184 }
185 
186 }  // namespace net
187