• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/files/file_path.h"
13 #include "base/lazy_instance.h"
14 #include "content/public/browser/tracing_controller.h"
15 
16 namespace base {
17 class RefCountedString;
18 }
19 
20 namespace content {
21 
22 class TraceMessageFilter;
23 class TracingUI;
24 
25 class 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 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(
45       bool* out_enabled,
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;
57 
58   void RegisterTracingUI(TracingUI* tracing_ui);
59   void UnregisterTracingUI(TracingUI* tracing_ui);
60 
61  private:
62   typedef std::set<scoped_refptr<TraceMessageFilter> > TraceMessageFilterSet;
63   class ResultFile;
64 
65   friend struct base::DefaultLazyInstanceTraits<TracingControllerImpl>;
66   friend class TraceMessageFilter;
67 
68   TracingControllerImpl();
69   virtual ~TracingControllerImpl();
70 
71   bool can_enable_recording() const {
72     return !is_recording_;
73   }
74 
75   bool can_disable_recording() const {
76     return is_recording_ && !result_file_;
77   }
78 
79   bool can_enable_monitoring() const {
80     return !is_monitoring_;
81   }
82 
83   bool can_disable_monitoring() const {
84     return is_monitoring_ && !monitoring_snapshot_file_;
85   }
86 
87   bool can_get_trace_buffer_percent_full() const {
88     return pending_trace_buffer_percent_full_callback_.is_null();
89   }
90 
91   bool can_cancel_watch_event() const {
92     return !watch_event_callback_.is_null();
93   }
94 
95   // Methods for use by TraceMessageFilter.
96   void AddTraceMessageFilter(TraceMessageFilter* trace_message_filter);
97   void RemoveTraceMessageFilter(TraceMessageFilter* trace_message_filter);
98 
99   void OnTraceDataCollected(
100       const scoped_refptr<base::RefCountedString>& events_str_ptr);
101   void OnMonitoringTraceDataCollected(
102       const scoped_refptr<base::RefCountedString>& events_str_ptr);
103 
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);
112 
113   void OnDisableRecordingAcked(
114       TraceMessageFilter* trace_message_filter,
115       const std::vector<std::string>& known_category_groups);
116   void OnDisableRecordingComplete();
117   void OnResultFileClosed();
118 
119 #if defined(OS_CHROMEOS) || defined(OS_WIN)
120   void OnEndSystemTracingAcked(
121       const scoped_refptr<base::RefCountedString>& events_str_ptr);
122 #endif
123 
124   void OnCaptureMonitoringSnapshotAcked(
125       TraceMessageFilter* trace_message_filter);
126   void OnMonitoringSnapshotFileClosed();
127 
128   void OnTraceBufferPercentFullReply(
129       TraceMessageFilter* trace_message_filter,
130       float percent_full);
131 
132   void OnWatchEventMatched();
133 
134   void SetEnabledOnFileThread(const std::string& category_filter,
135                               int mode,
136                               int options,
137                               const base::Closure& callback);
138   void SetDisabledOnFileThread(const base::Closure& callback);
139   void OnEnableRecordingDone(const std::string& category_filter,
140                              int trace_options,
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,
145                               int trace_options,
146                               const EnableMonitoringDoneCallback& callback);
147   void OnDisableMonitoringDone(const DisableMonitoringDoneCallback& callback);
148 
149   void OnMonitoringStateChanged(bool is_monitoring);
150 
151   TraceMessageFilterSet trace_message_filters_;
152 
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_;
163 
164 #if defined(OS_CHROMEOS) || defined(OS_WIN)
165   bool is_system_tracing_;
166 #endif
167   bool is_recording_;
168   bool is_monitoring_;
169   TracingController::Options options_;
170 
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_;
175 
176   std::string watch_category_name_;
177   std::string watch_event_name_;
178   WatchEventCallback watch_event_callback_;
179 
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);
185 };
186 
187 }  // namespace content
188 
189 #endif  // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_
190