blob: bce6bfc8033e53132c3a37e8fcc27b94619f6c26 [file] [log] [blame]
Primiano Tuccied0ce252017-11-09 19:35:10 +00001/*
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
Primiano Tuccied0ce252017-11-09 19:35:10 +000021#include "google/protobuf/message_lite.h"
22#include "ipc/service_descriptor.h"
23#include "ipc/src/client_impl.h"
Oystein Eftevaagdd727e42017-12-05 08:49:55 -080024#include "perfetto_base/logging.h"
25#include "perfetto_base/weak_ptr.h"
Primiano Tuccied0ce252017-11-09 19:35:10 +000026
27namespace perfetto {
28namespace ipc {
29
30ServiceProxy::ServiceProxy(EventListener* event_listener)
31 : weak_ptr_factory_(this), event_listener_(event_listener) {}
32
33ServiceProxy::~ServiceProxy() {
34 if (client_ && connected())
35 client_->UnbindService(service_id_);
36};
37
38void 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
47void 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 Tucci3052b1a2017-11-14 10:51:01 +000065 } else {
66 PERFETTO_DLOG("Cannot find method \"%s\" on the host", method_name.c_str());
Primiano Tuccied0ce252017-11-09 19:35:10 +000067 }
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
74void 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
89void ServiceProxy::OnConnect(bool success) {
Primiano Tucci3052b1a2017-11-14 10:51:01 +000090 if (success) {
91 PERFETTO_DCHECK(service_id_);
92 return event_listener_->OnConnect();
93 }
94 return event_listener_->OnDisconnect();
Primiano Tuccied0ce252017-11-09 19:35:10 +000095}
96
97void ServiceProxy::OnDisconnect() {
98 pending_callbacks_.clear(); // Will Reject() all the pending callbacks.
99 event_listener_->OnDisconnect();
100}
101
102base::WeakPtr<ServiceProxy> ServiceProxy::GetWeakPtr() const {
103 return weak_ptr_factory_.GetWeakPtr();
104}
105
106} // namespace ipc
107} // namespace perfetto