blob: 18dc1be6d754142c48aec7b057568f23150608fc [file] [log] [blame]
Primiano Tucci4f9b6d72017-12-05 20:59:16 +00001/*
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 Mayerd16508e2018-03-02 17:06:40 +000021#include "perfetto/base/scoped_file.h"
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000022#include "perfetto/ipc/client_info.h"
23
24namespace perfetto {
25namespace ipc {
26
27class ServiceDescriptor;
28
29// The base class for all the autogenerated host-side service interfaces.
30class Service {
31 public:
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010032 virtual ~Service();
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000033
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 Mayerd16508e2018-03-02 17:06:40 +000044 const ClientInfo& client_info() {
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000045 PERFETTO_DCHECK(client_info_.is_valid());
46 return client_info_;
47 }
48
Florian Mayerd16508e2018-03-02 17:06:40 +000049 base::ScopedFile TakeReceivedFD() {
50 if (received_fd_)
51 return std::move(*received_fd_);
52 return base::ScopedFile();
53 }
54
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000055 private:
56 friend class HostImpl;
57 ClientInfo client_info_;
Florian Mayerd16508e2018-03-02 17:06:40 +000058 // 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 Tucci4f9b6d72017-12-05 20:59:16 +000069};
70
71} // namespace ipc
72} // namespace perfetto
73
74#endif // INCLUDE_PERFETTO_IPC_SERVICE_H_