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 <cinttypes>
21 #include "accesstoken_kit.h"
22 #include "tokenid_kit.h"
23 #include "data_share_obs_proxy.h"
24 #include "hiview_adapter.h"
25 #include "hiview_fault_adapter.h"
26 #include "ipc_skeleton.h"
27 #include "ishared_result_set.h"
28 #include "itypes_util.h"
29 #include "log_print.h"
30 #include "qos.h"
31 #include "utils/anonymous.h"
32
33 namespace OHOS {
34 namespace DataShare {
35
36 class DataShareServiceStub::QosManager {
37 public:
QosManager()38 QosManager()
39 {
40 // set thread qos QOS_USER_INTERACTIVE
41 QOS::SetThreadQos(QOS::QosLevel::QOS_USER_INTERACTIVE);
42 }
~QosManager()43 ~QosManager()
44 {
45 QOS::ResetThreadQos();
46 }
47 };
48
CheckInterfaceToken(MessageParcel & data)49 bool DataShareServiceStub::CheckInterfaceToken(MessageParcel &data)
50 {
51 auto localDescriptor = IDataShareService::GetDescriptor();
52 auto remoteDescriptor = data.ReadInterfaceToken();
53 if (remoteDescriptor != localDescriptor) {
54 ZLOGE("interface token is not equal");
55 return false;
56 }
57 return true;
58 }
59
OnInsertEx(MessageParcel & data,MessageParcel & reply)60 int32_t DataShareServiceStub::OnInsertEx(MessageParcel &data, MessageParcel &reply)
61 {
62 std::string uri;
63 std::string extUri;
64 DataShareValuesBucket bucket;
65 if (!ITypesUtil::Unmarshal(data, uri, extUri, bucket.valuesMap)) {
66 ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
67 bucket.valuesMap.size());
68 return IPC_STUB_INVALID_DATA_ERR;
69 }
70 auto [errCode, status] = InsertEx(uri, extUri, bucket);
71 if (!ITypesUtil::Marshal(reply, errCode, status)) {
72 ZLOGE("Marshal errCode: 0x%{public}x, status: 0x%{public}x", errCode, status);
73 return IPC_STUB_WRITE_PARCEL_ERR;
74 }
75 return 0;
76 }
77
OnUpdateEx(MessageParcel & data,MessageParcel & reply)78 int32_t DataShareServiceStub::OnUpdateEx(MessageParcel &data, MessageParcel &reply)
79 {
80 std::string uri;
81 std::string extUri;
82 DataSharePredicates predicate;
83 DataShareValuesBucket bucket;
84 if (!ITypesUtil::Unmarshal(data, uri, extUri, predicate, bucket.valuesMap)) {
85 ZLOGE("Unmarshal uri:%{public}s bucket size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
86 bucket.valuesMap.size());
87 return IPC_STUB_INVALID_DATA_ERR;
88 }
89 auto [errCode, status] = UpdateEx(uri, extUri, predicate, bucket);
90 if (!ITypesUtil::Marshal(reply, errCode, status)) {
91 ZLOGE("Marshal errCode: 0x%{public}x, status: 0x%{public}x", errCode, status);
92 return IPC_STUB_WRITE_PARCEL_ERR;
93 }
94 return 0;
95 }
96
OnDeleteEx(MessageParcel & data,MessageParcel & reply)97 int32_t DataShareServiceStub::OnDeleteEx(MessageParcel &data, MessageParcel &reply)
98 {
99 std::string uri;
100 std::string extUri;
101 DataSharePredicates predicate;
102 if (!ITypesUtil::Unmarshal(data, uri, extUri, predicate)) {
103 ZLOGE("Unmarshal uri:%{public}s", DistributedData::Anonymous::Change(uri).c_str());
104 return IPC_STUB_INVALID_DATA_ERR;
105 }
106 auto [errCode, status] = DeleteEx(uri, extUri, predicate);
107 if (!ITypesUtil::Marshal(reply, errCode, status)) {
108 ZLOGE("Marshal errCode: 0x%{public}x, status: 0x%{public}x", errCode, status);
109 return IPC_STUB_WRITE_PARCEL_ERR;
110 }
111 return 0;
112 }
113
OnQuery(MessageParcel & data,MessageParcel & reply)114 int32_t DataShareServiceStub::OnQuery(MessageParcel &data, MessageParcel &reply)
115 {
116 std::string uri;
117 std::string extUri;
118 DataSharePredicates predicate;
119 std::vector<std::string> columns;
120 if (!ITypesUtil::Unmarshal(data, uri, extUri, predicate, columns)) {
121 ZLOGE("Unmarshal uri:%{public}s columns size:%{public}zu", DistributedData::Anonymous::Change(uri).c_str(),
122 columns.size());
123 return IPC_STUB_INVALID_DATA_ERR;
124 }
125 int status = 0;
126 auto result = ISharedResultSet::WriteToParcel(Query(uri, extUri, predicate, columns, status), reply);
127 if (!ITypesUtil::Marshal(reply, status)) {
128 ZLOGE("Marshal status:0x%{public}x", status);
129 return IPC_STUB_WRITE_PARCEL_ERR;
130 }
131 return 0;
132 }
133
OnAddTemplate(MessageParcel & data,MessageParcel & reply)134 int32_t DataShareServiceStub::OnAddTemplate(MessageParcel &data, MessageParcel &reply)
135 {
136 std::string uri;
137 int64_t subscriberId;
138 Template tpl;
139 if (!ITypesUtil::Unmarshal(data, uri, subscriberId, tpl.update_, tpl.predicates_, tpl.scheduler_)) {
140 ZLOGW("read device list failed.");
141 return -1;
142 }
143 int32_t status = AddTemplate(uri, subscriberId, tpl);
144 if (!ITypesUtil::Marshal(reply, status)) {
145 ZLOGE("Marshal status:0x%{public}x", status);
146 return IPC_STUB_WRITE_PARCEL_ERR;
147 }
148 return 0;
149 }
150
OnDelTemplate(MessageParcel & data,MessageParcel & reply)151 int32_t DataShareServiceStub::OnDelTemplate(MessageParcel &data, MessageParcel &reply)
152 {
153 std::string uri;
154 int64_t subscriberId;
155 if (!ITypesUtil::Unmarshal(data, uri, subscriberId)) {
156 ZLOGW("read device list failed.");
157 return -1;
158 }
159 int32_t status = DelTemplate(uri, subscriberId);
160 if (!ITypesUtil::Marshal(reply, status)) {
161 ZLOGE("Marshal status:0x%{public}x", status);
162 return IPC_STUB_WRITE_PARCEL_ERR;
163 }
164 return 0;
165 }
166
OnPublish(MessageParcel & data,MessageParcel & reply)167 int32_t DataShareServiceStub::OnPublish(MessageParcel &data, MessageParcel &reply)
168 {
169 Data publishData;
170 std::string bundleName;
171 if (!ITypesUtil::Unmarshal(data, publishData.datas_, publishData.version_, bundleName)) {
172 ZLOGW("read device list failed.");
173 return -1;
174 }
175 std::vector<OperationResult> results = Publish(publishData, bundleName);
176 if (!ITypesUtil::Marshal(reply, results)) {
177 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
178 return -1;
179 }
180 return 0;
181 }
182
OnGetData(MessageParcel & data,MessageParcel & reply)183 int32_t DataShareServiceStub::OnGetData(MessageParcel &data, MessageParcel &reply)
184 {
185 std::string bundleName;
186 if (!ITypesUtil::Unmarshal(data, bundleName)) {
187 ZLOGW("read device list failed.");
188 return -1;
189 }
190 ZLOGI("bundleName is %{public}s", bundleName.c_str());
191 int errorCode = E_OK;
192 auto results = GetData(bundleName, errorCode);
193 if (!ITypesUtil::Marshal(reply, results.datas_, errorCode)) {
194 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
195 return -1;
196 }
197 return 0;
198 }
199
OnSubscribeRdbData(MessageParcel & data,MessageParcel & reply)200 int32_t DataShareServiceStub::OnSubscribeRdbData(MessageParcel &data, MessageParcel &reply)
201 {
202 std::vector<std::string> uris;
203 TemplateId templateId;
204 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
205 ZLOGE("read device list failed.");
206 return -1;
207 }
208 auto remoteObj = data.ReadRemoteObject();
209 sptr<IDataProxyRdbObserver> observer = new (std::nothrow)RdbObserverProxy(remoteObj);
210 if (observer == nullptr) {
211 ZLOGE("obServer is nullptr");
212 return -1;
213 }
214 std::vector<OperationResult> results = SubscribeRdbData(uris, templateId, observer);
215 if (!ITypesUtil::Marshal(reply, results)) {
216 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
217 return -1;
218 }
219 return 0;
220 }
221
OnUnsubscribeRdbData(MessageParcel & data,MessageParcel & reply)222 int32_t DataShareServiceStub::OnUnsubscribeRdbData(MessageParcel &data, MessageParcel &reply)
223 {
224 std::vector<std::string> uris;
225 TemplateId templateId;
226 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
227 ZLOGE("read device list failed.");
228 return -1;
229 }
230 std::vector<OperationResult> results = UnsubscribeRdbData(uris, templateId);
231 if (!ITypesUtil::Marshal(reply, results)) {
232 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
233 return -1;
234 }
235 return 0;
236 }
237
OnEnableRdbSubs(MessageParcel & data,MessageParcel & reply)238 int32_t DataShareServiceStub::OnEnableRdbSubs(MessageParcel &data, MessageParcel &reply)
239 {
240 std::vector<std::string> uris;
241 TemplateId templateId;
242 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
243 ZLOGE("read device list failed.");
244 return -1;
245 }
246 std::vector<OperationResult> results = EnableRdbSubs(uris, templateId);
247 if (!ITypesUtil::Marshal(reply, results)) {
248 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
249 return -1;
250 }
251 return 0;
252 }
253
OnDisableRdbSubs(MessageParcel & data,MessageParcel & reply)254 int32_t DataShareServiceStub::OnDisableRdbSubs(MessageParcel &data, MessageParcel &reply)
255 {
256 std::vector<std::string> uris;
257 TemplateId templateId;
258 if (!ITypesUtil::Unmarshal(data, uris, templateId)) {
259 ZLOGE("read device list failed.");
260 return -1;
261 }
262 std::vector<OperationResult> results = DisableRdbSubs(uris, templateId);
263 if (!ITypesUtil::Marshal(reply, results)) {
264 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
265 return -1;
266 }
267 return 0;
268 }
269
OnSubscribePublishedData(MessageParcel & data,MessageParcel & reply)270 int32_t DataShareServiceStub::OnSubscribePublishedData(MessageParcel &data, MessageParcel &reply)
271 {
272 std::vector<std::string> uris;
273 int64_t subscriberId;
274 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
275 ZLOGE("read device list failed.");
276 return -1;
277 }
278 sptr<PublishedDataObserverProxy> observer = new (std::nothrow)PublishedDataObserverProxy(data.ReadRemoteObject());
279 if (observer == nullptr) {
280 ZLOGE("obServer is nullptr");
281 return -1;
282 }
283 std::vector<OperationResult> results = SubscribePublishedData(uris, subscriberId, observer);
284 if (!ITypesUtil::Marshal(reply, results)) {
285 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
286 return -1;
287 }
288 return 0;
289 }
290
OnUnsubscribePublishedData(MessageParcel & data,MessageParcel & reply)291 int32_t DataShareServiceStub::OnUnsubscribePublishedData(MessageParcel &data, MessageParcel &reply)
292 {
293 std::vector<std::string> uris;
294 int64_t subscriberId;
295 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
296 ZLOGE("read device list failed.");
297 return -1;
298 }
299 std::vector<OperationResult> results = UnsubscribePublishedData(uris, subscriberId);
300 if (!ITypesUtil::Marshal(reply, results)) {
301 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
302 return -1;
303 }
304 return 0;
305 }
306
OnEnablePubSubs(MessageParcel & data,MessageParcel & reply)307 int32_t DataShareServiceStub::OnEnablePubSubs(MessageParcel &data, MessageParcel &reply)
308 {
309 std::vector<std::string> uris;
310 int64_t subscriberId;
311 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
312 ZLOGE("read device list failed.");
313 return -1;
314 }
315 std::vector<OperationResult> results = EnablePubSubs(uris, subscriberId);
316 if (!ITypesUtil::Marshal(reply, results)) {
317 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
318 return -1;
319 }
320 return 0;
321 }
322
OnDisablePubSubs(MessageParcel & data,MessageParcel & reply)323 int32_t DataShareServiceStub::OnDisablePubSubs(MessageParcel &data, MessageParcel &reply)
324 {
325 std::vector<std::string> uris;
326 int64_t subscriberId;
327 if (!ITypesUtil::Unmarshal(data, uris, subscriberId)) {
328 ZLOGE("read device list failed.");
329 return -1;
330 }
331 std::vector<OperationResult> results = DisablePubSubs(uris, subscriberId);
332 if (!ITypesUtil::Marshal(reply, results)) {
333 ZLOGE("ITypesUtil::Marshal(reply, results) failed");
334 return -1;
335 }
336 return 0;
337 }
338
OnNotifyConnectDone(MessageParcel & data,MessageParcel & reply)339 int32_t DataShareServiceStub::OnNotifyConnectDone(MessageParcel &data, MessageParcel &reply)
340 {
341 OnConnectDone();
342 return 0;
343 }
344
CheckProxyCallingPermission(uint32_t tokenId)345 bool DataShareServiceStub::CheckProxyCallingPermission(uint32_t tokenId)
346 {
347 Security::AccessToken::ATokenTypeEnum tokenType =
348 Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(tokenId);
349 return (tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE ||
350 tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL);
351 }
352
353 // GetTokenType use tokenId, and IsSystemApp use fullTokenId, these are different
CheckSystemUidCallingPermission(uint32_t tokenId,uint64_t fullTokenId)354 bool DataShareServiceStub::CheckSystemUidCallingPermission(uint32_t tokenId, uint64_t fullTokenId)
355 {
356 if (CheckProxyCallingPermission(tokenId)) {
357 return true;
358 }
359 // IsSystemAppByFullTokenID here is not IPC
360 return Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(fullTokenId);
361 }
362
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply)363 int DataShareServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply)
364 {
365 // set thread qos
366 DataShareServiceStub::QosManager qos;
367 // check thread qos
368 QOS::QosLevel curLevel;
369 int qosRet = QOS::GetThreadQos(curLevel);
370
371 int tryTimes = TRY_TIMES;
372 while (!isReady_.load() && tryTimes > 0) {
373 tryTimes--;
374 std::this_thread::sleep_for(std::chrono::milliseconds(SLEEP_TIME));
375 }
376 auto callingPid = IPCSkeleton::GetCallingPid();
377 if (code >= DATA_SHARE_CMD_SYSTEM_CODE) {
378 auto fullTokenId = IPCSkeleton::GetCallingFullTokenID();
379 if (!CheckSystemUidCallingPermission(IPCSkeleton::GetCallingTokenID(), fullTokenId)) {
380 ZLOGE("CheckSystemUidCallingPermission fail, token:%{public}" PRIx64
381 ", callingPid:%{public}d, code:%{public}u", fullTokenId, callingPid, code);
382 return E_NOT_SYSTEM_APP;
383 }
384 code = code - DATA_SHARE_CMD_SYSTEM_CODE;
385 }
386 if (code != DATA_SHARE_SERVICE_CMD_QUERY && code != DATA_SHARE_SERVICE_CMD_GET_SILENT_PROXY_STATUS) {
387 ZLOGI("code:%{public}u, callingPid:%{public}d, qosRet:%{public}d, curLevel:%{public}d",
388 code, callingPid, qosRet, curLevel);
389 }
390 if (!CheckInterfaceToken(data)) {
391 return DATA_SHARE_ERROR;
392 }
393 int res = -1;
394 if (code < DATA_SHARE_SERVICE_CMD_MAX) {
395 auto callingTokenid = IPCSkeleton::GetCallingTokenID();
396 auto start = std::chrono::steady_clock::now();
397 res = (this->*HANDLERS[code])(data, reply);
398 auto finish = std::chrono::steady_clock::now();
399 auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start);
400 CallerInfo callerInfo(callingTokenid, IPCSkeleton::GetCallingUid(), callingPid, duration.count(), code);
401 if (duration >= TIME_THRESHOLD) {
402 int64_t milliseconds = duration.count();
403 ZLOGW("over time, code:%{public}u callingPid:%{public}d, cost:%{public}" PRIi64 "ms",
404 code, callingPid, milliseconds);
405 callerInfo.isSlowRequest = true;
406 }
407 HiViewAdapter::GetInstance().ReportDataStatistic(callerInfo);
408 }
409 return res;
410 }
411
OnNotifyObserver(MessageParcel & data,MessageParcel & reply)412 int32_t DataShareServiceStub::OnNotifyObserver(MessageParcel &data, MessageParcel &reply)
413 {
414 std::string uri;
415 if (!ITypesUtil::Unmarshal(data, uri)) {
416 ZLOGE("read device list failed.");
417 return -1;
418 }
419 NotifyObserver(uri);
420 return 0;
421 }
422
OnSetSilentSwitch(MessageParcel & data,MessageParcel & reply)423 int32_t DataShareServiceStub::OnSetSilentSwitch(MessageParcel &data, MessageParcel &reply)
424 {
425 std::string uri;
426 bool enable = false;
427 if (!ITypesUtil::Unmarshal(data, uri, enable)) {
428 ZLOGE("Unmarshal set silent switch failed. uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str());
429 return IPC_STUB_INVALID_DATA_ERR;
430 }
431 int32_t status = EnableSilentProxy(uri, enable);
432 if (!ITypesUtil::Marshal(reply, status)) {
433 ZLOGE("Marshal status:0x%{public}x", status);
434 return IPC_STUB_WRITE_PARCEL_ERR;
435 }
436 return E_OK;
437 }
438
OnGetSilentProxyStatus(MessageParcel & data,MessageParcel & reply)439 int32_t DataShareServiceStub::OnGetSilentProxyStatus(MessageParcel &data, MessageParcel &reply)
440 {
441 std::string uri;
442 if (!ITypesUtil::Unmarshal(data, uri)) {
443 ZLOGE("Unmarshal silent enable failed. uri: %{public}s", DistributedData::Anonymous::Change(uri).c_str());
444 return IPC_STUB_INVALID_DATA_ERR;
445 }
446 int32_t enable = GetSilentProxyStatus(uri);
447 if (!ITypesUtil::Marshal(reply, enable)) {
448 ZLOGE("Marshal enable:%{public}d failed.", enable);
449 return IPC_STUB_WRITE_PARCEL_ERR;
450 }
451 return E_OK;
452 }
453
OnRegisterObserver(MessageParcel & data,MessageParcel & reply)454 int32_t DataShareServiceStub::OnRegisterObserver(MessageParcel &data, MessageParcel &reply)
455 {
456 return DATA_SHARE_ERROR;
457 }
458
OnUnregisterObserver(MessageParcel & data,MessageParcel & reply)459 int32_t DataShareServiceStub::OnUnregisterObserver(MessageParcel &data, MessageParcel &reply)
460 {
461 return DATA_SHARE_ERROR;
462 }
463
SetServiceReady()464 void DataShareServiceStub::SetServiceReady()
465 {
466 isReady_.store(true);
467 }
468 } // namespace DataShare
469 } // namespace OHOS