Chromium Code Reviews| 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> | |
|
Yusuke Sato
2017/01/04 23:55:47
unused?
Earl Ou
2017/01/16 14:05:41
Done.
| |
| 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" | |
|
Yusuke Sato
2017/01/04 23:55:47
remove (see my comment on the header side)
Earl Ou
2017/01/16 14:05:42
It's only used in this cc file now.
| |
| 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 void OnStart(const ArcTraceAgent::StartAgentTracingCallback& callback, | |
| 31 dbus::Response* response) { | |
| 32 if (!response) | |
| 33 LOG(ERROR) << "Failed to request start"; | |
| 34 // Use PostTask as the convention of TraceAgent. The caller expects callback | |
| 35 // to be called after StartAgentTracing() returns. | |
| 36 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 37 FROM_HERE, | |
| 38 base::Bind(callback, kArcTracingAgentName, response != nullptr)); | |
| 39 } | |
| 40 | |
| 41 // A wrapper for controlling system tracing on Chrome OS. | |
| 42 class ArcTraceAgentImpl : public ArcTraceAgent { | |
| 43 public: | |
| 44 ArcTraceAgentImpl() : debugdaemon_proxy_(nullptr), weak_ptr_factory_(this) {} | |
| 45 | |
| 46 ~ArcTraceAgentImpl() override = default; | |
| 47 | |
| 48 // base::trace_event::TracingAgent overrides: | |
| 49 std::string GetTracingAgentName() override { return kArcTracingAgentName; } | |
| 50 | |
| 51 std::string GetTraceEventLabel() override { return kArcTraceLabel; } | |
| 52 | |
| 53 void StartAgentTracing(const base::trace_event::TraceConfig& trace_config, | |
| 54 const StartAgentTracingCallback& callback) override { | |
| 55 // delegate_ may be nullptr if ARC is not enabled on the system. In such | |
|
Yusuke Sato
2017/01/04 23:55:47
Apparently delegate_ is not initialized in the cto
Earl Ou
2017/01/16 14:05:42
Done.
| |
| 56 // case, simply do nothing. | |
| 57 if (!delegate_) { | |
| 58 // Use PostTask as the convention of TraceAgent. The caller expects | |
| 59 // callback to be called after this function returns. | |
| 60 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
| 61 FROM_HERE, base::Bind(callback, GetTracingAgentName(), false)); | |
| 62 return; | |
| 63 } | |
| 64 | |
| 65 delegate_->StartTracing(trace_config); | |
| 66 | |
| 67 dbus::MethodCall method_call(debugd::kDebugdInterface, "ArcTraceStart"); | |
| 68 debugdaemon_proxy_->CallMethod(&method_call, | |
| 69 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 70 base::Bind(&OnStart, callback)); | |
| 71 } | |
| 72 | |
| 73 void StopAgentTracing(const StopAgentTracingCallback& callback) override { | |
| 74 DCHECK(stop_agent_tracing_task_runner_); | |
| 75 | |
| 76 if (!stop_callback_.is_null()) { | |
| 77 LOG(ERROR) << "Previous StopAgentTracing() invocation is still pending."; | |
| 78 return; | |
| 79 } | |
| 80 | |
| 81 if (!delegate_) { | |
| 82 std::string no_data; | |
| 83 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), | |
| 84 base::RefCountedString::TakeString(&no_data)); | |
| 85 return; | |
| 86 } | |
| 87 | |
| 88 stop_callback_ = callback; | |
|
Yusuke Sato
2017/01/04 23:55:47
Can't we pass the |callback| directly to OnIOCompl
Earl Ou
2017/01/16 14:05:41
Done.
| |
| 89 | |
| 90 delegate_->StopTracing(); | |
| 91 | |
| 92 pipe_reader_ = base::MakeUnique<PipeReaderForString>( | |
| 93 stop_agent_tracing_task_runner_, | |
| 94 base::Bind(&ArcTraceAgentImpl::OnIOComplete, | |
| 95 weak_ptr_factory_.GetWeakPtr())); | |
| 96 | |
| 97 // PIPE reader will run on the given thread | |
| 98 // stop_agent_tracing_task_runner_, which should be an I/O thread given by | |
| 99 // the caller. | |
| 100 base::ScopedFD pipe_write_end = pipe_reader_->StartIO(); | |
| 101 DCHECK(pipe_write_end.is_valid()); | |
| 102 // Issue the dbus request to stop system tracing | |
| 103 dbus::MethodCall method_call(debugd::kDebugdInterface, "ArcTraceStop"); | |
| 104 dbus::MessageWriter writer(&method_call); | |
| 105 writer.AppendFileDescriptor(pipe_write_end.get()); | |
| 106 | |
| 107 debugdaemon_proxy_->CallMethod( | |
| 108 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT, | |
| 109 base::Bind(&ArcTraceAgentImpl::OnStopAgentTracing, | |
| 110 weak_ptr_factory_.GetWeakPtr())); | |
| 111 } | |
| 112 | |
| 113 // ArcTraceAgent overrides: | |
| 114 void SetDelegate(Delegate* delegate) override { delegate_ = delegate; } | |
| 115 | |
| 116 void SetStopAgentTracingTaskRunner( | |
| 117 scoped_refptr<base::TaskRunner> task_runner) override { | |
| 118 stop_agent_tracing_task_runner_ = task_runner; | |
| 119 } | |
| 120 | |
| 121 protected: | |
| 122 // DBusClient overrides: | |
| 123 void Init(dbus::Bus* bus) override { | |
| 124 debugdaemon_proxy_ = | |
| 125 bus->GetObjectProxy(debugd::kDebugdServiceName, | |
| 126 dbus::ObjectPath(debugd::kDebugdServicePath)); | |
| 127 } | |
| 128 | |
| 129 private: | |
| 130 // Called when pipe I/O completes; pass data on and delete the instance. | |
| 131 void OnIOComplete() { | |
| 132 std::string pipe_data; | |
| 133 pipe_reader_->GetData(&pipe_data); | |
| 134 pipe_reader_.reset(); | |
| 135 base::ResetAndReturn(&stop_callback_) | |
| 136 .Run(kArcTracingAgentName, kArcTraceLabel, | |
| 137 base::RefCountedString::TakeString(&pipe_data)); | |
| 138 } | |
| 139 | |
| 140 void OnStopAgentTracing(dbus::Response* response) { | |
| 141 if (!response) { | |
| 142 LOG(ERROR) << "Failed to request arc stop"; | |
| 143 // If debugd crashes or completes I/O before this message is processed | |
| 144 // then pipe_reader_ can be nullptr, see OnIOComplete(). | |
| 145 if (pipe_reader_.get()) | |
| 146 pipe_reader_->OnDataReady(-1); // terminate data stream | |
| 147 } | |
| 148 // NB: requester is signaled when I/O completes | |
| 149 } | |
| 150 | |
| 151 Delegate* delegate_; // Owned by ArcServiceLauncher. | |
| 152 dbus::ObjectProxy* debugdaemon_proxy_; // Owned by dbus::Bus. | |
| 153 std::unique_ptr<PipeReaderForString> pipe_reader_; | |
| 154 scoped_refptr<base::TaskRunner> stop_agent_tracing_task_runner_; | |
| 155 StopAgentTracingCallback stop_callback_; | |
| 156 base::WeakPtrFactory<ArcTraceAgentImpl> weak_ptr_factory_; | |
| 157 | |
| 158 DISALLOW_COPY_AND_ASSIGN(ArcTraceAgentImpl); | |
| 159 }; | |
| 160 | |
| 161 } // namespace | |
| 162 | |
| 163 // static | |
| 164 ArcTraceAgent* ArcTraceAgent::Create() { | |
| 165 return new ArcTraceAgentImpl(); | |
| 166 } | |
| 167 | |
| 168 ArcTraceAgent::ArcTraceAgent() = default; | |
| 169 | |
| 170 ArcTraceAgent::~ArcTraceAgent() = default; | |
| 171 | |
| 172 } // namespace chromeos | |
| OLD | NEW |