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 typedef base::Callback
<void(TraceMessageFilter
*)>
66 TraceMessageFilterAddedCallback
;
67 typedef std::set
<scoped_refptr
<TraceMessageFilter
> > TraceMessageFilterSet
;
69 TraceMessageFilterAddedCallback trace_filter_added_callback_
;
70 void SetTraceMessageFilterAddedCallback(
71 const TraceMessageFilterAddedCallback
& callback
);
72 void GetTraceMessageFilters(TraceMessageFilterSet
*);
75 friend struct base::DefaultLazyInstanceTraits
<TracingControllerImpl
>;
76 friend class TraceMessageFilter
;
78 TracingControllerImpl();
79 ~TracingControllerImpl() override
;
81 bool can_enable_recording() const {
82 return !is_recording_
;
85 bool can_disable_recording() const {
86 return is_recording_
&& !trace_data_sink_
.get();
89 bool can_enable_monitoring() const {
90 return !is_monitoring_
;
93 bool can_disable_monitoring() const {
94 return is_monitoring_
&& !monitoring_data_sink_
.get();
97 bool can_get_trace_buffer_usage() const {
98 return pending_trace_buffer_usage_callback_
.is_null();
101 bool can_cancel_watch_event() const {
102 return !watch_event_callback_
.is_null();
105 // Methods for use by TraceMessageFilter.
106 void AddTraceMessageFilter(TraceMessageFilter
* trace_message_filter
);
107 void RemoveTraceMessageFilter(TraceMessageFilter
* trace_message_filter
);
109 void OnTraceDataCollected(
110 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
111 void OnMonitoringTraceDataCollected(
112 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
114 // Callback of TraceLog::Flush() for the local trace.
115 void OnLocalTraceDataCollected(
116 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
,
117 bool has_more_events
);
118 // Callback of TraceLog::FlushMonitoring() for the local trace.
119 void OnLocalMonitoringTraceDataCollected(
120 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
,
121 bool has_more_events
);
123 void OnDisableRecordingAcked(
124 TraceMessageFilter
* trace_message_filter
,
125 const std::vector
<std::string
>& known_category_groups
);
127 #if defined(OS_CHROMEOS) || defined(OS_WIN)
128 void OnEndSystemTracingAcked(
129 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
132 void OnCaptureMonitoringSnapshotAcked(
133 TraceMessageFilter
* trace_message_filter
);
135 void OnTraceLogStatusReply(TraceMessageFilter
* trace_message_filter
,
136 const base::trace_event::TraceLogStatus
& status
);
137 void OnProcessMemoryDumpResponse(TraceMessageFilter
* trace_message_filter
,
141 // Callback of MemoryDumpManager::CreateProcessDump().
142 void OnBrowserProcessMemoryDumpDone(uint64 dump_guid
, bool success
);
144 void FinalizeGlobalMemoryDumpIfAllProcessesReplied();
146 void OnWatchEventMatched();
148 void SetEnabledOnFileThread(
149 const base::trace_event::TraceConfig
& trace_config
,
151 const base::Closure
& callback
);
152 void SetDisabledOnFileThread(const base::Closure
& callback
);
153 void OnEnableRecordingDone(
154 const base::trace_event::TraceConfig
& trace_config
,
155 const EnableRecordingDoneCallback
& callback
);
156 void OnDisableRecordingDone();
157 void OnEnableMonitoringDone(
158 const base::trace_event::TraceConfig
& trace_config
,
159 const EnableMonitoringDoneCallback
& callback
);
160 void OnDisableMonitoringDone(const DisableMonitoringDoneCallback
& callback
);
162 void OnMonitoringStateChanged(bool is_monitoring
);
164 TraceMessageFilterSet trace_message_filters_
;
166 // Pending acks for DisableRecording.
167 int pending_disable_recording_ack_count_
;
168 TraceMessageFilterSet pending_disable_recording_filters_
;
170 // Pending acks for CaptureMonitoringSnapshot.
171 int pending_capture_monitoring_snapshot_ack_count_
;
172 TraceMessageFilterSet pending_capture_monitoring_filters_
;
174 // Pending acks for GetTraceLogStatus.
175 int pending_trace_log_status_ack_count_
;
176 TraceMessageFilterSet pending_trace_log_status_filters_
;
177 float maximum_trace_buffer_usage_
;
178 size_t approximate_event_count_
;
180 // Pending acks for memory RequestGlobalDumpPoint.
181 int pending_memory_dump_ack_count_
;
182 int failed_memory_dump_count_
;
183 TraceMessageFilterSet pending_memory_dump_filters_
;
184 uint64 pending_memory_dump_guid_
;
185 base::trace_event::MemoryDumpCallback pending_memory_dump_callback_
;
187 #if defined(OS_CHROMEOS) || defined(OS_WIN)
188 bool is_system_tracing_
;
193 GetCategoriesDoneCallback pending_get_categories_done_callback_
;
194 GetTraceBufferUsageCallback pending_trace_buffer_usage_callback_
;
196 std::string watch_category_name_
;
197 std::string watch_event_name_
;
198 WatchEventCallback watch_event_callback_
;
200 TraceMessageFilterAddedCallback trace_message_filter_added_callback_
;
202 std::set
<std::string
> known_category_groups_
;
203 std::set
<TracingUI
*> tracing_uis_
;
204 scoped_refptr
<TraceDataSink
> trace_data_sink_
;
205 scoped_refptr
<TraceDataSink
> monitoring_data_sink_
;
207 DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl
);
210 } // namespace content
212 #endif // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_