1 // Copyright 2013 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 #ifndef CONTENT_PUBLIC_BROWSER_TRACING_CONTROLLER_H_
6 #define CONTENT_PUBLIC_BROWSER_TRACING_CONTROLLER_H_
11 #include "base/callback.h"
12 #include "base/memory/ref_counted.h"
13 #include "base/trace_event/trace_event.h"
14 #include "content/common/content_export.h"
18 class TracingController
;
20 // TracingController is used on the browser processes to enable/disable
21 // trace status and collect trace data. Only the browser UI thread is allowed
22 // to interact with the TracingController object. All callbacks are called on
24 class TracingController
{
27 CONTENT_EXPORT
static TracingController
* GetInstance();
29 // An interface for trace data consumer. An implemnentation of this interface
30 // is passed to either DisableTracing() or CaptureMonitoringSnapshot() and
31 // receives the trace data followed by a notification that all child processes
32 // have completed tracing and the data collection is over.
33 // All methods are called on the UI thread.
34 // Close method will be called exactly once and no methods will be
36 class CONTENT_EXPORT TraceDataSink
37 : public base::RefCountedThreadSafe
<TraceDataSink
> {
39 virtual void AddTraceChunk(const std::string
& chunk
) {}
40 virtual void SetSystemTrace(const std::string
& data
) {}
41 virtual void SetMetadata(const std::string
& data
) {}
42 virtual void Close() {}
45 friend class base::RefCountedThreadSafe
<TraceDataSink
>;
46 virtual ~TraceDataSink() {}
49 // An implementation of this interface is passed when constructing a
50 // TraceDataSink, and receives chunks of the final trace data as it's being
52 // Methods may be called from any thread.
53 class CONTENT_EXPORT TraceDataEndpoint
54 : public base::RefCountedThreadSafe
<TraceDataEndpoint
> {
56 virtual void ReceiveTraceChunk(const std::string
& chunk
) {}
57 virtual void ReceiveTraceFinalContents(const std::string
& contents
) {}
60 friend class base::RefCountedThreadSafe
<TraceDataEndpoint
>;
61 virtual ~TraceDataEndpoint() {}
64 // Create a trace sink that may be supplied to DisableRecording or
65 // CaptureMonitoringSnapshot to capture the trace data as a string.
66 CONTENT_EXPORT
static scoped_refptr
<TraceDataSink
> CreateStringSink(
67 const base::Callback
<void(base::RefCountedString
*)>& callback
);
69 CONTENT_EXPORT
static scoped_refptr
<TraceDataSink
> CreateCompressedStringSink(
70 scoped_refptr
<TraceDataEndpoint
> endpoint
);
72 // Create a trace sink that may be supplied to DisableRecording or
73 // CaptureMonitoringSnapshot to dump the trace data to a file.
74 CONTENT_EXPORT
static scoped_refptr
<TraceDataSink
> CreateFileSink(
75 const base::FilePath
& file_path
,
76 const base::Closure
& callback
);
78 // Create an endpoint that may be supplied to any TraceDataSink to
79 // dump the trace data to a callback.
80 CONTENT_EXPORT
static scoped_refptr
<TraceDataEndpoint
> CreateCallbackEndpoint(
81 const base::Callback
<void(base::RefCountedString
*)>& callback
);
83 // Create an endpoint that may be supplied to any TraceDataSink to
84 // dump the trace data to a file.
85 CONTENT_EXPORT
static scoped_refptr
<TraceDataEndpoint
> CreateFileEndpoint(
86 const base::FilePath
& file_path
,
87 const base::Closure
& callback
);
89 // Get a set of category groups. The category groups can change as
90 // new code paths are reached.
92 // Once all child processes have acked to the GetCategories request,
93 // GetCategoriesDoneCallback is called back with a set of category
95 typedef base::Callback
<void(const std::set
<std::string
>&)>
96 GetCategoriesDoneCallback
;
97 virtual bool GetCategories(
98 const GetCategoriesDoneCallback
& callback
) = 0;
100 // Start recording on all processes.
102 // Recording begins immediately locally, and asynchronously on child processes
103 // as soon as they receive the EnableRecording request.
105 // Once all child processes have acked to the EnableRecording request,
106 // EnableRecordingDoneCallback will be called back.
108 // |category_filter| is a filter to control what category groups should be
109 // traced. A filter can have an optional '-' prefix to exclude category groups
110 // that contain a matching category. Having both included and excluded
111 // category patterns in the same list would not be supported.
113 // Examples: "test_MyTest*",
114 // "test_MyTest*,test_OtherStuff",
115 // "-excluded_category1,-excluded_category2"
117 // |options| controls what kind of tracing is enabled.
118 typedef base::Callback
<void()> EnableRecordingDoneCallback
;
119 virtual bool EnableRecording(
120 const base::trace_event::TraceConfig
& trace_config
,
121 const EnableRecordingDoneCallback
& callback
) = 0;
123 // Stop recording on all processes.
125 // Child processes typically are caching trace data and only rarely flush
126 // and send trace data back to the browser process. That is because it may be
127 // an expensive operation to send the trace data over IPC, and we would like
128 // to avoid much runtime overhead of tracing. So, to end tracing, we must
129 // asynchronously ask all child processes to flush any pending trace data.
131 // Once all child processes have acked to the DisableRecording request,
132 // TracingFileResultCallback will be called back with a file that contains
135 // If |trace_data_sink| is not null, it will receive chunks of trace data
136 // as a comma-separated sequences of JSON-stringified events, followed by
137 // a notification that the trace collection is finished.
139 virtual bool DisableRecording(
140 const scoped_refptr
<TraceDataSink
>& trace_data_sink
) = 0;
142 // Start monitoring on all processes.
144 // Monitoring begins immediately locally, and asynchronously on child
145 // processes as soon as they receive the EnableMonitoring request.
147 // Once all child processes have acked to the EnableMonitoring request,
148 // EnableMonitoringDoneCallback will be called back.
150 // |category_filter| is a filter to control what category groups should be
153 // |options| controls what kind of tracing is enabled.
154 typedef base::Callback
<void()> EnableMonitoringDoneCallback
;
155 virtual bool EnableMonitoring(
156 const base::trace_event::TraceConfig
& trace_config
,
157 const EnableMonitoringDoneCallback
& callback
) = 0;
159 // Stop monitoring on all processes.
161 // Once all child processes have acked to the DisableMonitoring request,
162 // DisableMonitoringDoneCallback is called back.
163 typedef base::Callback
<void()> DisableMonitoringDoneCallback
;
164 virtual bool DisableMonitoring(
165 const DisableMonitoringDoneCallback
& callback
) = 0;
167 // Get the current monitoring configuration.
168 virtual void GetMonitoringStatus(
170 base::trace_event::TraceConfig
* out_trace_config
) = 0;
172 // Get the current monitoring traced data.
174 // Child processes typically are caching trace data and only rarely flush
175 // and send trace data back to the browser process. That is because it may be
176 // an expensive operation to send the trace data over IPC, and we would like
177 // to avoid much runtime overhead of tracing. So, to end tracing, we must
178 // asynchronously ask all child processes to flush any pending trace data.
180 // Once all child processes have acked to the CaptureMonitoringSnapshot
181 // request, TracingFileResultCallback will be called back with a file that
182 // contains the traced data.
184 // If |trace_data_sink| is not null, it will receive chunks of trace data
185 // as a comma-separated sequences of JSON-stringified events, followed by
186 // a notification that the trace collection is finished.
187 virtual bool CaptureMonitoringSnapshot(
188 const scoped_refptr
<TraceDataSink
>& trace_data_sink
) = 0;
190 // Get the maximum across processes of trace buffer percent full state.
191 // When the TraceBufferUsage value is determined, the callback is
193 typedef base::Callback
<void(float, size_t)> GetTraceBufferUsageCallback
;
194 virtual bool GetTraceBufferUsage(
195 const GetTraceBufferUsageCallback
& callback
) = 0;
197 // |callback| will will be called every time the given event occurs on any
199 typedef base::Callback
<void()> WatchEventCallback
;
200 virtual bool SetWatchEvent(const std::string
& category_name
,
201 const std::string
& event_name
,
202 const WatchEventCallback
& callback
) = 0;
204 // Cancel the watch event. If tracing is enabled, this may race with the
205 // watch event callback.
206 virtual bool CancelWatchEvent() = 0;
208 // Check if the tracing system is recording
209 virtual bool IsRecording() const = 0;
212 virtual ~TracingController() {}
215 } // namespace content
217 #endif // CONTENT_PUBLIC_BROWSER_TRACING_CONTROLLER_H_