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