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"
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"
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;
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
;
45 content::TracingController::GetInstance()->DisableRecording(
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
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
;
64 std::map
<int, scoped_refptr
<base::RefCountedString
> >::iterator data
=
66 if (data
== trace_data_
.end())
69 // Always return the data asychronously, so the behavior is consistant.
70 base::MessageLoopProxy::current()->PostTask(
72 base::Bind(callback
, data
->second
));
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_
)
104 if (!base::ReadFileToString(path
, &data
)) {
105 LOG(ERROR
) << "Failed to read trace data from: " << path
.value();
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
128 base::MessageLoopProxy::current()->PostTask(
130 base::Bind(&TracingManager::StartTracing
,
131 weak_ptr_factory_
.GetWeakPtr()));
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
;