| 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/services/tracing/public/cpp/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 "services/shell/public/cpp/connection.h" | |
| 16 | |
| 17 namespace mojo { | |
| 18 | |
| 19 TraceProviderImpl::TraceProviderImpl() | |
| 20 : binding_(this), tracing_forced_(false), weak_factory_(this) {} | |
| 21 | |
| 22 TraceProviderImpl::~TraceProviderImpl() { | |
| 23 StopTracing(); | |
| 24 } | |
| 25 | |
| 26 void TraceProviderImpl::Bind(InterfaceRequest<tracing::TraceProvider> request) { | |
| 27 if (!binding_.is_bound()) { | |
| 28 binding_.Bind(std::move(request)); | |
| 29 } else { | |
| 30 LOG(ERROR) << "Cannot accept two connections to TraceProvider."; | |
| 31 } | |
| 32 } | |
| 33 | |
| 34 void TraceProviderImpl::StartTracing(const String& categories, | |
| 35 tracing::TraceRecorderPtr recorder) { | |
| 36 DCHECK(!recorder_); | |
| 37 recorder_ = 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 if (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 | |
| 57 void TraceProviderImpl::ForceEnableTracing() { | |
| 58 base::trace_event::TraceLog::GetInstance()->SetEnabled( | |
| 59 base::trace_event::TraceConfig("*", base::trace_event::RECORD_UNTIL_FULL), | |
| 60 base::trace_event::TraceLog::RECORDING_MODE); | |
| 61 tracing_forced_ = true; | |
| 62 base::MessageLoop::current()->PostTask( | |
| 63 FROM_HERE, | |
| 64 base::Bind(&TraceProviderImpl::DelayedStop, weak_factory_.GetWeakPtr())); | |
| 65 } | |
| 66 | |
| 67 void TraceProviderImpl::DelayedStop() { | |
| 68 // We use this indirection to account for cases where the Initialize app | |
| 69 // method (within which TraceProviderImpl is created) takes more than one | |
| 70 // second to finish; thus we start the countdown only when the current thread | |
| 71 // is unblocked. | |
| 72 base::MessageLoop::current()->PostDelayedTask( | |
| 73 FROM_HERE, | |
| 74 base::Bind(&TraceProviderImpl::StopIfForced, weak_factory_.GetWeakPtr()), | |
| 75 base::TimeDelta::FromSeconds(1)); | |
| 76 } | |
| 77 | |
| 78 void TraceProviderImpl::StopIfForced() { | |
| 79 if (!tracing_forced_) { | |
| 80 return; | |
| 81 } | |
| 82 base::trace_event::TraceLog::GetInstance()->SetDisabled(); | |
| 83 base::trace_event::TraceLog::GetInstance()->Flush( | |
| 84 base::Callback<void(const scoped_refptr<base::RefCountedString>&, | |
| 85 bool)>()); | |
| 86 } | |
| 87 | |
| 88 void TraceProviderImpl::SendChunk( | |
| 89 const scoped_refptr<base::RefCountedString>& events_str, | |
| 90 bool has_more_events) { | |
| 91 DCHECK(recorder_); | |
| 92 // The string will be empty if an error eccured or there were no trace | |
| 93 // events. Empty string is not a valid chunk to record so skip in this case. | |
| 94 if (!events_str->data().empty()) { | |
| 95 recorder_->Record(mojo::String(events_str->data())); | |
| 96 } | |
| 97 if (!has_more_events) { | |
| 98 recorder_.reset(); | |
| 99 } | |
| 100 } | |
| 101 | |
| 102 } // namespace mojo | |
| OLD | NEW |