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