OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/log/trace_net_log_observer.h" | 5 #include "net/log/trace_net_log_observer.h" |
6 | 6 |
7 #include <stdio.h> | 7 #include <stdio.h> |
| 8 |
| 9 #include <memory> |
8 #include <string> | 10 #include <string> |
9 #include <utility> | 11 #include <utility> |
10 | 12 |
11 #include "base/json/json_writer.h" | 13 #include "base/json/json_writer.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/memory/scoped_ptr.h" | |
14 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
15 #include "base/values.h" | 16 #include "base/values.h" |
16 #include "net/log/net_log.h" | 17 #include "net/log/net_log.h" |
17 | 18 |
18 namespace net { | 19 namespace net { |
19 | 20 |
20 namespace { | 21 namespace { |
21 | 22 |
22 // TraceLog category for NetLog events. | 23 // TraceLog category for NetLog events. |
23 const char kNetLogTracingCategory[] = "netlog"; | 24 const char kNetLogTracingCategory[] = "netlog"; |
24 | 25 |
25 class TracedValue : public base::trace_event::ConvertableToTraceFormat { | 26 class TracedValue : public base::trace_event::ConvertableToTraceFormat { |
26 public: | 27 public: |
27 explicit TracedValue(scoped_ptr<base::Value> value) | 28 explicit TracedValue(std::unique_ptr<base::Value> value) |
28 : value_(std::move(value)) {} | 29 : value_(std::move(value)) {} |
29 | 30 |
30 private: | 31 private: |
31 ~TracedValue() override {} | 32 ~TracedValue() override {} |
32 | 33 |
33 void AppendAsTraceFormat(std::string* out) const override { | 34 void AppendAsTraceFormat(std::string* out) const override { |
34 if (value_) { | 35 if (value_) { |
35 std::string tmp; | 36 std::string tmp; |
36 base::JSONWriter::Write(*value_, &tmp); | 37 base::JSONWriter::Write(*value_, &tmp); |
37 *out += tmp; | 38 *out += tmp; |
38 } else { | 39 } else { |
39 *out += "\"\""; | 40 *out += "\"\""; |
40 } | 41 } |
41 } | 42 } |
42 | 43 |
43 private: | 44 private: |
44 scoped_ptr<base::Value> value_; | 45 std::unique_ptr<base::Value> value_; |
45 }; | 46 }; |
46 | 47 |
47 } // namespace | 48 } // namespace |
48 | 49 |
49 TraceNetLogObserver::TraceNetLogObserver() : net_log_to_watch_(NULL) { | 50 TraceNetLogObserver::TraceNetLogObserver() : net_log_to_watch_(NULL) { |
50 } | 51 } |
51 | 52 |
52 TraceNetLogObserver::~TraceNetLogObserver() { | 53 TraceNetLogObserver::~TraceNetLogObserver() { |
53 DCHECK(!net_log_to_watch_); | 54 DCHECK(!net_log_to_watch_); |
54 DCHECK(!net_log()); | 55 DCHECK(!net_log()); |
55 } | 56 } |
56 | 57 |
57 void TraceNetLogObserver::OnAddEntry(const NetLog::Entry& entry) { | 58 void TraceNetLogObserver::OnAddEntry(const NetLog::Entry& entry) { |
58 scoped_ptr<base::Value> params(entry.ParametersToValue()); | 59 std::unique_ptr<base::Value> params(entry.ParametersToValue()); |
59 switch (entry.phase()) { | 60 switch (entry.phase()) { |
60 case NetLog::PHASE_BEGIN: | 61 case NetLog::PHASE_BEGIN: |
61 TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( | 62 TRACE_EVENT_NESTABLE_ASYNC_BEGIN2( |
62 kNetLogTracingCategory, NetLog::EventTypeToString(entry.type()), | 63 kNetLogTracingCategory, NetLog::EventTypeToString(entry.type()), |
63 entry.source().id, "source_type", | 64 entry.source().id, "source_type", |
64 NetLog::SourceTypeToString(entry.source().type), "params", | 65 NetLog::SourceTypeToString(entry.source().type), "params", |
65 scoped_ptr<base::trace_event::ConvertableToTraceFormat>( | 66 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>( |
66 new TracedValue(std::move(params)))); | 67 new TracedValue(std::move(params)))); |
67 break; | 68 break; |
68 case NetLog::PHASE_END: | 69 case NetLog::PHASE_END: |
69 TRACE_EVENT_NESTABLE_ASYNC_END2( | 70 TRACE_EVENT_NESTABLE_ASYNC_END2( |
70 kNetLogTracingCategory, NetLog::EventTypeToString(entry.type()), | 71 kNetLogTracingCategory, NetLog::EventTypeToString(entry.type()), |
71 entry.source().id, "source_type", | 72 entry.source().id, "source_type", |
72 NetLog::SourceTypeToString(entry.source().type), "params", | 73 NetLog::SourceTypeToString(entry.source().type), "params", |
73 scoped_ptr<base::trace_event::ConvertableToTraceFormat>( | 74 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>( |
74 new TracedValue(std::move(params)))); | 75 new TracedValue(std::move(params)))); |
75 break; | 76 break; |
76 case NetLog::PHASE_NONE: | 77 case NetLog::PHASE_NONE: |
77 TRACE_EVENT_NESTABLE_ASYNC_INSTANT2( | 78 TRACE_EVENT_NESTABLE_ASYNC_INSTANT2( |
78 kNetLogTracingCategory, NetLog::EventTypeToString(entry.type()), | 79 kNetLogTracingCategory, NetLog::EventTypeToString(entry.type()), |
79 entry.source().id, "source_type", | 80 entry.source().id, "source_type", |
80 NetLog::SourceTypeToString(entry.source().type), "params", | 81 NetLog::SourceTypeToString(entry.source().type), "params", |
81 scoped_ptr<base::trace_event::ConvertableToTraceFormat>( | 82 std::unique_ptr<base::trace_event::ConvertableToTraceFormat>( |
82 new TracedValue(std::move(params)))); | 83 new TracedValue(std::move(params)))); |
83 break; | 84 break; |
84 } | 85 } |
85 } | 86 } |
86 | 87 |
87 void TraceNetLogObserver::WatchForTraceStart(NetLog* netlog) { | 88 void TraceNetLogObserver::WatchForTraceStart(NetLog* netlog) { |
88 DCHECK(!net_log_to_watch_); | 89 DCHECK(!net_log_to_watch_); |
89 DCHECK(!net_log()); | 90 DCHECK(!net_log()); |
90 net_log_to_watch_ = netlog; | 91 net_log_to_watch_ = netlog; |
91 base::trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); | 92 base::trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
(...skipping 11 matching lines...) Expand all Loading... |
103 void TraceNetLogObserver::OnTraceLogEnabled() { | 104 void TraceNetLogObserver::OnTraceLogEnabled() { |
104 net_log_to_watch_->DeprecatedAddObserver(this, NetLogCaptureMode::Default()); | 105 net_log_to_watch_->DeprecatedAddObserver(this, NetLogCaptureMode::Default()); |
105 } | 106 } |
106 | 107 |
107 void TraceNetLogObserver::OnTraceLogDisabled() { | 108 void TraceNetLogObserver::OnTraceLogDisabled() { |
108 if (net_log()) | 109 if (net_log()) |
109 net_log()->DeprecatedRemoveObserver(this); | 110 net_log()->DeprecatedRemoveObserver(this); |
110 } | 111 } |
111 | 112 |
112 } // namespace net | 113 } // namespace net |
OLD | NEW |