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