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