Primiano Tucci | ed0ce25 | 2017-11-09 19:35:10 +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 IPC_SRC_CLIENT_IMPL_H_ |
| 18 | #define IPC_SRC_CLIENT_IMPL_H_ |
| 19 | |
Primiano Tucci | ed0ce25 | 2017-11-09 19:35:10 +0000 | [diff] [blame] | 20 | #include "ipc/client.h" |
| 21 | #include "ipc/src/buffered_frame_deserializer.h" |
| 22 | #include "ipc/src/unix_socket.h" |
Oystein Eftevaag | dd727e4 | 2017-12-05 08:49:55 -0800 | [diff] [blame^] | 23 | #include "perfetto_base/scoped_file.h" |
| 24 | #include "perfetto_base/task_runner.h" |
Primiano Tucci | ed0ce25 | 2017-11-09 19:35:10 +0000 | [diff] [blame] | 25 | |
Primiano Tucci | 2ee254a | 2017-11-15 00:38:48 +0000 | [diff] [blame] | 26 | #include "ipc/src/wire_protocol.pb.h" |
Primiano Tucci | ed0ce25 | 2017-11-09 19:35:10 +0000 | [diff] [blame] | 27 | |
| 28 | #include <list> |
| 29 | #include <map> |
| 30 | #include <memory> |
| 31 | |
| 32 | namespace perfetto { |
| 33 | |
| 34 | namespace base { |
| 35 | class TaskRunner; |
| 36 | } // namespace base |
| 37 | |
| 38 | namespace ipc { |
| 39 | |
| 40 | class ServiceDescriptor; |
| 41 | |
| 42 | class ClientImpl : public Client, public UnixSocket::EventListener { |
| 43 | public: |
| 44 | ClientImpl(const char* socket_name, base::TaskRunner*); |
| 45 | ~ClientImpl() override; |
| 46 | |
| 47 | // Client implementation. |
| 48 | void BindService(base::WeakPtr<ServiceProxy>) override; |
| 49 | void UnbindService(ServiceID) override; |
Primiano Tucci | f54cae4 | 2017-11-21 19:37:13 +0000 | [diff] [blame] | 50 | base::ScopedFile TakeReceivedFD() override; |
Primiano Tucci | ed0ce25 | 2017-11-09 19:35:10 +0000 | [diff] [blame] | 51 | |
| 52 | // UnixSocket::EventListener implementation. |
| 53 | void OnConnect(UnixSocket*, bool connected) override; |
| 54 | void OnDisconnect(UnixSocket*) override; |
| 55 | void OnDataAvailable(UnixSocket*) override; |
| 56 | |
| 57 | RequestID BeginInvoke(ServiceID, |
| 58 | const std::string& method_name, |
| 59 | MethodID remote_method_id, |
| 60 | const ProtoMessage& method_args, |
| 61 | base::WeakPtr<ServiceProxy>); |
| 62 | |
| 63 | private: |
| 64 | struct QueuedRequest { |
| 65 | QueuedRequest(); |
| 66 | int type = 0; // From Frame::msg_case(), see wire_protocol.proto. |
| 67 | RequestID request_id = 0; |
| 68 | base::WeakPtr<ServiceProxy> service_proxy; |
| 69 | |
| 70 | // Only for type == kMsgInvokeMethod. |
| 71 | std::string method_name; |
| 72 | }; |
| 73 | |
| 74 | ClientImpl(const ClientImpl&) = delete; |
| 75 | ClientImpl& operator=(const ClientImpl&) = delete; |
| 76 | |
| 77 | bool SendFrame(const Frame&); |
| 78 | void OnFrameReceived(const Frame&); |
| 79 | void OnBindServiceReply(QueuedRequest, const Frame::BindServiceReply&); |
| 80 | void OnInvokeMethodReply(QueuedRequest, const Frame::InvokeMethodReply&); |
| 81 | |
| 82 | std::unique_ptr<UnixSocket> sock_; |
| 83 | base::TaskRunner* const task_runner_; |
| 84 | RequestID last_request_id_ = 0; |
| 85 | BufferedFrameDeserializer frame_deserializer_; |
Primiano Tucci | f54cae4 | 2017-11-21 19:37:13 +0000 | [diff] [blame] | 86 | base::ScopedFile received_fd_; |
Primiano Tucci | ed0ce25 | 2017-11-09 19:35:10 +0000 | [diff] [blame] | 87 | std::map<RequestID, QueuedRequest> queued_requests_; |
| 88 | std::map<ServiceID, base::WeakPtr<ServiceProxy>> service_bindings_; |
| 89 | base::WeakPtrFactory<Client> weak_ptr_factory_; |
| 90 | |
| 91 | // Queue of calls to BindService() that happened before the socket connected. |
| 92 | std::list<base::WeakPtr<ServiceProxy>> queued_bindings_; |
| 93 | }; |
| 94 | |
| 95 | } // namespace ipc |
| 96 | } // namespace perfetto |
| 97 | |
| 98 | #endif // IPC_SRC_CLIENT_IMPL_H_ |