OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "chromeos/dbus/arc_trace_agent.h" | |
6 | |
7 #include <string> | |
8 #include <utility> | |
9 | |
10 #include "base/callback_helpers.h" | |
11 #include "base/logging.h" | |
12 #include "base/memory/ptr_util.h" | |
13 #include "base/memory/ref_counted.h" | |
14 #include "base/threading/thread_task_runner_handle.h" | |
15 #include "chromeos/dbus/dbus_thread_manager.h" | |
16 #include "chromeos/dbus/pipe_reader.h" | |
17 #include "dbus/bus.h" | |
18 #include "dbus/message.h" | |
19 #include "dbus/object_path.h" | |
20 #include "dbus/object_proxy.h" | |
21 #include "third_party/cros_system_api/dbus/service_constants.h" | |
22 | |
23 namespace chromeos { | |
24 | |
25 namespace { | |
26 | |
27 constexpr char kArcTracingAgentName[] = "arc"; | |
28 constexpr char kArcTraceLabel[] = "ArcTraceEvents"; | |
29 | |
30 // A wrapper for controlling system tracing on Chrome OS. | |
31 class ArcTraceAgentImpl : public ArcTraceAgent { | |
32 public: | |
33 ArcTraceAgentImpl() : debugdaemon_proxy_(nullptr), weak_ptr_factory_(this) {} | |
34 | |
35 ~ArcTraceAgentImpl() override = default; | |
36 | |
37 // base::trace_event::TracingAgent overrides: | |
38 std::string GetTracingAgentName() override { return kArcTracingAgentName; } | |
39 std::string GetTraceEventLabel() override { return kArcTraceLabel; } | |
40 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | |
41 const StartAgentTracingCallback& callback) override { | |
42 // delegate_ may be nullptr if ARC is not enabled on the system. In such | |
43 // case, simply do nothing. | |
44 if (delegate_) { | |
Luis Héctor Chávez
2016/12/28 17:21:58
nit:
if (!delegate_)
return;
// rest of method
shunhsingou
2016/12/29 09:40:29
Done, and also pass false to the callback in this
| |
45 delegate_->StartTracing(trace_config); | |
46 | |
47 dbus::MethodCall method_call(debugd::kDebugdInterface, "ArcTraceStart"); | |
48 debugdaemon_proxy_->CallMethod( | |
49 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
50 base::Bind(&ArcTraceAgentImpl::OnStartMethod, | |
51 weak_ptr_factory_.GetWeakPtr())); | |
52 start_callback_ = callback; | |
Luis Héctor Chávez
2016/12/28 17:21:58
nit: You can avoid introducing |start_callback_| b
shunhsingou
2016/12/29 09:40:29
Done.
| |
53 } | |
54 } | |
55 void StopAgentTracing(const StopAgentTracingCallback& callback) override { | |
Luis Héctor Chávez
2016/12/28 17:21:58
nit: newline before.
shunhsingou
2016/12/29 09:40:29
Done.
| |
56 DCHECK(stop_agent_tracing_task_runner_); | |
57 if (!stop_callback_.is_null()) { | |
58 LOG(ERROR) << "Busy doing StopAgentTracing"; | |
Luis Héctor Chávez
2016/12/28 17:21:58
nit: maybe word this as "Previous StopAgentTracing
shunhsingou
2016/12/29 09:40:29
Done.
| |
59 return; | |
60 } | |
61 | |
62 if (!delegate_) { | |
63 std::string no_data; | |
64 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), | |
65 base::RefCountedString::TakeString(&no_data)); | |
66 return; | |
67 } | |
68 | |
69 delegate_->StopTracing(); | |
70 | |
71 pipe_reader_ = base::MakeUnique<PipeReaderForString>( | |
72 stop_agent_tracing_task_runner_, | |
73 base::Bind(&ArcTraceAgentImpl::OnIOComplete, | |
74 weak_ptr_factory_.GetWeakPtr())); | |
75 | |
76 base::ScopedFD pipe_write_end = pipe_reader_->StartIO(); | |
77 DCHECK(pipe_write_end.is_valid()); | |
78 // Issue the dbus request to stop system tracing | |
79 dbus::MethodCall method_call(debugd::kDebugdInterface, "ArcTraceStop"); | |
80 dbus::MessageWriter writer(&method_call); | |
81 writer.AppendFileDescriptor(pipe_write_end.get()); | |
82 | |
83 stop_callback_ = callback; | |
84 | |
85 debugdaemon_proxy_->CallMethod( | |
86 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
87 base::Bind(&ArcTraceAgentImpl::OnStopAgentTracing, | |
88 weak_ptr_factory_.GetWeakPtr())); | |
89 } | |
90 | |
91 void SetDelegate(Delegate* delegate) { delegate_ = delegate; } | |
Luis Héctor Chávez
2016/12/28 17:21:58
This should be marked as 'override'. Also add
//
shunhsingou
2016/12/29 09:40:29
Done.
| |
92 | |
93 void SetStopAgentTracingTaskRunner( | |
94 scoped_refptr<base::TaskRunner> task_runner) override { | |
95 stop_agent_tracing_task_runner_ = task_runner; | |
96 } | |
97 | |
98 protected: | |
99 // DBusClient overrides: | |
100 void Init(dbus::Bus* bus) override { | |
101 debugdaemon_proxy_ = | |
102 bus->GetObjectProxy(debugd::kDebugdServiceName, | |
103 dbus::ObjectPath(debugd::kDebugdServicePath)); | |
104 } | |
105 | |
106 private: | |
107 void OnStartMethod(dbus::Response* response) { | |
108 if (!response) { | |
109 LOG(ERROR) << "Failed to request start"; | |
110 } | |
111 base::ResetAndReturn(&start_callback_) | |
112 .Run(GetTracingAgentName(), response != nullptr); | |
113 } | |
Luis Héctor Chávez
2016/12/28 17:21:58
nit: newline between method definitions. Only skip
shunhsingou
2016/12/29 09:40:29
Done.
| |
114 // Called when pipe I/O completes; pass data on and delete the instance. | |
115 void OnIOComplete() { | |
116 std::string pipe_data; | |
117 pipe_reader_->GetData(&pipe_data); | |
Luis Héctor Chávez
2016/12/28 17:21:58
Weren't you calling |pipe_reader_.reset()| before?
shunhsingou
2016/12/29 09:40:29
Done. Add it back.
| |
118 base::ResetAndReturn(&stop_callback_) | |
119 .Run(GetTracingAgentName(), GetTraceEventLabel(), | |
120 base::RefCountedString::TakeString(&pipe_data)); | |
121 } | |
122 void OnStopAgentTracing(dbus::Response* response) { | |
123 if (!response) { | |
124 LOG(ERROR) << "Failed to request arc stop"; | |
125 // If debugd crashes or completes I/O before this message is processed | |
126 // then pipe_reader_ can be nullptr, see OnIOComplete(). | |
127 if (pipe_reader_.get()) | |
128 pipe_reader_->OnDataReady(-1); // terminate data stream | |
129 } | |
130 // NB: requester is signaled when I/O completes | |
131 } | |
132 | |
133 Delegate* delegate_; | |
Luis Héctor Chávez
2016/12/28 17:21:58
Who owns this?
shunhsingou
2016/12/29 09:40:29
Added comment here.
| |
134 dbus::ObjectProxy* debugdaemon_proxy_; // Owned by dbus::Bus. | |
135 std::unique_ptr<PipeReaderForString> pipe_reader_; | |
136 scoped_refptr<base::TaskRunner> stop_agent_tracing_task_runner_; | |
137 StartAgentTracingCallback start_callback_; | |
138 StopAgentTracingCallback stop_callback_; | |
139 base::WeakPtrFactory<ArcTraceAgentImpl> weak_ptr_factory_; | |
140 | |
141 DISALLOW_COPY_AND_ASSIGN(ArcTraceAgentImpl); | |
142 }; | |
143 | |
144 } // namespace | |
145 | |
146 // static | |
147 ArcTraceAgent* ArcTraceAgent::Create() { | |
148 return new ArcTraceAgentImpl(); | |
149 } | |
150 | |
151 } // namespace chromeos | |
OLD | NEW |