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 "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 32 namespace perfetto { 33 34 namespace base { 35 class TaskRunner; 36 } // namespace base 37 38 namespace ipc { 39 40 class ServiceDescriptor; 41 42 class 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, 61 bool drop_reply, 62 base::WeakPtr<ServiceProxy>, 63 int fd = -1); 64 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 79 bool SendFrame(const Frame&, int fd = -1); 80 void OnFrameReceived(const Frame&); 81 void OnBindServiceReply(QueuedRequest, const Frame::BindServiceReply&); 82 void OnInvokeMethodReply(QueuedRequest, const Frame::InvokeMethodReply&); 83 84 bool invoking_method_reply_ = false; 85 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_; 92 93 // Queue of calls to BindService() that happened before the socket connected. 94 std::list<base::WeakPtr<ServiceProxy>> queued_bindings_; 95 96 base::WeakPtrFactory<Client> weak_ptr_factory_; 97 }; 98 99 } // namespace ipc 100 } // namespace perfetto 101 102 #endif // SRC_IPC_CLIENT_IMPL_H_ 103