1 /*
2  * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "ability_scheduler_stub.h"
17 
18 #include "ability_manager_errors.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_tag_wrapper.h"
24 #include "ishared_result_set.h"
25 #include "session_info.h"
26 #include "values_bucket.h"
27 
28 namespace OHOS {
29 namespace AAFwk {
30 constexpr int CYCLE_LIMIT = 2000;
AbilitySchedulerStub()31 AbilitySchedulerStub::AbilitySchedulerStub()
32 {}
33 
~AbilitySchedulerStub()34 AbilitySchedulerStub::~AbilitySchedulerStub()
35 {}
36 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)37 int AbilitySchedulerStub::OnRemoteRequest(
38     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
39 {
40     std::u16string descriptor = AbilitySchedulerStub::GetDescriptor();
41     std::u16string remoteDescriptor = data.ReadInterfaceToken();
42     if (descriptor != remoteDescriptor) {
43         TAG_LOGE(AAFwkTag::ABILITYMGR, "local descriptor is not equal to remote");
44         return ERR_INVALID_STATE;
45     }
46     return OnRemoteRequestInner(code, data, reply, option);
47 }
48 
OnRemoteRequestInner(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)49 int AbilitySchedulerStub::OnRemoteRequestInner(
50     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
51 {
52     int retCode = ERR_OK;
53     retCode = OnRemoteRequestInnerFirst(code, data, reply, option);
54     if (retCode != ERR_CODE_NOT_EXIST) {
55         return retCode;
56     }
57     retCode = OnRemoteRequestInnerSecond(code, data, reply, option);
58     if (retCode != ERR_CODE_NOT_EXIST) {
59         return retCode;
60     }
61     retCode = OnRemoteRequestInnerThird(code, data, reply, option);
62     if (retCode != ERR_CODE_NOT_EXIST) {
63         return retCode;
64     }
65     TAG_LOGW(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::OnRemoteRequest, default case, need check.");
66     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
67 }
68 
OnRemoteRequestInnerFirst(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)69 int AbilitySchedulerStub::OnRemoteRequestInnerFirst(
70     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
71 {
72     switch (code) {
73         case SCHEDULE_ABILITY_TRANSACTION:
74             return AbilityTransactionInner(data, reply);
75         case SEND_RESULT:
76             return SendResultInner(data, reply);
77         case SCHEDULE_ABILITY_CONNECT:
78             return ConnectAbilityInner(data, reply);
79         case SCHEDULE_ABILITY_DISCONNECT:
80             return DisconnectAbilityInner(data, reply);
81         case SCHEDULE_ABILITY_COMMAND:
82             return CommandAbilityInner(data, reply);
83         case SCHEDULE_ABILITY_PREPARE_TERMINATE:
84             return PrepareTerminateAbilityInner(data, reply);
85         case SCHEDULE_ABILITY_COMMAND_WINDOW:
86             return CommandAbilityWindowInner(data, reply);
87         case SCHEDULE_SAVE_ABILITY_STATE:
88             return SaveAbilityStateInner(data, reply);
89         case SCHEDULE_RESTORE_ABILITY_STATE:
90             return RestoreAbilityStateInner(data, reply);
91         case SCHEDULE_GETFILETYPES:
92             return GetFileTypesInner(data, reply);
93         case SCHEDULE_OPENFILE:
94             return OpenFileInner(data, reply);
95         case SCHEDULE_OPENRAWFILE:
96             return OpenRawFileInner(data, reply);
97         case SCHEDULE_INSERT:
98             return InsertInner(data, reply);
99         case SCHEDULE_UPDATE:
100             return UpdatetInner(data, reply);
101         case SCHEDULE_DELETE:
102             return DeleteInner(data, reply);
103     }
104     return ERR_CODE_NOT_EXIST;
105 }
106 
OnRemoteRequestInnerSecond(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)107 int AbilitySchedulerStub::OnRemoteRequestInnerSecond(
108     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
109 {
110     switch (code) {
111         case SCHEDULE_QUERY:
112             return QueryInner(data, reply);
113         case SCHEDULE_CALL:
114             return CallInner(data, reply);
115         case SCHEDULE_GETTYPE:
116             return GetTypeInner(data, reply);
117         case SCHEDULE_RELOAD:
118             return ReloadInner(data, reply);
119         case SCHEDULE_BATCHINSERT:
120             return BatchInsertInner(data, reply);
121         case SCHEDULE_REGISTEROBSERVER:
122             return RegisterObserverInner(data, reply);
123         case SCHEDULE_UNREGISTEROBSERVER:
124             return UnregisterObserverInner(data, reply);
125         case SCHEDULE_NOTIFYCHANGE:
126             return NotifyChangeInner(data, reply);
127         case SCHEDULE_NORMALIZEURI:
128             return NormalizeUriInner(data, reply);
129         case SCHEDULE_DENORMALIZEURI:
130             return DenormalizeUriInner(data, reply);
131         case SCHEDULE_EXECUTEBATCH:
132             return ExecuteBatchInner(data, reply);
133         case NOTIFY_CONTINUATION_RESULT:
134             return NotifyContinuationResultInner(data, reply);
135         case REQUEST_CALL_REMOTE:
136             return CallRequestInner(data, reply);
137         case CONTINUE_ABILITY:
138             return ContinueAbilityInner(data, reply);
139         case DUMP_ABILITY_RUNNER_INNER:
140             return DumpAbilityInfoInner(data, reply);
141     }
142     return ERR_CODE_NOT_EXIST;
143 }
144 
OnRemoteRequestInnerThird(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)145 int AbilitySchedulerStub::OnRemoteRequestInnerThird(
146     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
147 {
148     switch (code) {
149         case SCHEDULE_SHARE_DATA:
150             return ShareDataInner(data, reply);
151         case SCHEDULE_ONEXECUTE_INTENT:
152             return OnExecuteIntentInner(data, reply);
153         case CREATE_MODAL_UI_EXTENSION:
154             return CreateModalUIExtensionInner(data, reply);
155         case UPDATE_SESSION_TOKEN:
156             return UpdateSessionTokenInner(data, reply);
157     }
158     return ERR_CODE_NOT_EXIST;
159 }
160 
AbilityTransactionInner(MessageParcel & data,MessageParcel & reply)161 int AbilitySchedulerStub::AbilityTransactionInner(MessageParcel &data, MessageParcel &reply)
162 {
163     std::shared_ptr<Want> want(data.ReadParcelable<Want>());
164     if (want == nullptr) {
165         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub want is nullptr");
166         return ERR_INVALID_VALUE;
167     }
168     std::unique_ptr<LifeCycleStateInfo> stateInfo(data.ReadParcelable<LifeCycleStateInfo>());
169     if (!stateInfo) {
170         TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable<LifeCycleStateInfo> failed");
171         return ERR_INVALID_VALUE;
172     }
173     sptr<SessionInfo> sessionInfo = nullptr;
174     if (data.ReadBool()) {
175         sessionInfo = data.ReadParcelable<SessionInfo>();
176     }
177     ScheduleAbilityTransaction(*want, *stateInfo, sessionInfo);
178     return NO_ERROR;
179 }
180 
ShareDataInner(MessageParcel & data,MessageParcel & reply)181 int AbilitySchedulerStub::ShareDataInner(MessageParcel &data, MessageParcel &reply)
182 {
183     int32_t requestCode = data.ReadInt32();
184     TAG_LOGI(AAFwkTag::ABILITYMGR, "requestCode:%{public}d.", requestCode);
185     ScheduleShareData(requestCode);
186     return NO_ERROR;
187 }
188 
SendResultInner(MessageParcel & data,MessageParcel & reply)189 int AbilitySchedulerStub::SendResultInner(MessageParcel &data, MessageParcel &reply)
190 {
191     int requestCode = data.ReadInt32();
192     int resultCode = data.ReadInt32();
193     std::shared_ptr<Want> want(data.ReadParcelable<Want>());
194     if (want == nullptr) {
195         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub want is nullptr");
196         return ERR_INVALID_VALUE;
197     }
198     SendResult(requestCode, resultCode, *want);
199     return NO_ERROR;
200 }
201 
ConnectAbilityInner(MessageParcel & data,MessageParcel & reply)202 int AbilitySchedulerStub::ConnectAbilityInner(MessageParcel &data, MessageParcel &reply)
203 {
204     std::shared_ptr<Want> want(data.ReadParcelable<Want>());
205     if (want == nullptr) {
206         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub want is nullptr");
207         return ERR_INVALID_VALUE;
208     }
209     ScheduleConnectAbility(*want);
210     return NO_ERROR;
211 }
212 
DisconnectAbilityInner(MessageParcel & data,MessageParcel & reply)213 int AbilitySchedulerStub::DisconnectAbilityInner(MessageParcel &data, MessageParcel &reply)
214 {
215     std::shared_ptr<Want> want(data.ReadParcelable<Want>());
216     if (want == nullptr) {
217         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub want is nullptr");
218         return ERR_INVALID_VALUE;
219     }
220     ScheduleDisconnectAbility(*want);
221     return NO_ERROR;
222 }
223 
CommandAbilityInner(MessageParcel & data,MessageParcel & reply)224 int AbilitySchedulerStub::CommandAbilityInner(MessageParcel &data, MessageParcel &reply)
225 {
226     std::shared_ptr<Want> want(data.ReadParcelable<Want>());
227     if (want == nullptr) {
228         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub want is nullptr");
229         return ERR_INVALID_VALUE;
230     }
231     bool reStart = data.ReadBool();
232     int startId = data.ReadInt32();
233     TAG_LOGD(AAFwkTag::ABILITYMGR, "ReadInt32, startId:%{public}d", startId);
234     ScheduleCommandAbility(*want, reStart, startId);
235     return NO_ERROR;
236 }
237 
PrepareTerminateAbilityInner(MessageParcel & data,MessageParcel & reply)238 int AbilitySchedulerStub::PrepareTerminateAbilityInner(MessageParcel &data, MessageParcel &reply)
239 {
240     bool ret = SchedulePrepareTerminateAbility();
241     if (!reply.WriteInt32(ret)) {
242         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to write ret");
243         return ERR_INVALID_VALUE;
244     }
245     return NO_ERROR;
246 }
247 
CommandAbilityWindowInner(MessageParcel & data,MessageParcel & reply)248 int AbilitySchedulerStub::CommandAbilityWindowInner(MessageParcel &data, MessageParcel &reply)
249 {
250     std::shared_ptr<Want> want(data.ReadParcelable<Want>());
251     if (want == nullptr) {
252         TAG_LOGE(AAFwkTag::ABILITYMGR, "want is nullptr");
253         return ERR_INVALID_VALUE;
254     }
255     sptr<SessionInfo> sessionInfo(data.ReadParcelable<SessionInfo>());
256     int32_t winCmd = data.ReadInt32();
257     ScheduleCommandAbilityWindow(*want, sessionInfo, static_cast<WindowCommand>(winCmd));
258     return NO_ERROR;
259 }
260 
SaveAbilityStateInner(MessageParcel & data,MessageParcel & reply)261 int AbilitySchedulerStub::SaveAbilityStateInner(MessageParcel &data, MessageParcel &reply)
262 {
263     ScheduleSaveAbilityState();
264     return NO_ERROR;
265 }
266 
RestoreAbilityStateInner(MessageParcel & data,MessageParcel & reply)267 int AbilitySchedulerStub::RestoreAbilityStateInner(MessageParcel &data, MessageParcel &reply)
268 {
269     std::shared_ptr<PacMap> pacMap(data.ReadParcelable<PacMap>());
270     if (pacMap == nullptr) {
271         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub RestoreAbilityState is nullptr");
272         return ERR_INVALID_VALUE;
273     }
274     ScheduleRestoreAbilityState(*pacMap);
275     return NO_ERROR;
276 }
277 
GetFileTypesInner(MessageParcel & data,MessageParcel & reply)278 int AbilitySchedulerStub::GetFileTypesInner(MessageParcel &data, MessageParcel &reply)
279 {
280     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
281     if (uri == nullptr) {
282         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
283         return ERR_INVALID_VALUE;
284     }
285     std::string mimeTypeFilter = data.ReadString();
286     if (mimeTypeFilter.empty()) {
287         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub mimeTypeFilter is nullptr");
288         return ERR_INVALID_VALUE;
289     }
290     std::vector<std::string> types = GetFileTypes(*uri, mimeTypeFilter);
291     if (!reply.WriteStringVector(types)) {
292         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteStringVector types");
293         return ERR_INVALID_VALUE;
294     }
295     return NO_ERROR;
296 }
297 
OpenFileInner(MessageParcel & data,MessageParcel & reply)298 int AbilitySchedulerStub::OpenFileInner(MessageParcel &data, MessageParcel &reply)
299 {
300     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
301     if (uri == nullptr) {
302         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
303         return ERR_INVALID_VALUE;
304     }
305     std::string mode = data.ReadString();
306     if (mode.empty()) {
307         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub mode is nullptr");
308         return ERR_INVALID_VALUE;
309     }
310     int fd = OpenFile(*uri, mode);
311     if (fd < 0) {
312         TAG_LOGE(AAFwkTag::ABILITYMGR, "OpenFile fail, fd is %{pubilc}d", fd);
313         return ERR_INVALID_VALUE;
314     }
315     if (!reply.WriteFileDescriptor(fd)) {
316         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteFileDescriptor fd");
317         return ERR_INVALID_VALUE;
318     }
319     return NO_ERROR;
320 }
321 
OpenRawFileInner(MessageParcel & data,MessageParcel & reply)322 int AbilitySchedulerStub::OpenRawFileInner(MessageParcel &data, MessageParcel &reply)
323 {
324     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
325     if (uri == nullptr) {
326         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
327         return ERR_INVALID_VALUE;
328     }
329     std::string mode = data.ReadString();
330     if (mode.empty()) {
331         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub mode is nullptr");
332         return ERR_INVALID_VALUE;
333     }
334     int fd = OpenRawFile(*uri, mode);
335     if (!reply.WriteInt32(fd)) {
336         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 fd");
337         return ERR_INVALID_VALUE;
338     }
339     return NO_ERROR;
340 }
341 
InsertInner(MessageParcel & data,MessageParcel & reply)342 int AbilitySchedulerStub::InsertInner(MessageParcel &data, MessageParcel &reply)
343 {
344     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
345     if (uri == nullptr) {
346         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
347         return ERR_INVALID_VALUE;
348     }
349     int index = Insert(*uri, NativeRdb::ValuesBucket::Unmarshalling(data));
350     if (!reply.WriteInt32(index)) {
351         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 index");
352         return ERR_INVALID_VALUE;
353     }
354     TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::InsertInner end");
355     return NO_ERROR;
356 }
357 
CallInner(MessageParcel & data,MessageParcel & reply)358 int AbilitySchedulerStub::CallInner(MessageParcel &data, MessageParcel &reply)
359 {
360     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
361     if (uri == nullptr) {
362         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
363         return ERR_INVALID_VALUE;
364     }
365     std::string method = data.ReadString();
366     if (method.empty()) {
367         TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable method is nullptr");
368         return ERR_INVALID_VALUE;
369     }
370     std::string arg = data.ReadString();
371     if (arg.empty()) {
372         TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable arg is nullptr");
373         return ERR_INVALID_VALUE;
374     }
375 
376     std::shared_ptr<AppExecFwk::PacMap> pacMap(data.ReadParcelable<AppExecFwk::PacMap>());
377     if (pacMap == nullptr) {
378         TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable pacMap is nullptr");
379         return ERR_INVALID_VALUE;
380     }
381     std::shared_ptr<AppExecFwk::PacMap> result = Call(*uri, method, arg, *pacMap);
382     if (!reply.WriteParcelable(result.get())) {
383         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable pacMap error");
384         return ERR_INVALID_VALUE;
385     }
386     TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::CallInner end");
387     return NO_ERROR;
388 }
389 
UpdatetInner(MessageParcel & data,MessageParcel & reply)390 int AbilitySchedulerStub::UpdatetInner(MessageParcel &data, MessageParcel &reply)
391 {
392     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
393     if (uri == nullptr) {
394         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
395         return ERR_INVALID_VALUE;
396     }
397     auto value = NativeRdb::ValuesBucket::Unmarshalling(data);
398     std::shared_ptr<NativeRdb::DataAbilityPredicates> predicates(
399         data.ReadParcelable<NativeRdb::DataAbilityPredicates>());
400     if (predicates == nullptr) {
401         TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable predicates is nullptr");
402         return ERR_INVALID_VALUE;
403     }
404     int index = Update(*uri, std::move(value), *predicates);
405     if (!reply.WriteInt32(index)) {
406         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 index");
407         return ERR_INVALID_VALUE;
408     }
409     return NO_ERROR;
410 }
411 
DeleteInner(MessageParcel & data,MessageParcel & reply)412 int AbilitySchedulerStub::DeleteInner(MessageParcel &data, MessageParcel &reply)
413 {
414     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
415     if (uri == nullptr) {
416         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
417         return ERR_INVALID_VALUE;
418     }
419     std::shared_ptr<NativeRdb::DataAbilityPredicates> predicates(
420         data.ReadParcelable<NativeRdb::DataAbilityPredicates>());
421     if (predicates == nullptr) {
422         TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable predicates is nullptr");
423         return ERR_INVALID_VALUE;
424     }
425     int index = Delete(*uri, *predicates);
426     if (!reply.WriteInt32(index)) {
427         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 index");
428         return ERR_INVALID_VALUE;
429     }
430     return NO_ERROR;
431 }
432 
QueryInner(MessageParcel & data,MessageParcel & reply)433 int AbilitySchedulerStub::QueryInner(MessageParcel &data, MessageParcel &reply)
434 {
435     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
436     if (uri == nullptr) {
437         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
438         return ERR_INVALID_VALUE;
439     }
440     std::vector<std::string> columns;
441     if (!data.ReadStringVector(&columns)) {
442         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadStringVector columns");
443         return ERR_INVALID_VALUE;
444     }
445     std::shared_ptr<NativeRdb::DataAbilityPredicates> predicates(
446         data.ReadParcelable<NativeRdb::DataAbilityPredicates>());
447     if (predicates == nullptr) {
448         TAG_LOGE(AAFwkTag::ABILITYMGR, "ReadParcelable predicates is nullptr");
449         return ERR_INVALID_VALUE;
450     }
451     auto resultSet = Query(*uri, columns, *predicates);
452     if (resultSet == nullptr) {
453         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable resultSet");
454         return ERR_INVALID_VALUE;
455     }
456     auto result = NativeRdb::ISharedResultSet::WriteToParcel(std::move(resultSet), reply);
457     if (result == nullptr) {
458         TAG_LOGE(AAFwkTag::ABILITYMGR, "!resultSet->Marshalling(reply)");
459         return ERR_INVALID_VALUE;
460     }
461     TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::QueryInner end");
462     return NO_ERROR;
463 }
464 
GetTypeInner(MessageParcel & data,MessageParcel & reply)465 int AbilitySchedulerStub::GetTypeInner(MessageParcel &data, MessageParcel &reply)
466 {
467     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
468     if (uri == nullptr) {
469         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
470         return ERR_INVALID_VALUE;
471     }
472     std::string type = GetType(*uri);
473     if (!reply.WriteString(type)) {
474         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteString type");
475         return ERR_INVALID_VALUE;
476     }
477     return NO_ERROR;
478 }
479 
ReloadInner(MessageParcel & data,MessageParcel & reply)480 int AbilitySchedulerStub::ReloadInner(MessageParcel &data, MessageParcel &reply)
481 {
482     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
483     if (uri == nullptr) {
484         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
485         return ERR_INVALID_VALUE;
486     }
487 
488     std::shared_ptr<PacMap> extras(data.ReadParcelable<PacMap>());
489     if (extras == nullptr) {
490         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub extras is nullptr");
491         return ERR_INVALID_VALUE;
492     }
493     bool ret = Reload(*uri, *extras);
494     if (!reply.WriteBool(ret)) {
495         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to writeBool ret");
496         return ERR_INVALID_VALUE;
497     }
498     return NO_ERROR;
499 }
500 
BatchInsertInner(MessageParcel & data,MessageParcel & reply)501 int AbilitySchedulerStub::BatchInsertInner(MessageParcel &data, MessageParcel &reply)
502 {
503     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
504     if (uri == nullptr) {
505         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
506         return ERR_INVALID_VALUE;
507     }
508 
509     int count = 0;
510     if (!data.ReadInt32(count)) {
511         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to ReadInt32 index");
512         return ERR_INVALID_VALUE;
513     }
514 
515     if (count > CYCLE_LIMIT) {
516         TAG_LOGE(AAFwkTag::ABILITYMGR, "count is too large");
517         return ERR_INVALID_VALUE;
518     }
519     std::vector<NativeRdb::ValuesBucket> values;
520     for (int i = 0; i < count; i++) {
521         values.emplace_back(NativeRdb::ValuesBucket::Unmarshalling(data));
522     }
523 
524     int ret = BatchInsert(*uri, values);
525     if (!reply.WriteInt32(ret)) {
526         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 ret");
527         return ERR_INVALID_VALUE;
528     }
529     return NO_ERROR;
530 }
531 
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)532 int AbilitySchedulerStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
533 {
534     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
535     if (uri == nullptr) {
536         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
537         return ERR_INVALID_VALUE;
538     }
539     auto obServer = iface_cast<IDataAbilityObserver>(data.ReadRemoteObject());
540     if (obServer == nullptr) {
541         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub obServer is nullptr");
542         return ERR_INVALID_VALUE;
543     }
544 
545     bool ret = ScheduleRegisterObserver(*uri, obServer);
546     if (!reply.WriteInt32(ret)) {
547         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 ret");
548         return ERR_INVALID_VALUE;
549     }
550     return NO_ERROR;
551 }
552 
UnregisterObserverInner(MessageParcel & data,MessageParcel & reply)553 int AbilitySchedulerStub::UnregisterObserverInner(MessageParcel &data, MessageParcel &reply)
554 {
555     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
556     if (uri == nullptr) {
557         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
558         return ERR_INVALID_VALUE;
559     }
560     auto obServer = iface_cast<IDataAbilityObserver>(data.ReadRemoteObject());
561     if (obServer == nullptr) {
562         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub obServer is nullptr");
563         return ERR_INVALID_VALUE;
564     }
565 
566     bool ret = ScheduleUnregisterObserver(*uri, obServer);
567     if (!reply.WriteInt32(ret)) {
568         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 ret");
569         return ERR_INVALID_VALUE;
570     }
571     return NO_ERROR;
572 }
573 
NotifyChangeInner(MessageParcel & data,MessageParcel & reply)574 int AbilitySchedulerStub::NotifyChangeInner(MessageParcel &data, MessageParcel &reply)
575 {
576     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
577     if (uri == nullptr) {
578         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
579         return ERR_INVALID_VALUE;
580     }
581 
582     bool ret = ScheduleNotifyChange(*uri);
583     if (!reply.WriteInt32(ret)) {
584         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 ret");
585         return ERR_INVALID_VALUE;
586     }
587     return NO_ERROR;
588 }
589 
NormalizeUriInner(MessageParcel & data,MessageParcel & reply)590 int AbilitySchedulerStub::NormalizeUriInner(MessageParcel &data, MessageParcel &reply)
591 {
592     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
593     if (uri == nullptr) {
594         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
595         return ERR_INVALID_VALUE;
596     }
597 
598     Uri ret("");
599     ret = NormalizeUri(*uri);
600     if (!reply.WriteParcelable(&ret)) {
601         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable type");
602         return ERR_INVALID_VALUE;
603     }
604     return NO_ERROR;
605 }
606 
DenormalizeUriInner(MessageParcel & data,MessageParcel & reply)607 int AbilitySchedulerStub::DenormalizeUriInner(MessageParcel &data, MessageParcel &reply)
608 {
609     std::shared_ptr<Uri> uri(data.ReadParcelable<Uri>());
610     if (uri == nullptr) {
611         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub uri is nullptr");
612         return ERR_INVALID_VALUE;
613     }
614 
615     Uri ret("");
616     ret = DenormalizeUri(*uri);
617     if (!reply.WriteParcelable(&ret)) {
618         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteParcelable type");
619         return ERR_INVALID_VALUE;
620     }
621     return NO_ERROR;
622 }
623 
ExecuteBatchInner(MessageParcel & data,MessageParcel & reply)624 int AbilitySchedulerStub::ExecuteBatchInner(MessageParcel &data, MessageParcel &reply)
625 {
626     TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::ExecuteBatchInner start");
627     int count = 0;
628     if (!data.ReadInt32(count)) {
629         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::ExecuteBatchInner fail to ReadInt32 count");
630         return ERR_INVALID_VALUE;
631     }
632     TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::ExecuteBatchInner count:%{public}d", count);
633     if (count > CYCLE_LIMIT) {
634         TAG_LOGE(AAFwkTag::ABILITYMGR, "count is too large");
635         return ERR_INVALID_VALUE;
636     }
637     std::vector<std::shared_ptr<AppExecFwk::DataAbilityOperation>> operations;
638     for (int i = 0; i < count; i++) {
639         std::shared_ptr<AppExecFwk::DataAbilityOperation> dataAbilityOperation(
640             data.ReadParcelable<AppExecFwk::DataAbilityOperation>());
641         if (dataAbilityOperation == nullptr) {
642             TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::ExecuteBatchInner dataAbilityOperation is nullptr, "
643                 "index = %{public}d", i);
644             return ERR_INVALID_VALUE;
645         }
646         operations.push_back(dataAbilityOperation);
647     }
648 
649     std::vector<std::shared_ptr<AppExecFwk::DataAbilityResult>> results = ExecuteBatch(operations);
650     int total = (int)results.size();
651     if (!reply.WriteInt32(total)) {
652         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::ExecuteBatchInner fail to WriteInt32 ret");
653         return ERR_INVALID_VALUE;
654     }
655     TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::ExecuteBatchInner total:%{public}d", total);
656     for (int i = 0; i < total; i++) {
657         if (results[i] == nullptr) {
658             TAG_LOGE(AAFwkTag::ABILITYMGR,
659                 "AbilitySchedulerStub::ExecuteBatchInner results[i] is nullptr, index = %{public}d", i);
660             return ERR_INVALID_VALUE;
661         }
662         if (!reply.WriteParcelable(results[i].get())) {
663             TAG_LOGE(AAFwkTag::ABILITYMGR,
664                 "AbilitySchedulerStub::ExecuteBatchInner fail to WriteParcelable operation, index = %{public}d", i);
665             return ERR_INVALID_VALUE;
666         }
667     }
668     TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::ExecuteBatchInner end");
669     return NO_ERROR;
670 }
671 
ContinueAbilityInner(MessageParcel & data,MessageParcel & reply)672 int AbilitySchedulerStub::ContinueAbilityInner(MessageParcel &data, MessageParcel &reply)
673 {
674     std::string deviceId = data.ReadString();
675     uint32_t versionCode = data.ReadUint32();
676     ContinueAbility(deviceId, versionCode);
677     return NO_ERROR;
678 }
679 
NotifyContinuationResultInner(MessageParcel & data,MessageParcel & reply)680 int AbilitySchedulerStub::NotifyContinuationResultInner(MessageParcel &data, MessageParcel &reply)
681 {
682     int32_t result = data.ReadInt32();
683     NotifyContinuationResult(result);
684     return NO_ERROR;
685 }
686 
DumpAbilityInfoInner(MessageParcel & data,MessageParcel & reply)687 int AbilitySchedulerStub::DumpAbilityInfoInner(MessageParcel &data, MessageParcel &reply)
688 {
689     std::vector<std::string> infos;
690     std::vector<std::string> params;
691     if (!data.ReadStringVector(&params)) {
692         TAG_LOGI(AAFwkTag::ABILITYMGR, "DumpAbilityInfoInner read params error");
693         return ERR_INVALID_VALUE;
694     }
695 
696     DumpAbilityInfo(params, infos);
697 
698     return NO_ERROR;
699 }
700 
CallRequestInner(MessageParcel & data,MessageParcel & reply)701 int AbilitySchedulerStub::CallRequestInner(MessageParcel &data, MessageParcel &reply)
702 {
703     CallRequest();
704     return NO_ERROR;
705 }
706 
OnExecuteIntentInner(MessageParcel & data,MessageParcel & reply)707 int AbilitySchedulerStub::OnExecuteIntentInner(MessageParcel &data, MessageParcel &reply)
708 {
709     TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub::OnExecuteIntentInner start");
710     std::shared_ptr<Want> want(data.ReadParcelable<Want>());
711     if (want == nullptr) {
712         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub want is nullptr");
713         return ERR_INVALID_VALUE;
714     }
715     OnExecuteIntent(*want);
716     return NO_ERROR;
717 }
718 
CreateModalUIExtensionInner(MessageParcel & data,MessageParcel & reply)719 int AbilitySchedulerStub::CreateModalUIExtensionInner(MessageParcel &data, MessageParcel &reply)
720 {
721     std::shared_ptr<Want> want(data.ReadParcelable<Want>());
722     if (want == nullptr) {
723         TAG_LOGE(AAFwkTag::ABILITYMGR, "AbilitySchedulerStub want is nullptr");
724         return ERR_INVALID_VALUE;
725     }
726     int ret = CreateModalUIExtension(*want);
727     if (!reply.WriteInt32(ret)) {
728         TAG_LOGE(AAFwkTag::ABILITYMGR, "fail to WriteInt32 ret");
729         return ERR_INVALID_VALUE;
730     }
731     return NO_ERROR;
732 }
733 
UpdateSessionTokenInner(MessageParcel & data,MessageParcel & reply)734 int AbilitySchedulerStub::UpdateSessionTokenInner(MessageParcel &data, MessageParcel &reply)
735 {
736     sptr<IRemoteObject> sessionToken = data.ReadRemoteObject();
737     UpdateSessionToken(sessionToken);
738     return NO_ERROR;
739 }
740 
OnRemoteDied(const wptr<IRemoteObject> & remote)741 void AbilitySchedulerRecipient::OnRemoteDied(const wptr<IRemoteObject> &remote)
742 {
743     TAG_LOGE(AAFwkTag::ABILITYMGR, "recv AbilitySchedulerRecipient death notice");
744 
745     if (handler_) {
746         handler_(remote);
747     }
748 }
749 
AbilitySchedulerRecipient(RemoteDiedHandler handler)750 AbilitySchedulerRecipient::AbilitySchedulerRecipient(RemoteDiedHandler handler) : handler_(handler)
751 {}
752 
~AbilitySchedulerRecipient()753 AbilitySchedulerRecipient::~AbilitySchedulerRecipient()
754 {}
755 }  // namespace AAFwk
756 }  // namespace OHOS
757