• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const char* socket_name, 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 
70  private:
71   struct QueuedRequest {
72     QueuedRequest();
73     int type = 0;  // From Frame::msg_case(), see wire_protocol.proto.
74     RequestID request_id = 0;
75     base::WeakPtr<ServiceProxy> service_proxy;
76 
77     // Only for type == kMsgInvokeMethod.
78     std::string method_name;
79   };
80 
81   ClientImpl(const ClientImpl&) = delete;
82   ClientImpl& operator=(const ClientImpl&) = delete;
83 
84   bool SendFrame(const Frame&, int fd = -1);
85   void OnFrameReceived(const Frame&);
86   void OnBindServiceReply(QueuedRequest,
87                           const protos::gen::IPCFrame_BindServiceReply&);
88   void OnInvokeMethodReply(QueuedRequest,
89                            const protos::gen::IPCFrame_InvokeMethodReply&);
90 
91   bool invoking_method_reply_ = false;
92   std::unique_ptr<base::UnixSocket> sock_;
93   base::TaskRunner* const task_runner_;
94   RequestID last_request_id_ = 0;
95   BufferedFrameDeserializer frame_deserializer_;
96   base::ScopedFile received_fd_;
97   std::map<RequestID, QueuedRequest> queued_requests_;
98   std::map<ServiceID, base::WeakPtr<ServiceProxy>> service_bindings_;
99 
100   // Queue of calls to BindService() that happened before the socket connected.
101   std::list<base::WeakPtr<ServiceProxy>> queued_bindings_;
102 
103   base::WeakPtrFactory<Client> weak_ptr_factory_;  // Keep last.
104 };
105 
106 }  // namespace ipc
107 }  // namespace perfetto
108 
109 #endif  // SRC_IPC_CLIENT_IMPL_H_
110