blob: 6faeb13a4e80cab06a4d5227fb7e9eebc18c1d07 [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_PROXY_H_
18#define INCLUDE_PERFETTO_IPC_SERVICE_PROXY_H_
19
20#include "perfetto/ipc/basic_types.h"
21
22#include <assert.h>
23
24#include <functional>
25#include <map>
26#include <memory>
27#include <string>
28
29#include "perfetto/base/weak_ptr.h"
30#include "perfetto/ipc/deferred.h"
31
32namespace perfetto {
33namespace ipc {
34
35class Client;
36class ServiceDescriptor;
37
38// The base class for the client-side autogenerated stubs that forward method
39// invocations to the host. All the methods of this class are meant to be called
40// only by the autogenerated code.
41class ServiceProxy {
42 public:
43 class EventListener {
44 public:
Primiano Tucci3cbb10a2018-04-10 17:52:40 +010045 virtual ~EventListener();
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000046
47 // Called once after Client::BindService() if the ServiceProxy has been
48 // successfully bound to the host. It is possible to start sending IPC
49 // requests soon after this.
50 virtual void OnConnect() {}
51
52 // Called if the connection fails to be established or drops after having
53 // been established.
54 virtual void OnDisconnect() {}
55 };
56
57 // Guarantees that no callback will happen after this object has been
58 // destroyed. The caller has to guarantee that the |event_listener| stays
59 // alive at least as long as the ServiceProxy instance.
60 explicit ServiceProxy(EventListener*);
61 virtual ~ServiceProxy();
62
63 void InitializeBinding(base::WeakPtr<Client>,
64 ServiceID,
65 std::map<std::string, MethodID>);
66
67 // Called by the IPC methods in the autogenerated classes.
68 void BeginInvoke(const std::string& method_name,
69 const ProtoMessage& request,
Florian Mayerd16508e2018-03-02 17:06:40 +000070 DeferredBase reply,
71 int fd = -1);
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000072
73 // Called by ClientImpl.
74 // |reply_args| == nullptr means request failure.
75 void EndInvoke(RequestID,
76 std::unique_ptr<ProtoMessage> reply_arg,
77 bool has_more);
78
79 // Called by ClientImpl.
80 void OnConnect(bool success);
81 void OnDisconnect();
82 bool connected() const { return service_id_ != 0; }
83
84 base::WeakPtr<ServiceProxy> GetWeakPtr() const;
85
86 // Implemented by the autogenerated class.
87 virtual const ServiceDescriptor& GetDescriptor() = 0;
88
89 private:
90 base::WeakPtr<Client> client_;
91 ServiceID service_id_ = 0;
92 std::map<std::string, MethodID> remote_method_ids_;
93 std::map<RequestID, DeferredBase> pending_callbacks_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000094 EventListener* const event_listener_;
Primiano Tucci68323b02017-12-18 10:54:13 +010095 base::WeakPtrFactory<ServiceProxy> weak_ptr_factory_;
Primiano Tucci4f9b6d72017-12-05 20:59:16 +000096};
97
98} // namespace ipc
99} // namespace perfetto
100
101#endif // INCLUDE_PERFETTO_IPC_SERVICE_PROXY_H_