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