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_CLIENT_H_ |
| 18 | #define INCLUDE_PERFETTO_IPC_CLIENT_H_ |
| 19 | |
| 20 | #include <functional> |
| 21 | #include <memory> |
| 22 | |
| 23 | #include "perfetto/base/scoped_file.h" |
| 24 | #include "perfetto/base/weak_ptr.h" |
| 25 | #include "perfetto/ipc/basic_types.h" |
| 26 | |
| 27 | namespace perfetto { |
| 28 | |
| 29 | namespace base { |
| 30 | class TaskRunner; |
| 31 | } // namespace base |
| 32 | |
| 33 | namespace ipc { |
| 34 | class ServiceProxy; |
| 35 | |
| 36 | // The client-side class that talks to the host over the socket and multiplexes |
| 37 | // requests coming from the various autogenerated ServiceProxy stubs. |
| 38 | // This is meant to be used by the user code as follows: |
| 39 | // auto client = Client::CreateInstance("socket_name", task_runner); |
| 40 | // std::unique_ptr<GreeterService> svc(new GreeterService()); |
| 41 | // client.BindService(svc); |
| 42 | // svc.OnConnect([] () { |
| 43 | // svc.SayHello(..., ...); |
| 44 | // }); |
| 45 | class Client { |
| 46 | public: |
| 47 | static std::unique_ptr<Client> CreateInstance(const char* socket_name, |
| 48 | base::TaskRunner*); |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 49 | virtual ~Client(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 50 | |
| 51 | virtual void BindService(base::WeakPtr<ServiceProxy>) = 0; |
| 52 | |
| 53 | // There is no need to call this method explicitly. Destroying the |
| 54 | // ServiceProxy instance is sufficient and will automatically unbind it. This |
| 55 | // method is exposed only for the ServiceProxy destructor. |
| 56 | virtual void UnbindService(ServiceID) = 0; |
| 57 | |
| 58 | // Returns (with move semantics) the last file descriptor received on the IPC |
| 59 | // channel. No buffering is performed: if a service sends two file descriptors |
| 60 | // and the caller doesn't read them immediately, the first one will be |
| 61 | // automatically closed when the second is received (and will hit a DCHECK in |
| 62 | // debug builds). |
| 63 | virtual base::ScopedFile TakeReceivedFD() = 0; |
| 64 | }; |
| 65 | |
| 66 | } // namespace ipc |
| 67 | } // namespace perfetto |
| 68 | |
| 69 | #endif // INCLUDE_PERFETTO_IPC_CLIENT_H_ |