summaryrefslogtreecommitdiff
path: root/chromium/components/feedback/tracing_manager.cc
blob: eb5fba1c5f02b0e3305663def0eceedef8fc515f (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// 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 "components/feedback/tracing_manager.h"

#include "base/bind.h"
#include "base/location.h"
#include "base/memory/ref_counted_memory.h"
#include "base/single_thread_task_runner.h"
#include "base/threading/thread_task_runner_handle.h"
#include "components/feedback/feedback_util.h"
#include "content/public/browser/tracing_controller.h"

namespace {

// Only once trace manager can exist at a time.
TracingManager* g_tracing_manager = nullptr;
// Trace IDs start at 1 and increase.
int g_next_trace_id = 1;
// Name of the file to store the tracing data as.
const base::FilePath::CharType kTracingFilename[] =
    FILE_PATH_LITERAL("tracing.json");
}

TracingManager::TracingManager() : current_trace_id_(0) {
  DCHECK(!g_tracing_manager);
  g_tracing_manager = this;
  StartTracing();
}

TracingManager::~TracingManager() {
  DCHECK(g_tracing_manager == this);
  g_tracing_manager = nullptr;
}

int TracingManager::RequestTrace() {
  // Return the current trace if one is being collected.
  if (current_trace_id_)
    return current_trace_id_;

  current_trace_id_ = g_next_trace_id;
  ++g_next_trace_id;
  content::TracingController::GetInstance()->StopTracing(
      content::TracingController::CreateStringEndpoint(
          base::BindOnce(&TracingManager::OnTraceDataCollected,
                         weak_ptr_factory_.GetWeakPtr())));
  return current_trace_id_;
}

bool TracingManager::GetTraceData(int id, TraceDataCallback callback) {
  // If a trace is being collected currently, send it via callback when
  // complete.
  if (current_trace_id_) {
    // Only allow one trace data request at a time.
    if (!trace_callback_) {
      trace_callback_ = std::move(callback);
      return true;
    } else {
      return false;
    }
  } else {
    auto data = trace_data_.find(id);
    if (data == trace_data_.end())
      return false;

    // Always return the data asychronously, so the behavior is consistant.
    base::ThreadTaskRunnerHandle::Get()->PostTask(
        FROM_HERE, base::BindOnce(std::move(callback), data->second));
    return true;
  }
}

void TracingManager::DiscardTraceData(int id) {
  trace_data_.erase(id);

  // If the trace is discarded before it is complete, clean up the accumulators.
  if (id == current_trace_id_) {
    current_trace_id_ = 0;

    // If the trace has already been requested, provide an empty string.
    if (trace_callback_)
      std::move(trace_callback_).Run(scoped_refptr<base::RefCountedString>());
  }
}

void TracingManager::StartTracing() {
  content::TracingController::GetInstance()->StartTracing(
      base::trace_event::TraceConfig(),
      content::TracingController::StartTracingDoneCallback());
}

void TracingManager::OnTraceDataCollected(
    std::unique_ptr<std::string> trace_data) {
  if (!current_trace_id_)
    return;

  std::string output_val;
  feedback_util::ZipString(base::FilePath(kTracingFilename), *trace_data,
                           &output_val);

  scoped_refptr<base::RefCountedString> output(
      base::RefCountedString::TakeString(&output_val));

  trace_data_[current_trace_id_] = output;

  if (trace_callback_)
    std::move(trace_callback_).Run(output);

  current_trace_id_ = 0;

  // Tracing has to be restarted asynchronous, so the TracingController can
  // clean up.
  base::ThreadTaskRunnerHandle::Get()->PostTask(
      FROM_HERE, base::BindOnce(&TracingManager::StartTracing,
                                weak_ptr_factory_.GetWeakPtr()));
}

// static
std::unique_ptr<TracingManager> TracingManager::Create() {
  if (g_tracing_manager)
    return std::unique_ptr<TracingManager>();
  return std::unique_ptr<TracingManager>(new TracingManager());
}

TracingManager* TracingManager::Get() {
  return g_tracing_manager;
}