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 "KvStoreDataServiceStub"
17
18 #include "kvstore_data_service_stub.h"
19 #include <ipc_skeleton.h>
20 #include "itypes_util.h"
21 #include "message_parcel.h"
22 #include "types.h"
23 #include "log_print.h"
24 #include "xcollie.h"
25
26 namespace OHOS {
27 namespace DistributedKv {
28 using namespace OHOS::DistributedData;
29 constexpr KvStoreDataServiceStub::RequestHandler
30 KvStoreDataServiceStub::HANDLERS[static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::SERVICE_CMD_LAST)];
31
RegisterClientDeathObserverOnRemote(MessageParcel & data,MessageParcel & reply)32 int32_t KvStoreDataServiceStub::RegisterClientDeathObserverOnRemote(MessageParcel &data, MessageParcel &reply)
33 {
34 XCollie xcollie(__FUNCTION__, XCollie::XCOLLIE_LOG | XCollie::XCOLLIE_RECOVERY);
35 AppId appId = { data.ReadString() };
36 sptr<IRemoteObject> kvStoreClientDeathObserverProxy = data.ReadRemoteObject();
37 if (kvStoreClientDeathObserverProxy == nullptr) {
38 return -1;
39 }
40 std::string featureName = data.ReadString();
41 Status status = RegisterClientDeathObserver(appId, std::move(kvStoreClientDeathObserverProxy), featureName);
42 if (!reply.WriteInt32(static_cast<int>(status))) {
43 return -1;
44 }
45 return 0;
46 }
47
GetFeatureInterfaceOnRemote(MessageParcel & data,MessageParcel & reply)48 int32_t KvStoreDataServiceStub::GetFeatureInterfaceOnRemote(MessageParcel &data, MessageParcel &reply)
49 {
50 XCollie xcollie(__FUNCTION__, XCollie::XCOLLIE_LOG | XCollie::XCOLLIE_RECOVERY);
51 std::string name;
52 if (!ITypesUtil::Unmarshal(data, name)) {
53 return -1;
54 }
55 auto remoteObject = GetFeatureInterface(name);
56 if (!ITypesUtil::Marshal(reply, remoteObject)) {
57 return -1;
58 }
59 return 0;
60 }
61
ClearAppStorageOnRemote(MessageParcel & data,MessageParcel & reply)62 int32_t KvStoreDataServiceStub::ClearAppStorageOnRemote(MessageParcel &data, MessageParcel &reply)
63 {
64 std::string bundleName;
65 int32_t userId;
66 int32_t appIndex;
67 int32_t tokenId;
68 if (!ITypesUtil::Unmarshal(data, bundleName, userId, appIndex, tokenId)) {
69 return -1;
70 }
71 auto code = ClearAppStorage(bundleName, userId, appIndex, tokenId);
72 if (!ITypesUtil::Marshal(reply, code)) {
73 return -1;
74 }
75 return 0;
76 }
77
ExitOnRemote(MessageParcel & data,MessageParcel & reply)78 int32_t KvStoreDataServiceStub::ExitOnRemote(MessageParcel &data, MessageParcel &reply)
79 {
80 std::string featureName;
81 if (!ITypesUtil::Unmarshal(data, featureName)) {
82 return -1;
83 }
84 auto code = Exit(featureName);
85 if (!ITypesUtil::Marshal(reply, code)) {
86 return -1;
87 }
88 return 0;
89 }
90
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)91 int32_t KvStoreDataServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
92 MessageOption &option)
93 {
94 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
95 std::u16string descriptor = KvStoreDataServiceStub::GetDescriptor();
96 std::u16string remoteDescriptor = data.ReadInterfaceToken();
97 if (descriptor != remoteDescriptor) {
98 ZLOGE("local descriptor is not equal to remote");
99 return -1;
100 }
101 if (code >= 0 && code < static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::SERVICE_CMD_LAST)) {
102 return (this->*HANDLERS[code])(data, reply);
103 } else {
104 MessageOption mo{ MessageOption::TF_SYNC };
105 return IPCObjectStub::OnRemoteRequest(code, data, reply, mo);
106 }
107 }
108 } // namespace DistributedKv
109 } // namespace OHOS
110