• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #define LOG_TAG "KVDBServiceStub"
16 #include "kvdb_service_stub.h"
17 
18 #include "ipc_skeleton.h"
19 #include "kv_types_util.h"
20 #include "log_print.h"
21 #include "utils/anonymous.h"
22 #include "utils/constant.h"
23 namespace OHOS::DistributedKv {
24 using namespace OHOS::DistributedData;
25 static constexpr const char *SLASH = "/";
26 static constexpr const char *BACK_SLASH  = "\\";
27 static constexpr const char *POINT = "..";
28 
29 const KVDBServiceStub::Handler
30     KVDBServiceStub::HANDLERS[static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_BUTT)] = {
31     &KVDBServiceStub::OnGetStoreIds,
32     &KVDBServiceStub::OnBeforeCreate,
33     &KVDBServiceStub::OnAfterCreate,
34     &KVDBServiceStub::OnDelete,
35     &KVDBServiceStub::OnSync,
36     &KVDBServiceStub::OnRegServiceNotifier,
37     &KVDBServiceStub::OnUnregServiceNotifier,
38     &KVDBServiceStub::OnSetSyncParam,
39     &KVDBServiceStub::OnGetSyncParam,
40     &KVDBServiceStub::OnEnableCap,
41     &KVDBServiceStub::OnDisableCap,
42     &KVDBServiceStub::OnSetCapability,
43     &KVDBServiceStub::OnAddSubInfo,
44     &KVDBServiceStub::OnRmvSubInfo,
45     &KVDBServiceStub::OnSubscribe,
46     &KVDBServiceStub::OnUnsubscribe,
47     &KVDBServiceStub::OnGetBackupPassword,
48     &KVDBServiceStub::OnCloudSync,
49     &KVDBServiceStub::OnNotifyDataChange,
50     &KVDBServiceStub::OnSetConfig,
51     &KVDBServiceStub::OnPutSwitch,
52     &KVDBServiceStub::OnGetSwitch,
53     &KVDBServiceStub::OnSubscribeSwitchData,
54     &KVDBServiceStub::OnUnsubscribeSwitchData,
55     &KVDBServiceStub::OnClose,
56     &KVDBServiceStub::OnRemoveDeviceData,
57 };
58 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)59 int KVDBServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
60 {
61     ZLOGI("code:%{public}u callingPid:%{public}u", code, IPCSkeleton::GetCallingPid());
62     std::u16string local = KVDBServiceStub::GetDescriptor();
63     std::u16string remote = data.ReadInterfaceToken();
64     if (local != remote) {
65         ZLOGE("local is not equal to remote");
66         return -1;
67     }
68 
69     if (static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_HEAD) > code ||
70         code >= static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_BUTT) || HANDLERS[code] == nullptr) {
71         ZLOGE("not support code:%{public}u, BUTT:%{public}d", code,
72               static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_BUTT));
73         return -1;
74     }
75     auto [status, storeInfo] = GetStoreInfo(code, data);
76     if (status != ERR_NONE) {
77         return status;
78     }
79     if (CheckPermission(code, storeInfo)) {
80         return (this->*HANDLERS[code])({ storeInfo.bundleName }, { storeInfo.storeId }, data, reply);
81     }
82     ZLOGE("PERMISSION_DENIED uid:%{public}d appId:%{public}s storeId:%{public}s", storeInfo.uid,
83         storeInfo.bundleName.c_str(), Anonymous::Change(storeInfo.storeId).c_str());
84 
85     if (!ITypesUtil::Marshal(reply, static_cast<int32_t>(PERMISSION_DENIED))) {
86         ZLOGE("Marshal PERMISSION_DENIED code:%{public}u appId:%{public}s storeId:%{public}s", code,
87             storeInfo.bundleName.c_str(), Anonymous::Change(storeInfo.storeId).c_str());
88         return IPC_STUB_WRITE_PARCEL_ERR;
89     }
90     return ERR_NONE;
91 }
92 
GetStoreInfo(uint32_t code,MessageParcel & data)93 std::pair<int32_t, KVDBServiceStub::StoreInfo> KVDBServiceStub::GetStoreInfo(uint32_t code, MessageParcel &data)
94 {
95     AppId appId;
96     StoreId storeId;
97     if (!ITypesUtil::Unmarshal(data, appId, storeId)) {
98         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s",
99             appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str());
100         return { IPC_STUB_INVALID_DATA_ERR, StoreInfo() };
101     }
102     appId.appId = Constant::TrimCopy(appId.appId);
103     storeId.storeId = Constant::TrimCopy(storeId.storeId);
104     StoreInfo info;
105     info.uid = IPCSkeleton::GetCallingUid();
106     info.tokenId = IPCSkeleton::GetCallingTokenID();
107     info.bundleName = std::move(appId.appId);
108     info.storeId = std::move(storeId.storeId);
109     return { ERR_NONE, info };
110 }
111 
CheckPermission(uint32_t code,const StoreInfo & storeInfo)112 bool KVDBServiceStub::CheckPermission(uint32_t code, const StoreInfo &storeInfo)
113 {
114     if (code >= static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_PUT_SWITCH) &&
115         code <= static_cast<uint32_t>(KVDBServiceInterfaceCode::TRANS_UNSUBSCRIBE_SWITCH_DATA)) {
116         return CheckerManager::GetInstance().IsSwitches(storeInfo);
117     }
118     return CheckerManager::GetInstance().IsValid(storeInfo);
119 }
120 
OnGetStoreIds(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)121 int32_t KVDBServiceStub::OnGetStoreIds(
122     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
123 {
124     int32_t subUser;
125     if (!ITypesUtil::Unmarshal(data, subUser)) {
126         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
127             Anonymous::Change(storeId.storeId).c_str());
128         return IPC_STUB_INVALID_DATA_ERR;
129     }
130     std::vector<StoreId> storeIds;
131     int32_t status = GetStoreIds(appId, subUser, storeIds);
132     if (!ITypesUtil::Marshal(reply, status, storeIds)) {
133         ZLOGE("Marshal status:0x%{public}d storeIds:%{public}zu", status, storeIds.size());
134         return IPC_STUB_WRITE_PARCEL_ERR;
135     }
136     return ERR_NONE;
137 }
138 
OnBeforeCreate(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)139 int32_t KVDBServiceStub::OnBeforeCreate(
140     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
141 {
142     Options options;
143     if (!ITypesUtil::Unmarshal(data, options)) {
144         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
145             Anonymous::Change(storeId.storeId).c_str());
146         return IPC_STUB_INVALID_DATA_ERR;
147     }
148     if (!IsValidField(appId.appId) || !IsValidField(storeId.storeId) || !IsValidField(options.hapName)) {
149         return IPC_STUB_INVALID_DATA_ERR;
150     }
151     int32_t status = BeforeCreate(appId, storeId, options);
152     if (!ITypesUtil::Marshal(reply, status)) {
153         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
154             Anonymous::Change(storeId.storeId).c_str());
155         return IPC_STUB_WRITE_PARCEL_ERR;
156     }
157     return ERR_NONE;
158 }
159 
OnAfterCreate(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)160 int32_t KVDBServiceStub::OnAfterCreate(
161     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
162 {
163     Options options;
164     std::vector<uint8_t> password;
165     if (!ITypesUtil::Unmarshal(data, options, password)) {
166         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
167             Anonymous::Change(storeId.storeId).c_str());
168         return IPC_STUB_INVALID_DATA_ERR;
169     }
170     if (!IsValidField(appId.appId) || !IsValidField(storeId.storeId) || !IsValidField(options.hapName)) {
171         return IPC_STUB_INVALID_DATA_ERR;
172     }
173     int32_t status = AfterCreate(appId, storeId, options, password);
174     password.assign(password.size(), 0);
175     if (!ITypesUtil::Marshal(reply, status)) {
176         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
177             Anonymous::Change(storeId.storeId).c_str());
178         return IPC_STUB_WRITE_PARCEL_ERR;
179     }
180     return ERR_NONE;
181 }
182 
OnDelete(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)183 int32_t KVDBServiceStub::OnDelete(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
184 {
185     int32_t subUser;
186     if (!ITypesUtil::Unmarshal(data, subUser)) {
187         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
188             Anonymous::Change(storeId.storeId).c_str());
189         return IPC_STUB_INVALID_DATA_ERR;
190     }
191     int32_t status = Delete(appId, storeId, subUser);
192     if (!ITypesUtil::Marshal(reply, status)) {
193         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
194             Anonymous::Change(storeId.storeId).c_str());
195         return IPC_STUB_WRITE_PARCEL_ERR;
196     }
197     return ERR_NONE;
198 }
199 
OnClose(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)200 int32_t KVDBServiceStub::OnClose(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
201 {
202     int32_t subUser;
203     if (!ITypesUtil::Unmarshal(data, subUser)) {
204         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
205             Anonymous::Change(storeId.storeId).c_str());
206         return IPC_STUB_INVALID_DATA_ERR;
207     }
208     int32_t status = Close(appId, storeId, subUser);
209     if (!ITypesUtil::Marshal(reply, status)) {
210         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
211             Anonymous::Change(storeId.storeId).c_str());
212         return IPC_STUB_WRITE_PARCEL_ERR;
213     }
214     return ERR_NONE;
215 }
216 
OnSync(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)217 int32_t KVDBServiceStub::OnSync(const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
218 {
219     SyncInfo syncInfo;
220     int32_t subUser;
221     if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.mode, syncInfo.devices, syncInfo.delay, syncInfo.query,
222         subUser, syncInfo.isRetry)) {
223         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
224             Anonymous::Change(storeId.storeId).c_str());
225         return IPC_STUB_INVALID_DATA_ERR;
226     }
227     int32_t status = Sync(appId, storeId, subUser, syncInfo);
228     if (!ITypesUtil::Marshal(reply, status)) {
229         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
230             Anonymous::Change(storeId.storeId).c_str());
231         return IPC_STUB_WRITE_PARCEL_ERR;
232     }
233     return ERR_NONE;
234 }
235 
OnCloudSync(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)236 int32_t KVDBServiceStub::OnCloudSync(
237     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
238 {
239     SyncInfo syncInfo;
240     if (!ITypesUtil::Unmarshal(data, syncInfo.seqId)) {
241         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
242               Anonymous::Change(storeId.storeId).c_str());
243         return IPC_STUB_INVALID_DATA_ERR;
244     }
245     int32_t status = CloudSync(appId, storeId, syncInfo);
246     if (!ITypesUtil::Marshal(reply, status)) {
247         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
248             Anonymous::Change(storeId.storeId).c_str());
249         return IPC_STUB_WRITE_PARCEL_ERR;
250     }
251     return ERR_NONE;
252 }
253 
OnNotifyDataChange(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)254 int32_t KVDBServiceStub::OnNotifyDataChange(
255     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
256 {
257     uint64_t delay = 0;
258     if (!ITypesUtil::Unmarshal(data, delay)) {
259         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
260             Anonymous::Change(storeId.storeId).c_str());
261         return IPC_STUB_INVALID_DATA_ERR;
262     }
263     int32_t status = NotifyDataChange(appId, storeId, delay);
264     if (!ITypesUtil::Marshal(reply, status)) {
265         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s",
266             status, appId.appId.c_str(), Anonymous::Change(storeId.storeId).c_str());
267         return IPC_STUB_WRITE_PARCEL_ERR;
268     }
269     return ERR_NONE;
270 }
271 
OnRegServiceNotifier(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)272 int32_t KVDBServiceStub::OnRegServiceNotifier(
273     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
274 {
275     sptr<IRemoteObject> remoteObj;
276     if (!ITypesUtil::Unmarshal(data, remoteObj)) {
277         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
278             Anonymous::Change(storeId.storeId).c_str());
279         return IPC_STUB_INVALID_DATA_ERR;
280     }
281     auto notifier = (remoteObj == nullptr) ? nullptr : iface_cast<IKVDBNotifier>(remoteObj);
282     int32_t status = RegServiceNotifier(appId, notifier);
283     if (!ITypesUtil::Marshal(reply, status)) {
284         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
285             Anonymous::Change(storeId.storeId).c_str());
286         return IPC_STUB_WRITE_PARCEL_ERR;
287     }
288     return ERR_NONE;
289 }
290 
OnUnregServiceNotifier(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)291 int32_t KVDBServiceStub::OnUnregServiceNotifier(
292     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
293 {
294     int32_t status = UnregServiceNotifier(appId);
295     if (!ITypesUtil::Marshal(reply, status)) {
296         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
297             Anonymous::Change(storeId.storeId).c_str());
298         return IPC_STUB_WRITE_PARCEL_ERR;
299     }
300     return ERR_NONE;
301 }
302 
OnSetSyncParam(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)303 int32_t KVDBServiceStub::OnSetSyncParam(
304     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
305 {
306     KvSyncParam syncParam;
307     int32_t subUser;
308     if (!ITypesUtil::Unmarshal(data, syncParam.allowedDelayMs, subUser)) {
309         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
310             Anonymous::Change(storeId.storeId).c_str());
311         return IPC_STUB_INVALID_DATA_ERR;
312     }
313     int32_t status = SetSyncParam(appId, storeId, subUser, syncParam);
314     if (!ITypesUtil::Marshal(reply, status)) {
315         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
316             Anonymous::Change(storeId.storeId).c_str());
317         return IPC_STUB_WRITE_PARCEL_ERR;
318     }
319     return ERR_NONE;
320 }
321 
OnGetSyncParam(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)322 int32_t KVDBServiceStub::OnGetSyncParam(
323     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
324 {
325     int32_t subUser;
326     if (!ITypesUtil::Unmarshal(data, subUser)) {
327         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
328             Anonymous::Change(storeId.storeId).c_str());
329         return IPC_STUB_INVALID_DATA_ERR;
330     }
331     KvSyncParam syncParam;
332     int32_t status = GetSyncParam(appId, storeId, subUser, syncParam);
333     if (!ITypesUtil::Marshal(reply, status, syncParam.allowedDelayMs)) {
334         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
335             Anonymous::Change(storeId.storeId).c_str());
336         return IPC_STUB_WRITE_PARCEL_ERR;
337     }
338     return ERR_NONE;
339 }
340 
OnEnableCap(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)341 int32_t KVDBServiceStub::OnEnableCap(
342     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
343 {
344     int32_t subUser;
345     if (!ITypesUtil::Unmarshal(data, subUser)) {
346         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
347             Anonymous::Change(storeId.storeId).c_str());
348         return IPC_STUB_INVALID_DATA_ERR;
349     }
350     int32_t status = EnableCapability(appId, storeId, subUser);
351     if (!ITypesUtil::Marshal(reply, status)) {
352         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
353             Anonymous::Change(storeId.storeId).c_str());
354         return IPC_STUB_WRITE_PARCEL_ERR;
355     }
356     return ERR_NONE;
357 }
358 
OnDisableCap(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)359 int32_t KVDBServiceStub::OnDisableCap(
360     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
361 {
362     int32_t subUser;
363     if (!ITypesUtil::Unmarshal(data, subUser)) {
364         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
365             Anonymous::Change(storeId.storeId).c_str());
366         return IPC_STUB_INVALID_DATA_ERR;
367     }
368     int32_t status = DisableCapability(appId, storeId, subUser);
369     if (!ITypesUtil::Marshal(reply, status)) {
370         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
371             Anonymous::Change(storeId.storeId).c_str());
372         return IPC_STUB_WRITE_PARCEL_ERR;
373     }
374     return ERR_NONE;
375 }
376 
OnSetCapability(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)377 int32_t KVDBServiceStub::OnSetCapability(
378     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
379 {
380     std::vector<std::string> local;
381     std::vector<std::string> remote;
382     int32_t subUser;
383     if (!ITypesUtil::Unmarshal(data, local, remote, subUser)) {
384         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
385             Anonymous::Change(storeId.storeId).c_str());
386         return IPC_STUB_INVALID_DATA_ERR;
387     }
388     int32_t status = SetCapability(appId, storeId, subUser, local, remote);
389     if (!ITypesUtil::Marshal(reply, status)) {
390         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
391             Anonymous::Change(storeId.storeId).c_str());
392         return IPC_STUB_WRITE_PARCEL_ERR;
393     }
394     return ERR_NONE;
395 }
396 
OnAddSubInfo(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)397 int32_t KVDBServiceStub::OnAddSubInfo(const AppId &appId, const StoreId &storeId, MessageParcel &data,
398     MessageParcel &reply)
399 {
400     SyncInfo syncInfo;
401     int32_t subUser;
402     if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.devices, syncInfo.query, subUser)) {
403         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
404             Anonymous::Change(storeId.storeId).c_str());
405         return IPC_STUB_INVALID_DATA_ERR;
406     }
407     int32_t status = AddSubscribeInfo(appId, storeId, subUser, syncInfo);
408     if (!ITypesUtil::Marshal(reply, status)) {
409         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
410             Anonymous::Change(storeId.storeId).c_str());
411         return IPC_STUB_WRITE_PARCEL_ERR;
412     }
413     return ERR_NONE;
414 }
415 
OnRmvSubInfo(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)416 int32_t KVDBServiceStub::OnRmvSubInfo(const AppId &appId, const StoreId &storeId, MessageParcel &data,
417     MessageParcel &reply)
418 {
419     SyncInfo syncInfo;
420     int32_t subUser;
421     if (!ITypesUtil::Unmarshal(data, syncInfo.seqId, syncInfo.devices, syncInfo.query, subUser)) {
422         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
423             Anonymous::Change(storeId.storeId).c_str());
424         return IPC_STUB_INVALID_DATA_ERR;
425     }
426     int32_t status = RmvSubscribeInfo(appId, storeId, subUser, syncInfo);
427     if (!ITypesUtil::Marshal(reply, status)) {
428         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
429             Anonymous::Change(storeId.storeId).c_str());
430         return IPC_STUB_WRITE_PARCEL_ERR;
431     }
432     return ERR_NONE;
433 }
434 
OnSubscribe(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)435 int32_t KVDBServiceStub::OnSubscribe(
436     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
437 {
438     sptr<IRemoteObject> remoteObj;
439     int32_t subUser;
440     if (!ITypesUtil::Unmarshal(data, remoteObj, subUser)) {
441         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
442             Anonymous::Change(storeId.storeId).c_str());
443         return IPC_STUB_INVALID_DATA_ERR;
444     }
445     auto observer = (remoteObj == nullptr) ? nullptr : iface_cast<IKvStoreObserver>(remoteObj);
446     int32_t status = Subscribe(appId, storeId, subUser, observer);
447     if (!ITypesUtil::Marshal(reply, status)) {
448         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
449             Anonymous::Change(storeId.storeId).c_str());
450         return IPC_STUB_WRITE_PARCEL_ERR;
451     }
452     return ERR_NONE;
453 }
454 
OnUnsubscribe(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)455 int32_t KVDBServiceStub::OnUnsubscribe(
456     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
457 {
458     sptr<IRemoteObject> remoteObj;
459     int32_t subUser;
460     if (!ITypesUtil::Unmarshal(data, remoteObj, subUser)) {
461         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
462             Anonymous::Change(storeId.storeId).c_str());
463         return IPC_STUB_INVALID_DATA_ERR;
464     }
465     auto observer = (remoteObj == nullptr) ? nullptr : iface_cast<IKvStoreObserver>(remoteObj);
466     int32_t status = Unsubscribe(appId, storeId, subUser, observer);
467     if (!ITypesUtil::Marshal(reply, status)) {
468         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
469             Anonymous::Change(storeId.storeId).c_str());
470         return IPC_STUB_WRITE_PARCEL_ERR;
471     }
472     return ERR_NONE;
473 }
474 
OnGetBackupPassword(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)475 int32_t KVDBServiceStub::OnGetBackupPassword(
476     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
477 {
478     int32_t passwordType;
479     int32_t subUser;
480     if (!ITypesUtil::Unmarshal(data, passwordType, subUser)) {
481         ZLOGE("Unmarshal type failed, appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
482             Anonymous::Change(storeId.storeId).c_str());
483         return IPC_STUB_INVALID_DATA_ERR;
484     }
485     std::vector<std::vector<uint8_t>> passwords;
486     int32_t status = GetBackupPassword(appId, storeId, subUser, passwords, passwordType);
487     if (!ITypesUtil::Marshal(reply, status, passwords)) {
488         ZLOGE("Marshal status:0x%{public}x appId:%{public}s storeId:%{public}s", status, appId.appId.c_str(),
489             Anonymous::Change(storeId.storeId).c_str());
490         for (auto &password : passwords) {
491             password.assign(password.size(), 0);
492         }
493         return IPC_STUB_WRITE_PARCEL_ERR;
494     }
495     for (auto &password : passwords) {
496         password.assign(password.size(), 0);
497     }
498     return ERR_NONE;
499 }
500 
OnPutSwitch(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)501 int32_t KVDBServiceStub::OnPutSwitch(
502     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
503 {
504     SwitchData switchData;
505     if (!ITypesUtil::Unmarshal(data, switchData)) {
506         ZLOGE("Unmarshal appId:%{public}s", appId.appId.c_str());
507         return IPC_STUB_INVALID_DATA_ERR;
508     }
509     int32_t status = PutSwitch(appId, switchData);
510     if (!ITypesUtil::Marshal(reply, status)) {
511         ZLOGE("Marshal status:0x%{public}x appId:%{public}s", status, appId.appId.c_str());
512         return IPC_STUB_WRITE_PARCEL_ERR;
513     }
514     return ERR_NONE;
515 }
516 
OnGetSwitch(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)517 int32_t KVDBServiceStub::OnGetSwitch(
518     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
519 {
520     std::string networkId;
521     if (!ITypesUtil::Unmarshal(data, networkId)) {
522         ZLOGE("Unmarshal appId:%{public}s networkId:%{public}s",
523             appId.appId.c_str(), Anonymous::Change(networkId).c_str());
524         return IPC_STUB_INVALID_DATA_ERR;
525     }
526     SwitchData switchData;
527     int32_t status = GetSwitch(appId, networkId, switchData);
528     if (!ITypesUtil::Marshal(reply, status, switchData)) {
529         ZLOGE("Marshal status:0x%{public}x appId:%{public}s networkId:%{public}s",
530             status, appId.appId.c_str(), Anonymous::Change(networkId).c_str());
531         return IPC_STUB_WRITE_PARCEL_ERR;
532     }
533     return ERR_NONE;
534 }
535 
OnSubscribeSwitchData(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)536 int32_t KVDBServiceStub::OnSubscribeSwitchData(
537     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
538 {
539     int32_t status = SubscribeSwitchData(appId);
540     if (!ITypesUtil::Marshal(reply, status)) {
541         ZLOGE("Marshal status:0x%{public}x appId:%{public}s", status, appId.appId.c_str());
542         return IPC_STUB_WRITE_PARCEL_ERR;
543     }
544     return ERR_NONE;
545 }
546 
OnUnsubscribeSwitchData(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)547 int32_t KVDBServiceStub::OnUnsubscribeSwitchData(
548     const AppId &appId, const StoreId &storeId, MessageParcel &data, MessageParcel &reply)
549 {
550     int32_t status = UnsubscribeSwitchData(appId);
551     if (!ITypesUtil::Marshal(reply, status)) {
552         ZLOGE("Marshal status:0x%{public}x appId:%{public}s", status, appId.appId.c_str());
553         return IPC_STUB_WRITE_PARCEL_ERR;
554     }
555     return ERR_NONE;
556 }
557 
OnSetConfig(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)558 int32_t KVDBServiceStub::OnSetConfig(const AppId &appId, const StoreId &storeId, MessageParcel &data,
559     MessageParcel &reply)
560 {
561     StoreConfig storeConfig;
562     if (!ITypesUtil::Unmarshal(data, storeConfig)) {
563         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
564             Anonymous::Change(storeId.storeId).c_str());
565         return IPC_STUB_INVALID_DATA_ERR;
566     }
567     int32_t status = SetConfig(appId, storeId, storeConfig);
568     if (!ITypesUtil::Marshal(reply, status)) {
569         ZLOGE("Marshal status:0x%{public}x appId:%{public}s", status, appId.appId.c_str());
570         return IPC_STUB_WRITE_PARCEL_ERR;
571     }
572     return ERR_NONE;
573 }
574 
OnRemoveDeviceData(const AppId & appId,const StoreId & storeId,MessageParcel & data,MessageParcel & reply)575 int32_t KVDBServiceStub::OnRemoveDeviceData(const AppId &appId, const StoreId &storeId, MessageParcel &data,
576     MessageParcel &reply)
577 {
578     std::string device;
579     int32_t subUser;
580     if (!ITypesUtil::Unmarshal(data, device, subUser)) {
581         ZLOGE("Unmarshal appId:%{public}s storeId:%{public}s", appId.appId.c_str(),
582             Anonymous::Change(storeId.storeId).c_str());
583         return IPC_STUB_INVALID_DATA_ERR;
584     }
585     int32_t status = RemoveDeviceData(appId, storeId, subUser, device);
586     if (!ITypesUtil::Marshal(reply, status)) {
587         ZLOGE("Marshal status:0x%{public}x appId:%{public}s", status, appId.appId.c_str());
588         return IPC_STUB_WRITE_PARCEL_ERR;
589     }
590     return ERR_NONE;
591 }
592 
IsValidField(const std::string & param)593 bool KVDBServiceStub::IsValidField(const std::string &param)
594 {
595     if ((param.find(SLASH) != std::string::npos) || (param.find(BACK_SLASH) != std::string::npos) || (param == POINT)) {
596         ZLOGE("check failed, param is: %{public}s", Anonymous::Change(param).c_str());
597         return false;
598     }
599     return true;
600 }
601 } // namespace OHOS::DistributedKv
602