summaryrefslogtreecommitdiff
path: root/chromium/net/base/trace_net_log_observer.cc
blob: 5d98c41d37b4cce2154fd12ac673f44547298624 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// 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>

#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 {

// TraceLog category for NetLog events.
const char kNetLogTracingCategory[] = TRACE_DISABLED_BY_DEFAULT("netlog");

class TracedValue : public base::debug::ConvertableToTraceFormat {
 public:
  explicit TracedValue(scoped_ptr<base::Value> value) : value_(value.Pass()) {}

 private:
  ~TracedValue() override {}

  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_NESTABLE_ASYNC_BEGIN2(
          kNetLogTracingCategory,
          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_NESTABLE_ASYNC_END2(
          kNetLogTracingCategory,
          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_NESTABLE_ASYNC_INSTANT2(
          kNetLogTracingCategory,
          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;
  }
}

void TraceNetLogObserver::WatchForTraceStart(NetLog* netlog) {
  DCHECK(!net_log_to_watch_);
  DCHECK(!net_log());
  net_log_to_watch_ = netlog;
  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