blob: 8997f64c762f8fd64d2546502b180154657bc086 [file] [log] [blame]
mikecironef22f9812016-10-04 03:40:191// 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 Kasting383640f52018-02-13 06:22:4011#include "base/callback.h"
mikecironef22f9812016-10-04 03:40:1912#include "base/debug/alias.h"
13#include "base/logging.h"
mikecironef22f9812016-10-04 03:40:1914#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
19namespace net {
20
21namespace {
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 Roman0d5fcb56a2018-12-15 04:46:5525// contents and |byte_count| > 0, then will include the actual bytes.
mikecironef22f9812016-10-04 03:40:1926std::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 Roman0d5fcb56a2018-12-15 04:46:5533 dict->SetKey("bytes", NetLogBinaryValue(bytes, byte_count));
mikecironef22f9812016-10-04 03:40:1934 return std::move(dict);
35}
36
37} // namespace
38
39NetLogWithSource::~NetLogWithSource() {
40 liveness_ = DEAD;
41}
42
43void NetLogWithSource::AddEntry(NetLogEventType type,
44 NetLogEventPhase phase) const {
45 CrashIfInvalid();
46
47 if (!net_log_)
48 return;
Raul Tambre94493c652019-03-11 17:18:3549 net_log_->AddEntry(type, source_, phase, nullptr);
mikecironef22f9812016-10-04 03:40:1950}
51
52void 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
63void NetLogWithSource::AddEvent(NetLogEventType type) const {
64 AddEntry(type, NetLogEventPhase::NONE);
65}
66
67void NetLogWithSource::AddEvent(
68 NetLogEventType type,
69 const NetLogParametersCallback& get_parameters) const {
70 AddEntry(type, NetLogEventPhase::NONE, get_parameters);
71}
72
73void NetLogWithSource::BeginEvent(NetLogEventType type) const {
74 AddEntry(type, NetLogEventPhase::BEGIN);
75}
76
77void NetLogWithSource::BeginEvent(
78 NetLogEventType type,
79 const NetLogParametersCallback& get_parameters) const {
80 AddEntry(type, NetLogEventPhase::BEGIN, get_parameters);
81}
82
83void NetLogWithSource::EndEvent(NetLogEventType type) const {
84 AddEntry(type, NetLogEventPhase::END);
85}
86
87void NetLogWithSource::EndEvent(
88 NetLogEventType type,
89 const NetLogParametersCallback& get_parameters) const {
90 AddEntry(type, NetLogEventPhase::END, get_parameters);
91}
92
93void 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
103void 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
113void 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
119bool NetLogWithSource::IsCapturing() const {
120 CrashIfInvalid();
121 return net_log_ && net_log_->IsCapturing();
122}
123
124// static
125NetLogWithSource 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
134void 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