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 "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
GetFeatureInterface(const std::string & name)36 sptr<IRemoteObject> KvStoreDataServiceProxy::GetFeatureInterface(const std::string &name)
37 {
38 ZLOGI("%s", name.c_str());
39 MessageParcel data;
40 if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) {
41 ZLOGE("write descriptor failed");
42 return nullptr;
43 }
44
45 if (!ITypesUtil::Marshal(data, name)) {
46 ZLOGE("write descriptor failed");
47 return nullptr;
48 }
49
50 MessageParcel reply;
51 MessageOption mo { MessageOption::TF_SYNC };
52 int32_t error = Remote()->SendRequest(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(REGISTERCLIENTDEATHOBSERVER, data, reply, mo);
89 if (error != 0) {
90 ZLOGW("failed during IPC. errCode %d", error);
91 return Status::IPC_ERROR;
92 }
93 return static_cast<Status>(reply.ReadInt32());
94 }
95
NoSupport(MessageParcel & data,MessageParcel & reply)96 int32_t KvStoreDataServiceStub::NoSupport(MessageParcel &data, MessageParcel &reply)
97 {
98 (void)data;
99 (void)reply;
100 return NOT_SUPPORT;
101 }
102
RegisterClientDeathObserverOnRemote(MessageParcel & data,MessageParcel & reply)103 int32_t KvStoreDataServiceStub::RegisterClientDeathObserverOnRemote(MessageParcel &data, MessageParcel &reply)
104 {
105 AppId appId = { data.ReadString() };
106 sptr<IRemoteObject> kvStoreClientDeathObserverProxy = data.ReadRemoteObject();
107 if (kvStoreClientDeathObserverProxy == nullptr) {
108 return -1;
109 }
110 Status status = RegisterClientDeathObserver(appId, std::move(kvStoreClientDeathObserverProxy));
111 if (!reply.WriteInt32(static_cast<int>(status))) {
112 return -1;
113 }
114 return 0;
115 }
116
GetFeatureInterfaceOnRemote(MessageParcel & data,MessageParcel & reply)117 int32_t KvStoreDataServiceStub::GetFeatureInterfaceOnRemote(MessageParcel &data, MessageParcel &reply)
118 {
119 std::string name;
120 if (!ITypesUtil::Unmarshal(data, name)) {
121 return -1;
122 }
123 auto remoteObject = GetFeatureInterface(name);
124 if (!ITypesUtil::Marshal(reply, remoteObject)) {
125 return -1;
126 }
127 return 0;
128 }
129
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)130 int32_t KvStoreDataServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
131 MessageParcel &reply, MessageOption &option)
132 {
133 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
134 std::u16string descriptor = KvStoreDataServiceStub::GetDescriptor();
135 std::u16string remoteDescriptor = data.ReadInterfaceToken();
136 if (descriptor != remoteDescriptor) {
137 ZLOGE("local descriptor is not equal to remote");
138 return -1;
139 }
140 if (code >= 0 && code < SERVICE_CMD_LAST) {
141 return (this->*HANDLERS[code])(data, reply);
142 } else {
143 MessageOption mo { MessageOption::TF_SYNC };
144 return IPCObjectStub::OnRemoteRequest(code, data, reply, mo);
145 }
146 }
147 } // namespace DistributedKv
148 } // namespace OHOS
149