Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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: |
Primiano Tucci | 3cbb10a | 2018-04-10 17:52:40 +0100 | [diff] [blame] | 45 | virtual ~EventListener(); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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. |
| 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 Mayer | d16508e | 2018-03-02 17:06:40 +0000 | [diff] [blame] | 70 | DeferredBase reply, |
| 71 | int fd = -1); |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 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(); |
| 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 Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 94 | EventListener* const event_listener_; |
Primiano Tucci | 68323b0 | 2017-12-18 10:54:13 +0100 | [diff] [blame] | 95 | base::WeakPtrFactory<ServiceProxy> weak_ptr_factory_; |
Primiano Tucci | 4f9b6d7 | 2017-12-05 20:59:16 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
| 98 | } // namespace ipc |
| 99 | } // namespace perfetto |
| 100 | |
| 101 | #endif // INCLUDE_PERFETTO_IPC_SERVICE_PROXY_H_ |