1// Copyright 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
16namespace base {
17class RefCountedString;
18}
19
20namespace content {
21
22class TraceMessageFilter;
23
24class TracingControllerImpl : public TracingController {
25 public:
26  static TracingControllerImpl* GetInstance();
27
28  // TracingController implementation.
29  virtual bool GetCategories(
30      const GetCategoriesDoneCallback& callback) OVERRIDE;
31  virtual bool EnableRecording(
32      const std::string& category_filter,
33      TracingController::Options options,
34      const EnableRecordingDoneCallback& callback) OVERRIDE;
35  virtual bool DisableRecording(
36      const base::FilePath& result_file_path,
37      const TracingFileResultCallback& callback) OVERRIDE;
38  virtual bool EnableMonitoring(const std::string& category_filter,
39      TracingController::Options options,
40      const EnableMonitoringDoneCallback& callback) OVERRIDE;
41  virtual bool DisableMonitoring(
42      const DisableMonitoringDoneCallback& callback) OVERRIDE;
43  virtual void GetMonitoringStatus(
44      bool* out_enabled,
45      std::string* out_category_filter,
46      TracingController::Options* out_options) OVERRIDE;
47  virtual bool CaptureMonitoringSnapshot(
48      const base::FilePath& result_file_path,
49      const TracingFileResultCallback& callback) 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 private:
58  typedef std::set<scoped_refptr<TraceMessageFilter> > TraceMessageFilterMap;
59  class ResultFile;
60
61  friend struct base::DefaultLazyInstanceTraits<TracingControllerImpl>;
62  friend class TraceMessageFilter;
63
64  TracingControllerImpl();
65  virtual ~TracingControllerImpl();
66
67  bool can_enable_recording() const {
68    return !is_recording_;
69  }
70
71  bool can_disable_recording() const {
72    return is_recording_ && !result_file_;
73  }
74
75  bool can_enable_monitoring() const {
76    return !is_monitoring_;
77  }
78
79  bool can_disable_monitoring() const {
80    return is_monitoring_ && !monitoring_snapshot_file_;
81  }
82
83  bool can_get_trace_buffer_percent_full() const {
84    return pending_trace_buffer_percent_full_callback_.is_null();
85  }
86
87  bool can_cancel_watch_event() const {
88    return !watch_event_callback_.is_null();
89  }
90
91  // Methods for use by TraceMessageFilter.
92  void AddTraceMessageFilter(TraceMessageFilter* trace_message_filter);
93  void RemoveTraceMessageFilter(TraceMessageFilter* trace_message_filter);
94
95  void OnTraceDataCollected(
96      const scoped_refptr<base::RefCountedString>& events_str_ptr);
97  void OnMonitoringTraceDataCollected(
98      const scoped_refptr<base::RefCountedString>& events_str_ptr);
99
100  // Callback of TraceLog::Flush() for the local trace.
101  void OnLocalTraceDataCollected(
102      const scoped_refptr<base::RefCountedString>& events_str_ptr,
103      bool has_more_events);
104  // Callback of TraceLog::FlushMonitoring() for the local trace.
105  void OnLocalMonitoringTraceDataCollected(
106      const scoped_refptr<base::RefCountedString>& events_str_ptr,
107      bool has_more_events);
108
109  void OnDisableRecordingAcked(
110      const std::vector<std::string>& known_category_groups);
111  void OnResultFileClosed();
112
113  void OnCaptureMonitoringSnapshotAcked();
114  void OnMonitoringSnapshotFileClosed();
115
116  void OnTraceBufferPercentFullReply(float percent_full);
117
118  void OnWatchEventMatched();
119
120  TraceMessageFilterMap trace_message_filters_;
121  // Pending acks for DisableRecording.
122  int pending_disable_recording_ack_count_;
123  // Pending acks for CaptureMonitoringSnapshot.
124  int pending_capture_monitoring_snapshot_ack_count_;
125  // Pending acks for GetTraceBufferPercentFull.
126  int pending_trace_buffer_percent_full_ack_count_;
127  float maximum_trace_buffer_percent_full_;
128
129  bool is_recording_;
130  bool is_monitoring_;
131
132  GetCategoriesDoneCallback pending_get_categories_done_callback_;
133  TracingFileResultCallback pending_disable_recording_done_callback_;
134  TracingFileResultCallback pending_capture_monitoring_snapshot_done_callback_;
135  GetTraceBufferPercentFullCallback pending_trace_buffer_percent_full_callback_;
136
137  std::string watch_category_name_;
138  std::string watch_event_name_;
139  WatchEventCallback watch_event_callback_;
140
141  std::set<std::string> known_category_groups_;
142  scoped_ptr<ResultFile> result_file_;
143  scoped_ptr<ResultFile> monitoring_snapshot_file_;
144  DISALLOW_COPY_AND_ASSIGN(TracingControllerImpl);
145};
146
147}  // namespace content
148
149#endif  // CONTENT_BROWSER_TRACING_TRACING_CONTROLLER_IMPL_H_
150