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__, HiviewDFX::XCOLLIE_FLAG_LOG | HiviewDFX::XCOLLIE_FLAG_RECOVERY);
35     AppId appId = { data.ReadString() };
36     sptr<IRemoteObject> kvStoreClientDeathObserverProxy = data.ReadRemoteObject();
37     if (kvStoreClientDeathObserverProxy == nullptr) {
38         return -1;
39     }
40     Status status = RegisterClientDeathObserver(appId, std::move(kvStoreClientDeathObserverProxy));
41     if (!reply.WriteInt32(static_cast<int>(status))) {
42         return -1;
43     }
44     return 0;
45 }
46 
GetFeatureInterfaceOnRemote(MessageParcel & data,MessageParcel & reply)47 int32_t KvStoreDataServiceStub::GetFeatureInterfaceOnRemote(MessageParcel &data, MessageParcel &reply)
48 {
49     XCollie xcollie(__FUNCTION__, HiviewDFX::XCOLLIE_FLAG_LOG | HiviewDFX::XCOLLIE_FLAG_RECOVERY);
50     std::string name;
51     if (!ITypesUtil::Unmarshal(data, name)) {
52         return -1;
53     }
54     auto remoteObject = GetFeatureInterface(name);
55     if (!ITypesUtil::Marshal(reply, remoteObject)) {
56         return -1;
57     }
58     return 0;
59 }
60 
ClearAppStorageOnRemote(MessageParcel & data,MessageParcel & reply)61 int32_t KvStoreDataServiceStub::ClearAppStorageOnRemote(MessageParcel &data, MessageParcel &reply)
62 {
63     std::string bundleName;
64     int32_t userId;
65     int32_t appIndex;
66     int32_t tokenId;
67     if (!ITypesUtil::Unmarshal(data, bundleName, userId, appIndex, tokenId)) {
68         return -1;
69     }
70     auto remoteObject = ClearAppStorage(bundleName, userId, appIndex, tokenId);
71     if (!ITypesUtil::Marshal(reply, remoteObject)) {
72         return -1;
73     }
74     return 0;
75 }
76 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)77 int32_t KvStoreDataServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
78     MessageOption &option)
79 {
80     ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
81     std::u16string descriptor = KvStoreDataServiceStub::GetDescriptor();
82     std::u16string remoteDescriptor = data.ReadInterfaceToken();
83     if (descriptor != remoteDescriptor) {
84         ZLOGE("local descriptor is not equal to remote");
85         return -1;
86     }
87     if (code >= 0 && code < static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::SERVICE_CMD_LAST)) {
88         return (this->*HANDLERS[code])(data, reply);
89     } else {
90         MessageOption mo{ MessageOption::TF_SYNC };
91         return IPCObjectStub::OnRemoteRequest(code, data, reply, mo);
92     }
93 }
94 }  // namespace DistributedKv
95 }  // namespace OHOS
96