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_GetTraceBufferPercentFull, 38 OnGetTraceBufferPercentFull) 39 IPC_MESSAGE_HANDLER(TracingMsg_SetWatchEvent, OnSetWatchEvent) 40 IPC_MESSAGE_HANDLER(TracingMsg_CancelWatchEvent, OnCancelWatchEvent) 41 IPC_MESSAGE_UNHANDLED(handled = false) 42 IPC_END_MESSAGE_MAP() 43 return handled; 44 } 45 46 ChildTraceMessageFilter::~ChildTraceMessageFilter() {} 47 48 void ChildTraceMessageFilter::OnBeginTracing( 49 const std::string& category_filter_str, 50 base::TimeTicks browser_time, 51 int options) { 52 #if defined(__native_client__) 53 // NaCl and system times are offset by a bit, so subtract some time from 54 // the captured timestamps. The value might be off by a bit due to messaging 55 // latency. 56 base::TimeDelta time_offset = base::TimeTicks::NowFromSystemTraceTime() - 57 browser_time; 58 TraceLog::GetInstance()->SetTimeOffset(time_offset); 59 #endif 60 TraceLog::GetInstance()->SetEnabled( 61 base::debug::CategoryFilter(category_filter_str), 62 static_cast<base::debug::TraceLog::Options>(options)); 63 } 64 65 void ChildTraceMessageFilter::OnEndTracing() { 66 TraceLog::GetInstance()->SetDisabled(); 67 68 // Flush will generate one or more callbacks to OnTraceDataCollected. It's 69 // important that the last OnTraceDataCollected gets called before 70 // EndTracingAck below. We are already on the IO thread, so the 71 // OnTraceDataCollected calls will not be deferred. 72 TraceLog::GetInstance()->Flush( 73 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this)); 74 75 std::vector<std::string> category_groups; 76 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); 77 channel_->Send(new TracingHostMsg_EndTracingAck(category_groups)); 78 } 79 80 void ChildTraceMessageFilter::OnGetTraceBufferPercentFull() { 81 float bpf = TraceLog::GetInstance()->GetBufferPercentFull(); 82 83 channel_->Send(new TracingHostMsg_TraceBufferPercentFullReply(bpf)); 84 } 85 86 void ChildTraceMessageFilter::OnSetWatchEvent(const std::string& category_name, 87 const std::string& event_name) { 88 TraceLog::GetInstance()->SetWatchEvent(category_name.c_str(), 89 event_name.c_str()); 90 } 91 92 void ChildTraceMessageFilter::OnCancelWatchEvent() { 93 TraceLog::GetInstance()->CancelWatchEvent(); 94 } 95 96 void ChildTraceMessageFilter::OnTraceDataCollected( 97 const scoped_refptr<base::RefCountedString>& events_str_ptr) { 98 if (!ipc_message_loop_->BelongsToCurrentThread()) { 99 ipc_message_loop_->PostTask(FROM_HERE, 100 base::Bind(&ChildTraceMessageFilter::OnTraceDataCollected, this, 101 events_str_ptr)); 102 return; 103 } 104 channel_->Send(new TracingHostMsg_TraceDataCollected( 105 events_str_ptr->data())); 106 } 107 108 void ChildTraceMessageFilter::OnTraceNotification(int notification) { 109 if (!ipc_message_loop_->BelongsToCurrentThread()) { 110 ipc_message_loop_->PostTask(FROM_HERE, 111 base::Bind(&ChildTraceMessageFilter::OnTraceNotification, this, 112 notification)); 113 return; 114 } 115 channel_->Send(new TracingHostMsg_TraceNotification(notification)); 116 } 117 118 } // namespace tracing 119