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