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 "content/browser/tracing/trace_message_filter.h" 6 7 #include "components/tracing/tracing_messages.h" 8 #include "content/browser/tracing/trace_controller_impl.h" 9 10 namespace content { 11 12 TraceMessageFilter::TraceMessageFilter() : 13 has_child_(false), 14 is_awaiting_end_ack_(false), 15 is_awaiting_buffer_percent_full_ack_(false) { 16 } 17 18 void TraceMessageFilter::OnFilterAdded(IPC::Channel* channel) { 19 // Always on IO thread (BrowserMessageFilter guarantee). 20 BrowserMessageFilter::OnFilterAdded(channel); 21 } 22 23 void TraceMessageFilter::OnChannelClosing() { 24 // Always on IO thread (BrowserMessageFilter guarantee). 25 BrowserMessageFilter::OnChannelClosing(); 26 27 if (has_child_) { 28 if (is_awaiting_end_ack_) 29 OnEndTracingAck(std::vector<std::string>()); 30 31 if (is_awaiting_buffer_percent_full_ack_) 32 OnTraceBufferPercentFullReply(0.0f); 33 34 TraceControllerImpl::GetInstance()->RemoveFilter(this); 35 } 36 } 37 38 bool TraceMessageFilter::OnMessageReceived(const IPC::Message& message, 39 bool* message_was_ok) { 40 // Always on IO thread (BrowserMessageFilter guarantee). 41 bool handled = true; 42 IPC_BEGIN_MESSAGE_MAP_EX(TraceMessageFilter, message, *message_was_ok) 43 IPC_MESSAGE_HANDLER(TracingHostMsg_ChildSupportsTracing, 44 OnChildSupportsTracing) 45 IPC_MESSAGE_HANDLER(TracingHostMsg_EndTracingAck, OnEndTracingAck) 46 IPC_MESSAGE_HANDLER(TracingHostMsg_TraceDataCollected, 47 OnTraceDataCollected) 48 IPC_MESSAGE_HANDLER(TracingHostMsg_TraceNotification, 49 OnTraceNotification) 50 IPC_MESSAGE_HANDLER(TracingHostMsg_TraceBufferPercentFullReply, 51 OnTraceBufferPercentFullReply) 52 IPC_MESSAGE_UNHANDLED(handled = false) 53 IPC_END_MESSAGE_MAP_EX() 54 return handled; 55 } 56 57 void TraceMessageFilter::SendBeginTracing( 58 const std::string& category_filter_str, 59 base::debug::TraceLog::Options options) { 60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 61 Send(new TracingMsg_BeginTracing(category_filter_str, 62 base::TimeTicks::NowFromSystemTraceTime(), 63 options)); 64 } 65 66 void TraceMessageFilter::SendEndTracing() { 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 68 DCHECK(!is_awaiting_end_ack_); 69 is_awaiting_end_ack_ = true; 70 Send(new TracingMsg_EndTracing); 71 } 72 73 void TraceMessageFilter::SendGetTraceBufferPercentFull() { 74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 75 DCHECK(!is_awaiting_buffer_percent_full_ack_); 76 is_awaiting_buffer_percent_full_ack_ = true; 77 Send(new TracingMsg_GetTraceBufferPercentFull); 78 } 79 80 void TraceMessageFilter::SendSetWatchEvent(const std::string& category_name, 81 const std::string& event_name) { 82 Send(new TracingMsg_SetWatchEvent(category_name, event_name)); 83 } 84 85 void TraceMessageFilter::SendCancelWatchEvent() { 86 Send(new TracingMsg_CancelWatchEvent); 87 } 88 89 TraceMessageFilter::~TraceMessageFilter() {} 90 91 void TraceMessageFilter::OnChildSupportsTracing() { 92 has_child_ = true; 93 TraceControllerImpl::GetInstance()->AddFilter(this); 94 } 95 96 void TraceMessageFilter::OnEndTracingAck( 97 const std::vector<std::string>& known_categories) { 98 // is_awaiting_end_ack_ should always be true here, but check in case the 99 // child process is compromised. 100 if (is_awaiting_end_ack_) { 101 is_awaiting_end_ack_ = false; 102 TraceControllerImpl::GetInstance()->OnEndTracingAck(known_categories); 103 } else { 104 NOTREACHED(); 105 } 106 } 107 108 void TraceMessageFilter::OnTraceDataCollected(const std::string& data) { 109 scoped_refptr<base::RefCountedString> data_ptr(new base::RefCountedString()); 110 data_ptr->data() = data; 111 TraceControllerImpl::GetInstance()->OnTraceDataCollected(data_ptr); 112 } 113 114 void TraceMessageFilter::OnTraceNotification(int notification) { 115 TraceControllerImpl::GetInstance()->OnTraceNotification(notification); 116 } 117 118 void TraceMessageFilter::OnTraceBufferPercentFullReply(float percent_full) { 119 if (is_awaiting_buffer_percent_full_ack_) { 120 is_awaiting_buffer_percent_full_ack_ = false; 121 TraceControllerImpl::GetInstance()->OnTraceBufferPercentFullReply( 122 percent_full); 123 } else { 124 NOTREACHED(); 125 } 126 } 127 128 } // namespace content 129