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..bd88057816febfd8a5bbfdceba552132ca225454 |
--- /dev/null |
+++ b/net/base/trace_net_log_observer.cc |
@@ -0,0 +1,102 @@ |
+// 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 "base/debug/trace_event.h" |
mmenke
2014/08/21 15:35:40
nit: Probably safe to leave this out, since heade
xunjieli
2014/08/22 17:20:09
Done. Thanks!
|
+#include "base/json/json_writer.h" |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+ |
+namespace net { |
+ |
+namespace { |
+ |
+class TracedValue : public base::debug::ConvertableToTraceFormat { |
+ public: |
+ explicit TracedValue(scoped_ptr<base::Value> value) : value_(value.Pass()) {} |
+ |
+ protected: |
mmenke
2014/08/21 15:35:39
Suggest either making these private or public - pr
xunjieli
2014/08/22 17:20:09
Done. I see. Thank you!
On 2014/08/21 15:35:39,
|
+ 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 |
mmenke
2014/08/21 15:35:39
nit: Blank line above end of namespace.
xunjieli
2014/08/22 17:20:10
Done. Thanks! I will keep this in mind next time!
|
+ |
+TraceNetLogObserver::TraceNetLogObserver(): |
+ log_level_(NetLog::LOG_STRIP_PRIVATE_DATA) { |
mmenke
2014/08/21 15:35:40
Initialize net_log_to_watch_ to NULL.
xunjieli
2014/08/22 17:20:09
Done.
|
+} |
+ |
+TraceNetLogObserver::~TraceNetLogObserver() { |
mmenke
2014/08/21 15:35:39
Maybe:
DCHECK(!net_log_to_watch_);
DCHECK(!net_lo
xunjieli
2014/08/22 17:20:09
Done.
|
+} |
+ |
+void TraceNetLogObserver::StartObserving(NetLog* net_log) { |
+ net_log->AddThreadSafeObserver(this, log_level_); |
+} |
+ |
+void TraceNetLogObserver::StopObserving() { |
+ net_log()->RemoveThreadSafeObserver(this); |
+} |
+ |
+void TraceNetLogObserver::OnAddEntry(const NetLog::Entry& entry) { |
+ scoped_ptr<base::Value> value(entry.ToValue()); |
+ switch (entry.phase()) { |
+ case(NetLog::PHASE_BEGIN): |
mmenke
2014/08/21 15:35:40
nit: case NetLog::PHASE_BEGIN:
(Same goes for th
xunjieli
2014/08/22 17:20:09
Done.Thanks!
|
+ TRACE_EVENT_NET_LOG_BEGIN( |
mmenke
2014/08/21 15:35:39
If this is only for NetLog events, then the macro
xunjieli
2014/08/22 17:20:09
Done.
|
+ "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, |
+ "value", scoped_refptr<base::debug::ConvertableToTraceFormat>( |
+ new TracedValue(value.Pass()))); |
mmenke
2014/08/21 15:35:39
Optional: This is a bit redundant. Value include
xunjieli
2014/08/22 17:20:10
Done. This is a good idea! Thanks!
On 2014/08/21 1
|
+ break; |
+ case(NetLog::PHASE_END): |
+ TRACE_EVENT_NET_LOG_END( |
+ "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, |
+ "value", scoped_refptr<base::debug::ConvertableToTraceFormat>( |
+ new TracedValue(value.Pass()))); |
+ break; |
+ case(NetLog::PHASE_NONE): |
+ TRACE_EVENT_NET_LOG_INSTANT( |
+ "netlog", NetLog::EventTypeToString(entry.type()), entry.source().id, |
+ "value", scoped_refptr<base::debug::ConvertableToTraceFormat>( |
+ new TracedValue(value.Pass()))); |
+ break; |
+ default: |
+ NOTREACHED(); |
mmenke
2014/08/21 15:35:39
Get rid of default path. Clang is configured to e
xunjieli
2014/08/22 17:20:09
Do you mean not doing anything in the default bran
|
+ } |
+} |
+ |
+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); |
+} |
mmenke
2014/08/21 15:35:39
net_log_to_watch_ = NULL?
xunjieli
2014/08/22 17:20:09
Done.
|
+ |
+void TraceNetLogObserver::OnTraceLogEnabled() { |
+ StartObserving(net_log_to_watch_); |
+} |
+ |
+void TraceNetLogObserver::OnTraceLogDisabled() { |
+ if (net_log()) |
+ StopObserving(); |
+} |
+ |
+} // namespace net |