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 "DataShareServiceStub"
17
18 #include "data_share_service_stub.h"
19
20 #include "data_share_obs_proxy.h"
21 #include "ipc_skeleton.h"
22 #include "ishared_result_set.h"
23 #include "itypes_util.h"
24 #include "log_print.h"
25 #include "utils/anonymous.h"
26
27 namespace OHOS {
28 namespace DataShare {
CheckInterfaceToken(MessageParcel & data)29 bool DataShareServiceStub::CheckInterfaceToken(MessageParcel &data)
30 {
31 auto localDescriptor = IDataShareService::GetDescriptor();
32 auto remoteDescriptor = data.ReadInterfaceToken();
33 if (remoteDescriptor != localDescriptor) {
34 ZLOGE("interface token is not equal");
35 return false;
36 }
37 return true;
38 }
39
OnRemoteInsert(MessageParcel & data,MessageParcel & reply)40 int32_t DataShareServiceStub::OnRemoteInsert(MessageParcel &data, MessageParcel &reply)
41 {
42 std::string uri;
43 DataShareValuesBucket bucket;
44 if (!ITypesUtil::Unmarshal(data, uri, bucket.valuesMap)) {
45 ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
46 bucket.valuesMap.size());
47 return IPC_STUB_INVALID_DATA_ERR;
48 }
49 int32_t status = Insert(uri, bucket);
50 if (!ITypesUtil::Marshal(reply, status)) {
51 ZLOGE("Marshal status:0x%{public}x", status);
52 return IPC_STUB_WRITE_PARCEL_ERR;
53 }
54 return 0;
55 }
56
OnRemoteUpdate(MessageParcel & data,MessageParcel & reply)57 int32_t DataShareServiceStub::OnRemoteUpdate(MessageParcel &data, MessageParcel &reply)
58 {
59 std::string uri;
60 DataSharePredicates predicate;
61 DataShareValuesBucket bucket;
62 if (!ITypesUtil::Unmarshal(data, uri, predicate, bucket.valuesMap)) {
63 ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
64 bucket.valuesMap.size());
65 return IPC_STUB_INVALID_DATA_ERR;
66 }
67 int32_t status = Update(uri, predicate, bucket);
68 if (!ITypesUtil::Marshal(reply, status)) {
69 ZLOGE("Marshal status:0x%{public}x", status);
70 return IPC_STUB_WRITE_PARCEL_ERR;
71 }
72 return 0;
73 }
74
OnRemoteDelete(MessageParcel & data,MessageParcel & reply)75 int32_t DataShareServiceStub::OnRemoteDelete(MessageParcel &data, MessageParcel &reply)
76 {
77 std::string uri;
78 DataSharePredicates predicate;
79 if (!ITypesUtil::Unmarshal(data, uri, predicate)) {
80 ZLOGE("Unmarshal uri:%{public}s", DistributedData::Anonymous::Change(uri).c_str());
81 return IPC_STUB_INVALID_DATA_ERR;
82 }
83 int32_t status = Delete(uri, predicate);
84 if (!ITypesUtil::Marshal(reply, status)) {
85 ZLOGE("Marshal status:0x%{public}x", status);
86 return IPC_STUB_WRITE_PARCEL_ERR;
87 }
88 return 0;
89 }
90
OnRemoteQuery(MessageParcel & data,MessageParcel & reply)91 int32_t DataShareServiceStub::OnRemoteQuery(MessageParcel &data, MessageParcel &reply)
92 {
93 std::string uri;
94 DataSharePredicates predicate;
95 std::vector<std::string> columns;
96 if (!ITypesUtil::Unmarshal(data, uri, predicate, columns)) {
97 ZLOGE("Unmarshal uri:%{public}s columns size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
98 columns.size());
99 return IPC_STUB_INVALID_DATA_ERR;
100 }
101 int status = 0;
102 auto result = ISharedResultSet::WriteToParcel(Query(uri, predicate, columns, status), reply);
103 if (!ITypesUtil::Marshal(reply, status)) {
104 ZLOGE("Marshal status:0x%{public}x", status);
105 return IPC_STUB_WRITE_PARCEL_ERR;
106 }
107 return 0;
108 }
109
OnRemoteAddTemplate(MessageParcel & data,MessageParcel & reply)110 int32_t DataShareServiceStub::OnRemoteAddTemplate(MessageParcel &data, MessageParcel &reply)
111 {
112 std::string uri;
113 int64_t subscriberId;
114 Template tpl;
115 if (!ITypesUtil::Unmarshal(data, uri, subscriberId, tpl.predicates_, tpl.scheduler_)) {
116 ZLOGW("read device list failed.");
117 return -1;
118 }
119 int32_t status = AddTemplate(uri, subscriberId, tpl);
120 if (!ITypesUtil::Marshal(reply, status)) {
121 ZLOGE("Marshal status:0x%{public}x", status);
122 return IPC_STUB_WRITE_PARCEL_ERR;
123 }
124 return 0;
125 }
126
OnRemoteDelTemplate(MessageParcel & data,MessageParcel & reply)127 int32_t DataShareServiceStub::OnRemoteDelTemplate(MessageParcel &data, MessageParcel &reply)
128 {
129 std::string uri;
130 int64_t subscriberId;
131 if (!ITypesUtil::Unmarshal(data, uri, subscriberId)) {
132 ZLOGW("read device list failed.");
133 return -1;
134 }
135 int32_t status = DelTemplate(uri, subscriberId);
136 if (!ITypesUtil::Marshal(reply, status)) {
137 ZLOGE("Marshal status:0x%{public}x", status);
138 return IPC_STUB_WRITE_PARCEL_ERR;
139 }
140 return 0;
141 }
142
OnRemotePublish(MessageParcel & data,MessageParcel & reply)143 int32_t DataShareServiceStub::OnRemotePublish(MessageParcel &data, MessageParcel &reply)
144 {
145 Data publishData;
146 std::string bundleName;
147 if (!ITypesUtil::Unmarshal(data, publishData.datas_, publishData.version_, bundleName)) {
148 ZLOGW("read device list failed.");
149 return -1;
150 }
151 std::vector<OperationResult> results = Publish(publishData, bundleName);
152 if (!ITypesUtil::Marshal(reply, results)) {
153 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
154 return -1;
155 }
156 return 0;
157 }
158
OnRemoteGetData(MessageParcel & data,MessageParcel & reply)159 int32_t DataShareServiceStub::OnRemoteGetData(MessageParcel &data, MessageParcel &reply)
160 {
161 std::string bundleName;
162 if (!ITypesUtil::Unmarshal(data, bundleName)) {
163 ZLOGW("read device list failed.");
164 return -1;
165 }
166 ZLOGI("bundleName is %{public}s", bundleName.c_str());
167 int errorCode = E_OK;
168 auto results = GetData(bundleName, errorCode);
169 if (!ITypesUtil::Marshal(reply, results.datas_, errorCode)) {
170 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
171 return -1;
172 }
173 return 0;
174 }
175
OnRemoteSubscribeRdbData(MessageParcel & data,MessageParcel & reply)176 int32_t DataShareServiceStub::OnRemoteSubscribeRdbData(MessageParcel &data, MessageParcel &reply)
177 {
178 std::vector<std::string> uris;
179 TemplateId templateId;
180 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
181 ZLOGE("read device list failed.");
182 return -1;
183 }
184 auto remoteObj = data.ReadRemoteObject();
185 sptr<IDataProxyRdbObserver> observer = new (std::nothrow)RdbObserverProxy(remoteObj);
186 if (observer == nullptr) {
187 ZLOGE("obServer is nullptr");
188 return -1;
189 }
190 std::vector<OperationResult> results = SubscribeRdbData(uris, templateId, observer);
191 if (!ITypesUtil::Marshal(reply, results)) {
192 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
193 return -1;
194 }
195 return 0;
196 }
197
OnRemoteUnsubscribeRdbData(MessageParcel & data,MessageParcel & reply)198 int32_t DataShareServiceStub::OnRemoteUnsubscribeRdbData(MessageParcel &data, MessageParcel &reply)
199 {
200 std::vector<std::string> uris;
201 TemplateId templateId;
202 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
203 ZLOGE("read device list failed.");
204 return -1;
205 }
206 std::vector<OperationResult> results = UnsubscribeRdbData(uris, templateId);
207 if (!ITypesUtil::Marshal(reply, results)) {
208 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
209 return -1;
210 }
211 return 0;
212 }
213
OnRemoteEnableRdbSubs(MessageParcel & data,MessageParcel & reply)214 int32_t DataShareServiceStub::OnRemoteEnableRdbSubs(MessageParcel &data, MessageParcel &reply)
215 {
216 std::vector<std::string> uris;
217 TemplateId templateId;
218 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
219 ZLOGE("read device list failed.");
220 return -1;
221 }
222 std::vector<OperationResult> results = EnableRdbSubs(uris, templateId);
223 if (!ITypesUtil::Marshal(reply, results)) {
224 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
225 return -1;
226 }
227 return 0;
228 }
229
OnRemoteDisableRdbSubs(MessageParcel & data,MessageParcel & reply)230 int32_t DataShareServiceStub::OnRemoteDisableRdbSubs(MessageParcel &data, MessageParcel &reply)
231 {
232 std::vector<std::string> uris;
233 TemplateId templateId;
234 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
235 ZLOGE("read device list failed.");
236 return -1;
237 }
238 std::vector<OperationResult> results = DisableRdbSubs(uris, templateId);
239 if (!ITypesUtil::Marshal(reply, results)) {
240 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
241 return -1;
242 }
243 return 0;
244 }
245
OnRemoteSubscribePublishedData(MessageParcel & data,MessageParcel & reply)246 int32_t DataShareServiceStub::OnRemoteSubscribePublishedData(MessageParcel &data, MessageParcel &reply)
247 {
248 std::vector<std::string> uris;
249 int64_t subscriberId;
250 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
251 ZLOGE("read device list failed.");
252 return -1;
253 }
254 sptr<PublishedDataObserverProxy> observer = new (std::nothrow)PublishedDataObserverProxy(data.ReadRemoteObject());
255 if (observer == nullptr) {
256 ZLOGE("obServer is nullptr");
257 return -1;
258 }
259 std::vector<OperationResult> results = SubscribePublishedData(uris, subscriberId, observer);
260 if (!ITypesUtil::Marshal(reply, results)) {
261 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
262 return -1;
263 }
264 return 0;
265 }
266
OnRemoteUnsubscribePublishedData(MessageParcel & data,MessageParcel & reply)267 int32_t DataShareServiceStub::OnRemoteUnsubscribePublishedData(MessageParcel &data, MessageParcel &reply)
268 {
269 std::vector<std::string> uris;
270 int64_t subscriberId;
271 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
272 ZLOGE("read device list failed.");
273 return -1;
274 }
275 std::vector<OperationResult> results = UnsubscribePublishedData(uris, subscriberId);
276 if (!ITypesUtil::Marshal(reply, results)) {
277 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
278 return -1;
279 }
280 return 0;
281 }
282
OnRemoteEnablePubSubs(MessageParcel & data,MessageParcel & reply)283 int32_t DataShareServiceStub::OnRemoteEnablePubSubs(MessageParcel &data, MessageParcel &reply)
284 {
285 std::vector<std::string> uris;
286 int64_t subscriberId;
287 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
288 ZLOGE("read device list failed.");
289 return -1;
290 }
291 std::vector<OperationResult> results = EnablePubSubs(uris, subscriberId);
292 if (!ITypesUtil::Marshal(reply, results)) {
293 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
294 return -1;
295 }
296 return 0;
297 }
298
OnRemoteDisablePubSubs(MessageParcel & data,MessageParcel & reply)299 int32_t DataShareServiceStub::OnRemoteDisablePubSubs(MessageParcel &data, MessageParcel &reply)
300 {
301 std::vector<std::string> uris;
302 int64_t subscriberId;
303 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
304 ZLOGE("read device list failed.");
305 return -1;
306 }
307 std::vector<OperationResult> results = DisablePubSubs(uris, subscriberId);
308 if (!ITypesUtil::Marshal(reply, results)) {
309 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
310 return -1;
311 }
312 return 0;
313 }
314
OnRemoteNotifyConnectDone(MessageParcel & data,MessageParcel & reply)315 int32_t DataShareServiceStub::OnRemoteNotifyConnectDone(MessageParcel &data, MessageParcel &reply)
316 {
317 OnConnectDone();
318 return 0;
319 }
320
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)321 int DataShareServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
322 {
323 ZLOGD("code:%{public}u, callingPid:%{public}d", code, IPCSkeleton::GetCallingPid());
324 if (!CheckInterfaceToken(data)) {
325 return DATA_SHARE_ERROR;
326 }
327 if (code < DATA_SHARE_SERVICE_CMD_MAX) {
328 return (this->*HANDLERS[code])(data, reply);
329 }
330 return -1;
331 }
332
OnRemoteNotifyObserver(MessageParcel & data,MessageParcel & reply)333 int32_t DataShareServiceStub::OnRemoteNotifyObserver(MessageParcel &data, MessageParcel &reply)
334 {
335 std::string uri;
336 if (!ITypesUtil::Unmarshal(data, uri)) {
337 ZLOGE("read device list failed.");
338 return -1;
339 }
340 NotifyObserver(uri);
341 return 0;
342 }
343 } // namespace DataShare
344 } // namespace OHOS