Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "mojo/common/trace_provider_impl.h" | 5 #include "mojo/common/trace_provider_impl.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/trace_event/trace_config.h" | 8 #include "base/trace_event/trace_config.h" |
| 9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
| 10 #include "mojo/public/cpp/application/application_connection.h" | 10 #include "mojo/public/cpp/application/application_connection.h" |
| 11 #include "mojo/public/cpp/application/application_impl.h" | 11 #include "mojo/public/cpp/application/application_impl.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 | 14 |
| 15 TraceProviderImpl::TraceProviderImpl( | 15 TraceProviderImpl::TraceProviderImpl() : tracing_forced_(false) {} |
| 16 InterfaceRequest<tracing::TraceProvider> request) | |
| 17 : tracing_already_started_(false), binding_(this, request.Pass()) { | |
| 18 } | |
| 19 | 16 |
| 20 TraceProviderImpl::~TraceProviderImpl() { | 17 TraceProviderImpl::~TraceProviderImpl() {} |
| 21 } | |
| 22 | 18 |
| 23 void TraceProviderImpl::StartTracing(const String& categories, | 19 void TraceProviderImpl::StartTracing(const String& categories, |
| 24 tracing::TraceRecorderPtr collector) { | 20 tracing::TraceRecorderPtr collector) { |
| 25 DCHECK(!recorder_.get()); | 21 DCHECK(!recorder_.get()); |
| 26 recorder_ = collector.Pass(); | 22 recorder_ = collector.Pass(); |
| 27 if (!tracing_already_started_) { | 23 tracing_forced_ = false; |
| 24 if (!base::trace_event::TraceLog::GetInstance()->IsEnabled()) { | |
| 28 std::string categories_str = categories.To<std::string>(); | 25 std::string categories_str = categories.To<std::string>(); |
| 29 base::trace_event::TraceLog::GetInstance()->SetEnabled( | 26 base::trace_event::TraceLog::GetInstance()->SetEnabled( |
| 30 base::trace_event::TraceConfig(categories_str, | 27 base::trace_event::TraceConfig(categories_str, |
| 31 base::trace_event::RECORD_UNTIL_FULL), | 28 base::trace_event::RECORD_UNTIL_FULL), |
| 32 base::trace_event::TraceLog::RECORDING_MODE); | 29 base::trace_event::TraceLog::RECORDING_MODE); |
| 33 } | 30 } |
| 34 } | 31 } |
| 35 | 32 |
| 36 void TraceProviderImpl::StopTracing() { | 33 void TraceProviderImpl::StopTracing() { |
| 37 DCHECK(recorder_); | 34 DCHECK(recorder_); |
| 38 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | 35 base::trace_event::TraceLog::GetInstance()->SetDisabled(); |
| 39 | 36 |
| 40 base::trace_event::TraceLog::GetInstance()->Flush( | 37 base::trace_event::TraceLog::GetInstance()->Flush( |
| 41 base::Bind(&TraceProviderImpl::SendChunk, base::Unretained(this))); | 38 base::Bind(&TraceProviderImpl::SendChunk, base::Unretained(this))); |
| 42 } | 39 } |
| 43 | 40 |
| 41 void TraceProviderImpl::EnableTracingNow() { | |
| 42 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
| 43 base::trace_event::TraceConfig("*", base::trace_event::RECORD_UNTIL_FULL), | |
| 44 base::trace_event::TraceLog::RECORDING_MODE); | |
| 45 tracing_forced_ = true; | |
| 46 base::MessageLoop::current()->PostTask( | |
| 47 FROM_HERE, | |
| 48 base::Bind(&TraceProviderImpl::DelayedStop, base::Unretained(this))); | |
|
qsr
2015/09/11 14:44:04
What is the lifecycle of this object. Are you guar
etiennej
2015/09/11 15:07:32
Fixed.
| |
| 49 } | |
| 50 | |
| 51 void TraceProviderImpl::DelayedStop() { | |
| 52 // We use this indirection to account for cases where the Initialize app | |
| 53 // method (within which TraceProviderImpl is created) takes more than one | |
| 54 // second to finish; thus we start the countdown only when the current thread | |
| 55 // is unblocked. | |
| 56 base::MessageLoop::current()->PostDelayedTask( | |
| 57 FROM_HERE, | |
| 58 base::Bind(&TraceProviderImpl::StopIfForced, base::Unretained(this)), | |
| 59 base::TimeDelta::FromSeconds(1)); | |
| 60 } | |
| 61 | |
| 62 void TraceProviderImpl::StopIfForced() { | |
| 63 if (!tracing_forced_) { | |
| 64 return; | |
| 65 } | |
| 66 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 67 base::trace_event::TraceLog::GetInstance()->Flush( | |
| 68 base::Callback<void(const scoped_refptr<base::RefCountedString>&, | |
| 69 bool)>()); | |
| 70 } | |
| 71 | |
| 44 void TraceProviderImpl::SendChunk( | 72 void TraceProviderImpl::SendChunk( |
| 45 const scoped_refptr<base::RefCountedString>& events_str, | 73 const scoped_refptr<base::RefCountedString>& events_str, |
| 46 bool has_more_events) { | 74 bool has_more_events) { |
| 47 DCHECK(recorder_); | 75 DCHECK(recorder_); |
| 48 // The string will be empty if an error eccured or there were no trace | 76 // The string will be empty if an error eccured or there were no trace |
| 49 // events. Empty string is not a valid chunk to record so skip in this case. | 77 // events. Empty string is not a valid chunk to record so skip in this case. |
| 50 if (events_str->data().length()) { | 78 if (events_str->data().length()) { |
| 51 recorder_->Record(mojo::String(events_str->data())); | 79 recorder_->Record(mojo::String(events_str->data())); |
| 52 } | 80 } |
| 53 if (!has_more_events) { | 81 if (!has_more_events) { |
| 54 recorder_.reset(); | 82 recorder_.reset(); |
| 55 } | 83 } |
| 56 } | 84 } |
| 57 | 85 |
| 58 } // namespace mojo | 86 } // namespace mojo |
| OLD | NEW |