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