1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 #ifndef SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_ 18 #define SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_ 19 20 #include <stdint.h> 21 22 #include <vector> 23 24 #include "perfetto/base/scoped_file.h" 25 #include "perfetto/base/weak_ptr.h" 26 #include "perfetto/ipc/service_proxy.h" 27 #include "perfetto/tracing/core/basic_types.h" 28 #include "perfetto/tracing/core/service.h" 29 #include "perfetto/tracing/core/trace_packet.h" 30 #include "perfetto/tracing/ipc/consumer_ipc_client.h" 31 32 #include "perfetto/ipc/consumer_port.ipc.h" 33 34 namespace perfetto { 35 36 namespace base { 37 class TaskRunner; 38 } // namespace base 39 40 namespace ipc { 41 class Client; 42 } // namespace ipc 43 44 class Consumer; 45 class TraceConfig; 46 47 // Exposes a Service endpoint to Consumer(s), proxying all requests through a 48 // IPC channel to the remote Service. This class is the glue layer between the 49 // generic Service interface exposed to the clients of the library and the 50 // actual IPC transport. 51 class ConsumerIPCClientImpl : public Service::ConsumerEndpoint, 52 public ipc::ServiceProxy::EventListener { 53 public: 54 ConsumerIPCClientImpl(const char* service_sock_name, 55 Consumer*, 56 base::TaskRunner*); 57 ~ConsumerIPCClientImpl() override; 58 59 // Service::ConsumerEndpoint implementation. 60 // These methods are invoked by the actual Consumer(s) code by clients of the 61 // tracing library, which know nothing about the IPC transport. 62 void EnableTracing(const TraceConfig&, base::ScopedFile) override; 63 void DisableTracing() override; 64 void ReadBuffers() override; 65 void FreeBuffers() override; 66 void Flush(uint32_t timeout_ms, FlushCallback) override; 67 68 // ipc::ServiceProxy::EventListener implementation. 69 // These methods are invoked by the IPC layer, which knows nothing about 70 // tracing, consumers and consumers. 71 void OnConnect() override; 72 void OnDisconnect() override; 73 74 private: 75 void OnReadBuffersResponse(ipc::AsyncResult<protos::ReadBuffersResponse>); 76 77 // TODO(primiano): think to dtor order, do we rely on any specific sequence? 78 Consumer* const consumer_; 79 80 // The object that owns the client socket and takes care of IPC traffic. 81 std::unique_ptr<ipc::Client> ipc_channel_; 82 83 // The proxy interface for the consumer port of the service. It is bound 84 // to |ipc_channel_| and (de)serializes method invocations over the wire. 85 protos::ConsumerPortProxy consumer_port_; 86 87 bool connected_ = false; 88 89 // When a packet is too big to fit into a ReadBuffersResponse IPC, the service 90 // will chunk it into several IPCs, each containing few slices of the packet 91 // (a packet's slice is always guaranteed to be << kIPCBufferSize). When 92 // chunking happens this field accumulates the slices received until the 93 // one with |last_slice_for_packet| == true is received. 94 TracePacket partial_packet_; 95 96 base::WeakPtrFactory<ConsumerIPCClientImpl> weak_ptr_factory_; 97 }; 98 99 } // namespace perfetto 100 101 #endif // SRC_TRACING_IPC_CONSUMER_CONSUMER_IPC_CLIENT_IMPL_H_ 102