• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
OpenFileInner(const Uri & uri,const std::string & mode,uint32_t requestCode,int32_t & errCode)70 int DataShareProxy::OpenFileInner(const Uri &uri, const std::string &mode, uint32_t requestCode, int32_t &errCode)
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(requestCode, data, reply, option);
92     if (err != E_OK) {
93         LOG_ERROR("OpenFile fail to SendRequest. err: %{public}d", err);
94         errCode = 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 
OpenFile(const Uri & uri,const std::string & mode)107 int DataShareProxy::OpenFile(const Uri &uri, const std::string &mode)
108 {
109     int32_t errCode = 0;
110     uint32_t requestCode = static_cast<uint32_t>(IDataShareInterfaceCode::CMD_OPEN_FILE);
111     return OpenFileInner(uri, mode, requestCode, errCode);
112 }
113 
OpenFileWithErrCode(const Uri & uri,const std::string & mode,int32_t & errCode)114 int DataShareProxy::OpenFileWithErrCode(const Uri &uri, const std::string &mode, int32_t &errCode)
115 {
116     uint32_t requestCode = static_cast<uint32_t>(IDataShareInterfaceCode::CMD_OPEN_FILE_WITH_ERR_CODE);
117     return OpenFileInner(uri, mode, requestCode, errCode);
118 }
119 
OpenRawFile(const Uri & uri,const std::string & mode)120 int DataShareProxy::OpenRawFile(const Uri &uri, const std::string &mode)
121 {
122     int fd = -1;
123     MessageParcel data;
124     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
125         LOG_ERROR("WriteInterfaceToken failed");
126         return fd;
127     }
128 
129     if (!data.WriteParcelable(&uri)) {
130         LOG_ERROR("fail to WriteParcelable uri");
131         return fd;
132     }
133 
134     if (!data.WriteString(mode)) {
135         LOG_ERROR("fail to WriteString mode");
136         return fd;
137     }
138 
139     MessageParcel reply;
140     MessageOption option;
141     int32_t err = Remote()->SendRequest(
142         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_OPEN_RAW_FILE), data, reply, option);
143     if (err != E_OK) {
144         LOG_ERROR("OpenRawFile fail to SendRequest. err: %{public}d", err);
145         return fd;
146     }
147 
148     if (!reply.ReadInt32(fd)) {
149         LOG_ERROR("fail to ReadInt32 fd");
150         return fd;
151     }
152 
153     return fd;
154 }
155 
Insert(const Uri & uri,const DataShareValuesBucket & value)156 int DataShareProxy::Insert(const Uri &uri, const DataShareValuesBucket &value)
157 {
158     int index = -1;
159     MessageParcel data;
160     data.SetMaxCapacity(MTU_SIZE);
161     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
162         LOG_ERROR("WriteInterfaceToken failed");
163         return index;
164     }
165     if (!ITypesUtil::Marshal(data, uri, value)) {
166         LOG_ERROR("fail to Marshal value");
167         return index;
168     }
169     MessageParcel reply;
170     MessageOption option;
171     int32_t err = Remote()->SendRequest(
172         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT), data, reply, option);
173     if (err != E_OK) {
174         LOG_ERROR("Insert fail to SendRequest. err: %{public}d", err);
175         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
176     }
177     if (!ITypesUtil::Unmarshal(reply, index)) {
178         LOG_ERROR("fail to Unmarshal index");
179         return index;
180     }
181 
182     return index;
183 }
184 
InsertExt(const Uri & uri,const DataShareValuesBucket & value,std::string & result)185 int DataShareProxy::InsertExt(const Uri &uri, const DataShareValuesBucket &value, std::string &result)
186 {
187     int index = -1;
188     MessageParcel data;
189     data.SetMaxCapacity(MTU_SIZE);
190     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
191         LOG_ERROR("WriteInterfaceToken failed");
192         return index;
193     }
194     if (!ITypesUtil::Marshal(data, uri, value)) {
195         LOG_ERROR("fail to Marshal value");
196         return index;
197     }
198     MessageParcel reply;
199     MessageOption option;
200     int32_t err = Remote()->SendRequest(
201         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT_EXT), data, reply, option);
202     if (err != E_OK) {
203         LOG_ERROR("Insert fail to SendRequest. err: %{public}d", err);
204         return index;
205     }
206     if (!ITypesUtil::Unmarshal(reply, index, result)) {
207         LOG_ERROR("fail to Unmarshal index");
208         return index;
209     }
210     return index;
211 }
212 
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)213 int DataShareProxy::Update(const Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
214 {
215     int index = -1;
216     MessageParcel data;
217     data.SetMaxCapacity(MTU_SIZE);
218     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
219         LOG_ERROR("WriteInterfaceToken failed");
220         return index;
221     }
222     if (!ITypesUtil::Marshal(data, uri, predicates, value)) {
223         LOG_ERROR("fail to Marshal value");
224         return index;
225     }
226     MessageParcel reply;
227     MessageOption option;
228     int32_t err = Remote()->SendRequest(
229         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UPDATE), data, reply, option);
230     if (err != E_OK) {
231         LOG_ERROR("Update fail to SendRequest. err: %{public}d", err);
232         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
233     }
234     if (!ITypesUtil::Unmarshal(reply, index)) {
235         LOG_ERROR("fail to Unmarshal index");
236         return index;
237     }
238     return index;
239 }
240 
BatchUpdate(const UpdateOperations & operations,std::vector<BatchUpdateResult> & results)241 int DataShareProxy::BatchUpdate(const UpdateOperations &operations, std::vector<BatchUpdateResult> &results)
242 {
243     int ret = -1;
244     MessageParcel data;
245     data.SetMaxCapacity(MTU_SIZE);
246     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
247         LOG_ERROR("WriteInterfaceToken failed");
248         return ret;
249     }
250     if (!CheckSize(operations)) {
251         return ret;
252     }
253     if (!ITypesUtil::Marshal(data, operations)) {
254         LOG_ERROR("fail to Marshalling");
255         return ret;
256     }
257     MessageParcel reply;
258     MessageOption option;
259     int32_t err = Remote()->SendRequest(
260         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_BATCH_UPDATE), data, reply, option);
261     if (err != E_OK) {
262         LOG_ERROR("BatchUpdate fail to SendRequest. err: %{public}d", err);
263         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : ret;
264     }
265     if (!ITypesUtil::Unmarshal(reply, results)) {
266         LOG_ERROR("fail to Unmarshal result");
267         return ret;
268     }
269     return 0;
270 }
271 
Delete(const Uri & uri,const DataSharePredicates & predicates)272 int DataShareProxy::Delete(const Uri &uri, const DataSharePredicates &predicates)
273 {
274     int index = -1;
275     MessageParcel data;
276     data.SetMaxCapacity(MTU_SIZE);
277     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
278         LOG_ERROR("WriteInterfaceToken failed");
279         return index;
280     }
281     if (!ITypesUtil::Marshal(data, uri, predicates)) {
282         LOG_ERROR("fail to Marshalling predicates");
283         return index;
284     }
285 
286     MessageParcel reply;
287     MessageOption option;
288     int32_t err = Remote()->SendRequest(
289         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DELETE), data, reply, option);
290     if (err != E_OK) {
291         LOG_ERROR("Delete fail to SendRequest. err: %{public}d", err);
292         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
293     }
294     if (!ITypesUtil::Unmarshal(reply, index)) {
295         LOG_ERROR("fail to Unmarshal index");
296         return index;
297     }
298     return index;
299 }
300 
InsertEx(const Uri & uri,const DataShareValuesBucket & value)301 std::pair<int32_t, int32_t> DataShareProxy::InsertEx(const Uri &uri, const DataShareValuesBucket &value)
302 {
303     MessageParcel data;
304     data.SetMaxCapacity(MTU_SIZE);
305     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
306         LOG_ERROR("WriteInterfaceToken failed");
307         return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
308     }
309     if (!ITypesUtil::Marshal(data, uri, value)) {
310         LOG_ERROR("fail to Marshal value");
311         return std::make_pair(E_MARSHAL_ERROR, 0);
312     }
313 
314     int32_t errCode = -1;
315     int32_t result = -1;
316     MessageParcel reply;
317     MessageOption option;
318     int32_t err = Remote()->SendRequest(
319         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT_EX), data, reply, option);
320     if (err != E_OK) {
321         LOG_ERROR("InsertEx fail to SendRequest. err: %{public}d", err);
322         return std::make_pair((err == PERMISSION_ERR ? PERMISSION_ERR_CODE : errCode), 0);
323     }
324 
325     if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
326         LOG_ERROR("fail to Unmarshal");
327         return std::make_pair(E_UNMARSHAL_ERROR, 0);
328     }
329     return std::make_pair(errCode, result);
330 }
331 
UpdateEx(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)332 std::pair<int32_t, int32_t> DataShareProxy::UpdateEx(const Uri &uri, const DataSharePredicates &predicates,
333     const DataShareValuesBucket &value)
334 {
335     MessageParcel data;
336     data.SetMaxCapacity(MTU_SIZE);
337     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
338         LOG_ERROR("WriteInterfaceToken failed");
339         return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
340     }
341     if (!ITypesUtil::Marshal(data, uri, predicates, value)) {
342         LOG_ERROR("fail to Marshal value");
343         return std::make_pair(E_MARSHAL_ERROR, 0);
344     }
345 
346     int32_t errCode = -1;
347     int32_t result = -1;
348     MessageParcel reply;
349     MessageOption option;
350     int32_t err = Remote()->SendRequest(
351         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UPDATE_EX), data, reply, option);
352     if (err != E_OK) {
353         LOG_ERROR("UpdateEx fail to SendRequest. err: %{public}d", err);
354         return std::make_pair((err == PERMISSION_ERR ? PERMISSION_ERR_CODE : errCode), 0);
355     }
356 
357     if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
358         LOG_ERROR("fail to Unmarshal");
359         return std::make_pair(E_UNMARSHAL_ERROR, 0);
360     }
361     return std::make_pair(errCode, result);
362 }
363 
DeleteEx(const Uri & uri,const DataSharePredicates & predicates)364 std::pair<int32_t, int32_t> DataShareProxy::DeleteEx(const Uri &uri, const DataSharePredicates &predicates)
365 {
366     MessageParcel data;
367     data.SetMaxCapacity(MTU_SIZE);
368     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
369         LOG_ERROR("WriteInterfaceToken failed");
370         return std::make_pair(E_WRITE_TO_PARCE_ERROR, 0);
371     }
372     if (!ITypesUtil::Marshal(data, uri, predicates)) {
373         LOG_ERROR("fail to Marshalling predicates");
374         return std::make_pair(E_MARSHAL_ERROR, 0);
375     }
376 
377     int32_t errCode = -1;
378     int32_t result = -1;
379     MessageParcel reply;
380     MessageOption option;
381     int32_t err = Remote()->SendRequest(
382         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DELETE_EX), data, reply, option);
383     if (err != E_OK) {
384         LOG_ERROR("DeleteEx fail to SendRequest. err: %{public}d", err);
385         return std::make_pair((err == PERMISSION_ERR ? PERMISSION_ERR_CODE : errCode), 0);
386     }
387 
388     if (!ITypesUtil::Unmarshal(reply, errCode, result)) {
389         LOG_ERROR("fail to Unmarshal");
390         return std::make_pair(E_UNMARSHAL_ERROR, 0);
391     }
392     return std::make_pair(errCode, result);
393 }
394 
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError)395 std::shared_ptr<DataShareResultSet> DataShareProxy::Query(const Uri &uri, const DataSharePredicates &predicates,
396     std::vector<std::string> &columns, DatashareBusinessError &businessError)
397 {
398     MessageParcel data;
399     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
400         LOG_ERROR("WriteInterfaceToken failed");
401         businessError.SetCode(E_WRITE_TO_PARCE_ERROR);
402         return nullptr;
403     }
404     if (!ITypesUtil::Marshal(data, uri, columns)) {
405         LOG_ERROR("Marshalling uri and columns to data failed");
406         businessError.SetCode(E_MARSHAL_ERROR);
407         return nullptr;
408     }
409     if (!ITypesUtil::MarshalPredicates(predicates, data)) {
410         LOG_ERROR("Marshalling predicates to shared-memory failed");
411         businessError.SetCode(E_MARSHAL_ERROR);
412         return nullptr;
413     }
414     MessageParcel reply;
415     MessageOption option;
416     int32_t err = Remote()->SendRequest(
417         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_QUERY), data, reply, option);
418     auto result = ISharedResultSet::ReadFromParcel(reply);
419     if (err != E_OK) {
420         LOG_ERROR("Query fail to SendRequest. err: %{public}d", err);
421         businessError.SetCode(err);
422         return nullptr;
423     }
424     businessError.SetCode(reply.ReadInt32());
425     businessError.SetMessage(reply.ReadString());
426     return result;
427 }
428 
GetType(const Uri & uri)429 std::string DataShareProxy::GetType(const Uri &uri)
430 {
431     std::string type;
432     MessageParcel data;
433     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
434         LOG_ERROR("WriteInterfaceToken failed");
435         return type;
436     }
437 
438     if (!ITypesUtil::Marshal(data, uri)) {
439         LOG_ERROR("fail to Marshal value");
440         return type;
441     }
442 
443     MessageParcel reply;
444     MessageOption option;
445     int32_t err = Remote()->SendRequest(
446         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_GET_TYPE), data, reply, option);
447     if (err != E_OK) {
448         LOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
449         return type;
450     }
451     if (!ITypesUtil::Unmarshal(reply, type)) {
452         LOG_ERROR("fail to Unmarshal index");
453         return type;
454     }
455     if (type.empty()) {
456         LOG_ERROR("fail to ReadString type");
457         return type;
458     }
459 
460     return type;
461 }
462 
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)463 int DataShareProxy::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
464 {
465     int ret = -1;
466     MessageParcel data;
467     data.SetMaxCapacity(MTU_SIZE);
468     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
469         LOG_ERROR("WriteInterfaceToken failed");
470         return ret;
471     }
472     if (!ITypesUtil::Marshal(data, uri)) {
473         LOG_ERROR("Marshalling uri to data failed");
474         return ret;
475     }
476     if (!ITypesUtil::MarshalValuesBucketVec(values, data)) {
477         LOG_ERROR("Marshalling DataShareValuesBucket to shared-memory failed");
478         return ret;
479     }
480     MessageParcel reply;
481     MessageOption option;
482     int32_t err = Remote()->SendRequest(
483         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_BATCH_INSERT), data, reply, option);
484     if (err != E_OK) {
485         LOG_ERROR("fail to SendRequest. err: %{public}d", err);
486         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : ret;
487     }
488     if (!ITypesUtil::Unmarshal(reply, ret)) {
489         LOG_ERROR("fail to Unmarshal index");
490         return ret;
491     }
492     return ret;
493 }
494 
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)495 int DataShareProxy::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
496 {
497     MessageParcel data;
498     data.SetMaxCapacity(MTU_SIZE);
499     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
500         LOG_ERROR("WriteInterfaceToken failed");
501         return -1;
502     }
503     if (!ITypesUtil::Marshal(data, statements)) {
504         LOG_ERROR("fail to Marshal");
505         return -1;
506     }
507 
508     MessageParcel reply;
509     MessageOption option;
510     int32_t err = Remote()->SendRequest(
511         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_EXECUTE_BATCH), data, reply, option);
512     if (err != E_OK) {
513         LOG_ERROR("fail to SendRequest. err: %{public}d", err);
514         return -1;
515     }
516     if (!ITypesUtil::Unmarshal(reply, result)) {
517         LOG_ERROR("fail to Unmarshal result");
518         return -1;
519     }
520     return 0;
521 }
522 
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)523 bool DataShareProxy::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
524 {
525     MessageParcel data;
526     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
527         LOG_ERROR("WriteInterfaceToken failed");
528         return false;
529     }
530 
531     if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
532         LOG_ERROR("fail to Marshalling");
533         return false;
534     }
535 
536     MessageParcel reply;
537     MessageOption option;
538     int32_t result = Remote()->SendRequest(
539         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_REGISTER_OBSERVER), data, reply, option);
540     if (result != ERR_NONE) {
541         LOG_ERROR("SendRequest error, result=%{public}d", result);
542         return false;
543     }
544     // the stub write bool value as int value to reply, 0 is false
545     return reply.ReadInt32() == 0 ? false : true;
546 }
547 
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)548 bool DataShareProxy::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
549 {
550     MessageParcel data;
551     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
552         LOG_ERROR("WriteInterfaceToken failed");
553         return false;
554     }
555     if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
556         LOG_ERROR("fail to Marshalling");
557         return false;
558     }
559 
560     MessageParcel reply;
561     MessageOption option;
562     int32_t result = Remote()->SendRequest(
563         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UNREGISTER_OBSERVER), data, reply, option);
564     if (result != ERR_NONE) {
565         LOG_ERROR("SendRequest error, result=%{public}d", result);
566         return false;
567     }
568     // the stub write bool value as int value to reply, 0 is false
569     return reply.ReadInt32() == 0 ? false : true;
570 }
571 
NotifyChange(const Uri & uri)572 bool DataShareProxy::NotifyChange(const Uri &uri)
573 {
574     MessageParcel data;
575     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
576         LOG_ERROR("WriteInterfaceToken failed");
577         return false;
578     }
579     if (!ITypesUtil::Marshal(data, uri)) {
580         LOG_ERROR("fail to Marshalling");
581         return false;
582     }
583 
584     MessageParcel reply;
585     MessageOption option;
586     int32_t result = Remote()->SendRequest(
587         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NOTIFY_CHANGE), data, reply, option);
588     if (result != ERR_NONE) {
589         LOG_ERROR("SendRequest error, result=%{public}d", result);
590         return false;
591     }
592     return true;
593 }
594 
595 // send IPC request, and there is no default implemention for the provider. It needs to be handled by the user.
RegisterObserverExtProvider(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver,bool isDescendants,RegisterOption registerOption)596 int DataShareProxy::RegisterObserverExtProvider(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver,
597     bool isDescendants, RegisterOption registerOption)
598 {
599     MessageParcel data;
600     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
601         LOG_ERROR("WriteInterfaceToken failed");
602         return E_WRITE_TO_PARCE_ERROR;
603     }
604 
605     if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject(), isDescendants, registerOption)) {
606         LOG_ERROR("fail to Marshalling");
607         return E_MARSHAL_ERROR;
608     }
609 
610     MessageParcel reply;
611     MessageOption option;
612     int32_t result = Remote()->SendRequest(
613         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_REGISTER_OBSERVEREXT_PROVIDER), data, reply, option);
614     if (result != ERR_NONE) {
615         LOG_ERROR("SendRequest error, result=%{public}d", result);
616         return DATA_SHARE_ERROR;
617     }
618     // the stub write int value to reply
619     return reply.ReadInt32();
620 }
621 
622 // send IPC request, and there is no default implemention for the provider. It needs to be handled by the user.
UnregisterObserverExtProvider(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)623 int DataShareProxy::UnregisterObserverExtProvider(const Uri &uri,
624     const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
625 {
626     MessageParcel data;
627     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
628         LOG_ERROR("WriteInterfaceToken failed");
629         return E_WRITE_TO_PARCE_ERROR;
630     }
631 
632     if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
633         LOG_ERROR("fail to Marshalling");
634         return E_MARSHAL_ERROR;
635     }
636 
637     MessageParcel reply;
638     MessageOption option;
639     int32_t result = Remote()->SendRequest(
640         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UNREGISTER_OBSERVEREXT_PROVIDER), data, reply, option);
641     if (result != ERR_NONE) {
642         LOG_ERROR("SendRequest error, result=%{public}d", result);
643         return DATA_SHARE_ERROR;
644     }
645     // the stub writeint value to reply
646     return reply.ReadInt32();
647 }
648 
649 // send IPC request, and there is no default implemention for the provider. It needs to be handled by the user.
NotifyChangeExtProvider(const ChangeInfo & changeInfo)650 int DataShareProxy::NotifyChangeExtProvider(const ChangeInfo &changeInfo)
651 {
652     MessageParcel data;
653     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
654         LOG_ERROR("WriteInterfaceToken failed");
655         return E_WRITE_TO_PARCE_ERROR;
656     }
657     if (!ChangeInfo::Marshalling(changeInfo, data)) {
658         LOG_ERROR("changeInfo marshalling failed, changeType:%{public}ud, num:%{public}zu,"
659             "null data:%{public}d, size:%{public}ud",
660             changeInfo.changeType_, changeInfo.uris_.size(), changeInfo.data_ == nullptr, changeInfo.size_);
661         return E_MARSHAL_ERROR;
662     }
663 
664     MessageParcel reply;
665     MessageOption option;
666     int32_t result = Remote()->SendRequest(
667         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NOTIFY_CHANGEEXT_PROVIDER), data, reply, option);
668     if (result != ERR_NONE) {
669         LOG_ERROR("SendRequest error, result=%{public}d", result);
670         return DATA_SHARE_ERROR;
671     }
672     // the stub write int value to reply
673     return reply.ReadInt32();
674 }
675 
NormalizeUri(const Uri & uri)676 Uri DataShareProxy::NormalizeUri(const Uri &uri)
677 {
678     MessageParcel data;
679     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
680         LOG_ERROR("WriteInterfaceToken failed");
681         return Uri("");
682     }
683     if (!ITypesUtil::Marshal(data, uri)) {
684         LOG_ERROR("fail to Marshalling");
685         return Uri("");
686     }
687 
688     MessageParcel reply;
689     MessageOption option;
690     int32_t err = Remote()->SendRequest(
691         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NORMALIZE_URI), data, reply, option);
692     if (err != E_OK) {
693         LOG_ERROR("NormalizeUri fail to SendRequest. err: %{public}d", err);
694         return Uri("");
695     }
696     Uri info("");
697     if (!ITypesUtil::Unmarshal(reply, info)) {
698         LOG_ERROR("fail to Unmarshal index");
699         return Uri("");
700     }
701     return info;
702 }
703 
DenormalizeUri(const Uri & uri)704 Uri DataShareProxy::DenormalizeUri(const Uri &uri)
705 {
706     MessageParcel data;
707     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
708         LOG_ERROR("WriteInterfaceToken failed");
709         return Uri("");
710     }
711 
712     if (!ITypesUtil::Marshal(data, uri)) {
713         LOG_ERROR("fail to Marshalling");
714         return Uri("");
715     }
716 
717     MessageParcel reply;
718     MessageOption option;
719     int32_t err = Remote()->SendRequest(
720         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DENORMALIZE_URI), data, reply, option);
721     if (err != E_OK) {
722         LOG_ERROR("DenormalizeUri fail to SendRequest. err: %{public}d", err);
723         return Uri("");
724     }
725 
726     Uri info("");
727     if (!ITypesUtil::Unmarshal(reply, info)) {
728         LOG_ERROR("fail to Unmarshal index");
729         return Uri("");
730     }
731     return info;
732 }
733 
CheckSize(const UpdateOperations & operations)734 bool DataShareProxy::CheckSize(const UpdateOperations &operations)
735 {
736     size_t size = 0;
737     for (const auto &it : operations) {
738         size += it.second.size();
739     }
740     if (size > MAX_SIZE) {
741         LOG_ERROR("operations size greater than limit");
742         return false;
743     }
744     return true;
745 }
746 
UserDefineFunc(MessageParcel & data,MessageParcel & reply,MessageOption & option)747 int32_t DataShareProxy::UserDefineFunc(
748     MessageParcel &data, MessageParcel &reply, MessageOption &option)
749 {
750     int32_t err = Remote()->SendRequest(
751         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_USER_DEFINE_FUNC), data, reply, option);
752     if (err != E_OK) {
753         LOG_ERROR("UserDefineFunc SendRequest fails. err: %{public}d", err);
754     }
755     return err;
756 }
757 } // namespace DataShare
758 } // namespace OHOS
759