• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 #define LOG_TAG "UdmfServiceStub"
17 
18 #include "udmf_service_stub.h"
19 
20 #include <vector>
21 #include <string_ex.h>
22 
23 #include "accesstoken_kit.h"
24 #include "ipc_skeleton.h"
25 #include "log_print.h"
26 #include "udmf_conversion.h"
27 #include "udmf_types_util.h"
28 #include "unified_data.h"
29 #include "unified_meta.h"
30 
31 namespace OHOS {
32 namespace UDMF {
33 constexpr UdmfServiceStub::Handler
34     UdmfServiceStub::HANDLERS[static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_BUTT)];
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)35 int UdmfServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
36 {
37     ZLOGI("start##code = %{public}u callingPid:%{public}u callingUid:%{public}u.", code, IPCSkeleton::GetCallingPid(),
38         IPCSkeleton::GetCallingUid());
39     std::u16string myDescripter = UdmfServiceStub::GetDescriptor();
40     std::u16string remoteDescripter = data.ReadInterfaceToken();
41     if (myDescripter != remoteDescripter) {
42         ZLOGE("end##descriptor checked fail,myDescripter = %{public}s,remoteDescripter = %{public}s.",
43             Str16ToStr8(myDescripter).c_str(), Str16ToStr8(remoteDescripter).c_str());
44         return -1;
45     }
46     if (static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_HEAD) > code ||
47         code >= static_cast<uint32_t>(UdmfServiceInterfaceCode::CODE_BUTT)) {
48         return -1;
49     }
50     return (this->*HANDLERS[code])(data, reply);
51 }
52 
OnSetData(MessageParcel & data,MessageParcel & reply)53 int32_t UdmfServiceStub::OnSetData(MessageParcel &data, MessageParcel &reply)
54 {
55     ZLOGI("start");
56     CustomOption customOption;
57     UnifiedData unifiedData;
58     if (!ITypesUtil::Unmarshal(data, customOption, unifiedData)) {
59         ZLOGE("Unmarshal customOption or unifiedData failed!");
60         return E_READ_PARCEL_ERROR;
61     }
62     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
63     customOption.tokenId = token;
64     std::string key;
65     int32_t status = SetData(customOption, unifiedData, key);
66     if (!ITypesUtil::Marshal(reply, status, key)) {
67         ZLOGE("Marshal status or key failed, status: %{public}d, key: %{public}s", status, key.c_str());
68         return E_WRITE_PARCEL_ERROR;
69     }
70     return E_OK;
71 }
72 
OnGetData(MessageParcel & data,MessageParcel & reply)73 int32_t UdmfServiceStub::OnGetData(MessageParcel &data, MessageParcel &reply)
74 {
75     ZLOGI("start");
76     QueryOption query;
77     if (!ITypesUtil::Unmarshal(data, query)) {
78         ZLOGE("Unmarshal queryOption failed!");
79         return E_READ_PARCEL_ERROR;
80     }
81     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
82     query.tokenId = token;
83     UnifiedData unifiedData;
84     int32_t status = GetData(query, unifiedData);
85     if (!ITypesUtil::Marshal(reply, status, unifiedData)) {
86         ZLOGE("Marshal status or unifiedData failed, status: %{public}d", status);
87         return E_WRITE_PARCEL_ERROR;
88     }
89     return E_OK;
90 }
91 
OnGetBatchData(MessageParcel & data,MessageParcel & reply)92 int32_t UdmfServiceStub::OnGetBatchData(MessageParcel &data, MessageParcel &reply)
93 {
94     ZLOGI("start");
95     QueryOption query;
96     if (!ITypesUtil::Unmarshal(data, query)) {
97         ZLOGE("Unmarshal queryOption failed!");
98         return E_READ_PARCEL_ERROR;
99     }
100     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
101     query.tokenId = token;
102     std::vector<UnifiedData> unifiedDataSet;
103     int32_t status = GetBatchData(query, unifiedDataSet);
104     if (!ITypesUtil::Marshal(reply, status, unifiedDataSet)) {
105         ZLOGE("Marshal status or unifiedDataSet failed, status: %{public}d", status);
106         return E_WRITE_PARCEL_ERROR;
107     }
108     return E_OK;
109 }
110 
OnUpdateData(MessageParcel & data,MessageParcel & reply)111 int32_t UdmfServiceStub::OnUpdateData(MessageParcel &data, MessageParcel &reply)
112 {
113     ZLOGI("start");
114     QueryOption query;
115     UnifiedData unifiedData;
116     if (!ITypesUtil::Unmarshal(data, query, unifiedData)) {
117         ZLOGE("Unmarshal queryOption or unifiedData failed!");
118         return E_READ_PARCEL_ERROR;
119     }
120     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
121     query.tokenId = token;
122     int32_t status = UpdateData(query, unifiedData);
123     if (!ITypesUtil::Marshal(reply, status)) {
124         ZLOGE("Marshal status failed, status: %{public}d", status);
125         return E_WRITE_PARCEL_ERROR;
126     }
127     return E_OK;
128 }
129 
OnDeleteData(MessageParcel & data,MessageParcel & reply)130 int32_t UdmfServiceStub::OnDeleteData(MessageParcel &data, MessageParcel &reply)
131 {
132     ZLOGI("start");
133     QueryOption query;
134     if (!ITypesUtil::Unmarshal(data, query)) {
135         ZLOGE("Unmarshal queryOption failed!");
136         return E_READ_PARCEL_ERROR;
137     }
138     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
139     query.tokenId = token;
140     std::vector<UnifiedData> unifiedDataSet;
141     int32_t status = DeleteData(query, unifiedDataSet);
142     if (!ITypesUtil::Marshal(reply, status, unifiedDataSet)) {
143         ZLOGE("Marshal status or unifiedDataSet failed, status: %{public}d", status);
144         return E_WRITE_PARCEL_ERROR;
145     }
146     return E_OK;
147 }
148 
OnGetSummary(MessageParcel & data,MessageParcel & reply)149 int32_t UdmfServiceStub::OnGetSummary(MessageParcel &data, MessageParcel &reply)
150 {
151     ZLOGI("start");
152     QueryOption query;
153     if (!ITypesUtil::Unmarshal(data, query)) {
154         ZLOGE("Unmarshal query failed");
155         return E_READ_PARCEL_ERROR;
156     }
157     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
158     query.tokenId = token;
159     Summary summary;
160     int32_t status = GetSummary(query, summary);
161     if (!ITypesUtil::Marshal(reply, status, summary)) {
162         ZLOGE("Marshal summary failed, key: %{public}s", query.key.c_str());
163         return E_WRITE_PARCEL_ERROR;
164     }
165     return E_OK;
166 }
167 
OnAddPrivilege(MessageParcel & data,MessageParcel & reply)168 int32_t UdmfServiceStub::OnAddPrivilege(MessageParcel &data, MessageParcel &reply)
169 {
170     ZLOGI("start");
171     QueryOption query;
172     Privilege privilege;
173     if (!ITypesUtil::Unmarshal(data, query, privilege)) {
174         ZLOGE("Unmarshal query and privilege failed");
175         return E_READ_PARCEL_ERROR;
176     }
177     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
178     query.tokenId = token;
179     int32_t status = AddPrivilege(query, privilege);
180     if (!ITypesUtil::Marshal(reply, status)) {
181         ZLOGE("Marshal status failed, key: %{public}s", query.key.c_str());
182         return E_WRITE_PARCEL_ERROR;
183     }
184     return E_OK;
185 }
186 
OnSync(MessageParcel & data,MessageParcel & reply)187 int32_t UdmfServiceStub::OnSync(MessageParcel &data, MessageParcel &reply)
188 {
189     ZLOGI("start");
190     QueryOption query;
191     std::vector<std::string> devices;
192     if (!ITypesUtil::Unmarshal(data, query, devices)) {
193         ZLOGE("Unmarshal query and devices failed");
194         return E_READ_PARCEL_ERROR;
195     }
196     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
197     query.tokenId = token;
198     int32_t status = Sync(query, devices);
199     if (!ITypesUtil::Marshal(reply, status)) {
200         ZLOGE("Marshal status failed, key: %{public}s", query.key.c_str());
201         return E_WRITE_PARCEL_ERROR;
202     }
203     return E_OK;
204 }
205 
OnIsRemoteData(MessageParcel & data,MessageParcel & reply)206 int32_t UdmfServiceStub::OnIsRemoteData(MessageParcel &data, MessageParcel &reply)
207 {
208     QueryOption query;
209     if (!ITypesUtil::Unmarshal(data, query)) {
210         ZLOGE("Unmarshal query failed");
211         return E_READ_PARCEL_ERROR;
212     }
213     bool result = false;
214     int32_t status = IsRemoteData(query, result);
215     if (!ITypesUtil::Marshal(reply, status, result)) {
216         ZLOGE("Marshal IsRemoteData result failed, key: %{public}s", query.key.c_str());
217         return E_WRITE_PARCEL_ERROR;
218     }
219     return E_OK;
220 }
221 
OnSetAppShareOption(MessageParcel & data,MessageParcel & reply)222 int32_t UdmfServiceStub::OnSetAppShareOption(MessageParcel &data, MessageParcel &reply)
223 {
224     std::string intention;
225     int32_t shareOption;
226     if (!ITypesUtil::Unmarshal(data, intention, shareOption)) {
227         ZLOGE("Unmarshal query failed");
228         return E_READ_PARCEL_ERROR;
229     }
230     int32_t status = SetAppShareOption(intention, shareOption);
231     if (!ITypesUtil::Marshal(reply, status)) {
232         ZLOGE("Marshal OnSetAppShareOption status failed, status: %{public}d", status);
233         return E_WRITE_PARCEL_ERROR;
234     }
235     return E_OK;
236 }
237 
OnGetAppShareOption(MessageParcel & data,MessageParcel & reply)238 int32_t UdmfServiceStub::OnGetAppShareOption(MessageParcel &data, MessageParcel &reply)
239 {
240     std::string intention;
241     int32_t shareOption = SHARE_OPTIONS_BUTT;
242     if (!ITypesUtil::Unmarshal(data, intention)) {
243         ZLOGE("Unmarshal query failed");
244         return E_READ_PARCEL_ERROR;
245     }
246     int32_t status = GetAppShareOption(intention, shareOption);
247     if (!ITypesUtil::Marshal(reply, status, shareOption)) {
248         ZLOGE("Marshal OnGetAppShareOption status failed, status: %{public}d", status);
249         return E_WRITE_PARCEL_ERROR;
250     }
251     return E_OK;
252 }
253 
OnRemoveAppShareOption(MessageParcel & data,MessageParcel & reply)254 int32_t UdmfServiceStub::OnRemoveAppShareOption(MessageParcel &data, MessageParcel &reply)
255 {
256     std::string intention;
257     if (!ITypesUtil::Unmarshal(data, intention)) {
258         ZLOGE("Unmarshal query failed");
259         return E_READ_PARCEL_ERROR;
260     }
261     int32_t status = RemoveAppShareOption(intention);
262     if (!ITypesUtil::Marshal(reply, status)) {
263         ZLOGE("Marshal OnRemoveAppShareOption status failed, status: %{public}d", status);
264         return E_WRITE_PARCEL_ERROR;
265     }
266     return E_OK;
267 }
268 
OnObtainAsynProcess(MessageParcel & data,MessageParcel & reply)269 int32_t UdmfServiceStub::OnObtainAsynProcess(MessageParcel &data, MessageParcel &reply)
270 {
271     ZLOGD("start");
272     AsyncProcessInfo processInfo;
273     if (!ITypesUtil::Unmarshal(data, processInfo)) {
274         ZLOGE("Unmarshal processInfo failed");
275         return E_READ_PARCEL_ERROR;
276     }
277     int32_t status = ObtainAsynProcess(processInfo);
278     if (!ITypesUtil::Marshal(reply, status, processInfo)) {
279         ZLOGE("Marshal status or processInfo failed, status: %{public}d", status);
280         return E_WRITE_PARCEL_ERROR;
281     }
282     return E_OK;
283 }
284 
OnClearAsynProcessByKey(MessageParcel & data,MessageParcel & reply)285 int32_t UdmfServiceStub::OnClearAsynProcessByKey(MessageParcel &data, MessageParcel &reply)
286 {
287     ZLOGD("start");
288     std::string businessUdKey;
289     if (!ITypesUtil::Unmarshal(data, businessUdKey)) {
290         ZLOGE("Unmarshal businessUdKey failed!");
291         return E_READ_PARCEL_ERROR;
292     }
293     int32_t status = ClearAsynProcessByKey(businessUdKey);
294     if (!ITypesUtil::Marshal(reply, status)) {
295         ZLOGE("Marshal status failed, status: %{public}d", status);
296         return E_WRITE_PARCEL_ERROR;
297     }
298     return E_OK;
299 }
300 } // namespace UDMF
301 } // namespace OHOS