Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/tracing/tracing_controller_impl.h" | 5 #include "content/browser/tracing/tracing_controller_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/file_util.h" | 8 #include "base/file_util.h" |
| 9 #include "base/json/string_escape.h" | 9 #include "base/json/string_escape.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "base/threading/thread_restrictions.h" | |
| 11 #include "content/browser/tracing/trace_message_filter.h" | 12 #include "content/browser/tracing/trace_message_filter.h" |
| 12 #include "content/common/child_process_messages.h" | 13 #include "content/common/child_process_messages.h" |
| 13 #include "content/public/browser/browser_message_filter.h" | 14 #include "content/public/browser/browser_message_filter.h" |
| 14 #include "content/public/common/content_switches.h" | 15 #include "content/public/common/content_switches.h" |
| 15 | 16 |
| 16 using base::debug::TraceLog; | 17 using base::debug::TraceLog; |
| 17 | 18 |
| 18 namespace content { | 19 namespace content { |
| 19 | 20 |
| 20 namespace { | 21 namespace { |
| 21 | 22 |
| 22 base::LazyInstance<TracingControllerImpl>::Leaky g_controller = | 23 base::LazyInstance<TracingControllerImpl>::Leaky g_controller = |
| 23 LAZY_INSTANCE_INITIALIZER; | 24 LAZY_INSTANCE_INITIALIZER; |
| 24 | 25 |
| 25 } // namespace | 26 } // namespace |
| 26 | 27 |
| 27 TracingController* TracingController::GetInstance() { | 28 TracingController* TracingController::GetInstance() { |
| 28 return TracingControllerImpl::GetInstance(); | 29 return TracingControllerImpl::GetInstance(); |
| 29 } | 30 } |
| 30 | 31 |
| 32 class TracingControllerImpl::ResultFile { | |
| 33 public: | |
| 34 ResultFile(const base::FilePath& path); | |
| 35 void Write(const scoped_refptr<base::RefCountedString>& events_str_ptr) { | |
| 36 BrowserThread::PostBlockingPoolSequencedTask(sequence_token_, FROM_HERE, | |
| 37 base::Bind(&TracingControllerImpl::ResultFile::WriteTask, | |
| 38 base::Unretained(this), events_str_ptr)); | |
| 39 } | |
| 40 void Close(const base::Closure& callback) { | |
| 41 BrowserThread::PostBlockingPoolSequencedTask(sequence_token_, FROM_HERE, | |
| 42 base::Bind(&TracingControllerImpl::ResultFile::CloseTask, | |
| 43 base::Unretained(this), callback)); | |
| 44 } | |
| 45 const base::FilePath& path() const { return path_; } | |
| 46 | |
| 47 private: | |
| 48 void OpenTask(); | |
| 49 void WriteTask(const scoped_refptr<base::RefCountedString>& events_str_ptr); | |
| 50 void CloseTask(const base::Closure& callback); | |
| 51 | |
| 52 FILE* file_; | |
| 53 std::string sequence_token_; | |
| 54 base::FilePath path_; | |
| 55 bool has_at_least_one_result_; | |
|
dsinclair
2013/11/18 16:32:18
Should this be DISALLOW_COPY_AND_ASSIGN?
Xianzhu
2013/11/18 21:50:39
Done.
| |
| 56 }; | |
| 57 | |
| 58 TracingControllerImpl::ResultFile::ResultFile(const base::FilePath& path) | |
| 59 : file_(NULL), | |
| 60 sequence_token_("trace_result_file"), | |
| 61 path_(path), | |
| 62 has_at_least_one_result_(false) { | |
| 63 BrowserThread::PostBlockingPoolSequencedTask(sequence_token_, FROM_HERE, | |
| 64 base::Bind(&TracingControllerImpl::ResultFile::OpenTask, | |
| 65 base::Unretained(this))); | |
| 66 } | |
| 67 | |
| 68 void TracingControllerImpl::ResultFile::OpenTask() { | |
| 69 if (path_.empty()) | |
| 70 file_util::CreateTemporaryFile(&path_); | |
| 71 file_ = file_util::OpenFile(path_, "w"); | |
| 72 if (!file_) { | |
| 73 LOG(ERROR) << "Failed to open " << path_.value(); | |
| 74 return; | |
| 75 } | |
| 76 const char* preamble = "{\"traceEvents\": ["; | |
| 77 size_t written = fwrite(preamble, strlen(preamble), 1, file_); | |
|
dsinclair
2013/11/18 16:32:18
This (and the DCHECK) are a little weird, we aren'
Xianzhu
2013/11/18 21:50:39
This is original code moved here. According to the
| |
| 78 DCHECK(written == 1); | |
| 79 } | |
| 80 | |
| 81 void TracingControllerImpl::ResultFile::WriteTask( | |
| 82 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | |
| 83 if (!file_) | |
| 84 return; | |
| 85 | |
| 86 // If there is already a result in the file, then put a commma | |
| 87 // before the next batch of results. | |
| 88 if (has_at_least_one_result_) | |
| 89 fwrite(",", 1, 1, file_); | |
| 90 else | |
| 91 has_at_least_one_result_ = true; | |
|
dsinclair
2013/11/18 16:32:18
Don't really need the else as we can always just s
Xianzhu
2013/11/18 21:50:39
Done.
| |
| 92 size_t written = fwrite(events_str_ptr->data().c_str(), | |
| 93 events_str_ptr->data().size(), 1, | |
|
dsinclair
2013/11/18 16:32:18
As above, params seem backwards.
| |
| 94 file_); | |
| 95 DCHECK(written == 1); | |
| 96 } | |
| 97 | |
| 98 void TracingControllerImpl::ResultFile::CloseTask( | |
| 99 const base::Closure& callback) { | |
| 100 if (!file_) | |
| 101 return; | |
| 102 | |
| 103 const char* trailout = "]}"; | |
| 104 size_t written = fwrite(trailout, strlen(trailout), 1, file_); | |
| 105 DCHECK(written == 1); | |
| 106 file_util::CloseFile(file_); | |
| 107 file_ = NULL; | |
| 108 | |
| 109 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, callback); | |
| 110 } | |
| 111 | |
| 112 | |
| 31 TracingControllerImpl::TracingControllerImpl() : | 113 TracingControllerImpl::TracingControllerImpl() : |
| 32 pending_disable_recording_ack_count_(0), | 114 pending_disable_recording_ack_count_(0), |
| 33 pending_capture_monitoring_snapshot_ack_count_(0), | 115 pending_capture_monitoring_snapshot_ack_count_(0), |
| 34 is_recording_(false), | 116 is_recording_(false), |
| 35 is_monitoring_(false), | 117 is_monitoring_(false), |
| 118 trace_options_(TraceLog::RECORD_UNTIL_FULL), | |
| 36 category_filter_( | 119 category_filter_( |
| 37 base::debug::CategoryFilter::kDefaultCategoryFilterString), | 120 base::debug::CategoryFilter::kDefaultCategoryFilterString) { |
| 38 result_file_(0), | |
| 39 result_file_has_at_least_one_result_(false) { | |
| 40 } | 121 } |
| 41 | 122 |
| 42 TracingControllerImpl::~TracingControllerImpl() { | 123 TracingControllerImpl::~TracingControllerImpl() { |
| 43 // This is a Leaky instance. | 124 // This is a Leaky instance. |
| 44 NOTREACHED(); | 125 NOTREACHED(); |
| 45 } | 126 } |
| 46 | 127 |
| 47 TracingControllerImpl* TracingControllerImpl::GetInstance() { | 128 TracingControllerImpl* TracingControllerImpl::GetInstance() { |
| 48 return g_controller.Pointer(); | 129 return g_controller.Pointer(); |
| 49 } | 130 } |
| 50 | 131 |
| 51 void TracingControllerImpl::GetCategories( | 132 void TracingControllerImpl::GetCategories( |
| 52 const GetCategoriesDoneCallback& callback) { | 133 const GetCategoriesDoneCallback& callback) { |
| 53 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 134 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 54 | 135 |
| 55 // Known categories come back from child processes with the EndTracingAck | 136 // Known categories come back from child processes with the EndTracingAck |
| 56 // message. So to get known categories, just begin and end tracing immediately | 137 // message. So to get known categories, just begin and end tracing immediately |
| 57 // afterwards. This will ping all the child processes for categories. | 138 // afterwards. This will ping all the child processes for categories. |
| 58 pending_get_categories_done_callback_ = callback; | 139 pending_get_categories_done_callback_ = callback; |
| 59 EnableRecording(base::debug::CategoryFilter("*"), | 140 EnableRecording(base::debug::CategoryFilter("*"), |
| 60 TracingController::Options(), | 141 TracingController::Options(), |
| 61 EnableRecordingDoneCallback()); | 142 EnableRecordingDoneCallback()); |
| 62 DisableRecording(TracingFileResultCallback()); | 143 DisableRecording(base::FilePath(), TracingFileResultCallback()); |
| 63 } | 144 } |
| 64 | 145 |
| 65 bool TracingControllerImpl::EnableRecording( | 146 bool TracingControllerImpl::EnableRecording( |
| 66 const base::debug::CategoryFilter& filter, | 147 const base::debug::CategoryFilter& filter, |
| 67 TracingController::Options options, | 148 TracingController::Options options, |
| 68 const EnableRecordingDoneCallback& callback) { | 149 const EnableRecordingDoneCallback& callback) { |
| 69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 150 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 70 | 151 |
| 71 if (!can_enable_recording()) | 152 if (!can_enable_recording()) |
| 72 return false; | 153 return false; |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 87 it->get()->SendBeginTracing( | 168 it->get()->SendBeginTracing( |
| 88 category_filter_.ToString(), trace_options_, false); | 169 category_filter_.ToString(), trace_options_, false); |
| 89 } | 170 } |
| 90 | 171 |
| 91 if (!callback.is_null()) | 172 if (!callback.is_null()) |
| 92 callback.Run(); | 173 callback.Run(); |
| 93 return true; | 174 return true; |
| 94 } | 175 } |
| 95 | 176 |
| 96 bool TracingControllerImpl::DisableRecording( | 177 bool TracingControllerImpl::DisableRecording( |
| 178 const base::FilePath& result_file_path, | |
| 97 const TracingFileResultCallback& callback) { | 179 const TracingFileResultCallback& callback) { |
| 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 180 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 99 | 181 |
| 100 if (!can_disable_recording()) | 182 if (!can_disable_recording()) |
| 101 return false; | 183 return false; |
| 102 | 184 |
| 103 pending_disable_recording_done_callback_ = callback; | 185 pending_disable_recording_done_callback_ = callback; |
| 104 | 186 |
| 105 // Disable local trace early to avoid traces during end-tracing process from | 187 // Disable local trace early to avoid traces during end-tracing process from |
| 106 // interfering with the process. | 188 // interfering with the process. |
| 107 TraceLog::GetInstance()->SetDisabled(); | 189 TraceLog::GetInstance()->SetDisabled(); |
| 108 | 190 |
| 109 #if defined(OS_ANDROID) | 191 #if defined(OS_ANDROID) |
| 110 if (pending_get_categories_done_callback_.is_null()) | 192 if (pending_get_categories_done_callback_.is_null()) |
| 111 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); | 193 TraceLog::GetInstance()->AddClockSyncMetadataEvent(); |
| 112 #endif | 194 #endif |
| 113 | 195 |
| 114 // We don't need to create a temporary file when getting categories. | 196 if (!callback.is_null() || !result_file_path.empty()) |
| 115 if (pending_get_categories_done_callback_.is_null()) { | 197 result_file_.reset(new ResultFile(result_file_path)); |
| 116 base::FilePath temporary_file; | |
| 117 file_util::CreateTemporaryFile(&temporary_file); | |
| 118 result_file_path_.reset(new base::FilePath(temporary_file)); | |
| 119 result_file_ = file_util::OpenFile(*result_file_path_, "w"); | |
| 120 result_file_has_at_least_one_result_ = false; | |
| 121 const char* preamble = "{\"traceEvents\": ["; | |
| 122 size_t written = fwrite(preamble, strlen(preamble), 1, result_file_); | |
| 123 DCHECK(written == 1); | |
| 124 } | |
| 125 | 198 |
| 126 // There could be a case where there are no child processes and filters_ | 199 // There could be a case where there are no child processes and filters_ |
| 127 // is empty. In that case we can immediately tell the subscriber that tracing | 200 // is empty. In that case we can immediately tell the subscriber that tracing |
| 128 // has ended. To avoid recursive calls back to the subscriber, we will just | 201 // has ended. To avoid recursive calls back to the subscriber, we will just |
| 129 // use the existing asynchronous OnDisableRecordingAcked code. | 202 // use the existing asynchronous OnDisableRecordingAcked code. |
| 130 // Count myself (local trace) in pending_disable_recording_ack_count_, | 203 // Count myself (local trace) in pending_disable_recording_ack_count_, |
| 131 // acked below. | 204 // acked below. |
| 132 pending_disable_recording_ack_count_ = filters_.size() + 1; | 205 pending_disable_recording_ack_count_ = filters_.size() + 1; |
| 133 | 206 |
| 134 // Handle special case of zero child processes. | 207 // Handle special case of zero child processes. |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 196 } | 269 } |
| 197 | 270 |
| 198 void TracingControllerImpl::GetMonitoringStatus( | 271 void TracingControllerImpl::GetMonitoringStatus( |
| 199 bool* out_enabled, | 272 bool* out_enabled, |
| 200 base::debug::CategoryFilter* out_filter, | 273 base::debug::CategoryFilter* out_filter, |
| 201 TracingController::Options* out_options) { | 274 TracingController::Options* out_options) { |
| 202 NOTIMPLEMENTED(); | 275 NOTIMPLEMENTED(); |
| 203 } | 276 } |
| 204 | 277 |
| 205 void TracingControllerImpl::CaptureMonitoringSnapshot( | 278 void TracingControllerImpl::CaptureMonitoringSnapshot( |
| 279 const base::FilePath& result_file_path, | |
| 206 const TracingFileResultCallback& callback) { | 280 const TracingFileResultCallback& callback) { |
| 207 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 281 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 208 | 282 |
| 209 if (!can_disable_monitoring()) | 283 if (!can_disable_monitoring()) |
| 210 return; | 284 return; |
| 211 | 285 |
| 212 pending_capture_monitoring_snapshot_done_callback_ = callback; | 286 pending_capture_monitoring_snapshot_done_callback_ = callback; |
| 213 | 287 |
| 214 base::FilePath temporary_file; | 288 if (!callback.is_null() || !result_file_path.empty()) |
| 215 file_util::CreateTemporaryFile(&temporary_file); | 289 monitoring_snapshot_file_.reset(new ResultFile(result_file_path)); |
| 216 result_file_path_.reset(new base::FilePath(temporary_file)); | |
| 217 result_file_ = file_util::OpenFile(*result_file_path_, "w"); | |
| 218 result_file_has_at_least_one_result_ = false; | |
| 219 const char* preamble = "{\"traceEvents\": ["; | |
| 220 size_t written = fwrite(preamble, strlen(preamble), 1, result_file_); | |
| 221 DCHECK(written == 1); | |
| 222 | 290 |
| 223 // There could be a case where there are no child processes and filters_ | 291 // There could be a case where there are no child processes and filters_ |
| 224 // is empty. In that case we can immediately tell the subscriber that tracing | 292 // is empty. In that case we can immediately tell the subscriber that tracing |
| 225 // has ended. To avoid recursive calls back to the subscriber, we will just | 293 // has ended. To avoid recursive calls back to the subscriber, we will just |
| 226 // use the existing asynchronous OnCaptureMonitoringSnapshotAcked code. | 294 // use the existing asynchronous OnCaptureMonitoringSnapshotAcked code. |
| 227 // Count myself in pending_capture_monitoring_snapshot_ack_count_, | 295 // Count myself in pending_capture_monitoring_snapshot_ack_count_, |
| 228 // acked below. | 296 // acked below. |
| 229 pending_capture_monitoring_snapshot_ack_count_ = filters_.size() + 1; | 297 pending_capture_monitoring_snapshot_ack_count_ = filters_.size() + 1; |
| 230 | 298 |
| 231 // Handle special case of zero child processes. | 299 // Handle special case of zero child processes. |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 return; | 369 return; |
| 302 | 370 |
| 303 // All acks (including from the subprocesses and the local trace) have been | 371 // All acks (including from the subprocesses and the local trace) have been |
| 304 // received. | 372 // received. |
| 305 is_recording_ = false; | 373 is_recording_ = false; |
| 306 | 374 |
| 307 // Trigger callback if one is set. | 375 // Trigger callback if one is set. |
| 308 if (!pending_get_categories_done_callback_.is_null()) { | 376 if (!pending_get_categories_done_callback_.is_null()) { |
| 309 pending_get_categories_done_callback_.Run(known_category_groups_); | 377 pending_get_categories_done_callback_.Run(known_category_groups_); |
| 310 pending_get_categories_done_callback_.Reset(); | 378 pending_get_categories_done_callback_.Reset(); |
| 311 } else if (!pending_disable_recording_done_callback_.is_null()) { | 379 } else if (result_file_) { |
| 312 const char* trailout = "]}"; | 380 result_file_->Close( |
| 313 size_t written = fwrite(trailout, strlen(trailout), 1, result_file_); | 381 base::Bind(&TracingControllerImpl::OnResultFileClosed, |
| 314 DCHECK(written == 1); | 382 base::Unretained(this))); |
| 315 file_util::CloseFile(result_file_); | |
| 316 result_file_ = 0; | |
| 317 pending_disable_recording_done_callback_.Run(result_file_path_.Pass()); | |
| 318 pending_disable_recording_done_callback_.Reset(); | |
| 319 } | 383 } |
| 320 } | 384 } |
| 321 | 385 |
| 386 void TracingControllerImpl::OnResultFileClosed() { | |
| 387 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 388 | |
| 389 if (!result_file_) | |
| 390 return; | |
| 391 | |
| 392 if (!pending_disable_recording_done_callback_.is_null()) { | |
| 393 pending_disable_recording_done_callback_.Run(result_file_->path()); | |
| 394 pending_disable_recording_done_callback_.Reset(); | |
| 395 } | |
| 396 result_file_.reset(); | |
| 397 } | |
| 398 | |
| 322 void TracingControllerImpl::OnCaptureMonitoringSnapshotAcked() { | 399 void TracingControllerImpl::OnCaptureMonitoringSnapshotAcked() { |
| 323 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 400 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 324 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 401 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 325 base::Bind(&TracingControllerImpl::OnCaptureMonitoringSnapshotAcked, | 402 base::Bind(&TracingControllerImpl::OnCaptureMonitoringSnapshotAcked, |
| 326 base::Unretained(this))); | 403 base::Unretained(this))); |
| 327 return; | 404 return; |
| 328 } | 405 } |
| 329 | 406 |
| 330 if (pending_capture_monitoring_snapshot_ack_count_ == 0) | 407 if (pending_capture_monitoring_snapshot_ack_count_ == 0) |
| 331 return; | 408 return; |
| 332 | 409 |
| 333 if (--pending_capture_monitoring_snapshot_ack_count_ == 1) { | 410 if (--pending_capture_monitoring_snapshot_ack_count_ == 1) { |
| 334 // All acks from subprocesses have been received. Now flush the local trace. | 411 // All acks from subprocesses have been received. Now flush the local trace. |
| 335 // During or after this call, our OnLocalMonitoringTraceDataCollected | 412 // During or after this call, our OnLocalMonitoringTraceDataCollected |
| 336 // will be called with the last of the local trace data. | 413 // will be called with the last of the local trace data. |
| 337 TraceLog::GetInstance()->FlushButLeaveBufferIntact( | 414 TraceLog::GetInstance()->FlushButLeaveBufferIntact( |
| 338 base::Bind(&TracingControllerImpl::OnLocalMonitoringTraceDataCollected, | 415 base::Bind(&TracingControllerImpl::OnLocalMonitoringTraceDataCollected, |
| 339 base::Unretained(this))); | 416 base::Unretained(this))); |
| 340 } | 417 } |
| 341 | 418 |
| 342 if (pending_capture_monitoring_snapshot_ack_count_ != 0) | 419 if (pending_capture_monitoring_snapshot_ack_count_ != 0) |
| 343 return; | 420 return; |
| 344 | 421 |
| 345 if (!pending_capture_monitoring_snapshot_done_callback_.is_null()) { | 422 if (monitoring_snapshot_file_) { |
| 346 const char* trailout = "]}"; | 423 monitoring_snapshot_file_->Close( |
| 347 size_t written = fwrite(trailout, strlen(trailout), 1, result_file_); | 424 base::Bind(&TracingControllerImpl::OnMonitoringSnapshotFileClosed, |
| 348 DCHECK(written == 1); | 425 base::Unretained(this))); |
| 349 file_util::CloseFile(result_file_); | |
| 350 result_file_ = 0; | |
| 351 pending_capture_monitoring_snapshot_done_callback_.Run( | |
| 352 result_file_path_.Pass()); | |
| 353 pending_capture_monitoring_snapshot_done_callback_.Reset(); | |
| 354 } | 426 } |
| 355 } | 427 } |
| 356 | 428 |
| 429 void TracingControllerImpl::OnMonitoringSnapshotFileClosed() { | |
| 430 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 431 | |
| 432 if (!monitoring_snapshot_file_) | |
| 433 return; | |
| 434 | |
| 435 if (!pending_capture_monitoring_snapshot_done_callback_.is_null()) { | |
| 436 pending_capture_monitoring_snapshot_done_callback_.Run( | |
| 437 monitoring_snapshot_file_->path()); | |
| 438 pending_capture_monitoring_snapshot_done_callback_.Reset(); | |
| 439 } | |
| 440 monitoring_snapshot_file_.reset(); | |
| 441 } | |
| 442 | |
| 357 void TracingControllerImpl::OnTraceDataCollected( | 443 void TracingControllerImpl::OnTraceDataCollected( |
| 358 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | 444 const scoped_refptr<base::RefCountedString>& events_str_ptr) { |
| 359 // OnTraceDataCollected may be called from any browser thread, either by the | 445 // OnTraceDataCollected may be called from any browser thread, either by the |
| 360 // local event trace system or from child processes via TraceMessageFilter. | 446 // local event trace system or from child processes via TraceMessageFilter. |
| 361 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 447 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 362 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 448 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, |
| 363 base::Bind(&TracingControllerImpl::OnTraceDataCollected, | 449 base::Bind(&TracingControllerImpl::OnTraceDataCollected, |
| 364 base::Unretained(this), events_str_ptr)); | 450 base::Unretained(this), events_str_ptr)); |
| 365 return; | 451 return; |
| 366 } | 452 } |
| 367 | 453 |
| 368 // Drop trace events if we are just getting categories. | 454 if (result_file_) |
| 369 if (!pending_get_categories_done_callback_.is_null()) | 455 result_file_->Write(events_str_ptr); |
| 456 } | |
| 457 | |
| 458 void TracingControllerImpl::OnMonitoringTraceDataCollected( | |
| 459 const scoped_refptr<base::RefCountedString>& events_str_ptr) { | |
| 460 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | |
| 461 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | |
| 462 base::Bind(&TracingControllerImpl::OnMonitoringTraceDataCollected, | |
| 463 base::Unretained(this), events_str_ptr)); | |
| 370 return; | 464 return; |
| 465 } | |
| 371 | 466 |
| 372 // If there is already a result in the file, then put a commma | 467 if (!monitoring_snapshot_file_) |
| 373 // before the next batch of results. | 468 monitoring_snapshot_file_->Write(events_str_ptr); |
| 374 if (result_file_has_at_least_one_result_) { | |
| 375 size_t written = fwrite(",", 1, 1, result_file_); | |
| 376 DCHECK(written == 1); | |
| 377 } else { | |
| 378 result_file_has_at_least_one_result_ = true; | |
| 379 } | |
| 380 size_t written = fwrite(events_str_ptr->data().c_str(), | |
| 381 events_str_ptr->data().size(), 1, | |
| 382 result_file_); | |
| 383 DCHECK(written == 1); | |
| 384 } | 469 } |
| 385 | 470 |
| 386 void TracingControllerImpl::OnLocalTraceDataCollected( | 471 void TracingControllerImpl::OnLocalTraceDataCollected( |
| 387 const scoped_refptr<base::RefCountedString>& events_str_ptr, | 472 const scoped_refptr<base::RefCountedString>& events_str_ptr, |
| 388 bool has_more_events) { | 473 bool has_more_events) { |
| 389 if (events_str_ptr->data().size()) | 474 if (events_str_ptr->data().size()) |
| 390 OnTraceDataCollected(events_str_ptr); | 475 OnTraceDataCollected(events_str_ptr); |
| 391 | 476 |
| 392 if (has_more_events) | 477 if (has_more_events) |
| 393 return; | 478 return; |
| 394 | 479 |
| 395 // Simulate an DisableRecordingAcked for the local trace. | 480 // Simulate an DisableRecordingAcked for the local trace. |
| 396 std::vector<std::string> category_groups; | 481 std::vector<std::string> category_groups; |
| 397 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); | 482 TraceLog::GetInstance()->GetKnownCategoryGroups(&category_groups); |
| 398 OnDisableRecordingAcked(category_groups); | 483 OnDisableRecordingAcked(category_groups); |
| 399 } | 484 } |
| 400 | 485 |
| 401 void TracingControllerImpl::OnLocalMonitoringTraceDataCollected( | 486 void TracingControllerImpl::OnLocalMonitoringTraceDataCollected( |
| 402 const scoped_refptr<base::RefCountedString>& events_str_ptr, | 487 const scoped_refptr<base::RefCountedString>& events_str_ptr, |
| 403 bool has_more_events) { | 488 bool has_more_events) { |
| 404 if (events_str_ptr->data().size()) | 489 if (events_str_ptr->data().size()) |
| 405 OnTraceDataCollected(events_str_ptr); | 490 OnMonitoringTraceDataCollected(events_str_ptr); |
| 406 | 491 |
| 407 if (has_more_events) | 492 if (has_more_events) |
| 408 return; | 493 return; |
| 409 | 494 |
| 410 // Simulate an CaptureMonitoringSnapshotAcked for the local trace. | 495 // Simulate an CaptureMonitoringSnapshotAcked for the local trace. |
| 411 OnCaptureMonitoringSnapshotAcked(); | 496 OnCaptureMonitoringSnapshotAcked(); |
| 412 } | 497 } |
| 413 | 498 |
| 414 } // namespace content | 499 } // namespace content |
| OLD | NEW |