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 != 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 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 != DATA_SHARE_NO_ERROR) {
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 != DATA_SHARE_NO_ERROR) {
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 != DATA_SHARE_NO_ERROR) {
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
Delete(const Uri & uri,const DataSharePredicates & predicates)228 int DataShareProxy::Delete(const Uri &uri, const DataSharePredicates &predicates)
229 {
230 int index = -1;
231 MessageParcel data;
232 data.SetMaxCapacity(MTU_SIZE);
233 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
234 LOG_ERROR("WriteInterfaceToken failed");
235 return index;
236 }
237 if (!ITypesUtil::Marshal(data, uri, predicates)) {
238 LOG_ERROR("fail to Marshalling predicates");
239 return index;
240 }
241
242 MessageParcel reply;
243 MessageOption option;
244 int32_t err = Remote()->SendRequest(
245 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DELETE), data, reply, option);
246 if (err != DATA_SHARE_NO_ERROR) {
247 LOG_ERROR("Delete fail to SendRequest. err: %{public}d", err);
248 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : index;
249 }
250 if (!ITypesUtil::Unmarshal(reply, index)) {
251 LOG_ERROR("fail to Unmarshal index");
252 return index;
253 }
254 return index;
255 }
256
Query(const Uri & uri,const DataSharePredicates & predicates,std::vector<std::string> & columns,DatashareBusinessError & businessError)257 std::shared_ptr<DataShareResultSet> DataShareProxy::Query(const Uri &uri, const DataSharePredicates &predicates,
258 std::vector<std::string> &columns, DatashareBusinessError &businessError)
259 {
260 MessageParcel data;
261 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
262 LOG_ERROR("WriteInterfaceToken failed");
263 return nullptr;
264 }
265 if (!ITypesUtil::Marshal(data, uri, predicates, columns)) {
266 LOG_ERROR("fail to Marshalling");
267 return nullptr;
268 }
269 MessageParcel reply;
270 MessageOption option;
271 int32_t err = Remote()->SendRequest(
272 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_QUERY), data, reply, option);
273 auto result = ISharedResultSet::ReadFromParcel(reply);
274 businessError.SetCode(reply.ReadInt32());
275 businessError.SetMessage(reply.ReadString());
276 if (err != DATA_SHARE_NO_ERROR) {
277 LOG_ERROR("Query fail to SendRequest. err: %{public}d", err);
278 return nullptr;
279 }
280 return result;
281 }
282
GetType(const Uri & uri)283 std::string DataShareProxy::GetType(const Uri &uri)
284 {
285 std::string type;
286 MessageParcel data;
287 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
288 LOG_ERROR("WriteInterfaceToken failed");
289 return type;
290 }
291
292 if (!ITypesUtil::Marshal(data, uri)) {
293 LOG_ERROR("fail to Marshal value");
294 return type;
295 }
296
297 MessageParcel reply;
298 MessageOption option;
299 int32_t err = Remote()->SendRequest(
300 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_GET_TYPE), data, reply, option);
301 if (err != DATA_SHARE_NO_ERROR) {
302 LOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
303 return type;
304 }
305 if (!ITypesUtil::Unmarshal(reply, type)) {
306 LOG_ERROR("fail to Unmarshal index");
307 return type;
308 }
309 if (type.empty()) {
310 LOG_ERROR("fail to ReadString type");
311 return type;
312 }
313
314 return type;
315 }
316
BatchInsert(const Uri & uri,const std::vector<DataShareValuesBucket> & values)317 int DataShareProxy::BatchInsert(const Uri &uri, const std::vector<DataShareValuesBucket> &values)
318 {
319 int ret = -1;
320 MessageParcel data;
321 data.SetMaxCapacity(MTU_SIZE);
322 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
323 LOG_ERROR("WriteInterfaceToken failed");
324 return ret;
325 }
326 if (!ITypesUtil::Marshal(data, uri, values)) {
327 LOG_ERROR("fail to Marshalling");
328 return ret;
329 }
330
331 MessageParcel reply;
332 MessageOption option;
333 int32_t err = Remote()->SendRequest(
334 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_BATCH_INSERT), data, reply, option);
335 if (err != DATA_SHARE_NO_ERROR) {
336 LOG_ERROR("fail to SendRequest. err: %{public}d", err);
337 return err == PERMISSION_ERR ? PERMISSION_ERR_CODE : ret;
338 }
339 if (!ITypesUtil::Unmarshal(reply, ret)) {
340 LOG_ERROR("fail to Unmarshal index");
341 return ret;
342 }
343 return ret;
344 }
345
ExecuteBatch(const std::vector<OperationStatement> & statements,ExecResultSet & result)346 int DataShareProxy::ExecuteBatch(const std::vector<OperationStatement> &statements, ExecResultSet &result)
347 {
348 MessageParcel data;
349 data.SetMaxCapacity(MTU_SIZE);
350 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
351 LOG_ERROR("WriteInterfaceToken failed");
352 return -1;
353 }
354 if (!ITypesUtil::Marshal(data, statements)) {
355 LOG_ERROR("fail to Marshal");
356 return -1;
357 }
358
359 MessageParcel reply;
360 MessageOption option;
361 int32_t err = Remote()->SendRequest(
362 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_EXECUTE_BATCH), data, reply, option);
363 if (err != DATA_SHARE_NO_ERROR) {
364 LOG_ERROR("fail to SendRequest. err: %{public}d", err);
365 return -1;
366 }
367 if (!ITypesUtil::Unmarshal(reply, result)) {
368 LOG_ERROR("fail to Unmarshal result");
369 return -1;
370 }
371 return 0;
372 }
373
RegisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)374 bool DataShareProxy::RegisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
375 {
376 MessageParcel data;
377 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
378 LOG_ERROR("WriteInterfaceToken failed");
379 return false;
380 }
381
382 if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
383 LOG_ERROR("fail to Marshalling");
384 return false;
385 }
386
387 MessageParcel reply;
388 MessageOption option;
389 int32_t result = Remote()->SendRequest(
390 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_REGISTER_OBSERVER), data, reply, option);
391 if (result != ERR_NONE) {
392 LOG_ERROR("SendRequest error, result=%{public}d", result);
393 return false;
394 }
395 return true;
396 }
397
UnregisterObserver(const Uri & uri,const sptr<AAFwk::IDataAbilityObserver> & dataObserver)398 bool DataShareProxy::UnregisterObserver(const Uri &uri, const sptr<AAFwk::IDataAbilityObserver> &dataObserver)
399 {
400 MessageParcel data;
401 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
402 LOG_ERROR("WriteInterfaceToken failed");
403 return false;
404 }
405 if (!ITypesUtil::Marshal(data, uri, dataObserver->AsObject())) {
406 LOG_ERROR("fail to Marshalling");
407 return false;
408 }
409
410 MessageParcel reply;
411 MessageOption option;
412 int32_t result = Remote()->SendRequest(
413 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_UNREGISTER_OBSERVER), data, reply, option);
414 if (result != ERR_NONE) {
415 LOG_ERROR("SendRequest error, result=%{public}d", result);
416 return false;
417 }
418 return true;
419 }
420
NotifyChange(const Uri & uri)421 bool DataShareProxy::NotifyChange(const Uri &uri)
422 {
423 MessageParcel data;
424 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
425 LOG_ERROR("WriteInterfaceToken failed");
426 return false;
427 }
428 if (!ITypesUtil::Marshal(data, uri)) {
429 LOG_ERROR("fail to Marshalling");
430 return false;
431 }
432
433 MessageParcel reply;
434 MessageOption option;
435 int32_t result = Remote()->SendRequest(
436 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NOTIFY_CHANGE), data, reply, option);
437 if (result != ERR_NONE) {
438 LOG_ERROR("SendRequest error, result=%{public}d", result);
439 return false;
440 }
441 return true;
442 }
443
NormalizeUri(const Uri & uri)444 Uri DataShareProxy::NormalizeUri(const Uri &uri)
445 {
446 MessageParcel data;
447 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
448 LOG_ERROR("WriteInterfaceToken failed");
449 return Uri("");
450 }
451 if (!ITypesUtil::Marshal(data, uri)) {
452 LOG_ERROR("fail to Marshalling");
453 return Uri("");
454 }
455
456 MessageParcel reply;
457 MessageOption option;
458 int32_t err = Remote()->SendRequest(
459 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_NORMALIZE_URI), data, reply, option);
460 if (err != DATA_SHARE_NO_ERROR) {
461 LOG_ERROR("NormalizeUri fail to SendRequest. err: %{public}d", err);
462 return Uri("");
463 }
464 Uri info("");
465 if (!ITypesUtil::Unmarshal(reply, info)) {
466 LOG_ERROR("fail to Unmarshal index");
467 return Uri("");
468 }
469 return info;
470 }
471
DenormalizeUri(const Uri & uri)472 Uri DataShareProxy::DenormalizeUri(const Uri &uri)
473 {
474 MessageParcel data;
475 if (!data.WriteInterfaceToken(DataShareProxy::GetDescriptor())) {
476 LOG_ERROR("WriteInterfaceToken failed");
477 return Uri("");
478 }
479
480 if (!ITypesUtil::Marshal(data, uri)) {
481 LOG_ERROR("fail to Marshalling");
482 return Uri("");
483 }
484
485 MessageParcel reply;
486 MessageOption option;
487 int32_t err = Remote()->SendRequest(
488 static_cast<uint32_t>(IDataShareInterfaceCode::CMD_DENORMALIZE_URI), data, reply, option);
489 if (err != DATA_SHARE_NO_ERROR) {
490 LOG_ERROR("DenormalizeUri fail to SendRequest. err: %{public}d", err);
491 return Uri("");
492 }
493
494 Uri info("");
495 if (!ITypesUtil::Unmarshal(reply, info)) {
496 LOG_ERROR("fail to Unmarshal index");
497 return Uri("");
498 }
499 return info;
500 }
501 } // namespace DataShare
502 } // namespace OHOS
503