• 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_stub.h"
17 
18 #include "ability_info.h"
19 #include "adapter/dnetwork_adapter.h"
20 #include "caller_info.h"
21 #include "datetime_ex.h"
22 #include "dfx/dms_hisysevent_report.h"
23 #include "dfx/dms_hitrace_chain.h"
24 #include "dfx/dms_hitrace_constants.h"
25 #include "distributed_sched_permission.h"
26 #include "dtbschedmgr_log.h"
27 #include "dtbschedmgr_device_info_storage.h"
28 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
29 #include "image_source.h"
30 #include "mission/distributed_sched_mission_manager.h"
31 #include "mission/mission_info_converter.h"
32 #include "mission/snapshot_converter.h"
33 #endif
34 #include "ipc_skeleton.h"
35 #include "message_parcel.h"
36 
37 #include "parcel_helper.h"
38 
39 namespace OHOS {
40 namespace DistributedSchedule {
41 using namespace std;
42 using namespace AAFwk;
43 using namespace AppExecFwk;
44 
45 namespace {
46 constexpr int32_t HID_HAP = 10000; /* first hap user */
47 const std::string TAG = "DistributedSchedStub";
48 const std::u16string DMS_STUB_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
49 const std::string EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID";
50 const std::string EXTRO_INFO_JSON_KEY_REQUEST_CODE = "requestCode";
51 const std::string PERMISSION_DISTRIBUTED_DATASYNC = "ohos.permission.DISTRIBUTED_DATASYNC";
52 const std::string PARAM_FREEINSTALL_APPID = "ohos.freeinstall.params.callingAppId";
53 const std::string PARAM_FREEINSTALL_BUNDLENAMES = "ohos.freeinstall.params.callingBundleNames";
54 const std::string CMPT_PARAM_FREEINSTALL_BUNDLENAMES = "ohos.extra.param.key.allowedBundles";
55 const std::string DMS_VERSION_ID = "dmsVersion";
56 const int DEFAULT_REQUEST_CODE = -1;
57 }
58 
DistributedSchedStub()59 DistributedSchedStub::DistributedSchedStub()
60 {
61     localFuncsMap_[START_REMOTE_ABILITY] = &DistributedSchedStub::StartRemoteAbilityInner;
62     localFuncsMap_[CONTINUE_MISSION] = &DistributedSchedStub::ContinueMissionInner;
63     localFuncsMap_[START_CONTINUATION] = &DistributedSchedStub::StartContinuationInner;
64     localFuncsMap_[NOTIFY_COMPLETE_CONTINUATION] = &DistributedSchedStub::NotifyCompleteContinuationInner;
65     localFuncsMap_[CONNECT_REMOTE_ABILITY] = &DistributedSchedStub::ConnectRemoteAbilityInner;
66     localFuncsMap_[DISCONNECT_REMOTE_ABILITY] = &DistributedSchedStub::DisconnectRemoteAbilityInner;
67     // request codes for mission manager
68 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
69     localFuncsMap_[GET_REMOTE_MISSION_SNAPSHOT_INFO] = &DistributedSchedStub::GetRemoteMissionSnapshotInfoInner;
70     localFuncsMap_[REGISTER_MISSION_LISTENER] = &DistributedSchedStub::RegisterMissionListenerInner;
71     localFuncsMap_[UNREGISTER_MISSION_LISTENER] = &DistributedSchedStub::UnRegisterMissionListenerInner;
72     localFuncsMap_[GET_MISSION_INFOS] = &DistributedSchedStub::GetMissionInfosInner;
73     localFuncsMap_[START_SYNC_MISSIONS] = &DistributedSchedStub::StartSyncRemoteMissionsInner;
74     localFuncsMap_[STOP_SYNC_MISSIONS] = &DistributedSchedStub::StopSyncRemoteMissionsInner;
75 #endif
76     remoteFuncsMap_[START_ABILITY_FROM_REMOTE] = &DistributedSchedStub::StartAbilityFromRemoteInner;
77     remoteFuncsMap_[SEND_RESULT_FROM_REMOTE] = &DistributedSchedStub::SendResultFromRemoteInner;
78     remoteFuncsMap_[NOTIFY_CONTINUATION_RESULT_FROM_REMOTE] =
79         &DistributedSchedStub::NotifyContinuationResultFromRemoteInner;
80     remoteFuncsMap_[CONNECT_ABILITY_FROM_REMOTE] = &DistributedSchedStub::ConnectAbilityFromRemoteInner;
81     remoteFuncsMap_[DISCONNECT_ABILITY_FROM_REMOTE] = &DistributedSchedStub::DisconnectAbilityFromRemoteInner;
82     remoteFuncsMap_[NOTIFY_PROCESS_DIED_FROM_REMOTE] = &DistributedSchedStub::NotifyProcessDiedFromRemoteInner;
83 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
84     // request codes for mission manager
85     remoteFuncsMap_[START_SYNC_MISSIONS_FROM_REMOTE] =
86         &DistributedSchedStub::StartSyncMissionsFromRemoteInner;
87     remoteFuncsMap_[STOP_SYNC_MISSIONS_FROM_REMOTE] =
88         &DistributedSchedStub::StopSyncMissionsFromRemoteInner;
89     remoteFuncsMap_[NOTIFY_MISSIONS_CHANGED_FROM_REMOTE] = &DistributedSchedStub::NotifyMissionsChangedFromRemoteInner;
90 #endif
91     remoteFuncsMap_[CONTINUE_MISSION] = &DistributedSchedStub::ContinueMissionInner;
92     // request codes for call ability
93     localFuncsMap_[START_REMOTE_ABILITY_BY_CALL] = &DistributedSchedStub::StartRemoteAbilityByCallInner;
94     localFuncsMap_[RELEASE_REMOTE_ABILITY] = &DistributedSchedStub::ReleaseRemoteAbilityInner;
95     remoteFuncsMap_[START_ABILITY_BY_CALL_FROM_REMOTE] = &DistributedSchedStub::StartAbilityByCallFromRemoteInner;
96     remoteFuncsMap_[RELEASE_ABILITY_FROM_REMOTE] = &DistributedSchedStub::ReleaseAbilityFromRemoteInner;
97 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
98     localFuncsMap_[START_REMOTE_SHARE_FORM] = &DistributedSchedStub::StartRemoteShareFormInner;
99     remoteFuncsMap_[START_SHARE_FORM_FROM_REMOTE] = &DistributedSchedStub::StartShareFormFromRemoteInner;
100 #endif
101     localFuncsMap_[GET_DISTRIBUTED_COMPONENT_LIST] = &DistributedSchedStub::GetDistributedComponentListInner;
102     localFuncsMap_[START_REMOTE_FREE_INSTALL] = &DistributedSchedStub::StartRemoteFreeInstallInner;
103     remoteFuncsMap_[START_FREE_INSTALL_FROM_REMOTE] = &DistributedSchedStub::StartFreeInstallFromRemoteInner;
104     remoteFuncsMap_[NOTIFY_COMPLETE_FREE_INSTALL_FROM_REMOTE] =
105         &DistributedSchedStub::NotifyCompleteFreeInstallFromRemoteInner;
106 }
107 
~DistributedSchedStub()108 DistributedSchedStub::~DistributedSchedStub()
109 {
110     remoteFuncsMap_.clear();
111     localFuncsMap_.clear();
112 }
113 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)114 int32_t DistributedSchedStub::OnRemoteRequest(uint32_t code,
115     MessageParcel& data, MessageParcel& reply, MessageOption& option)
116 {
117     bool IsLocalCalling = IPCSkeleton::IsLocalCalling();
118     HILOGI("OnRemoteRequest, code = %{public}d, flags = %{public}d, IsLocalCalling = %{public}d.",
119         code, option.GetFlags(), IsLocalCalling);
120 
121     const auto& funcsMap = IsLocalCalling ? localFuncsMap_ : remoteFuncsMap_;
122     auto iter = funcsMap.find(code);
123     if (iter != funcsMap.end()) {
124         auto func = iter->second;
125         if (!EnforceInterfaceToken(data)) {
126             HILOGW("OnRemoteRequest interface token check failed!");
127             return DMS_PERMISSION_DENIED;
128         }
129         if (func != nullptr) {
130             return (this->*func)(data, reply);
131         }
132     }
133 
134     HILOGW("OnRemoteRequest default case, need check.");
135     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
136 }
137 
StartRemoteAbilityInner(MessageParcel & data,MessageParcel & reply)138 int32_t DistributedSchedStub::StartRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
139 {
140     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
141         return DMS_PERMISSION_DENIED;
142     }
143     DmsHiTraceChain hiTraceChain(TraceValue::START_REMOTE_ABILITY);
144     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::START_REMOTE_ABILITY);
145     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
146     if (want == nullptr) {
147         HILOGW("START_ABILITY want readParcelable failed!");
148         return ERR_NULL_OBJECT;
149     }
150     int32_t callerUid = 0;
151     PARCEL_READ_HELPER(data, Int32, callerUid);
152     int32_t requestCode = 0;
153     PARCEL_READ_HELPER(data, Int32, requestCode);
154     uint32_t accessToken = 0;
155     PARCEL_READ_HELPER(data, Uint32, accessToken);
156     HILOGD("get callerUid = %d, AccessTokenID = %u", callerUid, accessToken);
157     if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
158         PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
159         HILOGE("check data_sync permission failed!");
160         return DMS_PERMISSION_DENIED;
161     }
162     int32_t result = StartRemoteAbility(*want, callerUid, requestCode, accessToken);
163     BehaviorEventParam eventParam = { EventCallingType::LOCAL, BehaviorEvent::START_REMOTE_ABILITY, result,
164         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerUid };
165     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
166     HILOGI("StartRemoteAbilityInner result = %{public}d", result);
167     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
168 }
169 
StartAbilityFromRemoteInner(MessageParcel & data,MessageParcel & reply)170 int32_t DistributedSchedStub::StartAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
171 {
172     if (!CheckCallingUid()) {
173         HILOGW("request DENIED!");
174         return DMS_PERMISSION_DENIED;
175     }
176 
177     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
178     if (want == nullptr) {
179         HILOGW("want readParcelable failed!");
180         return ERR_NULL_OBJECT;
181     }
182     unique_ptr<CompatibleAbilityInfo> cmpAbilityInfo(data.ReadParcelable<CompatibleAbilityInfo>());
183     if (cmpAbilityInfo == nullptr) {
184         HILOGW("AbilityInfo readParcelable failed!");
185         return ERR_NULL_OBJECT;
186     }
187     AbilityInfo abilityInfo;
188     cmpAbilityInfo->ConvertToAbilityInfo(abilityInfo);
189     std::string package = abilityInfo.bundleName;
190     std::string deviceId = abilityInfo.deviceId;
191     int64_t begin = GetTickCount();
192     int32_t requestCode = 0;
193     PARCEL_READ_HELPER(data, Int32, requestCode);
194     CallerInfo callerInfo;
195     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
196     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
197     callerInfo.callerType = CALLER_TYPE_HARMONY;
198     AccountInfo accountInfo;
199     accountInfo.accountType = data.ReadInt32();
200     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
201     callerInfo.callerAppId = data.ReadString();
202     std::string extraInfo = data.ReadString();
203     if (extraInfo.empty()) {
204         HILOGD("extra info is empty!");
205     }
206     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
207     if (!extraInfoJson.is_discarded()) {
208         SaveExtraInfo(extraInfoJson, callerInfo);
209         HILOGD("parse extra info");
210     }
211     int32_t result = StartAbilityFromRemote(*want, abilityInfo, requestCode, callerInfo, accountInfo);
212     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::START_REMOTE_ABILITY, result,
213         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerInfo.uid };
214     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
215     HILOGI("result = %{public}d", result);
216     PARCEL_WRITE_HELPER(reply, Int32, result);
217     int64_t end = GetTickCount();
218     PARCEL_WRITE_HELPER(reply, Int64, end - begin);
219     PARCEL_WRITE_HELPER(reply, String, package);
220     PARCEL_WRITE_HELPER(reply, String, deviceId);
221     return ERR_NONE;
222 }
223 
SaveExtraInfo(const nlohmann::json & extraInfoJson,CallerInfo & callerInfo)224 void DistributedSchedStub::SaveExtraInfo(const nlohmann::json& extraInfoJson, CallerInfo& callerInfo)
225 {
226     uint32_t accessToken = extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN];
227     callerInfo.accessToken = accessToken;
228     HILOGD("parse extra info, accessTokenID = %u", accessToken);
229     if (extraInfoJson.find(DMS_VERSION_ID) != extraInfoJson.end()) {
230         std::string dmsVersion = extraInfoJson[DMS_VERSION_ID];
231         callerInfo.extraInfoJson[DMS_VERSION_ID] = dmsVersion;
232         HILOGD("save dms version");
233     }
234 }
235 
SendResultFromRemoteInner(MessageParcel & data,MessageParcel & reply)236 int32_t DistributedSchedStub::SendResultFromRemoteInner(MessageParcel& data, MessageParcel& reply)
237 {
238     if (!CheckCallingUid()) {
239         HILOGW("request DENIED!");
240         return DMS_PERMISSION_DENIED;
241     }
242     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
243     if (want == nullptr) {
244         HILOGW("want readParcelable failed!");
245         return ERR_NULL_OBJECT;
246     }
247     int64_t begin = GetTickCount();
248     int32_t requestCode = 0;
249     PARCEL_READ_HELPER(data, Int32, requestCode);
250     CallerInfo callerInfo;
251     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
252     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
253     callerInfo.callerType = CALLER_TYPE_HARMONY;
254     AccountInfo accountInfo;
255     accountInfo.accountType = data.ReadInt32();
256     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
257     callerInfo.callerAppId = data.ReadString();
258     int32_t resultCode = 0;
259     PARCEL_READ_HELPER(data, Int32, resultCode);
260     std::string extraInfo = data.ReadString();
261     if (extraInfo.empty()) {
262         HILOGD("extra info is empty!");
263     }
264     callerInfo.extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
265     int32_t result = SendResultFromRemote(*want, requestCode, callerInfo, accountInfo, resultCode);
266     HILOGI("result = %{public}d", result);
267     PARCEL_WRITE_HELPER(reply, Int32, result);
268     int64_t end = GetTickCount();
269     PARCEL_WRITE_HELPER(reply, Int64, end - begin);
270     return ERR_NONE;
271 }
272 
ContinueMissionInner(MessageParcel & data,MessageParcel & reply)273 int32_t DistributedSchedStub::ContinueMissionInner(MessageParcel& data, MessageParcel& reply)
274 {
275     bool isLocalCalling = IPCSkeleton::IsLocalCalling();
276     if ((isLocalCalling && !DistributedSchedPermission::GetInstance().IsFoundationCall()) ||
277         (!isLocalCalling && !CheckCallingUid())) {
278         HILOGE("check permission failed!");
279         return DMS_PERMISSION_DENIED;
280     }
281 
282     std::string srcDevId;
283     std::string dstDevId;
284     PARCEL_READ_HELPER(data, String, srcDevId);
285     PARCEL_READ_HELPER(data, String, dstDevId);
286     int32_t missionId = 0;
287     PARCEL_READ_HELPER(data, Int32, missionId);
288     sptr<IRemoteObject> callback = data.ReadRemoteObject();
289     if (callback == nullptr) {
290         HILOGW("read callback failed!");
291         return ERR_NULL_OBJECT;
292     }
293     shared_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
294     if (wantParams == nullptr) {
295         HILOGW("wantParams readParcelable failed!");
296         return ERR_NULL_OBJECT;
297     }
298     int32_t result = ContinueMission(srcDevId, dstDevId, missionId, callback, *wantParams);
299     HILOGI("result = %{public}d", result);
300     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
301 }
302 
StartContinuationInner(MessageParcel & data,MessageParcel & reply)303 int32_t DistributedSchedStub::StartContinuationInner(MessageParcel& data, MessageParcel& reply)
304 {
305     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
306         return DMS_PERMISSION_DENIED;
307     }
308     DmsHiTraceChain hiTraceChain(TraceValue::START_CONTINUATION);
309     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::START_CONTINUATION);
310     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
311     if (want == nullptr) {
312         HILOGW("want readParcelable failed!");
313         return ERR_NULL_OBJECT;
314     }
315     int32_t missionId = data.ReadInt32();
316     int32_t callerUid = data.ReadInt32();
317     int32_t status = data.ReadInt32();
318     uint32_t accessToken = 0;
319     PARCEL_READ_HELPER(data, Uint32, accessToken);
320     HILOGI("get AccessTokenID = %{public}u", accessToken);
321     if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
322         PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
323         HILOGE("check data_sync permission failed!");
324         return DMS_PERMISSION_DENIED;
325     }
326     int32_t result = StartContinuation(*want, missionId, callerUid, status, accessToken);
327     BehaviorEventParam eventParam = { EventCallingType::LOCAL, BehaviorEvent::START_CONTINUATION, result,
328         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerUid };
329     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
330     HILOGI("result = %{public}d", result);
331     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
332 }
333 
NotifyCompleteContinuationInner(MessageParcel & data,MessageParcel & reply)334 int32_t DistributedSchedStub::NotifyCompleteContinuationInner(MessageParcel& data,
335     [[maybe_unused]] MessageParcel& reply)
336 {
337     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
338         HILOGE("check permission failed!");
339         return DMS_PERMISSION_DENIED;
340     }
341 
342     u16string devId = data.ReadString16();
343     if (devId.empty()) {
344         HILOGE("devId is empty!");
345         return INVALID_PARAMETERS_ERR;
346     }
347     int32_t sessionId = 0;
348     PARCEL_READ_HELPER(data, Int32, sessionId);
349     bool continuationResult = false;
350     PARCEL_READ_HELPER(data, Bool, continuationResult);
351     NotifyCompleteContinuation(devId, sessionId, continuationResult);
352     return ERR_OK;
353 }
354 
NotifyContinuationResultFromRemoteInner(MessageParcel & data,MessageParcel & reply)355 int32_t DistributedSchedStub::NotifyContinuationResultFromRemoteInner(MessageParcel& data,
356     [[maybe_unused]] MessageParcel& reply)
357 {
358     if (!CheckCallingUid()) {
359         HILOGW("request DENIED!");
360         return DMS_PERMISSION_DENIED;
361     }
362 
363     int32_t sessionId = 0;
364     PARCEL_READ_HELPER(data, Int32, sessionId);
365     bool continuationResult = false;
366     PARCEL_READ_HELPER(data, Bool, continuationResult);
367     return NotifyContinuationResultFromRemote(sessionId, continuationResult);
368 }
369 
ConnectRemoteAbilityInner(MessageParcel & data,MessageParcel & reply)370 int32_t DistributedSchedStub::ConnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
371 {
372     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
373         return DMS_PERMISSION_DENIED;
374     }
375     DmsHiTraceChain hiTraceChain(TraceValue::CONNECT_REMOTE_ABILITY);
376     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::CONNECT_REMOTE_ABILITY);
377     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
378     if (want == nullptr) {
379         HILOGW("want readParcelable failed!");
380         return ERR_NULL_OBJECT;
381     }
382     sptr<IRemoteObject> connect = data.ReadRemoteObject();
383     int32_t callerUid = 0;
384     PARCEL_READ_HELPER(data, Int32, callerUid);
385     int32_t callerPid = 0;
386     PARCEL_READ_HELPER(data, Int32, callerPid);
387     uint32_t accessToken = 0;
388     PARCEL_READ_HELPER(data, Uint32, accessToken);
389     HILOGD("get callerUid = %d, callerPid = %d, AccessTokenID = %u", callerUid, callerPid,
390         accessToken);
391     if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
392         PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
393         HILOGE("check data_sync permission failed!");
394         return DMS_PERMISSION_DENIED;
395     }
396     int32_t result = ConnectRemoteAbility(*want, connect, callerUid, callerPid, accessToken);
397     BehaviorEventParam eventParam = { EventCallingType::LOCAL, BehaviorEvent::CONNECT_REMOTE_ABILITY, result,
398         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerUid };
399     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
400     HILOGI("result = %{public}d", result);
401     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
402 }
403 
DisconnectRemoteAbilityInner(MessageParcel & data,MessageParcel & reply)404 int32_t DistributedSchedStub::DisconnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
405 {
406     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
407         return DMS_PERMISSION_DENIED;
408     }
409     DmsHiTraceChain hiTraceChain(TraceValue::DISCONNECT_REMOTE_ABILITY);
410     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::DISCONNECT_REMOTE_ABILITY);
411     sptr<IRemoteObject> connect = data.ReadRemoteObject();
412     int32_t callerUid = 0;
413     PARCEL_READ_HELPER(data, Int32, callerUid);
414     uint32_t accessToken = 0;
415     PARCEL_READ_HELPER(data, Uint32, accessToken);
416     HILOGD("get callerUid = %d, AccessTokenID = %u", callerUid, accessToken);
417     if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
418         PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
419         HILOGE("check data_sync permission failed!");
420         return DMS_PERMISSION_DENIED;
421     }
422     int32_t result = DisconnectRemoteAbility(connect, callerUid, accessToken);
423     BehaviorEventParam eventParam = { EventCallingType::LOCAL, BehaviorEvent::DISCONNECT_REMOTE_ABILITY, result };
424     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
425     HILOGI("result = %{public}d", result);
426     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
427 }
428 
ConnectAbilityFromRemoteInner(MessageParcel & data,MessageParcel & reply)429 int32_t DistributedSchedStub::ConnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
430 {
431     if (!CheckCallingUid()) {
432         HILOGW("request DENIED!");
433         return DMS_PERMISSION_DENIED;
434     }
435 
436     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
437     if (want == nullptr) {
438         HILOGW("want readParcelable failed!");
439         return ERR_NULL_OBJECT;
440     }
441     unique_ptr<CompatibleAbilityInfo> cmpAbilityInfo(data.ReadParcelable<CompatibleAbilityInfo>());
442     if (cmpAbilityInfo == nullptr) {
443         HILOGW("abilityInfo readParcelable failed!");
444         return ERR_NULL_OBJECT;
445     }
446     AbilityInfo abilityInfo;
447     cmpAbilityInfo->ConvertToAbilityInfo(abilityInfo);
448     sptr<IRemoteObject> connect = data.ReadRemoteObject();
449     CallerInfo callerInfo;
450     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
451     PARCEL_READ_HELPER(data, Int32, callerInfo.pid);
452     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
453     callerInfo.callerType = CALLER_TYPE_HARMONY;
454     AccountInfo accountInfo;
455     accountInfo.accountType = data.ReadInt32();
456     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
457     callerInfo.callerAppId = data.ReadString();
458     std::string extraInfo = data.ReadString();
459     if (extraInfo.empty()) {
460         HILOGD("extra info is empty!");
461     }
462     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
463     if (!extraInfoJson.is_discarded()) {
464         SaveExtraInfo(extraInfoJson, callerInfo);
465         HILOGD("parse extra info");
466     }
467     std::string package = abilityInfo.bundleName;
468     std::string deviceId = abilityInfo.deviceId;
469     int64_t begin = GetTickCount();
470     int32_t result = ConnectAbilityFromRemote(*want, abilityInfo, connect, callerInfo, accountInfo);
471     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::CONNECT_REMOTE_ABILITY, result,
472         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerInfo.uid };
473     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
474     HILOGW("result = %{public}d", result);
475     int64_t end = GetTickCount();
476     PARCEL_WRITE_HELPER(reply, Int32, result);
477     PARCEL_WRITE_HELPER(reply, Int64, end - begin);
478     PARCEL_WRITE_HELPER(reply, String, package);
479     PARCEL_WRITE_HELPER(reply, String, deviceId);
480     return ERR_NONE;
481 }
482 
DisconnectAbilityFromRemoteInner(MessageParcel & data,MessageParcel & reply)483 int32_t DistributedSchedStub::DisconnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
484 {
485     if (!CheckCallingUid()) {
486         HILOGW("request DENIED!");
487         return DMS_PERMISSION_DENIED;
488     }
489 
490     sptr<IRemoteObject> connect = data.ReadRemoteObject();
491     int32_t uid = 0;
492     PARCEL_READ_HELPER(data, Int32, uid);
493     string sourceDeviceId;
494     PARCEL_READ_HELPER(data, String, sourceDeviceId);
495     int32_t result = DisconnectAbilityFromRemote(connect, uid, sourceDeviceId);
496     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::DISCONNECT_REMOTE_ABILITY, result };
497     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
498     HILOGI("result %{public}d", result);
499     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
500 }
501 
NotifyProcessDiedFromRemoteInner(MessageParcel & data,MessageParcel & reply)502 int32_t DistributedSchedStub::NotifyProcessDiedFromRemoteInner(MessageParcel& data, MessageParcel& reply)
503 {
504     if (!CheckCallingUid()) {
505         HILOGW("request DENIED!");
506         return DMS_PERMISSION_DENIED;
507     }
508 
509     int32_t uid = 0;
510     PARCEL_READ_HELPER(data, Int32, uid);
511     int32_t pid = 0;
512     PARCEL_READ_HELPER(data, Int32, pid);
513     string sourceDeviceId;
514     PARCEL_READ_HELPER(data, String, sourceDeviceId);
515     CallerInfo callerInfo;
516     callerInfo.uid = uid;
517     callerInfo.pid = pid;
518     callerInfo.sourceDeviceId = sourceDeviceId;
519     callerInfo.callerType = CALLER_TYPE_HARMONY;
520     int32_t result = NotifyProcessDiedFromRemote(callerInfo);
521     HILOGI("result %{public}d", result);
522     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
523 }
524 
CheckCallingUid()525 bool DistributedSchedStub::CheckCallingUid()
526 {
527     // never allow non-system uid for distributed request
528     auto callingUid = IPCSkeleton::GetCallingUid();
529     return callingUid < HID_HAP;
530 }
531 
EnforceInterfaceToken(MessageParcel & data)532 bool DistributedSchedStub::EnforceInterfaceToken(MessageParcel& data)
533 {
534     u16string interfaceToken = data.ReadInterfaceToken();
535     return interfaceToken == DMS_STUB_INTERFACE_TOKEN;
536 }
537 
538 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
GetMissionInfosInner(MessageParcel & data,MessageParcel & reply)539 int32_t DistributedSchedStub::GetMissionInfosInner(MessageParcel& data, MessageParcel& reply)
540 {
541     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
542     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
543         return DMS_PERMISSION_DENIED;
544     }
545     std::u16string deviceId = data.ReadString16();
546     int32_t numMissions = 0;
547     PARCEL_READ_HELPER(data, Int32, numMissions);
548 
549     std::vector<MissionInfo> missionInfos;
550     int32_t result = GetMissionInfos(Str16ToStr8(deviceId), numMissions, missionInfos);
551     HILOGI("result = %{public}d", result);
552     if (result == ERR_NONE) {
553         result = MissionInfoConverter::WriteMissionInfosToParcel(reply, missionInfos) ? ERR_NONE : ERR_FLATTEN_OBJECT;
554     }
555     return result;
556 }
557 
GetRemoteMissionSnapshotInfoInner(MessageParcel & data,MessageParcel & reply)558 int32_t DistributedSchedStub::GetRemoteMissionSnapshotInfoInner(MessageParcel& data, MessageParcel& reply)
559 {
560     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
561     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
562         return DMS_PERMISSION_DENIED;
563     }
564     string networkId = data.ReadString();
565     if (networkId.empty()) {
566         HILOGE("networkId is empty!");
567         return ERR_FLATTEN_OBJECT;
568     }
569     int32_t missionId = 0;
570     PARCEL_READ_HELPER(data, Int32, missionId);
571     if (missionId < 0) {
572         HILOGE("missionId is invalid");
573         return INVALID_PARAMETERS_ERR;
574     }
575     std::unique_ptr<MissionSnapshot> missionSnapshotPtr = std::make_unique<MissionSnapshot>();
576     int32_t errCode = GetRemoteMissionSnapshotInfo(networkId, missionId, missionSnapshotPtr);
577     if (errCode != ERR_NONE) {
578         HILOGE("get mission snapshot failed!");
579         return ERR_NULL_OBJECT;
580     }
581     PARCEL_WRITE_HELPER(reply, Parcelable, missionSnapshotPtr.get());
582     std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId);
583     if (uuid.empty()) {
584         HILOGE("uuid is empty!");
585         return ERR_NULL_OBJECT;
586     }
587     std::unique_ptr<Snapshot> snapshotPtr = make_unique<Snapshot>();
588     SnapshotConverter::ConvertToSnapshot(*missionSnapshotPtr, snapshotPtr);
589     DistributedSchedMissionManager::GetInstance().EnqueueCachedSnapshotInfo(uuid,
590         missionId, std::move(snapshotPtr));
591     return ERR_NONE;
592 }
593 
RegisterMissionListenerInner(MessageParcel & data,MessageParcel & reply)594 int32_t DistributedSchedStub::RegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply)
595 {
596     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
597         return DMS_PERMISSION_DENIED;
598     }
599     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
600     u16string devId = data.ReadString16();
601     if (devId.empty()) {
602         HILOGW("read deviceId failed!");
603         return ERR_FLATTEN_OBJECT;
604     }
605     sptr<IRemoteObject> missionChangedListener = data.ReadRemoteObject();
606     if (missionChangedListener == nullptr) {
607         HILOGW("read IRemoteObject failed!");
608         return ERR_FLATTEN_OBJECT;
609     }
610     int32_t result = RegisterMissionListener(devId, missionChangedListener);
611     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
612 }
613 
UnRegisterMissionListenerInner(MessageParcel & data,MessageParcel & reply)614 int32_t DistributedSchedStub::UnRegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply)
615 {
616     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
617         return DMS_PERMISSION_DENIED;
618     }
619     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
620     u16string devId = data.ReadString16();
621     if (devId.empty()) {
622         HILOGW("read deviceId failed!");
623         return ERR_FLATTEN_OBJECT;
624     }
625     sptr<IRemoteObject> missionChangedListener = data.ReadRemoteObject();
626     if (missionChangedListener == nullptr) {
627         HILOGW("read IRemoteObject failed!");
628         return ERR_FLATTEN_OBJECT;
629     }
630     int32_t result = UnRegisterMissionListener(devId, missionChangedListener);
631     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
632 }
633 
StartSyncMissionsFromRemoteInner(MessageParcel & data,MessageParcel & reply)634 int32_t DistributedSchedStub::StartSyncMissionsFromRemoteInner(MessageParcel& data, MessageParcel& reply)
635 {
636     if (!CheckCallingUid()) {
637         HILOGW("request DENIED!");
638         return DMS_PERMISSION_DENIED;
639     }
640     CallerInfo callerInfo;
641     if (!CallerInfoUnmarshalling(callerInfo, data)) {
642         HILOGW("read callerInfo failed!");
643         return ERR_FLATTEN_OBJECT;
644     }
645     std::vector<DstbMissionInfo> missionInfos;
646     if (StartSyncMissionsFromRemote(callerInfo, missionInfos) != ERR_NONE) {
647         return ERR_FLATTEN_OBJECT;
648     }
649     if (!reply.WriteInt32(VERSION)) {
650         return ERR_FLATTEN_OBJECT;
651     }
652     if (!DstbMissionInfo::WriteDstbMissionInfosToParcel(reply, missionInfos)) {
653         HILOGE("write mission info failed!");
654         return ERR_FLATTEN_OBJECT;
655     }
656     return ERR_NONE;
657 }
658 
StopSyncRemoteMissionsInner(MessageParcel & data,MessageParcel & reply)659 int32_t DistributedSchedStub::StopSyncRemoteMissionsInner(MessageParcel& data, MessageParcel& reply)
660 {
661     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
662     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
663         return DMS_PERMISSION_DENIED;
664     }
665     u16string devId = data.ReadString16();
666     if (devId.empty()) {
667         HILOGW("read deviceId failed!");
668         return ERR_FLATTEN_OBJECT;
669     }
670     int32_t result = StopSyncRemoteMissions(Str16ToStr8(devId));
671     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
672 }
673 
StopSyncMissionsFromRemoteInner(MessageParcel & data,MessageParcel & reply)674 int32_t DistributedSchedStub::StopSyncMissionsFromRemoteInner(MessageParcel& data, MessageParcel& reply)
675 {
676     if (!CheckCallingUid()) {
677         HILOGW("request DENIED!");
678         return DMS_PERMISSION_DENIED;
679     }
680     CallerInfo callerInfo;
681     if (!CallerInfoUnmarshalling(callerInfo, data)) {
682         HILOGW("read callerInfo failed!");
683         return ERR_FLATTEN_OBJECT;
684     }
685     int32_t result = StopSyncMissionsFromRemote(callerInfo);
686     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
687 }
688 
NotifyMissionsChangedFromRemoteInner(MessageParcel & data,MessageParcel & reply)689 int32_t DistributedSchedStub::NotifyMissionsChangedFromRemoteInner(MessageParcel& data, MessageParcel& reply)
690 {
691     if (!CheckCallingUid()) {
692         HILOGW("request DENIED!");
693         return DMS_PERMISSION_DENIED;
694     }
695     int32_t version = data.ReadInt32();
696     HILOGD("version is %{public}d", version);
697     std::vector<DstbMissionInfo> missionInfos;
698     if (!DstbMissionInfo::ReadDstbMissionInfosFromParcel(data, missionInfos)) {
699         return ERR_FLATTEN_OBJECT;
700     }
701     CallerInfo callerInfo;
702     callerInfo.sourceDeviceId = data.ReadString();
703     callerInfo.uid = data.ReadInt32();
704     callerInfo.pid = data.ReadInt32();
705     callerInfo.dmsVersion = data.ReadInt32();
706     int32_t result = NotifyMissionsChangedFromRemote(missionInfos, callerInfo);
707     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
708 }
709 
StartSyncRemoteMissionsInner(MessageParcel & data,MessageParcel & reply)710 int32_t DistributedSchedStub::StartSyncRemoteMissionsInner(MessageParcel& data, MessageParcel& reply)
711 {
712     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
713         return DMS_PERMISSION_DENIED;
714     }
715     HILOGI("[PerformanceTest], IPC end = %{public}" PRId64, GetTickCount());
716     u16string devId = data.ReadString16();
717     if (devId.empty()) {
718         HILOGW("read deviceId failed!");
719         return ERR_FLATTEN_OBJECT;
720     }
721     string deviceId = Str16ToStr8(devId);
722     bool fixConflict = data.ReadBool();
723     int64_t tag = data.ReadInt64();
724     int32_t result = StartSyncRemoteMissions(deviceId, fixConflict, tag);
725     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
726 }
727 #endif
728 
CallerInfoUnmarshalling(CallerInfo & callerInfo,MessageParcel & data)729 bool DistributedSchedStub::CallerInfoUnmarshalling(CallerInfo& callerInfo, MessageParcel& data)
730 {
731     int32_t uid = -1;
732     PARCEL_READ_HELPER_RET(data, Int32, uid, false);
733     int32_t pid = -1;
734     PARCEL_READ_HELPER_RET(data, Int32, pid, false);
735     int32_t callerType = CALLER_TYPE_NONE;
736     PARCEL_READ_HELPER_RET(data, Int32, callerType, false);
737     std::string sourceDeviceId;
738     PARCEL_READ_HELPER_RET(data, String, sourceDeviceId, false);
739     HILOGI("sourceDeviceId = %{public}s",
740         DnetworkAdapter::AnonymizeDeviceId(sourceDeviceId).c_str());
741     int32_t duid = -1;
742     PARCEL_READ_HELPER_RET(data, Int32, duid, false);
743     std::string callerAppId;
744     PARCEL_READ_HELPER_RET(data, String, callerAppId, false);
745     int32_t version = -1;
746     PARCEL_READ_HELPER_RET(data, Int32, version, false);
747     callerInfo.uid = uid;
748     callerInfo.pid = pid;
749     callerInfo.callerType = callerType;
750     callerInfo.sourceDeviceId = sourceDeviceId;
751     callerInfo.duid = duid;
752     callerInfo.callerAppId = callerAppId;
753     callerInfo.dmsVersion = version;
754     return true;
755 }
756 
StartRemoteAbilityByCallInner(MessageParcel & data,MessageParcel & reply)757 int32_t DistributedSchedStub::StartRemoteAbilityByCallInner(MessageParcel& data, MessageParcel& reply)
758 {
759     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
760         return DMS_PERMISSION_DENIED;
761     }
762     DmsHiTraceChain hiTraceChain(TraceValue::START_REMOTE_ABILITY_BYCALL);
763     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::START_REMOTE_ABILITY_BYCALL);
764     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
765     if (want == nullptr) {
766         HILOGW("want readParcelable failed!");
767         return ERR_NULL_OBJECT;
768     }
769     sptr<IRemoteObject> connect = data.ReadRemoteObject();
770     int32_t callerUid = 0;
771     PARCEL_READ_HELPER(data, Int32, callerUid);
772     int32_t callerPid = 0;
773     PARCEL_READ_HELPER(data, Int32, callerPid);
774     uint32_t accessToken = 0;
775     PARCEL_READ_HELPER(data, Uint32, accessToken);
776     if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
777         PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
778         HILOGE("check data_sync permission failed!");
779         return DMS_PERMISSION_DENIED;
780     }
781     int32_t result = StartRemoteAbilityByCall(*want, connect, callerUid, callerPid, accessToken);
782     BehaviorEventParam eventParam = { EventCallingType::LOCAL, BehaviorEvent::START_REMOTE_ABILITY_BYCALL, result,
783         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerUid };
784     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
785     HILOGI("result = %{public}d", result);
786     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
787 }
788 
ReleaseRemoteAbilityInner(MessageParcel & data,MessageParcel & reply)789 int32_t DistributedSchedStub::ReleaseRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
790 {
791     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
792         return DMS_PERMISSION_DENIED;
793     }
794     DmsHiTraceChain hiTraceChain(TraceValue::RELEASE_REMOTE_ABILITY);
795     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::RELEASE_REMOTE_ABILITY);
796     sptr<IRemoteObject> connect = data.ReadRemoteObject();
797     shared_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
798     if (element == nullptr) {
799         HILOGE("ReleaseRemoteAbilityInner receive element is nullptr");
800         return ERR_INVALID_VALUE;
801     }
802     int32_t result = ReleaseRemoteAbility(connect, *element);
803     BehaviorEventParam eventParam = { EventCallingType::LOCAL, BehaviorEvent::RELEASE_REMOTE_ABILITY, result,
804         element->GetBundleName(), element->GetAbilityName() };
805     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
806     HILOGI("result = %{public}d", result);
807     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
808 }
809 
StartAbilityByCallFromRemoteInner(MessageParcel & data,MessageParcel & reply)810 int32_t DistributedSchedStub::StartAbilityByCallFromRemoteInner(MessageParcel& data, MessageParcel& reply)
811 {
812     if (!CheckCallingUid()) {
813         HILOGW("request DENIED!");
814         return DMS_PERMISSION_DENIED;
815     }
816 
817     sptr<IRemoteObject> connect = data.ReadRemoteObject();
818     CallerInfo callerInfo;
819     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
820     PARCEL_READ_HELPER(data, Int32, callerInfo.pid);
821     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
822     AccountInfo accountInfo;
823     accountInfo.accountType = data.ReadInt32();
824     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
825     callerInfo.callerAppId = data.ReadString();
826     std::string extraInfo = data.ReadString();
827     if (extraInfo.empty()) {
828         HILOGW("read extraInfo failed!");
829         return ERR_NULL_OBJECT;
830     }
831     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
832     if (!extraInfoJson.is_discarded()) {
833         SaveExtraInfo(extraInfoJson, callerInfo);
834         HILOGD("parse extra info");
835     }
836     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
837     if (want == nullptr) {
838         HILOGW("want readParcelable failed!");
839         return ERR_NULL_OBJECT;
840     }
841     int32_t result = StartAbilityByCallFromRemote(*want, connect, callerInfo, accountInfo);
842     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::START_REMOTE_ABILITY_BYCALL, result,
843         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerInfo.uid };
844     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
845     HILOGI("result = %{public}d", result);
846     PARCEL_WRITE_HELPER(reply, Int32, result);
847     return ERR_NONE;
848 }
849 
ReleaseAbilityFromRemoteInner(MessageParcel & data,MessageParcel & reply)850 int32_t DistributedSchedStub::ReleaseAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
851 {
852     if (!CheckCallingUid()) {
853         HILOGW("request DENIED!");
854         return DMS_PERMISSION_DENIED;
855     }
856 
857     sptr<IRemoteObject> connect = data.ReadRemoteObject();
858     shared_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
859     if (element == nullptr) {
860         HILOGE("ReleaseAbilityFromRemoteInner receive element is nullptr");
861         return ERR_INVALID_VALUE;
862     }
863     CallerInfo callerInfo;
864     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
865     std::string extraInfo;
866     PARCEL_READ_HELPER(data, String, extraInfo);
867     int32_t result = ReleaseAbilityFromRemote(connect, *element, callerInfo);
868     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::RELEASE_REMOTE_ABILITY, result,
869         element->GetBundleName(), element->GetAbilityName() };
870     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
871     HILOGI("result %{public}d", result);
872     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
873 }
874 
875 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
StartRemoteShareFormInner(MessageParcel & data,MessageParcel & reply)876 int32_t DistributedSchedStub::StartRemoteShareFormInner(MessageParcel& data, MessageParcel& reply)
877 {
878     HILOGD("SHAREFORM:: func call");
879     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
880         return DMS_PERMISSION_DENIED;
881     }
882 
883     std::string deviceId = "";
884     PARCEL_READ_HELPER(data, String, deviceId);
885     shared_ptr<AppExecFwk::FormShareInfo> formShareInfo(data.ReadParcelable<AppExecFwk::FormShareInfo>());
886     if (formShareInfo == nullptr) {
887         HILOGW("SHARE_FORM readParcelable failed!");
888         PARCEL_WRITE_REPLY_NOERROR(reply, Int32, static_cast<int32_t>(ERR_FLATTEN_OBJECT));
889         return ERR_FLATTEN_OBJECT;
890     }
891 
892     int32_t result = StartRemoteShareForm(deviceId, *formShareInfo);
893     HILOGI("result = %{public}d", result);
894     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
895 }
896 
StartShareFormFromRemoteInner(MessageParcel & data,MessageParcel & reply)897 int32_t DistributedSchedStub::StartShareFormFromRemoteInner(MessageParcel& data, MessageParcel& reply)
898 {
899     HILOGD("SHAREFORM:: func call");
900     if (!CheckCallingUid()) {
901         HILOGW("request DENIED!");
902         PARCEL_WRITE_REPLY_NOERROR(reply, Int32, static_cast<int32_t>(DMS_PERMISSION_DENIED));
903         return DMS_PERMISSION_DENIED;
904     }
905 
906     std::string deviceId = "";
907     PARCEL_READ_HELPER(data, String, deviceId);
908     shared_ptr<AppExecFwk::FormShareInfo> formShareInfo(data.ReadParcelable<AppExecFwk::FormShareInfo>());
909     if (formShareInfo == nullptr) {
910         HILOGW("SHARE_FORM readParcelable failed!");
911         PARCEL_WRITE_REPLY_NOERROR(reply, Int32, static_cast<int32_t>(ERR_FLATTEN_OBJECT));
912         return ERR_FLATTEN_OBJECT;
913     }
914 
915     int32_t result = StartShareFormFromRemote(deviceId, *formShareInfo);
916     HILOGI("result = %{public}d", result);
917     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
918 }
919 #endif
920 
GetDistributedComponentListInner(MessageParcel & data,MessageParcel & reply)921 int32_t DistributedSchedStub::GetDistributedComponentListInner(MessageParcel& data, MessageParcel& reply)
922 {
923     if (!CheckCallingUid()) {
924         HILOGW("request DENIED!");
925         return DMS_PERMISSION_DENIED;
926     }
927     std::vector<std::string> distributedComponents;
928     int32_t result = GetDistributedComponentList(distributedComponents);
929     HILOGI("result = %{public}d", result);
930     PARCEL_WRITE_HELPER(reply, Int32, result);
931     PARCEL_WRITE_HELPER(reply, StringVector, distributedComponents);
932     return ERR_NONE;
933 }
934 
StartRemoteFreeInstallInner(MessageParcel & data,MessageParcel & reply)935 int32_t DistributedSchedStub::StartRemoteFreeInstallInner(MessageParcel& data, MessageParcel& reply)
936 {
937     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
938         return DMS_PERMISSION_DENIED;
939     }
940     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
941     if (want == nullptr) {
942         HILOGE("want readParcelable failed!");
943         return ERR_NULL_OBJECT;
944     }
945     int32_t callerUid = 0;
946     int32_t requestCode = 0;
947     uint32_t accessToken = 0;
948     PARCEL_READ_HELPER(data, Int32, callerUid);
949     PARCEL_READ_HELPER(data, Int32, requestCode);
950     PARCEL_READ_HELPER(data, Uint32, accessToken);
951     sptr<IRemoteObject> callback = data.ReadRemoteObject();
952     if (callback == nullptr) {
953         HILOGE("read callback failed!");
954         return ERR_NULL_OBJECT;
955     }
956     if (DistributedSchedPermission::GetInstance().CheckPermission(accessToken,
957         PERMISSION_DISTRIBUTED_DATASYNC) != ERR_OK) {
958         HILOGE("check data_sync permission failed!");
959         return DMS_PERMISSION_DENIED;
960     }
961     int32_t result = StartRemoteFreeInstall(*want, callerUid, requestCode, accessToken, callback);
962     HILOGI("result = %{public}d", result);
963     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
964     return ERR_NONE;
965 }
966 
StartFreeInstallFromRemoteInner(MessageParcel & data,MessageParcel & reply)967 int32_t DistributedSchedStub::StartFreeInstallFromRemoteInner(MessageParcel& data, MessageParcel& reply)
968 {
969     if (!CheckCallingUid()) {
970         HILOGW("request DENIED!");
971         return DMS_PERMISSION_DENIED;
972     }
973     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
974     if (want == nullptr) {
975         HILOGE("want readParcelable failed!");
976         return ERR_NULL_OBJECT;
977     }
978 
979     int64_t begin = GetTickCount();
980     CallerInfo callerInfo = {.accessToken = 0};
981     callerInfo.callerType = CALLER_TYPE_HARMONY;
982     AccountInfo accountInfo = {};
983     int64_t taskId = 0;
984 
985     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
986     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
987     accountInfo.accountType = data.ReadInt32();
988     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
989     callerInfo.callerAppId = data.ReadString();
990     PARCEL_READ_HELPER(data, Int64, taskId);
991     shared_ptr<AAFwk::Want> cmpWant(data.ReadParcelable<AAFwk::Want>());
992     std::string extraInfo = data.ReadString();
993     if (extraInfo.empty()) {
994         HILOGD("extra info is empty!");
995     }
996     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
997     int32_t requestCode = DEFAULT_REQUEST_CODE;
998     if (!extraInfoJson.is_discarded()) {
999         SaveExtraInfo(extraInfoJson, callerInfo);
1000         if (extraInfoJson.contains(EXTRO_INFO_JSON_KEY_REQUEST_CODE)) {
1001             requestCode = extraInfoJson[EXTRO_INFO_JSON_KEY_REQUEST_CODE];
1002             HILOGD("parse extra info, requestCode = %d", requestCode);
1003         }
1004     }
1005 
1006     FreeInstallInfo info = {
1007         .want = *want, .callerInfo = callerInfo, .accountInfo = accountInfo, .requestCode = requestCode};
1008     info.want.SetParam(PARAM_FREEINSTALL_APPID, callerInfo.callerAppId);
1009     info.want.SetParam(
1010         PARAM_FREEINSTALL_BUNDLENAMES, (*cmpWant).GetStringArrayParam(CMPT_PARAM_FREEINSTALL_BUNDLENAMES));
1011     int32_t result = StartFreeInstallFromRemote(info, taskId);
1012     HILOGI("result = %{public}d", result);
1013     PARCEL_WRITE_HELPER(reply, Int32, result);
1014     int64_t end = GetTickCount();
1015     PARCEL_WRITE_HELPER(reply, Int64, end - begin);
1016     return ERR_NONE;
1017 }
1018 
NotifyCompleteFreeInstallFromRemoteInner(MessageParcel & data,MessageParcel & reply)1019 int32_t DistributedSchedStub::NotifyCompleteFreeInstallFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1020 {
1021     if (!CheckCallingUid()) {
1022         HILOGW("request DENIED!");
1023         return DMS_PERMISSION_DENIED;
1024     }
1025     int64_t taskId = 0;
1026     int32_t resultCode = 0;
1027     PARCEL_READ_HELPER(data, Int64, taskId);
1028     PARCEL_READ_HELPER(data, Int32, resultCode);
1029     int32_t result = NotifyCompleteFreeInstallFromRemote(taskId, resultCode);
1030     HILOGI("result = %{public}d", result);
1031     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1032 }
1033 } // namespace DistributedSchedule
1034 } // namespace OHOS
1035