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_HOST_IMPL_H_ 18 #define SRC_IPC_HOST_IMPL_H_ 19 20 #include <map> 21 #include <set> 22 #include <string> 23 #include <vector> 24 25 #include "perfetto/base/task_runner.h" 26 #include "perfetto/base/thread_checker.h" 27 #include "perfetto/base/unix_socket.h" 28 #include "perfetto/ipc/deferred.h" 29 #include "perfetto/ipc/host.h" 30 #include "src/ipc/buffered_frame_deserializer.h" 31 32 namespace perfetto { 33 namespace ipc { 34 35 class Frame; 36 37 class HostImpl : public Host, public base::UnixSocket::EventListener { 38 public: 39 HostImpl(const char* socket_name, base::TaskRunner*); 40 HostImpl(base::ScopedFile socket_fd, base::TaskRunner*); 41 ~HostImpl() override; 42 43 // Host implementation. 44 bool ExposeService(std::unique_ptr<Service>) override; 45 46 // base::UnixSocket::EventListener implementation. 47 void OnNewIncomingConnection(base::UnixSocket*, 48 std::unique_ptr<base::UnixSocket>) override; 49 void OnDisconnect(base::UnixSocket*) override; 50 void OnDataAvailable(base::UnixSocket*) override; 51 sock()52 const base::UnixSocket* sock() const { return sock_.get(); } 53 54 private: 55 // Owns the per-client receive buffer (BufferedFrameDeserializer). 56 struct ClientConnection { 57 ~ClientConnection(); 58 ClientID id; 59 std::unique_ptr<base::UnixSocket> sock; 60 BufferedFrameDeserializer frame_deserializer; 61 base::ScopedFile received_fd; 62 }; 63 struct ExposedService { 64 ExposedService(ServiceID, const std::string&, std::unique_ptr<Service>); 65 ~ExposedService(); 66 ExposedService(ExposedService&&) noexcept; 67 ExposedService& operator=(ExposedService&&); 68 69 ServiceID id; 70 std::string name; 71 std::unique_ptr<Service> instance; 72 }; 73 74 HostImpl(const HostImpl&) = delete; 75 HostImpl& operator=(const HostImpl&) = delete; 76 77 bool Initialize(const char* socket_name); 78 void OnReceivedFrame(ClientConnection*, const Frame&); 79 void OnBindService(ClientConnection*, const Frame&); 80 void OnInvokeMethod(ClientConnection*, const Frame&); 81 void ReplyToMethodInvocation(ClientID, RequestID, AsyncResult<ProtoMessage>); 82 const ExposedService* GetServiceByName(const std::string&); 83 84 static void SendFrame(ClientConnection*, const Frame&, int fd = -1); 85 86 base::TaskRunner* const task_runner_; 87 std::map<ServiceID, ExposedService> services_; 88 std::unique_ptr<base::UnixSocket> sock_; // The listening socket. 89 std::map<ClientID, std::unique_ptr<ClientConnection>> clients_; 90 std::map<base::UnixSocket*, ClientConnection*> clients_by_socket_; 91 ServiceID last_service_id_ = 0; 92 ClientID last_client_id_ = 0; 93 PERFETTO_THREAD_CHECKER(thread_checker_) 94 base::WeakPtrFactory<HostImpl> weak_ptr_factory_; // Keep last. 95 }; 96 97 } // namespace ipc 98 } // namespace perfetto 99 100 #endif // SRC_IPC_HOST_IMPL_H_ 101