1 /*
2 * Copyright (c) 2022 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 "RdbServiceStub"
17
18 #include "rdb_service_stub.h"
19 #include <ipc_skeleton.h>
20 #include "log_print.h"
21 #include "itypes_util.h"
22 #include "utils/anonymous.h"
23
24 namespace OHOS::DistributedRdb {
25 using Anonymous = DistributedData::Anonymous;
OnRemoteObtainDistributedTableName(MessageParcel & data,MessageParcel & reply)26 int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, MessageParcel &reply)
27 {
28 std::string device;
29 std::string table;
30 if (!ITypesUtil::Unmarshal(data, device, table)) {
31 ZLOGE("Unmarshal device:%{public}s table:%{public}s", Anonymous::Change(device).c_str(), table.c_str());
32 return IPC_STUB_INVALID_DATA_ERR;
33 }
34
35 std::string distributedTableName = ObtainDistributedTableName(device, table);
36 if (!ITypesUtil::Marshal(reply, distributedTableName)) {
37 ZLOGE("Marshal distributedTableName:%{public}s", distributedTableName.c_str());
38 return IPC_STUB_WRITE_PARCEL_ERR;
39 }
40 return RDB_OK;
41 }
42
OnGetSchema(MessageParcel & data,MessageParcel & reply)43 int32_t RdbServiceStub::OnGetSchema(MessageParcel &data, MessageParcel &reply)
44 {
45 RdbSyncerParam param;
46 if (!ITypesUtil::Unmarshal(data, param)) {
47 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
48 Anonymous::Change(param.storeName_).c_str());
49 return IPC_STUB_INVALID_DATA_ERR;
50 }
51 auto status = GetSchema(param);
52 if (!ITypesUtil::Marshal(reply, status)) {
53 ZLOGE("Marshal status:0x%{public}x", status);
54 return IPC_STUB_WRITE_PARCEL_ERR;
55 }
56 return RDB_OK;
57 }
58
OnDelete(MessageParcel & data,MessageParcel & reply)59 int32_t RdbServiceStub::OnDelete(MessageParcel &data, MessageParcel &reply)
60 {
61 RdbSyncerParam param;
62 if (!ITypesUtil::Unmarshal(data, param)) {
63 ZLOGE("Unmarshal storeName_:%{public}s", Anonymous::Change(param.storeName_).c_str());
64 return IPC_STUB_INVALID_DATA_ERR;
65 }
66 auto status = Delete(param);
67 if (!ITypesUtil::Marshal(reply, status)) {
68 ZLOGE("Marshal status:0x%{public}x", status);
69 return IPC_STUB_WRITE_PARCEL_ERR;
70 }
71 return RDB_OK;
72 }
73
OnRemoteInitNotifier(MessageParcel & data,MessageParcel & reply)74 int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply)
75 {
76 RdbSyncerParam param;
77 sptr<IRemoteObject> notifier;
78 if (!ITypesUtil::Unmarshal(data, param, notifier) || notifier == nullptr) {
79 ZLOGE("Unmarshal bundleName:%{public}s storeName_:%{public}s notifier is nullptr:%{public}d",
80 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), notifier == nullptr);
81 return IPC_STUB_INVALID_DATA_ERR;
82 }
83 auto status = InitNotifier(param, notifier);
84 if (!ITypesUtil::Marshal(reply, status)) {
85 ZLOGE("Marshal status:0x%{public}x", status);
86 return IPC_STUB_WRITE_PARCEL_ERR;
87 }
88 return RDB_OK;
89 }
90
OnRemoteSetDistributedTables(MessageParcel & data,MessageParcel & reply)91 int32_t RdbServiceStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply)
92 {
93 RdbSyncerParam param;
94 std::vector<std::string> tables;
95 int32_t type;
96 if (!ITypesUtil::Unmarshal(data, param, tables, type)) {
97 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables size:%{public}zu type:%{public}d",
98 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), tables.size(), type);
99 return IPC_STUB_INVALID_DATA_ERR;
100 }
101
102 auto status = SetDistributedTables(param, tables, type);
103 if (!ITypesUtil::Marshal(reply, status)) {
104 ZLOGE("Marshal status:0x%{public}x", status);
105 return IPC_STUB_WRITE_PARCEL_ERR;
106 }
107 return RDB_OK;
108 }
109
OnRemoteDoSync(MessageParcel & data,MessageParcel & reply)110 int32_t RdbServiceStub::OnRemoteDoSync(MessageParcel &data, MessageParcel &reply)
111 {
112 RdbSyncerParam param;
113 Option option {};
114 PredicatesMemo predicates;
115 if (!ITypesUtil::Unmarshal(data, param, option, predicates)) {
116 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s tables:%{public}zu", param.bundleName_.c_str(),
117 Anonymous::Change(param.storeName_).c_str(), predicates.tables_.size());
118 return IPC_STUB_INVALID_DATA_ERR;
119 }
120
121 Details result = {};
122 auto status = Sync(param, option, predicates, [&result](Details &&details) { result = std::move(details); });
123 if (!ITypesUtil::Marshal(reply, status, result)) {
124 ZLOGE("Marshal status:0x%{public}x result size:%{public}zu", status, result.size());
125 return IPC_STUB_WRITE_PARCEL_ERR;
126 }
127 return RDB_OK;
128 }
129
OnRemoteDoAsync(MessageParcel & data,MessageParcel & reply)130 int32_t RdbServiceStub::OnRemoteDoAsync(MessageParcel &data, MessageParcel &reply)
131 {
132 RdbSyncerParam param;
133 Option option {};
134 PredicatesMemo predicates;
135 if (!ITypesUtil::Unmarshal(data, param, option, predicates)) {
136 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s seqNum:%{public}u table:%{public}s",
137 param.bundleName_.c_str(), Anonymous::Change(param.storeName_).c_str(), option.seqNum,
138 predicates.tables_.empty() ? "null" : predicates.tables_.begin()->c_str());
139 return IPC_STUB_INVALID_DATA_ERR;
140 }
141 auto status = Sync(param, option, predicates, nullptr);
142 if (!ITypesUtil::Marshal(reply, status)) {
143 ZLOGE("Marshal status:0x%{public}x", status);
144 return IPC_STUB_WRITE_PARCEL_ERR;
145 }
146 return RDB_OK;
147 }
148
OnRemoteDoSubscribe(MessageParcel & data,MessageParcel & reply)149 int32_t RdbServiceStub::OnRemoteDoSubscribe(MessageParcel &data, MessageParcel &reply)
150 {
151 RdbSyncerParam param;
152 SubscribeOption option;
153 if (!ITypesUtil::Unmarshal(data, param, option)) {
154 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
155 Anonymous::Change(param.storeName_).c_str());
156 return IPC_STUB_INVALID_DATA_ERR;
157 }
158
159 auto status = Subscribe(param, option, nullptr);
160 if (!ITypesUtil::Marshal(reply, status)) {
161 ZLOGE("Marshal status:0x%{public}x", status);
162 return IPC_STUB_WRITE_PARCEL_ERR;
163 }
164 return RDB_OK;
165 }
166
OnRemoteDoUnSubscribe(MessageParcel & data,MessageParcel & reply)167 int32_t RdbServiceStub::OnRemoteDoUnSubscribe(MessageParcel &data, MessageParcel &reply)
168 {
169 RdbSyncerParam param;
170 SubscribeOption option;
171 if (!ITypesUtil::Unmarshal(data, param)) {
172 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(),
173 Anonymous::Change(param.storeName_).c_str());
174 return IPC_STUB_INVALID_DATA_ERR;
175 }
176
177 auto status = UnSubscribe(param, option, nullptr);
178 if (!ITypesUtil::Marshal(reply, status)) {
179 ZLOGE("Marshal status:0x%{public}x", status);
180 return IPC_STUB_WRITE_PARCEL_ERR;
181 }
182 return RDB_OK;
183 }
184
OnRemoteDoRemoteQuery(MessageParcel & data,MessageParcel & reply)185 int32_t RdbServiceStub::OnRemoteDoRemoteQuery(MessageParcel& data, MessageParcel& reply)
186 {
187 RdbSyncerParam param;
188 std::string device;
189 std::string sql;
190 std::vector<std::string> selectionArgs;
191 if (!ITypesUtil::Unmarshal(data, param, device, sql, selectionArgs)) {
192 ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s device:%{public}s sql:%{public}s "
193 "selectionArgs size:%{public}zu", param.bundleName_.c_str(),
194 Anonymous::Change(param.storeName_).c_str(), Anonymous::Change(device).c_str(),
195 Anonymous::Change(sql).c_str(), selectionArgs.size());
196 return IPC_STUB_INVALID_DATA_ERR;
197 }
198
199 sptr<IRemoteObject> resultSet;
200 auto status = RemoteQuery(param, device, sql, selectionArgs, resultSet);
201 if (!ITypesUtil::Marshal(reply, status, resultSet)) {
202 ZLOGE("Marshal status:0x%{public}x", status);
203 return IPC_STUB_WRITE_PARCEL_ERR;
204 }
205 return RDB_OK;
206 }
207
CheckInterfaceToken(MessageParcel & data)208 bool RdbServiceStub::CheckInterfaceToken(MessageParcel& data)
209 {
210 auto localDescriptor = GetDescriptor();
211 auto remoteDescriptor = data.ReadInterfaceToken();
212 if (remoteDescriptor != localDescriptor) {
213 ZLOGE("interface token is not equal");
214 return false;
215 }
216 return true;
217 }
218
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)219 int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply)
220 {
221 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
222 if (!CheckInterfaceToken(data)) {
223 return RDB_ERROR;
224 }
225 if (code >= 0 && code < static_cast<uint32_t>(RdbServiceCode::RDB_SERVICE_CMD_MAX)) {
226 return (this->*HANDLERS[code])(data, reply);
227 }
228 return RDB_ERROR;
229 }
230 } // namespace OHOS::DistributedRdb
231