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_SERVICE_H_ |
| 18 | #define INCLUDE_PERFETTO_IPC_SERVICE_H_ |
| 19 | |
| 20 | #include "perfetto/base/logging.h" |
Florian Mayer | d16508e | 2018-03-02 17:06:40 +0000 | [diff] [blame] | 21 | #include "perfetto/base/scoped_file.h" |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 22 | #include "perfetto/ipc/client_info.h" |
| 23 | |
| 24 | namespace perfetto { |
| 25 | namespace ipc { |
| 26 | |
| 27 | class ServiceDescriptor; |
| 28 | |
| 29 | // The base class for all the autogenerated host-side service interfaces. |
| 30 | class Service { |
| 31 | public: |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 32 | virtual ~Service(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 33 | |
| 34 | // Overridden by the auto-generated class. Provides the list of methods and |
| 35 | // the protobuf (de)serialization functions for their arguments. |
| 36 | virtual const ServiceDescriptor& GetDescriptor() = 0; |
| 37 | |
| 38 | // Invoked when a remote client disconnects. Use client_info() to obtain |
| 39 | // details about the client that disconnected. |
| 40 | virtual void OnClientDisconnected() {} |
| 41 | |
| 42 | // Returns the ClientInfo for the current IPC request. Returns an invalid |
| 43 | // ClientInfo if called outside the scope of an IPC method. |
Florian Mayer | d16508e | 2018-03-02 17:06:40 +0000 | [diff] [blame] | 44 | const ClientInfo& client_info() { |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 45 | PERFETTO_DCHECK(client_info_.is_valid()); |
| 46 | return client_info_; |
| 47 | } |
| 48 | |
Florian Mayer | d16508e | 2018-03-02 17:06:40 +0000 | [diff] [blame] | 49 | base::ScopedFile TakeReceivedFD() { |
| 50 | if (received_fd_) |
| 51 | return std::move(*received_fd_); |
| 52 | return base::ScopedFile(); |
| 53 | } |
| 54 | |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 55 | private: |
| 56 | friend class HostImpl; |
| 57 | ClientInfo client_info_; |
Florian Mayer | d16508e | 2018-03-02 17:06:40 +0000 | [diff] [blame] | 58 | // This is a pointer because the received fd needs to remain owned by the |
| 59 | // ClientConnection, as we will provide it to all method invocations |
| 60 | // for that client until one of them calls Service::TakeReceivedFD. |
| 61 | // |
| 62 | // Different clients might have sent different FDs so this cannot be owned |
| 63 | // here. |
| 64 | // |
| 65 | // Note that this means that there can always only be one outstanding |
| 66 | // invocation per client that supplies an FD and the client needs to |
| 67 | // wait for this one to return before calling another one. |
| 68 | base::ScopedFile* received_fd_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | } // namespace ipc |
| 72 | } // namespace perfetto |
| 73 | |
| 74 | #endif // INCLUDE_PERFETTO_IPC_SERVICE_H_ |