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 #include "src/tracing/ipc/producer/relay_ipc_client.h"
18
19 #include "perfetto/base/task_runner.h"
20 #include "perfetto/tracing/core/forward_decls.h"
21 #include "protos/perfetto/ipc/relay_port.ipc.h"
22
23 namespace perfetto {
24
25 RelayIPCClient::EventListener::~EventListener() = default;
26
RelayIPCClient(ipc::Client::ConnArgs conn_args,base::WeakPtr<EventListener> listener,base::TaskRunner * task_runner)27 RelayIPCClient::RelayIPCClient(ipc::Client::ConnArgs conn_args,
28 base::WeakPtr<EventListener> listener,
29 base::TaskRunner* task_runner)
30 : listener_(std::move(listener)),
31 task_runner_(task_runner),
32 ipc_channel_(
33 ipc::Client::CreateInstance(std::move(conn_args), task_runner)),
34 relay_proxy_(new protos::gen::RelayPortProxy(this /* event_listener */)) {
35 ipc_channel_->BindService(relay_proxy_->GetWeakPtr());
36 PERFETTO_DCHECK_THREAD(thread_checker_);
37 }
38
39 RelayIPCClient::~RelayIPCClient() = default;
40
OnConnect()41 void RelayIPCClient::OnConnect() {
42 PERFETTO_DCHECK_THREAD(thread_checker_);
43 connected_ = true;
44
45 if (listener_)
46 listener_->OnServiceConnected();
47 }
48
OnDisconnect()49 void RelayIPCClient::OnDisconnect() {
50 PERFETTO_DCHECK_THREAD(thread_checker_);
51 connected_ = false;
52
53 if (listener_)
54 listener_->OnServiceDisconnected();
55 }
56
SyncClock(const SyncClockRequest & sync_clock_request)57 void RelayIPCClient::SyncClock(const SyncClockRequest& sync_clock_request) {
58 PERFETTO_DCHECK_THREAD(thread_checker_);
59 if (!connected_) {
60 return task_runner_->PostTask([listener = listener_]() {
61 if (listener)
62 listener->OnServiceDisconnected();
63 });
64 }
65
66 SyncClockResponse resp;
67 ipc::Deferred<protos::gen::SyncClockResponse> async_resp;
68 async_resp.Bind(
69 [listener = listener_](ipc::AsyncResult<SyncClockResponse> resp) {
70 if (!listener)
71 return;
72 if (!resp)
73 return listener->OnServiceDisconnected();
74 listener->OnSyncClockResponse(*resp);
75 });
76 relay_proxy_->SyncClock(sync_clock_request, std::move(async_resp), -1);
77 }
78
79 } // namespace perfetto
80