blob: f2762ca06533033da3cf31fa3793f74911e8bfb5 [file] [log] [blame]
Primiano Tuccied0ce252017-11-09 19:35:10 +00001/*
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 IPC_SRC_CLIENT_IMPL_H_
18#define IPC_SRC_CLIENT_IMPL_H_
19
Primiano Tuccied0ce252017-11-09 19:35:10 +000020#include "ipc/client.h"
21#include "ipc/src/buffered_frame_deserializer.h"
22#include "ipc/src/unix_socket.h"
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080023#include "perfetto_base/scoped_file.h"
24#include "perfetto_base/task_runner.h"
Primiano Tuccied0ce252017-11-09 19:35:10 +000025
Primiano Tucci2ee254a2017-11-15 00:38:48 +000026#include "ipc/src/wire_protocol.pb.h"
Primiano Tuccied0ce252017-11-09 19:35:10 +000027
28#include <list>
29#include <map>
30#include <memory>
31
32namespace perfetto {
33
34namespace base {
35class TaskRunner;
36} // namespace base
37
38namespace ipc {
39
40class ServiceDescriptor;
41
42class ClientImpl : public Client, public UnixSocket::EventListener {
43 public:
44 ClientImpl(const char* socket_name, base::TaskRunner*);
45 ~ClientImpl() override;
46
47 // Client implementation.
48 void BindService(base::WeakPtr<ServiceProxy>) override;
49 void UnbindService(ServiceID) override;
Primiano Tuccif54cae42017-11-21 19:37:13 +000050 base::ScopedFile TakeReceivedFD() override;
Primiano Tuccied0ce252017-11-09 19:35:10 +000051
52 // UnixSocket::EventListener implementation.
53 void OnConnect(UnixSocket*, bool connected) override;
54 void OnDisconnect(UnixSocket*) override;
55 void OnDataAvailable(UnixSocket*) override;
56
57 RequestID BeginInvoke(ServiceID,
58 const std::string& method_name,
59 MethodID remote_method_id,
60 const ProtoMessage& method_args,
61 base::WeakPtr<ServiceProxy>);
62
63 private:
64 struct QueuedRequest {
65 QueuedRequest();
66 int type = 0; // From Frame::msg_case(), see wire_protocol.proto.
67 RequestID request_id = 0;
68 base::WeakPtr<ServiceProxy> service_proxy;
69
70 // Only for type == kMsgInvokeMethod.
71 std::string method_name;
72 };
73
74 ClientImpl(const ClientImpl&) = delete;
75 ClientImpl& operator=(const ClientImpl&) = delete;
76
77 bool SendFrame(const Frame&);
78 void OnFrameReceived(const Frame&);
79 void OnBindServiceReply(QueuedRequest, const Frame::BindServiceReply&);
80 void OnInvokeMethodReply(QueuedRequest, const Frame::InvokeMethodReply&);
81
82 std::unique_ptr<UnixSocket> sock_;
83 base::TaskRunner* const task_runner_;
84 RequestID last_request_id_ = 0;
85 BufferedFrameDeserializer frame_deserializer_;
Primiano Tuccif54cae42017-11-21 19:37:13 +000086 base::ScopedFile received_fd_;
Primiano Tuccied0ce252017-11-09 19:35:10 +000087 std::map<RequestID, QueuedRequest> queued_requests_;
88 std::map<ServiceID, base::WeakPtr<ServiceProxy>> service_bindings_;
89 base::WeakPtrFactory<Client> weak_ptr_factory_;
90
91 // Queue of calls to BindService() that happened before the socket connected.
92 std::list<base::WeakPtr<ServiceProxy>> queued_bindings_;
93};
94
95} // namespace ipc
96} // namespace perfetto
97
98#endif // IPC_SRC_CLIENT_IMPL_H_