1 /* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <unistd.h> 17 #include "nlohmann/json.hpp" 18 #include "ipc_transactor.h" 19 20 namespace OHOS::perftest { 21 using namespace std; 22 using namespace nlohmann; 23 using namespace OHOS; 24 using Message = MessageParcel &; 25 OnRemoteRequest(uint32_t code,Message data,Message reply,MessageOption & option)26 int ApiCallerStub::OnRemoteRequest(uint32_t code, Message data, Message reply, MessageOption &option) 27 { 28 if (data.ReadInterfaceToken() != GetDescriptor()) { 29 return -1; 30 } 31 if (code == TRANS_ID_CALL) { 32 // IPC io: verify on write 33 ApiCallInfo call; 34 string paramListStr; 35 call.apiId_ = data.ReadString(); 36 call.callerObjRef_ = data.ReadString(); 37 paramListStr = data.ReadString(); 38 call.paramList_ = json::parse(paramListStr, nullptr, false); 39 DCHECK(!call.paramList_.is_discarded()); 40 ApiReplyInfo result; 41 Call(call, result); 42 auto ret = reply.WriteString(result.resultValue_.dump()) && reply.WriteUint32(result.exception_.code_) && 43 reply.WriteString(result.exception_.message_); 44 return ret ? 0 : -1; 45 } else if (code == TRANS_ID_SET_BACKCALLER) { 46 reply.WriteBool(SetBackCaller(data.ReadRemoteObject())); 47 return 0; 48 } else { 49 return IPCObjectStub::OnRemoteRequest(code, data, reply, option); 50 } 51 } 52 Call(const ApiCallInfo & call,ApiReplyInfo & result)53 void ApiCallerStub::Call(const ApiCallInfo &call, ApiReplyInfo &result) 54 { 55 DCHECK(handler_ != nullptr); 56 handler_(call, result); 57 } 58 SetBackCaller(const sptr<IRemoteObject> & caller)59 bool ApiCallerStub::SetBackCaller(const sptr<IRemoteObject> &caller) 60 { 61 if (backcallerHandler_ == nullptr) { 62 LOG_W("No backcallerHandler set!"); 63 return false; 64 } 65 backcallerHandler_(caller); 66 return true; 67 } 68 SetCallHandler(ApiCallHandler handler)69 void ApiCallerStub::SetCallHandler(ApiCallHandler handler) 70 { 71 handler_ = handler; 72 } 73 SetBackCallerHandler(function<void (sptr<IRemoteObject>)> handler)74 void ApiCallerStub::SetBackCallerHandler(function<void(sptr<IRemoteObject>)> handler) 75 { 76 backcallerHandler_ = handler; 77 } 78 ApiCallerProxy(const sptr<IRemoteObject> & impl)79 ApiCallerProxy::ApiCallerProxy(const sptr<IRemoteObject> &impl) : IRemoteProxy<IApiCaller>(impl) {} 80 Call(const ApiCallInfo & call,ApiReplyInfo & result)81 void ApiCallerProxy::Call(const ApiCallInfo &call, ApiReplyInfo &result) 82 { 83 MessageOption option; 84 MessageParcel data; 85 MessageParcel reply; 86 // IPC io: verify on write 87 auto ret = data.WriteInterfaceToken(GetDescriptor()) && data.WriteString(call.apiId_) && 88 data.WriteString(call.callerObjRef_) && data.WriteString(call.paramList_.dump()); 89 if (!ret || Remote()->SendRequest(TRANS_ID_CALL, data, reply, option) != 0) { 90 result.exception_ = ApiCallErr(ERR_INTERNAL, "IPC SendRequest failed"); 91 result.resultValue_ = nullptr; 92 } else { 93 result.resultValue_ = json::parse(reply.ReadString(), nullptr, false); 94 DCHECK(!result.resultValue_.is_discarded()); 95 result.exception_.code_ = static_cast<ErrCode>(reply.ReadUint32()); 96 result.exception_.message_ = reply.ReadString(); 97 } 98 } 99 SetBackCaller(const OHOS::sptr<IRemoteObject> & caller)100 bool ApiCallerProxy::SetBackCaller(const OHOS::sptr<IRemoteObject> &caller) 101 { 102 MessageOption option; 103 MessageParcel data; 104 MessageParcel reply; 105 auto writeStat = data.WriteInterfaceToken(GetDescriptor()) && data.WriteRemoteObject(caller); 106 if (!writeStat || (Remote()->SendRequest(TRANS_ID_SET_BACKCALLER, data, reply, option) != 0)) { 107 LOG_E("IPC SendRequest failed"); 108 return false; 109 } 110 return reply.ReadBool(); 111 } 112 SetRemoteDeathCallback(const sptr<IRemoteObject::DeathRecipient> & callback)113 bool ApiCallerProxy::SetRemoteDeathCallback(const sptr<IRemoteObject::DeathRecipient> &callback) 114 { 115 return Remote()->AddDeathRecipient(callback); 116 } 117 UnsetRemoteDeathCallback(const sptr<OHOS::IRemoteObject::DeathRecipient> & callback)118 bool ApiCallerProxy::UnsetRemoteDeathCallback(const sptr<OHOS::IRemoteObject::DeathRecipient> &callback) 119 { 120 return Remote()->RemoveDeathRecipient(callback); 121 } 122 OnRemoteDied(const wptr<IRemoteObject> & remote)123 void DeathRecipientForwarder::OnRemoteDied(const wptr<IRemoteObject> &remote) 124 { 125 if (handler_ != nullptr) { 126 handler_(); 127 } 128 } 129 } // namespace OHOS::perftest