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 #include "components/tracing/child_trace_message_filter.h"
7 #include "base/debug/trace_event.h"
8 #include "base/message_loop/message_loop_proxy.h"
9 #include "components/tracing/tracing_messages.h"
11 using base::debug::TraceLog;
15 ChildTraceMessageFilter::ChildTraceMessageFilter(
16 base::MessageLoopProxy* ipc_message_loop)
18 ipc_message_loop_(ipc_message_loop) {}
20 void ChildTraceMessageFilter::OnFilterAdded(IPC::Channel* channel) {
22 TraceLog::GetInstance()->SetNotificationCallback(
23 base::Bind(&ChildTraceMessageFilter::OnTraceNotification, this));
24 channel_->Send(new TracingHostMsg_ChildSupportsTracing());
27 void ChildTraceMessageFilter::OnFilterRemoved() {
28 TraceLog::GetInstance()->SetNotificationCallback(
29 TraceLog::NotificationCallback());
32 bool ChildTraceMessageFilter::OnMessageReceived(const IPC::Message& message) {
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)
50 ChildTraceMessageFilter::~ChildTraceMessageFilter() {}
52 void ChildTraceMessageFilter::OnBeginTracing(
53 const std::string& category_filter_str,
54 base::TimeTicks browser_time,
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
61 base::TimeDelta time_offset = base::TimeTicks::NowFromSystemTraceTime() -
63 TraceLog::GetInstance()->SetTimeOffset(time_offset);
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));
76 void ChildTraceMessageFilter::OnEndTracing() {
77 TraceLog::GetInstance()->SetDisabled();
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));
87 void ChildTraceMessageFilter::OnEnableMonitoring(
88 const std::string& category_filter_str,
89 base::TimeTicks browser_time,
91 TraceLog::GetInstance()->SetEnabled(
92 base::debug::CategoryFilter(category_filter_str),
93 static_cast<base::debug::TraceLog::Options>(options));
96 void ChildTraceMessageFilter::OnDisableMonitoring() {
97 TraceLog::GetInstance()->SetDisabled();
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,
111 channel_->Send(new TracingHostMsg_CaptureMonitoringSnapshotAck());
114 void ChildTraceMessageFilter::OnGetTraceBufferPercentFull() {
115 float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
117 channel_->Send(new TracingHostMsg_TraceBufferPercentFullReply(bpf));
120 void ChildTraceMessageFilter::OnSetWatchEvent(const std::string& category_name,
121 const std::string& event_name) {
122 TraceLog::GetInstance()->SetWatchEvent(category_name.c_str(),
126 void ChildTraceMessageFilter::OnCancelWatchEvent() {
127 TraceLog::GetInstance()->CancelWatchEvent();
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));
139 if (events_str_ptr->data().size()) {
140 channel_->Send(new TracingHostMsg_TraceDataCollected(
141 events_str_ptr->data()));
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));
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,
162 channel_->Send(new TracingHostMsg_MonitoringTraceDataCollected(
163 events_str_ptr->data()));
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,
173 channel_->Send(new TracingHostMsg_TraceNotification(notification));
176 } // namespace tracing