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 "distributed_sched_proxy.h"
17 #include "dfx/dms_hitrace_constants.h"
18 #include "distributed_want.h"
19 #include "dms_constant.h"
20 #include "dtbschedmgr_log.h"
21 #include "ipc_types.h"
22 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
23 #include "mission/mission_info_converter.h"
24 #include "pixel_map.h"
25 #endif
26 #include "parcel_helper.h"
27 #include "string_ex.h"
28 
29 namespace OHOS {
30 namespace DistributedSchedule {
31 using namespace std;
32 using namespace AAFwk;
33 
34 namespace {
35 const std::string TAG = "DistributedSchedProxy";
36 const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
37 const std::string EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID";
38 const std::string EXTRO_INFO_JSON_KEY_REQUEST_CODE = "requestCode";
39 const std::string CMPT_PARAM_FREEINSTALL_BUNDLENAMES = "ohos.extra.param.key.allowedBundles";
40 const std::string DMS_VERSION_ID = "dmsVersion";
41 const std::string DMS_UID_SPEC_BUNDLE_NAME = "dmsCallerUidBundleName";
42 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
43 constexpr int32_t WAIT_TIME = 15;
44 #endif
45 }
46 
StartRemoteAbility(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode, uint32_t accessToken)47 int32_t DistributedSchedProxy::StartRemoteAbility(const OHOS::AAFwk::Want& want,
48     int32_t callerUid, int32_t requestCode, uint32_t accessToken)
49 {
50     sptr<IRemoteObject> remote = Remote();
51     if (remote == nullptr) {
52         return ERR_NULL_OBJECT;
53     }
54     MessageParcel data;
55     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
56         return ERR_FLATTEN_OBJECT;
57     }
58     PARCEL_WRITE_HELPER(data, Parcelable, &want);
59     PARCEL_WRITE_HELPER(data, Int32, callerUid);
60     PARCEL_WRITE_HELPER(data, Int32, requestCode);
61     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
62     MessageParcel msgReply;
63     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_ABILITY),
64         data, msgReply);
65 }
66 
StartAbilityFromRemote(const OHOS::AAFwk::Want& want, const OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t requestCode, const CallerInfo& callerInfo, const AccountInfo& accountInfo)67 int32_t DistributedSchedProxy::StartAbilityFromRemote(const OHOS::AAFwk::Want& want,
68     const OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t requestCode,
69     const CallerInfo& callerInfo, const AccountInfo& accountInfo)
70 {
71     sptr<IRemoteObject> remote = Remote();
72     if (remote == nullptr) {
73         HILOGE("StartAbilityFromRemote remote service null");
74         return ERR_NULL_OBJECT;
75     }
76     MessageParcel data;
77     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
78         return ERR_FLATTEN_OBJECT;
79     }
80     DistributedWant dstbWant(want);
81     PARCEL_WRITE_HELPER(data, Parcelable, &dstbWant);
82     AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
83     abilityInfo.ConvertToCompatiableAbilityInfo(compatibleAbilityInfo);
84     PARCEL_WRITE_HELPER(data, Parcelable, &compatibleAbilityInfo);
85     PARCEL_WRITE_HELPER(data, Int32, requestCode);
86     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
87     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
88     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
89     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
90     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
91     nlohmann::json extraInfoJson;
92     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = callerInfo.accessToken;
93     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID] = accountInfo.activeAccountId;
94     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID] = accountInfo.userId;
95     if (callerInfo.extraInfoJson.find(DMS_VERSION_ID) != callerInfo.extraInfoJson.end()) {
96         extraInfoJson[DMS_VERSION_ID] = callerInfo.extraInfoJson[DMS_VERSION_ID];
97     }
98     std::string extraInfo = extraInfoJson.dump();
99     PARCEL_WRITE_HELPER(data, String, extraInfo);
100     MessageParcel reply;
101     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
102     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_ABILITY_FROM_REMOTE),
103         data, reply);
104 }
105 
SendResultFromRemote(OHOS::AAFwk::Want& want, int32_t requestCode, const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t resultCode)106 int32_t DistributedSchedProxy::SendResultFromRemote(OHOS::AAFwk::Want& want, int32_t requestCode,
107     const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t resultCode)
108 {
109     sptr<IRemoteObject> remote = Remote();
110     if (remote == nullptr) {
111         HILOGE("SendResultFromRemote remote service null");
112         return ERR_NULL_OBJECT;
113     }
114     MessageParcel data;
115     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
116         return ERR_FLATTEN_OBJECT;
117     }
118     DistributedWant dstbWant(want);
119     PARCEL_WRITE_HELPER(data, Parcelable, &dstbWant);
120     PARCEL_WRITE_HELPER(data, Int32, requestCode);
121     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
122     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
123     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
124     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
125     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
126     PARCEL_WRITE_HELPER(data, Int32, resultCode);
127     nlohmann::json extraInfoJson;
128     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID] = accountInfo.activeAccountId;
129     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID] = accountInfo.userId;
130     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_CALLER_INFO_EX] = callerInfo.extraInfoJson.dump();
131     std::string extraInfo = extraInfoJson.dump();
132     PARCEL_WRITE_HELPER(data, String, extraInfo);
133     MessageParcel reply;
134     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::SEND_RESULT_FROM_REMOTE),
135         data, reply);
136 }
137 
ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId, int32_t missionId, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)138 int32_t DistributedSchedProxy::ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
139     int32_t missionId, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)
140 {
141     if (callback == nullptr) {
142         HILOGE("ContinueMission callback null");
143         return ERR_NULL_OBJECT;
144     }
145     sptr<IRemoteObject> remote = Remote();
146     if (remote == nullptr) {
147         HILOGE("ContinueMission remote service null");
148         return ERR_NULL_OBJECT;
149     }
150     MessageParcel data;
151     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
152         return ERR_FLATTEN_OBJECT;
153     }
154     PARCEL_WRITE_HELPER(data, String, srcDeviceId);
155     PARCEL_WRITE_HELPER(data, String, dstDeviceId);
156     PARCEL_WRITE_HELPER(data, Int32, missionId);
157     PARCEL_WRITE_HELPER(data, RemoteObject, callback);
158     PARCEL_WRITE_HELPER(data, Parcelable, &wantParams);
159     MessageParcel msgReply;
160     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::CONTINUE_MISSION), data, msgReply);
161 }
162 
ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId, const std::string& bundleName, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)163 int32_t DistributedSchedProxy::ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
164     const std::string& bundleName, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)
165 {
166     if (callback == nullptr) {
167         HILOGE("ContinueMission callback null");
168         return ERR_NULL_OBJECT;
169     }
170     sptr<IRemoteObject> remote = Remote();
171     if (remote == nullptr) {
172         HILOGE("ContinueMission remote service null");
173         return ERR_NULL_OBJECT;
174     }
175     MessageParcel data;
176     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
177         return ERR_FLATTEN_OBJECT;
178     }
179     PARCEL_WRITE_HELPER(data, String, srcDeviceId);
180     PARCEL_WRITE_HELPER(data, String, dstDeviceId);
181     PARCEL_WRITE_HELPER(data, String, bundleName);
182     PARCEL_WRITE_HELPER(data, RemoteObject, callback);
183     PARCEL_WRITE_HELPER(data, Parcelable, &wantParams);
184     MessageParcel msgReply;
185     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::CONTINUE_MISSION_OF_BUNDLENAME),
186         data, msgReply);
187 }
188 
StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId, int32_t callerUid, int32_t status, uint32_t accessToken)189 int32_t DistributedSchedProxy::StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId, int32_t callerUid,
190     int32_t status, uint32_t accessToken)
191 {
192     sptr<IRemoteObject> remote = Remote();
193     if (remote == nullptr) {
194         HILOGE("StartContinuation remote service null");
195         return ERR_NULL_OBJECT;
196     }
197     MessageParcel data;
198     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
199         return ERR_FLATTEN_OBJECT;
200     }
201     PARCEL_WRITE_HELPER(data, Parcelable, &want);
202     PARCEL_WRITE_HELPER(data, Int32, missionId);
203     PARCEL_WRITE_HELPER(data, Int32, callerUid);
204     PARCEL_WRITE_HELPER(data, Int32, status);
205     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
206     MessageParcel msgReply;
207     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_CONTINUATION),
208         data, msgReply);
209 }
210 
NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess)211 void DistributedSchedProxy::NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess)
212 {
213     sptr<IRemoteObject> remote = Remote();
214     if (remote == nullptr) {
215         HILOGE("NotifyCompleteContinuation remote service null");
216         return;
217     }
218     MessageParcel data;
219     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
220         return;
221     }
222     PARCEL_WRITE_HELPER_NORET(data, String16, devId);
223     PARCEL_WRITE_HELPER_NORET(data, Int32, sessionId);
224     PARCEL_WRITE_HELPER_NORET(data, Bool, isSuccess);
225     MessageParcel reply;
226     PARCEL_TRANSACT_SYNC_NORET(remote, static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_COMPLETE_CONTINUATION),
227         data, reply);
228 }
229 
NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult)230 int32_t DistributedSchedProxy::NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult)
231 {
232     sptr<IRemoteObject> remote = Remote();
233     if (remote == nullptr) {
234         HILOGE("NotifyDSchedEventResultFromRemote remote service null");
235         return ERR_NULL_OBJECT;
236     }
237     MessageParcel data;
238     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
239         return ERR_FLATTEN_OBJECT;
240     }
241     PARCEL_WRITE_HELPER(data, String, type);
242     PARCEL_WRITE_HELPER(data, Int32, dSchedEventResult);
243     MessageParcel reply;
244     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
245     (IDSchedInterfaceCode::NOTIFY_DSCHED_EVENT_RESULT_FROM_REMOTE), data, reply);
246 }
247 
NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess, const std::string dstInfo)248 int32_t DistributedSchedProxy::NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess,
249     const std::string dstInfo)
250 {
251     sptr<IRemoteObject> remote = Remote();
252     if (remote == nullptr) {
253         HILOGE("NotifyContinuationResultFromRemote remote service null");
254         return ERR_NULL_OBJECT;
255     }
256     MessageParcel data;
257     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
258         return ERR_FLATTEN_OBJECT;
259     }
260     PARCEL_WRITE_HELPER(data, Int32, sessionId);
261     PARCEL_WRITE_HELPER(data, Bool, isSuccess);
262     PARCEL_WRITE_HELPER(data, String, dstInfo);
263     MessageParcel reply;
264     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
265     (IDSchedInterfaceCode::NOTIFY_CONTINUATION_RESULT_FROM_REMOTE), data, reply);
266 }
267 
ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect, int32_t callerUid, int32_t callerPid, uint32_t accessToken)268 int32_t DistributedSchedProxy::ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
269     int32_t callerUid, int32_t callerPid, uint32_t accessToken)
270 {
271     if (connect == nullptr) {
272         HILOGE("ConnectRemoteAbility connect is null");
273         return ERR_NULL_OBJECT;
274     }
275 
276     sptr<IRemoteObject> remote = Remote();
277     if (remote == nullptr) {
278         HILOGE("ConnectRemoteAbility remote is null");
279         return ERR_NULL_OBJECT;
280     }
281     MessageParcel data;
282     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
283         return ERR_FLATTEN_OBJECT;
284     }
285     PARCEL_WRITE_HELPER(data, Parcelable, &want);
286     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
287     PARCEL_WRITE_HELPER(data, Int32, callerUid);
288     PARCEL_WRITE_HELPER(data, Int32, callerPid);
289     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
290     MessageParcel reply;
291     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::CONNECT_REMOTE_ABILITY),
292         data, reply);
293 }
294 
DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid, uint32_t accessToken)295 int32_t DistributedSchedProxy::DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid,
296     uint32_t accessToken)
297 {
298     if (connect == nullptr) {
299         HILOGE("DisconnectRemoteAbility connect is null");
300         return ERR_NULL_OBJECT;
301     }
302 
303     sptr<IRemoteObject> remote = Remote();
304     if (remote == nullptr) {
305         HILOGE("DisconnectRemoteAbility remote is null");
306         return ERR_NULL_OBJECT;
307     }
308     MessageParcel data;
309     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
310         return ERR_FLATTEN_OBJECT;
311     }
312     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
313     PARCEL_WRITE_HELPER(data, Int32, callerUid);
314     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
315     MessageParcel reply;
316     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::DISCONNECT_REMOTE_ABILITY),
317         data, reply);
318 }
319 
ConnectAbilityFromRemote(const OHOS::AAFwk::Want& want, const AppExecFwk::AbilityInfo& abilityInfo, const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo, const AccountInfo& accountInfo)320 int32_t DistributedSchedProxy::ConnectAbilityFromRemote(const OHOS::AAFwk::Want& want,
321     const AppExecFwk::AbilityInfo& abilityInfo, const sptr<IRemoteObject>& connect,
322     const CallerInfo& callerInfo, const AccountInfo& accountInfo)
323 {
324     if (connect == nullptr) {
325         HILOGE("ConnectAbilityFromRemote connect is null");
326         return ERR_NULL_OBJECT;
327     }
328 
329     sptr<IRemoteObject> remote = Remote();
330     if (remote == nullptr) {
331         HILOGE("ConnectAbilityFromRemote remote is null");
332         return ERR_NULL_OBJECT;
333     }
334     MessageParcel data;
335     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
336         return ERR_FLATTEN_OBJECT;
337     }
338     DistributedWant dstbWant(want);
339     PARCEL_WRITE_HELPER(data, Parcelable, &dstbWant);
340     AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
341     abilityInfo.ConvertToCompatiableAbilityInfo(compatibleAbilityInfo);
342     PARCEL_WRITE_HELPER(data, Parcelable, &compatibleAbilityInfo);
343     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
344     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
345     PARCEL_WRITE_HELPER(data, Int32, callerInfo.pid);
346     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
347     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
348     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
349     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
350     nlohmann::json extraInfoJson;
351     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = callerInfo.accessToken;
352     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID] = accountInfo.activeAccountId;
353     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID] = accountInfo.userId;
354     if (callerInfo.extraInfoJson.find(DMS_VERSION_ID) != callerInfo.extraInfoJson.end()) {
355         extraInfoJson[DMS_VERSION_ID] = callerInfo.extraInfoJson[DMS_VERSION_ID];
356     }
357     std::string extraInfo = extraInfoJson.dump();
358     PARCEL_WRITE_HELPER(data, String, extraInfo);
359     MessageParcel reply;
360     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
361     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::CONNECT_ABILITY_FROM_REMOTE),
362         data, reply);
363 }
364 
DisconnectAbilityFromRemote(const sptr<IRemoteObject>& connect, int32_t uid, const std::string& sourceDeviceId)365 int32_t DistributedSchedProxy::DisconnectAbilityFromRemote(const sptr<IRemoteObject>& connect,
366     int32_t uid, const std::string& sourceDeviceId)
367 {
368     if (connect == nullptr) {
369         HILOGE("DisconnectAbilityFromRemote connect is null");
370         return ERR_NULL_OBJECT;
371     }
372 
373     sptr<IRemoteObject> remote = Remote();
374     if (remote == nullptr) {
375         HILOGE("DisconnectAbilityFromRemote remote is null");
376         return ERR_NULL_OBJECT;
377     }
378     MessageParcel data;
379     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
380         return ERR_FLATTEN_OBJECT;
381     }
382     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
383     PARCEL_WRITE_HELPER(data, Int32, uid);
384     PARCEL_WRITE_HELPER(data, String, sourceDeviceId);
385     MessageParcel reply;
386     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
387     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::DISCONNECT_ABILITY_FROM_REMOTE),
388         data, reply);
389 }
390 
NotifyProcessDiedFromRemote(const CallerInfo& callerInfo)391 int32_t DistributedSchedProxy::NotifyProcessDiedFromRemote(const CallerInfo& callerInfo)
392 {
393     HILOGD("DistributedSchedProxy::NotifyProcessDiedFromRemote called");
394     sptr<IRemoteObject> remote = Remote();
395     if (remote == nullptr) {
396         HILOGE("NotifyProcessDiedFromRemote remote is null");
397         return ERR_NULL_OBJECT;
398     }
399     MessageParcel data;
400     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
401         return ERR_FLATTEN_OBJECT;
402     }
403     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
404     PARCEL_WRITE_HELPER(data, Int32, callerInfo.pid);
405     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
406     MessageParcel reply;
407     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_PROCESS_DIED_FROM_REMOTE),
408         data, reply);
409 }
410 
411 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag)412 int32_t DistributedSchedProxy::StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag)
413 {
414     HILOGI("called");
415     sptr<IRemoteObject> remote = Remote();
416     if (remote == nullptr) {
417         HILOGE("remote system ability is null");
418         return ERR_NULL_OBJECT;
419     }
420 
421     MessageParcel data;
422     MessageParcel reply;
423     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
424         return ERR_FLATTEN_OBJECT;
425     }
426     PARCEL_WRITE_HELPER(data, String16, Str8ToStr16(devId));
427     PARCEL_WRITE_HELPER(data, Bool, fixConflict);
428     PARCEL_WRITE_HELPER(data, Int64, tag);
429     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_SYNC_MISSIONS), data, reply);
430 }
431 
StartSyncMissionsFromRemote(const CallerInfo& callerInfo, std::vector<DstbMissionInfo>& missionInfos)432 int32_t DistributedSchedProxy::StartSyncMissionsFromRemote(const CallerInfo& callerInfo,
433     std::vector<DstbMissionInfo>& missionInfos)
434 {
435     HILOGI("called");
436     sptr<IRemoteObject> remote = Remote();
437     if (remote == nullptr) {
438         HILOGE("remote service is null");
439         return ERR_NULL_OBJECT;
440     }
441     MessageParcel data;
442     MessageParcel reply;
443     MessageOption option { MessageOption::TF_SYNC, WAIT_TIME };
444     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
445         return ERR_FLATTEN_OBJECT;
446     }
447     if (!CallerInfoMarshalling(callerInfo, data)) {
448         return ERR_FLATTEN_OBJECT;
449     }
450     int32_t error = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::START_SYNC_MISSIONS_FROM_REMOTE),
451         data, reply, option);
452     if (error != ERR_NONE) {
453         HILOGW("fail, error: %{public}d", error);
454         return error;
455     }
456     int32_t version = reply.ReadInt32();
457     HILOGD("version : %{public}d", version);
458     return DstbMissionInfo::ReadDstbMissionInfosFromParcel(reply, missionInfos) ? ERR_NONE : ERR_FLATTEN_OBJECT;
459 }
460 
StopSyncRemoteMissions(const std::string& devId)461 int32_t DistributedSchedProxy::StopSyncRemoteMissions(const std::string& devId)
462 {
463     HILOGI("called");
464     sptr<IRemoteObject> remote = Remote();
465     if (remote == nullptr) {
466         HILOGE("remote system ability is null");
467         return ERR_NULL_OBJECT;
468     }
469     MessageParcel data;
470     MessageParcel reply;
471     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
472         return ERR_FLATTEN_OBJECT;
473     }
474     PARCEL_WRITE_HELPER(data, String16, Str8ToStr16(devId));
475     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::STOP_SYNC_MISSIONS), data, reply);
476 }
477 
StopSyncMissionsFromRemote(const CallerInfo& callerInfo)478 int32_t DistributedSchedProxy::StopSyncMissionsFromRemote(const CallerInfo& callerInfo)
479 {
480     HILOGI("called");
481     sptr<IRemoteObject> remote = Remote();
482     if (remote == nullptr) {
483         HILOGE("remote service is null");
484         return ERR_NULL_OBJECT;
485     }
486     MessageParcel data;
487     MessageParcel reply;
488     MessageOption option { MessageOption::TF_SYNC, WAIT_TIME };
489     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
490         return ERR_FLATTEN_OBJECT;
491     }
492     if (!CallerInfoMarshalling(callerInfo, data)) {
493         return ERR_FLATTEN_OBJECT;
494     }
495     int32_t error = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::STOP_SYNC_MISSIONS_FROM_REMOTE),
496         data, reply, option);
497     if (error != ERR_NONE) {
498         HILOGW("sendRequest fail, error: %{public}d", error);
499         return error;
500     }
501     return reply.ReadInt32();
502 }
503 
RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj)504 int32_t DistributedSchedProxy::RegisterMissionListener(const std::u16string& devId,
505     const sptr<IRemoteObject>& obj)
506 {
507     HILOGI("RegisterMissionListener called");
508     sptr<IRemoteObject> remote = Remote();
509     if (remote == nullptr) {
510         HILOGE("remote system ability is null");
511         return ERR_NULL_OBJECT;
512     }
513     MessageParcel data;
514     MessageParcel reply;
515     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
516         return ERR_FLATTEN_OBJECT;
517     }
518     PARCEL_WRITE_HELPER(data, String16, devId);
519     PARCEL_WRITE_HELPER(data, RemoteObject, obj);
520     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_MISSION_LISTENER),
521         data, reply);
522 }
523 
RegisterDSchedEventListener(const DSchedEventType& type, const sptr<IRemoteObject>& obj)524 int32_t DistributedSchedProxy::RegisterDSchedEventListener(const DSchedEventType& type,
525     const sptr<IRemoteObject>& obj)
526 {
527     HILOGI("RegisterDSchedEventListener called");
528     sptr<IRemoteObject> remote = Remote();
529     if (remote == nullptr) {
530         HILOGE("remote system ability is null");
531         return ERR_NULL_OBJECT;
532     }
533     MessageParcel data;
534     MessageParcel reply;
535     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
536         return ERR_FLATTEN_OBJECT;
537     }
538     PARCEL_WRITE_HELPER(data, Uint8, type);
539     PARCEL_WRITE_HELPER(data, RemoteObject, obj);
540     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_DSCHED_EVENT_LISTENER),
541         data, reply);
542 }
543 
UnRegisterDSchedEventListener(const DSchedEventType& type, const sptr<IRemoteObject>& obj)544 int32_t DistributedSchedProxy::UnRegisterDSchedEventListener(const DSchedEventType& type,
545     const sptr<IRemoteObject>& obj)
546 {
547     HILOGI("UnRegisterDSchedEventListener called");
548     sptr<IRemoteObject> remote = Remote();
549     if (remote == nullptr) {
550         HILOGE("remote system ability is null");
551         return ERR_NULL_OBJECT;
552     }
553     MessageParcel data;
554     MessageParcel reply;
555     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
556         return ERR_FLATTEN_OBJECT;
557     }
558     PARCEL_WRITE_HELPER(data, Uint8, type);
559     PARCEL_WRITE_HELPER(data, RemoteObject, obj);
560     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_DSCHED_EVENT_LISTENER),
561         data, reply);
562 }
563 
RegisterOnListener(const std::string& type, const sptr<IRemoteObject>& obj, int32_t callingUid)564 int32_t DistributedSchedProxy::RegisterOnListener(const std::string& type,
565     const sptr<IRemoteObject>& obj, int32_t callingUid)
566 {
567     HILOGI("RegisterOnListener called");
568     sptr<IRemoteObject> remote = Remote();
569     if (remote == nullptr) {
570         HILOGE("remote system ability is null");
571         return ERR_NULL_OBJECT;
572     }
573     MessageParcel data;
574     MessageParcel reply;
575     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
576         return ERR_FLATTEN_OBJECT;
577     }
578     PARCEL_WRITE_HELPER(data, String, type);
579     PARCEL_WRITE_HELPER(data, RemoteObject, obj);
580     PARCEL_WRITE_HELPER(data, Int32, callingUid);
581     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_ON_LISTENER),
582         data, reply);
583 }
584 
RegisterOffListener(const std::string& type, const sptr<IRemoteObject>& obj, int32_t callingUid)585 int32_t DistributedSchedProxy::RegisterOffListener(const std::string& type,
586     const sptr<IRemoteObject>& obj, int32_t callingUid)
587 {
588     HILOGI("RegisterOffListener called");
589     sptr<IRemoteObject> remote = Remote();
590     if (remote == nullptr) {
591         HILOGE("remote system ability is null");
592         return ERR_NULL_OBJECT;
593     }
594     MessageParcel data;
595     MessageParcel reply;
596     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
597         return ERR_FLATTEN_OBJECT;
598     }
599     PARCEL_WRITE_HELPER(data, String, type);
600     PARCEL_WRITE_HELPER(data, RemoteObject, obj);
601     PARCEL_WRITE_HELPER(data, Int32, callingUid);
602     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_OFF_LISTENER),
603         data, reply);
604 }
605 
UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj)606 int32_t DistributedSchedProxy::UnRegisterMissionListener(const std::u16string& devId,
607     const sptr<IRemoteObject>& obj)
608 {
609     HILOGI("UnRegisterMissionListener called");
610     sptr<IRemoteObject> remote = Remote();
611     if (remote == nullptr) {
612         HILOGE("remote system ability is null");
613         return ERR_NULL_OBJECT;
614     }
615     MessageParcel data;
616     MessageParcel reply;
617     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
618         return ERR_FLATTEN_OBJECT;
619     }
620     PARCEL_WRITE_HELPER(data, String16, devId);
621     PARCEL_WRITE_HELPER(data, RemoteObject, obj);
622     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_MISSION_LISTENER),
623         data, reply);
624 }
625 
GetMissionInfos(const std::string& deviceId, int32_t numMissions, std::vector<AAFwk::MissionInfo>& missionInfos)626 int32_t DistributedSchedProxy::GetMissionInfos(const std::string& deviceId, int32_t numMissions,
627     std::vector<AAFwk::MissionInfo>& missionInfos)
628 {
629     HILOGI("called");
630     sptr<IRemoteObject> remote = Remote();
631     if (remote == nullptr) {
632         HILOGE("remote system ability is null");
633         return ERR_NULL_OBJECT;
634     }
635 
636     MessageParcel data;
637     MessageParcel reply;
638     MessageOption option;
639     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
640         return ERR_FLATTEN_OBJECT;
641     }
642     PARCEL_WRITE_HELPER(data, String16, Str8ToStr16(deviceId));
643     PARCEL_WRITE_HELPER(data, Int32, numMissions);
644     int32_t ret = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_MISSION_INFOS), data, reply,
645         option);
646     if (ret != ERR_NONE) {
647         HILOGW("sendRequest fail, error: %{public}d", ret);
648         return ret;
649     }
650     return MissionInfoConverter::ReadMissionInfosFromParcel(reply, missionInfos) ? ERR_NONE : ERR_FLATTEN_OBJECT;
651 }
652 
NotifyMissionsChangedFromRemote(const std::vector<DstbMissionInfo>& missionInfos, const CallerInfo& callerInfo)653 int32_t DistributedSchedProxy::NotifyMissionsChangedFromRemote(const std::vector<DstbMissionInfo>& missionInfos,
654     const CallerInfo& callerInfo)
655 {
656     HILOGI("NotifyMissionsChangedFromRemote is called");
657     sptr<IRemoteObject> remote = Remote();
658     if (remote == nullptr) {
659         HILOGE("NotifyMissionsChangedFromRemote remote service is null");
660         return ERR_NULL_OBJECT;
661     }
662 
663     MessageParcel data;
664     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
665         return ERR_FLATTEN_OBJECT;
666     }
667     PARCEL_WRITE_HELPER(data, Int32, callerInfo.dmsVersion);
668     if (!DstbMissionInfo::WriteDstbMissionInfosToParcel(data, missionInfos)) {
669         return ERR_FLATTEN_OBJECT;
670     }
671     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
672     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
673     PARCEL_WRITE_HELPER(data, Int32, callerInfo.pid);
674     PARCEL_WRITE_HELPER(data, Int32, callerInfo.dmsVersion);
675     MessageParcel reply;
676     MessageOption option { MessageOption::TF_SYNC, WAIT_TIME };
677     int32_t error = remote->SendRequest(static_cast<uint32_t>
678         (IDSchedInterfaceCode::NOTIFY_MISSIONS_CHANGED_FROM_REMOTE), data, reply, option);
679     if (error != ERR_NONE) {
680         HILOGE("%{public}s transact failed, error: %{public}d", __func__, error);
681         return error;
682     }
683     int32_t result = reply.ReadInt32();
684     HILOGD("%{public}s get result from server data = %{public}d", __func__, result);
685     return result;
686 }
687 
GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId, std::unique_ptr<MissionSnapshot>& missionSnapshot)688 int32_t DistributedSchedProxy::GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
689     std::unique_ptr<MissionSnapshot>& missionSnapshot)
690 {
691     if (networkId.empty()) {
692         HILOGE("networkId is null");
693         return ERR_NULL_OBJECT;
694     }
695     if (missionId < 0) {
696         HILOGE("missionId is invalid");
697         return INVALID_PARAMETERS_ERR;
698     }
699     sptr<IRemoteObject> remote = Remote();
700     if (remote == nullptr) {
701         HILOGE("remote is null");
702         return ERR_NULL_OBJECT;
703     }
704     MessageParcel data;
705     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
706         return ERR_FLATTEN_OBJECT;
707     }
708     PARCEL_WRITE_HELPER(data, String, networkId);
709     PARCEL_WRITE_HELPER(data, Int32, missionId);
710     MessageParcel reply;
711     MessageOption option;
712     int32_t error = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_REMOTE_MISSION_SNAPSHOT_INFO),
713         data, reply, option);
714     if (error != ERR_NONE) {
715         HILOGE("transact failed, error: %{public}d", error);
716         return error;
717     }
718     std::unique_ptr<MissionSnapshot> missionSnapshotPtr(reply.ReadParcelable<MissionSnapshot>());
719     missionSnapshot = std::move(missionSnapshotPtr);
720     return ERR_NONE;
721 }
722 
SetMissionContinueState(int32_t missionId, const AAFwk::ContinueState &state)723 int32_t DistributedSchedProxy::SetMissionContinueState(int32_t missionId, const AAFwk::ContinueState &state)
724 {
725     HILOGD("DistributedSchedProxy::SetMissionContinueState called");
726     sptr<IRemoteObject> remote = Remote();
727     if (remote == nullptr) {
728         HILOGE("SetMissionContinueState remote is null");
729         return ERR_NULL_OBJECT;
730     }
731 
732     MessageParcel data;
733     MessageParcel reply;
734     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
735         return ERR_FLATTEN_OBJECT;
736     }
737     PARCEL_WRITE_HELPER(data, Int32, missionId);
738     PARCEL_WRITE_HELPER(data, Int32, static_cast<int32_t>(state));
739     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::SET_MISSION_CONTINUE_STATE),
740         data, reply);
741 }
742 #endif
743 
CallerInfoMarshalling(const CallerInfo& callerInfo, MessageParcel& data)744 bool DistributedSchedProxy::CallerInfoMarshalling(const CallerInfo& callerInfo, MessageParcel& data)
745 {
746     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.uid, false);
747     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.pid, false);
748     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.callerType, false);
749     PARCEL_WRITE_HELPER_RET(data, String, callerInfo.sourceDeviceId, false);
750     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.duid, false);
751     PARCEL_WRITE_HELPER_RET(data, String, callerInfo.callerAppId, false);
752     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.dmsVersion, false);
753     return true;
754 }
755 
StartRemoteAbilityByCall(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect, int32_t callerUid, int32_t callerPid, uint32_t accessToken)756 int32_t DistributedSchedProxy::StartRemoteAbilityByCall(const OHOS::AAFwk::Want& want,
757     const sptr<IRemoteObject>& connect, int32_t callerUid, int32_t callerPid, uint32_t accessToken)
758 {
759     if (connect == nullptr) {
760         HILOGE("StartRemoteAbilityByCall connect is null");
761         return ERR_NULL_OBJECT;
762     }
763 
764     sptr<IRemoteObject> remote = Remote();
765     if (remote == nullptr) {
766         HILOGE("StartRemoteAbilityByCall remote is null");
767         return ERR_NULL_OBJECT;
768     }
769     MessageParcel data;
770     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
771         return ERR_FLATTEN_OBJECT;
772     }
773     PARCEL_WRITE_HELPER(data, Parcelable, &want);
774     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
775     PARCEL_WRITE_HELPER(data, Int32, callerUid);
776     PARCEL_WRITE_HELPER(data, Int32, callerPid);
777     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
778     MessageParcel reply;
779     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_ABILITY_BY_CALL),
780         data, reply);
781 }
782 
ReleaseRemoteAbility(const sptr<IRemoteObject>& connect, const AppExecFwk::ElementName &element)783 int32_t DistributedSchedProxy::ReleaseRemoteAbility(const sptr<IRemoteObject>& connect,
784     const AppExecFwk::ElementName &element)
785 {
786     if (connect == nullptr) {
787         HILOGE("ReleaseRemoteAbility connect is null");
788         return ERR_NULL_OBJECT;
789     }
790 
791     sptr<IRemoteObject> remote = Remote();
792     if (remote == nullptr) {
793         HILOGE("ReleaseRemoteAbility remote is null");
794         return ERR_NULL_OBJECT;
795     }
796     MessageParcel data;
797     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
798         return ERR_FLATTEN_OBJECT;
799     }
800     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
801     if (!data.WriteParcelable(&element)) {
802         HILOGE("ReleaseRemoteAbility write element error.");
803         return ERR_INVALID_VALUE;
804     }
805     MessageParcel reply;
806     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::RELEASE_REMOTE_ABILITY),
807         data, reply);
808 }
809 
StartAbilityByCallFromRemote(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo, const AccountInfo& accountInfo)810 int32_t DistributedSchedProxy::StartAbilityByCallFromRemote(const OHOS::AAFwk::Want& want,
811     const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo, const AccountInfo& accountInfo)
812 {
813     if (connect == nullptr) {
814         HILOGE("StartAbilityByCallFromRemote connect is null");
815         return ERR_NULL_OBJECT;
816     }
817 
818     sptr<IRemoteObject> remote = Remote();
819     if (remote == nullptr) {
820         HILOGE("StartAbilityByCallFromRemote remote is null");
821         return ERR_NULL_OBJECT;
822     }
823     MessageParcel data;
824     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
825         return ERR_FLATTEN_OBJECT;
826     }
827     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
828     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
829     PARCEL_WRITE_HELPER(data, Int32, callerInfo.pid);
830     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
831     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
832     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
833     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
834     nlohmann::json extraInfoJson;
835     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = callerInfo.accessToken;
836     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID] = accountInfo.activeAccountId;
837     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID] = accountInfo.userId;
838     if (callerInfo.extraInfoJson.find(DMS_VERSION_ID) != callerInfo.extraInfoJson.end()) {
839         extraInfoJson[DMS_VERSION_ID] = callerInfo.extraInfoJson[DMS_VERSION_ID];
840     }
841     if (callerInfo.extraInfoJson.find(DMS_UID_SPEC_BUNDLE_NAME) != callerInfo.extraInfoJson.end()) {
842         extraInfoJson[DMS_UID_SPEC_BUNDLE_NAME] = callerInfo.extraInfoJson[DMS_UID_SPEC_BUNDLE_NAME];
843     }
844 
845     std::string extraInfo = extraInfoJson.dump();
846     PARCEL_WRITE_HELPER(data, String, extraInfo);
847     DistributedWant dstbWant(want);
848     PARCEL_WRITE_HELPER(data, Parcelable, &dstbWant);
849     MessageParcel reply;
850     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
851     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
852         (IDSchedInterfaceCode::START_ABILITY_BY_CALL_FROM_REMOTE), data, reply);
853 }
854 
ReleaseAbilityFromRemote(const sptr<IRemoteObject>& connect, const AppExecFwk::ElementName &element, const CallerInfo& callerInfo)855 int32_t DistributedSchedProxy::ReleaseAbilityFromRemote(const sptr<IRemoteObject>& connect,
856     const AppExecFwk::ElementName &element, const CallerInfo& callerInfo)
857 {
858     if (connect == nullptr) {
859         HILOGE("ReleaseAbilityFromRemote connect is null");
860         return ERR_NULL_OBJECT;
861     }
862 
863     sptr<IRemoteObject> remote = Remote();
864     if (remote == nullptr) {
865         HILOGE("ReleaseAbilityFromRemote remote is null");
866         return ERR_NULL_OBJECT;
867     }
868     MessageParcel data;
869     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
870         return ERR_FLATTEN_OBJECT;
871     }
872     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
873     if (!data.WriteParcelable(&element)) {
874         HILOGE("ReleaseAbilityFromRemote write element error.");
875         return ERR_INVALID_VALUE;
876     }
877     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
878     std::string extraInfo = "";
879     PARCEL_WRITE_HELPER(data, String, extraInfo);
880     MessageParcel reply;
881     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
882     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::RELEASE_ABILITY_FROM_REMOTE),
883         data, reply);
884 }
885 
StartRemoteFreeInstall(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode, uint32_t accessToken, const sptr<IRemoteObject>& callback)886 int32_t DistributedSchedProxy::StartRemoteFreeInstall(const OHOS::AAFwk::Want& want,
887     int32_t callerUid, int32_t requestCode, uint32_t accessToken, const sptr<IRemoteObject>& callback)
888 {
889     HILOGD("called.");
890     if (callback == nullptr) {
891         HILOGE("ContinueMission callback null");
892         return ERR_NULL_OBJECT;
893     }
894 
895     sptr<IRemoteObject> remote = Remote();
896     if (remote == nullptr) {
897         HILOGE("remote callback null");
898         return ERR_NULL_OBJECT;
899     }
900 
901     MessageParcel data;
902     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
903         HILOGE("write interface token null");
904         return ERR_FLATTEN_OBJECT;
905     }
906 
907     PARCEL_WRITE_HELPER(data, Parcelable, &want);
908     PARCEL_WRITE_HELPER(data, Int32, callerUid);
909     PARCEL_WRITE_HELPER(data, Int32, requestCode);
910     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
911     PARCEL_WRITE_HELPER(data, RemoteObject, callback);
912     MessageParcel reply;
913     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_FREE_INSTALL),
914         data, reply);
915 }
916 
StartFreeInstallFromRemote(const FreeInstallInfo& info, int64_t taskId)917 int32_t DistributedSchedProxy::StartFreeInstallFromRemote(const FreeInstallInfo& info, int64_t taskId)
918 {
919     HILOGD("called.");
920     sptr<IRemoteObject> remote = Remote();
921     if (remote == nullptr) {
922         HILOGE("remote is null");
923         return ERR_NULL_OBJECT;
924     }
925 
926     MessageParcel data;
927     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
928         HILOGE("write interface token null");
929         return ERR_FLATTEN_OBJECT;
930     }
931 
932     DistributedWant dstbWant(info.want);
933     PARCEL_WRITE_HELPER(data, Parcelable, &dstbWant);
934     PARCEL_WRITE_HELPER(data, Int32, info.callerInfo.uid);
935     PARCEL_WRITE_HELPER(data, String, info.callerInfo.sourceDeviceId);
936     PARCEL_WRITE_HELPER(data, Int32, info.accountInfo.accountType);
937     PARCEL_WRITE_HELPER(data, StringVector, info.accountInfo.groupIdList);
938     PARCEL_WRITE_HELPER(data, String, info.callerInfo.callerAppId);
939     PARCEL_WRITE_HELPER(data, Int64, taskId);
940     DistributedWant cmpWant;
941     cmpWant.SetParam(CMPT_PARAM_FREEINSTALL_BUNDLENAMES, info.callerInfo.bundleNames);
942     PARCEL_WRITE_HELPER(data, Parcelable, &cmpWant);
943     nlohmann::json extraInfoJson;
944     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = info.callerInfo.accessToken;
945     extraInfoJson[EXTRO_INFO_JSON_KEY_REQUEST_CODE] = info.requestCode;
946     if (info.callerInfo.extraInfoJson.find(DMS_VERSION_ID) != info.callerInfo.extraInfoJson.end()) {
947         extraInfoJson[DMS_VERSION_ID] = info.callerInfo.extraInfoJson[DMS_VERSION_ID];
948     }
949     std::string extraInfo = extraInfoJson.dump();
950     PARCEL_WRITE_HELPER(data, String, extraInfo);
951     MessageParcel reply;
952     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
953     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_FREE_INSTALL_FROM_REMOTE),
954         data, reply);
955 }
956 
NotifyCompleteFreeInstallFromRemote(int64_t taskId, int32_t resultCode)957 int32_t DistributedSchedProxy::NotifyCompleteFreeInstallFromRemote(int64_t taskId, int32_t resultCode)
958 {
959     HILOGD("called.");
960     sptr<IRemoteObject> remote = Remote();
961     if (remote == nullptr) {
962         HILOGE("remote is null");
963         return ERR_NULL_OBJECT;
964     }
965 
966     MessageParcel data;
967     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
968         HILOGE("write interface token null");
969         return ERR_FLATTEN_OBJECT;
970     }
971 
972     PARCEL_WRITE_HELPER(data, Int64, taskId);
973     PARCEL_WRITE_HELPER(data, Int32, resultCode);
974     MessageParcel reply;
975     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
976         (IDSchedInterfaceCode::NOTIFY_COMPLETE_FREE_INSTALL_FROM_REMOTE), data, reply);
977 }
978 
979 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
StartRemoteShareForm( const std::string& remoteDeviceId, const AppExecFwk::FormShareInfo& formShareInfo)980 int32_t DistributedSchedProxy::StartRemoteShareForm(
981     const std::string& remoteDeviceId, const AppExecFwk::FormShareInfo& formShareInfo)
982 {
983     HILOGD("SHAREFORM:: func call");
984     if (remoteDeviceId.empty()) {
985         HILOGE("input params deviceId is nullptr.");
986         return INVALID_PARAMETERS_ERR;
987     }
988 
989     auto remote = Remote();
990     if (remote == nullptr) {
991         HILOGE("fail to get dms remote.");
992         return DMSPROXY_SERVICE_ERR;
993     }
994 
995     MessageParcel data;
996     MessageParcel reply;
997     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
998         HILOGE("WriteInterfaceToken failed");
999         return ERR_FLATTEN_OBJECT;
1000     }
1001 
1002     PARCEL_WRITE_HELPER(data, String, remoteDeviceId);
1003     PARCEL_WRITE_HELPER(data, Parcelable, &formShareInfo);
1004 
1005     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_SHARE_FORM),
1006         data, reply);
1007 }
1008 
StartShareFormFromRemote( const std::string& remoteDeviceId, const AppExecFwk::FormShareInfo& formShareInfo)1009 int32_t DistributedSchedProxy::StartShareFormFromRemote(
1010     const std::string& remoteDeviceId, const AppExecFwk::FormShareInfo& formShareInfo)
1011 {
1012     HILOGD("SHAREFORM:: func call");
1013     if (remoteDeviceId.empty()) {
1014         HILOGE("input params deviceId is nullptr.");
1015         return INVALID_PARAMETERS_ERR;
1016     }
1017 
1018     auto remote = Remote();
1019     if (remote == nullptr) {
1020         HILOGE("fail to get dms remote.");
1021         return DMSPROXY_SERVICE_ERR;
1022     }
1023 
1024     MessageParcel data;
1025     MessageParcel reply;
1026     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
1027         HILOGE("WriteInterfaceToken failed");
1028         return ERR_FLATTEN_OBJECT;
1029     }
1030 
1031     PARCEL_WRITE_HELPER(data, String, remoteDeviceId);
1032     PARCEL_WRITE_HELPER(data, Parcelable, &formShareInfo);
1033 
1034     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::START_SHARE_FORM_FROM_REMOTE),
1035         data, reply);
1036 }
1037 #endif
1038 
NotifyStateChangedFromRemote(int32_t abilityState, int32_t connectToken, const AppExecFwk::ElementName& element)1039 int32_t DistributedSchedProxy::NotifyStateChangedFromRemote(int32_t abilityState, int32_t connectToken,
1040     const AppExecFwk::ElementName& element)
1041 {
1042     sptr<IRemoteObject> remote = Remote();
1043     if (remote == nullptr) {
1044         HILOGE("NotifyStateChangedFromRemote remote service null");
1045         return ERR_NULL_OBJECT;
1046     }
1047     MessageParcel data;
1048     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
1049         HILOGE("WriteInterfaceToken failed");
1050         return ERR_FLATTEN_OBJECT;
1051     }
1052     PARCEL_WRITE_HELPER(data, Int32, abilityState);
1053     PARCEL_WRITE_HELPER(data, Int32, connectToken);
1054     if (!data.WriteParcelable(&element)) {
1055         HILOGE("NotifyStateChangedFromRemote write element error.");
1056         return ERR_INVALID_VALUE;
1057     }
1058     MessageParcel reply;
1059     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_STATE_CHANGED_FROM_REMOTE),
1060         data, reply);
1061 }
1062 
GetDistributedComponentList(std::vector<std::string>& distributedComponents)1063 int32_t DistributedSchedProxy::GetDistributedComponentList(std::vector<std::string>& distributedComponents)
1064 {
1065     sptr<IRemoteObject> remote = Remote();
1066     if (remote == nullptr) {
1067         HILOGE("GetDistributedComponentList remote is null");
1068         return ERR_NULL_OBJECT;
1069     }
1070     MessageParcel data;
1071     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
1072         return ERR_FLATTEN_OBJECT;
1073     }
1074     MessageParcel reply;
1075     MessageOption option;
1076     int32_t error = remote->SendRequest(static_cast<uint32_t>(IDSchedInterfaceCode::GET_DISTRIBUTED_COMPONENT_LIST),
1077         data, reply, option);
1078     if (error != ERR_NONE) {
1079         HILOGE("GetDistributedComponentList SendRequest error = %{public}d", error);
1080         return error;
1081     }
1082     int32_t result = reply.ReadInt32();
1083     if (result != ERR_NONE) {
1084         HILOGE("GetDistributedComponentList result = %{public}d", result);
1085         return result;
1086     }
1087     PARCEL_READ_HELPER(reply, StringVector, &distributedComponents);
1088     return ERR_NONE;
1089 }
1090 
StopRemoteExtensionAbility( const OHOS::AAFwk::Want& want, int32_t callerUid, uint32_t accessToken, int32_t extensionType)1091 int32_t DistributedSchedProxy::StopRemoteExtensionAbility(
1092     const OHOS::AAFwk::Want& want, int32_t callerUid, uint32_t accessToken, int32_t extensionType)
1093 {
1094     sptr<IRemoteObject> remote = Remote();
1095     if (remote == nullptr) {
1096         HILOGE("StopRemoteExtensionAbility remote service null");
1097         return ERR_NULL_OBJECT;
1098     }
1099     MessageParcel data;
1100     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
1101         HILOGE("StopRemoteExtensionAbility WriteInterfaceToken failed");
1102         return ERR_FLATTEN_OBJECT;
1103     }
1104     PARCEL_WRITE_HELPER(data, Parcelable, &want);
1105     PARCEL_WRITE_HELPER(data, Int32, callerUid);
1106     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
1107     PARCEL_WRITE_HELPER(data, Int32, extensionType);
1108     MessageParcel reply;
1109     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>(IDSchedInterfaceCode::STOP_REMOTE_EXTERNSION_ABILITY),
1110         data, reply);
1111 }
1112 
StopExtensionAbilityFromRemote(const OHOS::AAFwk::Want& want, const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t extensionType)1113 int32_t DistributedSchedProxy::StopExtensionAbilityFromRemote(const OHOS::AAFwk::Want& want,
1114     const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t extensionType)
1115 {
1116     sptr<IRemoteObject> remote = Remote();
1117     if (remote == nullptr) {
1118         HILOGE("StopExtensionAbilityFromRemote remote service null");
1119         return ERR_NULL_OBJECT;
1120     }
1121     MessageParcel data;
1122     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
1123         HILOGE("StopExtensionAbilityFromRemote WriteInterfaceToken failed");
1124         return ERR_FLATTEN_OBJECT;
1125     }
1126     DistributedWant dstbWant(want);
1127     PARCEL_WRITE_HELPER(data, Parcelable, &dstbWant);
1128     PARCEL_WRITE_HELPER(data, Int32, extensionType);
1129     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
1130     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
1131     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
1132     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
1133     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
1134     nlohmann::json extraInfoJson;
1135     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = callerInfo.accessToken;
1136     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID] = accountInfo.activeAccountId;
1137     extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID] = accountInfo.userId;
1138     std::string extraInfo = extraInfoJson.dump();
1139     PARCEL_WRITE_HELPER(data, String, extraInfo);
1140     MessageParcel reply;
1141     PARCEL_TRANSACT_SYNC_RET_INT(remote, static_cast<uint32_t>
1142         (IDSchedInterfaceCode::STOP_EXTERNSION_ABILITY_FROM_REMOTE), data, reply);
1143 }
1144 } // namespace DistributedSchedule
1145 } // namespace OHOS
1146 
1147