• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
use_shmem_emulation()55   bool use_shmem_emulation() { return use_shmem_emulation_; }
56 
57  private:
58   friend class HostImpl;
59   ClientInfo client_info_;
60   // This is a pointer because the received fd needs to remain owned by the
61   // ClientConnection, as we will provide it to all method invocations
62   // for that client until one of them calls Service::TakeReceivedFD.
63   //
64   // Different clients might have sent different FDs so this cannot be owned
65   // here.
66   //
67   // Note that this means that there can always only be one outstanding
68   // invocation per client that supplies an FD and the client needs to
69   // wait for this one to return before calling another one.
70   base::ScopedFile* received_fd_;
71 
72   // Whether the socket needs to emulate shared memory buffer. Set by HostImpl
73   // when the service is exposed.
74   bool use_shmem_emulation_ = false;
75 };
76 
77 }  // namespace ipc
78 }  // namespace perfetto
79 
80 #endif  // INCLUDE_PERFETTO_EXT_IPC_SERVICE_H_
81