• 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_PROXY_H_
18 #define INCLUDE_PERFETTO_EXT_IPC_SERVICE_PROXY_H_
19 
20 #include "perfetto/ext/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/export.h"
30 #include "perfetto/ext/base/weak_ptr.h"
31 #include "perfetto/ext/ipc/deferred.h"
32 
33 namespace perfetto {
34 namespace ipc {
35 
36 class Client;
37 class ServiceDescriptor;
38 
39 // The base class for the client-side autogenerated stubs that forward method
40 // invocations to the host. All the methods of this class are meant to be called
41 // only by the autogenerated code.
42 class PERFETTO_EXPORT_COMPONENT ServiceProxy {
43  public:
44   class EventListener {
45    public:
46     virtual ~EventListener();
47 
48     // Called once after Client::BindService() if the ServiceProxy has been
49     // successfully bound to the host. It is possible to start sending IPC
50     // requests soon after this.
OnConnect()51     virtual void OnConnect() {}
52 
53     // Called if the connection fails to be established or drops after having
54     // been established.
OnDisconnect()55     virtual void OnDisconnect() {}
56   };
57 
58   // Guarantees that no callback will happen after this object has been
59   // destroyed. The caller has to guarantee that the |event_listener| stays
60   // alive at least as long as the ServiceProxy instance.
61   explicit ServiceProxy(EventListener*);
62   virtual ~ServiceProxy();
63 
64   void InitializeBinding(base::WeakPtr<Client>,
65                          ServiceID,
66                          std::map<std::string, MethodID>);
67 
68   // Called by the IPC methods in the autogenerated classes.
69   void BeginInvoke(const std::string& method_name,
70                    const ProtoMessage& request,
71                    DeferredBase reply,
72                    int fd = -1);
73 
74   // Called by ClientImpl.
75   // |reply_args| == nullptr means request failure.
76   void EndInvoke(RequestID,
77                  std::unique_ptr<ProtoMessage> reply_arg,
78                  bool has_more);
79 
80   // Called by ClientImpl.
81   void OnConnect(bool success);
82   void OnDisconnect();
connected()83   bool connected() const { return service_id_ != 0; }
84 
85   base::WeakPtr<ServiceProxy> GetWeakPtr() const;
86 
87   // Implemented by the autogenerated class.
88   virtual const ServiceDescriptor& GetDescriptor() = 0;
89 
90  private:
91   base::WeakPtr<Client> client_;
92   ServiceID service_id_ = 0;
93   std::map<std::string, MethodID> remote_method_ids_;
94   std::map<RequestID, DeferredBase> pending_callbacks_;
95   EventListener* const event_listener_;
96   base::WeakPtrFactory<ServiceProxy> weak_ptr_factory_;  // Keep last.
97 };
98 
99 }  // namespace ipc
100 }  // namespace perfetto
101 
102 #endif  // INCLUDE_PERFETTO_EXT_IPC_SERVICE_PROXY_H_
103