- add sources.
[platform/framework/web/crosswalk.git] / src / content / browser / tracing / trace_controller_impl.h
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.
4
5 #ifndef CONTENT_BROWSER_TRACING_TRACE_CONTROLLER_IMPL_H_
6 #define CONTENT_BROWSER_TRACING_TRACE_CONTROLLER_IMPL_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
12 #include "base/debug/trace_event.h"
13 #include "base/lazy_instance.h"
14 #include "content/public/browser/trace_controller.h"
15
16 class CommandLine;
17
18 namespace content {
19 class TraceMessageFilter;
20
21 class TraceControllerImpl : public TraceController {
22  public:
23   static TraceControllerImpl* GetInstance();
24
25   // Called on the main thread of the browser process to initialize
26   // startup tracing.
27   void InitStartupTracing(const CommandLine& command_line);
28   bool is_tracing_startup() const { return is_tracing_startup_; }
29
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)
43       OVERRIDE;
44
45  private:
46   typedef std::set<scoped_refptr<TraceMessageFilter> > FilterMap;
47
48   friend struct base::DefaultLazyInstanceTraits<TraceControllerImpl>;
49   friend class TraceMessageFilter;
50
51   TraceControllerImpl();
52   virtual ~TraceControllerImpl();
53
54   bool is_tracing_enabled() const {
55     return can_end_tracing();
56   }
57
58   bool can_end_tracing() const {
59     return is_tracing_ && pending_end_ack_count_ == 0;
60   }
61
62   // Can get Buffer Percent Full
63   bool can_get_buffer_percent_full() const {
64     return is_tracing_ &&
65         pending_end_ack_count_ == 0 &&
66         pending_bpf_ack_count_ == 0;
67   }
68
69   bool can_begin_tracing(TraceSubscriber* subscriber) const {
70     return !is_tracing_ &&
71         (subscriber_ == NULL || subscriber == subscriber_);
72   }
73
74   // Methods for use by TraceMessageFilter.
75
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);
84
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);
89
90   FilterMap filters_;
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_;
96   float maximum_bpf_;
97   bool is_tracing_;
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_;
105
106   DISALLOW_COPY_AND_ASSIGN(TraceControllerImpl);
107 };
108
109 }  // namespace content
110
111 #endif  // CONTENT_BROWSER_TRACING_TRACE_CONTROLLER_IMPL_H_