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 #ifndef OHOS_MEDIA_IPC_USER_INNER_IPC_CLIENT_H 17 #define OHOS_MEDIA_IPC_USER_INNER_IPC_CLIENT_H 18 19 #include <memory> 20 #include <string> 21 #include <unordered_map> 22 23 #include "datashare_helper.h" 24 #include "medialibrary_errno.h" 25 #include "message_option.h" 26 #include "message_parcel.h" 27 #include "media_log.h" 28 #include "media_req_vo.h" 29 #include "media_resp_vo.h" 30 #include "media_empty_obj_vo.h" 31 32 namespace OHOS::Media::IPC { 33 class UserInnerIPCClient { 34 private: 35 std::string traceId_; 36 int32_t userId_ = -1; 37 std::unordered_map<std::string, std::string> header_; 38 std::shared_ptr<DataShare::DataShareHelper> dataShareHelper_; 39 40 private: 41 int32_t HeaderMarshalling(MessageParcel &data); 42 virtual int32_t UserDefineFunc(MessageParcel &data, MessageParcel &reply, MessageOption &option); 43 44 public: // getters & setters 45 UserInnerIPCClient &SetTraceId(const std::string &traceId); 46 std::string GetTraceId() const; 47 UserInnerIPCClient &SetUserId(const int32_t &userId); 48 int32_t GetUserId() const; 49 std::unordered_map<std::string, std::string> GetHeader() const; 50 UserInnerIPCClient &SetHeader(const std::unordered_map<std::string, std::string> &header); 51 UserInnerIPCClient &SetDataShareHelper(std::shared_ptr<DataShare::DataShareHelper> dataShareHelper); 52 53 private: 54 template <class REQ> BodyMarshalling(MessageParcel & data,const uint32_t code,const REQ & reqBody)55 int32_t BodyMarshalling(MessageParcel &data, const uint32_t code, const REQ &reqBody) 56 { 57 MediaReqVo<REQ> reqVo; 58 // user define command code. 59 reqVo.SetUserId(userId_); 60 reqVo.SetCode(code); 61 reqVo.SetBody(reqBody); 62 reqVo.SetTraceId(this->GetTraceId()); 63 reqVo.SetHeader(this->GetHeader()); 64 bool isValid = reqVo.Marshalling(data); 65 CHECK_AND_RETURN_RET_LOG(isValid, E_IPC_INVAL_ARG, "Failed to Marshalling"); 66 return E_OK; 67 } 68 template <class RSP> BodyUnmarshalling(MessageParcel & reply,RSP & respBody,int32_t & errCode)69 int32_t BodyUnmarshalling(MessageParcel &reply, RSP &respBody, int32_t &errCode) 70 { 71 MediaRespVo<RSP> respVo; 72 bool isValid = respVo.Unmarshalling(reply); 73 CHECK_AND_RETURN_RET_LOG(isValid, E_IPC_INVAL_ARG, "Failed to UnMarshalling"); 74 respBody = respVo.GetBody(); 75 errCode = respVo.GetErrCode(); 76 return E_OK; 77 } 78 79 public: 80 template <class REQ, class RSP> Call(const uint32_t code,const REQ & reqBody,RSP & respBody)81 int32_t Call(const uint32_t code, const REQ &reqBody, RSP &respBody) 82 { 83 MessageParcel data; 84 MessageParcel reply; 85 MessageOption option; 86 int32_t ret = this->HeaderMarshalling(data); 87 bool errConn = ret != E_OK; 88 CHECK_AND_RETURN_RET(!errConn, ret); 89 // user define request data. 90 ret = this->BodyMarshalling<REQ>(data, code, reqBody); 91 errConn = ret != E_OK; 92 CHECK_AND_RETURN_RET(!errConn, ret); 93 // post user define request to service. 94 ret = this->UserDefineFunc(data, reply, option); 95 errConn = ret != E_OK; 96 CHECK_AND_RETURN_RET_LOG(!errConn, 97 ret, 98 "Failed to connect Server, ret: %{public}d, func: %{public}u, traceId: %{public}s", 99 ret, 100 code, 101 this->GetTraceId().c_str()); 102 // user define response data. 103 int32_t errCode = E_OK; 104 ret = this->BodyUnmarshalling<RSP>(reply, respBody, errCode); 105 errConn = ret != E_OK; 106 CHECK_AND_RETURN_RET(!errConn, ret); 107 return errCode; 108 } 109 template <class REQ> Call(uint32_t code,const REQ & reqBody)110 int32_t Call(uint32_t code, const REQ &reqBody) 111 { 112 MediaEmptyObjVo respBody; 113 return Call<REQ, MediaEmptyObjVo>(code, reqBody, respBody); 114 } Call(uint32_t code)115 int32_t Call(uint32_t code) 116 { 117 MediaEmptyObjVo respBody; 118 return Call<MediaEmptyObjVo, MediaEmptyObjVo>(code, MediaEmptyObjVo(), respBody); 119 } 120 template <class REQ, class RSP> Post(const uint32_t code,const REQ & reqBody,RSP & respBody)121 int32_t Post(const uint32_t code, const REQ &reqBody, RSP &respBody) 122 { 123 return this->Call(code, reqBody, respBody); 124 } 125 template <class REQ> Post(const uint32_t code,const REQ & reqBody)126 int32_t Post(const uint32_t code, const REQ &reqBody) 127 { 128 MediaEmptyObjVo respBody; 129 return this->Call<REQ, MediaEmptyObjVo>(code, reqBody, respBody); 130 } Post(const uint32_t code)131 int32_t Post(const uint32_t code) 132 { 133 MediaEmptyObjVo reqBody; 134 MediaEmptyObjVo respBody; 135 return this->Call<MediaEmptyObjVo, MediaEmptyObjVo>(code, reqBody, respBody); 136 } 137 template <class RSP> Get(const uint32_t code,RSP & respBody)138 int32_t Get(const uint32_t code, RSP &respBody) 139 { 140 MediaEmptyObjVo reqBody; 141 return this->Call<MediaEmptyObjVo, RSP>(code, reqBody, respBody); 142 } Get(const uint32_t code)143 int32_t Get(const uint32_t code) 144 { 145 MediaEmptyObjVo reqBody; 146 MediaEmptyObjVo respBody; 147 return this->Call<MediaEmptyObjVo, MediaEmptyObjVo>(code, reqBody, respBody); 148 } 149 }; 150 } // namespace OHOS::Media::IPC 151 #endif // OHOS_MEDIA_IPC_USER_INNER_IPC_CLIENT_H