1 /*
2 * Copyright (c) 2021-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 #include "ability_scheduler_proxy.h"
16
17 #include "abs_shared_result_set.h"
18 #include "data_ability_observer_interface.h"
19 #include "data_ability_operation.h"
20 #include "data_ability_predicates.h"
21 #include "data_ability_result.h"
22 #include "hilog_wrapper.h"
23 #include "ipc_types.h"
24 #include "ishared_result_set.h"
25 #include "pac_map.h"
26 #include "values_bucket.h"
27 #include "want.h"
28
29 namespace OHOS {
30 namespace AAFwk {
WriteInterfaceToken(MessageParcel & data)31 bool AbilitySchedulerProxy::WriteInterfaceToken(MessageParcel &data)
32 {
33 if (!data.WriteInterfaceToken(AbilitySchedulerProxy::GetDescriptor())) {
34 HILOG_ERROR("write interface token failed");
35 return false;
36 }
37 return true;
38 }
39
ScheduleAbilityTransaction(const Want & want,const LifeCycleStateInfo & stateInfo)40 void AbilitySchedulerProxy::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo)
41 {
42 MessageParcel data;
43 MessageParcel reply;
44 MessageOption option(MessageOption::TF_ASYNC);
45 if (!WriteInterfaceToken(data)) {
46 return;
47 }
48 data.WriteParcelable(&want);
49 data.WriteParcelable(&stateInfo);
50 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_TRANSACTION, data, reply, option);
51 if (err != NO_ERROR) {
52 HILOG_ERROR("ScheduleAbilityTransaction fail to SendRequest. err: %{public}d", err);
53 }
54 }
55
SendResult(int requestCode,int resultCode,const Want & resultWant)56 void AbilitySchedulerProxy::SendResult(int requestCode, int resultCode, const Want &resultWant)
57 {
58 MessageParcel data;
59 MessageParcel reply;
60 MessageOption option(MessageOption::TF_ASYNC);
61 if (!WriteInterfaceToken(data)) {
62 return;
63 }
64 data.WriteInt32(requestCode);
65 data.WriteInt32(resultCode);
66 if (!data.WriteParcelable(&resultWant)) {
67 HILOG_ERROR("fail to WriteParcelable");
68 return;
69 }
70 int32_t err = Remote()->SendRequest(IAbilityScheduler::SEND_RESULT, data, reply, option);
71 if (err != NO_ERROR) {
72 HILOG_ERROR("SendResult fail to SendRequest. err: %{public}d", err);
73 }
74 }
75
ScheduleConnectAbility(const Want & want)76 void AbilitySchedulerProxy::ScheduleConnectAbility(const Want &want)
77 {
78 MessageParcel data;
79 MessageParcel reply;
80 MessageOption option(MessageOption::TF_ASYNC);
81 if (!WriteInterfaceToken(data)) {
82 return;
83 }
84 if (!data.WriteParcelable(&want)) {
85 HILOG_ERROR("fail to WriteParcelable");
86 return;
87 }
88 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_CONNECT, data, reply, option);
89 if (err != NO_ERROR) {
90 HILOG_ERROR("ScheduleConnectAbility fail to SendRequest. err: %{public}d", err);
91 }
92 }
93
ScheduleDisconnectAbility(const Want & want)94 void AbilitySchedulerProxy::ScheduleDisconnectAbility(const Want &want)
95 {
96 MessageParcel data;
97 MessageParcel reply;
98 MessageOption option(MessageOption::TF_ASYNC);
99 if (!WriteInterfaceToken(data)) {
100 return;
101 }
102 if (!data.WriteParcelable(&want)) {
103 HILOG_ERROR("fail to WriteParcelable");
104 return;
105 }
106
107 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_DISCONNECT, data, reply, option);
108 if (err != NO_ERROR) {
109 HILOG_ERROR("ScheduleDisconnectAbility fail to SendRequest. err: %{public}d", err);
110 }
111 }
112
ScheduleCommandAbility(const Want & want,bool restart,int startId)113 void AbilitySchedulerProxy::ScheduleCommandAbility(const Want &want, bool restart, int startId)
114 {
115 MessageParcel data;
116 MessageParcel reply;
117 MessageOption option(MessageOption::TF_ASYNC);
118 if (!WriteInterfaceToken(data)) {
119 return;
120 }
121 if (!data.WriteParcelable(&want)) {
122 HILOG_ERROR("fail to WriteParcelable");
123 return;
124 }
125 if (!data.WriteBool(restart)) {
126 HILOG_ERROR("fail to WriteBool");
127 return;
128 }
129 HILOG_INFO("WriteInt32,startId:%{public}d", startId);
130 if (!data.WriteInt32(startId)) {
131 HILOG_ERROR("fail to WriteInt32");
132 return;
133 }
134
135 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_ABILITY_COMMAND, data, reply, option);
136 if (err != NO_ERROR) {
137 HILOG_ERROR("ScheduleCommandAbility fail to SendRequest. err: %{public}d", err);
138 }
139 }
140
ScheduleSaveAbilityState()141 void AbilitySchedulerProxy::ScheduleSaveAbilityState()
142 {
143 MessageParcel data;
144 MessageParcel reply;
145 MessageOption option(MessageOption::TF_ASYNC);
146 if (!WriteInterfaceToken(data)) {
147 return;
148 }
149 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_SAVE_ABILITY_STATE, data, reply, option);
150 if (err != NO_ERROR) {
151 HILOG_ERROR("ScheduleSaveAbilityState fail to SendRequest. err: %{public}d", err);
152 }
153 }
154
ScheduleRestoreAbilityState(const PacMap & inState)155 void AbilitySchedulerProxy::ScheduleRestoreAbilityState(const PacMap &inState)
156 {
157 MessageParcel data;
158 MessageParcel reply;
159 MessageOption option(MessageOption::TF_ASYNC);
160 if (!WriteInterfaceToken(data)) {
161 return;
162 }
163 if (!data.WriteParcelable(&inState)) {
164 HILOG_ERROR("WriteParcelable error");
165 return;
166 }
167 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_RESTORE_ABILITY_STATE, data, reply, option);
168 if (err != NO_ERROR) {
169 HILOG_ERROR("ScheduleRestoreAbilityState fail to SendRequest. err: %{public}d", err);
170 }
171 }
172
173 /**
174 * @brief Obtains the MIME types of files supported.
175 *
176 * @param uri Indicates the path of the files to obtain.
177 * @param mimeTypeFilter Indicates the MIME types of the files to obtain. This parameter cannot be null.
178 *
179 * @return Returns the matched MIME types. If there is no match, null is returned.
180 */
GetFileTypes(const Uri & uri,const std::string & mimeTypeFilter)181 std::vector<std::string> AbilitySchedulerProxy::GetFileTypes(const Uri &uri, const std::string &mimeTypeFilter)
182 {
183 std::vector<std::string> types;
184
185 MessageParcel data;
186 MessageParcel reply;
187 MessageOption option;
188
189 if (!WriteInterfaceToken(data)) {
190 return types;
191 }
192
193 if (!data.WriteParcelable(&uri)) {
194 HILOG_ERROR("fail to WriteParcelable uri");
195 return types;
196 }
197
198 if (!data.WriteString(mimeTypeFilter)) {
199 HILOG_ERROR("fail to WriteString mimeTypeFilter");
200 return types;
201 }
202
203 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_GETFILETYPES, data, reply, option);
204 if (err != NO_ERROR) {
205 HILOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
206 }
207
208 if (!reply.ReadStringVector(&types)) {
209 HILOG_ERROR("fail to ReadStringVector types");
210 }
211
212 return types;
213 }
214
215 /**
216 * @brief Opens a file in a specified remote path.
217 *
218 * @param uri Indicates the path of the file to open.
219 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
220 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
221 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing data,
222 * or "rwt" for read and write access that truncates any existing file.
223 *
224 * @return Returns the file descriptor.
225 */
OpenFile(const Uri & uri,const std::string & mode)226 int AbilitySchedulerProxy::OpenFile(const Uri &uri, const std::string &mode)
227 {
228 int fd = -1;
229
230 MessageParcel data;
231 MessageParcel reply;
232 MessageOption option;
233
234 if (!WriteInterfaceToken(data)) {
235 return fd;
236 }
237
238 if (!data.WriteParcelable(&uri)) {
239 HILOG_ERROR("fail to WriteParcelable uri");
240 return fd;
241 }
242
243 if (!data.WriteString(mode)) {
244 HILOG_ERROR("fail to WriteString mode");
245 return fd;
246 }
247
248 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_OPENFILE, data, reply, option);
249 if (err != NO_ERROR) {
250 HILOG_ERROR("OpenFile fail to SendRequest. err: %{public}d", err);
251 return fd;
252 }
253
254 fd = reply.ReadFileDescriptor();
255 if (fd == -1) {
256 HILOG_ERROR("fail to ReadInt32 fd");
257 return fd;
258 }
259
260 return fd;
261 }
262
263 /**
264 * @brief This is like openFile, open a file that need to be able to return sub-sections of files,often assets
265 * inside of their .hap.
266 *
267 * @param uri Indicates the path of the file to open.
268 * @param mode Indicates the file open mode, which can be "r" for read-only access, "w" for write-only access
269 * (erasing whatever data is currently in the file), "wt" for write access that truncates any existing file,
270 * "wa" for write-only access to append to any existing data, "rw" for read and write access on any existing
271 * data, or "rwt" for read and write access that truncates any existing file.
272 *
273 * @return Returns the RawFileDescriptor object containing file descriptor.
274 */
OpenRawFile(const Uri & uri,const std::string & mode)275 int AbilitySchedulerProxy::OpenRawFile(const Uri &uri, const std::string &mode)
276 {
277 int fd = -1;
278
279 MessageParcel data;
280 MessageParcel reply;
281 MessageOption option;
282
283 if (!WriteInterfaceToken(data)) {
284 return fd;
285 }
286
287 if (!data.WriteParcelable(&uri)) {
288 HILOG_ERROR("fail to WriteParcelable uri");
289 return fd;
290 }
291
292 if (!data.WriteString(mode)) {
293 HILOG_ERROR("fail to WriteString mode");
294 return fd;
295 }
296
297 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_OPENRAWFILE, data, reply, option);
298 if (err != NO_ERROR) {
299 HILOG_ERROR("OpenFile fail to SendRequest. err: %{public}d", err);
300 return fd;
301 }
302
303 if (!reply.ReadInt32(fd)) {
304 HILOG_ERROR("fail to ReadInt32 fd");
305 return fd;
306 }
307
308 return fd;
309 }
310
311 /**
312 * @brief Inserts a single data record into the database.
313 *
314 * @param uri Indicates the path of the data to operate.
315 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
316 *
317 * @return Returns the index of the inserted data record.
318 */
Insert(const Uri & uri,const NativeRdb::ValuesBucket & value)319 int AbilitySchedulerProxy::Insert(const Uri &uri, const NativeRdb::ValuesBucket &value)
320 {
321 int index = -1;
322
323 MessageParcel data;
324 MessageParcel reply;
325 MessageOption option;
326
327 if (!WriteInterfaceToken(data)) {
328 return index;
329 }
330
331 if (!data.WriteParcelable(&uri)) {
332 HILOG_ERROR("fail to WriteParcelable uri");
333 return index;
334 }
335
336 if (!data.WriteParcelable(&value)) {
337 HILOG_ERROR("fail to WriteParcelable value");
338 return index;
339 }
340
341 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_INSERT, data, reply, option);
342 if (err != NO_ERROR) {
343 HILOG_ERROR("Insert fail to SendRequest. err: %{public}d", err);
344 return index;
345 }
346
347 if (!reply.ReadInt32(index)) {
348 HILOG_ERROR("fail to ReadInt32 index");
349 return index;
350 }
351
352 return index;
353 }
354
355 /**
356 * @brief Inserts a single data record into the database.
357 *
358 * @param uri Indicates the path of the data to operate.
359 * @param value Indicates the data record to insert. If this parameter is null, a blank row will be inserted.
360 *
361 * @return Returns the index of the inserted data record.
362 */
Call(const Uri & uri,const std::string & method,const std::string & arg,const AppExecFwk::PacMap & pacMap)363 std::shared_ptr<AppExecFwk::PacMap> AbilitySchedulerProxy::Call(
364 const Uri &uri, const std::string &method, const std::string &arg, const AppExecFwk::PacMap &pacMap)
365 {
366 MessageParcel data;
367 MessageParcel reply;
368 MessageOption option;
369
370 if (!WriteInterfaceToken(data)) {
371 return nullptr;
372 }
373
374 if (!data.WriteParcelable(&uri)) {
375 HILOG_ERROR("fail to WriteParcelable uri");
376 return nullptr;
377 }
378
379 if (!data.WriteString(method)) {
380 HILOG_ERROR("fail to WriteString method");
381 return nullptr;
382 }
383
384 if (!data.WriteString(arg)) {
385 HILOG_ERROR("fail to WriteString arg");
386 return nullptr;
387 }
388
389 if (!data.WriteParcelable(&pacMap)) {
390 HILOG_ERROR("fail to WriteParcelable pacMap");
391 return nullptr;
392 }
393
394 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_CALL, data, reply, option);
395 if (err != NO_ERROR) {
396 HILOG_ERROR("Call fail to SendRequest. err: %{public}d", err);
397 return nullptr;
398 }
399 std::shared_ptr<AppExecFwk::PacMap> result(reply.ReadParcelable<AppExecFwk::PacMap>());
400 if (!result) {
401 HILOG_ERROR("ReadParcelable value is nullptr.");
402 return nullptr;
403 }
404 return result;
405 }
406
407 /**
408 * @brief Updates data records in the database.
409 *
410 * @param uri Indicates the path of data to update.
411 * @param value Indicates the data to update. This parameter can be null.
412 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
413 *
414 * @return Returns the number of data records updated.
415 */
Update(const Uri & uri,const NativeRdb::ValuesBucket & value,const NativeRdb::DataAbilityPredicates & predicates)416 int AbilitySchedulerProxy::Update(const Uri &uri, const NativeRdb::ValuesBucket &value,
417 const NativeRdb::DataAbilityPredicates &predicates)
418 {
419 int index = -1;
420
421 MessageParcel data;
422 MessageParcel reply;
423 MessageOption option;
424
425 if (!WriteInterfaceToken(data)) {
426 return index;
427 }
428
429 if (!data.WriteParcelable(&uri)) {
430 HILOG_ERROR("fail to WriteParcelable uri");
431 return index;
432 }
433
434 if (!data.WriteParcelable(&value)) {
435 HILOG_ERROR("fail to WriteParcelable value");
436 return index;
437 }
438
439 if (!data.WriteParcelable(&predicates)) {
440 HILOG_ERROR("fail to WriteParcelable predicates");
441 return index;
442 }
443
444 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_UPDATE, data, reply, option);
445 if (err != NO_ERROR) {
446 HILOG_ERROR("Update fail to SendRequest. err: %{public}d", err);
447 return index;
448 }
449
450 if (!reply.ReadInt32(index)) {
451 HILOG_ERROR("fail to ReadInt32 index");
452 return index;
453 }
454
455 return index;
456 }
457
458 /**
459 * @brief Deletes one or more data records from the database.
460 *
461 * @param uri Indicates the path of the data to operate.
462 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
463 *
464 * @return Returns the number of data records deleted.
465 */
Delete(const Uri & uri,const NativeRdb::DataAbilityPredicates & predicates)466 int AbilitySchedulerProxy::Delete(const Uri &uri, const NativeRdb::DataAbilityPredicates &predicates)
467 {
468 int index = -1;
469
470 MessageParcel data;
471 MessageParcel reply;
472 MessageOption option;
473
474 if (!WriteInterfaceToken(data)) {
475 return index;
476 }
477
478 if (!data.WriteParcelable(&uri)) {
479 HILOG_ERROR("fail to WriteParcelable uri");
480 return index;
481 }
482
483 if (!data.WriteParcelable(&predicates)) {
484 HILOG_ERROR("fail to WriteParcelable predicates");
485 return index;
486 }
487
488 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_DELETE, data, reply, option);
489 if (err != NO_ERROR) {
490 HILOG_ERROR("Delete fail to SendRequest. err: %{public}d", err);
491 return index;
492 }
493
494 if (!reply.ReadInt32(index)) {
495 HILOG_ERROR("fail to ReadInt32 index");
496 return index;
497 }
498
499 return index;
500 }
501
502 /**
503 * @brief Deletes one or more data records from the database.
504 *
505 * @param uri Indicates the path of data to query.
506 * @param columns Indicates the columns to query. If this parameter is null, all columns are queried.
507 * @param predicates Indicates filter criteria. You should define the processing logic when this parameter is null.
508 *
509 * @return Returns the query result.
510 */
Query(const Uri & uri,std::vector<std::string> & columns,const NativeRdb::DataAbilityPredicates & predicates)511 std::shared_ptr<NativeRdb::AbsSharedResultSet> AbilitySchedulerProxy::Query(
512 const Uri &uri, std::vector<std::string> &columns, const NativeRdb::DataAbilityPredicates &predicates)
513 {
514 MessageParcel data;
515 MessageParcel reply;
516 MessageOption option;
517
518 if (!WriteInterfaceToken(data)) {
519 return nullptr;
520 }
521
522 if (!data.WriteParcelable(&uri)) {
523 HILOG_ERROR("fail to WriteParcelable uri");
524 return nullptr;
525 }
526
527 if (!data.WriteStringVector(columns)) {
528 HILOG_ERROR("fail to WriteStringVector columns");
529 return nullptr;
530 }
531
532 if (!data.WriteParcelable(&predicates)) {
533 HILOG_ERROR("fail to WriteParcelable predicates");
534 return nullptr;
535 }
536
537 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_QUERY, data, reply, option);
538 if (err != NO_ERROR) {
539 HILOG_ERROR("Query fail to SendRequest. err: %{public}d", err);
540 return nullptr;
541 }
542 return OHOS::NativeRdb::ISharedResultSet::ReadFromParcel(reply);
543 }
544
545 /**
546 * @brief Obtains the MIME type matching the data specified by the URI of the Data ability. This method should be
547 * implemented by a Data ability. Data abilities supports general data types, including text, HTML, and JPEG.
548 *
549 * @param uri Indicates the URI of the data.
550 *
551 * @return Returns the MIME type that matches the data specified by uri.
552 */
GetType(const Uri & uri)553 std::string AbilitySchedulerProxy::GetType(const Uri &uri)
554 {
555 std::string type;
556
557 MessageParcel data;
558 MessageParcel reply;
559 MessageOption option;
560
561 if (!WriteInterfaceToken(data)) {
562 return type;
563 }
564
565 if (!data.WriteParcelable(&uri)) {
566 HILOG_ERROR("fail to WriteParcelable uri");
567 return type;
568 }
569
570 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_GETTYPE, data, reply, option);
571 if (err != NO_ERROR) {
572 HILOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
573 return type;
574 }
575
576 type = reply.ReadString();
577 if (type.empty()) {
578 HILOG_ERROR("fail to ReadString type");
579 return type;
580 }
581
582 return type;
583 }
584
585 /**
586 * @brief Reloads data in the database.
587 *
588 * @param uri Indicates the position where the data is to reload. This parameter is mandatory.
589 * @param extras Indicates the PacMap object containing the additional parameters to be passed in this call. This
590 * parameter can be null. If a custom Sequenceable object is put in the PacMap object and will be transferred across
591 * processes, you must call BasePacMap.setClassLoader(ClassLoader) to set a class loader for the custom object.
592 *
593 * @return Returns true if the data is successfully reloaded; returns false otherwise.
594 */
Reload(const Uri & uri,const PacMap & extras)595 bool AbilitySchedulerProxy::Reload(const Uri &uri, const PacMap &extras)
596 {
597 bool ret = false;
598
599 MessageParcel data;
600 MessageParcel reply;
601 MessageOption option;
602
603 if (!WriteInterfaceToken(data)) {
604 return ret;
605 }
606
607 if (!data.WriteParcelable(&uri)) {
608 HILOG_ERROR("fail to WriteParcelable uri");
609 return ret;
610 }
611
612 if (!data.WriteParcelable(&extras)) {
613 HILOG_ERROR("fail to WriteParcelable extras");
614 return ret;
615 }
616
617 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_RELOAD, data, reply, option);
618 if (err != NO_ERROR) {
619 HILOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
620 return ret;
621 }
622
623 ret = reply.ReadBool();
624 if (!ret) {
625 HILOG_ERROR("fail to ReadBool ret");
626 return ret;
627 }
628
629 return ret;
630 }
631
632 /**
633 * @brief Inserts multiple data records into the database.
634 *
635 * @param uri Indicates the path of the data to operate.
636 * @param values Indicates the data records to insert.
637 *
638 * @return Returns the number of data records inserted.
639 */
BatchInsert(const Uri & uri,const std::vector<NativeRdb::ValuesBucket> & values)640 int AbilitySchedulerProxy::BatchInsert(const Uri &uri, const std::vector<NativeRdb::ValuesBucket> &values)
641 {
642 int ret = -1;
643
644 MessageParcel data;
645 MessageParcel reply;
646 MessageOption option;
647
648 if (!WriteInterfaceToken(data)) {
649 return ret;
650 }
651
652 if (!data.WriteParcelable(&uri)) {
653 HILOG_ERROR("fail to WriteParcelable uri");
654 return ret;
655 }
656
657 int count = (int)values.size();
658 if (!data.WriteInt32(count)) {
659 HILOG_ERROR("fail to WriteInt32 ret");
660 return ret;
661 }
662
663 for (int i = 0; i < count; i++) {
664 if (!data.WriteParcelable(&values[i])) {
665 HILOG_ERROR("fail to WriteParcelable ret, index = %{public}d", i);
666 return ret;
667 }
668 }
669
670 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_BATCHINSERT, data, reply, option);
671 if (err != NO_ERROR) {
672 HILOG_ERROR("GetFileTypes fail to SendRequest. err: %{public}d", err);
673 return ret;
674 }
675
676 if (!reply.ReadInt32(ret)) {
677 HILOG_ERROR("fail to ReadInt32 index");
678 return ret;
679 }
680
681 return ret;
682 }
683
684 /**
685 * @brief Registers an observer to DataObsMgr specified by the given Uri.
686 *
687 * @param uri, Indicates the path of the data to operate.
688 * @param dataObserver, Indicates the IDataAbilityObserver object.
689 */
ScheduleRegisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)690 bool AbilitySchedulerProxy::ScheduleRegisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver)
691 {
692 MessageParcel data;
693 MessageParcel reply;
694 MessageOption option;
695
696 if (!WriteInterfaceToken(data)) {
697 HILOG_ERROR("%{public}s WriteInterfaceToken(data) return false", __func__);
698 return false;
699 }
700
701 if (!data.WriteParcelable(&uri)) {
702 HILOG_ERROR("%{public}s failed to WriteParcelable uri ", __func__);
703 return false;
704 }
705
706 if (!data.WriteRemoteObject(dataObserver->AsObject())) {
707 HILOG_ERROR("%{public}s failed to WriteParcelable dataObserver ", __func__);
708 return false;
709 }
710
711 int32_t result = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_REGISTEROBSERVER, data, reply, option);
712 if (result == ERR_NONE) {
713 HILOG_INFO("%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32());
714 return true;
715 } else {
716 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
717 return false;
718 }
719 }
720
721 /**
722 * @brief Deregisters an observer used for DataObsMgr specified by the given Uri.
723 *
724 * @param uri, Indicates the path of the data to operate.
725 * @param dataObserver, Indicates the IDataAbilityObserver object.
726 */
ScheduleUnregisterObserver(const Uri & uri,const sptr<IDataAbilityObserver> & dataObserver)727 bool AbilitySchedulerProxy::ScheduleUnregisterObserver(const Uri &uri, const sptr<IDataAbilityObserver> &dataObserver)
728 {
729 MessageParcel data;
730 MessageParcel reply;
731 MessageOption option;
732
733 if (!WriteInterfaceToken(data)) {
734 HILOG_ERROR("%{public}s WriteInterfaceToken(data) return false", __func__);
735 return false;
736 }
737
738 if (!data.WriteParcelable(&uri)) {
739 HILOG_ERROR("%{public}s failed to WriteParcelable uri ", __func__);
740 return false;
741 }
742
743 if (!data.WriteRemoteObject(dataObserver->AsObject())) {
744 HILOG_ERROR("%{public}s failed to WriteParcelable dataObserver ", __func__);
745 return false;
746 }
747
748 int32_t result = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_UNREGISTEROBSERVER, data, reply, option);
749 if (result == ERR_NONE) {
750 HILOG_INFO("%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32());
751 return true;
752 } else {
753 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
754 return false;
755 }
756 }
757
758 /**
759 * @brief Notifies the registered observers of a change to the data resource specified by Uri.
760 *
761 * @param uri, Indicates the path of the data to operate.
762 */
ScheduleNotifyChange(const Uri & uri)763 bool AbilitySchedulerProxy::ScheduleNotifyChange(const Uri &uri)
764 {
765 MessageParcel data;
766 MessageParcel reply;
767 MessageOption option;
768
769 if (!WriteInterfaceToken(data)) {
770 HILOG_ERROR("%{public}s WriteInterfaceToken(data) return false", __func__);
771 return false;
772 }
773
774 if (!data.WriteParcelable(&uri)) {
775 HILOG_ERROR("%{public}s failed to WriteParcelable uri ", __func__);
776 return false;
777 }
778
779 int32_t result = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_NOTIFYCHANGE, data, reply, option);
780 if (result == ERR_NONE) {
781 HILOG_INFO("%{public}s SendRequest ok, retval is %{public}d", __func__, reply.ReadInt32());
782 return true;
783 } else {
784 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
785 return false;
786 }
787 }
788
789 /**
790 * @brief Converts the given uri that refer to the Data ability into a normalized URI. A normalized URI can be used
791 * across devices, persisted, backed up, and restored. It can refer to the same item in the Data ability even if the
792 * context has changed. If you implement URI normalization for a Data ability, you must also implement
793 * denormalizeUri(ohos.utils.net.Uri) to enable URI denormalization. After this feature is enabled, URIs passed to
794 * any method that is called on the Data ability must require normalization verification and denormalization. The
795 * default implementation of this method returns null, indicating that this Data ability does not support URI
796 * normalization.
797 *
798 * @param uri Indicates the Uri object to normalize.
799 *
800 * @return Returns the normalized Uri object if the Data ability supports URI normalization; returns null otherwise.
801 */
NormalizeUri(const Uri & uri)802 Uri AbilitySchedulerProxy::NormalizeUri(const Uri &uri)
803 {
804 Uri urivalue("");
805
806 MessageParcel data;
807 MessageParcel reply;
808 MessageOption option;
809
810 if (!WriteInterfaceToken(data)) {
811 return urivalue;
812 }
813
814 if (!data.WriteParcelable(&uri)) {
815 HILOG_ERROR("fail to WriteParcelable uri");
816 return urivalue;
817 }
818
819 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_NORMALIZEURI, data, reply, option);
820 if (err != NO_ERROR) {
821 HILOG_ERROR("NormalizeUri fail to SendRequest. err: %{public}d", err);
822 return Uri("");
823 }
824
825 std::unique_ptr<Uri> info(reply.ReadParcelable<Uri>());
826 if (!info) {
827 HILOG_ERROR("ReadParcelable value is nullptr.");
828 return Uri("");
829 }
830 return *info;
831 }
832
833 /**
834 * @brief Converts the given normalized uri generated by normalizeUri(ohos.utils.net.Uri) into a denormalized one.
835 * The default implementation of this method returns the original URI passed to it.
836 *
837 * @param uri uri Indicates the Uri object to denormalize.
838 *
839 * @return Returns the denormalized Uri object if the denormalization is successful; returns the original Uri passed
840 * to this method if there is nothing to do; returns null if the data identified by the original Uri cannot be found
841 * in the current environment.
842 */
DenormalizeUri(const Uri & uri)843 Uri AbilitySchedulerProxy::DenormalizeUri(const Uri &uri)
844 {
845 Uri urivalue("");
846
847 MessageParcel data;
848 MessageParcel reply;
849 MessageOption option;
850
851 if (!WriteInterfaceToken(data)) {
852 return urivalue;
853 }
854
855 if (!data.WriteParcelable(&uri)) {
856 HILOG_ERROR("fail to WriteParcelable uri");
857 return urivalue;
858 }
859
860 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_DENORMALIZEURI, data, reply, option);
861 if (err != NO_ERROR) {
862 HILOG_ERROR("DenormalizeUri fail to SendRequest. err: %{public}d", err);
863 return Uri("");
864 }
865
866 std::unique_ptr<Uri> info(reply.ReadParcelable<Uri>());
867 if (!info) {
868 HILOG_ERROR("ReadParcelable value is nullptr.");
869 return Uri("");
870 }
871 return *info;
872 }
873
ExecuteBatch(const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> & operations)874 std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> AbilitySchedulerProxy::ExecuteBatch(
875 const std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> &operations)
876 {
877 HILOG_INFO("AbilitySchedulerProxy::ExecuteBatch start");
878 MessageParcel data;
879 MessageParcel reply;
880 MessageOption option;
881
882 std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> results;
883 results.clear();
884
885 if (!WriteInterfaceToken(data)) {
886 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to Writer token");
887 return results;
888 }
889
890 int count = (int)operations.size();
891 if (!data.WriteInt32(count)) {
892 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to WriteInt32 ret");
893 return results;
894 }
895
896 for (int i = 0; i < count; i++) {
897 if (!data.WriteParcelable(operations[i].get())) {
898 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to WriteParcelable ret, index = %{public}d", i);
899 return results;
900 }
901 }
902
903 int32_t err = Remote()->SendRequest(IAbilityScheduler::SCHEDULE_EXECUTEBATCH, data, reply, option);
904 if (err != NO_ERROR) {
905 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to SendRequest. err: %{public}d", err);
906 return results;
907 }
908
909 int total = 0;
910 if (!reply.ReadInt32(total)) {
911 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch fail to ReadInt32 count %{public}d", total);
912 return results;
913 }
914
915 for (int i = 0; i < total; i++) {
916 AppExecFwk::DataAbilityResult *result = reply.ReadParcelable<AppExecFwk::DataAbilityResult>();
917 if (result == nullptr) {
918 HILOG_ERROR("AbilitySchedulerProxy::ExecuteBatch result is nullptr, index = %{public}d", i);
919 return results;
920 }
921 std::shared_ptr<AppExecFwk::DataAbilityResult> dataAbilityResult(result);
922 results.push_back(dataAbilityResult);
923 }
924 HILOG_INFO("AbilitySchedulerProxy::ExecuteBatch end %{public}d", total);
925 return results;
926 }
927
ContinueAbility(const std::string & deviceId,uint32_t versionCode)928 void AbilitySchedulerProxy::ContinueAbility(const std::string& deviceId, uint32_t versionCode)
929 {
930 MessageParcel data;
931 MessageParcel reply;
932 MessageOption option(MessageOption::TF_ASYNC);
933 if (!WriteInterfaceToken(data)) {
934 HILOG_ERROR("ContinueAbility fail to write token");
935 return;
936 }
937 if (!data.WriteString(deviceId)) {
938 HILOG_ERROR("ContinueAbility fail to write deviceId");
939 return;
940 }
941 if (!data.WriteUint32(versionCode)) {
942 HILOG_ERROR("ContinueAbility fail to write versionCode");
943 return;
944 }
945
946 int32_t err = Remote()->SendRequest(IAbilityScheduler::CONTINUE_ABILITY, data, reply, option);
947 if (err != NO_ERROR) {
948 HILOG_ERROR("ContinueAbility fail to SendRequest. err: %{public}d", err);
949 }
950 }
951
NotifyContinuationResult(int32_t result)952 void AbilitySchedulerProxy::NotifyContinuationResult(int32_t result)
953 {
954 MessageParcel data;
955 MessageParcel reply;
956 MessageOption option(MessageOption::TF_ASYNC);
957 if (!WriteInterfaceToken(data)) {
958 HILOG_ERROR("NotifyContinuationResult fail to write token");
959 return;
960 }
961 if (!data.WriteInt32(result)) {
962 HILOG_ERROR("NotifyContinuationResult fail to write result");
963 return;
964 }
965
966 int32_t err = Remote()->SendRequest(IAbilityScheduler::NOTIFY_CONTINUATION_RESULT, data, reply, option);
967 if (err != NO_ERROR) {
968 HILOG_ERROR("NotifyContinuationResult fail to SendRequest. err: %{public}d", err);
969 }
970 }
971
DumpAbilityInfo(const std::vector<std::string> & params,std::vector<std::string> & info)972 void AbilitySchedulerProxy::DumpAbilityInfo(const std::vector<std::string> ¶ms, std::vector<std::string> &info)
973 {
974 MessageParcel data;
975 MessageParcel reply;
976 MessageOption option(MessageOption::TF_ASYNC);
977 if (!WriteInterfaceToken(data)) {
978 HILOG_ERROR("DumpAbilityRunner fail to write token");
979 return;
980 }
981
982 if (!data.WriteStringVector(params)) {
983 HILOG_ERROR("DumpAbilityRunner fail to write params");
984 return;
985 }
986
987 int32_t err = Remote()->SendRequest(IAbilityScheduler::DUMP_ABILITY_RUNNER_INNER, data, reply, option);
988 if (err != NO_ERROR) {
989 HILOG_ERROR("DumpAbilityRunner fail to SendRequest. err: %{public}d", err);
990 }
991 }
992
CallRequest()993 void AbilitySchedulerProxy::CallRequest()
994 {
995 HILOG_INFO("AbilitySchedulerProxy::CallRequest start");
996
997 MessageParcel data;
998 MessageParcel reply;
999 MessageOption option(MessageOption::TF_ASYNC);
1000
1001 if (!WriteInterfaceToken(data)) {
1002 return;
1003 }
1004
1005 int32_t err = Remote()->SendRequest(IAbilityScheduler::REQUEST_CALL_REMOTE, data, reply, option);
1006 if (err != NO_ERROR) {
1007 HILOG_ERROR("CallRequest fail to SendRequest. err: %{public}d", err);
1008 return;
1009 }
1010
1011 HILOG_INFO("AbilitySchedulerProxy::CallRequest end");
1012 }
1013
1014 #ifdef ABILITY_COMMAND_FOR_TEST
BlockAbility()1015 int AbilitySchedulerProxy::BlockAbility()
1016 {
1017 HILOG_INFO("AbilitySchedulerProxy::BlockAbility start");
1018 int ret = -1;
1019 MessageParcel data;
1020 MessageParcel reply;
1021 MessageOption option;
1022
1023 if (!WriteInterfaceToken(data)) {
1024 return ret;
1025 }
1026 int32_t err = Remote()->SendRequest(IAbilityScheduler::BLOCK_ABILITY_INNER, data, reply, option);
1027 if (err != NO_ERROR) {
1028 HILOG_ERROR("BlockAbility fail to SendRequest. err: %d", err);
1029 return ret;
1030 }
1031 if (!reply.ReadInt32(ret)) {
1032 HILOG_ERROR("fail to ReadInt32 ret");
1033 return ret;
1034 }
1035 return ret;
1036 }
1037 #endif
1038 } // namespace AAFwk
1039 } // namespace OHOS
1040