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 | |
| 9 #include <string> | |
| 10 | |
| 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" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 class TracedValue : public base::debug::ConvertableToTraceFormat { | |
| 23 public: | |
| 24 explicit TracedValue(scoped_ptr<base::Value> value) : value_(value.Pass()) {} | |
| 25 | |
| 26 private: | |
| 27 virtual ~TracedValue() {} | |
| 28 | |
| 29 virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { | |
| 30 if (value_) { | |
| 31 std::string tmp; | |
| 32 base::JSONWriter::Write(value_.get(), &tmp); | |
| 33 *out += tmp; | |
| 34 } else { | |
| 35 *out += "\"\""; | |
| 36 } | |
| 37 } | |
| 38 | |
| 39 private: | |
| 40 scoped_ptr<base::Value> value_; | |
| 41 }; | |
| 42 | |
| 43 } // namespace | |
| 44 | |
| 45 TraceNetLogObserver::TraceNetLogObserver() : net_log_to_watch_(NULL) { | |
| 46 } | |
| 47 | |
| 48 TraceNetLogObserver::~TraceNetLogObserver() { | |
| 49 DCHECK(!net_log_to_watch_); | |
| 50 DCHECK(!net_log()); | |
| 51 } | |
| 52 | |
| 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_NET_LOG_BEGIN( | |
| 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()))); | |
| 62 break; | |
| 63 case NetLog::PHASE_END: | |
| 64 TRACE_EVENT_NET_LOG_END( | |
| 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()))); | |
| 69 break; | |
| 70 case NetLog::PHASE_NONE: | |
| 71 TRACE_EVENT_NET_LOG_INSTANT( | |
| 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()))); | |
| 76 break; | |
| 77 } | |
| 78 } | |
| 79 | |
| 80 void TraceNetLogObserver::WatchForTraceStart(NetLog* net_log) { | |
|
mmenke
2014/08/27 19:03:14
DCHECK(!net_log_to_watch_);?
Otherwise, we'd end
mmenke
2014/08/27 19:03:14
DCHECK(!net_log());
xunjieli
2014/09/02 16:37:33
Done.
xunjieli
2014/09/02 16:37:33
Done.
| |
| 81 net_log_to_watch_ = net_log; | |
| 82 base::debug::TraceLog::GetInstance()->AddEnabledStateObserver(this); | |
| 83 } | |
| 84 | |
| 85 void TraceNetLogObserver::StopWatchForTraceStart() { | |
| 86 // Should only stop if is currently watching. | |
| 87 DCHECK(net_log_to_watch_); | |
| 88 base::debug::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
| 89 if (net_log()) | |
| 90 net_log()->RemoveThreadSafeObserver(this); | |
| 91 net_log_to_watch_ = NULL; | |
| 92 } | |
| 93 | |
| 94 void TraceNetLogObserver::OnTraceLogEnabled() { | |
| 95 net_log_to_watch_->AddThreadSafeObserver(this, | |
| 96 NetLog::LOG_STRIP_PRIVATE_DATA); | |
| 97 } | |
| 98 | |
| 99 void TraceNetLogObserver::OnTraceLogDisabled() { | |
| 100 if (net_log()) | |
| 101 net_log()->RemoveThreadSafeObserver(this); | |
| 102 } | |
| 103 | |
| 104 } // namespace net | |
| OLD | NEW |