1 // Copyright (c) 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_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_
6 #define CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_
12 #include "base/files/file_path.h"
13 #include "base/lazy_instance.h"
14 #include "content/public/browser/tracing_controller.h"
17 class RefCountedString
;
22 class TraceMessageFilter
;
25 class TracingControllerImpl
: public TracingController
{
27 static TracingControllerImpl
* GetInstance();
29 // TracingController implementation.
30 virtual bool GetCategories(
31 const GetCategoriesDoneCallback
& callback
) OVERRIDE
;
32 virtual bool EnableRecording(
33 const std::string
& category_filter
,
34 TracingController::Options options
,
35 const EnableRecordingDoneCallback
& callback
) OVERRIDE
;
36 virtual bool DisableRecording(
37 const base::FilePath
& result_file_path
,
38 const TracingFileResultCallback
& callback
) OVERRIDE
;
39 virtual bool EnableMonitoring(const std::string
& category_filter
,
40 TracingController::Options options
,
41 const EnableMonitoringDoneCallback
& callback
) OVERRIDE
;
42 virtual bool DisableMonitoring(
43 const DisableMonitoringDoneCallback
& callback
) OVERRIDE
;
44 virtual void GetMonitoringStatus(
46 std::string
* out_category_filter
,
47 TracingController::Options
* out_options
) OVERRIDE
;
48 virtual bool CaptureMonitoringSnapshot(
49 const base::FilePath
& result_file_path
,
50 const TracingFileResultCallback
& callback
) OVERRIDE
;
51 virtual bool GetTraceBufferPercentFull(
52 const GetTraceBufferPercentFullCallback
& callback
) OVERRIDE
;
53 virtual bool SetWatchEvent(const std::string
& category_name
,
54 const std::string
& event_name
,
55 const WatchEventCallback
& callback
) OVERRIDE
;
56 virtual bool CancelWatchEvent() OVERRIDE
;
58 void RegisterTracingUI(TracingUI
* tracing_ui
);
59 void UnregisterTracingUI(TracingUI
* tracing_ui
);
62 typedef std::set
<scoped_refptr
<TraceMessageFilter
> > TraceMessageFilterSet
;
65 friend struct base::DefaultLazyInstanceTraits
<TracingControllerImpl
>;
66 friend class TraceMessageFilter
;
68 TracingControllerImpl();
69 virtual ~TracingControllerImpl();
71 bool can_enable_recording() const {
72 return !is_recording_
;
75 bool can_disable_recording() const {
76 return is_recording_
&& !result_file_
;
79 bool can_enable_monitoring() const {
80 return !is_monitoring_
;
83 bool can_disable_monitoring() const {
84 return is_monitoring_
&& !monitoring_snapshot_file_
;
87 bool can_get_trace_buffer_percent_full() const {
88 return pending_trace_buffer_percent_full_callback_
.is_null();
91 bool can_cancel_watch_event() const {
92 return !watch_event_callback_
.is_null();
95 // Methods for use by TraceMessageFilter.
96 void AddTraceMessageFilter(TraceMessageFilter
* trace_message_filter
);
97 void RemoveTraceMessageFilter(TraceMessageFilter
* trace_message_filter
);
99 void OnTraceDataCollected(
100 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
101 void OnMonitoringTraceDataCollected(
102 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
104 // Callback of TraceLog::Flush() for the local trace.
105 void OnLocalTraceDataCollected(
106 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
,
107 bool has_more_events
);
108 // Callback of TraceLog::FlushMonitoring() for the local trace.
109 void OnLocalMonitoringTraceDataCollected(
110 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
,
111 bool has_more_events
);
113 void OnDisableRecordingAcked(
114 TraceMessageFilter
* trace_message_filter
,
115 const std::vector
<std::string
>& known_category_groups
);
116 void OnDisableRecordingComplete();
117 void OnResultFileClosed();
119 #if defined(OS_CHROMEOS) || defined(OS_WIN)
120 void OnEndSystemTracingAcked(
121 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
124 void OnCaptureMonitoringSnapshotAcked(
125 TraceMessageFilter
* trace_message_filter
);
126 void OnMonitoringSnapshotFileClosed();
128 void OnTraceBufferPercentFullReply(
129 TraceMessageFilter
* trace_message_filter
,
132 void OnWatchEventMatched();
134 void SetEnabledOnFileThread(const std::string
& category_filter
,
137 const base::Closure
& callback
);
138 void SetDisabledOnFileThread(const base::Closure
& callback
);
139 void OnEnableRecordingDone(const std::string
& category_filter
,
141 const EnableRecordingDoneCallback
& callback
);
142 void OnDisableRecordingDone(const base::FilePath
& result_file_path
,
143 const TracingFileResultCallback
& callback
);
144 void OnEnableMonitoringDone(const std::string
& category_filter
,
146 const EnableMonitoringDoneCallback
& callback
);
147 void OnDisableMonitoringDone(const DisableMonitoringDoneCallback
& callback
);
149 void OnMonitoringStateChanged(bool is_monitoring
);
151 TraceMessageFilterSet trace_message_filters_
;
153 // Pending acks for DisableRecording.
154 int pending_disable_recording_ack_count_
;
155 TraceMessageFilterSet pending_disable_recording_filters_
;
156 // Pending acks for CaptureMonitoringSnapshot.
157 int pending_capture_monitoring_snapshot_ack_count_
;
158 TraceMessageFilterSet pending_capture_monitoring_filters_
;
159 // Pending acks for GetTraceBufferPercentFull.
160 int pending_trace_buffer_percent_full_ack_count_
;
161 TraceMessageFilterSet pending_trace_buffer_percent_full_filters_
;
162 float maximum_trace_buffer_percent_full_
;
164 #if defined(OS_CHROMEOS) || defined(OS_WIN)
165 bool is_system_tracing_
;
169 TracingController::Options options_
;
171 GetCategoriesDoneCallback pending_get_categories_done_callback_
;
172 TracingFileResultCallback pending_disable_recording_done_callback_
;
173 TracingFileResultCallback pending_capture_monitoring_snapshot_done_callback_
;
174 GetTraceBufferPercentFullCallback pending_trace_buffer_percent_full_callback_
;
176 std::string watch_category_name_
;
177 std::string watch_event_name_
;
178 WatchEventCallback watch_event_callback_
;
180 std::set
<std::string
> known_category_groups_
;
181 std::set
<TracingUI
*> tracing_uis_
;
182 scoped_ptr
<ResultFile
> result_file_
;
183 scoped_ptr
<ResultFile
> monitoring_snapshot_file_
;
184 DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl
);
187 } // namespace content
189 #endif // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_