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