Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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 INCLUDE_PERFETTO_IPC_HOST_H_ |
| 18 | #define INCLUDE_PERFETTO_IPC_HOST_H_ |
| 19 | |
| 20 | #include <memory> |
| 21 | |
Primiano Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 22 | #include "perfetto/base/scoped_file.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 23 | #include "perfetto/ipc/basic_types.h" |
| 24 | |
| 25 | namespace perfetto { |
| 26 | |
| 27 | namespace base { |
| 28 | class TaskRunner; |
| 29 | } // namespace base |
| 30 | |
| 31 | namespace ipc { |
| 32 | |
| 33 | class 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. |
| 39 | class 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 Tucci | 9b5d336 | 2017-12-21 21:42:17 +0100 | [diff] [blame] | 45 | |
| 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 Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 51 | virtual ~Host(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 52 | |
| 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_ |