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