1 /*
2 * Copyright (c) 2022-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 #include "datashare_proxy.h"
17
18 #include <string_ex.h>
19
20 #include "data_ability_observer_interface.h"
21 #include "datashare_itypes_utils.h"
22 #include "datashare_log.h"
23 #include "datashare_result_set.h"
24 #include "ipc_types.h"
25 #include "ishared_result_set.h"
26 #include "pac_map.h"
27
28 using namespace OHOS::DistributedShare::DataShare;
29
30 namespace OHOS {
31 namespace DataShare {
32 constexpr int32_t PERMISSION_ERR = 1;
33 constexpr int PERMISSION_ERR_CODE = -2;
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)34 std::vector<std::string> DataShareProxy::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
35 {
36 std::vector<std::string> types;
37
38 MessageParcel data;
39 MessageParcel reply;
40 MessageOption option;
41
42 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
43 LOG_ERROR("WriteInterfaceToken failed");
44 return types;
45 }
46
47 if (!data.WriteParcelable(&uri)) {
48 LOG_ERROR("fail to WriteParcelable uri");
49 return types;
50 }
51
52 if (!data.WriteString(mimeTypeFilter)) {
53 LOG_ERROR("fail to WriteString mimeTypeFilter");
54 return types;
55 }
56
57 int32_t err = Remote()->SendRequest(
58 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_GET_FILE_TYPES), data, reply, option);
59 if (err != E_OK) {
60 LOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
61 }
62
63 if (!reply.ReadStringVector(&types)) {
64 LOG_ERROR("fail to ReadStringVector types");
65 }
66
67 return types;
68 }
69
OpenFile(const Uri & uri,const std::string & mode)70 int DataShareProxy::OpenFile(const Uri &uri, const std::string &mode)
71 {
72 int fd = -1;
73 MessageParcel data;
74 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
75 LOG_ERROR("WriteInterfaceToken failed");
76 return fd;
77 }
78
79 if (!data.WriteParcelable(&uri)) {
80 LOG_ERROR("fail to WriteParcelable uri");
81 return fd;
82 }
83
84 if (!data.WriteString(mode)) {
85 LOG_ERROR("fail to WriteString mode");
86 return fd;
87 }
88
89 MessageParcel reply;
90 MessageOption option;
91 int32_t err = Remote()->SendRequest(
92 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_OPEN_FILE), data, reply, option);
93 if (err != E_OK) {
94 LOG_ERROR("OpenFile fail to SendRequest. err: %{public}d", err);
95 return fd;
96 }
97
98 fd = reply.ReadFileDescriptor();
99 if (fd == -1) {
100 LOG_ERROR("fail to ReadFileDescriptor fd");
101 return fd;
102 }
103
104 return fd;
105 }
106
OpenRawFile(const Uri & uri,const std::string & mode)107 int DataShareProxy::OpenRawFile(const Uri &uri, const std::string &mode)
108 {
109 int fd = -1;
110 MessageParcel data;
111 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
112 LOG_ERROR("WriteInterfaceToken failed");
113 return fd;
114 }
115
116 if (!data.WriteParcelable(&uri)) {
117 LOG_ERROR("fail to WriteParcelable uri");
118 return fd;
119 }
120
121 if (!data.WriteString(mode)) {
122 LOG_ERROR("fail to WriteString mode");
123 return fd;
124 }
125
126 MessageParcel reply;
127 MessageOption option;
128 int32_t err = Remote()->SendRequest(
129 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_OPEN_RAW_FILE), data, reply, option);
130 if (err != E_OK) {
131 LOG_ERROR("OpenRawFile fail to SendRequest. err: %{public}d", err);
132 return fd;
133 }
134
135 if (!reply.ReadInt32(fd)) {
136 LOG_ERROR("fail to ReadInt32 fd");
137 return fd;
138 }
139
140 return fd;
141 }
142
Insert(const Uri & uri,const DataShareValuesBucket & value)143 int DataShareProxy::Insert(const Uri &uri, const DataShareValuesBucket &value)
144 {
145 int index = -1;
146 MessageParcel data;
147 data.SetMaxCapacity(MTU_SIZE);
148 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
149 LOG_ERROR("WriteInterfaceToken failed");
150 return index;
151 }
152 if (!ITypesUtil::Marshal(data, uri, value)) {
153 LOG_ERROR("fail to Marshal value");
154 return index;
155 }
156 MessageParcel reply;
157 MessageOption option;
158 int32_t err = Remote()->SendRequest(
159 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT), data, reply, option);
160 if (err != E_OK) {
161 LOG_ERROR("Insert fail to SendRequest. err: %{public}d", err);
162 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
163 }
164 if (!ITypesUtil::Unmarshal(reply, index)) {
165 LOG_ERROR("fail to Unmarshal index");
166 return index;
167 }
168
169 return index;
170 }
171
InsertExt(const Uri & uri,const DataShareValuesBucket & value,std::string & result)172 int DataShareProxy::InsertExt(const Uri &uri, const DataShareValuesBucket &value, std::string &result)
173 {
174 int index = -1;
175 MessageParcel data;
176 data.SetMaxCapacity(MTU_SIZE);
177 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
178 LOG_ERROR("WriteInterfaceToken failed");
179 return index;
180 }
181 if (!ITypesUtil::Marshal(data, uri, value)) {
182 LOG_ERROR("fail to Marshal value");
183 return index;
184 }
185 MessageParcel reply;
186 MessageOption option;
187 int32_t err = Remote()->SendRequest(
188 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT_EXT), data, reply, option);
189 if (err != E_OK) {
190 LOG_ERROR("Insert fail to SendRequest. err: %{public}d", err);
191 return index;
192 }
193 if (!ITypesUtil::Unmarshal(reply, index, result)) {
194 LOG_ERROR("fail to Unmarshal index");
195 return index;
196 }
197 return index;
198 }
199
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)200 int DataShareProxy::Update(const Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
201 {
202 int index = -1;
203 MessageParcel data;
204 data.SetMaxCapacity(MTU_SIZE);
205 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
206 LOG_ERROR("WriteInterfaceToken failed");
207 return index;
208 }
209 if (!ITypesUtil::Marshal(data, uri, predicates, value)) {
210 LOG_ERROR("fail to Marshal value");
211 return index;
212 }
213 MessageParcel reply;
214 MessageOption option;
215 int32_t err = Remote()->SendRequest(
216 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UPDATE), data, reply, option);
217 if (err != E_OK) {
218 LOG_ERROR("Update fail to SendRequest. err: %{public}d", err);
219 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
220 }
221 if (!ITypesUtil::Unmarshal(reply, index)) {
222 LOG_ERROR("fail to Unmarshal index");
223 return index;
224 }
225 return index;
226 }
227
BatchUpdate(const UpdateOperations & operations,std::vector<BatchUpdateResult> & results)228 int DataShareProxy::BatchUpdate(const UpdateOperations &operations, std::vector<BatchUpdateResult> &results)
229 {
230 int ret = -1;
231 MessageParcel data;
232 data.SetMaxCapacity(MTU_SIZE);
233 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
234 LOG_ERROR("WriteInterfaceToken failed");
235 return ret;
236 }
237 if (!CheckSize(operations)) {
238 return ret;
239 }
240 if (!ITypesUtil::Marshal(data, operations)) {
241 LOG_ERROR("fail to Marshalling");
242 return ret;
243 }
244 MessageParcel reply;
245 MessageOption option;
246 int32_t err = Remote()->SendRequest(
247 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_BATCH_UPDATE), data, reply, option);
248 if (err != E_OK) {
249 LOG_ERROR("BatchUpdate fail to SendRequest. err: %{public}d", err);
250 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : ret;
251 }
252 if (!ITypesUtil::Unmarshal(reply, results)) {
253 LOG_ERROR("fail to Unmarshal result");
254 return ret;
255 }
256 return 0;
257 }
258
Delete(const Uri & uri,const DataSharePredicates & predicates)259 int DataShareProxy::Delete(const Uri &uri, const DataSharePredicates &predicates)
260 {
261 int index = -1;
262 MessageParcel data;
263 data.SetMaxCapacity(MTU_SIZE);
264 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
265 LOG_ERROR("WriteInterfaceToken failed");
266 return index;
267 }
268 if (!ITypesUtil::Marshal(data, uri, predicates)) {
269 LOG_ERROR("fail to Marshalling predicates");
270 return index;
271 }
272
273 MessageParcel reply;
274 MessageOption option;
275 int32_t err = Remote()->SendRequest(
276 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DELETE), data, reply, option);
277 if (err != E_OK) {
278 LOG_ERROR("Delete fail to SendRequest. err: %{public}d", err);
279 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
280 }
281 if (!ITypesUtil::Unmarshal(reply, index)) {
282 LOG_ERROR("fail to Unmarshal index");
283 return index;
284 }
285 return index;
286 }
287
InsertEx(const Uri & uri,const DataShareValuesBucket & value)288 std::pair<int32_t, int32_t> DataShareProxy::InsertEx(const Uri &uri, const DataShareValuesBucket &value)
289 {
290 MessageParcel data;
291 data.SetMaxCapacity(MTU_SIZE);
292 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
293 LOG_ERROR("WriteInterfaceToken failed");
294 return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
295 }
296 if (!ITypesUtil::Marshal(data, uri, value)) {
297 LOG_ERROR("fail to Marshal value");
298 return std::make_pair(E_MARSHAL_ERROR, 0);
299 }
300
301 int32_t errCode = -1;
302 int32_t result = -1;
303 MessageParcel reply;
304 MessageOption option;
305 int32_t err = Remote()->SendRequest(
306 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT_EX), data, reply, option);
307 if (err != E_OK) {
308 LOG_ERROR("InsertEx fail to SendRequest. err: %{public}d", err);
309 return std::make_pair((err == PERMISSION_ERR ? PERMISSION_ERR_CODE : errCode), 0);
310 }
311
312 if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
313 LOG_ERROR("fail to Unmarshal");
314 return std::make_pair(E_UNMARSHAL_ERROR, 0);
315 }
316 return std::make_pair(errCode, result);
317 }
318
UpdateEx(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)319 std::pair<int32_t, int32_t> DataShareProxy::UpdateEx(const Uri &uri, const DataSharePredicates &predicates,
320 const DataShareValuesBucket &value)
321 {
322 MessageParcel data;
323 data.SetMaxCapacity(MTU_SIZE);
324 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
325 LOG_ERROR("WriteInterfaceToken failed");
326 return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
327 }
328 if (!ITypesUtil::Marshal(data, uri, predicates, value)) {
329 LOG_ERROR("fail to Marshal value");
330 return std::make_pair(E_MARSHAL_ERROR, 0);
331 }
332
333 int32_t errCode = -1;
334 int32_t result = -1;
335 MessageParcel reply;
336 MessageOption option;
337 int32_t err = Remote()->SendRequest(
338 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UPDATE_EX), data, reply, option);
339 if (err != E_OK) {
340 LOG_ERROR("UpdateEx fail to SendRequest. err: %{public}d", err);
341 return std::make_pair((err == PERMISSION_ERR ? PERMISSION_ERR_CODE : errCode), 0);
342 }
343
344 if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
345 LOG_ERROR("fail to Unmarshal");
346 return std::make_pair(E_UNMARSHAL_ERROR, 0);
347 }
348 return std::make_pair(errCode, result);
349 }
350
DeleteEx(const Uri & uri,const DataSharePredicates & predicates)351 std::pair<int32_t, int32_t> DataShareProxy::DeleteEx(const Uri &uri, const DataSharePredicates &predicates)
352 {
353 MessageParcel data;
354 data.SetMaxCapacity(MTU_SIZE);
355 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
356 LOG_ERROR("WriteInterfaceToken failed");
357 return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
358 }
359 if (!ITypesUtil::Marshal(data, uri, predicates)) {
360 LOG_ERROR("fail to Marshalling predicates");
361 return std::make_pair(E_MARSHAL_ERROR, 0);
362 }
363
364 int32_t errCode = -1;
365 int32_t result = -1;
366 MessageParcel reply;
367 MessageOption option;
368 int32_t err = Remote()->SendRequest(
369 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DELETE_EX), data, reply, option);
370 if (err != E_OK) {
371 LOG_ERROR("DeleteEx fail to SendRequest. err: %{public}d", err);
372 return std::make_pair((err == PERMISSION_ERR ? PERMISSION_ERR_CODE : errCode), 0);
373 }
374
375 if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
376 LOG_ERROR("fail to Unmarshal");
377 return std::make_pair(E_UNMARSHAL_ERROR, 0);
378 }
379 return std::make_pair(errCode, result);
380 }
381
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError)382 std::shared_ptr<DataShareResultSet> DataShareProxy::Query(const Uri &uri, const DataSharePredicates &predicates,
383 std::vector<std::string> &columns, DatashareBusinessError &businessError)
384 {
385 MessageParcel data;
386 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
387 LOG_ERROR("WriteInterfaceToken failed");
388 return nullptr;
389 }
390 if (!ITypesUtil::Marshal(data, uri, columns)) {
391 LOG_ERROR("Marshalling uri and columns to data failed");
392 return nullptr;
393 }
394 if (!ITypesUtil::MarshalPredicates(predicates, data)) {
395 LOG_ERROR("Marshalling predicates to shared-memory failed");
396 return nullptr;
397 }
398 MessageParcel reply;
399 MessageOption option;
400 int32_t err = Remote()->SendRequest(
401 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_QUERY), data, reply, option);
402 auto result = ISharedResultSet::ReadFromParcel(reply);
403 businessError.SetCode(reply.ReadInt32());
404 businessError.SetMessage(reply.ReadString());
405 if (err != E_OK) {
406 LOG_ERROR("Query fail to SendRequest. err: %{public}d", err);
407 return nullptr;
408 }
409 return result;
410 }
411
GetType(const Uri & uri)412 std::string DataShareProxy::GetType(const Uri &uri)
413 {
414 std::string type;
415 MessageParcel data;
416 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
417 LOG_ERROR("WriteInterfaceToken failed");
418 return type;
419 }
420
421 if (!ITypesUtil::Marshal(data, uri)) {
422 LOG_ERROR("fail to Marshal value");
423 return type;
424 }
425
426 MessageParcel reply;
427 MessageOption option;
428 int32_t err = Remote()->SendRequest(
429 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_GET_TYPE), data, reply, option);
430 if (err != E_OK) {
431 LOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
432 return type;
433 }
434 if (!ITypesUtil::Unmarshal(reply, type)) {
435 LOG_ERROR("fail to Unmarshal index");
436 return type;
437 }
438 if (type.empty()) {
439 LOG_ERROR("fail to ReadString type");
440 return type;
441 }
442
443 return type;
444 }
445
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)446 int DataShareProxy::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
447 {
448 int ret = -1;
449 MessageParcel data;
450 data.SetMaxCapacity(MTU_SIZE);
451 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
452 LOG_ERROR("WriteInterfaceToken failed");
453 return ret;
454 }
455 if (!ITypesUtil::Marshal(data, uri)) {
456 LOG_ERROR("Marshalling uri to data failed");
457 return ret;
458 }
459 if (!ITypesUtil::MarshalValuesBucketVec(values, data)) {
460 LOG_ERROR("Marshalling DataShareValuesBucket to shared-memory failed");
461 return ret;
462 }
463 MessageParcel reply;
464 MessageOption option;
465 int32_t err = Remote()->SendRequest(
466 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_BATCH_INSERT), data, reply, option);
467 if (err != E_OK) {
468 LOG_ERROR("fail to SendRequest. err: %{public}d", err);
469 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : ret;
470 }
471 if (!ITypesUtil::Unmarshal(reply, ret)) {
472 LOG_ERROR("fail to Unmarshal index");
473 return ret;
474 }
475 return ret;
476 }
477
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)478 int DataShareProxy::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
479 {
480 MessageParcel data;
481 data.SetMaxCapacity(MTU_SIZE);
482 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
483 LOG_ERROR("WriteInterfaceToken failed");
484 return -1;
485 }
486 if (!ITypesUtil::Marshal(data, statements)) {
487 LOG_ERROR("fail to Marshal");
488 return -1;
489 }
490
491 MessageParcel reply;
492 MessageOption option;
493 int32_t err = Remote()->SendRequest(
494 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_EXECUTE_BATCH), data, reply, option);
495 if (err != E_OK) {
496 LOG_ERROR("fail to SendRequest. err: %{public}d", err);
497 return -1;
498 }
499 if (!ITypesUtil::Unmarshal(reply, result)) {
500 LOG_ERROR("fail to Unmarshal result");
501 return -1;
502 }
503 return 0;
504 }
505
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)506 bool DataShareProxy::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
507 {
508 MessageParcel data;
509 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
510 LOG_ERROR("WriteInterfaceToken failed");
511 return false;
512 }
513
514 if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
515 LOG_ERROR("fail to Marshalling");
516 return false;
517 }
518
519 MessageParcel reply;
520 MessageOption option;
521 int32_t result = Remote()->SendRequest(
522 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_REGISTER_OBSERVER), data, reply, option);
523 if (result != ERR_NONE) {
524 LOG_ERROR("SendRequest error, result=%{public}d", result);
525 return false;
526 }
527 return true;
528 }
529
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)530 bool DataShareProxy::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
531 {
532 MessageParcel data;
533 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
534 LOG_ERROR("WriteInterfaceToken failed");
535 return false;
536 }
537 if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
538 LOG_ERROR("fail to Marshalling");
539 return false;
540 }
541
542 MessageParcel reply;
543 MessageOption option;
544 int32_t result = Remote()->SendRequest(
545 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UNREGISTER_OBSERVER), data, reply, option);
546 if (result != ERR_NONE) {
547 LOG_ERROR("SendRequest error, result=%{public}d", result);
548 return false;
549 }
550 return true;
551 }
552
NotifyChange(const Uri & uri)553 bool DataShareProxy::NotifyChange(const Uri &uri)
554 {
555 MessageParcel data;
556 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
557 LOG_ERROR("WriteInterfaceToken failed");
558 return false;
559 }
560 if (!ITypesUtil::Marshal(data, uri)) {
561 LOG_ERROR("fail to Marshalling");
562 return false;
563 }
564
565 MessageParcel reply;
566 MessageOption option;
567 int32_t result = Remote()->SendRequest(
568 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NOTIFY_CHANGE), data, reply, option);
569 if (result != ERR_NONE) {
570 LOG_ERROR("SendRequest error, result=%{public}d", result);
571 return false;
572 }
573 return true;
574 }
575
NormalizeUri(const Uri & uri)576 Uri DataShareProxy::NormalizeUri(const Uri &uri)
577 {
578 MessageParcel data;
579 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
580 LOG_ERROR("WriteInterfaceToken failed");
581 return Uri("");
582 }
583 if (!ITypesUtil::Marshal(data, uri)) {
584 LOG_ERROR("fail to Marshalling");
585 return Uri("");
586 }
587
588 MessageParcel reply;
589 MessageOption option;
590 int32_t err = Remote()->SendRequest(
591 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NORMALIZE_URI), data, reply, option);
592 if (err != E_OK) {
593 LOG_ERROR("NormalizeUri fail to SendRequest. err: %{public}d", err);
594 return Uri("");
595 }
596 Uri info("");
597 if (!ITypesUtil::Unmarshal(reply, info)) {
598 LOG_ERROR("fail to Unmarshal index");
599 return Uri("");
600 }
601 return info;
602 }
603
DenormalizeUri(const Uri & uri)604 Uri DataShareProxy::DenormalizeUri(const Uri &uri)
605 {
606 MessageParcel data;
607 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
608 LOG_ERROR("WriteInterfaceToken failed");
609 return Uri("");
610 }
611
612 if (!ITypesUtil::Marshal(data, uri)) {
613 LOG_ERROR("fail to Marshalling");
614 return Uri("");
615 }
616
617 MessageParcel reply;
618 MessageOption option;
619 int32_t err = Remote()->SendRequest(
620 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DENORMALIZE_URI), data, reply, option);
621 if (err != E_OK) {
622 LOG_ERROR("DenormalizeUri fail to SendRequest. err: %{public}d", err);
623 return Uri("");
624 }
625
626 Uri info("");
627 if (!ITypesUtil::Unmarshal(reply, info)) {
628 LOG_ERROR("fail to Unmarshal index");
629 return Uri("");
630 }
631 return info;
632 }
633
CheckSize(const UpdateOperations & operations)634 bool DataShareProxy::CheckSize(const UpdateOperations &operations)
635 {
636 size_t size = 0;
637 for (const auto &it : operations) {
638 size += it.second.size();
639 }
640 if (size > MAX_SIZE) {
641 LOG_ERROR("operations size greater than limit");
642 return false;
643 }
644 return true;
645 }
UserDefineFunc(MessageParcel & data,MessageParcel & reply,MessageOption & option)646 int32_t DataShareProxy::UserDefineFunc(
647 MessageParcel &data, MessageParcel &reply, MessageOption &option)
648 {
649 int32_t errCode = -1;
650 int32_t err = Remote()->SendRequest(
651 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_USER_DEFINE_FUNC), data, reply, option);
652 if (err != E_OK) {
653 LOG_ERROR("UserDefineFunc fail to SendRequest. err: %{public}d", err);
654 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : errCode;
655 }
656 return err;
657 }
658 } // namespace DataShare
659 } // namespace OHOS
660