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     UdmfConversion::InitValueObject(unifiedData);
67     if (!ITypesUtil::Marshal(reply, status, key)) {
68         ZLOGE("Marshal status or key failed, status: %{public}d, key: %{public}s", status, key.c_str());
69         return E_WRITE_PARCEL_ERROR;
70     }
71     return E_OK;
72 }
73 
OnGetData(MessageParcel & data,MessageParcel & reply)74 int32_t UdmfServiceStub::OnGetData(MessageParcel &data, MessageParcel &reply)
75 {
76     ZLOGI("start");
77     QueryOption query;
78     if (!ITypesUtil::Unmarshal(data, query)) {
79         ZLOGE("Unmarshal queryOption failed!");
80         return E_READ_PARCEL_ERROR;
81     }
82     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
83     query.tokenId = token;
84     UnifiedData unifiedData;
85     int32_t status = GetData(query, unifiedData);
86     UdmfConversion::InitValueObject(unifiedData);
87     if (!ITypesUtil::Marshal(reply, status, unifiedData)) {
88         ZLOGE("Marshal status or unifiedData failed, status: %{public}d", status);
89         return E_WRITE_PARCEL_ERROR;
90     }
91     return E_OK;
92 }
93 
OnGetBatchData(MessageParcel & data,MessageParcel & reply)94 int32_t UdmfServiceStub::OnGetBatchData(MessageParcel &data, MessageParcel &reply)
95 {
96     ZLOGI("start");
97     QueryOption query;
98     if (!ITypesUtil::Unmarshal(data, query)) {
99         ZLOGE("Unmarshal queryOption failed!");
100         return E_READ_PARCEL_ERROR;
101     }
102     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
103     query.tokenId = token;
104     std::vector<UnifiedData> unifiedDataSet;
105     int32_t status = GetBatchData(query, unifiedDataSet);
106     UdmfConversion::InitValueObject(unifiedDataSet);
107     if (!ITypesUtil::Marshal(reply, status, unifiedDataSet)) {
108         ZLOGE("Marshal status or unifiedDataSet failed, status: %{public}d", status);
109         return E_WRITE_PARCEL_ERROR;
110     }
111     return E_OK;
112 }
113 
OnUpdateData(MessageParcel & data,MessageParcel & reply)114 int32_t UdmfServiceStub::OnUpdateData(MessageParcel &data, MessageParcel &reply)
115 {
116     ZLOGI("start");
117     QueryOption query;
118     UnifiedData unifiedData;
119     if (!ITypesUtil::Unmarshal(data, query, unifiedData)) {
120         ZLOGE("Unmarshal queryOption or unifiedData failed!");
121         return E_READ_PARCEL_ERROR;
122     }
123     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
124     query.tokenId = token;
125     int32_t status = UpdateData(query, unifiedData);
126     if (!ITypesUtil::Marshal(reply, status)) {
127         ZLOGE("Marshal status failed, status: %{public}d", status);
128         return E_WRITE_PARCEL_ERROR;
129     }
130     return E_OK;
131 }
132 
OnDeleteData(MessageParcel & data,MessageParcel & reply)133 int32_t UdmfServiceStub::OnDeleteData(MessageParcel &data, MessageParcel &reply)
134 {
135     ZLOGI("start");
136     QueryOption query;
137     if (!ITypesUtil::Unmarshal(data, query)) {
138         ZLOGE("Unmarshal queryOption failed!");
139         return E_READ_PARCEL_ERROR;
140     }
141     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
142     query.tokenId = token;
143     std::vector<UnifiedData> unifiedDataSet;
144     int32_t status = DeleteData(query, unifiedDataSet);
145     UdmfConversion::InitValueObject(unifiedDataSet);
146     if (!ITypesUtil::Marshal(reply, status, unifiedDataSet)) {
147         ZLOGE("Marshal status or unifiedDataSet failed, status: %{public}d", status);
148         return E_WRITE_PARCEL_ERROR;
149     }
150     return E_OK;
151 }
152 
OnGetSummary(MessageParcel & data,MessageParcel & reply)153 int32_t UdmfServiceStub::OnGetSummary(MessageParcel &data, MessageParcel &reply)
154 {
155     ZLOGI("start");
156     QueryOption query;
157     if (!ITypesUtil::Unmarshal(data, query)) {
158         ZLOGE("Unmarshal query failed");
159         return E_READ_PARCEL_ERROR;
160     }
161     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
162     query.tokenId = token;
163     Summary summary;
164     int32_t status = GetSummary(query, summary);
165     if (!ITypesUtil::Marshal(reply, status, summary)) {
166         ZLOGE("Marshal summary failed, key: %{public}s", query.key.c_str());
167         return E_WRITE_PARCEL_ERROR;
168     }
169     return E_OK;
170 }
171 
OnAddPrivilege(MessageParcel & data,MessageParcel & reply)172 int32_t UdmfServiceStub::OnAddPrivilege(MessageParcel &data, MessageParcel &reply)
173 {
174     ZLOGI("start");
175     QueryOption query;
176     Privilege privilege;
177     if (!ITypesUtil::Unmarshal(data, query, privilege)) {
178         ZLOGE("Unmarshal query and privilege failed");
179         return E_READ_PARCEL_ERROR;
180     }
181     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
182     query.tokenId = token;
183     int32_t status = AddPrivilege(query, privilege);
184     if (!ITypesUtil::Marshal(reply, status)) {
185         ZLOGE("Marshal status failed, key: %{public}s", query.key.c_str());
186         return E_WRITE_PARCEL_ERROR;
187     }
188     return E_OK;
189 }
190 
OnSync(MessageParcel & data,MessageParcel & reply)191 int32_t UdmfServiceStub::OnSync(MessageParcel &data, MessageParcel &reply)
192 {
193     ZLOGI("start");
194     QueryOption query;
195     std::vector<std::string> devices;
196     if (!ITypesUtil::Unmarshal(data, query, devices)) {
197         ZLOGE("Unmarshal query and devices failed");
198         return E_READ_PARCEL_ERROR;
199     }
200     uint32_t token = static_cast<uint32_t>(IPCSkeleton::GetCallingTokenID());
201     query.tokenId = token;
202     int32_t status = Sync(query, devices);
203     if (!ITypesUtil::Marshal(reply, status)) {
204         ZLOGE("Marshal status failed, key: %{public}s", query.key.c_str());
205         return E_WRITE_PARCEL_ERROR;
206     }
207     return E_OK;
208 }
209 
OnIsRemoteData(MessageParcel & data,MessageParcel & reply)210 int32_t UdmfServiceStub::OnIsRemoteData(MessageParcel &data, MessageParcel &reply)
211 {
212     QueryOption query;
213     if (!ITypesUtil::Unmarshal(data, query)) {
214         ZLOGE("Unmarshal query failed");
215         return E_READ_PARCEL_ERROR;
216     }
217     bool result = false;
218     int32_t status = IsRemoteData(query, result);
219     if (!ITypesUtil::Marshal(reply, status, result)) {
220         ZLOGE("Marshal IsRemoteData result failed, key: %{public}s", query.key.c_str());
221         return E_WRITE_PARCEL_ERROR;
222     }
223     return E_OK;
224 }
225 
OnSetAppShareOption(MessageParcel & data,MessageParcel & reply)226 int32_t UdmfServiceStub::OnSetAppShareOption(MessageParcel &data, MessageParcel &reply)
227 {
228     std::string intention;
229     int32_t shareOption;
230     if (!ITypesUtil::Unmarshal(data, intention, shareOption)) {
231         ZLOGE("Unmarshal query failed");
232         return E_READ_PARCEL_ERROR;
233     }
234     int32_t status = SetAppShareOption(intention, shareOption);
235     if (!ITypesUtil::Marshal(reply, status)) {
236         ZLOGE("Marshal OnSetAppShareOption status failed, status: %{public}d", status);
237         return E_WRITE_PARCEL_ERROR;
238     }
239     return E_OK;
240 }
241 
OnGetAppShareOption(MessageParcel & data,MessageParcel & reply)242 int32_t UdmfServiceStub::OnGetAppShareOption(MessageParcel &data, MessageParcel &reply)
243 {
244     std::string intention;
245     int32_t shareOption = SHARE_OPTIONS_BUTT;
246     if (!ITypesUtil::Unmarshal(data, intention)) {
247         ZLOGE("Unmarshal query failed");
248         return E_READ_PARCEL_ERROR;
249     }
250     int32_t status = GetAppShareOption(intention, shareOption);
251     if (!ITypesUtil::Marshal(reply, status, shareOption)) {
252         ZLOGE("Marshal OnGetAppShareOption status failed, status: %{public}d", status);
253         return E_WRITE_PARCEL_ERROR;
254     }
255     return E_OK;
256 }
257 
OnRemoveAppShareOption(MessageParcel & data,MessageParcel & reply)258 int32_t UdmfServiceStub::OnRemoveAppShareOption(MessageParcel &data, MessageParcel &reply)
259 {
260     std::string intention;
261     if (!ITypesUtil::Unmarshal(data, intention)) {
262         ZLOGE("Unmarshal query failed");
263         return E_READ_PARCEL_ERROR;
264     }
265     int32_t status = RemoveAppShareOption(intention);
266     if (!ITypesUtil::Marshal(reply, status)) {
267         ZLOGE("Marshal OnRemoveAppShareOption status failed, status: %{public}d", status);
268         return E_WRITE_PARCEL_ERROR;
269     }
270     return E_OK;
271 }
272 } // namespace UDMF
273 } // namespace OHOS