• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_manager_errors.h"
17 #include "ability_manager_ipc_interface_code.h"
18 #include "hilog_tag_wrapper.h"
19 #include "hitrace_meter.h"
20 #include "message_parcel.h"
21 #include "mission_manager_proxy.h"
22 #include "want.h"
23 
24 namespace OHOS::AAFwk {
WriteInterfaceToken(MessageParcel & data)25 bool MissionManagerProxy::WriteInterfaceToken(MessageParcel &data)
26 {
27     if (!data.WriteInterfaceToken(MissionManagerProxy::GetDescriptor())) {
28         TAG_LOGE(AAFwkTag::MISSION, "write token failed");
29         return false;
30     }
31     return true;
32 }
33 
SendRequest(AbilityManagerInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)34 ErrCode MissionManagerProxy::SendRequest(AbilityManagerInterfaceCode code, MessageParcel &data,
35     MessageParcel &reply, MessageOption &option)
36 {
37     auto remote = Remote();
38     if (remote == nullptr) {
39         TAG_LOGE(AAFwkTag::MISSION, "null remote");
40         return INVALID_REMOTE_PARAMETERS_ERR;
41     }
42 
43     return remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
44 }
45 
ContinueMission(const std::string & srcDeviceId,const std::string & dstDeviceId,int32_t missionId,const sptr<IRemoteObject> & callBack,AAFwk::WantParams & wantParams)46 int MissionManagerProxy::ContinueMission(const std::string &srcDeviceId, const std::string &dstDeviceId,
47     int32_t missionId, const sptr<IRemoteObject> &callBack, AAFwk::WantParams &wantParams)
48 {
49     TAG_LOGI(AAFwkTag::MISSION, "called");
50     MessageParcel data;
51     MessageParcel reply;
52     MessageOption option;
53     if (!WriteInterfaceToken(data)) {
54         return INVALID_PARAMETERS_ERR;
55     }
56     if (!data.WriteString(srcDeviceId)) {
57         TAG_LOGE(AAFwkTag::MISSION, "srcDeviceId write fail");
58         return INVALID_PARAMETERS_ERR;
59     }
60     if (!data.WriteString(dstDeviceId)) {
61         TAG_LOGE(AAFwkTag::MISSION, "dstDeviceId write fail");
62         return INVALID_PARAMETERS_ERR;
63     }
64     if (!data.WriteInt32(missionId)) {
65         TAG_LOGE(AAFwkTag::MISSION, "missionId write fail");
66         return INVALID_PARAMETERS_ERR;
67     }
68     if (!data.WriteRemoteObject(callBack)) {
69         TAG_LOGE(AAFwkTag::MISSION, "callBack write fail");
70         return INVALID_PARAMETERS_ERR;
71     }
72     if (!data.WriteParcelable(&wantParams)) {
73         TAG_LOGE(AAFwkTag::MISSION, "wantParams write fail");
74         return INVALID_PARAMETERS_ERR;
75     }
76 
77     auto error = SendRequest(AbilityManagerInterfaceCode::CONTINUE_MISSION, data, reply, option);
78     if (error != NO_ERROR) {
79         TAG_LOGE(AAFwkTag::MISSION, "sendRequest error: %{public}d", error);
80         return error;
81     }
82     return reply.ReadInt32();
83 }
84 
ContinueMission(AAFwk::ContinueMissionInfo continueMissionInfo,const sptr<IRemoteObject> & callback)85 int MissionManagerProxy::ContinueMission(AAFwk::ContinueMissionInfo continueMissionInfo,
86     const sptr<IRemoteObject> &callback)
87 {
88     MessageParcel data;
89     MessageParcel reply;
90     MessageOption option;
91     if (!WriteInterfaceToken(data)) {
92         return INVALID_PARAMETERS_ERR;
93     }
94     if (!data.WriteString(continueMissionInfo.srcDeviceId)) {
95         TAG_LOGE(AAFwkTag::MISSION, "srcDeviceId write fail");
96         return INVALID_PARAMETERS_ERR;
97     }
98     if (!data.WriteString(continueMissionInfo.dstDeviceId)) {
99         TAG_LOGE(AAFwkTag::MISSION, "dstDeviceId write fail");
100         return INVALID_PARAMETERS_ERR;
101     }
102     if (!data.WriteString(continueMissionInfo.bundleName)) {
103         TAG_LOGE(AAFwkTag::MISSION, "missionId write fail");
104         return INVALID_PARAMETERS_ERR;
105     }
106     if (!data.WriteRemoteObject(callback)) {
107         TAG_LOGE(AAFwkTag::MISSION, "callBack write fail");
108         return INVALID_PARAMETERS_ERR;
109     }
110     if (!data.WriteParcelable(&continueMissionInfo.wantParams)) {
111         TAG_LOGE(AAFwkTag::MISSION, "wantParams write fail");
112         return INVALID_PARAMETERS_ERR;
113     }
114     if (!data.WriteString(continueMissionInfo.srcBundleName)) {
115         TAG_LOGE(AAFwkTag::MISSION, "srcBundleName write fail");
116         return INVALID_PARAMETERS_ERR;
117     }
118     if (!data.WriteString(continueMissionInfo.continueType)) {
119         TAG_LOGE(AAFwkTag::MISSION, "continueType write fail");
120         return INVALID_PARAMETERS_ERR;
121     }
122     auto error = SendRequest(AbilityManagerInterfaceCode::CONTINUE_MISSION_OF_BUNDLENAME, data, reply, option);
123     if (error != NO_ERROR) {
124         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
125         return error;
126     }
127     return reply.ReadInt32();
128 }
129 
LockMissionForCleanup(int32_t missionId)130 int MissionManagerProxy::LockMissionForCleanup(int32_t missionId)
131 {
132     int error;
133     MessageParcel data;
134     MessageParcel reply;
135     MessageOption option;
136 
137     if (!WriteInterfaceToken(data)) {
138         return INVALID_PARAMETERS_ERR;
139     }
140     if (!data.WriteInt32(missionId)) {
141         TAG_LOGE(AAFwkTag::MISSION, "missionId write fail");
142         return INVALID_PARAMETERS_ERR;
143     }
144 
145     error = SendRequest(AbilityManagerInterfaceCode::LOCK_MISSION_FOR_CLEANUP, data, reply, option);
146     if (error != NO_ERROR) {
147         TAG_LOGE(AAFwkTag::MISSION, "send error:%{public}d", error);
148         return error;
149     }
150     return reply.ReadInt32();
151 }
152 
UnlockMissionForCleanup(int32_t missionId)153 int MissionManagerProxy::UnlockMissionForCleanup(int32_t missionId)
154 {
155     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
156     int error;
157     MessageParcel data;
158     MessageParcel reply;
159     MessageOption option;
160 
161     if (!WriteInterfaceToken(data)) {
162         return INVALID_PARAMETERS_ERR;
163     }
164     if (!data.WriteInt32(missionId)) {
165         TAG_LOGE(AAFwkTag::MISSION, "writeInt32 fail");
166         return INVALID_PARAMETERS_ERR;
167     }
168     error = SendRequest(AbilityManagerInterfaceCode::UNLOCK_MISSION_FOR_CLEANUP, data, reply, option);
169     if (error != NO_ERROR) {
170         TAG_LOGE(AAFwkTag::MISSION, "unlock mission,error:%{public}d", error);
171         return error;
172     }
173     return reply.ReadInt32();
174 }
175 
RegisterMissionListener(const sptr<IMissionListener> & listener)176 int MissionManagerProxy::RegisterMissionListener(const sptr<IMissionListener> &listener)
177 {
178     int error;
179     MessageParcel data;
180     MessageParcel reply;
181     MessageOption option;
182     if (!listener) {
183         TAG_LOGE(AAFwkTag::MISSION, "listener null");
184         return INVALID_PARAMETERS_ERR;
185     }
186 
187     if (!WriteInterfaceToken(data)) {
188         return INVALID_PARAMETERS_ERR;
189     }
190     if (!data.WriteRemoteObject(listener->AsObject())) {
191         TAG_LOGE(AAFwkTag::MISSION, "write missionListener fail");
192         return INVALID_PARAMETERS_ERR;
193     }
194 
195     error = SendRequest(AbilityManagerInterfaceCode::REGISTER_MISSION_LISTENER, data, reply, option);
196     if (error != NO_ERROR) {
197         TAG_LOGE(AAFwkTag::MISSION, "request error: %{public}d", error);
198         return error;
199     }
200     return reply.ReadInt32();
201 }
202 
UnRegisterMissionListener(const sptr<IMissionListener> & listener)203 int MissionManagerProxy::UnRegisterMissionListener(const sptr<IMissionListener> &listener)
204 {
205     int error;
206     MessageParcel data;
207     MessageParcel reply;
208     MessageOption option;
209     if (!listener) {
210         TAG_LOGE(AAFwkTag::MISSION, "listener null");
211         return INVALID_PARAMETERS_ERR;
212     }
213 
214     if (!WriteInterfaceToken(data)) {
215         return INVALID_PARAMETERS_ERR;
216     }
217     if (!data.WriteRemoteObject(listener->AsObject())) {
218         TAG_LOGE(AAFwkTag::MISSION, "write missionListener fail");
219         return INVALID_PARAMETERS_ERR;
220     }
221 
222     error = SendRequest(AbilityManagerInterfaceCode::UNREGISTER_MISSION_LISTENER, data, reply, option);
223     if (error != NO_ERROR) {
224         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
225         return error;
226     }
227     return reply.ReadInt32();
228 }
229 
RegisterMissionListener(const std::string & deviceId,const sptr<IRemoteMissionListener> & listener)230 int MissionManagerProxy::RegisterMissionListener(const std::string &deviceId,
231     const sptr<IRemoteMissionListener> &listener)
232 {
233     MessageParcel data;
234     MessageParcel reply;
235     MessageOption option;
236     if (!WriteInterfaceToken(data)) {
237         return INVALID_PARAMETERS_ERR;
238     }
239     if (!data.WriteString(deviceId)) {
240         TAG_LOGE(AAFwkTag::MISSION, "deviceId write fail");
241         return INVALID_PARAMETERS_ERR;
242     }
243     if (!data.WriteRemoteObject(listener->AsObject())) {
244         TAG_LOGE(AAFwkTag::MISSION, "listener write fail");
245         return INVALID_PARAMETERS_ERR;
246     }
247 
248     auto error = SendRequest(AbilityManagerInterfaceCode::REGISTER_REMOTE_MISSION_LISTENER,
249         data, reply, option);
250     if (error != NO_ERROR) {
251         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
252         return error;
253     }
254     return reply.ReadInt32();
255 }
256 
UnRegisterMissionListener(const std::string & deviceId,const sptr<IRemoteMissionListener> & listener)257 int MissionManagerProxy::UnRegisterMissionListener(const std::string &deviceId,
258     const sptr<IRemoteMissionListener> &listener)
259 {
260     MessageParcel data;
261     MessageParcel reply;
262     MessageOption option;
263     if (!WriteInterfaceToken(data)) {
264         return INVALID_PARAMETERS_ERR;
265     }
266     if (!data.WriteString(deviceId)) {
267         TAG_LOGE(AAFwkTag::MISSION, "deviceId write fail");
268         return INVALID_PARAMETERS_ERR;
269     }
270     if (!data.WriteRemoteObject(listener->AsObject())) {
271         TAG_LOGE(AAFwkTag::MISSION, "listener write fail");
272         return INVALID_PARAMETERS_ERR;
273     }
274 
275     auto error = SendRequest(AbilityManagerInterfaceCode::UNREGISTER_REMOTE_MISSION_LISTENER,
276         data, reply, option);
277     if (error != NO_ERROR) {
278         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
279         return error;
280     }
281     return reply.ReadInt32();
282 }
283 
284 template <typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)285 int MissionManagerProxy::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
286 {
287     int32_t infoSize = reply.ReadInt32();
288     if (infoSize > CYCLE_LIMIT) {
289         TAG_LOGE(AAFwkTag::MISSION, "infoSize large");
290         return INVALID_PARAMETERS_ERR;
291     }
292 
293     for (int32_t i = 0; i < infoSize; i++) {
294         std::unique_ptr<T> info(reply.ReadParcelable<T>());
295         if (!info) {
296             TAG_LOGE(AAFwkTag::MISSION, "readParcelableInfos fail");
297             return INVALID_PARAMETERS_ERR;
298         }
299         parcelableInfos.emplace_back(*info);
300     }
301     return NO_ERROR;
302 }
303 
GetMissionInfos(const std::string & deviceId,int32_t numMax,std::vector<MissionInfo> & missionInfos)304 int MissionManagerProxy::GetMissionInfos(const std::string &deviceId, int32_t numMax,
305     std::vector<MissionInfo> &missionInfos)
306 {
307     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
308     int error;
309     MessageParcel data;
310     MessageParcel reply;
311     MessageOption option;
312     if (!WriteInterfaceToken(data)) {
313         return INVALID_PARAMETERS_ERR;
314     }
315     if (!data.WriteString16(Str8ToStr16(deviceId))) {
316         TAG_LOGE(AAFwkTag::MISSION, "write deviceId fail");
317         return INVALID_PARAMETERS_ERR;
318     }
319     if (!data.WriteInt32(numMax)) {
320         TAG_LOGE(AAFwkTag::MISSION, "writeInt32 fail");
321         return INVALID_PARAMETERS_ERR;
322     }
323     error = SendRequest(AbilityManagerInterfaceCode::GET_MISSION_INFOS, data, reply, option);
324     if (error != NO_ERROR) {
325         TAG_LOGE(AAFwkTag::MISSION, " request error:%{public}d", error);
326         return error;
327     }
328     error = GetParcelableInfos<MissionInfo>(reply, missionInfos);
329     if (error != NO_ERROR) {
330         TAG_LOGE(AAFwkTag::MISSION, "getMissionInfos error: %{public}d", error);
331         return error;
332     }
333     return reply.ReadInt32();
334 }
335 
GetMissionInfo(const std::string & deviceId,int32_t missionId,MissionInfo & missionInfo)336 int MissionManagerProxy::GetMissionInfo(const std::string &deviceId, int32_t missionId,
337     MissionInfo &missionInfo)
338 {
339     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
340     int error;
341     MessageParcel data;
342     MessageParcel reply;
343     MessageOption option;
344     if (!WriteInterfaceToken(data)) {
345         return INVALID_PARAMETERS_ERR;
346     }
347     if (!data.WriteString16(Str8ToStr16(deviceId))) {
348         TAG_LOGE(AAFwkTag::MISSION, "write deviceId failed");
349         return INVALID_PARAMETERS_ERR;
350     }
351     if (!data.WriteInt32(missionId)) {
352         TAG_LOGE(AAFwkTag::MISSION, "writeInt32 failed");
353         return INVALID_PARAMETERS_ERR;
354     }
355     error = SendRequest(AbilityManagerInterfaceCode::GET_MISSION_INFO_BY_ID, data, reply, option);
356     if (error != NO_ERROR) {
357         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
358         return error;
359     }
360 
361     std::unique_ptr<MissionInfo> info(reply.ReadParcelable<MissionInfo>());
362     if (!info) {
363         TAG_LOGE(AAFwkTag::MISSION, "read missioninfo fail");
364         return ERR_UNKNOWN_OBJECT;
365     }
366     missionInfo = *info;
367     return reply.ReadInt32();
368 }
369 
GetMissionSnapshot(const std::string & deviceId,int32_t missionId,MissionSnapshot & snapshot,bool isLowResolution)370 int MissionManagerProxy::GetMissionSnapshot(const std::string &deviceId, int32_t missionId,
371     MissionSnapshot &snapshot, bool isLowResolution)
372 {
373     int error;
374     MessageParcel data;
375     MessageParcel reply;
376     MessageOption option;
377 
378     if (!WriteInterfaceToken(data)) {
379         return INVALID_PARAMETERS_ERR;
380     }
381     if (!data.WriteString(deviceId)) {
382         TAG_LOGE(AAFwkTag::MISSION, "deviceId write fail");
383         return INVALID_PARAMETERS_ERR;
384     }
385     if (!data.WriteInt32(missionId)) {
386         TAG_LOGE(AAFwkTag::MISSION, "missionId write fail");
387         return INVALID_PARAMETERS_ERR;
388     }
389     if (!data.WriteBool(isLowResolution)) {
390         TAG_LOGE(AAFwkTag::MISSION, "isLowResolution write fail");
391         return INVALID_PARAMETERS_ERR;
392     }
393     error = SendRequest(AbilityManagerInterfaceCode::GET_MISSION_SNAPSHOT_INFO, data, reply, option);
394     if (error != NO_ERROR) {
395         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
396         return error;
397     }
398     std::unique_ptr<MissionSnapshot> info(reply.ReadParcelable<MissionSnapshot>());
399     if (!info) {
400         TAG_LOGE(AAFwkTag::MISSION, "readParcelableInfo fail");
401         auto errorCode = reply.ReadInt32();
402         return errorCode ? errorCode : ERR_UNKNOWN_OBJECT;
403     }
404     snapshot = *info;
405     return reply.ReadInt32();
406 }
407 
CleanMission(int32_t missionId)408 int MissionManagerProxy::CleanMission(int32_t missionId)
409 {
410     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
411     int error;
412     MessageParcel data;
413     MessageParcel reply;
414     MessageOption option;
415 
416     if (!WriteInterfaceToken(data)) {
417         return INVALID_PARAMETERS_ERR;
418     }
419     if (!data.WriteInt32(missionId)) {
420         TAG_LOGE(AAFwkTag::MISSION, "writeInt32 fail");
421         return INVALID_PARAMETERS_ERR;
422     }
423     error = SendRequest(AbilityManagerInterfaceCode::CLEAN_MISSION, data, reply, option);
424     if (error != NO_ERROR) {
425         TAG_LOGE(AAFwkTag::MISSION, "clean mission, error: %{public}d", error);
426         return error;
427     }
428     return reply.ReadInt32();
429 }
430 
CleanAllMissions()431 int MissionManagerProxy::CleanAllMissions()
432 {
433     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
434     int error;
435     MessageParcel data;
436     MessageParcel reply;
437     MessageOption option;
438 
439     if (!WriteInterfaceToken(data)) {
440         return INVALID_PARAMETERS_ERR;
441     }
442     error = SendRequest(AbilityManagerInterfaceCode::CLEAN_ALL_MISSIONS, data, reply, option);
443     if (error != NO_ERROR) {
444         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
445         return error;
446     }
447     return reply.ReadInt32();
448 }
449 
MoveMissionToFront(int32_t missionId)450 int MissionManagerProxy::MoveMissionToFront(int32_t missionId)
451 {
452     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
453     int error;
454     MessageParcel data;
455     MessageParcel reply;
456     MessageOption option;
457 
458     if (!WriteInterfaceToken(data)) {
459         return INVALID_PARAMETERS_ERR;
460     }
461     if (!data.WriteInt32(missionId)) {
462         TAG_LOGE(AAFwkTag::MISSION, "writeInt32 fail");
463         return INVALID_PARAMETERS_ERR;
464     }
465     error = SendRequest(AbilityManagerInterfaceCode::MOVE_MISSION_TO_FRONT, data, reply, option);
466     if (error != NO_ERROR) {
467         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
468         return error;
469     }
470     return reply.ReadInt32();
471 }
472 
MoveMissionToFront(int32_t missionId,const StartOptions & startOptions)473 int MissionManagerProxy::MoveMissionToFront(int32_t missionId, const StartOptions &startOptions)
474 {
475     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
476     int error;
477     MessageParcel data;
478     MessageParcel reply;
479     MessageOption option;
480 
481     if (!WriteInterfaceToken(data)) {
482         return INVALID_PARAMETERS_ERR;
483     }
484     if (!data.WriteInt32(missionId)) {
485         TAG_LOGE(AAFwkTag::MISSION, "writeInt32 fail");
486         return INVALID_PARAMETERS_ERR;
487     }
488     if (!data.WriteParcelable(&startOptions)) {
489         TAG_LOGE(AAFwkTag::MISSION, "startOptions write fail");
490         return INVALID_PARAMETERS_ERR;
491     }
492     error = SendRequest(AbilityManagerInterfaceCode::MOVE_MISSION_TO_FRONT_BY_OPTIONS, data, reply, option);
493     if (error != NO_ERROR) {
494         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
495         return error;
496     }
497     return reply.ReadInt32();
498 }
499 
MoveMissionsToForeground(const std::vector<int32_t> & missionIds,int32_t topMissionId)500 int MissionManagerProxy::MoveMissionsToForeground(const std::vector<int32_t> &missionIds, int32_t topMissionId)
501 {
502     MessageParcel data;
503     MessageParcel reply;
504     MessageOption option;
505     if (!WriteInterfaceToken(data)) {
506         return INVALID_PARAMETERS_ERR;
507     }
508 
509     if (!data.WriteInt32Vector(missionIds)) {
510         TAG_LOGE(AAFwkTag::MISSION, "missionIds write fail");
511         return INVALID_PARAMETERS_ERR;
512     }
513 
514     if (!data.WriteInt32(topMissionId)) {
515         TAG_LOGE(AAFwkTag::MISSION, "topMissionId write fail");
516         return INVALID_PARAMETERS_ERR;
517     }
518 
519     auto error = SendRequest(AbilityManagerInterfaceCode::MOVE_MISSIONS_TO_FOREGROUND, data, reply, option);
520     if (error != NO_ERROR) {
521         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
522         return error;
523     }
524 
525     return reply.ReadInt32();
526 }
527 
MoveMissionsToBackground(const std::vector<int32_t> & missionIds,std::vector<int32_t> & result)528 int MissionManagerProxy::MoveMissionsToBackground(const std::vector<int32_t> &missionIds, std::vector<int32_t> &result)
529 {
530     MessageParcel data;
531     MessageParcel reply;
532     MessageOption option;
533     if (!WriteInterfaceToken(data)) {
534         return INVALID_PARAMETERS_ERR;
535     }
536 
537     if (!data.WriteInt32Vector(missionIds)) {
538         TAG_LOGE(AAFwkTag::MISSION, "mission id write fail");
539         return INVALID_PARAMETERS_ERR;
540     }
541 
542     auto error = SendRequest(AbilityManagerInterfaceCode::MOVE_MISSIONS_TO_BACKGROUND, data, reply, option);
543     if (error != NO_ERROR) {
544         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
545         return error;
546     }
547 
548     if (!reply.ReadInt32Vector(&result)) {
549         TAG_LOGE(AAFwkTag::MISSION, "read result fail");
550         return INVALID_PARAMETERS_ERR;
551     }
552     return reply.ReadInt32();
553 }
554 
GetMissionIdByToken(const sptr<IRemoteObject> & token)555 int32_t MissionManagerProxy::GetMissionIdByToken(const sptr<IRemoteObject> &token)
556 {
557     if (!token) {
558         TAG_LOGE(AAFwkTag::MISSION, "token null");
559         return ERR_INVALID_CALLER;
560     }
561 
562     MessageParcel data;
563     MessageParcel reply;
564     MessageOption option;
565     if (!WriteInterfaceToken(data)) {
566         TAG_LOGE(AAFwkTag::MISSION, "write token fail");
567         return INVALID_PARAMETERS_ERR;
568     }
569 
570     if (!data.WriteRemoteObject(token)) {
571         TAG_LOGE(AAFwkTag::MISSION, "data write fail");
572         return INVALID_PARAMETERS_ERR;
573     }
574 
575     auto error = SendRequest(AbilityManagerInterfaceCode::GET_MISSION_ID_BY_ABILITY_TOKEN,
576         data, reply, option);
577     if (error != NO_ERROR) {
578         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
579         return INVALID_PARAMETERS_ERR;
580     }
581 
582     return reply.ReadInt32();
583 }
584 
StartSyncRemoteMissions(const std::string & devId,bool fixConflict,int64_t tag)585 int MissionManagerProxy::StartSyncRemoteMissions(const std::string &devId, bool fixConflict, int64_t tag)
586 {
587     TAG_LOGI(AAFwkTag::MISSION, "called");
588     MessageParcel data;
589     MessageParcel reply;
590     MessageOption option;
591 
592     if (!WriteInterfaceToken(data)) {
593         TAG_LOGE(AAFwkTag::MISSION, "writeInterfaceToken fail");
594         return INVALID_PARAMETERS_ERR;
595     }
596     if (!data.WriteString(devId)) {
597         TAG_LOGE(AAFwkTag::MISSION, "write deviceId fail");
598         return INVALID_PARAMETERS_ERR;
599     }
600 
601     if (!data.WriteBool(fixConflict)) {
602         TAG_LOGE(AAFwkTag::MISSION, "writeBool fail");
603         return INVALID_PARAMETERS_ERR;
604     }
605 
606     if (!data.WriteInt64(tag)) {
607         TAG_LOGE(AAFwkTag::MISSION, "writeInt64 fail");
608         return INVALID_PARAMETERS_ERR;
609     }
610 
611     auto error = SendRequest(AbilityManagerInterfaceCode::START_SYNC_MISSIONS, data, reply, option);
612     if (error != NO_ERROR) {
613         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
614         return error;
615     }
616     return reply.ReadInt32();
617 }
618 
StopSyncRemoteMissions(const std::string & devId)619 int32_t MissionManagerProxy::StopSyncRemoteMissions(const std::string &devId)
620 {
621     TAG_LOGI(AAFwkTag::MISSION, "call");
622     MessageParcel data;
623     MessageParcel reply;
624     MessageOption option;
625 
626     if (!WriteInterfaceToken(data)) {
627         TAG_LOGE(AAFwkTag::MISSION, "writeInterfaceToken fail");
628         return INVALID_PARAMETERS_ERR;
629     }
630     if (!data.WriteString(devId)) {
631         TAG_LOGE(AAFwkTag::MISSION, "write deviceId fail");
632         return INVALID_PARAMETERS_ERR;
633     }
634     auto error = SendRequest(AbilityManagerInterfaceCode::STOP_SYNC_MISSIONS, data, reply, option);
635     if (error != NO_ERROR) {
636         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
637         return error;
638     }
639     return reply.ReadInt32();
640 }
641 
SetMissionContinueState(const sptr<IRemoteObject> & token,const AAFwk::ContinueState & state)642 int MissionManagerProxy::SetMissionContinueState(const sptr<IRemoteObject> &token, const AAFwk::ContinueState &state)
643 {
644     MessageParcel data;
645     MessageParcel reply;
646     MessageOption option;
647     if (!WriteInterfaceToken(data)) {
648         return INVALID_PARAMETERS_ERR;
649     }
650     if (!data.WriteRemoteObject(token)) {
651         TAG_LOGE(AAFwkTag::MISSION, "write token fail");
652         return INVALID_PARAMETERS_ERR;
653     }
654     if (!data.WriteInt32(static_cast<int32_t>(state))) {
655         TAG_LOGE(AAFwkTag::MISSION, "write state fail");
656         return INVALID_PARAMETERS_ERR;
657     }
658     auto error = SendRequest(AbilityManagerInterfaceCode::SET_MISSION_CONTINUE_STATE, data, reply, option);
659     if (error != NO_ERROR) {
660         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
661         return error;
662     }
663     return reply.ReadInt32();
664 }
665 
666 #ifdef SUPPORT_SCREEN
SetMissionLabel(const sptr<IRemoteObject> & token,const std::string & label)667 int MissionManagerProxy::SetMissionLabel(const sptr<IRemoteObject> &token, const std::string &label)
668 {
669     MessageParcel data;
670     MessageParcel reply;
671     MessageOption option;
672     if (!WriteInterfaceToken(data)) {
673         return INVALID_PARAMETERS_ERR;
674     }
675     if (!data.WriteRemoteObject(token)) {
676         TAG_LOGE(AAFwkTag::MISSION, "write token fail");
677         return INVALID_PARAMETERS_ERR;
678     }
679     if (!data.WriteString16(Str8ToStr16(label))) {
680         TAG_LOGE(AAFwkTag::MISSION, "write label fail");
681         return INVALID_PARAMETERS_ERR;
682     }
683     auto error = SendRequest(AbilityManagerInterfaceCode::SET_MISSION_LABEL, data, reply, option);
684     if (error != NO_ERROR) {
685         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
686         return error;
687     }
688     return reply.ReadInt32();
689 }
690 
SetMissionIcon(const sptr<IRemoteObject> & token,const std::shared_ptr<OHOS::Media::PixelMap> & icon)691 int MissionManagerProxy::SetMissionIcon(const sptr<IRemoteObject> &token,
692     const std::shared_ptr<OHOS::Media::PixelMap> &icon)
693 {
694     if (!token || !icon) {
695         TAG_LOGE(AAFwkTag::MISSION, "abilitytoken or icon invalid");
696         return INVALID_PARAMETERS_ERR;
697     }
698 
699     MessageParcel data;
700     MessageParcel reply;
701     MessageOption option;
702     if (!WriteInterfaceToken(data)) {
703         return INVALID_PARAMETERS_ERR;
704     }
705     if (!data.WriteRemoteObject(token)) {
706         TAG_LOGE(AAFwkTag::MISSION, "write token fail");
707         return INVALID_PARAMETERS_ERR;
708     }
709 
710     if (!data.WriteParcelable(icon.get())) {
711         TAG_LOGE(AAFwkTag::MISSION, "write icon fail");
712         return INVALID_PARAMETERS_ERR;
713     }
714 
715     auto error = SendRequest(AbilityManagerInterfaceCode::SET_MISSION_ICON, data, reply, option);
716     if (error != NO_ERROR) {
717         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
718         return error;
719     }
720     return reply.ReadInt32();
721 }
722 
UpdateMissionSnapShot(const sptr<IRemoteObject> & token,const std::shared_ptr<Media::PixelMap> & pixelMap)723 void MissionManagerProxy::UpdateMissionSnapShot(const sptr<IRemoteObject> &token,
724     const std::shared_ptr<Media::PixelMap> &pixelMap)
725 {
726     MessageParcel data;
727     MessageParcel reply;
728     MessageOption option(MessageOption::TF_ASYNC);
729 
730     if (!WriteInterfaceToken(data)) {
731         return;
732     }
733     if (!data.WriteRemoteObject(token)) {
734         TAG_LOGE(AAFwkTag::MISSION, "write token fail");
735         return;
736     }
737     if (!data.WriteParcelable(pixelMap.get())) {
738         TAG_LOGE(AAFwkTag::MISSION, "write pixelMap fail");
739         return;
740     }
741     auto error = SendRequest(AbilityManagerInterfaceCode::UPDATE_MISSION_SNAPSHOT_FROM_WMS,
742         data, reply, option);
743     if (error != NO_ERROR) {
744         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
745     }
746 }
747 #endif
748 
IsValidMissionIds(const std::vector<int32_t> & missionIds,std::vector<MissionValidResult> & results)749 int32_t MissionManagerProxy::IsValidMissionIds(
750     const std::vector<int32_t> &missionIds, std::vector<MissionValidResult> &results)
751 {
752     TAG_LOGI(AAFwkTag::MISSION, "call, query size:%{public}zu", missionIds.size());
753     MessageParcel data;
754     MessageParcel reply;
755     MessageOption option;
756     if (!WriteInterfaceToken(data)) {
757         TAG_LOGE(AAFwkTag::MISSION, "write token fail");
758         return INVALID_PARAMETERS_ERR;
759     }
760 
761     constexpr int32_t MAX_COUNT = 20;
762     int32_t num = static_cast<int32_t>(missionIds.size() > MAX_COUNT ? MAX_COUNT : missionIds.size());
763     if (!data.WriteInt32(num)) {
764         TAG_LOGE(AAFwkTag::MISSION, "write num fail");
765         return INVALID_PARAMETERS_ERR;
766     }
767     for (auto i = 0; i < num; ++i) {
768         if (!data.WriteInt32(missionIds.at(i))) {
769             TAG_LOGE(AAFwkTag::MISSION, "write missionId fail");
770             return INVALID_PARAMETERS_ERR;
771         }
772     }
773 
774     auto error = SendRequest(AbilityManagerInterfaceCode::QUERY_MISSION_VAILD, data, reply, option);
775     if (error != NO_ERROR) {
776         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
777         return error;
778     }
779 
780     auto resultCode = reply.ReadInt32();
781     if (resultCode != ERR_OK) {
782         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", resultCode);
783         return resultCode;
784     }
785 
786     auto infoSize = reply.ReadInt32();
787     for (auto i = 0; i < infoSize && i < MAX_COUNT; ++i) {
788         std::unique_ptr<MissionValidResult> info(reply.ReadParcelable<MissionValidResult>());
789         if (!info) {
790             TAG_LOGE(AAFwkTag::MISSION, "read result fail");
791             return INVALID_PARAMETERS_ERR;
792         }
793         results.emplace_back(*info);
794     }
795 
796     return resultCode;
797 }
798 
PreStartMission(const std::string & bundleName,const std::string & moduleName,const std::string & abilityName,const std::string & startTime)799 int32_t MissionManagerProxy::PreStartMission(const std::string &bundleName, const std::string &moduleName,
800     const std::string &abilityName, const std::string &startTime)
801 {
802     MessageParcel data;
803     MessageParcel reply;
804     MessageOption option;
805     if (!WriteInterfaceToken(data)) {
806         return INVALID_PARAMETERS_ERR;
807     }
808     if (!data.WriteString(bundleName)) {
809         TAG_LOGE(AAFwkTag::MISSION, "write bundleName fail");
810         return INVALID_PARAMETERS_ERR;
811     }
812     if (!data.WriteString(moduleName)) {
813         TAG_LOGE(AAFwkTag::MISSION, "write moduleName fail");
814         return INVALID_PARAMETERS_ERR;
815     }
816     if (!data.WriteString(abilityName)) {
817         TAG_LOGE(AAFwkTag::MISSION, "write abilityName fail");
818         return INVALID_PARAMETERS_ERR;
819     }
820     if (!data.WriteString(startTime)) {
821         TAG_LOGE(AAFwkTag::MISSION, "write startTime fail");
822         return INVALID_PARAMETERS_ERR;
823     }
824     auto error = SendRequest(AbilityManagerInterfaceCode::PRE_START_MISSION, data, reply, option);
825     if (error != NO_ERROR) {
826         TAG_LOGE(AAFwkTag::MISSION, "request error:%{public}d", error);
827         return error;
828     }
829     return reply.ReadInt32();
830 }
831 
TerminateMission(int32_t missionId)832 int32_t MissionManagerProxy::TerminateMission(int32_t missionId)
833 {
834     MessageParcel data;
835     MessageParcel reply;
836     MessageOption option;
837     if (!WriteInterfaceToken(data)) {
838         return INVALID_PARAMETERS_ERR;
839     }
840     if (!data.WriteInt32(missionId)) {
841         TAG_LOGE(AAFwkTag::MISSION, "missionId write fail");
842         return INVALID_PARAMETERS_ERR;
843     }
844 
845     auto error = SendRequest(AbilityManagerInterfaceCode::TERMINATE_MISSION,
846         data, reply, option);
847     if (error != NO_ERROR) {
848         TAG_LOGE(AAFwkTag::MISSION, "request error: %{public}d", error);
849         return error;
850     }
851 
852     return reply.ReadInt32();
853 }
854 }  // namespace OHOS::AAFwk
855