Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [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 TRACING_SRC_IPC_SERVICE_PRODUCER_IPC_SERVICE_H_ |
| 18 | #define TRACING_SRC_IPC_SERVICE_PRODUCER_IPC_SERVICE_H_ |
| 19 | |
| 20 | #include <map> |
| 21 | #include <memory> |
| 22 | #include <string> |
| 23 | |
| 24 | #include "base/weak_ptr.h" |
| 25 | #include "ipc/basic_types.h" |
| 26 | #include "tracing/core/producer.h" |
| 27 | #include "tracing/core/service.h" |
| 28 | |
| 29 | #include "tracing/src/ipc/producer_port.ipc.h" // From producer_port.proto. |
| 30 | |
| 31 | namespace perfetto { |
Primiano Tucci | f54cae4 | 2017-11-21 19:37:13 +0000 | [diff] [blame] | 32 | |
Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [diff] [blame] | 33 | namespace ipc { |
| 34 | class Host; |
Primiano Tucci | f54cae4 | 2017-11-21 19:37:13 +0000 | [diff] [blame] | 35 | } // namespace ipc |
Primiano Tucci | a616648 | 2017-11-20 13:05:45 +0000 | [diff] [blame] | 36 | |
| 37 | // Implements the Producer port of the IPC service. This class proxies requests |
| 38 | // and responses between the core service logic (|svc_|) and remote Producer(s) |
| 39 | // on the IPC socket, through the methods overriddden from ProducerPort. |
| 40 | class ProducerIPCService : public ProducerPort /* from producer_port.proto */ { |
| 41 | public: |
| 42 | using Service = ::perfetto::Service; // To avoid collisions w/ ipc::Service. |
| 43 | explicit ProducerIPCService(Service* core_service); |
| 44 | ~ProducerIPCService() override; |
| 45 | |
| 46 | // ProducerPort implementation (from .proto IPC definition). |
| 47 | void InitializeConnection(const InitializeConnectionRequest&, |
| 48 | DeferredInitializeConnectionResponse) override; |
| 49 | void RegisterDataSource(const RegisterDataSourceRequest&, |
| 50 | DeferredRegisterDataSourceResponse) override; |
| 51 | void UnregisterDataSource(const UnregisterDataSourceRequest&, |
| 52 | DeferredUnregisterDataSourceResponse) override; |
| 53 | void NotifySharedMemoryUpdate( |
| 54 | const NotifySharedMemoryUpdateRequest&, |
| 55 | DeferredNotifySharedMemoryUpdateResponse) override; |
| 56 | void GetAsyncCommand(const GetAsyncCommandRequest&, |
| 57 | DeferredGetAsyncCommandResponse) override; |
| 58 | void OnClientDisconnected() override; |
| 59 | |
| 60 | private: |
| 61 | // Acts like a Producer with the core Service business logic (which doesn't |
| 62 | // know anything about the remote transport), but all it does is proxying |
| 63 | // methods to the remote Producer on the other side of the IPC channel. |
| 64 | class RemoteProducer : public Producer { |
| 65 | public: |
| 66 | RemoteProducer(); |
| 67 | ~RemoteProducer() override; |
| 68 | |
| 69 | // These methods are called by the |core_service_| business logic. There is |
| 70 | // no connection here, these methods are posted straight away. |
| 71 | void OnConnect() override; |
| 72 | void OnDisconnect() override; |
| 73 | void CreateDataSourceInstance(DataSourceInstanceID, |
| 74 | const DataSourceConfig&) override; |
| 75 | void TearDownDataSourceInstance(DataSourceInstanceID) override; |
| 76 | |
| 77 | // RegisterDataSource requests that haven't been replied yet. |
| 78 | std::map<std::string, DeferredRegisterDataSourceResponse> |
| 79 | pending_data_sources; |
| 80 | |
| 81 | // The interface obtained from the core service business logic through |
| 82 | // Service::ConnectProducer(this). This allows to invoke methods for a |
| 83 | // specific Producer on the Service business logic. |
| 84 | std::unique_ptr<Service::ProducerEndpoint> service_endpoint; |
| 85 | |
| 86 | // The back-channel (based on a never ending stream request) that allows us |
| 87 | // to send asynchronous commands to the remote Producer (e.g. start/stop a |
| 88 | // data source). |
| 89 | DeferredGetAsyncCommandResponse async_producer_commands; |
| 90 | }; |
| 91 | |
| 92 | ProducerIPCService(const ProducerIPCService&) = delete; |
| 93 | ProducerIPCService& operator=(const ProducerIPCService&) = delete; |
| 94 | |
| 95 | // Returns the ProducerEndpoint in the core business logic that corresponds to |
| 96 | // the current IPC request. |
| 97 | RemoteProducer* GetProducerForCurrentRequest(); |
| 98 | |
| 99 | // Called back by the |core_service_| business logic, soon after a call to |
| 100 | // RegisterDataSource(). |
| 101 | void OnDataSourceRegistered(ipc::ClientID, std::string, DataSourceID); |
| 102 | |
| 103 | Service* const core_service_; |
| 104 | base::WeakPtrFactory<ProducerIPCService> weak_ptr_factory_; |
| 105 | |
| 106 | // Maps IPC clients to ProducerEndpoint instances registered on the |
| 107 | // |core_service_| business logic. |
| 108 | std::map<ipc::ClientID, std::unique_ptr<RemoteProducer>> producers_; |
| 109 | }; |
| 110 | |
| 111 | } // namespace perfetto |
| 112 | |
| 113 | #endif // TRACING_SRC_IPC_SERVICE_PRODUCER_IPC_SERVICE_H_ |