blob: 2402d17b4cd57feee1509f88685082fa83e2ba60 [file] [log] [blame]
Primiano Tucci4f9b6d72017-12-05 20:59:16 +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 SRC_IPC_CLIENT_IMPL_H_
18#define SRC_IPC_CLIENT_IMPL_H_
19
20#include "perfetto/base/scoped_file.h"
21#include "perfetto/base/task_runner.h"
22#include "perfetto/ipc/client.h"
23#include "src/ipc/buffered_frame_deserializer.h"
24#include "src/ipc/unix_socket.h"
25
26#include "src/ipc/wire_protocol.pb.h"
27
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;
50 base::ScopedFile TakeReceivedFD() override;
51
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,
Primiano Tucci2d0b2252018-01-25 13:37:50 +000061 bool drop_reply,
Florian Mayerd16508e2018-03-02 17:06:40 +000062 base::WeakPtr<ServiceProxy>,
63 int fd = -1);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000064
65 private:
66 struct QueuedRequest {
67 QueuedRequest();
68 int type = 0; // From Frame::msg_case(), see wire_protocol.proto.
69 RequestID request_id = 0;
70 base::WeakPtr<ServiceProxy> service_proxy;
71
72 // Only for type == kMsgInvokeMethod.
73 std::string method_name;
74 };
75
76 ClientImpl(const ClientImpl&) = delete;
77 ClientImpl& operator=(const ClientImpl&) = delete;
78
Florian Mayerd16508e2018-03-02 17:06:40 +000079 bool SendFrame(const Frame&, int fd = -1);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000080 void OnFrameReceived(const Frame&);
81 void OnBindServiceReply(QueuedRequest, const Frame::BindServiceReply&);
82 void OnInvokeMethodReply(QueuedRequest, const Frame::InvokeMethodReply&);
83
Lalit Maganti79f2d7b2018-01-23 18:27:33 +000084 bool invoking_method_reply_ = false;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000085 std::unique_ptr<UnixSocket> sock_;
86 base::TaskRunner* const task_runner_;
87 RequestID last_request_id_ = 0;
88 BufferedFrameDeserializer frame_deserializer_;
89 base::ScopedFile received_fd_;
90 std::map<RequestID, QueuedRequest> queued_requests_;
91 std::map<ServiceID, base::WeakPtr<ServiceProxy>> service_bindings_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000092
93 // Queue of calls to BindService() that happened before the socket connected.
94 std::list<base::WeakPtr<ServiceProxy>> queued_bindings_;
Primiano Tucci68323b02017-12-18 10:54:13 +010095
96 base::WeakPtrFactory<Client> weak_ptr_factory_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000097};
98
99} // namespace ipc
100} // namespace perfetto
101
102#endif // SRC_IPC_CLIENT_IMPL_H_