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