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/tracing_controller_impl.h" 9 10 namespace content { 11 12 TraceMessageFilter::TraceMessageFilter() : 13 has_child_(false), 14 is_awaiting_end_ack_(false), 15 is_awaiting_capture_monitoring_snapshot_ack_(false), 16 is_awaiting_buffer_percent_full_ack_(false) { 17 } 18 19 TraceMessageFilter::~TraceMessageFilter() {} 20 21 void TraceMessageFilter::OnChannelClosing() { 22 if (has_child_) { 23 if (is_awaiting_end_ack_) 24 OnEndTracingAck(std::vector<std::string>()); 25 26 if (is_awaiting_capture_monitoring_snapshot_ack_) 27 OnCaptureMonitoringSnapshotAcked(); 28 29 if (is_awaiting_buffer_percent_full_ack_) 30 OnTraceBufferPercentFullReply(0.0f); 31 32 TracingControllerImpl::GetInstance()->RemoveTraceMessageFilter(this); 33 } 34 } 35 36 bool TraceMessageFilter::OnMessageReceived(const IPC::Message& message, 37 bool* message_was_ok) { 38 // Always on IO thread (BrowserMessageFilter guarantee). 39 bool handled = true; 40 IPC_BEGIN_MESSAGE_MAP_EX(TraceMessageFilter, message, *message_was_ok) 41 IPC_MESSAGE_HANDLER(TracingHostMsg_ChildSupportsTracing, 42 OnChildSupportsTracing) 43 IPC_MESSAGE_HANDLER(TracingHostMsg_EndTracingAck, OnEndTracingAck) 44 IPC_MESSAGE_HANDLER(TracingHostMsg_CaptureMonitoringSnapshotAck, 45 OnCaptureMonitoringSnapshotAcked) 46 IPC_MESSAGE_HANDLER(TracingHostMsg_TraceDataCollected, 47 OnTraceDataCollected) 48 IPC_MESSAGE_HANDLER(TracingHostMsg_MonitoringTraceDataCollected, 49 OnMonitoringTraceDataCollected) 50 IPC_MESSAGE_HANDLER(TracingHostMsg_WatchEventMatched, 51 OnWatchEventMatched) 52 IPC_MESSAGE_HANDLER(TracingHostMsg_TraceBufferPercentFullReply, 53 OnTraceBufferPercentFullReply) 54 IPC_MESSAGE_UNHANDLED(handled = false) 55 IPC_END_MESSAGE_MAP_EX() 56 return handled; 57 } 58 59 void TraceMessageFilter::SendBeginTracing( 60 const std::string& category_filter_str, 61 base::debug::TraceLog::Options options) { 62 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 63 Send(new TracingMsg_BeginTracing(category_filter_str, 64 base::TimeTicks::NowFromSystemTraceTime(), 65 options)); 66 } 67 68 void TraceMessageFilter::SendEndTracing() { 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 70 DCHECK(!is_awaiting_end_ack_); 71 is_awaiting_end_ack_ = true; 72 Send(new TracingMsg_EndTracing); 73 } 74 75 void TraceMessageFilter::SendEnableMonitoring( 76 const std::string& category_filter_str, 77 base::debug::TraceLog::Options options) { 78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 79 Send(new TracingMsg_EnableMonitoring(category_filter_str, 80 base::TimeTicks::NowFromSystemTraceTime(), 81 options)); 82 } 83 84 void TraceMessageFilter::SendDisableMonitoring() { 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 86 Send(new TracingMsg_DisableMonitoring); 87 } 88 89 void TraceMessageFilter::SendCaptureMonitoringSnapshot() { 90 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 91 DCHECK(!is_awaiting_capture_monitoring_snapshot_ack_); 92 is_awaiting_capture_monitoring_snapshot_ack_ = true; 93 Send(new TracingMsg_CaptureMonitoringSnapshot); 94 } 95 96 void TraceMessageFilter::SendGetTraceBufferPercentFull() { 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 98 DCHECK(!is_awaiting_buffer_percent_full_ack_); 99 is_awaiting_buffer_percent_full_ack_ = true; 100 Send(new TracingMsg_GetTraceBufferPercentFull); 101 } 102 103 void TraceMessageFilter::SendSetWatchEvent(const std::string& category_name, 104 const std::string& event_name) { 105 Send(new TracingMsg_SetWatchEvent(category_name, event_name)); 106 } 107 108 void TraceMessageFilter::SendCancelWatchEvent() { 109 Send(new TracingMsg_CancelWatchEvent); 110 } 111 112 void TraceMessageFilter::OnChildSupportsTracing() { 113 has_child_ = true; 114 TracingControllerImpl::GetInstance()->AddTraceMessageFilter(this); 115 } 116 117 void TraceMessageFilter::OnEndTracingAck( 118 const std::vector<std::string>& known_categories) { 119 // is_awaiting_end_ack_ should always be true here, but check in case the 120 // child process is compromised. 121 if (is_awaiting_end_ack_) { 122 is_awaiting_end_ack_ = false; 123 TracingControllerImpl::GetInstance()->OnDisableRecordingAcked( 124 known_categories); 125 } else { 126 NOTREACHED(); 127 } 128 } 129 130 void TraceMessageFilter::OnCaptureMonitoringSnapshotAcked() { 131 // is_awaiting_capture_monitoring_snapshot_ack_ should always be true here, 132 // but check in case the child process is compromised. 133 if (is_awaiting_capture_monitoring_snapshot_ack_) { 134 is_awaiting_capture_monitoring_snapshot_ack_ = false; 135 TracingControllerImpl::GetInstance()->OnCaptureMonitoringSnapshotAcked(); 136 } else { 137 NOTREACHED(); 138 } 139 } 140 141 void TraceMessageFilter::OnTraceDataCollected(const std::string& data) { 142 scoped_refptr<base::RefCountedString> data_ptr(new base::RefCountedString()); 143 data_ptr->data() = data; 144 TracingControllerImpl::GetInstance()->OnTraceDataCollected(data_ptr); 145 } 146 147 void TraceMessageFilter::OnMonitoringTraceDataCollected( 148 const std::string& data) { 149 scoped_refptr<base::RefCountedString> data_ptr(new base::RefCountedString()); 150 data_ptr->data() = data; 151 TracingControllerImpl::GetInstance()->OnMonitoringTraceDataCollected( 152 data_ptr); 153 } 154 155 void TraceMessageFilter::OnWatchEventMatched() { 156 TracingControllerImpl::GetInstance()->OnWatchEventMatched(); 157 } 158 159 void TraceMessageFilter::OnTraceBufferPercentFullReply(float percent_full) { 160 if (is_awaiting_buffer_percent_full_ack_) { 161 is_awaiting_buffer_percent_full_ack_ = false; 162 TracingControllerImpl::GetInstance()->OnTraceBufferPercentFullReply( 163 percent_full); 164 } else { 165 NOTREACHED(); 166 } 167 } 168 169 } // namespace content 170