Chromium Code Reviews| Index: net/base/trace_net_log_observer.cc |
| diff --git a/net/base/trace_net_log_observer.cc b/net/base/trace_net_log_observer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d247c705dc63c9c5d897c2123c8079928de7c7d1 |
| --- /dev/null |
| +++ b/net/base/trace_net_log_observer.cc |
| @@ -0,0 +1,105 @@ |
| +// Copyright 2014 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/base/trace_net_log_observer.h" |
| + |
| +#include <stdio.h> |
| +#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!
|
| + |
| +#include "base/debug/trace_event.h" |
| +#include "base/json/json_writer.h" |
| +#include "base/logging.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/values.h" |
| +#include "net/base/net_log.h" |
| + |
| +namespace net { |
| + |
| +namespace { |
| + |
| +class TracedValue : public base::debug::ConvertableToTraceFormat { |
| + public: |
| + explicit TracedValue(scoped_ptr<base::Value> value) : value_(value.Pass()) {} |
| + |
| + private: |
| + virtual ~TracedValue() {} |
| + |
| + virtual void AppendAsTraceFormat(std::string* out) const OVERRIDE { |
| + if (value_) { |
| + std::string tmp; |
| + base::JSONWriter::Write(value_.get(), &tmp); |
| + *out += tmp; |
| + } else { |
| + *out += "\"\""; |
| + } |
| + } |
| + |
| + private: |
| + scoped_ptr<base::Value> value_; |
| +}; |
| + |
| +} // namespace |
| + |
| +TraceNetLogObserver::TraceNetLogObserver() : net_log_to_watch_(NULL) { |
| +} |
| + |
| +TraceNetLogObserver::~TraceNetLogObserver() { |
| + DCHECK(!net_log_to_watch_); |
| + DCHECK(!net_log()); |
| +} |
| + |
| +void TraceNetLogObserver::OnAddEntry(const NetLog::Entry& entry) { |
| + scoped_ptr<base::Value> params(entry.ParametersToValue()); |
| + switch (entry.phase()) { |
| + case NetLog::PHASE_BEGIN: |
| + TRACE_EVENT_NET_LOG_BEGIN( |
| + "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, |
| + "source_type", NetLog::SourceTypeToString(entry.source().type), |
| + "params", scoped_refptr<base::debug::ConvertableToTraceFormat>( |
| + new TracedValue(params.Pass()))); |
| + break; |
| + case NetLog::PHASE_END: |
| + TRACE_EVENT_NET_LOG_END( |
| + "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, |
| + "source_type", NetLog::SourceTypeToString(entry.source().type), |
| + "params", scoped_refptr<base::debug::ConvertableToTraceFormat>( |
| + new TracedValue(params.Pass()))); |
| + break; |
| + case NetLog::PHASE_NONE: |
| + TRACE_EVENT_NET_LOG_INSTANT( |
| + "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, |
| + "source_type", NetLog::SourceTypeToString(entry.source().type), |
| + "params", scoped_refptr<base::debug::ConvertableToTraceFormat>( |
| + new TracedValue(params.Pass()))); |
| + break; |
| + default: |
| + 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
|
| + } |
| +} |
| + |
| +void TraceNetLogObserver::WatchForTraceStart(NetLog* net_log) { |
| + net_log_to_watch_ = net_log; |
| + base::debug::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| +} |
| + |
| +void TraceNetLogObserver::StopWatchForTraceStart() { |
| + // Should only stop if is currently watching. |
| + DCHECK(net_log_to_watch_); |
| + base::debug::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| + if (net_log()) |
| + net_log()->RemoveThreadSafeObserver(this); |
| + net_log_to_watch_ = NULL; |
| +} |
| + |
| +void TraceNetLogObserver::OnTraceLogEnabled() { |
| + net_log_to_watch_->AddThreadSafeObserver(this, |
| + NetLog::LOG_STRIP_PRIVATE_DATA); |
| +} |
| + |
| +void TraceNetLogObserver::OnTraceLogDisabled() { |
| + if (net_log()) |
| + net_log()->RemoveThreadSafeObserver(this); |
| +} |
| + |
| +} // namespace net |