1 // Copyright 2014 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.
5 #include "net/base/trace_net_log_observer.h"
11 #include "base/debug/trace_event.h"
12 #include "base/json/json_writer.h"
13 #include "base/logging.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/values.h"
16 #include "net/base/net_log.h"
22 class TracedValue
: public base::debug::ConvertableToTraceFormat
{
24 explicit TracedValue(scoped_ptr
<base::Value
> value
) : value_(value
.Pass()) {}
27 virtual ~TracedValue() {}
29 virtual void AppendAsTraceFormat(std::string
* out
) const override
{
32 base::JSONWriter::Write(value_
.get(), &tmp
);
40 scoped_ptr
<base::Value
> value_
;
45 TraceNetLogObserver::TraceNetLogObserver() : net_log_to_watch_(NULL
) {
48 TraceNetLogObserver::~TraceNetLogObserver() {
49 DCHECK(!net_log_to_watch_
);
53 void TraceNetLogObserver::OnAddEntry(const NetLog::Entry
& entry
) {
54 scoped_ptr
<base::Value
> params(entry
.ParametersToValue());
55 switch (entry
.phase()) {
56 case NetLog::PHASE_BEGIN
:
57 TRACE_EVENT_NESTABLE_ASYNC_BEGIN2(
58 "netlog", NetLog::EventTypeToString(entry
.type()), entry
.source().id
,
59 "source_type", NetLog::SourceTypeToString(entry
.source().type
),
60 "params", scoped_refptr
<base::debug::ConvertableToTraceFormat
>(
61 new TracedValue(params
.Pass())));
63 case NetLog::PHASE_END
:
64 TRACE_EVENT_NESTABLE_ASYNC_END2(
65 "netlog", NetLog::EventTypeToString(entry
.type()), entry
.source().id
,
66 "source_type", NetLog::SourceTypeToString(entry
.source().type
),
67 "params", scoped_refptr
<base::debug::ConvertableToTraceFormat
>(
68 new TracedValue(params
.Pass())));
70 case NetLog::PHASE_NONE
:
71 TRACE_EVENT_NESTABLE_ASYNC_INSTANT2(
72 "netlog", NetLog::EventTypeToString(entry
.type()), entry
.source().id
,
73 "source_type", NetLog::SourceTypeToString(entry
.source().type
),
74 "params", scoped_refptr
<base::debug::ConvertableToTraceFormat
>(
75 new TracedValue(params
.Pass())));
80 void TraceNetLogObserver::WatchForTraceStart(NetLog
* netlog
) {
81 DCHECK(!net_log_to_watch_
);
83 net_log_to_watch_
= netlog
;
84 base::debug::TraceLog::GetInstance()->AddEnabledStateObserver(this);
87 void TraceNetLogObserver::StopWatchForTraceStart() {
88 // Should only stop if is currently watching.
89 DCHECK(net_log_to_watch_
);
90 base::debug::TraceLog::GetInstance()->RemoveEnabledStateObserver(this);
92 net_log()->RemoveThreadSafeObserver(this);
93 net_log_to_watch_
= NULL
;
96 void TraceNetLogObserver::OnTraceLogEnabled() {
97 net_log_to_watch_
->AddThreadSafeObserver(this,
98 NetLog::LOG_STRIP_PRIVATE_DATA
);
101 void TraceNetLogObserver::OnTraceLogDisabled() {
103 net_log()->RemoveThreadSafeObserver(this);