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
;
65 typedef std::set
<scoped_refptr
<TraceMessageFilter
> > TraceMessageFilterSet
;
67 friend struct base::DefaultLazyInstanceTraits
<TracingControllerImpl
>;
68 friend class TraceMessageFilter
;
70 TracingControllerImpl();
71 ~TracingControllerImpl() override
;
73 bool can_enable_recording() const {
74 return !is_recording_
;
77 bool can_disable_recording() const {
78 return is_recording_
&& !trace_data_sink_
.get();
81 bool can_enable_monitoring() const {
82 return !is_monitoring_
;
85 bool can_disable_monitoring() const {
86 return is_monitoring_
&& !monitoring_data_sink_
.get();
89 bool can_get_trace_buffer_usage() const {
90 return pending_trace_buffer_usage_callback_
.is_null();
93 bool can_cancel_watch_event() const {
94 return !watch_event_callback_
.is_null();
97 // Methods for use by TraceMessageFilter.
98 void AddTraceMessageFilter(TraceMessageFilter
* trace_message_filter
);
99 void RemoveTraceMessageFilter(TraceMessageFilter
* trace_message_filter
);
101 void OnTraceDataCollected(
102 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
103 void OnMonitoringTraceDataCollected(
104 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
);
106 // Callback of TraceLog::Flush() for the local trace.
107 void OnLocalTraceDataCollected(
108 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
,
109 bool has_more_events
);
110 // Callback of TraceLog::FlushMonitoring() for the local trace.
111 void OnLocalMonitoringTraceDataCollected(
112 const scoped_refptr
<base::RefCountedString
>& events_str_ptr
,
113 bool has_more_events
);
115 void OnDisableRecordingAcked(
116 TraceMessageFilter
* trace_message_filter
,
117 const std::vector
<std::string
>& known_category_groups
);
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
);
127 void OnTraceLogStatusReply(TraceMessageFilter
* trace_message_filter
,
128 const base::trace_event::TraceLogStatus
& status
);
129 void OnProcessMemoryDumpResponse(TraceMessageFilter
* trace_message_filter
,
133 // Callback of MemoryDumpManager::CreateProcessDump().
134 void OnBrowserProcessMemoryDumpDone(uint64 dump_guid
, bool success
);
136 void FinalizeGlobalMemoryDumpIfAllProcessesReplied();
138 void OnWatchEventMatched();
140 void SetEnabledOnFileThread(
141 const base::trace_event::TraceConfig
& trace_config
,
143 const base::Closure
& callback
);
144 void SetDisabledOnFileThread(const base::Closure
& callback
);
145 void OnEnableRecordingDone(
146 const base::trace_event::TraceConfig
& trace_config
,
147 const EnableRecordingDoneCallback
& callback
);
148 void OnDisableRecordingDone();
149 void OnEnableMonitoringDone(
150 const base::trace_event::TraceConfig
& trace_config
,
151 const EnableMonitoringDoneCallback
& callback
);
152 void OnDisableMonitoringDone(const DisableMonitoringDoneCallback
& callback
);
154 void OnMonitoringStateChanged(bool is_monitoring
);
156 TraceMessageFilterSet trace_message_filters_
;
158 // Pending acks for DisableRecording.
159 int pending_disable_recording_ack_count_
;
160 TraceMessageFilterSet pending_disable_recording_filters_
;
162 // Pending acks for CaptureMonitoringSnapshot.
163 int pending_capture_monitoring_snapshot_ack_count_
;
164 TraceMessageFilterSet pending_capture_monitoring_filters_
;
166 // Pending acks for GetTraceLogStatus.
167 int pending_trace_log_status_ack_count_
;
168 TraceMessageFilterSet pending_trace_log_status_filters_
;
169 float maximum_trace_buffer_usage_
;
170 size_t approximate_event_count_
;
172 // Pending acks for memory RequestGlobalDumpPoint.
173 int pending_memory_dump_ack_count_
;
174 int failed_memory_dump_count_
;
175 TraceMessageFilterSet pending_memory_dump_filters_
;
176 uint64 pending_memory_dump_guid_
;
177 base::trace_event::MemoryDumpCallback pending_memory_dump_callback_
;
179 #if defined(OS_CHROMEOS) || defined(OS_WIN)
180 bool is_system_tracing_
;
185 GetCategoriesDoneCallback pending_get_categories_done_callback_
;
186 GetTraceBufferUsageCallback pending_trace_buffer_usage_callback_
;
188 std::string watch_category_name_
;
189 std::string watch_event_name_
;
190 WatchEventCallback watch_event_callback_
;
192 std::set
<std::string
> known_category_groups_
;
193 std::set
<TracingUI
*> tracing_uis_
;
194 scoped_refptr
<TraceDataSink
> trace_data_sink_
;
195 scoped_refptr
<TraceDataSink
> monitoring_data_sink_
;
197 DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl
);
200 } // namespace content
202 #endif // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_