forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_log.cc
299 lines (249 loc) · 8.41 KB
/
net_log.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "net/log/net_log.h"
#include <algorithm>
#include <utility>
#include "base/bind.h"
#include "base/logging.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
namespace net {
namespace {
std::unique_ptr<base::Value> NetLogBoolCallback(
const char* name,
bool value,
NetLogCaptureMode /* capture_mode */) {
std::unique_ptr<base::DictionaryValue> event_params(
new base::DictionaryValue());
event_params->SetBoolean(name, value);
return std::move(event_params);
}
std::unique_ptr<base::Value> NetLogIntCallback(
const char* name,
int value,
NetLogCaptureMode /* capture_mode */) {
std::unique_ptr<base::DictionaryValue> event_params(
new base::DictionaryValue());
event_params->SetInteger(name, value);
return std::move(event_params);
}
std::unique_ptr<base::Value> NetLogInt64Callback(
const char* name,
int64_t value,
NetLogCaptureMode /* capture_mode */) {
std::unique_ptr<base::DictionaryValue> event_params(
new base::DictionaryValue());
event_params->SetString(name, base::Int64ToString(value));
return std::move(event_params);
}
std::unique_ptr<base::Value> NetLogStringCallback(
const char* name,
const std::string* value,
NetLogCaptureMode /* capture_mode */) {
std::unique_ptr<base::DictionaryValue> event_params(
new base::DictionaryValue());
event_params->SetString(name, *value);
return std::move(event_params);
}
std::unique_ptr<base::Value> NetLogCharStringCallback(
const char* name,
const char* value,
NetLogCaptureMode /* capture_mode */) {
std::unique_ptr<base::DictionaryValue> event_params(
new base::DictionaryValue());
event_params->SetString(name, value);
return std::move(event_params);
}
std::unique_ptr<base::Value> NetLogString16Callback(
const char* name,
const base::string16* value,
NetLogCaptureMode /* capture_mode */) {
std::unique_ptr<base::DictionaryValue> event_params(
new base::DictionaryValue());
event_params->SetString(name, *value);
return std::move(event_params);
}
} // namespace
NetLog::ThreadSafeObserver::ThreadSafeObserver() : net_log_(NULL) {
}
NetLog::ThreadSafeObserver::~ThreadSafeObserver() {
// Make sure we aren't watching a NetLog on destruction. Because the NetLog
// may pass events to each observer on multiple threads, we cannot safely
// stop watching a NetLog automatically from a parent class.
DCHECK(!net_log_);
}
NetLogCaptureMode NetLog::ThreadSafeObserver::capture_mode() const {
DCHECK(net_log_);
return capture_mode_;
}
NetLog* NetLog::ThreadSafeObserver::net_log() const {
return net_log_;
}
void NetLog::ThreadSafeObserver::OnAddEntryData(
const NetLogEntryData& entry_data) {
OnAddEntry(NetLogEntry(&entry_data, capture_mode()));
}
NetLog::NetLog() : last_id_(0), is_capturing_(0) {
}
NetLog::~NetLog() {
}
void NetLog::AddGlobalEntry(NetLogEventType type) {
AddEntry(type, NetLogSource(NetLogSourceType::NONE, NextID()),
NetLogEventPhase::NONE, NULL);
}
void NetLog::AddGlobalEntry(
NetLogEventType type,
const NetLogParametersCallback& parameters_callback) {
AddEntry(type, NetLogSource(NetLogSourceType::NONE, NextID()),
NetLogEventPhase::NONE, ¶meters_callback);
}
uint32_t NetLog::NextID() {
return base::subtle::NoBarrier_AtomicIncrement(&last_id_, 1);
}
bool NetLog::IsCapturing() const {
return base::subtle::NoBarrier_Load(&is_capturing_) != 0;
}
void NetLog::DeprecatedAddObserver(NetLog::ThreadSafeObserver* observer,
NetLogCaptureMode capture_mode) {
base::AutoLock lock(lock_);
DCHECK(!observer->net_log_);
DCHECK(!HasObserver(observer));
DCHECK_LT(observers_.size(), 20u); // Performance sanity check.
observers_.push_back(observer);
observer->net_log_ = this;
observer->capture_mode_ = capture_mode;
UpdateIsCapturing();
}
void NetLog::SetObserverCaptureMode(NetLog::ThreadSafeObserver* observer,
NetLogCaptureMode capture_mode) {
base::AutoLock lock(lock_);
DCHECK(HasObserver(observer));
DCHECK_EQ(this, observer->net_log_);
observer->capture_mode_ = capture_mode;
}
void NetLog::DeprecatedRemoveObserver(NetLog::ThreadSafeObserver* observer) {
base::AutoLock lock(lock_);
DCHECK_EQ(this, observer->net_log_);
auto it = std::find(observers_.begin(), observers_.end(), observer);
DCHECK(it != observers_.end());
observers_.erase(it);
observer->net_log_ = NULL;
observer->capture_mode_ = NetLogCaptureMode();
UpdateIsCapturing();
}
void NetLog::UpdateIsCapturing() {
lock_.AssertAcquired();
base::subtle::NoBarrier_Store(&is_capturing_, observers_.size() ? 1 : 0);
}
bool NetLog::HasObserver(ThreadSafeObserver* observer) {
lock_.AssertAcquired();
auto it = std::find(observers_.begin(), observers_.end(), observer);
return it != observers_.end();
}
// static
std::string NetLog::TickCountToString(const base::TimeTicks& time) {
int64_t delta_time = (time - base::TimeTicks()).InMilliseconds();
return base::Int64ToString(delta_time);
}
// static
const char* NetLog::EventTypeToString(NetLogEventType event) {
switch (event) {
#define EVENT_TYPE(label) \
case NetLogEventType::label: \
return #label;
#include "net/log/net_log_event_type_list.h"
#undef EVENT_TYPE
default:
NOTREACHED();
return NULL;
}
}
// static
base::Value* NetLog::GetEventTypesAsValue() {
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
for (int i = 0; i < static_cast<int>(NetLogEventType::COUNT); ++i) {
dict->SetInteger(EventTypeToString(static_cast<NetLogEventType>(i)), i);
}
return dict.release();
}
// static
const char* NetLog::SourceTypeToString(NetLogSourceType source) {
switch (source) {
#define SOURCE_TYPE(label) \
case NetLogSourceType::label: \
return #label;
#include "net/log/net_log_source_type_list.h"
#undef SOURCE_TYPE
default:
NOTREACHED();
return NULL;
}
}
// static
base::Value* NetLog::GetSourceTypesAsValue() {
std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue());
for (int i = 0; i < static_cast<int>(NetLogSourceType::COUNT); ++i) {
dict->SetInteger(SourceTypeToString(static_cast<NetLogSourceType>(i)), i);
}
return dict.release();
}
// static
const char* NetLog::EventPhaseToString(NetLogEventPhase phase) {
switch (phase) {
case NetLogEventPhase::BEGIN:
return "PHASE_BEGIN";
case NetLogEventPhase::END:
return "PHASE_END";
case NetLogEventPhase::NONE:
return "PHASE_NONE";
}
NOTREACHED();
return NULL;
}
// static
NetLogParametersCallback NetLog::BoolCallback(const char* name, bool value) {
return base::Bind(&NetLogBoolCallback, name, value);
}
// static
NetLogParametersCallback NetLog::IntCallback(const char* name, int value) {
return base::Bind(&NetLogIntCallback, name, value);
}
// static
NetLogParametersCallback NetLog::Int64Callback(const char* name,
int64_t value) {
return base::Bind(&NetLogInt64Callback, name, value);
}
// static
NetLogParametersCallback NetLog::StringCallback(const char* name,
const std::string* value) {
DCHECK(value);
return base::Bind(&NetLogStringCallback, name, value);
}
// static
NetLogParametersCallback NetLog::StringCallback(const char* name,
const char* value) {
DCHECK(value);
return base::Bind(&NetLogCharStringCallback, name, value);
}
// static
NetLogParametersCallback NetLog::StringCallback(const char* name,
const base::string16* value) {
DCHECK(value);
return base::Bind(&NetLogString16Callback, name, value);
}
void NetLog::AddEntry(NetLogEventType type,
const NetLogSource& source,
NetLogEventPhase phase,
const NetLogParametersCallback* parameters_callback) {
if (!IsCapturing())
return;
NetLogEntryData entry_data(type, source, phase, base::TimeTicks::Now(),
parameters_callback);
// Notify all of the log observers.
base::AutoLock lock(lock_);
for (auto* observer : observers_)
observer->OnAddEntryData(entry_data);
}
} // namespace net