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_SERVICE_H_
18 #define INCLUDE_PERFETTO_EXT_IPC_SERVICE_H_
19 
20 #include "perfetto/base/logging.h"
21 #include "perfetto/ext/base/scoped_file.h"
22 #include "perfetto/ext/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:
32   virtual ~Service();
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.
OnClientDisconnected()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.
client_info()44   const ClientInfo& client_info() {
45     PERFETTO_DCHECK(client_info_.is_valid());
46     return client_info_;
47   }
48 
TakeReceivedFD()49   base::ScopedFile TakeReceivedFD() {
50     if (received_fd_)
51       return std::move(*received_fd_);
52     return base::ScopedFile();
53   }
54 
55  private:
56   friend class HostImpl;
57   ClientInfo client_info_;
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_;
69 };
70 
71 }  // namespace ipc
72 }  // namespace perfetto
73 
74 #endif  // INCLUDE_PERFETTO_EXT_IPC_SERVICE_H_
75