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"
10 #include "ipc/ipc_channel.h"
12 using base::debug::TraceLog;
16 ChildTraceMessageFilter::ChildTraceMessageFilter(
17 base::MessageLoopProxy* ipc_message_loop)
19 ipc_message_loop_(ipc_message_loop) {}
21 void ChildTraceMessageFilter::OnFilterAdded(IPC::Channel* channel) {
23 channel_->Send(new TracingHostMsg_ChildSupportsTracing());
26 void ChildTraceMessageFilter::OnFilterRemoved() {
30 bool ChildTraceMessageFilter::OnMessageReceived(const IPC::Message& message) {
32 IPC_BEGIN_MESSAGE_MAP(ChildTraceMessageFilter, message)
33 IPC_MESSAGE_HANDLER(TracingMsg_BeginTracing, OnBeginTracing)
34 IPC_MESSAGE_HANDLER(TracingMsg_EndTracing, OnEndTracing)
35 IPC_MESSAGE_HANDLER(TracingMsg_EnableMonitoring, OnEnableMonitoring)
36 IPC_MESSAGE_HANDLER(TracingMsg_DisableMonitoring, OnDisableMonitoring)
37 IPC_MESSAGE_HANDLER(TracingMsg_CaptureMonitoringSnapshot,
38 OnCaptureMonitoringSnapshot)
39 IPC_MESSAGE_HANDLER(TracingMsg_GetTraceBufferPercentFull,
40 OnGetTraceBufferPercentFull)
41 IPC_MESSAGE_HANDLER(TracingMsg_SetWatchEvent, OnSetWatchEvent)
42 IPC_MESSAGE_HANDLER(TracingMsg_CancelWatchEvent, OnCancelWatchEvent)
43 IPC_MESSAGE_UNHANDLED(handled = false)
48 ChildTraceMessageFilter::~ChildTraceMessageFilter() {}
50 void ChildTraceMessageFilter::OnBeginTracing(
51 const std::string& category_filter_str,
52 base::TimeTicks browser_time,
54 #if defined(__native_client__)
55 // NaCl and system times are offset by a bit, so subtract some time from
56 // the captured timestamps. The value might be off by a bit due to messaging
58 base::TimeDelta time_offset = base::TimeTicks::NowFromSystemTraceTime() -
60 TraceLog::GetInstance()->SetTimeOffset(time_offset);
63 TraceLog::GetInstance()->SetEnabled(
64 base::debug::CategoryFilter(category_filter_str),
65 base::debug::TraceLog::RECORDING_MODE,
66 static_cast<base::debug::TraceLog::Options>(options));
69 void ChildTraceMessageFilter::OnEndTracing() {
70 TraceLog::GetInstance()->SetDisabled();
72 // Flush will generate one or more callbacks to OnTraceDataCollected
73 // synchronously or asynchronously. EndTracingAck will be sent in the last
74 // OnTraceDataCollected. We are already on the IO thread, so the
75 // OnTraceDataCollected calls will not be deferred.
76 TraceLog::GetInstance()->Flush(
77 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this));
80 void ChildTraceMessageFilter::OnEnableMonitoring(
81 const std::string& category_filter_str,
82 base::TimeTicks browser_time,
84 TraceLog::GetInstance()->SetEnabled(
85 base::debug::CategoryFilter(category_filter_str),
86 base::debug::TraceLog::MONITORING_MODE,
87 static_cast<base::debug::TraceLog::Options>(options));
90 void ChildTraceMessageFilter::OnDisableMonitoring() {
91 TraceLog::GetInstance()->SetDisabled();
94 void ChildTraceMessageFilter::OnCaptureMonitoringSnapshot() {
95 // Flush will generate one or more callbacks to
96 // OnMonitoringTraceDataCollected. It's important that the last
97 // OnMonitoringTraceDataCollected gets called before
98 // CaptureMonitoringSnapshotAck below. We are already on the IO thread,
99 // so the OnMonitoringTraceDataCollected calls will not be deferred.
100 TraceLog::GetInstance()->FlushButLeaveBufferIntact(
101 base::Bind(&ChildTraceMessageFilter::OnMonitoringTraceDataCollected,
105 void ChildTraceMessageFilter::OnGetTraceBufferPercentFull() {
106 float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
108 channel_->Send(new TracingHostMsg_TraceBufferPercentFullReply(bpf));
111 void ChildTraceMessageFilter::OnSetWatchEvent(const std::string& category_name,
112 const std::string& event_name) {
113 TraceLog::GetInstance()->SetWatchEvent(
114 category_name, event_name,
115 base::Bind(&ChildTraceMessageFilter::OnWatchEventMatched, this));
118 void ChildTraceMessageFilter::OnCancelWatchEvent() {
119 TraceLog::GetInstance()->CancelWatchEvent();
122 void ChildTraceMessageFilter::OnWatchEventMatched() {
123 if (!ipc_message_loop_->BelongsToCurrentThread()) {
124 ipc_message_loop_->PostTask(FROM_HERE,
125 base::Bind(&ChildTraceMessageFilter::OnWatchEventMatched, this));
128 channel_->Send(new TracingHostMsg_WatchEventMatched);
131 void ChildTraceMessageFilter::OnTraceDataCollected(
132 const scoped_refptr<base::RefCountedString>& events_str_ptr,
133 bool has_more_events) {
134 if (!ipc_message_loop_->BelongsToCurrentThread()) {
135 ipc_message_loop_->PostTask(FROM_HERE,
136 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this,
137 events_str_ptr, has_more_events));
140 if (events_str_ptr->data().size()) {
141 channel_->Send(new TracingHostMsg_TraceDataCollected(
142 events_str_ptr->data()));
144 if (!has_more_events) {
145 std::vector<std::string> category_groups;
146 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups);
147 channel_->Send(new TracingHostMsg_EndTracingAck(category_groups));
151 void ChildTraceMessageFilter::OnMonitoringTraceDataCollected(
152 const scoped_refptr<base::RefCountedString>& events_str_ptr,
153 bool has_more_events) {
154 if (!ipc_message_loop_->BelongsToCurrentThread()) {
155 ipc_message_loop_->PostTask(FROM_HERE,
156 base::Bind(&ChildTraceMessageFilter::
157 OnMonitoringTraceDataCollected,
163 channel_->Send(new TracingHostMsg_MonitoringTraceDataCollected(
164 events_str_ptr->data()));
166 if (!has_more_events)
167 channel_->Send(new TracingHostMsg_CaptureMonitoringSnapshotAck());
170 } // namespace tracing