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