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_SERVICE_CONSUMER_IPC_SERVICE_H_ 18 #define SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_ 19 20 #include <list> 21 #include <map> 22 #include <memory> 23 #include <string> 24 25 #include "perfetto/base/weak_ptr.h" 26 #include "perfetto/ipc/basic_types.h" 27 #include "perfetto/tracing/core/consumer.h" 28 #include "perfetto/tracing/core/service.h" 29 30 #include "perfetto/ipc/consumer_port.ipc.h" 31 32 namespace perfetto { 33 34 namespace ipc { 35 class Host; 36 } // namespace ipc 37 38 // Implements the Consumer port of the IPC service. This class proxies requests 39 // and responses between the core service logic (|svc_|) and remote Consumer(s) 40 // on the IPC socket, through the methods overriddden from ConsumerPort. 41 class ConsumerIPCService : public protos::ConsumerPort { 42 public: 43 using Service = ::perfetto::Service; // To avoid collisions w/ ipc::Service. 44 explicit ConsumerIPCService(Service* core_service); 45 ~ConsumerIPCService() override; 46 47 // ConsumerPort implementation (from .proto IPC definition). 48 void EnableTracing(const protos::EnableTracingRequest&, 49 DeferredEnableTracingResponse) override; 50 void DisableTracing(const protos::DisableTracingRequest&, 51 DeferredDisableTracingResponse) override; 52 void ReadBuffers(const protos::ReadBuffersRequest&, 53 DeferredReadBuffersResponse) override; 54 void FreeBuffers(const protos::FreeBuffersRequest&, 55 DeferredFreeBuffersResponse) override; 56 void Flush(const protos::FlushRequest&, DeferredFlushResponse) override; 57 void OnClientDisconnected() override; 58 59 private: 60 // Acts like a Consumer with the core Service business logic (which doesn't 61 // know anything about the remote transport), but all it does is proxying 62 // methods to the remote Consumer on the other side of the IPC channel. 63 class RemoteConsumer : public Consumer { 64 public: 65 RemoteConsumer(); 66 ~RemoteConsumer() override; 67 68 // These methods are called by the |core_service_| business logic. There is 69 // no connection here, these methods are posted straight away. 70 void OnConnect() override; 71 void OnDisconnect() override; 72 void OnTracingDisabled() override; 73 void OnTraceData(std::vector<TracePacket>, bool has_more) override; 74 75 // The interface obtained from the core service business logic through 76 // Service::ConnectConsumer(this). This allows to invoke methods for a 77 // specific Consumer on the Service business logic. 78 std::unique_ptr<Service::ConsumerEndpoint> service_endpoint; 79 80 // After DisableTracing() is invoked, this binds the async callback that 81 // allows to stream trace packets back to the client. 82 DeferredReadBuffersResponse read_buffers_response; 83 84 // After EnableTracing() is invoked, this binds the async callback that 85 // allows to send the OnTracingDisabled notification. 86 DeferredEnableTracingResponse enable_tracing_response; 87 }; 88 89 // This has to be a container that doesn't invalidate iterators. 90 using PendingFlushResponses = std::list<DeferredFlushResponse>; 91 92 ConsumerIPCService(const ConsumerIPCService&) = delete; 93 ConsumerIPCService& operator=(const ConsumerIPCService&) = delete; 94 95 // Returns the ConsumerEndpoint in the core business logic that corresponds to 96 // the current IPC request. 97 RemoteConsumer* GetConsumerForCurrentRequest(); 98 99 void OnFlushCallback(bool success, PendingFlushResponses::iterator); 100 101 Service* const core_service_; 102 103 // Maps IPC clients to ConsumerEndpoint instances registered on the 104 // |core_service_| business logic. 105 std::map<ipc::ClientID, std::unique_ptr<RemoteConsumer>> consumers_; 106 107 PendingFlushResponses pending_flush_responses_; 108 109 base::WeakPtrFactory<ConsumerIPCService> weak_ptr_factory_; // Keep last. 110 }; 111 112 } // namespace perfetto 113 114 #endif // SRC_TRACING_IPC_SERVICE_CONSUMER_IPC_SERVICE_H_ 115