blob: bef1d9e40d9bb0d6fe6615b37e1d5291c6bd6b92 [file] [log] [blame]
Primiano Tucci4f9b6d72017-12-05 20:59:16 +00001/*
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/ipc/deferred.h"
28#include "perfetto/ipc/host.h"
29#include "src/ipc/buffered_frame_deserializer.h"
30#include "src/ipc/unix_socket.h"
31
32namespace perfetto {
33namespace ipc {
34
35class Frame;
36
37class HostImpl : public Host, public UnixSocket::EventListener {
38 public:
39 HostImpl(const char* socket_name, base::TaskRunner*);
Primiano Tucci9b5d3362017-12-21 21:42:17 +010040 HostImpl(base::ScopedFile socket_fd, base::TaskRunner*);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000041 ~HostImpl() override;
42
43 // Host implementation.
44 bool ExposeService(std::unique_ptr<Service>) override;
45
46 // UnixSocket::EventListener implementation.
47 void OnNewIncomingConnection(UnixSocket*,
48 std::unique_ptr<UnixSocket>) override;
49 void OnDisconnect(UnixSocket*) override;
50 void OnDataAvailable(UnixSocket*) override;
51
52 const 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<UnixSocket> sock;
60 BufferedFrameDeserializer frame_deserializer;
Florian Mayerd16508e2018-03-02 17:06:40 +000061 base::ScopedFile received_fd;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000062 };
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_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000087 std::map<ServiceID, ExposedService> services_;
88 std::unique_ptr<UnixSocket> sock_; // The listening socket.
89 std::map<ClientID, std::unique_ptr<ClientConnection>> clients_;
90 std::map<UnixSocket*, ClientConnection*> clients_by_socket_;
91 ServiceID last_service_id_ = 0;
92 ClientID last_client_id_ = 0;
Primiano Tucci68323b02017-12-18 10:54:13 +010093 base::WeakPtrFactory<HostImpl> weak_ptr_factory_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000094 PERFETTO_THREAD_CHECKER(thread_checker_)
95};
96
97} // namespace ipc
98} // namespace perfetto
99
100#endif // SRC_IPC_HOST_IMPL_H_