- add sources.
[platform/framework/web/crosswalk.git] / src / components / tracing / child_trace_message_filter.cc
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 #include "components/tracing/child_trace_message_filter.h"
6
7 #include "base/debug/trace_event.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "components/tracing/tracing_messages.h"
10
11 using base::debug::TraceLog;
12
13 namespace tracing {
14
15 ChildTraceMessageFilter::ChildTraceMessageFilter(
16     base::MessageLoopProxy* ipc_message_loop)
17     : channel_(NULL),
18       ipc_message_loop_(ipc_message_loop) {}
19
20 void ChildTraceMessageFilter::OnFilterAdded(IPC::Channel* channel) {
21   channel_ = channel;
22   TraceLog::GetInstance()->SetNotificationCallback(
23       base::Bind(&ChildTraceMessageFilter::OnTraceNotification, this));
24   channel_->Send(new TracingHostMsg_ChildSupportsTracing());
25 }
26
27 void ChildTraceMessageFilter::OnFilterRemoved() {
28   TraceLog::GetInstance()->SetNotificationCallback(
29       TraceLog::NotificationCallback());
30 }
31
32 bool ChildTraceMessageFilter::OnMessageReceived(const IPC::Message& message) {
33   bool handled = true;
34   IPC_BEGIN_MESSAGE_MAP(ChildTraceMessageFilter, message)
35     IPC_MESSAGE_HANDLER(TracingMsg_BeginTracing, OnBeginTracing)
36     IPC_MESSAGE_HANDLER(TracingMsg_EndTracing, OnEndTracing)
37     IPC_MESSAGE_HANDLER(TracingMsg_EnableMonitoring, OnEnableMonitoring)
38     IPC_MESSAGE_HANDLER(TracingMsg_DisableMonitoring, OnDisableMonitoring)
39     IPC_MESSAGE_HANDLER(TracingMsg_CaptureMonitoringSnapshot,
40                         OnCaptureMonitoringSnapshot)
41     IPC_MESSAGE_HANDLER(TracingMsg_GetTraceBufferPercentFull,
42                         OnGetTraceBufferPercentFull)
43     IPC_MESSAGE_HANDLER(TracingMsg_SetWatchEvent, OnSetWatchEvent)
44     IPC_MESSAGE_HANDLER(TracingMsg_CancelWatchEvent, OnCancelWatchEvent)
45     IPC_MESSAGE_UNHANDLED(handled = false)
46   IPC_END_MESSAGE_MAP()
47   return handled;
48 }
49
50 ChildTraceMessageFilter::~ChildTraceMessageFilter() {}
51
52 void ChildTraceMessageFilter::OnBeginTracing(
53     const std::string& category_filter_str,
54     base::TimeTicks browser_time,
55     int options,
56     bool tracing_startup) {
57 #if defined(__native_client__)
58   // NaCl and system times are offset by a bit, so subtract some time from
59   // the captured timestamps. The value might be off by a bit due to messaging
60   // latency.
61   base::TimeDelta time_offset = base::TimeTicks::NowFromSystemTraceTime() -
62       browser_time;
63   TraceLog::GetInstance()->SetTimeOffset(time_offset);
64 #endif
65
66   // Some subprocesses handle --trace-startup by themselves to begin
67   // trace as early as possible. Don't start twice, otherwise the trace
68   // buffer can't be correctly flushed on the end of startup tracing.
69   if (!tracing_startup || !TraceLog::GetInstance()->IsEnabled()) {
70     TraceLog::GetInstance()->SetEnabled(
71         base::debug::CategoryFilter(category_filter_str),
72         static_cast<base::debug::TraceLog::Options>(options));
73   }
74 }
75
76 void ChildTraceMessageFilter::OnEndTracing() {
77   TraceLog::GetInstance()->SetDisabled();
78
79   // Flush will generate one or more callbacks to OnTraceDataCollected
80   // synchronously or asynchronously. EndTracingAck will be sent in the last
81   // OnTraceDataCollected. We are already on the IO thread, so the
82   // OnTraceDataCollected calls will not be deferred.
83   TraceLog::GetInstance()->Flush(
84       base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this));
85 }
86
87 void ChildTraceMessageFilter::OnEnableMonitoring(
88     const std::string& category_filter_str,
89     base::TimeTicks browser_time,
90     int options) {
91   TraceLog::GetInstance()->SetEnabled(
92       base::debug::CategoryFilter(category_filter_str),
93       static_cast<base::debug::TraceLog::Options>(options));
94 }
95
96 void ChildTraceMessageFilter::OnDisableMonitoring() {
97   TraceLog::GetInstance()->SetDisabled();
98 }
99
100 void ChildTraceMessageFilter::OnCaptureMonitoringSnapshot() {
101   // Flush will generate one or more callbacks to
102   // OnMonitoringTraceDataCollected. It's important that the last
103   // OnMonitoringTraceDataCollected gets called before
104   // CaptureMonitoringSnapshotAck below. We are already on the IO thread,
105   // so the OnMonitoringTraceDataCollected calls will not be deferred.
106   TraceLog::GetInstance()->FlushButLeaveBufferIntact(
107       base::Bind(&ChildTraceMessageFilter::
108                  OnMonitoringTraceDataCollected,
109                  this));
110
111   channel_->Send(new TracingHostMsg_CaptureMonitoringSnapshotAck());
112 }
113
114 void ChildTraceMessageFilter::OnGetTraceBufferPercentFull() {
115   float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
116
117   channel_->Send(new TracingHostMsg_TraceBufferPercentFullReply(bpf));
118 }
119
120 void ChildTraceMessageFilter::OnSetWatchEvent(const std::string& category_name,
121                                               const std::string& event_name) {
122   TraceLog::GetInstance()->SetWatchEvent(category_name.c_str(),
123                                          event_name.c_str());
124 }
125
126 void ChildTraceMessageFilter::OnCancelWatchEvent() {
127   TraceLog::GetInstance()->CancelWatchEvent();
128 }
129
130 void ChildTraceMessageFilter::OnTraceDataCollected(
131     const scoped_refptr<base::RefCountedString>& events_str_ptr,
132     bool has_more_events) {
133   if (!ipc_message_loop_->BelongsToCurrentThread()) {
134     ipc_message_loop_->PostTask(FROM_HERE,
135         base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this,
136                    events_str_ptr, has_more_events));
137     return;
138   }
139   if (events_str_ptr->data().size()) {
140     channel_->Send(new TracingHostMsg_TraceDataCollected(
141         events_str_ptr->data()));
142   }
143   if (!has_more_events) {
144     std::vector<std::string> category_groups;
145     TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups);
146     channel_->Send(new TracingHostMsg_EndTracingAck(category_groups));
147   }
148 }
149
150 void ChildTraceMessageFilter::OnMonitoringTraceDataCollected(
151      const scoped_refptr<base::RefCountedString>& events_str_ptr,
152      bool has_more_events) {
153   if (!ipc_message_loop_->BelongsToCurrentThread()) {
154     ipc_message_loop_->PostTask(FROM_HERE,
155         base::Bind(&ChildTraceMessageFilter::
156                    OnMonitoringTraceDataCollected,
157                    this,
158                    events_str_ptr,
159                    has_more_events));
160     return;
161   }
162   channel_->Send(new TracingHostMsg_MonitoringTraceDataCollected(
163       events_str_ptr->data()));
164 }
165
166 void ChildTraceMessageFilter::OnTraceNotification(int notification) {
167   if (!ipc_message_loop_->BelongsToCurrentThread()) {
168     ipc_message_loop_->PostTask(FROM_HERE,
169         base::Bind(&ChildTraceMessageFilter::OnTraceNotification, this,
170                    notification));
171     return;
172   }
173   channel_->Send(new TracingHostMsg_TraceNotification(notification));
174 }
175
176 }  // namespace tracing