• 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_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 
32 namespace perfetto {
33 namespace ipc {
34 
35 class Client;
36 class 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.
41 class ServiceProxy {
42  public:
43   class EventListener {
44    public:
45     virtual ~EventListener();
46 
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.
OnConnect()50     virtual void OnConnect() {}
51 
52     // Called if the connection fails to be established or drops after having
53     // been established.
OnDisconnect()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,
70                    DeferredBase reply,
71                    int fd = -1);
72 
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();
connected()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_;
94   EventListener* const event_listener_;
95   base::WeakPtrFactory<ServiceProxy> weak_ptr_factory_;  // Keep last.
96 };
97 
98 }  // namespace ipc
99 }  // namespace perfetto
100 
101 #endif  // INCLUDE_PERFETTO_IPC_SERVICE_PROXY_H_
102