blob: a3abb998797f952375c22c96dba4f6ab50e05726 [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 INCLUDE_PERFETTO_IPC_HOST_H_
18#define INCLUDE_PERFETTO_IPC_HOST_H_
19
20#include <memory>
21
Primiano Tucci9b5d3362017-12-21 21:42:17 +010022#include "perfetto/base/scoped_file.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000023#include "perfetto/ipc/basic_types.h"
24
25namespace perfetto {
26
27namespace base {
28class TaskRunner;
29} // namespace base
30
31namespace ipc {
32
33class Service;
34
35// The host-side of the IPC layer. This class acts as a registry and request
36// dispatcher. It listen on the UnixSocket |socket_name| for incoming requests
37// (coming Client instances) and dispatches their requests to the various
38// Services exposed.
39class Host {
40 public:
41 // Creates an instance and starts listening on the given |socket_name|.
42 // Returns nullptr if listening on the socket fails.
43 static std::unique_ptr<Host> CreateInstance(const char* socket_name,
44 base::TaskRunner*);
Primiano Tucci9b5d3362017-12-21 21:42:17 +010045
46 // Like the above but takes a file descriptor to a pre-bound unix socket.
47 // Returns nullptr if listening on the socket fails.
48 static std::unique_ptr<Host> CreateInstance(base::ScopedFile socket_fd,
49 base::TaskRunner*);
50
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010051 virtual ~Host();
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000052
53 // Registers a new service and makes it available to remote IPC peers.
54 // All the exposed Service instances will be destroyed when destroying the
55 // Host instance if ExposeService suceeds and returns true, or immediately
56 // after the call in case of failure.
57 // Returns true if the register has been succesfully registered, false in case
58 // of errors (e.g., another service with the same name is already registered).
59 virtual bool ExposeService(std::unique_ptr<Service>) = 0;
60};
61
62} // namespace ipc
63} // namespace perfetto
64
65#endif // INCLUDE_PERFETTO_IPC_HOST_H_