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