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