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.
4
5#ifndef CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_
6#define CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_
7
8#include <set>
9#include <string>
10#include <vector>
11
12#include "base/lazy_instance.h"
13#include "content/public/browser/tracing_controller.h"
14
15namespace base {
16class RefCountedString;
17class RefCountedMemory;
18}
19
20namespace content {
21
22class TraceMessageFilter;
23class TracingUI;
24
25class TracingControllerImpl : public TracingController {
26 public:
27  static TracingControllerImpl* GetInstance();
28
29  // TracingController implementation.
30  virtual bool GetCategories(
31      const GetCategoriesDoneCallback& callback) OVERRIDE;
32  virtual bool EnableRecording(
33      const base::debug::CategoryFilter& category_filter,
34      const base::debug::TraceOptions& trace_options,
35      const EnableRecordingDoneCallback& callback) OVERRIDE;
36  virtual bool DisableRecording(
37      const scoped_refptr<TraceDataSink>& sink) OVERRIDE;
38  virtual bool EnableMonitoring(
39      const base::debug::CategoryFilter& category_filter,
40      const base::debug::TraceOptions& trace_options,
41      const EnableMonitoringDoneCallback& callback) OVERRIDE;
42  virtual bool DisableMonitoring(
43      const DisableMonitoringDoneCallback& callback) OVERRIDE;
44  virtual void GetMonitoringStatus(
45      bool* out_enabled,
46      base::debug::CategoryFilter* out_category_filter,
47      base::debug::TraceOptions* out_trace_options) OVERRIDE;
48  virtual bool CaptureMonitoringSnapshot(
49      const scoped_refptr<TraceDataSink>& sink) OVERRIDE;
50  virtual bool GetTraceBufferPercentFull(
51      const GetTraceBufferPercentFullCallback& callback) OVERRIDE;
52  virtual bool SetWatchEvent(const std::string& category_name,
53                             const std::string& event_name,
54                             const WatchEventCallback& callback) OVERRIDE;
55  virtual bool CancelWatchEvent() OVERRIDE;
56
57  void RegisterTracingUI(TracingUI* tracing_ui);
58  void UnregisterTracingUI(TracingUI* tracing_ui);
59
60 private:
61  typedef std::set<scoped_refptr<TraceMessageFilter> > TraceMessageFilterSet;
62
63  friend struct base::DefaultLazyInstanceTraits<TracingControllerImpl>;
64  friend class TraceMessageFilter;
65
66  TracingControllerImpl();
67  virtual ~TracingControllerImpl();
68
69  bool can_enable_recording() const {
70    return !is_recording_;
71  }
72
73  bool can_disable_recording() const {
74    return is_recording_ && !trace_data_sink_.get();
75  }
76
77  bool can_enable_monitoring() const {
78    return !is_monitoring_;
79  }
80
81  bool can_disable_monitoring() const {
82    return is_monitoring_ && !monitoring_data_sink_.get();
83  }
84
85  bool can_get_trace_buffer_percent_full() const {
86    return pending_trace_buffer_percent_full_callback_.is_null();
87  }
88
89  bool can_cancel_watch_event() const {
90    return !watch_event_callback_.is_null();
91  }
92
93  // Methods for use by TraceMessageFilter.
94  void AddTraceMessageFilter(TraceMessageFilter* trace_message_filter);
95  void RemoveTraceMessageFilter(TraceMessageFilter* trace_message_filter);
96
97  void OnTraceDataCollected(
98      const scoped_refptr<base::RefCountedString>& events_str_ptr);
99  void OnMonitoringTraceDataCollected(
100      const scoped_refptr<base::RefCountedString>& events_str_ptr);
101
102  // Callback of TraceLog::Flush() for the local trace.
103  void OnLocalTraceDataCollected(
104      const scoped_refptr<base::RefCountedString>& events_str_ptr,
105      bool has_more_events);
106  // Callback of TraceLog::FlushMonitoring() for the local trace.
107  void OnLocalMonitoringTraceDataCollected(
108      const scoped_refptr<base::RefCountedString>& events_str_ptr,
109      bool has_more_events);
110
111  void OnDisableRecordingAcked(
112      TraceMessageFilter* trace_message_filter,
113      const std::vector<std::string>& known_category_groups);
114
115#if defined(OS_CHROMEOS) || defined(OS_WIN)
116  void OnEndSystemTracingAcked(
117      const scoped_refptr<base::RefCountedString>& events_str_ptr);
118#endif
119
120  void OnCaptureMonitoringSnapshotAcked(
121      TraceMessageFilter* trace_message_filter);
122
123  void OnTraceBufferPercentFullReply(
124      TraceMessageFilter* trace_message_filter,
125      float percent_full);
126
127  void OnWatchEventMatched();
128
129  void SetEnabledOnFileThread(
130      const base::debug::CategoryFilter& category_filter,
131      int mode,
132      const base::debug::TraceOptions& trace_options,
133      const base::Closure& callback);
134  void SetDisabledOnFileThread(const base::Closure& callback);
135  void OnEnableRecordingDone(const base::debug::CategoryFilter& category_filter,
136                             const base::debug::TraceOptions& trace_options,
137                             const EnableRecordingDoneCallback& callback);
138  void OnDisableRecordingDone();
139  void OnEnableMonitoringDone(
140      const base::debug::CategoryFilter& category_filter,
141      const base::debug::TraceOptions& trace_options,
142      const EnableMonitoringDoneCallback& callback);
143  void OnDisableMonitoringDone(const DisableMonitoringDoneCallback& callback);
144
145  void OnMonitoringStateChanged(bool is_monitoring);
146
147  TraceMessageFilterSet trace_message_filters_;
148
149  // Pending acks for DisableRecording.
150  int pending_disable_recording_ack_count_;
151  TraceMessageFilterSet pending_disable_recording_filters_;
152  // Pending acks for CaptureMonitoringSnapshot.
153  int pending_capture_monitoring_snapshot_ack_count_;
154  TraceMessageFilterSet pending_capture_monitoring_filters_;
155  // Pending acks for GetTraceBufferPercentFull.
156  int pending_trace_buffer_percent_full_ack_count_;
157  TraceMessageFilterSet pending_trace_buffer_percent_full_filters_;
158  float maximum_trace_buffer_percent_full_;
159
160#if defined(OS_CHROMEOS) || defined(OS_WIN)
161  bool is_system_tracing_;
162#endif
163  bool is_recording_;
164  bool is_monitoring_;
165  base::debug::TraceOptions trace_options_;
166
167  GetCategoriesDoneCallback pending_get_categories_done_callback_;
168  GetTraceBufferPercentFullCallback pending_trace_buffer_percent_full_callback_;
169
170  std::string watch_category_name_;
171  std::string watch_event_name_;
172  WatchEventCallback watch_event_callback_;
173
174  std::set<std::string> known_category_groups_;
175  std::set<TracingUI*> tracing_uis_;
176  scoped_refptr<TraceDataSink> trace_data_sink_;
177  scoped_refptr<TraceDataSink> monitoring_data_sink_;
178  DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl);
179};
180
181}  // namespace content
182
183#endif  // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_
184