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 <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)];
KvStoreDataServiceProxy(const sptr<IRemoteObject> & impl)29 KvStoreDataServiceProxy::KvStoreDataServiceProxy(const sptr<IRemoteObject> &impl)
30 : IRemoteProxy<IKvStoreDataService>(impl)
31 {
32 ZLOGI("init data service proxy.");
33 }
34
GetFeatureInterface(const std::string & name)35 sptr<IRemoteObject> KvStoreDataServiceProxy::GetFeatureInterface(const std::string &name)
36 {
37 ZLOGI("%s", name.c_str());
38 MessageParcel data;
39 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
40 ZLOGE("write descriptor failed");
41 return nullptr;
42 }
43
44 if (!ITypesUtil::Marshal(data, name)) {
45 ZLOGE("write descriptor failed");
46 return nullptr;
47 }
48
49 MessageParcel reply;
50 MessageOption mo { MessageOption::TF_SYNC };
51 int32_t error = Remote()->SendRequest(
52 static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::GET_FEATURE_INTERFACE), data, reply, mo);
53 if (error != 0) {
54 ZLOGE("SendRequest returned %{public}d", error);
55 return nullptr;
56 }
57
58 sptr<IRemoteObject> remoteObject;
59 if (!ITypesUtil::Unmarshal(reply, remoteObject)) {
60 ZLOGE("remote object is nullptr");
61 return nullptr;
62 }
63 return remoteObject;
64 }
65
RegisterClientDeathObserver(const AppId & appId,sptr<IRemoteObject> observer)66 Status KvStoreDataServiceProxy::RegisterClientDeathObserver(const AppId &appId, sptr<IRemoteObject> observer)
67 {
68 MessageParcel data;
69 MessageParcel reply;
70 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
71 ZLOGE("write descriptor failed");
72 return Status::IPC_ERROR;
73 }
74 if (!data.WriteString(appId.appId)) {
75 ZLOGW("failed to write string.");
76 return Status::IPC_ERROR;
77 }
78 if (observer != nullptr) {
79 if (!data.WriteRemoteObject(observer)) {
80 ZLOGW("failed to write parcel.");
81 return Status::IPC_ERROR;
82 }
83 } else {
84 return Status::INVALID_ARGUMENT;
85 }
86
87 MessageOption mo { MessageOption::TF_SYNC };
88 int32_t error = Remote()->SendRequest(
89 static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::REGISTERCLIENTDEATHOBSERVER), data, reply, mo);
90 if (error != 0) {
91 ZLOGW("failed during IPC. errCode %d", error);
92 return Status::IPC_ERROR;
93 }
94 return static_cast<Status>(reply.ReadInt32());
95 }
96
RegisterClientDeathObserverOnRemote(MessageParcel & data,MessageParcel & reply)97 int32_t KvStoreDataServiceStub::RegisterClientDeathObserverOnRemote(MessageParcel &data, MessageParcel &reply)
98 {
99 AppId appId = { data.ReadString() };
100 sptr<IRemoteObject> kvStoreClientDeathObserverProxy = data.ReadRemoteObject();
101 if (kvStoreClientDeathObserverProxy == nullptr) {
102 return -1;
103 }
104 Status status = RegisterClientDeathObserver(appId, std::move(kvStoreClientDeathObserverProxy));
105 if (!reply.WriteInt32(static_cast<int>(status))) {
106 return -1;
107 }
108 return 0;
109 }
110
GetFeatureInterfaceOnRemote(MessageParcel & data,MessageParcel & reply)111 int32_t KvStoreDataServiceStub::GetFeatureInterfaceOnRemote(MessageParcel &data, MessageParcel &reply)
112 {
113 std::string name;
114 if (!ITypesUtil::Unmarshal(data, name)) {
115 return -1;
116 }
117 auto remoteObject = GetFeatureInterface(name);
118 if (!ITypesUtil::Marshal(reply, remoteObject)) {
119 return -1;
120 }
121 return 0;
122 }
123
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)124 int32_t KvStoreDataServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
125 MessageParcel &reply, MessageOption &option)
126 {
127 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
128 std::u16string descriptor = KvStoreDataServiceStub::GetDescriptor();
129 std::u16string remoteDescriptor = data.ReadInterfaceToken();
130 if (descriptor != remoteDescriptor) {
131 ZLOGE("local descriptor is not equal to remote");
132 return -1;
133 }
134 if (code >= 0 && code < static_cast<uint32_t>(KvStoreDataServiceInterfaceCode::SERVICE_CMD_LAST)) {
135 return (this->*HANDLERS[code])(data, reply);
136 } else {
137 MessageOption mo { MessageOption::TF_SYNC };
138 return IPCObjectStub::OnRemoteRequest(code, data, reply, mo);
139 }
140 }
141 } // namespace DistributedKv
142 } // namespace OHOS
143