• 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 "distributed_sched_proxy.h"
17 
18 #include "dfx/dms_hitrace_constants.h"
19 #include "dtbschedmgr_log.h"
20 #include "ipc_types.h"
21 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
22 #include "mission/mission_info_converter.h"
23 #include "pixel_map.h"
24 #endif
25 #include "parcel_helper.h"
26 #include "string_ex.h"
27 
28 namespace OHOS {
29 namespace DistributedSchedule {
30 using namespace std;
31 using namespace AAFwk;
32 
33 namespace {
34 const std::string TAG = "DistributedSchedProxy";
35 const std::u16string DMS_PROXY_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
36 const std::string EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID";
37 const std::string EXTRO_INFO_JSON_KEY_REQUEST_CODE = "requestCode";
38 const std::string CMPT_PARAM_FREEINSTALL_BUNDLENAMES = "ohos.extra.param.key.allowedBundles";
39 const std::string DMS_VERSION_ID = "dmsVersion";
40 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
41 constexpr int32_t WAIT_TIME = 15;
42 #endif
43 }
44 
StartRemoteAbility(const OHOS::AAFwk::Want & want,int32_t callerUid,int32_t requestCode,uint32_t accessToken)45 int32_t DistributedSchedProxy::StartRemoteAbility(const OHOS::AAFwk::Want& want,
46     int32_t callerUid, int32_t requestCode, uint32_t accessToken)
47 {
48     sptr<IRemoteObject> remote = Remote();
49     if (remote == nullptr) {
50         return ERR_NULL_OBJECT;
51     }
52     MessageParcel data;
53     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
54         return ERR_FLATTEN_OBJECT;
55     }
56     PARCEL_WRITE_HELPER(data, Parcelable, &want);
57     PARCEL_WRITE_HELPER(data, Int32, callerUid);
58     PARCEL_WRITE_HELPER(data, Int32, requestCode);
59     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
60     MessageParcel reply;
61     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_REMOTE_ABILITY, data, reply);
62 }
63 
StartAbilityFromRemote(const OHOS::AAFwk::Want & want,const OHOS::AppExecFwk::AbilityInfo & abilityInfo,int32_t requestCode,const CallerInfo & callerInfo,const AccountInfo & accountInfo)64 int32_t DistributedSchedProxy::StartAbilityFromRemote(const OHOS::AAFwk::Want& want,
65     const OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t requestCode,
66     const CallerInfo& callerInfo, const AccountInfo& accountInfo)
67 {
68     sptr<IRemoteObject> remote = Remote();
69     if (remote == nullptr) {
70         HILOGE("StartAbilityFromRemote remote service null");
71         return ERR_NULL_OBJECT;
72     }
73     MessageParcel data;
74     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
75         return ERR_FLATTEN_OBJECT;
76     }
77     PARCEL_WRITE_HELPER(data, Parcelable, &want);
78     AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
79     abilityInfo.ConvertToCompatiableAbilityInfo(compatibleAbilityInfo);
80     PARCEL_WRITE_HELPER(data, Parcelable, &compatibleAbilityInfo);
81     PARCEL_WRITE_HELPER(data, Int32, requestCode);
82     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
83     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
84     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
85     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
86     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
87     nlohmann::json extraInfoJson;
88     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = callerInfo.accessToken;
89     if (callerInfo.extraInfoJson.find(DMS_VERSION_ID) != callerInfo.extraInfoJson.end()) {
90         extraInfoJson[DMS_VERSION_ID] = callerInfo.extraInfoJson[DMS_VERSION_ID];
91     }
92     std::string extraInfo = extraInfoJson.dump();
93     PARCEL_WRITE_HELPER(data, String, extraInfo);
94     MessageParcel reply;
95     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
96     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_ABILITY_FROM_REMOTE, data, reply);
97 }
98 
SendResultFromRemote(OHOS::AAFwk::Want & want,int32_t requestCode,const CallerInfo & callerInfo,const AccountInfo & accountInfo,int32_t resultCode)99 int32_t DistributedSchedProxy::SendResultFromRemote(OHOS::AAFwk::Want& want, int32_t requestCode,
100     const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t resultCode)
101 {
102     sptr<IRemoteObject> remote = Remote();
103     if (remote == nullptr) {
104         HILOGE("SendResultFromRemote remote service null");
105         return ERR_NULL_OBJECT;
106     }
107     MessageParcel data;
108     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
109         return ERR_FLATTEN_OBJECT;
110     }
111     PARCEL_WRITE_HELPER(data, Parcelable, &want);
112     PARCEL_WRITE_HELPER(data, Int32, requestCode);
113     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
114     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
115     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
116     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
117     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
118     PARCEL_WRITE_HELPER(data, Int32, resultCode);
119     std::string extraInfo = callerInfo.extraInfoJson.dump();
120     PARCEL_WRITE_HELPER(data, String, extraInfo);
121     MessageParcel reply;
122     PARCEL_TRANSACT_SYNC_RET_INT(remote, SEND_RESULT_FROM_REMOTE, data, reply);
123 }
124 
ContinueMission(const std::string & srcDeviceId,const std::string & dstDeviceId,int32_t missionId,const sptr<IRemoteObject> & callback,const OHOS::AAFwk::WantParams & wantParams)125 int32_t DistributedSchedProxy::ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
126     int32_t missionId, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)
127 {
128     if (callback == nullptr) {
129         HILOGE("ContinueMission callback null");
130         return ERR_NULL_OBJECT;
131     }
132     sptr<IRemoteObject> remote = Remote();
133     if (remote == nullptr) {
134         HILOGE("ContinueMission remote service null");
135         return ERR_NULL_OBJECT;
136     }
137     MessageParcel data;
138     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
139         return ERR_FLATTEN_OBJECT;
140     }
141     PARCEL_WRITE_HELPER(data, String, srcDeviceId);
142     PARCEL_WRITE_HELPER(data, String, dstDeviceId);
143     PARCEL_WRITE_HELPER(data, Int32, missionId);
144     PARCEL_WRITE_HELPER(data, RemoteObject, callback);
145     PARCEL_WRITE_HELPER(data, Parcelable, &wantParams);
146     MessageParcel reply;
147     PARCEL_TRANSACT_SYNC_RET_INT(remote, CONTINUE_MISSION, data, reply);
148 }
149 
StartContinuation(const OHOS::AAFwk::Want & want,int32_t missionId,int32_t callerUid,int32_t status,uint32_t accessToken)150 int32_t DistributedSchedProxy::StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId, int32_t callerUid,
151     int32_t status, uint32_t accessToken)
152 {
153     sptr<IRemoteObject> remote = Remote();
154     if (remote == nullptr) {
155         HILOGE("StartContinuation remote service null");
156         return ERR_NULL_OBJECT;
157     }
158     MessageParcel data;
159     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
160         return ERR_FLATTEN_OBJECT;
161     }
162     PARCEL_WRITE_HELPER(data, Parcelable, &want);
163     PARCEL_WRITE_HELPER(data, Int32, missionId);
164     PARCEL_WRITE_HELPER(data, Int32, callerUid);
165     PARCEL_WRITE_HELPER(data, Int32, status);
166     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
167     MessageParcel reply;
168     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_CONTINUATION, data, reply);
169 }
170 
NotifyCompleteContinuation(const std::u16string & devId,int32_t sessionId,bool isSuccess)171 void DistributedSchedProxy::NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess)
172 {
173     sptr<IRemoteObject> remote = Remote();
174     if (remote == nullptr) {
175         HILOGE("NotifyCompleteContinuation remote service null");
176         return;
177     }
178     MessageParcel data;
179     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
180         return;
181     }
182     PARCEL_WRITE_HELPER_NORET(data, String16, devId);
183     PARCEL_WRITE_HELPER_NORET(data, Int32, sessionId);
184     PARCEL_WRITE_HELPER_NORET(data, Bool, isSuccess);
185     MessageParcel reply;
186     PARCEL_TRANSACT_SYNC_NORET(remote, NOTIFY_COMPLETE_CONTINUATION, data, reply);
187 }
188 
NotifyContinuationResultFromRemote(int32_t sessionId,bool isSuccess)189 int32_t DistributedSchedProxy::NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess)
190 {
191     sptr<IRemoteObject> remote = Remote();
192     if (remote == nullptr) {
193         HILOGE("NotifyContinuationResultFromRemote remote service null");
194         return ERR_NULL_OBJECT;
195     }
196     MessageParcel data;
197     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
198         return ERR_FLATTEN_OBJECT;
199     }
200     PARCEL_WRITE_HELPER(data, Int32, sessionId);
201     PARCEL_WRITE_HELPER(data, Bool, isSuccess);
202     MessageParcel reply;
203     PARCEL_TRANSACT_SYNC_RET_INT(remote, NOTIFY_CONTINUATION_RESULT_FROM_REMOTE, data, reply);
204 }
205 
ConnectRemoteAbility(const OHOS::AAFwk::Want & want,const sptr<IRemoteObject> & connect,int32_t callerUid,int32_t callerPid,uint32_t accessToken)206 int32_t DistributedSchedProxy::ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect,
207     int32_t callerUid, int32_t callerPid, uint32_t accessToken)
208 {
209     if (connect == nullptr) {
210         HILOGE("ConnectRemoteAbility connect is null");
211         return ERR_NULL_OBJECT;
212     }
213 
214     sptr<IRemoteObject> remote = Remote();
215     if (remote == nullptr) {
216         HILOGE("ConnectRemoteAbility remote is null");
217         return ERR_NULL_OBJECT;
218     }
219     MessageParcel data;
220     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
221         return ERR_FLATTEN_OBJECT;
222     }
223     PARCEL_WRITE_HELPER(data, Parcelable, &want);
224     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
225     PARCEL_WRITE_HELPER(data, Int32, callerUid);
226     PARCEL_WRITE_HELPER(data, Int32, callerPid);
227     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
228     MessageParcel reply;
229     PARCEL_TRANSACT_SYNC_RET_INT(remote, CONNECT_REMOTE_ABILITY, data, reply);
230 }
231 
DisconnectRemoteAbility(const sptr<IRemoteObject> & connect,int32_t callerUid,uint32_t accessToken)232 int32_t DistributedSchedProxy::DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid,
233     uint32_t accessToken)
234 {
235     if (connect == nullptr) {
236         HILOGE("DisconnectRemoteAbility connect is null");
237         return ERR_NULL_OBJECT;
238     }
239 
240     sptr<IRemoteObject> remote = Remote();
241     if (remote == nullptr) {
242         HILOGE("DisconnectRemoteAbility remote is null");
243         return ERR_NULL_OBJECT;
244     }
245     MessageParcel data;
246     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
247         return ERR_FLATTEN_OBJECT;
248     }
249     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
250     PARCEL_WRITE_HELPER(data, Int32, callerUid);
251     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
252     MessageParcel reply;
253     PARCEL_TRANSACT_SYNC_RET_INT(remote, DISCONNECT_REMOTE_ABILITY, data, reply);
254 }
255 
ConnectAbilityFromRemote(const OHOS::AAFwk::Want & want,const AppExecFwk::AbilityInfo & abilityInfo,const sptr<IRemoteObject> & connect,const CallerInfo & callerInfo,const AccountInfo & accountInfo)256 int32_t DistributedSchedProxy::ConnectAbilityFromRemote(const OHOS::AAFwk::Want& want,
257     const AppExecFwk::AbilityInfo& abilityInfo, const sptr<IRemoteObject>& connect,
258     const CallerInfo& callerInfo, const AccountInfo& accountInfo)
259 {
260     if (connect == nullptr) {
261         HILOGE("ConnectAbilityFromRemote connect is null");
262         return ERR_NULL_OBJECT;
263     }
264 
265     sptr<IRemoteObject> remote = Remote();
266     if (remote == nullptr) {
267         HILOGE("ConnectAbilityFromRemote remote is null");
268         return ERR_NULL_OBJECT;
269     }
270     MessageParcel data;
271     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
272         return ERR_FLATTEN_OBJECT;
273     }
274     PARCEL_WRITE_HELPER(data, Parcelable, &want);
275     AppExecFwk::CompatibleAbilityInfo compatibleAbilityInfo;
276     abilityInfo.ConvertToCompatiableAbilityInfo(compatibleAbilityInfo);
277     PARCEL_WRITE_HELPER(data, Parcelable, &compatibleAbilityInfo);
278     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
279     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
280     PARCEL_WRITE_HELPER(data, Int32, callerInfo.pid);
281     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
282     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
283     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
284     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
285     nlohmann::json extraInfoJson;
286     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = callerInfo.accessToken;
287     if (callerInfo.extraInfoJson.find(DMS_VERSION_ID) != callerInfo.extraInfoJson.end()) {
288         extraInfoJson[DMS_VERSION_ID] = callerInfo.extraInfoJson[DMS_VERSION_ID];
289     }
290     std::string extraInfo = extraInfoJson.dump();
291     PARCEL_WRITE_HELPER(data, String, extraInfo);
292     MessageParcel reply;
293     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
294     PARCEL_TRANSACT_SYNC_RET_INT(remote, CONNECT_ABILITY_FROM_REMOTE, data, reply);
295 }
296 
DisconnectAbilityFromRemote(const sptr<IRemoteObject> & connect,int32_t uid,const std::string & sourceDeviceId)297 int32_t DistributedSchedProxy::DisconnectAbilityFromRemote(const sptr<IRemoteObject>& connect,
298     int32_t uid, const std::string& sourceDeviceId)
299 {
300     if (connect == nullptr) {
301         HILOGE("DisconnectAbilityFromRemote connect is null");
302         return ERR_NULL_OBJECT;
303     }
304 
305     sptr<IRemoteObject> remote = Remote();
306     if (remote == nullptr) {
307         HILOGE("DisconnectAbilityFromRemote remote is null");
308         return ERR_NULL_OBJECT;
309     }
310     MessageParcel data;
311     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
312         return ERR_FLATTEN_OBJECT;
313     }
314     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
315     PARCEL_WRITE_HELPER(data, Int32, uid);
316     PARCEL_WRITE_HELPER(data, String, sourceDeviceId);
317     MessageParcel reply;
318     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
319     PARCEL_TRANSACT_SYNC_RET_INT(remote, DISCONNECT_ABILITY_FROM_REMOTE, data, reply);
320 }
321 
NotifyProcessDiedFromRemote(const CallerInfo & callerInfo)322 int32_t DistributedSchedProxy::NotifyProcessDiedFromRemote(const CallerInfo& callerInfo)
323 {
324     HILOGD("DistributedSchedProxy::NotifyProcessDiedFromRemote called");
325     sptr<IRemoteObject> remote = Remote();
326     if (remote == nullptr) {
327         HILOGE("NotifyProcessDiedFromRemote remote is null");
328         return ERR_NULL_OBJECT;
329     }
330     MessageParcel data;
331     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
332         return ERR_FLATTEN_OBJECT;
333     }
334     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
335     PARCEL_WRITE_HELPER(data, Int32, callerInfo.pid);
336     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
337     MessageParcel reply;
338     PARCEL_TRANSACT_SYNC_RET_INT(remote, NOTIFY_PROCESS_DIED_FROM_REMOTE, data, reply);
339 }
340 
341 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
StartSyncRemoteMissions(const std::string & devId,bool fixConflict,int64_t tag)342 int32_t DistributedSchedProxy::StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag)
343 {
344     HILOGI("called");
345     sptr<IRemoteObject> remote = Remote();
346     if (remote == nullptr) {
347         HILOGE("remote system ability is null");
348         return ERR_NULL_OBJECT;
349     }
350 
351     MessageParcel data;
352     MessageParcel reply;
353     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
354         return ERR_FLATTEN_OBJECT;
355     }
356     PARCEL_WRITE_HELPER(data, String16, Str8ToStr16(devId));
357     PARCEL_WRITE_HELPER(data, Bool, fixConflict);
358     PARCEL_WRITE_HELPER(data, Int64, tag);
359     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_SYNC_MISSIONS, data, reply);
360 }
361 
StartSyncMissionsFromRemote(const CallerInfo & callerInfo,std::vector<DstbMissionInfo> & missionInfos)362 int32_t DistributedSchedProxy::StartSyncMissionsFromRemote(const CallerInfo& callerInfo,
363     std::vector<DstbMissionInfo>& missionInfos)
364 {
365     HILOGI("called");
366     sptr<IRemoteObject> remote = Remote();
367     if (remote == nullptr) {
368         HILOGE("remote service is null");
369         return ERR_NULL_OBJECT;
370     }
371     MessageParcel data;
372     MessageParcel reply;
373     MessageOption option { MessageOption::TF_SYNC, WAIT_TIME };
374     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
375         return ERR_FLATTEN_OBJECT;
376     }
377     if (!CallerInfoMarshalling(callerInfo, data)) {
378         return ERR_FLATTEN_OBJECT;
379     }
380     int32_t error = remote->SendRequest(START_SYNC_MISSIONS_FROM_REMOTE, data, reply, option);
381     if (error != ERR_NONE) {
382         HILOGW("fail, error: %{public}d", error);
383         return error;
384     }
385     int32_t version = reply.ReadInt32();
386     HILOGD("version : %{public}d", version);
387     return DstbMissionInfo::ReadDstbMissionInfosFromParcel(reply, missionInfos) ? ERR_NONE : ERR_FLATTEN_OBJECT;
388 }
389 
StopSyncRemoteMissions(const std::string & devId)390 int32_t DistributedSchedProxy::StopSyncRemoteMissions(const std::string& devId)
391 {
392     HILOGI("called");
393     sptr<IRemoteObject> remote = Remote();
394     if (remote == nullptr) {
395         HILOGE("remote system ability is null");
396         return ERR_NULL_OBJECT;
397     }
398     MessageParcel data;
399     MessageParcel reply;
400     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
401         return ERR_FLATTEN_OBJECT;
402     }
403     PARCEL_WRITE_HELPER(data, String16, Str8ToStr16(devId));
404     PARCEL_TRANSACT_SYNC_RET_INT(remote, STOP_SYNC_MISSIONS, data, reply);
405 }
406 
StopSyncMissionsFromRemote(const CallerInfo & callerInfo)407 int32_t DistributedSchedProxy::StopSyncMissionsFromRemote(const CallerInfo& callerInfo)
408 {
409     HILOGI("called");
410     sptr<IRemoteObject> remote = Remote();
411     if (remote == nullptr) {
412         HILOGE("remote service is null");
413         return ERR_NULL_OBJECT;
414     }
415     MessageParcel data;
416     MessageParcel reply;
417     MessageOption option { MessageOption::TF_SYNC, WAIT_TIME };
418     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
419         return ERR_FLATTEN_OBJECT;
420     }
421     if (!CallerInfoMarshalling(callerInfo, data)) {
422         return ERR_FLATTEN_OBJECT;
423     }
424     int32_t error = remote->SendRequest(STOP_SYNC_MISSIONS_FROM_REMOTE, data, reply, option);
425     if (error != ERR_NONE) {
426         HILOGW("sendRequest fail, error: %{public}d", error);
427         return error;
428     }
429     return reply.ReadInt32();
430 }
431 
RegisterMissionListener(const std::u16string & devId,const sptr<IRemoteObject> & obj)432 int32_t DistributedSchedProxy::RegisterMissionListener(const std::u16string& devId,
433     const sptr<IRemoteObject>& obj)
434 {
435     HILOGI("RegisterMissionListener called");
436     sptr<IRemoteObject> remote = Remote();
437     if (remote == nullptr) {
438         HILOGE("remote system ability is null");
439         return ERR_NULL_OBJECT;
440     }
441     MessageParcel data;
442     MessageParcel reply;
443     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
444         return ERR_FLATTEN_OBJECT;
445     }
446     PARCEL_WRITE_HELPER(data, String16, devId);
447     PARCEL_WRITE_HELPER(data, RemoteObject, obj);
448     PARCEL_TRANSACT_SYNC_RET_INT(remote, REGISTER_MISSION_LISTENER, data, reply);
449 }
450 
UnRegisterMissionListener(const std::u16string & devId,const sptr<IRemoteObject> & obj)451 int32_t DistributedSchedProxy::UnRegisterMissionListener(const std::u16string& devId,
452     const sptr<IRemoteObject>& obj)
453 {
454     HILOGI("UnRegisterMissionListener called");
455     sptr<IRemoteObject> remote = Remote();
456     if (remote == nullptr) {
457         HILOGE("remote system ability is null");
458         return ERR_NULL_OBJECT;
459     }
460     MessageParcel data;
461     MessageParcel reply;
462     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
463         return ERR_FLATTEN_OBJECT;
464     }
465     PARCEL_WRITE_HELPER(data, String16, devId);
466     PARCEL_WRITE_HELPER(data, RemoteObject, obj);
467     PARCEL_TRANSACT_SYNC_RET_INT(remote, UNREGISTER_MISSION_LISTENER, data, reply);
468 }
469 
GetMissionInfos(const std::string & deviceId,int32_t numMissions,std::vector<AAFwk::MissionInfo> & missionInfos)470 int32_t DistributedSchedProxy::GetMissionInfos(const std::string& deviceId, int32_t numMissions,
471     std::vector<AAFwk::MissionInfo>& missionInfos)
472 {
473     HILOGI("called");
474     sptr<IRemoteObject> remote = Remote();
475     if (remote == nullptr) {
476         HILOGE("remote system ability is null");
477         return ERR_NULL_OBJECT;
478     }
479 
480     MessageParcel data;
481     MessageParcel reply;
482     MessageOption option;
483     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
484         return ERR_FLATTEN_OBJECT;
485     }
486     PARCEL_WRITE_HELPER(data, String16, Str8ToStr16(deviceId));
487     PARCEL_WRITE_HELPER(data, Int32, numMissions);
488     int32_t ret = remote->SendRequest(GET_MISSION_INFOS, data, reply, option);
489     if (ret != ERR_NONE) {
490         HILOGW("sendRequest fail, error: %{public}d", ret);
491         return ret;
492     }
493     return MissionInfoConverter::ReadMissionInfosFromParcel(reply, missionInfos) ? ERR_NONE : ERR_FLATTEN_OBJECT;
494 }
495 
NotifyMissionsChangedFromRemote(const std::vector<DstbMissionInfo> & missionInfos,const CallerInfo & callerInfo)496 int32_t DistributedSchedProxy::NotifyMissionsChangedFromRemote(const std::vector<DstbMissionInfo>& missionInfos,
497     const CallerInfo& callerInfo)
498 {
499     HILOGI("NotifyMissionsChangedFromRemote is called");
500     sptr<IRemoteObject> remote = Remote();
501     if (remote == nullptr) {
502         HILOGE("NotifyMissionsChangedFromRemote remote service is null");
503         return ERR_NULL_OBJECT;
504     }
505 
506     MessageParcel data;
507     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
508         return ERR_FLATTEN_OBJECT;
509     }
510     PARCEL_WRITE_HELPER(data, Int32, callerInfo.dmsVersion);
511     if (!DstbMissionInfo::WriteDstbMissionInfosToParcel(data, missionInfos)) {
512         return ERR_FLATTEN_OBJECT;
513     }
514     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
515     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
516     PARCEL_WRITE_HELPER(data, Int32, callerInfo.pid);
517     PARCEL_WRITE_HELPER(data, Int32, callerInfo.dmsVersion);
518     MessageParcel reply;
519     MessageOption option { MessageOption::TF_SYNC, WAIT_TIME };
520     int32_t error = remote->SendRequest(NOTIFY_MISSIONS_CHANGED_FROM_REMOTE, data, reply, option);
521     if (error != ERR_NONE) {
522         HILOGE("%{public}s transact failed, error: %{public}d", __func__, error);
523         return error;
524     }
525     int32_t result = reply.ReadInt32();
526     HILOGD("%{public}s get result from server data = %{public}d", __func__, result);
527     return result;
528 }
529 
GetRemoteMissionSnapshotInfo(const std::string & networkId,int32_t missionId,std::unique_ptr<MissionSnapshot> & missionSnapshot)530 int32_t DistributedSchedProxy::GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
531     std::unique_ptr<MissionSnapshot>& missionSnapshot)
532 {
533     if (networkId.empty()) {
534         HILOGE("networkId is null");
535         return ERR_NULL_OBJECT;
536     }
537     if (missionId < 0) {
538         HILOGE("missionId is invalid");
539         return INVALID_PARAMETERS_ERR;
540     }
541     sptr<IRemoteObject> remote = Remote();
542     if (remote == nullptr) {
543         HILOGE("remote is null");
544         return ERR_NULL_OBJECT;
545     }
546     MessageParcel data;
547     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
548         return ERR_FLATTEN_OBJECT;
549     }
550     PARCEL_WRITE_HELPER(data, String, networkId);
551     PARCEL_WRITE_HELPER(data, Int32, missionId);
552     MessageParcel reply;
553     MessageOption option;
554     int32_t error = remote->SendRequest(GET_REMOTE_MISSION_SNAPSHOT_INFO, data, reply, option);
555     if (error != ERR_NONE) {
556         HILOGE("transact failed, error: %{public}d", error);
557         return error;
558     }
559     std::unique_ptr<MissionSnapshot> missionSnapshotPtr(reply.ReadParcelable<MissionSnapshot>());
560     missionSnapshot = std::move(missionSnapshotPtr);
561     return ERR_NONE;
562 }
563 #endif
564 
CallerInfoMarshalling(const CallerInfo & callerInfo,MessageParcel & data)565 bool DistributedSchedProxy::CallerInfoMarshalling(const CallerInfo& callerInfo, MessageParcel& data)
566 {
567     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.uid, false);
568     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.pid, false);
569     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.callerType, false);
570     PARCEL_WRITE_HELPER_RET(data, String, callerInfo.sourceDeviceId, false);
571     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.duid, false);
572     PARCEL_WRITE_HELPER_RET(data, String, callerInfo.callerAppId, false);
573     PARCEL_WRITE_HELPER_RET(data, Int32, callerInfo.dmsVersion, false);
574     return true;
575 }
576 
StartRemoteAbilityByCall(const OHOS::AAFwk::Want & want,const sptr<IRemoteObject> & connect,int32_t callerUid,int32_t callerPid,uint32_t accessToken)577 int32_t DistributedSchedProxy::StartRemoteAbilityByCall(const OHOS::AAFwk::Want& want,
578     const sptr<IRemoteObject>& connect, int32_t callerUid, int32_t callerPid, uint32_t accessToken)
579 {
580     if (connect == nullptr) {
581         HILOGE("StartRemoteAbilityByCall connect is null");
582         return ERR_NULL_OBJECT;
583     }
584 
585     sptr<IRemoteObject> remote = Remote();
586     if (remote == nullptr) {
587         HILOGE("StartRemoteAbilityByCall remote is null");
588         return ERR_NULL_OBJECT;
589     }
590     MessageParcel data;
591     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
592         return ERR_FLATTEN_OBJECT;
593     }
594     PARCEL_WRITE_HELPER(data, Parcelable, &want);
595     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
596     PARCEL_WRITE_HELPER(data, Int32, callerUid);
597     PARCEL_WRITE_HELPER(data, Int32, callerPid);
598     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
599     MessageParcel reply;
600     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_REMOTE_ABILITY_BY_CALL, data, reply);
601 }
602 
ReleaseRemoteAbility(const sptr<IRemoteObject> & connect,const AppExecFwk::ElementName & element)603 int32_t DistributedSchedProxy::ReleaseRemoteAbility(const sptr<IRemoteObject>& connect,
604     const AppExecFwk::ElementName &element)
605 {
606     if (connect == nullptr) {
607         HILOGE("ReleaseRemoteAbility connect is null");
608         return ERR_NULL_OBJECT;
609     }
610 
611     sptr<IRemoteObject> remote = Remote();
612     if (remote == nullptr) {
613         HILOGE("ReleaseRemoteAbility remote is null");
614         return ERR_NULL_OBJECT;
615     }
616     MessageParcel data;
617     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
618         return ERR_FLATTEN_OBJECT;
619     }
620     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
621     if (!data.WriteParcelable(&element)) {
622         HILOGE("ReleaseRemoteAbility write element error.");
623         return ERR_INVALID_VALUE;
624     }
625     MessageParcel reply;
626     PARCEL_TRANSACT_SYNC_RET_INT(remote, RELEASE_REMOTE_ABILITY, data, reply);
627 }
628 
StartAbilityByCallFromRemote(const OHOS::AAFwk::Want & want,const sptr<IRemoteObject> & connect,const CallerInfo & callerInfo,const AccountInfo & accountInfo)629 int32_t DistributedSchedProxy::StartAbilityByCallFromRemote(const OHOS::AAFwk::Want& want,
630     const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo, const AccountInfo& accountInfo)
631 {
632     if (connect == nullptr) {
633         HILOGE("StartAbilityByCallFromRemote connect is null");
634         return ERR_NULL_OBJECT;
635     }
636 
637     sptr<IRemoteObject> remote = Remote();
638     if (remote == nullptr) {
639         HILOGE("StartAbilityByCallFromRemote remote is null");
640         return ERR_NULL_OBJECT;
641     }
642     MessageParcel data;
643     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
644         return ERR_FLATTEN_OBJECT;
645     }
646     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
647     PARCEL_WRITE_HELPER(data, Int32, callerInfo.uid);
648     PARCEL_WRITE_HELPER(data, Int32, callerInfo.pid);
649     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
650     PARCEL_WRITE_HELPER(data, Int32, accountInfo.accountType);
651     PARCEL_WRITE_HELPER(data, StringVector, accountInfo.groupIdList);
652     PARCEL_WRITE_HELPER(data, String, callerInfo.callerAppId);
653     nlohmann::json extraInfoJson;
654     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = callerInfo.accessToken;
655     if (callerInfo.extraInfoJson.find(DMS_VERSION_ID) != callerInfo.extraInfoJson.end()) {
656         extraInfoJson[DMS_VERSION_ID] = callerInfo.extraInfoJson[DMS_VERSION_ID];
657     }
658     std::string extraInfo = extraInfoJson.dump();
659     PARCEL_WRITE_HELPER(data, String, extraInfo);
660     PARCEL_WRITE_HELPER(data, Parcelable, &want);
661     MessageParcel reply;
662     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
663     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_ABILITY_BY_CALL_FROM_REMOTE, data, reply);
664 }
665 
ReleaseAbilityFromRemote(const sptr<IRemoteObject> & connect,const AppExecFwk::ElementName & element,const CallerInfo & callerInfo)666 int32_t DistributedSchedProxy::ReleaseAbilityFromRemote(const sptr<IRemoteObject>& connect,
667     const AppExecFwk::ElementName &element, const CallerInfo& callerInfo)
668 {
669     if (connect == nullptr) {
670         HILOGE("ReleaseAbilityFromRemote connect is null");
671         return ERR_NULL_OBJECT;
672     }
673 
674     sptr<IRemoteObject> remote = Remote();
675     if (remote == nullptr) {
676         HILOGE("ReleaseAbilityFromRemote remote is null");
677         return ERR_NULL_OBJECT;
678     }
679     MessageParcel data;
680     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
681         return ERR_FLATTEN_OBJECT;
682     }
683     PARCEL_WRITE_HELPER(data, RemoteObject, connect);
684     if (!data.WriteParcelable(&element)) {
685         HILOGE("ReleaseAbilityFromRemote write element error.");
686         return ERR_INVALID_VALUE;
687     }
688     PARCEL_WRITE_HELPER(data, String, callerInfo.sourceDeviceId);
689     std::string extraInfo = "";
690     PARCEL_WRITE_HELPER(data, String, extraInfo);
691     MessageParcel reply;
692     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
693     PARCEL_TRANSACT_SYNC_RET_INT(remote, RELEASE_ABILITY_FROM_REMOTE, data, reply);
694 }
695 
StartRemoteFreeInstall(const OHOS::AAFwk::Want & want,int32_t callerUid,int32_t requestCode,uint32_t accessToken,const sptr<IRemoteObject> & callback)696 int32_t DistributedSchedProxy::StartRemoteFreeInstall(const OHOS::AAFwk::Want& want,
697     int32_t callerUid, int32_t requestCode, uint32_t accessToken, const sptr<IRemoteObject>& callback)
698 {
699     HILOGD("called.");
700     if (callback == nullptr) {
701         HILOGE("ContinueMission callback null");
702         return ERR_NULL_OBJECT;
703     }
704 
705     sptr<IRemoteObject> remote = Remote();
706     if (remote == nullptr) {
707         HILOGE("remote callback null");
708         return ERR_NULL_OBJECT;
709     }
710 
711     MessageParcel data;
712     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
713         HILOGE("write interface token null");
714         return ERR_FLATTEN_OBJECT;
715     }
716 
717     PARCEL_WRITE_HELPER(data, Parcelable, &want);
718     PARCEL_WRITE_HELPER(data, Int32, callerUid);
719     PARCEL_WRITE_HELPER(data, Int32, requestCode);
720     PARCEL_WRITE_HELPER(data, Uint32, accessToken);
721     PARCEL_WRITE_HELPER(data, RemoteObject, callback);
722     MessageParcel reply;
723     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_REMOTE_FREE_INSTALL, data, reply);
724 }
725 
StartFreeInstallFromRemote(const FreeInstallInfo & info,int64_t taskId)726 int32_t DistributedSchedProxy::StartFreeInstallFromRemote(const FreeInstallInfo& info, int64_t taskId)
727 {
728     HILOGD("called.");
729     sptr<IRemoteObject> remote = Remote();
730     if (remote == nullptr) {
731         HILOGE("remote is null");
732         return ERR_NULL_OBJECT;
733     }
734 
735     MessageParcel data;
736     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
737         HILOGE("write interface token null");
738         return ERR_FLATTEN_OBJECT;
739     }
740 
741     PARCEL_WRITE_HELPER(data, Parcelable, &info.want);
742     PARCEL_WRITE_HELPER(data, Int32, info.callerInfo.uid);
743     PARCEL_WRITE_HELPER(data, String, info.callerInfo.sourceDeviceId);
744     PARCEL_WRITE_HELPER(data, Int32, info.accountInfo.accountType);
745     PARCEL_WRITE_HELPER(data, StringVector, info.accountInfo.groupIdList);
746     PARCEL_WRITE_HELPER(data, String, info.callerInfo.callerAppId);
747     PARCEL_WRITE_HELPER(data, Int64, taskId);
748     OHOS::AAFwk::Want cmpWant;
749     cmpWant.SetParam(CMPT_PARAM_FREEINSTALL_BUNDLENAMES, info.callerInfo.bundleNames);
750     PARCEL_WRITE_HELPER(data, Parcelable, &cmpWant);
751     nlohmann::json extraInfoJson;
752     extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN] = info.callerInfo.accessToken;
753     extraInfoJson[EXTRO_INFO_JSON_KEY_REQUEST_CODE] = info.requestCode;
754     if (info.callerInfo.extraInfoJson.find(DMS_VERSION_ID) != info.callerInfo.extraInfoJson.end()) {
755         extraInfoJson[DMS_VERSION_ID] = info.callerInfo.extraInfoJson[DMS_VERSION_ID];
756     }
757     std::string extraInfo = extraInfoJson.dump();
758     PARCEL_WRITE_HELPER(data, String, extraInfo);
759     MessageParcel reply;
760     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::REMOTE_PROCEDURE_CALL);
761     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_FREE_INSTALL_FROM_REMOTE, data, reply);
762 }
763 
NotifyCompleteFreeInstallFromRemote(int64_t taskId,int32_t resultCode)764 int32_t DistributedSchedProxy::NotifyCompleteFreeInstallFromRemote(int64_t taskId, int32_t resultCode)
765 {
766     HILOGD("called.");
767     sptr<IRemoteObject> remote = Remote();
768     if (remote == nullptr) {
769         HILOGE("remote is null");
770         return ERR_NULL_OBJECT;
771     }
772 
773     MessageParcel data;
774     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
775         HILOGE("write interface token null");
776         return ERR_FLATTEN_OBJECT;
777     }
778 
779     PARCEL_WRITE_HELPER(data, Int64, taskId);
780     PARCEL_WRITE_HELPER(data, Int32, resultCode);
781     MessageParcel reply;
782     PARCEL_TRANSACT_SYNC_RET_INT(remote, NOTIFY_COMPLETE_FREE_INSTALL_FROM_REMOTE, data, reply);
783 }
784 
785 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
StartRemoteShareForm(const std::string & remoteDeviceId,const AppExecFwk::FormShareInfo & formShareInfo)786 int32_t DistributedSchedProxy::StartRemoteShareForm(
787     const std::string& remoteDeviceId, const AppExecFwk::FormShareInfo& formShareInfo)
788 {
789     HILOGD("SHAREFORM:: func call");
790     if (remoteDeviceId.empty()) {
791         HILOGE("input params deviceId is nullptr.");
792         return INVALID_PARAMETERS_ERR;
793     }
794 
795     auto remote = Remote();
796     if (remote == nullptr) {
797         HILOGE("fail to get dms remote.");
798         return DMSPROXY_SERVICE_ERR;
799     }
800 
801     MessageParcel data;
802     MessageParcel reply;
803     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
804         HILOGE("WriteInterfaceToken failed");
805         return ERR_FLATTEN_OBJECT;
806     }
807 
808     PARCEL_WRITE_HELPER(data, String, remoteDeviceId);
809     PARCEL_WRITE_HELPER(data, Parcelable, &formShareInfo);
810 
811     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_REMOTE_SHARE_FORM, data, reply);
812 }
813 
StartShareFormFromRemote(const std::string & remoteDeviceId,const AppExecFwk::FormShareInfo & formShareInfo)814 int32_t DistributedSchedProxy::StartShareFormFromRemote(
815     const std::string& remoteDeviceId, const AppExecFwk::FormShareInfo& formShareInfo)
816 {
817     HILOGD("SHAREFORM:: func call");
818     if (remoteDeviceId.empty()) {
819         HILOGE("input params deviceId is nullptr.");
820         return INVALID_PARAMETERS_ERR;
821     }
822 
823     auto remote = Remote();
824     if (remote == nullptr) {
825         HILOGE("fail to get dms remote.");
826         return DMSPROXY_SERVICE_ERR;
827     }
828 
829     MessageParcel data;
830     MessageParcel reply;
831     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
832         HILOGE("WriteInterfaceToken failed");
833         return ERR_FLATTEN_OBJECT;
834     }
835 
836     PARCEL_WRITE_HELPER(data, String, remoteDeviceId);
837     PARCEL_WRITE_HELPER(data, Parcelable, &formShareInfo);
838 
839     PARCEL_TRANSACT_SYNC_RET_INT(remote, START_SHARE_FORM_FROM_REMOTE, data, reply);
840 }
841 #endif
842 
GetDistributedComponentList(std::vector<std::string> & distributedComponents)843 int32_t DistributedSchedProxy::GetDistributedComponentList(std::vector<std::string>& distributedComponents)
844 {
845     sptr<IRemoteObject> remote = Remote();
846     if (remote == nullptr) {
847         HILOGE("GetDistributedComponentList remote is null");
848         return ERR_NULL_OBJECT;
849     }
850     MessageParcel data;
851     if (!data.WriteInterfaceToken(DMS_PROXY_INTERFACE_TOKEN)) {
852         return ERR_FLATTEN_OBJECT;
853     }
854     MessageParcel reply;
855     MessageOption option;
856     int32_t error = remote->SendRequest(GET_DISTRIBUTED_COMPONENT_LIST, data, reply, option);
857     if (error != ERR_NONE) {
858         HILOGE("GetDistributedComponentList SendRequest error = %{public}d", error);
859         return error;
860     }
861     int32_t result = reply.ReadInt32();
862     if (result != ERR_NONE) {
863         HILOGE("GetDistributedComponentList result = %{public}d", result);
864         return result;
865     }
866     PARCEL_READ_HELPER(reply, StringVector, &distributedComponents);
867     return ERR_NONE;
868 }
869 } // namespace DistributedSchedule
870 } // namespace OHOS
871 
872