1 /*
2 * Copyright (c) 2021 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 "KvStoreDataServiceProxy"
17
18 #include "ikvstore_data_service.h"
19 #include "constant.h"
20 #include "irdb_service.h"
21 #include "rdb_service_proxy.h"
22 #include "itypes_util.h"
23 #include "message_parcel.h"
24 #include "types.h"
25 #include "log_print.h"
26
27 namespace OHOS {
28 namespace DistributedKv {
29 constexpr KvStoreDataServiceStub::RequestHandler KvStoreDataServiceStub::HANDLERS[SERVICE_CMD_LAST];
KvStoreDataServiceProxy(const sptr<IRemoteObject> & impl)30 KvStoreDataServiceProxy::KvStoreDataServiceProxy(const sptr<IRemoteObject> &impl)
31 : IRemoteProxy<IKvStoreDataService>(impl)
32 {
33 ZLOGI("init data service proxy.");
34 }
35
GetKvStore(const Options & options,const AppId & appId,const StoreId & storeId,std::function<void (sptr<IKvStoreImpl>)> callback)36 Status KvStoreDataServiceProxy::GetKvStore(const Options &options, const AppId &appId, const StoreId &storeId,
37 std::function<void(sptr<IKvStoreImpl>)> callback)
38 {
39 ZLOGI("%s %s", appId.appId.c_str(), storeId.storeId.c_str());
40 MessageParcel data;
41 MessageParcel reply;
42
43 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
44 ZLOGE("write descriptor failed");
45 return Status::IPC_ERROR;
46 }
47
48 // Passing a struct with an std::string field is a potential security exploit.
49 OptionsIpc optionsIpc;
50 optionsIpc.createIfMissing = options.createIfMissing;
51 optionsIpc.encrypt = options.encrypt;
52 optionsIpc.persistant = options.persistant;
53 optionsIpc.backup = options.backup;
54 optionsIpc.autoSync = options.autoSync;
55 optionsIpc.securityLevel = options.securityLevel;
56 optionsIpc.syncPolicy = options.syncPolicy;
57 optionsIpc.kvStoreType = options.kvStoreType;
58 optionsIpc.syncable = options.syncable;
59 optionsIpc.dataOwnership = true; // set default value
60
61 if (!data.WriteBuffer(&optionsIpc, sizeof(optionsIpc)) ||
62 !data.WriteString(appId.appId) ||
63 !data.WriteString(storeId.storeId)) {
64 ZLOGW("failed to write parcel.");
65 return Status::IPC_ERROR;
66 }
67 MessageOption mo { MessageOption::TF_SYNC };
68 int32_t error = Remote()->SendRequest(GETKVSTORE, data, reply, mo);
69 if (error != 0) {
70 ZLOGW("failed to write parcel.");
71 return Status::IPC_ERROR;
72 }
73 Status ret = static_cast<Status>(reply.ReadInt32());
74 if (ret == Status::SUCCESS) {
75 sptr<IRemoteObject> remote = reply.ReadRemoteObject();
76 if (remote != nullptr) {
77 sptr<IKvStoreImpl> kvstoreImplProxy = iface_cast<IKvStoreImpl>(remote);
78 callback(std::move(kvstoreImplProxy));
79 }
80 } else {
81 callback(nullptr);
82 }
83 return ret;
84 }
85
GetSingleKvStore(const Options & options,const AppId & appId,const StoreId & storeId,std::function<void (sptr<ISingleKvStore>)> callback)86 Status KvStoreDataServiceProxy::GetSingleKvStore(const Options &options, const AppId &appId, const StoreId &storeId,
87 std::function<void(sptr<ISingleKvStore>)> callback)
88 {
89 ZLOGI("%s %s", appId.appId.c_str(), storeId.storeId.c_str());
90 MessageParcel data;
91 MessageParcel reply;
92 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
93 ZLOGE("write descriptor failed");
94 return Status::IPC_ERROR;
95 }
96 if (!data.SetMaxCapacity(Constant::MAX_IPC_CAPACITY)) {
97 ZLOGW("SetMaxCapacity failed.");
98 return Status::IPC_ERROR;
99 }
100 // Passing a struct with an std::string field is a potential security exploit.
101 OptionsIpc optionsIpc;
102 optionsIpc.createIfMissing = options.createIfMissing;
103 optionsIpc.encrypt = options.encrypt;
104 optionsIpc.persistant = options.persistant;
105 optionsIpc.backup = options.backup;
106 optionsIpc.autoSync = options.autoSync;
107 optionsIpc.securityLevel = options.securityLevel;
108 optionsIpc.syncPolicy = options.syncPolicy;
109 optionsIpc.kvStoreType = options.kvStoreType;
110 optionsIpc.syncable = options.syncable;
111 optionsIpc.dataOwnership = true; // set default value
112 std::string schemaString = options.schema;
113
114 if (!data.WriteBuffer(&optionsIpc, sizeof(OptionsIpc)) ||
115 !data.WriteString(appId.appId) ||
116 !data.WriteString(storeId.storeId) ||
117 !data.WriteString(schemaString)) {
118 ZLOGW("failed to write parcel.");
119 return Status::IPC_ERROR;
120 }
121 MessageOption mo { MessageOption::TF_SYNC };
122 int32_t error = Remote()->SendRequest(GETSINGLEKVSTORE, data, reply, mo);
123 if (error != 0) {
124 ZLOGW("failed during IPC. errCode %d", error);
125 return Status::IPC_ERROR;
126 }
127 Status status = static_cast<Status>(reply.ReadInt32());
128 if (status == Status::SUCCESS) {
129 sptr<IRemoteObject> remote = reply.ReadRemoteObject();
130 if (remote != nullptr) {
131 sptr<ISingleKvStore> kvstoreImplProxy = iface_cast<ISingleKvStore>(remote);
132 callback(std::move(kvstoreImplProxy));
133 }
134 } else {
135 callback(nullptr);
136 }
137 return status;
138 }
139
GetAllKvStoreId(const AppId & appId,std::function<void (Status,std::vector<StoreId> &)> callback)140 void KvStoreDataServiceProxy::GetAllKvStoreId(const AppId &appId,
141 std::function<void(Status, std::vector<StoreId> &)> callback)
142 {
143 MessageParcel data;
144 MessageParcel reply;
145 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
146 ZLOGE("write descriptor failed");
147 return;
148 }
149 if (!data.WriteString(appId.appId)) {
150 ZLOGW("failed to write parcel.");
151 return;
152 }
153 std::vector<StoreId> storeIds;
154 MessageOption mo { MessageOption::TF_SYNC };
155 int32_t error = Remote()->SendRequest(GETALLKVSTOREID, data, reply, mo);
156 if (error != 0) {
157 ZLOGW("failed during IPC. errCode %d", error);
158 callback(Status::IPC_ERROR, storeIds);
159 return;
160 }
161 std::vector<std::string> stores;
162 reply.ReadStringVector(&stores);
163 for (const auto &id: stores) {
164 storeIds.push_back({id});
165 }
166 Status status = static_cast<Status>(reply.ReadInt32());
167 callback(status, storeIds);
168 }
169
CloseKvStore(const AppId & appId,const StoreId & storeId)170 Status KvStoreDataServiceProxy::CloseKvStore(const AppId &appId, const StoreId &storeId)
171 {
172 MessageParcel data;
173 MessageParcel reply;
174 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
175 ZLOGE("write descriptor failed");
176 return Status::IPC_ERROR;
177 }
178 if (!data.WriteString(appId.appId) ||
179 !data.WriteString(storeId.storeId)) {
180 ZLOGW("failed to write parcel.");
181 return Status::IPC_ERROR;
182 }
183 MessageOption mo { MessageOption::TF_SYNC };
184 int32_t error = Remote()->SendRequest(CLOSEKVSTORE, data, reply, mo);
185 if (error != 0) {
186 ZLOGW("failed during IPC. errCode %d", error);
187 return Status::IPC_ERROR;
188 }
189 return static_cast<Status>(reply.ReadInt32());
190 }
191
192 /* close all opened kvstore */
CloseAllKvStore(const AppId & appId)193 Status KvStoreDataServiceProxy::CloseAllKvStore(const AppId &appId)
194 {
195 MessageParcel data;
196 MessageParcel reply;
197 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
198 ZLOGE("write descriptor failed");
199 return Status::IPC_ERROR;
200 }
201 if (!data.WriteString(appId.appId)) {
202 ZLOGW("failed to write parcel.");
203 return Status::IPC_ERROR;
204 }
205 MessageOption mo { MessageOption::TF_SYNC };
206 int32_t error = Remote()->SendRequest(CLOSEALLKVSTORE, data, reply, mo);
207 if (error != 0) {
208 ZLOGW("failed during IPC. errCode %d", error);
209 return Status::IPC_ERROR;
210 }
211 return static_cast<Status>(reply.ReadInt32());
212 }
213
DeleteKvStore(const AppId & appId,const StoreId & storeId)214 Status KvStoreDataServiceProxy::DeleteKvStore(const AppId &appId, const StoreId &storeId)
215 {
216 MessageParcel data;
217 MessageParcel reply;
218 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
219 ZLOGE("write descriptor failed");
220 return Status::IPC_ERROR;
221 }
222 if (!data.WriteString(appId.appId) ||
223 !data.WriteString(storeId.storeId)) {
224 ZLOGW("failed to write parcel.");
225 return Status::IPC_ERROR;
226 }
227 MessageOption mo { MessageOption::TF_SYNC };
228 int32_t error = Remote()->SendRequest(DELETEKVSTORE, data, reply, mo);
229 if (error != 0) {
230 ZLOGW("failed during IPC. errCode %d", error);
231 return Status::IPC_ERROR;
232 }
233 return static_cast<Status>(reply.ReadInt32());
234 }
235
236 /* delete all kv store */
DeleteAllKvStore(const AppId & appId)237 Status KvStoreDataServiceProxy::DeleteAllKvStore(const AppId &appId)
238 {
239 MessageParcel data;
240 MessageParcel reply;
241 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
242 ZLOGE("write descriptor failed");
243 return Status::IPC_ERROR;
244 }
245 if (!data.WriteString(appId.appId)) {
246 ZLOGW("failed to write parcel.");
247 return Status::IPC_ERROR;
248 }
249 MessageOption mo { MessageOption::TF_SYNC };
250 int32_t error = Remote()->SendRequest(DELETEALLKVSTORE, data, reply, mo);
251 if (error != 0) {
252 ZLOGW("failed during IPC. errCode %d", error);
253 return Status::IPC_ERROR;
254 }
255 return static_cast<Status>(reply.ReadInt32());
256 }
257
RegisterClientDeathObserver(const AppId & appId,sptr<IRemoteObject> observer)258 Status KvStoreDataServiceProxy::RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer)
259 {
260 MessageParcel data;
261 MessageParcel reply;
262 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
263 ZLOGE("write descriptor failed");
264 return Status::IPC_ERROR;
265 }
266 if (!data.WriteString(appId.appId)) {
267 ZLOGW("failed to write string.");
268 return Status::IPC_ERROR;
269 }
270 if (observer != nullptr) {
271 if (!data.WriteRemoteObject(observer)) {
272 ZLOGW("failed to write parcel.");
273 return Status::IPC_ERROR;
274 }
275 } else {
276 return Status::INVALID_ARGUMENT;
277 }
278
279 MessageOption mo { MessageOption::TF_SYNC };
280 int32_t error = Remote()->SendRequest(REGISTERCLIENTDEATHOBSERVER, data, reply, mo);
281 if (error != 0) {
282 ZLOGW("failed during IPC. errCode %d", error);
283 return Status::IPC_ERROR;
284 }
285 return static_cast<Status>(reply.ReadInt32());
286 }
287
GetLocalDevice(OHOS::DistributedKv::DeviceInfo & device)288 Status KvStoreDataServiceProxy::GetLocalDevice(OHOS::DistributedKv::DeviceInfo &device)
289 {
290 MessageParcel data;
291 MessageParcel reply;
292 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
293 ZLOGE("write descriptor failed");
294 return Status::IPC_ERROR;
295 }
296 MessageOption mo { MessageOption::TF_SYNC };
297 int32_t error = Remote()->SendRequest(GETLOCALDEVICE, data, reply, mo);
298 if (error != 0) {
299 ZLOGW("SendRequest returned %d", error);
300 return Status::IPC_ERROR;
301 }
302 Status status = static_cast<Status>(reply.ReadInt32());
303 if (status == Status::SUCCESS) {
304 device = {reply.ReadString(), reply.ReadString(), reply.ReadString()};
305 }
306 return status;
307 }
308
GetDeviceList(std::vector<DeviceInfo> & deviceInfoList,DeviceFilterStrategy strategy)309 Status KvStoreDataServiceProxy::GetDeviceList(std::vector<DeviceInfo> &deviceInfoList, DeviceFilterStrategy strategy)
310 {
311 MessageParcel data;
312 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
313 ZLOGE("write descriptor failed");
314 return Status::IPC_ERROR;
315 }
316 if (!data.WriteInt32(static_cast<int>(strategy))) {
317 ZLOGW("write int failed.");
318 return Status::IPC_ERROR;
319 }
320 MessageParcel reply;
321 MessageOption mo { MessageOption::TF_SYNC };
322 int32_t error = Remote()->SendRequest(GETDEVICELIST, data, reply, mo);
323 if (error != 0) {
324 ZLOGW("SendRequest returned %d", error);
325 return Status::IPC_ERROR;
326 }
327 Status status = static_cast<Status>(reply.ReadInt32());
328 if (status == Status::SUCCESS) {
329 int len = reply.ReadInt32();
330 for (int i = 0; i < len; i++) {
331 DeviceInfo deviceInfo = {
332 .deviceId = reply.ReadString(),
333 .deviceName = reply.ReadString(),
334 .deviceType = reply.ReadString()
335 };
336 deviceInfoList.push_back(std::move(deviceInfo));
337 }
338 }
339 return status;
340 }
341
StartWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer,DeviceFilterStrategy strategy)342 Status KvStoreDataServiceProxy::StartWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer,
343 DeviceFilterStrategy strategy)
344 {
345 MessageParcel data;
346 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
347 ZLOGE("write descriptor failed");
348 return Status::IPC_ERROR;
349 }
350 if (!data.WriteInt32(static_cast<int>(strategy))) {
351 ZLOGW("write int failed.");
352 return Status::IPC_ERROR;
353 }
354 if (observer != nullptr) {
355 if (!data.WriteRemoteObject(observer->AsObject().GetRefPtr())) {
356 return Status::IPC_ERROR;
357 }
358 } else {
359 return Status::INVALID_ARGUMENT;
360 }
361 MessageParcel reply;
362 MessageOption mo { MessageOption::TF_SYNC };
363 int32_t error = Remote()->SendRequest(STARTWATCHDEVICECHANGE, data, reply, mo);
364 if (error != 0) {
365 ZLOGW("SendRequest returned %d", error);
366 return Status::IPC_ERROR;
367 }
368 return static_cast<Status>(reply.ReadInt32());
369 }
370
StopWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer)371 Status KvStoreDataServiceProxy::StopWatchDeviceChange(sptr<IDeviceStatusChangeListener> observer)
372 {
373 MessageParcel data;
374 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
375 ZLOGE("write descriptor failed");
376 return Status::IPC_ERROR;
377 }
378 if (observer != nullptr) {
379 if (!data.WriteRemoteObject(observer->AsObject().GetRefPtr())) {
380 return Status::IPC_ERROR;
381 }
382 } else {
383 return Status::INVALID_ARGUMENT;
384 }
385 MessageParcel reply;
386 MessageOption mo { MessageOption::TF_SYNC };
387 int32_t error = Remote()->SendRequest(STOPWATCHDEVICECHANGE, data, reply, mo);
388 if (error != 0) {
389 ZLOGW("SendRequest returned %d", error);
390 return Status::IPC_ERROR;
391 }
392 return static_cast<Status>(reply.ReadInt32());
393 }
394
GetRdbService()395 sptr<DistributedRdb::IRdbService> KvStoreDataServiceProxy::GetRdbService()
396 {
397 ZLOGI("enter");
398 MessageParcel data;
399 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
400 ZLOGE("write descriptor failed");
401 return nullptr;
402 }
403
404 MessageParcel reply;
405 MessageOption mo { MessageOption::TF_SYNC };
406 int32_t error = Remote()->SendRequest(GET_RDB_SERVICE, data, reply, mo);
407 if (error != 0) {
408 ZLOGE("SendRequest returned %{public}d", error);
409 return nullptr;
410 }
411 auto remoteObject = reply.ReadRemoteObject();
412 if (remoteObject == nullptr) {
413 ZLOGE("remote object is nullptr");
414 return nullptr;
415 }
416 return iface_cast<DistributedRdb::RdbServiceProxy>(remoteObject);
417 }
418
GetKvStoreOnRemote(MessageParcel & data,MessageParcel & reply)419 int32_t KvStoreDataServiceStub::GetKvStoreOnRemote(MessageParcel &data, MessageParcel &reply)
420 {
421 const OptionsIpc *optionIpcPtr = reinterpret_cast<const OptionsIpc *>(data.ReadBuffer(sizeof(OptionsIpc)));
422 if (optionIpcPtr == nullptr) {
423 ZLOGW("optionPtr is nullptr");
424 if (!reply.WriteInt32(static_cast<int>(Status::INVALID_ARGUMENT))) {
425 return -1;
426 }
427 return 0;
428 }
429 OptionsIpc optionsIpc = *optionIpcPtr;
430 Options options;
431 options.createIfMissing = optionsIpc.createIfMissing;
432 options.encrypt = optionsIpc.encrypt;
433 options.persistant = optionsIpc.persistant;
434 options.backup = optionsIpc.backup;
435 options.autoSync = optionsIpc.autoSync;
436 options.securityLevel = optionsIpc.securityLevel;
437 options.syncPolicy = optionsIpc.syncPolicy;
438 options.kvStoreType = optionsIpc.kvStoreType;
439 options.syncable = optionsIpc.syncable;
440 options.dataOwnership = optionsIpc.dataOwnership;
441 AppId appId = { Constant::TrimCopy<std::string>(data.ReadString())};
442 StoreId storeId = { Constant::TrimCopy<std::string>(data.ReadString())};
443 sptr<IKvStoreImpl> proxyTmp;
444 Status status = GetKvStore(options, appId, storeId,
445 [&proxyTmp](sptr<IKvStoreImpl> proxy) { proxyTmp = std::move(proxy); });
446 if (!reply.WriteInt32(static_cast<int>(status))) {
447 return -1;
448 }
449 if (proxyTmp == nullptr) {
450 ZLOGW("proxy is null.");
451 return 0;
452 }
453 if (status == Status::SUCCESS && !reply.WriteRemoteObject(proxyTmp->AsObject().GetRefPtr())) {
454 ZLOGW("write ipc failed.");
455 return -1;
456 }
457 return 0;
458 }
GetAllKvStoreIdOnRemote(MessageParcel & data,MessageParcel & reply)459 int32_t KvStoreDataServiceStub::GetAllKvStoreIdOnRemote(MessageParcel &data, MessageParcel &reply)
460 {
461 AppId appId = { Constant::TrimCopy<std::string>(data.ReadString())};
462 std::vector<std::string> storeIdList;
463 Status statusTmp;
464 GetAllKvStoreId(appId, [&](Status status, std::vector<StoreId> &storeIds) {
465 for (const auto &id : storeIds) {
466 storeIdList.push_back(id.storeId);
467 }
468 statusTmp = status;
469 });
470
471 if (!reply.WriteStringVector(storeIdList)) {
472 return -1;
473 }
474
475 if (!reply.WriteInt32(static_cast<int>(statusTmp))) {
476 return -1;
477 }
478 return 0;
479 }
GetDeviceListOnRemote(MessageParcel & data,MessageParcel & reply)480 int32_t KvStoreDataServiceStub::GetDeviceListOnRemote(MessageParcel &data, MessageParcel &reply)
481 {
482 std::vector<DeviceInfo> infos;
483 DeviceFilterStrategy strategy = static_cast<DeviceFilterStrategy>(data.ReadInt32());
484 Status status = GetDeviceList(infos, strategy);
485 if (!reply.WriteInt32(static_cast<int>(status))) {
486 return -1;
487 }
488 if (status == Status::SUCCESS) {
489 if (!reply.WriteInt32(infos.size())) {
490 return -1;
491 }
492 for (DeviceInfo const &info : infos) {
493 if (!reply.WriteString(info.deviceId) || !reply.WriteString(info.deviceName) ||
494 !reply.WriteString(info.deviceType)) {
495 return -1;
496 }
497 }
498 }
499 return 0;
500 }
StartWatchDeviceChangeOnRemote(MessageParcel & data,MessageParcel & reply)501 int32_t KvStoreDataServiceStub::StartWatchDeviceChangeOnRemote(MessageParcel &data, MessageParcel &reply)
502 {
503 DeviceFilterStrategy strategy = static_cast<DeviceFilterStrategy>(data.ReadInt32());
504 sptr<IRemoteObject> remote = data.ReadRemoteObject();
505 if (remote == nullptr) {
506 ZLOGW("observerProxy nullptr after ipc");
507 if (!reply.WriteInt32(static_cast<int>(Status::IPC_ERROR))) {
508 return -1;
509 }
510 return 0;
511 }
512 sptr<IDeviceStatusChangeListener> observerProxy = iface_cast<IDeviceStatusChangeListener>(remote);
513 Status status = StartWatchDeviceChange(std::move(observerProxy), strategy);
514 if (!reply.WriteInt32(static_cast<int>(status))) {
515 return -1;
516 }
517 return 0;
518 }
StopWatchDeviceChangeOnRemote(MessageParcel & data,MessageParcel & reply)519 int32_t KvStoreDataServiceStub::StopWatchDeviceChangeOnRemote(MessageParcel &data, MessageParcel &reply)
520 {
521 sptr<IRemoteObject> remote = data.ReadRemoteObject();
522 if (remote == nullptr) {
523 ZLOGW("observerProxy nullptr after ipc");
524 if (!reply.WriteInt32(static_cast<int>(Status::IPC_ERROR))) {
525 return -1;
526 }
527 return 0;
528 }
529 sptr<IDeviceStatusChangeListener> observerProxy = iface_cast<IDeviceStatusChangeListener>(remote);
530 Status status = StopWatchDeviceChange(std::move(observerProxy));
531 if (!reply.WriteInt32(static_cast<int>(status))) {
532 return -1;
533 }
534 return 0;
535 }
GetSingleKvStoreOnRemote(MessageParcel & data,MessageParcel & reply)536 int32_t KvStoreDataServiceStub::GetSingleKvStoreOnRemote(MessageParcel &data, MessageParcel &reply)
537 {
538 const OptionsIpc *optionIpcPtr = reinterpret_cast<const OptionsIpc *>(data.ReadBuffer(sizeof(OptionsIpc)));
539 if (optionIpcPtr == nullptr) {
540 ZLOGW("optionPtr is nullptr");
541 if (!reply.WriteInt32(static_cast<int>(Status::INVALID_ARGUMENT))) {
542 return -1;
543 }
544 return 0;
545 }
546 OptionsIpc optionsIpc = *optionIpcPtr;
547 AppId appId = { Constant::TrimCopy<std::string>(data.ReadString())};
548 StoreId storeId = { Constant::TrimCopy<std::string>(data.ReadString())};
549 Options options;
550 options.createIfMissing = optionsIpc.createIfMissing;
551 options.encrypt = optionsIpc.encrypt;
552 options.persistant = optionsIpc.persistant;
553 options.backup = optionsIpc.backup;
554 options.autoSync = optionsIpc.autoSync;
555 options.securityLevel = optionsIpc.securityLevel;
556 options.syncPolicy = optionsIpc.syncPolicy;
557 options.kvStoreType = optionsIpc.kvStoreType;
558 options.syncable = optionsIpc.syncable;
559 options.dataOwnership = optionsIpc.dataOwnership;
560 options.schema = data.ReadString();
561 sptr<ISingleKvStore> proxyTmp;
562 Status status = GetSingleKvStore(options, appId, storeId,
563 [&](sptr<ISingleKvStore> proxy) { proxyTmp = std::move(proxy); });
564 if (!reply.WriteInt32(static_cast<int>(status))) {
565 return -1;
566 }
567 if (status == Status::SUCCESS && proxyTmp != nullptr) {
568 if (!reply.WriteRemoteObject(proxyTmp->AsObject().GetRefPtr())) {
569 return -1;
570 }
571 }
572 return 0;
573 }
574
CloseKvStoreOnRemote(MessageParcel & data,MessageParcel & reply)575 int32_t KvStoreDataServiceStub::CloseKvStoreOnRemote(MessageParcel &data, MessageParcel &reply)
576 {
577 AppId appId = { Constant::TrimCopy<std::string>(data.ReadString())};
578 StoreId storeId = { Constant::TrimCopy<std::string>(data.ReadString())};
579 Status status = CloseKvStore(appId, storeId);
580 if (!reply.WriteInt32(static_cast<int>(status))) {
581 return -1;
582 }
583 return 0;
584 }
585
CloseAllKvStoreOnRemote(MessageParcel & data,MessageParcel & reply)586 int32_t KvStoreDataServiceStub::CloseAllKvStoreOnRemote(MessageParcel &data, MessageParcel &reply)
587 {
588 AppId appId = { Constant::TrimCopy<std::string>(data.ReadString())};
589 Status status = CloseAllKvStore(appId);
590 if (!reply.WriteInt32(static_cast<int>(status))) {
591 return -1;
592 }
593 return 0;
594 }
595
DeleteKvStoreOnRemote(MessageParcel & data,MessageParcel & reply)596 int32_t KvStoreDataServiceStub::DeleteKvStoreOnRemote(MessageParcel &data, MessageParcel &reply)
597 {
598 AppId appId = { Constant::TrimCopy<std::string>(data.ReadString())};
599 StoreId storeId = { Constant::TrimCopy<std::string>(data.ReadString())};
600 Status status = DeleteKvStore(appId, storeId);
601 if (!reply.WriteInt32(static_cast<int>(status))) {
602 return -1;
603 }
604 return 0;
605 }
606
DeleteAllKvStoreOnRemote(MessageParcel & data,MessageParcel & reply)607 int32_t KvStoreDataServiceStub::DeleteAllKvStoreOnRemote(MessageParcel &data, MessageParcel &reply)
608 {
609 AppId appId = { Constant::TrimCopy<std::string>(data.ReadString())};
610 Status status = DeleteAllKvStore(appId);
611 if (!reply.WriteInt32(static_cast<int>(status))) {
612 return -1;
613 }
614 return 0;
615 }
616
RegisterClientDeathObserverOnRemote(MessageParcel & data,MessageParcel & reply)617 int32_t KvStoreDataServiceStub::RegisterClientDeathObserverOnRemote(MessageParcel &data, MessageParcel &reply)
618 {
619 AppId appId = { Constant::TrimCopy<std::string>(data.ReadString())};
620 sptr<IRemoteObject> kvStoreClientDeathObserverProxy = data.ReadRemoteObject();
621 if (kvStoreClientDeathObserverProxy == nullptr) {
622 return -1;
623 }
624 Status status = RegisterClientDeathObserver(appId, std::move(kvStoreClientDeathObserverProxy));
625 if (!reply.WriteInt32(static_cast<int>(status))) {
626 return -1;
627 }
628 return 0;
629 }
630
GetLocalDeviceOnRemote(MessageParcel & data,MessageParcel & reply)631 int32_t KvStoreDataServiceStub::GetLocalDeviceOnRemote(MessageParcel &data, MessageParcel &reply)
632 {
633 DeviceInfo info;
634 Status status = GetLocalDevice(info);
635 if (!reply.WriteInt32(static_cast<int>(status)) || !reply.WriteString(info.deviceId) ||
636 !reply.WriteString(info.deviceName) || !reply.WriteString(info.deviceType)) {
637 return -1;
638 }
639 return 0;
640 }
641
GetRdbServiceOnRemote(MessageParcel & data,MessageParcel & reply)642 int32_t KvStoreDataServiceStub::GetRdbServiceOnRemote(MessageParcel &data, MessageParcel &reply)
643 {
644 auto rdbService = GetRdbService();
645 reply.WriteRemoteObject(rdbService->AsObject().GetRefPtr());
646 return 0;
647 }
648
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)649 int32_t KvStoreDataServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
650 MessageParcel &reply, MessageOption &option)
651 {
652 ZLOGD("%{public}d", code);
653 std::u16string descriptor = KvStoreDataServiceStub::GetDescriptor();
654 std::u16string remoteDescriptor = data.ReadInterfaceToken();
655 if (descriptor != remoteDescriptor) {
656 ZLOGE("local descriptor is not equal to remote");
657 return -1;
658 }
659 if (code >= 0 && code < SERVICE_CMD_LAST) {
660 return (this->*HANDLERS[code])(data, reply);
661 } else {
662 MessageOption mo { MessageOption::TF_SYNC };
663 return IPCObjectStub::OnRemoteRequest(code, data, reply, mo);
664 }
665 }
666 } // namespace DistributedKv
667 } // namespace OHOS
668