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 channel_->Send(new TracingHostMsg_ChildSupportsTracing());
25 void ChildTraceMessageFilter::OnFilterRemoved() {
29 bool ChildTraceMessageFilter::OnMessageReceived(const IPC::Message& message) {
31 IPC_BEGIN_MESSAGE_MAP(ChildTraceMessageFilter, message)
32 IPC_MESSAGE_HANDLER(TracingMsg_BeginTracing, OnBeginTracing)
33 IPC_MESSAGE_HANDLER(TracingMsg_EndTracing, OnEndTracing)
34 IPC_MESSAGE_HANDLER(TracingMsg_EnableMonitoring, OnEnableMonitoring)
35 IPC_MESSAGE_HANDLER(TracingMsg_DisableMonitoring, OnDisableMonitoring)
36 IPC_MESSAGE_HANDLER(TracingMsg_CaptureMonitoringSnapshot,
37 OnCaptureMonitoringSnapshot)
38 IPC_MESSAGE_HANDLER(TracingMsg_GetTraceBufferPercentFull,
39 OnGetTraceBufferPercentFull)
40 IPC_MESSAGE_HANDLER(TracingMsg_SetWatchEvent, OnSetWatchEvent)
41 IPC_MESSAGE_HANDLER(TracingMsg_CancelWatchEvent, OnCancelWatchEvent)
42 IPC_MESSAGE_UNHANDLED(handled = false)
47 ChildTraceMessageFilter::~ChildTraceMessageFilter() {}
49 void ChildTraceMessageFilter::OnBeginTracing(
50 const std::string& category_filter_str,
51 base::TimeTicks browser_time,
53 #if defined(__native_client__)
54 // NaCl and system times are offset by a bit, so subtract some time from
55 // the captured timestamps. The value might be off by a bit due to messaging
57 base::TimeDelta time_offset = base::TimeTicks::NowFromSystemTraceTime() -
59 TraceLog::GetInstance()->SetTimeOffset(time_offset);
62 TraceLog::GetInstance()->SetEnabled(
63 base::debug::CategoryFilter(category_filter_str),
64 base::debug::TraceLog::RECORDING_MODE,
65 static_cast<base::debug::TraceLog::Options>(options));
68 void ChildTraceMessageFilter::OnEndTracing() {
69 TraceLog::GetInstance()->SetDisabled();
71 // Flush will generate one or more callbacks to OnTraceDataCollected
72 // synchronously or asynchronously. EndTracingAck will be sent in the last
73 // OnTraceDataCollected. We are already on the IO thread, so the
74 // OnTraceDataCollected calls will not be deferred.
75 TraceLog::GetInstance()->Flush(
76 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this));
79 void ChildTraceMessageFilter::OnEnableMonitoring(
80 const std::string& category_filter_str,
81 base::TimeTicks browser_time,
83 TraceLog::GetInstance()->SetEnabled(
84 base::debug::CategoryFilter(category_filter_str),
85 base::debug::TraceLog::MONITORING_MODE,
86 static_cast<base::debug::TraceLog::Options>(options));
89 void ChildTraceMessageFilter::OnDisableMonitoring() {
90 TraceLog::GetInstance()->SetDisabled();
93 void ChildTraceMessageFilter::OnCaptureMonitoringSnapshot() {
94 // Flush will generate one or more callbacks to
95 // OnMonitoringTraceDataCollected. It's important that the last
96 // OnMonitoringTraceDataCollected gets called before
97 // CaptureMonitoringSnapshotAck below. We are already on the IO thread,
98 // so the OnMonitoringTraceDataCollected calls will not be deferred.
99 TraceLog::GetInstance()->FlushButLeaveBufferIntact(
100 base::Bind(&ChildTraceMessageFilter::OnMonitoringTraceDataCollected,
104 void ChildTraceMessageFilter::OnGetTraceBufferPercentFull() {
105 float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
107 channel_->Send(new TracingHostMsg_TraceBufferPercentFullReply(bpf));
110 void ChildTraceMessageFilter::OnSetWatchEvent(const std::string& category_name,
111 const std::string& event_name) {
112 TraceLog::GetInstance()->SetWatchEvent(
113 category_name, event_name,
114 base::Bind(&ChildTraceMessageFilter::OnWatchEventMatched, this));
117 void ChildTraceMessageFilter::OnCancelWatchEvent() {
118 TraceLog::GetInstance()->CancelWatchEvent();
121 void ChildTraceMessageFilter::OnWatchEventMatched() {
122 if (!ipc_message_loop_->BelongsToCurrentThread()) {
123 ipc_message_loop_->PostTask(FROM_HERE,
124 base::Bind(&ChildTraceMessageFilter::OnWatchEventMatched, this));
127 channel_->Send(new TracingHostMsg_WatchEventMatched);
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()));
165 if (!has_more_events)
166 channel_->Send(new TracingHostMsg_CaptureMonitoringSnapshotAck());
169 } // namespace tracing