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_IPC_CLIENT_IMPL_H_
18 #define SRC_IPC_CLIENT_IMPL_H_
19 
20 #include <list>
21 #include <map>
22 #include <memory>
23 
24 #include "perfetto/base/task_runner.h"
25 #include "perfetto/ext/base/scoped_file.h"
26 #include "perfetto/ext/base/unix_socket.h"
27 #include "perfetto/ext/ipc/client.h"
28 #include "src/ipc/buffered_frame_deserializer.h"
29 
30 namespace perfetto {
31 
32 namespace protos {
33 namespace gen {
34 class IPCFrame_BindServiceReply;
35 class IPCFrame_InvokeMethodReply;
36 }  // namespace gen
37 }  // namespace protos
38 
39 namespace base {
40 class TaskRunner;
41 }  // namespace base
42 
43 namespace ipc {
44 
45 class ServiceDescriptor;
46 
47 class ClientImpl : public Client, public base::UnixSocket::EventListener {
48  public:
49   ClientImpl(ConnArgs, base::TaskRunner*);
50   ~ClientImpl() override;
51 
52   // Client implementation.
53   void BindService(base::WeakPtr<ServiceProxy>) override;
54   void UnbindService(ServiceID) override;
55   base::ScopedFile TakeReceivedFD() override;
56 
57   // base::UnixSocket::EventListener implementation.
58   void OnConnect(base::UnixSocket*, bool connected) override;
59   void OnDisconnect(base::UnixSocket*) override;
60   void OnDataAvailable(base::UnixSocket*) override;
61 
62   RequestID BeginInvoke(ServiceID,
63                         const std::string& method_name,
64                         MethodID remote_method_id,
65                         const ProtoMessage& method_args,
66                         bool drop_reply,
67                         base::WeakPtr<ServiceProxy>,
68                         int fd = -1);
69 
GetUnixSocketForTesting()70   base::UnixSocket* GetUnixSocketForTesting() { return sock_.get(); }
71 
72  private:
73   struct QueuedRequest {
74     QueuedRequest();
75     int type = 0;  // From Frame::msg_case(), see wire_protocol.proto.
76     RequestID request_id = 0;
77     base::WeakPtr<ServiceProxy> service_proxy;
78 
79     // Only for type == kMsgInvokeMethod.
80     std::string method_name;
81   };
82 
83   ClientImpl(const ClientImpl&) = delete;
84   ClientImpl& operator=(const ClientImpl&) = delete;
85 
86   void TryConnect();
87   bool SendFrame(const Frame&, int fd = -1);
88   void OnFrameReceived(const Frame&);
89   void OnBindServiceReply(QueuedRequest,
90                           const protos::gen::IPCFrame_BindServiceReply&);
91   void OnInvokeMethodReply(QueuedRequest,
92                            const protos::gen::IPCFrame_InvokeMethodReply&);
93 
94   bool invoking_method_reply_ = false;
95   const char* socket_name_ = nullptr;
96   bool socket_retry_ = false;
97   uint32_t socket_backoff_ms_ = 0;
98   std::unique_ptr<base::UnixSocket> sock_;
99   base::TaskRunner* const task_runner_;
100   RequestID last_request_id_ = 0;
101   BufferedFrameDeserializer frame_deserializer_;
102   base::ScopedFile received_fd_;
103   std::map<RequestID, QueuedRequest> queued_requests_;
104   std::map<ServiceID, base::WeakPtr<ServiceProxy>> service_bindings_;
105 
106   // Queue of calls to BindService() that happened before the socket connected.
107   std::list<base::WeakPtr<ServiceProxy>> queued_bindings_;
108 
109   base::WeakPtrFactory<Client> weak_ptr_factory_;  // Keep last.
110 };
111 
112 }  // namespace ipc
113 }  // namespace perfetto
114 
115 #endif  // SRC_IPC_CLIENT_IMPL_H_
116