Roll ANGLE bc75f36:ef9d63e
[chromium-blink-merge.git] / components / feedback / tracing_manager.cc
blobf35fe80876f44763eee829050efbe73d4febb7b0
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.
5 #include "components/feedback/tracing_manager.h"
7 #include "base/bind.h"
8 #include "base/file_util.h"
9 #include "base/location.h"
10 #include "base/memory/ref_counted_memory.h"
11 #include "base/message_loop/message_loop_proxy.h"
12 #include "components/feedback/feedback_util.h"
13 #include "content/public/browser/tracing_controller.h"
15 namespace {
16 // Only once trace manager can exist at a time.
17 TracingManager* g_tracing_manager = NULL;
18 // Trace IDs start at 1 and increase.
19 int g_next_trace_id = 1;
20 // Name of the file to store the tracing data as.
21 const base::FilePath::CharType kTracingFilename[] =
22 FILE_PATH_LITERAL("tracing.json");
25 TracingManager::TracingManager()
26 : current_trace_id_(0),
27 weak_ptr_factory_(this) {
28 DCHECK(!g_tracing_manager);
29 g_tracing_manager = this;
30 StartTracing();
33 TracingManager::~TracingManager() {
34 DCHECK(g_tracing_manager == this);
35 g_tracing_manager = NULL;
38 int TracingManager::RequestTrace() {
39 // Return the current trace if one is being collected.
40 if (current_trace_id_)
41 return current_trace_id_;
43 current_trace_id_ = g_next_trace_id;
44 ++g_next_trace_id;
45 content::TracingController::GetInstance()->DisableRecording(
46 base::FilePath(),
47 base::Bind(&TracingManager::OnTraceDataCollected,
48 weak_ptr_factory_.GetWeakPtr()));
49 return current_trace_id_;
52 bool TracingManager::GetTraceData(int id, const TraceDataCallback& callback) {
53 // If a trace is being collected currently, send it via callback when
54 // complete.
55 if (current_trace_id_) {
56 // Only allow one trace data request at a time.
57 if (trace_callback_.is_null()) {
58 trace_callback_ = callback;
59 return true;
60 } else {
61 return false;
63 } else {
64 std::map<int, scoped_refptr<base::RefCountedString> >::iterator data =
65 trace_data_.find(id);
66 if (data == trace_data_.end())
67 return false;
69 // Always return the data asychronously, so the behavior is consistant.
70 base::MessageLoopProxy::current()->PostTask(
71 FROM_HERE,
72 base::Bind(callback, data->second));
73 return true;
77 void TracingManager::DiscardTraceData(int id) {
78 trace_data_.erase(id);
80 // If the trace is discarded before it is complete, clean up the accumulators.
81 if (id == current_trace_id_) {
82 current_trace_id_ = 0;
84 // If the trace has already been requested, provide an empty string.
85 if (!trace_callback_.is_null()) {
86 trace_callback_.Run(scoped_refptr<base::RefCountedString>());
87 trace_callback_.Reset();
92 void TracingManager::StartTracing() {
93 content::TracingController::GetInstance()->EnableRecording(
94 base::debug::CategoryFilter(),
95 base::debug::TraceOptions(),
96 content::TracingController::EnableRecordingDoneCallback());
99 void TracingManager::OnTraceDataCollected(const base::FilePath& path) {
100 if (!current_trace_id_)
101 return;
103 std::string data;
104 if (!base::ReadFileToString(path, &data)) {
105 LOG(ERROR) << "Failed to read trace data from: " << path.value();
106 return;
108 base::DeleteFile(path, false);
110 std::string output_val;
111 feedback_util::ZipString(
112 base::FilePath(kTracingFilename), data, &output_val);
114 scoped_refptr<base::RefCountedString> output(
115 base::RefCountedString::TakeString(&output_val));
117 trace_data_[current_trace_id_] = output;
119 if (!trace_callback_.is_null()) {
120 trace_callback_.Run(output);
121 trace_callback_.Reset();
124 current_trace_id_ = 0;
126 // Tracing has to be restarted asynchronous, so the TracingController can
127 // clean up.
128 base::MessageLoopProxy::current()->PostTask(
129 FROM_HERE,
130 base::Bind(&TracingManager::StartTracing,
131 weak_ptr_factory_.GetWeakPtr()));
134 // static
135 scoped_ptr<TracingManager> TracingManager::Create() {
136 if (g_tracing_manager)
137 return scoped_ptr<TracingManager>();
138 return scoped_ptr<TracingManager>(new TracingManager());
141 TracingManager* TracingManager::Get() {
142 return g_tracing_manager;