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