Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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. | |
| 4 | |
| 5 #include "net/base/trace_net_log_observer.h" | |
| 6 | |
| 7 #include <stdio.h> | |
| 8 #include <string> | |
|
mmenke
2014/08/22 18:28:14
nit: blank line between standard C and C++ header
xunjieli
2014/08/22 19:04:39
Done. Thanks!
| |
| 9 | |
| 10 #include "base/debug/trace_event.h" | |
| 11 #include "base/json/json_writer.h" | |
| 12 #include "base/logging.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/values.h" | |
| 15 #include "net/base/net_log.h" | |
| 16 | |
| 17 namespace net { | |
| 18 | |
| 19 namespace { | |
| 20 | |
| 21 class TracedValue : public base::debug::ConvertableToTraceFormat { | |
| 22 public: | |
| 23 explicit TracedValue(scoped_ptr<base::Value> value) : value_(value.Pass()) {} | |
| 24 | |
| 25 private: | |
| 26 virtual ~TracedValue() {} | |
| 27 | |
| 28 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { | |
| 29 if (value_) { | |
| 30 std::string tmp; | |
| 31 base::JSONWriter::Write(value_.get(), &tmp); | |
| 32 *out += tmp; | |
| 33 } else { | |
| 34 *out += "\"\""; | |
| 35 } | |
| 36 } | |
| 37 | |
| 38 private: | |
| 39 scoped_ptr<base::Value> value_; | |
| 40 }; | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 TraceNetLogObserver::TraceNetLogObserver() : net_log_to_watch_(NULL) { | |
| 45 } | |
| 46 | |
| 47 TraceNetLogObserver::~TraceNetLogObserver() { | |
| 48 DCHECK(!net_log_to_watch_); | |
| 49 DCHECK(!net_log()); | |
| 50 } | |
| 51 | |
| 52 void TraceNetLogObserver::OnAddEntry(const NetLog::Entry& entry) { | |
| 53 scoped_ptr<base::Value> params(entry.ParametersToValue()); | |
| 54 switch (entry.phase()) { | |
| 55 case NetLog::PHASE_BEGIN: | |
| 56 TRACE_EVENT_NET_LOG_BEGIN( | |
| 57 "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, | |
| 58 "source_type", NetLog::SourceTypeToString(entry.source().type), | |
| 59 "params", scoped_refptr<base::debug::ConvertableToTraceFormat>( | |
| 60 new TracedValue(params.Pass()))); | |
| 61 break; | |
| 62 case NetLog::PHASE_END: | |
| 63 TRACE_EVENT_NET_LOG_END( | |
| 64 "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, | |
| 65 "source_type", NetLog::SourceTypeToString(entry.source().type), | |
| 66 "params", scoped_refptr<base::debug::ConvertableToTraceFormat>( | |
| 67 new TracedValue(params.Pass()))); | |
| 68 break; | |
| 69 case NetLog::PHASE_NONE: | |
| 70 TRACE_EVENT_NET_LOG_INSTANT( | |
| 71 "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, | |
| 72 "source_type", NetLog::SourceTypeToString(entry.source().type), | |
| 73 "params", scoped_refptr<base::debug::ConvertableToTraceFormat>( | |
| 74 new TracedValue(params.Pass()))); | |
| 75 break; | |
| 76 default: | |
| 77 break; | |
|
mmenke
2014/08/22 18:28:14
No, I meant removing the default case entirely. W
xunjieli
2014/08/22 19:04:39
Done. I see. Thanks!
On 2014/08/22 18:28:14, mmen
| |
| 78 } | |
| 79 } | |
| 80 | |
| 81 void TraceNetLogObserver::WatchForTraceStart(NetLog* net_log) { | |
| 82 net_log_to_watch_ = net_log; | |
| 83 base::debug::TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
| 84 } | |
| 85 | |
| 86 void TraceNetLogObserver::StopWatchForTraceStart() { | |
| 87 // Should only stop if is currently watching. | |
| 88 DCHECK(net_log_to_watch_); | |
| 89 base::debug::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
| 90 if (net_log()) | |
| 91 net_log()->RemoveThreadSafeObserver(this); | |
| 92 net_log_to_watch_ = NULL; | |
| 93 } | |
| 94 | |
| 95 void TraceNetLogObserver::OnTraceLogEnabled() { | |
| 96 net_log_to_watch_->AddThreadSafeObserver(this, | |
| 97 NetLog::LOG_STRIP_PRIVATE_DATA); | |
| 98 } | |
| 99 | |
| 100 void TraceNetLogObserver::OnTraceLogDisabled() { | |
| 101 if (net_log()) | |
| 102 net_log()->RemoveThreadSafeObserver(this); | |
| 103 } | |
| 104 | |
| 105 } // namespace net | |
| OLD | NEW |