blob: bbb6a33d47aab358ee0be083fac5782b4ff2d862 [file] [log] [blame]
Primiano Tucci420e47e2017-11-14 11:31:49 +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 IPC_SRC_HOST_IMPL_H_
18#define IPC_SRC_HOST_IMPL_H_
19
20#include <map>
21#include <set>
22#include <string>
23#include <vector>
24
Primiano Tucci420e47e2017-11-14 11:31:49 +000025#include "ipc/deferred.h"
26#include "ipc/host.h"
27#include "ipc/src/buffered_frame_deserializer.h"
28#include "ipc/src/unix_socket.h"
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080029#include "perfetto_base/task_runner.h"
30#include "perfetto_base/thread_checker.h"
Primiano Tucci420e47e2017-11-14 11:31:49 +000031
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*);
40 ~HostImpl() override;
41
42 // Host implementation.
43 bool ExposeService(std::unique_ptr<Service>) override;
44
45 // UnixSocket::EventListener implementation.
46 void OnNewIncomingConnection(UnixSocket*,
47 std::unique_ptr<UnixSocket>) override;
48 void OnDisconnect(UnixSocket*) override;
49 void OnDataAvailable(UnixSocket*) override;
50
51 const UnixSocket* sock() const { return sock_.get(); }
52
53 private:
54 // Owns the per-client receive buffer (BufferedFrameDeserializer).
55 struct ClientConnection {
56 ~ClientConnection();
57 ClientID id;
58 std::unique_ptr<UnixSocket> sock;
59 BufferedFrameDeserializer frame_deserializer;
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
Primiano Tuccif54cae42017-11-21 19:37:13 +000082 static void SendFrame(ClientConnection*, const Frame&, int fd = -1);
Primiano Tucci420e47e2017-11-14 11:31:49 +000083
84 base::TaskRunner* const task_runner_;
85 base::WeakPtrFactory<HostImpl> weak_ptr_factory_;
86 std::map<ServiceID, ExposedService> services_;
87 std::unique_ptr<UnixSocket> sock_; // The listening socket.
88 std::map<ClientID, std::unique_ptr<ClientConnection>> clients_;
89 std::map<UnixSocket*, ClientConnection*> clients_by_socket_;
90 ServiceID last_service_id_ = 0;
91 ClientID last_client_id_ = 0;
92 PERFETTO_THREAD_CHECKER(thread_checker_)
93};
94
95} // namespace ipc
96} // namespace perfetto
97
98#endif // IPC_SRC_HOST_IMPL_H_