By moving the call to Load() up in SearchProvider::Start(), we are giving a chance...
[chromium-blink-merge.git] / content / browser / trace_controller_impl.cc
blob01542e320481368c9cb737157f722609acaac957
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 "content/browser/trace_controller_impl.h"
7 #include "base/bind.h"
8 #include "base/command_line.h"
9 #include "base/debug/trace_event.h"
10 #include "base/string_number_conversions.h"
11 #include "content/browser/trace_message_filter.h"
12 #include "content/browser/trace_subscriber_stdio.h"
13 #include "content/common/child_process_messages.h"
14 #include "content/public/browser/browser_message_filter.h"
15 #include "content/public/common/content_switches.h"
17 using base::debug::TraceLog;
19 namespace content {
21 namespace {
23 base::LazyInstance<TraceControllerImpl>::Leaky g_controller =
24 LAZY_INSTANCE_INITIALIZER;
26 class AutoStopTraceSubscriberStdio : public TraceSubscriberStdio {
27 public:
28 AutoStopTraceSubscriberStdio(const FilePath& file_path)
29 : TraceSubscriberStdio(file_path) {}
31 static void EndStartupTrace(TraceSubscriberStdio* subscriber) {
32 if (!TraceControllerImpl::GetInstance()->EndTracingAsync(subscriber))
33 delete subscriber;
34 // else, the tracing will end asynchronously in OnEndTracingComplete().
37 virtual void OnEndTracingComplete() {
38 TraceSubscriberStdio::OnEndTracingComplete();
39 delete this;
40 // TODO(joth): this would be the time to automatically open up
41 // chrome://tracing/ and load up the trace data collected.
45 } // namespace
47 TraceController* TraceController::GetInstance() {
48 return TraceControllerImpl::GetInstance();
51 TraceControllerImpl::TraceControllerImpl() :
52 subscriber_(NULL),
53 pending_end_ack_count_(0),
54 pending_bpf_ack_count_(0),
55 maximum_bpf_(0.0f),
56 is_tracing_(false),
57 is_get_categories_(false) {
58 TraceLog::GetInstance()->SetNotificationCallback(
59 base::Bind(&TraceControllerImpl::OnTraceNotification,
60 base::Unretained(this)));
63 TraceControllerImpl::~TraceControllerImpl() {
64 // No need to SetNotificationCallback(nil) on the TraceLog since this is a
65 // Leaky instance.
66 NOTREACHED();
69 TraceControllerImpl* TraceControllerImpl::GetInstance() {
70 return g_controller.Pointer();
73 void TraceControllerImpl::InitStartupTracing(const CommandLine& command_line) {
74 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
75 FilePath trace_file = command_line.GetSwitchValuePath(
76 switches::kTraceStartupFile);
77 // trace_file = "none" means that startup events will show up for the next
78 // begin/end tracing (via about:tracing or AutomationProxy::BeginTracing/
79 // EndTracing, for example).
80 if (trace_file == FilePath().AppendASCII("none"))
81 return;
83 if (trace_file.empty()) {
84 // Default to saving the startup trace into the current dir.
85 trace_file = FilePath().AppendASCII("chrometrace.log");
87 scoped_ptr<AutoStopTraceSubscriberStdio> subscriber(
88 new AutoStopTraceSubscriberStdio(trace_file));
89 DCHECK(can_begin_tracing(subscriber.get()));
91 std::string delay_str = command_line.GetSwitchValueASCII(
92 switches::kTraceStartupDuration);
93 int delay_secs = 5;
94 if (!delay_str.empty() && !base::StringToInt(delay_str, &delay_secs)) {
95 DLOG(WARNING) << "Could not parse --" << switches::kTraceStartupDuration
96 << "=" << delay_str << " defaulting to 5 (secs)";
97 delay_secs = 5;
100 OnTracingBegan(subscriber.get());
101 BrowserThread::PostDelayedTask(
102 BrowserThread::UI,
103 FROM_HERE,
104 base::Bind(&AutoStopTraceSubscriberStdio::EndStartupTrace,
105 base::Unretained(subscriber.release())),
106 base::TimeDelta::FromSeconds(delay_secs));
109 bool TraceControllerImpl::GetKnownCategoriesAsync(TraceSubscriber* subscriber) {
110 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
112 // Known categories come back from child processes with the EndTracingAck
113 // message. So to get known categories, just begin and end tracing immediately
114 // afterwards. This will ping all the child processes for categories.
115 is_get_categories_ = true;
116 bool success = BeginTracing(subscriber, "*") &&
117 EndTracingAsync(subscriber);
118 is_get_categories_ = success;
119 return success;
122 bool TraceControllerImpl::BeginTracing(
123 TraceSubscriber* subscriber,
124 const std::vector<std::string>& included_categories,
125 const std::vector<std::string>& excluded_categories) {
126 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
128 if (!can_begin_tracing(subscriber))
129 return false;
131 // Enable tracing
132 TraceLog::GetInstance()->SetEnabled(included_categories, excluded_categories);
133 OnTracingBegan(subscriber);
135 return true;
138 bool TraceControllerImpl::BeginTracing(TraceSubscriber* subscriber,
139 const std::string& categories) {
140 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
142 if (!can_begin_tracing(subscriber))
143 return false;
145 // Enable tracing
146 TraceLog::GetInstance()->SetEnabled(categories);
148 OnTracingBegan(subscriber);
150 return true;
153 bool TraceControllerImpl::EndTracingAsync(TraceSubscriber* subscriber) {
154 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
156 if (!can_end_tracing() || subscriber != subscriber_)
157 return false;
159 // There could be a case where there are no child processes and filters_
160 // is empty. In that case we can immediately tell the subscriber that tracing
161 // has ended. To avoid recursive calls back to the subscriber, we will just
162 // use the existing asynchronous OnEndTracingAck code.
163 // Count myself (local trace) in pending_end_ack_count_, acked below.
164 pending_end_ack_count_ = filters_.size() + 1;
166 // Handle special case of zero child processes.
167 if (pending_end_ack_count_ == 1) {
168 // Ack asynchronously now, because we don't have any children to wait for.
169 std::vector<std::string> categories;
170 TraceLog::GetInstance()->GetKnownCategories(&categories);
171 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
172 base::Bind(&TraceControllerImpl::OnEndTracingAck,
173 base::Unretained(this), categories));
176 // Notify all child processes.
177 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) {
178 it->get()->SendEndTracing();
181 return true;
184 bool TraceControllerImpl::GetTraceBufferPercentFullAsync(
185 TraceSubscriber* subscriber) {
186 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
188 if (!can_get_buffer_percent_full() || subscriber != subscriber_)
189 return false;
191 maximum_bpf_ = 0.0f;
192 pending_bpf_ack_count_ = filters_.size() + 1;
194 // Handle special case of zero child processes.
195 if (pending_bpf_ack_count_ == 1) {
196 // Ack asynchronously now, because we don't have any children to wait for.
197 float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
198 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
199 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply,
200 base::Unretained(this), bpf));
203 // Message all child processes.
204 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) {
205 it->get()->SendGetTraceBufferPercentFull();
208 return true;
211 bool TraceControllerImpl::SetWatchEvent(TraceSubscriber* subscriber,
212 const std::string& category_name,
213 const std::string& event_name) {
214 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
215 if (subscriber != subscriber_)
216 return false;
218 watch_category_ = category_name;
219 watch_name_ = event_name;
221 TraceLog::GetInstance()->SetWatchEvent(category_name, event_name);
222 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it)
223 it->get()->SendSetWatchEvent(category_name, event_name);
225 return true;
228 bool TraceControllerImpl::CancelWatchEvent(TraceSubscriber* subscriber) {
229 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
230 if (subscriber != subscriber_)
231 return false;
233 watch_category_.clear();
234 watch_name_.clear();
236 TraceLog::GetInstance()->CancelWatchEvent();
237 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it)
238 it->get()->SendCancelWatchEvent();
240 return true;
243 void TraceControllerImpl::CancelSubscriber(TraceSubscriber* subscriber) {
244 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
246 if (subscriber == subscriber_) {
247 subscriber_ = NULL;
248 // End tracing if necessary.
249 if (is_tracing_ && pending_end_ack_count_ == 0)
250 EndTracingAsync(NULL);
254 void TraceControllerImpl::AddFilter(TraceMessageFilter* filter) {
255 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
256 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
257 base::Bind(&TraceControllerImpl::AddFilter, base::Unretained(this),
258 make_scoped_refptr(filter)));
259 return;
262 filters_.insert(filter);
263 if (is_tracing_enabled()) {
264 filter->SendBeginTracing(included_categories_, excluded_categories_);
265 if (!watch_category_.empty())
266 filter->SendSetWatchEvent(watch_category_, watch_name_);
270 void TraceControllerImpl::RemoveFilter(TraceMessageFilter* filter) {
271 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
272 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
273 base::Bind(&TraceControllerImpl::RemoveFilter, base::Unretained(this),
274 make_scoped_refptr(filter)));
275 return;
278 filters_.erase(filter);
281 void TraceControllerImpl::OnTracingBegan(TraceSubscriber* subscriber) {
282 is_tracing_ = true;
284 subscriber_ = subscriber;
286 TraceLog::GetInstance()->GetEnabledTraceCategories(&included_categories_,
287 &excluded_categories_);
288 // Notify all child processes.
289 for (FilterMap::iterator it = filters_.begin(); it != filters_.end(); ++it) {
290 it->get()->SendBeginTracing(included_categories_, excluded_categories_);
294 void TraceControllerImpl::OnEndTracingAck(
295 const std::vector<std::string>& known_categories) {
296 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
297 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
298 base::Bind(&TraceControllerImpl::OnEndTracingAck,
299 base::Unretained(this), known_categories));
300 return;
303 // Merge known_categories with known_categories_
304 known_categories_.insert(known_categories.begin(), known_categories.end());
306 if (pending_end_ack_count_ == 0)
307 return;
309 if (--pending_end_ack_count_ == 0) {
310 // All acks have been received.
311 is_tracing_ = false;
313 // Disable local trace.
314 TraceLog::GetInstance()->SetDisabled();
316 // During this call, our OnTraceDataCollected will be
317 // called with the last of the local trace data. Since we are on the UI
318 // thread, the call to OnTraceDataCollected will be synchronous, so we can
319 // immediately call OnEndTracingComplete below.
320 TraceLog::GetInstance()->Flush(
321 base::Bind(&TraceControllerImpl::OnTraceDataCollected,
322 base::Unretained(this)));
324 // Trigger callback if one is set.
325 if (subscriber_) {
326 if (is_get_categories_)
327 subscriber_->OnKnownCategoriesCollected(known_categories_);
328 else
329 subscriber_->OnEndTracingComplete();
330 // Clear subscriber so that others can use TraceController.
331 subscriber_ = NULL;
334 is_get_categories_ = false;
337 if (pending_end_ack_count_ == 1) {
338 // The last ack represents local trace, so we need to ack it now. Note that
339 // this code only executes if there were child processes.
340 std::vector<std::string> categories;
341 TraceLog::GetInstance()->GetKnownCategories(&categories);
342 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
343 base::Bind(&TraceControllerImpl::OnEndTracingAck,
344 base::Unretained(this), categories));
348 void TraceControllerImpl::OnTraceDataCollected(
349 const scoped_refptr<base::RefCountedString>& events_str_ptr) {
350 // OnTraceDataCollected may be called from any browser thread, either by the
351 // local event trace system or from child processes via TraceMessageFilter.
352 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
353 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
354 base::Bind(&TraceControllerImpl::OnTraceDataCollected,
355 base::Unretained(this), events_str_ptr));
356 return;
359 // Drop trace events if we are just getting categories.
360 if (subscriber_ && !is_get_categories_)
361 subscriber_->OnTraceDataCollected(events_str_ptr);
364 void TraceControllerImpl::OnTraceNotification(int notification) {
365 // OnTraceNotification may be called from any browser thread, either by the
366 // local event trace system or from child processes via TraceMessageFilter.
367 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
368 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
369 base::Bind(&TraceControllerImpl::OnTraceNotification,
370 base::Unretained(this), notification));
371 return;
374 if (notification & base::debug::TraceLog::TRACE_BUFFER_FULL) {
375 // EndTracingAsync may return false if tracing is already in the process
376 // of being ended. That is ok.
377 EndTracingAsync(subscriber_);
379 if (notification & base::debug::TraceLog::EVENT_WATCH_NOTIFICATION) {
380 if (subscriber_)
381 subscriber_->OnEventWatchNotification();
385 void TraceControllerImpl::OnTraceBufferPercentFullReply(float percent_full) {
386 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
387 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
388 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply,
389 base::Unretained(this), percent_full));
390 return;
393 if (pending_bpf_ack_count_ == 0)
394 return;
396 maximum_bpf_ = (maximum_bpf_ > percent_full)? maximum_bpf_ : percent_full;
398 if (--pending_bpf_ack_count_ == 0) {
399 // Trigger callback if one is set.
400 if (subscriber_)
401 subscriber_->OnTraceBufferPercentFullReply(maximum_bpf_);
404 if (pending_bpf_ack_count_ == 1) {
405 // The last ack represents local trace, so we need to ack it now. Note that
406 // this code only executes if there were child processes.
407 float bpf = TraceLog::GetInstance()->GetBufferPercentFull();
408 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
409 base::Bind(&TraceControllerImpl::OnTraceBufferPercentFullReply,
410 base::Unretained(this), bpf));
414 } // namespace content