xref: /aosp_15_r20/external/perfetto/src/tracing/ipc/producer/relay_ipc_client.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2024 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_PRODUCER_RELAY_IPC_CLIENT_H_
18 #define SRC_TRACING_IPC_PRODUCER_RELAY_IPC_CLIENT_H_
19 
20 #include "perfetto/ext/base/thread_checker.h"
21 #include "perfetto/ext/base/weak_ptr.h"
22 #include "perfetto/ext/ipc/client.h"
23 #include "perfetto/ext/ipc/service_proxy.h"
24 #include "perfetto/ext/tracing/core/tracing_service.h"
25 
26 #include "protos/perfetto/ipc/relay_port.ipc.h"
27 
28 namespace perfetto {
29 
30 namespace base {
31 class TaskRunner;
32 }  // namespace base
33 
34 // Exposes a Service endpoint to the relay service, proxying all requests
35 // through an IPC channel to the remote Service. This class is the glue layer
36 // between the generic Service interface exposed to the clients of the library
37 // and the actual IPC transport.
38 class RelayIPCClient : public ipc::ServiceProxy::EventListener {
39  public:
40   class EventListener {
41    public:
42     virtual ~EventListener();
43     // Called when the client receives the response of the SyncClock() request.
44     virtual void OnSyncClockResponse(const SyncClockResponse&) = 0;
45     // Called when the IPC service is connected and is ready for the SyncClock()
46     // requests.
47     virtual void OnServiceConnected() = 0;
48     // Called when the IPC service is disconnected.
49     virtual void OnServiceDisconnected() = 0;
50   };
51 
52   using SyncClockCallback = std::function<void(const SyncClockResponse&)>;
53   using OnDisconnectCallback = std::function<void()>;
54 
55   RelayIPCClient(ipc::Client::ConnArgs,
56                  base::WeakPtr<EventListener>,
57                  base::TaskRunner*);
58   ~RelayIPCClient() override;
59 
60   void SyncClock(const SyncClockRequest&);
61 
62   // ipc::ServiceProxy::EventListener implementation.
63   // These methods are invoked by the IPC layer, which knows nothing about
64   // tracing, producers and consumers.
65   void OnConnect() override;
66   void OnDisconnect() override;
67 
68  private:
69   base::WeakPtr<EventListener> listener_;
70 
71   base::TaskRunner* const task_runner_;
72 
73   // The object that owns the client socket and takes care of IPC traffic.
74   std::unique_ptr<ipc::Client> ipc_channel_;
75 
76   // The proxy interface for the producer port of the service. It is bound
77   // to |ipc_channel_| and (de)serializes method invocations over the wire.
78   std::unique_ptr<protos::gen::RelayPortProxy> relay_proxy_;
79 
80   bool connected_ = false;
81   PERFETTO_THREAD_CHECKER(thread_checker_)
82 };
83 
84 }  // namespace perfetto
85 
86 #endif  // SRC_TRACING_IPC_PRODUCER_RELAY_IPC_CLIENT_H_
87