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