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