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/lazy_instance.h"
13 #include "base/trace_event/memory_dump_manager.h"
14 #include "content/public/browser/tracing_controller.h"
17 class RefCountedString
;
18 class RefCountedMemory
;
23 class TraceMessageFilter
;
26 class TracingControllerImpl
27 : public TracingController
,
28 public base::trace_event::MemoryDumpManagerDelegate
{
30 static TracingControllerImpl
* GetInstance();
32 // TracingController implementation.
33 bool GetCategories(const GetCategoriesDoneCallback
& callback
) override
;
34 bool EnableRecording(const base::trace_event::TraceConfig
& trace_config
,
35 const EnableRecordingDoneCallback
& callback
) override
;
36 bool DisableRecording(const scoped_refptr
<TraceDataSink
>& sink
) override
;
37 bool EnableMonitoring(
38 const base::trace_event::TraceConfig
& trace_config
,
39 const EnableMonitoringDoneCallback
& callback
) override
;
40 bool DisableMonitoring(
41 const DisableMonitoringDoneCallback
& callback
) override
;
42 void GetMonitoringStatus(
44 base::trace_event::TraceConfig
* out_trace_config
) override
;
45 bool CaptureMonitoringSnapshot(
46 const scoped_refptr
<TraceDataSink
>& sink
) override
;
47 bool GetTraceBufferUsage(
48 const GetTraceBufferUsageCallback
& callback
) override
;
49 bool SetWatchEvent(const std::string
& category_name
,
50 const std::string
& event_name
,
51 const WatchEventCallback
& callback
) override
;
52 bool CancelWatchEvent() override
;
53 bool IsRecording() const override
;
55 void RegisterTracingUI(TracingUI
* tracing_ui
);
56 void UnregisterTracingUI(TracingUI
* tracing_ui
);
58 // base::trace_event::MemoryDumpManagerDelegate implementation.
59 void RequestGlobalMemoryDump(
60 const base::trace_event::MemoryDumpRequestArgs
& args
,
61 const base::trace_event::MemoryDumpCallback
& callback
) override
;
62 bool IsCoordinatorProcess() const override
;
63 uint64
GetTracingProcessId() const override
;
65 class TraceMessageFilterObserver
{
67 virtual void OnTraceMessageFilterAdded(TraceMessageFilter
* filter
) = 0;
68 virtual void OnTraceMessageFilterRemoved(TraceMessageFilter
* filter
) = 0;
70 void AddTraceMessageFilterObserver(TraceMessageFilterObserver
* observer
);
71 void RemoveTraceMessageFilterObserver(TraceMessageFilterObserver
* observer
);
74 friend struct base::DefaultLazyInstanceTraits
<TracingControllerImpl
>;
75 friend class TraceMessageFilter
;
77 TracingControllerImpl();
78 ~TracingControllerImpl() override
;
80 bool can_enable_recording() const {
81 return !is_recording_
;
84 bool can_disable_recording() const {
85 return is_recording_
&& !trace_data_sink_
.get();
88 bool can_enable_monitoring() const {
89 return !is_monitoring_
;
92 bool can_disable_monitoring() const {
93 return is_monitoring_
&& !monitoring_data_sink_
.get();
96 bool can_get_trace_buffer_usage() const {
97 return pending_trace_buffer_usage_callback_
.is_null();
100 bool can_cancel_watch_event() const {
101 return !watch_event_callback_
.is_null();
104 // Methods for use by TraceMessageFilter.
105 void AddTraceMessageFilter(TraceMessageFilter
* trace_message_filter
);
106 void RemoveTraceMessageFilter(TraceMessageFilter
* trace_message_filter
);
108 void OnTraceDataCollected(
109 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
110 void OnMonitoringTraceDataCollected(
111 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
113 // Callback of TraceLog::Flush() for the local trace.
114 void OnLocalTraceDataCollected(
115 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
,
116 bool has_more_events
);
117 // Callback of TraceLog::FlushMonitoring() for the local trace.
118 void OnLocalMonitoringTraceDataCollected(
119 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
,
120 bool has_more_events
);
122 void OnDisableRecordingAcked(
123 TraceMessageFilter
* trace_message_filter
,
124 const std::vector
<std::string
>& known_category_groups
);
126 #if defined(OS_CHROMEOS) || defined(OS_WIN)
127 void OnEndSystemTracingAcked(
128 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
131 void OnEndPowerTracingAcked(
132 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
134 void OnCaptureMonitoringSnapshotAcked(
135 TraceMessageFilter
* trace_message_filter
);
137 void OnTraceLogStatusReply(TraceMessageFilter
* trace_message_filter
,
138 const base::trace_event::TraceLogStatus
& status
);
139 void OnProcessMemoryDumpResponse(TraceMessageFilter
* trace_message_filter
,
143 // Callback of MemoryDumpManager::CreateProcessDump().
144 void OnBrowserProcessMemoryDumpDone(uint64 dump_guid
, bool success
);
146 void FinalizeGlobalMemoryDumpIfAllProcessesReplied();
148 void OnWatchEventMatched();
150 void SetEnabledOnFileThread(
151 const base::trace_event::TraceConfig
& trace_config
,
153 const base::Closure
& callback
);
154 void SetDisabledOnFileThread(const base::Closure
& callback
);
155 void OnEnableRecordingDone(
156 const base::trace_event::TraceConfig
& trace_config
,
157 const EnableRecordingDoneCallback
& callback
);
158 void OnDisableRecordingDone();
159 void OnEnableMonitoringDone(
160 const base::trace_event::TraceConfig
& trace_config
,
161 const EnableMonitoringDoneCallback
& callback
);
162 void OnDisableMonitoringDone(const DisableMonitoringDoneCallback
& callback
);
164 void OnMonitoringStateChanged(bool is_monitoring
);
166 typedef std::set
<scoped_refptr
<TraceMessageFilter
>> TraceMessageFilterSet
;
167 TraceMessageFilterSet trace_message_filters_
;
169 // Pending acks for DisableRecording.
170 int pending_disable_recording_ack_count_
;
171 TraceMessageFilterSet pending_disable_recording_filters_
;
173 // Pending acks for CaptureMonitoringSnapshot.
174 int pending_capture_monitoring_snapshot_ack_count_
;
175 TraceMessageFilterSet pending_capture_monitoring_filters_
;
177 // Pending acks for GetTraceLogStatus.
178 int pending_trace_log_status_ack_count_
;
179 TraceMessageFilterSet pending_trace_log_status_filters_
;
180 float maximum_trace_buffer_usage_
;
181 size_t approximate_event_count_
;
183 // Pending acks for memory RequestGlobalDumpPoint.
184 int pending_memory_dump_ack_count_
;
185 int failed_memory_dump_count_
;
186 TraceMessageFilterSet pending_memory_dump_filters_
;
187 uint64 pending_memory_dump_guid_
;
188 base::trace_event::MemoryDumpCallback pending_memory_dump_callback_
;
190 #if defined(OS_CHROMEOS) || defined(OS_WIN)
191 bool is_system_tracing_
;
195 bool is_power_tracing_
;
197 GetCategoriesDoneCallback pending_get_categories_done_callback_
;
198 GetTraceBufferUsageCallback pending_trace_buffer_usage_callback_
;
200 std::string watch_category_name_
;
201 std::string watch_event_name_
;
202 WatchEventCallback watch_event_callback_
;
204 base::ObserverList
<TraceMessageFilterObserver
>
205 trace_message_filter_observers_
;
207 std::set
<std::string
> known_category_groups_
;
208 std::set
<TracingUI
*> tracing_uis_
;
209 scoped_refptr
<TraceDataSink
> trace_data_sink_
;
210 scoped_refptr
<TraceDataSink
> monitoring_data_sink_
;
212 DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl
);
215 } // namespace content
217 #endif // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_