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 | #include "ipc/service_proxy.h" |
| 18 | |
| 19 | #include <utility> |
| 20 | |
| 21 | #include "base/logging.h" |
| 22 | #include "base/weak_ptr.h" |
| 23 | #include "google/protobuf/message_lite.h" |
| 24 | #include "ipc/service_descriptor.h" |
| 25 | #include "ipc/src/client_impl.h" |
| 26 | |
| 27 | namespace perfetto { |
| 28 | namespace ipc { |
| 29 | |
| 30 | ServiceProxy::ServiceProxy(EventListener* event_listener) |
| 31 | : weak_ptr_factory_(this), event_listener_(event_listener) {} |
| 32 | |
| 33 | ServiceProxy::~ServiceProxy() { |
| 34 | if (client_ && connected()) |
| 35 | client_->UnbindService(service_id_); |
| 36 | }; |
| 37 | |
| 38 | void ServiceProxy::InitializeBinding( |
| 39 | base::WeakPtr<Client> client, |
| 40 | ServiceID service_id, |
| 41 | std::map<std::string, MethodID> remote_method_ids) { |
| 42 | client_ = client; |
| 43 | service_id_ = service_id; |
| 44 | remote_method_ids_ = std::move(remote_method_ids); |
| 45 | } |
| 46 | |
| 47 | void ServiceProxy::BeginInvoke(const std::string& method_name, |
| 48 | const ProtoMessage& request, |
| 49 | DeferredBase reply) { |
| 50 | // |reply| will auto-resolve if it gets out of scope early. |
| 51 | if (!connected()) { |
| 52 | PERFETTO_DCHECK(false); |
| 53 | return; |
| 54 | } |
| 55 | if (!client_) |
| 56 | return; // The Client object has been destroyed in the meantime. |
| 57 | |
| 58 | auto remote_method_it = remote_method_ids_.find(method_name); |
| 59 | RequestID request_id = 0; |
| 60 | if (remote_method_it != remote_method_ids_.end()) { |
| 61 | request_id = |
| 62 | static_cast<ClientImpl*>(client_.get()) |
| 63 | ->BeginInvoke(service_id_, method_name, remote_method_it->second, |
| 64 | request, weak_ptr_factory_.GetWeakPtr()); |
Primiano Tucci | 3052b1a | 2017-11-14 10:51:01 +0000 | [diff] [blame] | 65 | } else { |
| 66 | PERFETTO_DLOG("Cannot find method \"%s\" on the host", method_name.c_str()); |
Primiano Tucci | ed0ce25 | 2017-11-09 19:35:10 +0000 | [diff] [blame] | 67 | } |
| 68 | if (!request_id) |
| 69 | return; |
| 70 | PERFETTO_DCHECK(pending_callbacks_.count(request_id) == 0); |
| 71 | pending_callbacks_.emplace(request_id, std::move(reply)); |
| 72 | } |
| 73 | |
| 74 | void ServiceProxy::EndInvoke(RequestID request_id, |
| 75 | std::unique_ptr<ProtoMessage> result, |
| 76 | bool has_more) { |
| 77 | auto callback_it = pending_callbacks_.find(request_id); |
| 78 | if (callback_it == pending_callbacks_.end()) { |
| 79 | PERFETTO_DCHECK(false); |
| 80 | return; |
| 81 | } |
| 82 | DeferredBase& reply_callback = callback_it->second; |
| 83 | AsyncResult<ProtoMessage> reply(std::move(result), has_more); |
| 84 | reply_callback.Resolve(std::move(reply)); |
| 85 | if (!has_more) |
| 86 | pending_callbacks_.erase(callback_it); |
| 87 | } |
| 88 | |
| 89 | void ServiceProxy::OnConnect(bool success) { |
Primiano Tucci | 3052b1a | 2017-11-14 10:51:01 +0000 | [diff] [blame] | 90 | if (success) { |
| 91 | PERFETTO_DCHECK(service_id_); |
| 92 | return event_listener_->OnConnect(); |
| 93 | } |
| 94 | return event_listener_->OnDisconnect(); |
Primiano Tucci | ed0ce25 | 2017-11-09 19:35:10 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void ServiceProxy::OnDisconnect() { |
| 98 | pending_callbacks_.clear(); // Will Reject() all the pending callbacks. |
| 99 | event_listener_->OnDisconnect(); |
| 100 | } |
| 101 | |
| 102 | base::WeakPtr<ServiceProxy> ServiceProxy::GetWeakPtr() const { |
| 103 | return weak_ptr_factory_.GetWeakPtr(); |
| 104 | } |
| 105 | |
| 106 | } // namespace ipc |
| 107 | } // namespace perfetto |