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