• 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 #include "perfetto/ext/ipc/service_proxy.h"
18 
19 #include <utility>
20 
21 #include "perfetto/base/logging.h"
22 #include "perfetto/ext/base/weak_ptr.h"
23 #include "perfetto/ext/ipc/service_descriptor.h"
24 #include "src/ipc/client_impl.h"
25 
26 #include "protos/perfetto/ipc/wire_protocol.gen.h"
27 
28 namespace perfetto {
29 namespace ipc {
30 
ServiceProxy(EventListener * event_listener)31 ServiceProxy::ServiceProxy(EventListener* event_listener)
32     : event_listener_(event_listener), weak_ptr_factory_(this) {}
33 
~ServiceProxy()34 ServiceProxy::~ServiceProxy() {
35   if (client_ && connected())
36     client_->UnbindService(service_id_);
37 }
38 
InitializeBinding(base::WeakPtr<Client> client,ServiceID service_id,std::map<std::string,MethodID> remote_method_ids)39 void ServiceProxy::InitializeBinding(
40     base::WeakPtr<Client> client,
41     ServiceID service_id,
42     std::map<std::string, MethodID> remote_method_ids) {
43   client_ = std::move(client);
44   service_id_ = service_id;
45   remote_method_ids_ = std::move(remote_method_ids);
46 }
47 
BeginInvoke(const std::string & method_name,const ProtoMessage & request,DeferredBase reply,int fd)48 void ServiceProxy::BeginInvoke(const std::string& method_name,
49                                const ProtoMessage& request,
50                                DeferredBase reply,
51                                int fd) {
52   // |reply| will auto-resolve if it gets out of scope early.
53   if (!connected()) {
54     PERFETTO_DFATAL("Not connected.");
55     return;
56   }
57   if (!client_)
58     return;  // The Client object has been destroyed in the meantime.
59 
60   auto remote_method_it = remote_method_ids_.find(method_name);
61   RequestID request_id = 0;
62   const bool drop_reply = !reply.IsBound();
63   if (remote_method_it != remote_method_ids_.end()) {
64     request_id =
65         static_cast<ClientImpl*>(client_.get())
66             ->BeginInvoke(service_id_, method_name, remote_method_it->second,
67                           request, drop_reply, weak_ptr_factory_.GetWeakPtr(),
68                           fd);
69   } else {
70     PERFETTO_DLOG("Cannot find method \"%s\" on the host", method_name.c_str());
71   }
72 
73   // When passing |drop_reply| == true, the returned |request_id| should be 0.
74   PERFETTO_DCHECK(!drop_reply || !request_id);
75 
76   if (!request_id)
77     return;
78   PERFETTO_DCHECK(pending_callbacks_.count(request_id) == 0);
79   pending_callbacks_.emplace(request_id, std::move(reply));
80 }
81 
EndInvoke(RequestID request_id,std::unique_ptr<ProtoMessage> result,bool has_more)82 void ServiceProxy::EndInvoke(RequestID request_id,
83                              std::unique_ptr<ProtoMessage> result,
84                              bool has_more) {
85   auto callback_it = pending_callbacks_.find(request_id);
86   if (callback_it == pending_callbacks_.end()) {
87     // Either we are getting a reply for a method we never invoked, or we are
88     // getting a reply to a method marked drop_reply (that has been invoked
89     // without binding any callback in the Defererd response object).
90     PERFETTO_DFATAL("Unexpected reply received.");
91     return;
92   }
93   DeferredBase& reply_callback = callback_it->second;
94   AsyncResult<ProtoMessage> reply(std::move(result), has_more);
95   reply_callback.Resolve(std::move(reply));
96   if (!has_more)
97     pending_callbacks_.erase(callback_it);
98 }
99 
OnConnect(bool success)100 void ServiceProxy::OnConnect(bool success) {
101   if (success) {
102     PERFETTO_DCHECK(service_id_);
103     return event_listener_->OnConnect();
104   }
105   return event_listener_->OnDisconnect();
106 }
107 
OnDisconnect()108 void ServiceProxy::OnDisconnect() {
109   pending_callbacks_.clear();  // Will Reject() all the pending callbacks.
110   event_listener_->OnDisconnect();
111 }
112 
GetWeakPtr() const113 base::WeakPtr<ServiceProxy> ServiceProxy::GetWeakPtr() const {
114   return weak_ptr_factory_.GetWeakPtr();
115 }
116 
117 }  // namespace ipc
118 }  // namespace perfetto
119