Home | History | Annotate | Download | only in service
      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_PRODUCER_IPC_SERVICE_H_
     18 #define SRC_TRACING_IPC_SERVICE_PRODUCER_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/producer.h"
     27 #include "perfetto/tracing/core/service.h"
     28 
     29 #include "perfetto/ipc/producer_port.ipc.h"
     30 
     31 namespace perfetto {
     32 
     33 namespace ipc {
     34 class Host;
     35 }  // namespace ipc
     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 protos::ProducerPort {
     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 protos::InitializeConnectionRequest&,
     48                             DeferredInitializeConnectionResponse) override;
     49   void RegisterDataSource(const protos::RegisterDataSourceRequest&,
     50                           DeferredRegisterDataSourceResponse) override;
     51   void UnregisterDataSource(const protos::UnregisterDataSourceRequest&,
     52                             DeferredUnregisterDataSourceResponse) override;
     53   void CommitData(const protos::CommitDataRequest&,
     54                   DeferredCommitDataResponse) override;
     55   void GetAsyncCommand(const protos::GetAsyncCommandRequest&,
     56                        DeferredGetAsyncCommandResponse) override;
     57   void OnClientDisconnected() override;
     58 
     59  private:
     60   // Acts like a Producer 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 Producer on the other side of the IPC channel.
     63   class RemoteProducer : public Producer {
     64    public:
     65     RemoteProducer();
     66     ~RemoteProducer() 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 CreateDataSourceInstance(DataSourceInstanceID,
     73                                   const DataSourceConfig&) override;
     74     void TearDownDataSourceInstance(DataSourceInstanceID) override;
     75     void OnTracingSetup() override;
     76     void Flush(FlushRequestID,
     77                const DataSourceInstanceID* data_source_ids,
     78                size_t num_data_sources) override;
     79 
     80     // The interface obtained from the core service business logic through
     81     // Service::ConnectProducer(this). This allows to invoke methods for a
     82     // specific Producer on the Service business logic.
     83     std::unique_ptr<Service::ProducerEndpoint> service_endpoint;
     84 
     85     // The back-channel (based on a never ending stream request) that allows us
     86     // to send asynchronous commands to the remote Producer (e.g. start/stop a
     87     // data source).
     88     DeferredGetAsyncCommandResponse async_producer_commands;
     89   };
     90 
     91   ProducerIPCService(const ProducerIPCService&) = delete;
     92   ProducerIPCService& operator=(const ProducerIPCService&) = delete;
     93 
     94   // Returns the ProducerEndpoint in the core business logic that corresponds to
     95   // the current IPC request.
     96   RemoteProducer* GetProducerForCurrentRequest();
     97 
     98   Service* const core_service_;
     99 
    100   // Maps IPC clients to ProducerEndpoint instances registered on the
    101   // |core_service_| business logic.
    102   std::map<ipc::ClientID, std::unique_ptr<RemoteProducer>> producers_;
    103 
    104   base::WeakPtrFactory<ProducerIPCService> weak_ptr_factory_;
    105 };
    106 
    107 }  // namespace perfetto
    108 
    109 #endif  // SRC_TRACING_IPC_SERVICE_PRODUCER_IPC_SERVICE_H_
    110