1 // Copyright (c) 2012 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_TRACE_CONTROLLER_IMPL_H_
6 #define CONTENT_BROWSER_TRACING_TRACE_CONTROLLER_IMPL_H_
12 #include "base/debug/trace_event.h"
13 #include "base/lazy_instance.h"
14 #include "content/public/browser/trace_controller.h"
19 class TraceMessageFilter;
21 class TraceControllerImpl : public TraceController {
23 static TraceControllerImpl* GetInstance();
25 // Called on the main thread of the browser process to initialize
27 void InitStartupTracing(const CommandLine& command_line);
28 bool is_tracing_startup() const { return is_tracing_startup_; }
30 // TraceController implementation:
31 virtual bool BeginTracing(TraceSubscriber* subscriber,
32 const std::string& category_patterns,
33 base::debug::TraceLog::Options options) OVERRIDE;
34 virtual bool EndTracingAsync(TraceSubscriber* subscriber) OVERRIDE;
35 virtual bool GetTraceBufferPercentFullAsync(
36 TraceSubscriber* subscriber) OVERRIDE;
37 virtual bool SetWatchEvent(TraceSubscriber* subscriber,
38 const std::string& category_name,
39 const std::string& event_name) OVERRIDE;
40 virtual bool CancelWatchEvent(TraceSubscriber* subscriber) OVERRIDE;
41 virtual void CancelSubscriber(TraceSubscriber* subscriber) OVERRIDE;
42 virtual bool GetKnownCategoryGroupsAsync(TraceSubscriber* subscriber)
46 typedef std::set<scoped_refptr<TraceMessageFilter> > FilterMap;
48 friend struct base::DefaultLazyInstanceTraits<TraceControllerImpl>;
49 friend class TraceMessageFilter;
51 TraceControllerImpl();
52 virtual ~TraceControllerImpl();
54 bool is_tracing_enabled() const {
55 return can_end_tracing();
58 bool can_end_tracing() const {
59 return is_tracing_ && pending_end_ack_count_ == 0;
62 // Can get Buffer Percent Full
63 bool can_get_buffer_percent_full() const {
65 pending_end_ack_count_ == 0 &&
66 pending_bpf_ack_count_ == 0;
69 bool can_begin_tracing(TraceSubscriber* subscriber) const {
70 return !is_tracing_ &&
71 (subscriber_ == NULL || subscriber == subscriber_);
74 // Methods for use by TraceMessageFilter.
76 void AddFilter(TraceMessageFilter* filter);
77 void RemoveFilter(TraceMessageFilter* filter);
78 void OnTracingBegan(TraceSubscriber* subscriber);
79 void OnEndTracingAck(const std::vector<std::string>& known_category_groups);
80 void OnTraceDataCollected(
81 const scoped_refptr<base::RefCountedString>& events_str_ptr);
82 void OnTraceNotification(int notification);
83 void OnTraceBufferPercentFullReply(float percent_full);
85 // Callback of TraceLog::Flush() for the local trace.
86 void OnLocalTraceDataCollected(
87 const scoped_refptr<base::RefCountedString>& events_str_ptr,
88 bool has_more_events);
91 TraceSubscriber* subscriber_;
92 // Pending acks for EndTracingAsync:
93 int pending_end_ack_count_;
94 // Pending acks for GetTraceBufferPercentFullAsync:
95 int pending_bpf_ack_count_;
98 bool is_tracing_startup_;
99 bool is_get_category_groups_;
100 std::set<std::string> known_category_groups_;
101 std::string watch_category_;
102 std::string watch_name_;
103 base::debug::TraceLog::Options trace_options_;
104 base::debug::CategoryFilter category_filter_;
106 DISALLOW_COPY_AND_ASSIGN(TraceControllerImpl);
109 } // namespace content
111 #endif // CONTENT_BROWSER_TRACING_TRACE_CONTROLLER_IMPL_H_