• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #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 != DATA_SHARE_NO_ERROR) {
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 != DATA_SHARE_NO_ERROR) {
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 != DATA_SHARE_NO_ERROR) {
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     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
148         LOG_ERROR("WriteInterfaceToken failed");
149         return index;
150     }
151     if (!ITypesUtil::Marshal(data, uri, value)) {
152         LOG_ERROR("fail to Marshal value");
153         return index;
154     }
155     MessageParcel reply;
156     MessageOption option;
157     int32_t err = Remote()->SendRequest(
158         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT), data, reply, option);
159     if (err != DATA_SHARE_NO_ERROR) {
160         LOG_ERROR("Insert fail to SendRequest. err: %{public}d", err);
161         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
162     }
163     if (!ITypesUtil::Unmarshal(reply, index)) {
164         LOG_ERROR("fail to Unmarshal index");
165         return index;
166     }
167 
168     return index;
169 }
170 
InsertExt(const Uri & uri,const DataShareValuesBucket & value,std::string & result)171 int DataShareProxy::InsertExt(const Uri &uri, const DataShareValuesBucket &value, std::string &result)
172 {
173     int index = -1;
174     MessageParcel data;
175     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
176         LOG_ERROR("WriteInterfaceToken failed");
177         return index;
178     }
179     if (!ITypesUtil::Marshal(data, uri, value)) {
180         LOG_ERROR("fail to Marshal value");
181         return index;
182     }
183     MessageParcel reply;
184     MessageOption option;
185     int32_t err = Remote()->SendRequest(
186         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_INSERT_EXT), data, reply, option);
187     if (err != DATA_SHARE_NO_ERROR) {
188         LOG_ERROR("Insert fail to SendRequest. err: %{public}d", err);
189         return index;
190     }
191     if (!ITypesUtil::Unmarshal(reply, index, result)) {
192         LOG_ERROR("fail to Unmarshal index");
193         return index;
194     }
195     return index;
196 }
197 
Update(const Uri & uri,const DataSharePredicates & predicates,const DataShareValuesBucket & value)198 int DataShareProxy::Update(const Uri &uri, const DataSharePredicates &predicates, const DataShareValuesBucket &value)
199 {
200     int index = -1;
201     MessageParcel data;
202     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
203         LOG_ERROR("WriteInterfaceToken failed");
204         return index;
205     }
206     if (!ITypesUtil::Marshal(data, uri, predicates, value)) {
207         LOG_ERROR("fail to Marshal value");
208         return index;
209     }
210     MessageParcel reply;
211     MessageOption option;
212     int32_t err = Remote()->SendRequest(
213         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UPDATE), data, reply, option);
214     if (err != DATA_SHARE_NO_ERROR) {
215         LOG_ERROR("Update fail to SendRequest. err: %{public}d", err);
216         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
217     }
218     if (!ITypesUtil::Unmarshal(reply, index)) {
219         LOG_ERROR("fail to Unmarshal index");
220         return index;
221     }
222     return index;
223 }
224 
Delete(const Uri & uri,const DataSharePredicates & predicates)225 int DataShareProxy::Delete(const Uri &uri, const DataSharePredicates &predicates)
226 {
227     int index = -1;
228     MessageParcel data;
229     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
230         LOG_ERROR("WriteInterfaceToken failed");
231         return index;
232     }
233     if (!ITypesUtil::Marshal(data, uri, predicates)) {
234         LOG_ERROR("fail to Marshalling predicates");
235         return index;
236     }
237 
238     MessageParcel reply;
239     MessageOption option;
240     int32_t err = Remote()->SendRequest(
241         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DELETE), data, reply, option);
242     if (err != DATA_SHARE_NO_ERROR) {
243         LOG_ERROR("Delete fail to SendRequest. err: %{public}d", err);
244         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
245     }
246     if (!ITypesUtil::Unmarshal(reply, index)) {
247         LOG_ERROR("fail to Unmarshal index");
248         return index;
249     }
250     return index;
251 }
252 
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError)253 std::shared_ptr<DataShareResultSet> DataShareProxy::Query(const Uri &uri, const DataSharePredicates &predicates,
254     std::vector<std::string> &columns, DatashareBusinessError &businessError)
255 {
256     MessageParcel data;
257     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
258         LOG_ERROR("WriteInterfaceToken failed");
259         return nullptr;
260     }
261     if (!ITypesUtil::Marshal(data, uri, predicates, columns)) {
262         LOG_ERROR("fail to Marshalling");
263         return nullptr;
264     }
265     MessageParcel reply;
266     MessageOption option;
267     int32_t err = Remote()->SendRequest(
268         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_QUERY), data, reply, option);
269     auto result = ISharedResultSet::ReadFromParcel(reply);
270     businessError.SetCode(reply.ReadInt32());
271     businessError.SetMessage(reply.ReadString());
272     if (err != DATA_SHARE_NO_ERROR) {
273         LOG_ERROR("Query fail to SendRequest. err: %{public}d", err);
274         return nullptr;
275     }
276     return result;
277 }
278 
GetType(const Uri & uri)279 std::string DataShareProxy::GetType(const Uri &uri)
280 {
281     LOG_INFO("begin.");
282     std::string type;
283     MessageParcel data;
284     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
285         LOG_ERROR("WriteInterfaceToken failed");
286         return type;
287     }
288 
289     if (!ITypesUtil::Marshal(data, uri)) {
290         LOG_ERROR("fail to Marshal value");
291         return type;
292     }
293 
294     MessageParcel reply;
295     MessageOption option;
296     int32_t err = Remote()->SendRequest(
297         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_GET_TYPE), data, reply, option);
298     if (err != DATA_SHARE_NO_ERROR) {
299         LOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
300         return type;
301     }
302     if (!ITypesUtil::Unmarshal(reply, type)) {
303         LOG_ERROR("fail to Unmarshal index");
304         return type;
305     }
306     if (type.empty()) {
307         LOG_ERROR("fail to ReadString type");
308         return type;
309     }
310 
311     LOG_INFO("end successfully.");
312     return type;
313 }
314 
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)315 int DataShareProxy::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
316 {
317     LOG_INFO("begin.");
318     int ret = -1;
319     MessageParcel data;
320     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
321         LOG_ERROR("WriteInterfaceToken failed");
322         return ret;
323     }
324     if (!ITypesUtil::Marshal(data, uri, values)) {
325         LOG_ERROR("fail to Marshalling");
326         return ret;
327     }
328 
329     MessageParcel reply;
330     MessageOption option;
331     int32_t err = Remote()->SendRequest(
332         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_BATCH_INSERT), data, reply, option);
333     if (err != DATA_SHARE_NO_ERROR) {
334         LOG_ERROR("fail to SendRequest. err: %{public}d", err);
335         return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : ret;
336     }
337     if (!ITypesUtil::Unmarshal(reply, ret)) {
338         LOG_ERROR("fail to Unmarshal index");
339         return ret;
340     }
341     LOG_INFO("end successfully.");
342     return ret;
343 }
344 
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)345 int DataShareProxy::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
346 {
347     MessageParcel data;
348     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
349         LOG_ERROR("WriteInterfaceToken failed");
350         return -1;
351     }
352     if (!ITypesUtil::Marshal(data, statements)) {
353         LOG_ERROR("fail to Marshal");
354         return -1;
355     }
356 
357     MessageParcel reply;
358     MessageOption option;
359     int32_t err = Remote()->SendRequest(
360         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_EXECUTE_BATCH), data, reply, option);
361     if (err != DATA_SHARE_NO_ERROR) {
362         LOG_ERROR("fail to SendRequest. err: %{public}d", err);
363         return -1;
364     }
365     if (!ITypesUtil::Unmarshal(reply, result)) {
366         LOG_ERROR("fail to Unmarshal result");
367         return -1;
368     }
369     return 0;
370 }
371 
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)372 bool DataShareProxy::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
373 {
374     LOG_INFO("begin.");
375     MessageParcel data;
376     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
377         LOG_ERROR("WriteInterfaceToken failed");
378         return false;
379     }
380 
381     if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
382         LOG_ERROR("fail to Marshalling");
383         return false;
384     }
385 
386     MessageParcel reply;
387     MessageOption option;
388     int32_t result = Remote()->SendRequest(
389         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_REGISTER_OBSERVER), data, reply, option);
390     if (result == ERR_NONE) {
391         LOG_INFO("SendRequest ok, retval is %{public}d", reply.ReadInt32());
392     } else {
393         LOG_ERROR("SendRequest error, result=%{public}d", result);
394         return false;
395     }
396     LOG_INFO("end.");
397     return true;
398 }
399 
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)400 bool DataShareProxy::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
401 {
402     LOG_INFO("begin.");
403     MessageParcel data;
404     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
405         LOG_ERROR("WriteInterfaceToken failed");
406         return false;
407     }
408     if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
409         LOG_ERROR("fail to Marshalling");
410         return false;
411     }
412 
413     MessageParcel reply;
414     MessageOption option;
415     int32_t result = Remote()->SendRequest(
416         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UNREGISTER_OBSERVER), data, reply, option);
417     if (result == ERR_NONE) {
418         LOG_INFO("SendRequest ok, retval is %{public}d", reply.ReadInt32());
419     } else {
420         LOG_ERROR("SendRequest error, result=%{public}d", result);
421         return false;
422     }
423     LOG_INFO("end successfully.");
424     return true;
425 }
426 
NotifyChange(const Uri & uri)427 bool DataShareProxy::NotifyChange(const Uri &uri)
428 {
429     LOG_INFO("begin.");
430     MessageParcel data;
431     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
432         LOG_ERROR("WriteInterfaceToken failed");
433         return false;
434     }
435     if (!ITypesUtil::Marshal(data, uri)) {
436         LOG_ERROR("fail to Marshalling");
437         return false;
438     }
439 
440     MessageParcel reply;
441     MessageOption option;
442     int32_t result = Remote()->SendRequest(
443         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NOTIFY_CHANGE), data, reply, option);
444     if (result == ERR_NONE) {
445         LOG_INFO("SendRequest ok, retval is %{public}d", reply.ReadInt32());
446     } else {
447         LOG_ERROR("SendRequest error, result=%{public}d", result);
448         return false;
449     }
450     LOG_INFO("end successfully.");
451     return true;
452 }
453 
NormalizeUri(const Uri & uri)454 Uri DataShareProxy::NormalizeUri(const Uri &uri)
455 {
456     LOG_INFO("begin.");
457     MessageParcel data;
458     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
459         LOG_ERROR("WriteInterfaceToken failed");
460         return Uri("");
461     }
462     if (!ITypesUtil::Marshal(data, uri)) {
463         LOG_ERROR("fail to Marshalling");
464         return Uri("");
465     }
466 
467     MessageParcel reply;
468     MessageOption option;
469     int32_t err = Remote()->SendRequest(
470         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NORMALIZE_URI), data, reply, option);
471     if (err != DATA_SHARE_NO_ERROR) {
472         LOG_ERROR("NormalizeUri fail to SendRequest. err: %{public}d", err);
473         return Uri("");
474     }
475     Uri info("");
476     if (!ITypesUtil::Unmarshal(reply, info)) {
477         LOG_ERROR("fail to Unmarshal index");
478         return Uri("");
479     }
480     LOG_INFO("end successfully.");
481     return info;
482 }
483 
DenormalizeUri(const Uri & uri)484 Uri DataShareProxy::DenormalizeUri(const Uri &uri)
485 {
486     LOG_INFO("begin.");
487     MessageParcel data;
488     if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
489         LOG_ERROR("WriteInterfaceToken failed");
490         return Uri("");
491     }
492 
493     if (!ITypesUtil::Marshal(data, uri)) {
494         LOG_ERROR("fail to Marshalling");
495         return Uri("");
496     }
497 
498     MessageParcel reply;
499     MessageOption option;
500     int32_t err = Remote()->SendRequest(
501         static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DENORMALIZE_URI), data, reply, option);
502     if (err != DATA_SHARE_NO_ERROR) {
503         LOG_ERROR("DenormalizeUri fail to SendRequest. err: %{public}d", err);
504         return Uri("");
505     }
506 
507     Uri info("");
508     if (!ITypesUtil::Unmarshal(reply, info)) {
509         LOG_ERROR("fail to Unmarshal index");
510         return Uri("");
511     }
512     LOG_INFO("end successfully.");
513     return info;
514 }
515 } // namespace DataShare
516 } // namespace OHOS
517