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_stub.h"
17 
18 #include "ability_info.h"
19 #include "datetime_ex.h"
20 #include "ipc_skeleton.h"
21 #include "message_parcel.h"
22 
23 #include "adapter/dnetwork_adapter.h"
24 #include "bundle/bundle_manager_internal.h"
25 #include "caller_info.h"
26 #include "dfx/dms_continue_time_dumper.h"
27 #include "dfx/dms_hisysevent_report.h"
28 #include "dfx/dms_hitrace_chain.h"
29 #include "dfx/dms_hitrace_constants.h"
30 #include "distributed_want.h"
31 #include "distributed_sched_permission.h"
32 #include "distributed_sched_service.h"
33 #include "distributed_sched_types.h"
34 #include "distributed_sched_utils.h"
35 #include "dms_constant.h"
36 #include "dms_version_manager.h"
37 #include "dsched_continue_manager.h"
38 #include "dsched_transport_softbus_adapter.h"
39 #include "dtbschedmgr_log.h"
40 #include "dtbschedmgr_device_info_storage.h"
41 #include "parcel_helper.h"
42 
43 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
44 #include "image_source.h"
45 
46 #include "mission/distributed_sched_mission_manager.h"
47 #include "mission/dms_continue_recv_manager.h"
48 #include "mission/mission_info_converter.h"
49 #include "mission/snapshot_converter.h"
50 #include "napi_error_code.h"
51 #endif
52 
53 namespace OHOS {
54 namespace DistributedSchedule {
55 using namespace std;
56 using namespace AAFwk;
57 using namespace AppExecFwk;
58 
59 namespace {
60 constexpr int32_t HID_HAP = 10000; /* first hap user */
61 const std::string TAG = "DistributedSchedStub";
62 const std::u16string DMS_STUB_INTERFACE_TOKEN = u"ohos.distributedschedule.accessToken";
63 const std::string EXTRO_INFO_JSON_KEY_ACCESS_TOKEN = "accessTokenID";
64 const std::string EXTRO_INFO_JSON_KEY_REQUEST_CODE = "requestCode";
65 const std::string PARAM_FREEINSTALL_APPID = "ohos.freeinstall.params.callingAppId";
66 const std::string PARAM_FREEINSTALL_BUNDLENAMES = "ohos.freeinstall.params.callingBundleNames";
67 const std::string CMPT_PARAM_FREEINSTALL_BUNDLENAMES = "ohos.extra.param.key.allowedBundles";
68 const std::string FEATURE_ABILITY_FLAG_KEY = "ohos.dms.faFlag";
69 const std::string DMS_VERSION_ID = "dmsVersion";
70 const std::string DMS_UID_SPEC_BUNDLE_NAME = "dmsCallerUidBundleName";
71 constexpr int32_t QOS_THRESHOLD_VERSION = 5;
72 const int DEFAULT_REQUEST_CODE = -1;
73 }
74 
DistributedSchedStub()75 DistributedSchedStub::DistributedSchedStub()
76 {
77     InitExtendedLocalFuncsInner();
78     InitLocalFuncsInner();
79     InitRemoteFuncsInner();
80 }
81 
~DistributedSchedStub()82 DistributedSchedStub::~DistributedSchedStub()
83 {
84     remoteFuncsMap_.clear();
85     localFuncsMap_.clear();
86 }
87 
InitExtendedLocalFuncsInner()88 void DistributedSchedStub::InitExtendedLocalFuncsInner()
89 {
90     // request codes for mission manager
91 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
92     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_DSCHED_EVENT_LISTENER)] =
93         &DistributedSchedStub::RegisterDSchedEventListenerInner;
94     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_DSCHED_EVENT_LISTENER)] =
95         &DistributedSchedStub::UnRegisterDSchedEventListenerInner;
96     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::GET_CONTINUE_INFO)] =
97         &DistributedSchedStub::GetContinueInfoInner;
98     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::GET_DSCHED_EVENT_INFO)] =
99         &DistributedSchedStub::GetDSchedEventInfoInner;
100 #endif
101 }
102 
InitLocalFuncsInner()103 void DistributedSchedStub::InitLocalFuncsInner()
104 {
105     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_ABILITY)] =
106         &DistributedSchedStub::StartRemoteAbilityInner;
107     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::CONTINUE_MISSION)] =
108         &DistributedSchedStub::ContinueMissionInner;
109     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::CONTINUE_MISSION_OF_BUNDLENAME)] =
110         &DistributedSchedStub::ContinueMissionOfBundleNameInner;
111     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_CONTINUATION)] =
112         &DistributedSchedStub::StartContinuationInner;
113     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_COMPLETE_CONTINUATION)] =
114         &DistributedSchedStub::NotifyCompleteContinuationInner;
115     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::CONNECT_REMOTE_ABILITY)] =
116         &DistributedSchedStub::ConnectRemoteAbilityInner;
117     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::DISCONNECT_REMOTE_ABILITY)] =
118         &DistributedSchedStub::DisconnectRemoteAbilityInner;
119     // request codes for mission manager
120 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
121     InitLocalMissionManagerInner();
122 #endif
123     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_ABILITY_BY_CALL)] =
124         &DistributedSchedStub::StartRemoteAbilityByCallInner;
125     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::RELEASE_REMOTE_ABILITY)] =
126         &DistributedSchedStub::ReleaseRemoteAbilityInner;
127 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
128     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_SHARE_FORM)] =
129         &DistributedSchedStub::StartRemoteShareFormInner;
130 #endif
131     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::GET_DISTRIBUTED_COMPONENT_LIST)] =
132         &DistributedSchedStub::GetDistributedComponentListInner;
133     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_REMOTE_FREE_INSTALL)] =
134         &DistributedSchedStub::StartRemoteFreeInstallInner;
135     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::STOP_REMOTE_EXTERNSION_ABILITY)] =
136         &DistributedSchedStub::StopRemoteExtensionAbilityInner;
137 }
138 
InitLocalMissionManagerInner()139 void DistributedSchedStub::InitLocalMissionManagerInner()
140 {
141     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::GET_REMOTE_MISSION_SNAPSHOT_INFO)] =
142         &DistributedSchedStub::GetRemoteMissionSnapshotInfoInner;
143     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_MISSION_LISTENER)] =
144         &DistributedSchedStub::RegisterMissionListenerInner;
145     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_ON_LISTENER)] =
146         &DistributedSchedStub::RegisterOnListenerInner;
147     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::REGISTER_OFF_LISTENER)] =
148         &DistributedSchedStub::RegisterOffListenerInner;
149     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::UNREGISTER_MISSION_LISTENER)] =
150         &DistributedSchedStub::UnRegisterMissionListenerInner;
151     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::GET_MISSION_INFOS)] =
152         &DistributedSchedStub::GetMissionInfosInner;
153     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_SYNC_MISSIONS)] =
154         &DistributedSchedStub::StartSyncRemoteMissionsInner;
155     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::STOP_SYNC_MISSIONS)] =
156         &DistributedSchedStub::StopSyncRemoteMissionsInner;
157     localFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::SET_MISSION_CONTINUE_STATE)] =
158         &DistributedSchedStub::SetMissionContinueStateInner;
159 }
160 
InitRemoteFuncsInner()161 void DistributedSchedStub::InitRemoteFuncsInner()
162 {
163     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_ABILITY_FROM_REMOTE)] =
164         &DistributedSchedStub::StartAbilityFromRemoteInner;
165     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::STOP_ABILITY_FROM_REMOTE)] =
166         &DistributedSchedStub::StopAbilityFromRemoteInner;
167     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::SEND_RESULT_FROM_REMOTE)] =
168         &DistributedSchedStub::SendResultFromRemoteInner;
169     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_CONTINUATION_RESULT_FROM_REMOTE)] =
170         &DistributedSchedStub::NotifyContinuationResultFromRemoteInner;
171     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_DSCHED_EVENT_RESULT_FROM_REMOTE)] =
172         &DistributedSchedStub::NotifyDSchedEventResultFromRemoteInner;
173     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::CONNECT_ABILITY_FROM_REMOTE)] =
174         &DistributedSchedStub::ConnectAbilityFromRemoteInner;
175     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::DISCONNECT_ABILITY_FROM_REMOTE)] =
176         &DistributedSchedStub::DisconnectAbilityFromRemoteInner;
177     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_PROCESS_DIED_FROM_REMOTE)] =
178         &DistributedSchedStub::NotifyProcessDiedFromRemoteInner;
179 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
180     // request codes for mission manager
181     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_SYNC_MISSIONS_FROM_REMOTE)] =
182         &DistributedSchedStub::StartSyncMissionsFromRemoteInner;
183     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::STOP_SYNC_MISSIONS_FROM_REMOTE)] =
184         &DistributedSchedStub::StopSyncMissionsFromRemoteInner;
185     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_MISSIONS_CHANGED_FROM_REMOTE)] =
186         &DistributedSchedStub::NotifyMissionsChangedFromRemoteInner;
187 #endif
188     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::CONTINUE_MISSION)] =
189         &DistributedSchedStub::ContinueMissionInner;
190     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::CONTINUE_MISSION_OF_BUNDLENAME)] =
191         &DistributedSchedStub::ContinueMissionOfBundleNameInner;
192     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_ABILITY_BY_CALL_FROM_REMOTE)] =
193         &DistributedSchedStub::StartAbilityByCallFromRemoteInner;
194     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::RELEASE_ABILITY_FROM_REMOTE)] =
195         &DistributedSchedStub::ReleaseAbilityFromRemoteInner;
196     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_STATE_CHANGED_FROM_REMOTE)] =
197         &DistributedSchedStub::NotifyStateChangedFromRemoteInner;
198 
199 #ifdef DMSFWK_INTERACTIVE_ADAPTER
200     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_ABILITY_LIFECYCLE_CHANGED_FROM_REMOTE)] =
201         &DistributedSchedStub::NotifyAbilityLifecycleChangedFromRemoteAdapterInner;
202 #endif
203 
204 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
205     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_SHARE_FORM_FROM_REMOTE)] =
206         &DistributedSchedStub::StartShareFormFromRemoteInner;
207 #endif
208     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::START_FREE_INSTALL_FROM_REMOTE)] =
209         &DistributedSchedStub::StartFreeInstallFromRemoteInner;
210     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::NOTIFY_COMPLETE_FREE_INSTALL_FROM_REMOTE)] =
211         &DistributedSchedStub::NotifyCompleteFreeInstallFromRemoteInner;
212     remoteFuncsMap_[static_cast<uint32_t>(IDSchedInterfaceCode::STOP_EXTERNSION_ABILITY_FROM_REMOTE)] =
213         &DistributedSchedStub::StopExtensionAbilityFromRemoteInner;
214 }
215 
OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)216 int32_t DistributedSchedStub::OnRemoteRequest(uint32_t code,
217     MessageParcel& data, MessageParcel& reply, MessageOption& option)
218 {
219     bool IsLocalCalling = IPCSkeleton::IsLocalCalling();
220     HILOGI("OnRemoteRequest, code = %{public}u, flags = %{public}d, IsLocalCalling = %{public}d.",
221         code, option.GetFlags(), IsLocalCalling);
222 
223     const auto& funcsMap = IsLocalCalling ? localFuncsMap_ : remoteFuncsMap_;
224     auto iter = funcsMap.find(code);
225     if (iter != funcsMap.end()) {
226         auto func = iter->second;
227         if (!EnforceInterfaceToken(data)) {
228             HILOGW("OnRemoteRequest interface token check failed!");
229             return DMS_PERMISSION_DENIED;
230         }
231         if (func != nullptr) {
232             return (this->*func)(data, reply);
233         }
234     }
235 
236     HILOGW("OnRemoteRequest default case, need check.");
237     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
238 }
239 
StartRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)240 int32_t DistributedSchedStub::StartRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
241 {
242     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
243         return DMS_PERMISSION_DENIED;
244     }
245     DmsHiTraceChain hiTraceChain(TraceValue::START_REMOTE_ABILITY);
246     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::START_REMOTE_ABILITY);
247     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
248     if (want == nullptr) {
249         HILOGW("START_ABILITY want readParcelable failed!");
250         return ERR_NULL_OBJECT;
251     }
252     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
253     int32_t callerUid = 0;
254     PARCEL_READ_HELPER(data, Int32, callerUid);
255     int32_t requestCode = 0;
256     PARCEL_READ_HELPER(data, Int32, requestCode);
257     uint32_t accessToken = 0;
258     PARCEL_READ_HELPER(data, Uint32, accessToken);
259     HILOGD("get callerUid = %{public}d, AccessTokenID = %{private}s", callerUid,
260         GetAnonymStr(std::to_string(accessToken)).c_str());
261     DistributedSchedPermission::GetInstance().MarkUriPermission(*want, accessToken);
262     int32_t result = StartRemoteAbility(*want, callerUid, requestCode, accessToken);
263     ReportEvent(*want, BehaviorEvent::START_REMOTE_ABILITY, result, callerUid);
264     HILOGI("StartRemoteAbilityInner result = %{public}d", result);
265     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
266 }
267 
ReportEvent(const OHOS::AAFwk::Want& want, const std::string& eventName, int32_t result, int32_t callerUid)268 void DistributedSchedStub::ReportEvent(const OHOS::AAFwk::Want& want, const std::string& eventName, int32_t result,
269     int32_t callerUid)
270 {
271     std::vector<std::string> bundleNames;
272     if (!BundleManagerInternal::GetBundleNameListFromBms(callerUid, bundleNames)) {
273         HILOGE("GetBundleNameListFromBms failed");
274         return;
275     }
276     std::string srcBundleName = bundleNames.empty() ? std::string() : bundleNames.front();
277     HILOGD("srcBundleName %{public}s", srcBundleName.c_str());
278     AppExecFwk::BundleInfo localBundleInfo;
279     if (BundleManagerInternal::GetLocalBundleInfo(srcBundleName, localBundleInfo) != ERR_OK) {
280         HILOGE("get local bundle info failed");
281         return;
282     }
283     HILOGD("version %{public}s", localBundleInfo.versionName.c_str());
284 
285     BehaviorEventParam eventParam = { EventCallingType::LOCAL, eventName, result, want.GetElement().GetBundleName(),
286         want.GetElement().GetAbilityName(), callerUid, srcBundleName, localBundleInfo.versionName };
287     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
288     HILOGD("report event success!");
289 }
290 
ReadDistributedWant(MessageParcel& data)291 shared_ptr<AAFwk::Want> DistributedSchedStub::ReadDistributedWant(MessageParcel& data)
292 {
293     shared_ptr<DistributedWant> dstbWant(data.ReadParcelable<DistributedWant>());
294     shared_ptr<AAFwk::Want> want = nullptr;
295     if (dstbWant != nullptr) {
296         want = dstbWant->ToWant();
297     }
298     return want;
299 }
300 
GetStartAbilityFromRemoteExParam(MessageParcel& data, OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t& requestCode, CallerInfo& callerInfo, AccountInfo& accountInfo)301 int32_t DistributedSchedStub::GetStartAbilityFromRemoteExParam(MessageParcel& data,
302     OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t& requestCode,
303     CallerInfo& callerInfo, AccountInfo& accountInfo)
304 {
305     unique_ptr<CompatibleAbilityInfo> cmpAbilityInfo(data.ReadParcelable<CompatibleAbilityInfo>());
306     if (cmpAbilityInfo == nullptr) {
307         HILOGE("AbilityInfo readParcelable failed!");
308         return ERR_NULL_OBJECT;
309     }
310     cmpAbilityInfo->ConvertToAbilityInfo(abilityInfo);
311 
312     PARCEL_READ_HELPER(data, Int32, requestCode);
313 
314     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
315     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
316     callerInfo.callerType = CALLER_TYPE_HARMONY;
317 
318     accountInfo.accountType = data.ReadInt32();
319     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
320 
321     callerInfo.callerAppId = data.ReadString();
322     std::string extraInfo = data.ReadString();
323     if (!extraInfo.empty()) {
324         nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
325         if (!extraInfoJson.is_discarded()) {
326             SaveExtraInfo(extraInfoJson, callerInfo, accountInfo);
327             HILOGD("parse extra info");
328         }
329     }
330     return ERR_OK;
331 }
332 
GetConnectAbilityFromRemoteExParam(MessageParcel& data, AppExecFwk::AbilityInfo& abilityInfo, sptr<IRemoteObject>& connect, CallerInfo& callerInfo, AccountInfo& accountInfo)333 int32_t DistributedSchedStub::GetConnectAbilityFromRemoteExParam(MessageParcel& data,
334     AppExecFwk::AbilityInfo& abilityInfo, sptr<IRemoteObject>& connect,
335     CallerInfo& callerInfo, AccountInfo& accountInfo)
336 {
337     unique_ptr<CompatibleAbilityInfo> cmpAbilityInfo(data.ReadParcelable<CompatibleAbilityInfo>());
338     if (cmpAbilityInfo == nullptr) {
339         HILOGE("abilityInfo readParcelable failed!");
340         return ERR_NULL_OBJECT;
341     }
342     cmpAbilityInfo->ConvertToAbilityInfo(abilityInfo);
343 
344     connect = data.ReadRemoteObject();
345 
346     int32_t result = ReadDataForConnect(data, callerInfo, accountInfo);
347     if (result != ERR_NONE) {
348         HILOGD("Read callerInfo and accountInfo for connect fail, ret %{public}d.", result);
349         return result;
350     }
351     std::string extraInfo = data.ReadString();
352     if (extraInfo.empty()) {
353         HILOGD("extra info is empty!");
354     }
355     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
356     if (!extraInfoJson.is_discarded()) {
357         SaveExtraInfo(extraInfoJson, callerInfo, accountInfo);
358         HILOGD("parse extra info");
359     }
360     return ERR_OK;
361 }
362 
StartAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)363 int32_t DistributedSchedStub::StartAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
364 {
365 #ifdef DMSFWK_INTERACTIVE_ADAPTER
366     if (CheckRemoteOsType(IPCSkeleton::GetCallingDeviceID())) {
367         return StartAbilityFromRemoteAdapterInner(data, reply);
368     }
369 #endif
370 
371     if (!CheckCallingUid()) {
372         HILOGW("request DENIED!");
373         return DMS_PERMISSION_DENIED;
374     }
375     int64_t begin = GetTickCount();
376 
377     shared_ptr<AAFwk::Want> want = ReadDistributedWant(data);
378     if (want == nullptr) {
379         HILOGW("want readParcelable failed!");
380         return ERR_NULL_OBJECT;
381     }
382     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
383 
384     AbilityInfo abilityInfo;
385     int32_t requestCode = 0;
386     CallerInfo callerInfo;
387     AccountInfo accountInfo;
388     if (GetStartAbilityFromRemoteExParam(data, abilityInfo, requestCode, callerInfo, accountInfo) != ERR_OK) {
389         HILOGE("Get start ability from remote exParam fail!");
390         return INVALID_PARAMETERS_ERR;
391     }
392     std::string package = abilityInfo.bundleName;
393     std::string deviceId = abilityInfo.deviceId;
394 
395     int32_t result = StartAbilityFromRemote(*want, abilityInfo, requestCode, callerInfo, accountInfo);
396     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::START_REMOTE_ABILITY, result,
397         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerInfo.uid };
398     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
399     HILOGI("result = %{public}d", result);
400     PARCEL_WRITE_HELPER(reply, Int32, result);
401     int64_t end = GetTickCount();
402     PARCEL_WRITE_HELPER(reply, Int64, end - begin);
403     PARCEL_WRITE_HELPER(reply, String, package);
404     PARCEL_WRITE_HELPER(reply, String, deviceId);
405     return ERR_NONE;
406 }
407 
StopAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)408 int32_t DistributedSchedStub::StopAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
409 {
410 #ifdef DMSFWK_INTERACTIVE_ADAPTER
411     if (CheckRemoteOsType(IPCSkeleton::GetCallingDeviceID())) {
412         return StopAbilityFromRemoteAdapterInner(data, reply);
413     }
414 #endif
415     int32_t result = ERR_OK;
416     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
417 }
418 
SaveExtraInfo(const nlohmann::json& extraInfoJson, CallerInfo& callerInfo, AccountInfo& accountInfo)419 void DistributedSchedStub::SaveExtraInfo(const nlohmann::json& extraInfoJson, CallerInfo& callerInfo,
420     AccountInfo& accountInfo)
421 {
422     if (extraInfoJson.find(EXTRO_INFO_JSON_KEY_ACCESS_TOKEN) != extraInfoJson.end() &&
423         extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN].is_number_unsigned()) {
424         uint32_t accessToken = extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN];
425         callerInfo.accessToken = accessToken;
426         HILOGD("parse extra info, accessTokenID = %s", GetAnonymStr(std::to_string(accessToken)).c_str());
427     }
428 
429     if (extraInfoJson.find(DMS_VERSION_ID) != extraInfoJson.end() && extraInfoJson[DMS_VERSION_ID].is_string()) {
430         std::string dmsVersion = extraInfoJson[DMS_VERSION_ID];
431         callerInfo.extraInfoJson[DMS_VERSION_ID] = dmsVersion;
432     }
433 
434     if (extraInfoJson.find(DMS_UID_SPEC_BUNDLE_NAME) != extraInfoJson.end() &&
435         extraInfoJson[DMS_UID_SPEC_BUNDLE_NAME].is_string()) {
436         std::string uidBundleName = extraInfoJson[DMS_UID_SPEC_BUNDLE_NAME];
437         callerInfo.extraInfoJson[DMS_UID_SPEC_BUNDLE_NAME] = uidBundleName;
438     }
439 
440     if (extraInfoJson.find(Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID) != extraInfoJson.end() &&
441         extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID].is_string()) {
442         accountInfo.activeAccountId = extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID];
443     }
444     if (extraInfoJson.find(Constants::EXTRO_INFO_JSON_KEY_USERID_ID) != extraInfoJson.end() &&
445         extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID].is_number()) {
446         accountInfo.userId = extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID];
447     }
448     HILOGD("save dms version");
449 }
450 
SaveSendResultExtraInfo(const nlohmann::json& extraInfoJson, CallerInfo& callerInfo, AccountInfo& accountInfo)451 void DistributedSchedStub::SaveSendResultExtraInfo(const nlohmann::json& extraInfoJson, CallerInfo& callerInfo,
452     AccountInfo& accountInfo)
453 {
454     if (extraInfoJson.find(Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID) != extraInfoJson.end() &&
455         extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID].is_string()) {
456         accountInfo.activeAccountId = extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_ACCOUNT_ID];
457     }
458     if (extraInfoJson.find(Constants::EXTRO_INFO_JSON_KEY_USERID_ID) != extraInfoJson.end() &&
459         extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID].is_number()) {
460         accountInfo.userId = extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_USERID_ID];
461     }
462 
463     if (extraInfoJson.find(Constants::EXTRO_INFO_JSON_KEY_CALLER_INFO_EX) != extraInfoJson.end() &&
464         extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_CALLER_INFO_EX].is_string()) {
465         std::string callerExJsonStr = extraInfoJson[Constants::EXTRO_INFO_JSON_KEY_CALLER_INFO_EX];
466         if (callerExJsonStr.empty()) {
467             HILOGD("caller extra info json string is empty!");
468         }
469         callerInfo.extraInfoJson = nlohmann::json::parse(callerExJsonStr, nullptr, false);
470     }
471     HILOGD("save dms version");
472 }
473 
SendResultFromRemoteInner(MessageParcel& data, MessageParcel& reply)474 int32_t DistributedSchedStub::SendResultFromRemoteInner(MessageParcel& data, MessageParcel& reply)
475 {
476     if (!CheckCallingUid()) {
477         HILOGW("request DENIED!");
478         return DMS_PERMISSION_DENIED;
479     }
480     shared_ptr<DistributedWant> dstbWant(data.ReadParcelable<DistributedWant>());
481     shared_ptr<AAFwk::Want> want = nullptr;
482     if (dstbWant != nullptr) {
483         want = dstbWant->ToWant();
484     }
485     if (want == nullptr) {
486         HILOGW("want readParcelable failed!");
487         return ERR_NULL_OBJECT;
488     }
489     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
490     int64_t begin = GetTickCount();
491     int32_t requestCode = 0;
492     PARCEL_READ_HELPER(data, Int32, requestCode);
493     CallerInfo callerInfo;
494     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
495     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
496     callerInfo.callerType = CALLER_TYPE_HARMONY;
497     AccountInfo accountInfo;
498     accountInfo.accountType = data.ReadInt32();
499     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
500     callerInfo.callerAppId = data.ReadString();
501     int32_t resultCode = 0;
502     PARCEL_READ_HELPER(data, Int32, resultCode);
503     std::string extraInfo = data.ReadString();
504     if (extraInfo.empty()) {
505         HILOGD("extra info is empty!");
506     }
507     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
508     if (!extraInfoJson.is_discarded()) {
509         SaveSendResultExtraInfo(extraInfoJson, callerInfo, accountInfo);
510         HILOGD("parse extra info");
511     }
512 
513     int32_t result = SendResultFromRemote(*want, requestCode, callerInfo, accountInfo, resultCode);
514     HILOGI("result = %{public}d", result);
515     PARCEL_WRITE_HELPER(reply, Int32, result);
516     int64_t end = GetTickCount();
517     PARCEL_WRITE_HELPER(reply, Int64, end - begin);
518     return ERR_NONE;
519 }
520 
ContinueMissionInner(MessageParcel& data, MessageParcel& reply)521 int32_t DistributedSchedStub::ContinueMissionInner(MessageParcel& data, MessageParcel& reply)
522 {
523     bool isLocalCalling = IPCSkeleton::IsLocalCalling();
524     if ((isLocalCalling && !DistributedSchedPermission::GetInstance().IsFoundationCall()) ||
525         (!isLocalCalling && !CheckCallingUid())) {
526         HILOGE("check permission failed!");
527         return DMS_PERMISSION_DENIED;
528     }
529 
530     std::string srcDevId;
531     std::string dstDevId;
532     PARCEL_READ_HELPER(data, String, srcDevId);
533     PARCEL_READ_HELPER(data, String, dstDevId);
534     int32_t missionId = 0;
535     PARCEL_READ_HELPER(data, Int32, missionId);
536     sptr<IRemoteObject> callback = data.ReadRemoteObject();
537     if (callback == nullptr) {
538         HILOGW("read callback failed!");
539         return ERR_NULL_OBJECT;
540     }
541     shared_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
542     if (wantParams == nullptr) {
543         HILOGW("wantParams readParcelable failed!");
544         return ERR_NULL_OBJECT;
545     }
546 
547     int32_t result = ERR_OK;
548     AAFwk::MissionInfo missionInfo;
549     if (isLocalCalling) {
550         std::string remoteDeviceId = (IPCSkeleton::GetCallingDeviceID() == srcDevId) ? dstDevId : srcDevId;
551         if (AAFwk::AbilityManagerClient::GetInstance()->GetMissionInfo("", missionId, missionInfo) != ERR_OK) {
552             return ERR_NULL_OBJECT;
553         }
554         std::string sourceBundleName = missionInfo.want.GetBundle();
555         missionInfo.want.SetParams(*wantParams);
556         bool isFreeInstall = missionInfo.want.GetBoolParam("isFreeInstall", false);
557         if ((!isFreeInstall && IsUsingQos(remoteDeviceId)) ||
558             (isFreeInstall && IsRemoteInstall(remoteDeviceId, sourceBundleName))) {
559             DSchedTransportSoftbusAdapter::GetInstance().SetCallingTokenId(IPCSkeleton::GetCallingTokenID());
560             result = DSchedContinueManager::GetInstance().ContinueMission(srcDevId, dstDevId, missionId, callback,
561                 *wantParams);
562             HILOGI("result = %{public}d", result);
563             PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
564         }
565     }
566     result = ContinueMission(srcDevId, dstDevId, missionId, callback, *wantParams);
567     HILOGI("result = %{public}d", result);
568     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
569 }
570 
ContinueMissionOfBundleNameInner(MessageParcel& data, MessageParcel& reply)571 int32_t DistributedSchedStub::ContinueMissionOfBundleNameInner(MessageParcel& data, MessageParcel& reply)
572 {
573     bool isLocalCalling = IPCSkeleton::IsLocalCalling();
574     if ((isLocalCalling && !DistributedSchedPermission::GetInstance().IsFoundationCall()) ||
575         (!isLocalCalling && !CheckCallingUid())) {
576         HILOGE("check permission failed!");
577         return DMS_PERMISSION_DENIED;
578     }
579 
580     std::string srcDevId;
581     std::string dstDevId;
582     std::string bundleName;
583     PARCEL_READ_HELPER(data, String, srcDevId);
584     PARCEL_READ_HELPER(data, String, dstDevId);
585     PARCEL_READ_HELPER(data, String, bundleName);
586     sptr<IRemoteObject> callback = data.ReadRemoteObject();
587     if (callback == nullptr) {
588         HILOGW("read callback failed!");
589         return ERR_NULL_OBJECT;
590     }
591     shared_ptr<AAFwk::WantParams> wantParams(data.ReadParcelable<AAFwk::WantParams>());
592     if (wantParams == nullptr) {
593         HILOGW("wantParams readParcelable failed!");
594         return ERR_NULL_OBJECT;
595     }
596 
597     std::string srcBundleName;
598     std::string continueType;
599     PARCEL_READ_HELPER_NORET(data, String, srcBundleName);
600     PARCEL_READ_HELPER_NORET(data, String, continueType);
601     if (continueType == "") {
602         continueType = DMSContinueRecvMgr::GetInstance().GetContinueType(bundleName);
603     }
604 
605     int32_t result = ERR_OK;
606     AAFwk::MissionInfo missionInfo;
607     if (isLocalCalling) {
608         std::string remoteDeviceId = (IPCSkeleton::GetCallingDeviceID() == srcDevId) ? dstDevId : srcDevId;
609         missionInfo.want.SetParams(*wantParams);
610         bool isFreeInstall = missionInfo.want.GetBoolParam("isFreeInstall", false);
611         if ((!isFreeInstall && IsUsingQos(remoteDeviceId)) ||
612             (isFreeInstall && IsRemoteInstall(remoteDeviceId, bundleName))) {
613             DSchedTransportSoftbusAdapter::GetInstance().SetCallingTokenId(IPCSkeleton::GetCallingTokenID());
614             result = DSchedContinueManager::GetInstance().ContinueMission(
615                 DSchedContinueInfo(srcDevId, srcBundleName, dstDevId, bundleName, continueType),
616                 callback, *wantParams);
617             HILOGI("result = %{public}d", result);
618             PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
619         }
620     }
621     result = ContinueMission(srcDevId, dstDevId, bundleName, callback, *wantParams);
622     HILOGI("result = %{public}d", result);
623     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
624 }
625 
IsRemoteInstall(const std::string &networkId, const std::string &bundleName)626 bool DistributedSchedStub::IsRemoteInstall(const std::string &networkId, const std::string &bundleName)
627 {
628     DmsBundleInfo info;
629     DmsBmStorage::GetInstance()->GetStorageDistributeInfo(networkId, bundleName, info);
630     if (info.bundleName.empty()) {
631         return false;
632     }
633     return true;
634 }
635 
StartContinuationInner(MessageParcel& data, MessageParcel& reply)636 int32_t DistributedSchedStub::StartContinuationInner(MessageParcel& data, MessageParcel& reply)
637 {
638     int64_t saveDataEnd = GetTickCount();
639     DmsContinueTime::GetInstance().SetSaveDataDurationEnd(saveDataEnd);
640 
641     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
642         return DMS_PERMISSION_DENIED;
643     }
644     DmsHiTraceChain hiTraceChain(TraceValue::START_CONTINUATION);
645     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::START_CONTINUATION);
646     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
647     if (want == nullptr) {
648         HILOGW("want readParcelable failed!");
649         return ERR_NULL_OBJECT;
650     }
651     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
652     int32_t missionId = data.ReadInt32();
653     int32_t callerUid = data.ReadInt32();
654     int32_t status = data.ReadInt32();
655     uint32_t accessToken = 0;
656     PARCEL_READ_HELPER(data, Uint32, accessToken);
657     HILOGI("get AccessTokenID = %{public}s", GetAnonymStr(std::to_string(accessToken)).c_str());
658     DistributedSchedPermission::GetInstance().MarkUriPermission(*want, accessToken);
659 
660     // set in ability runtime, used to seperate callings from FA or stage model
661     bool isFA = want->GetBoolParam(FEATURE_ABILITY_FLAG_KEY, false);
662     want->RemoveParam(FEATURE_ABILITY_FLAG_KEY);
663 
664     bool isFreeInstall = DistributedSchedService::GetInstance().GetIsFreeInstall(missionId);
665 
666     int32_t result = (isFA || isFreeInstall || !IsUsingQos(want->GetElement().GetDeviceID())) ?
667         StartContinuation(*want, missionId, callerUid, status, accessToken) :
668         DSchedContinueManager::GetInstance().StartContinuation(*want, missionId, callerUid, status, accessToken);
669 
670     ReportEvent(*want, BehaviorEvent::START_CONTINUATION, result, callerUid);
671     HILOGI("result = %{public}d", result);
672     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
673 }
674 
NotifyCompleteContinuationInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)675 int32_t DistributedSchedStub::NotifyCompleteContinuationInner(MessageParcel& data,
676     [[maybe_unused]] MessageParcel& reply)
677 {
678     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
679         HILOGE("check permission failed!");
680         return DMS_PERMISSION_DENIED;
681     }
682 
683     u16string devId = data.ReadString16();
684     if (devId.empty()) {
685         HILOGE("devId is empty!");
686         return INVALID_PARAMETERS_ERR;
687     }
688     int32_t sessionId = 0;
689     PARCEL_READ_HELPER(data, Int32, sessionId);
690     bool continuationResult = false;
691     PARCEL_READ_HELPER(data, Bool, continuationResult);
692     std::string callerBundleName = data.ReadString();
693     auto dContinue = DSchedContinueManager::GetInstance().GetDSchedContinueByDevId(devId, sessionId);
694     if (dContinue != nullptr) {
695         DSchedContinueManager::GetInstance().NotifyCompleteContinuation(devId, sessionId, continuationResult,
696             callerBundleName);
697     } else {
698         NotifyCompleteContinuation(devId, sessionId, continuationResult);
699     }
700     return ERR_OK;
701 }
702 
IsUsingQos(const std::string& remoteDeviceId)703 bool DistributedSchedStub::IsUsingQos(const std::string& remoteDeviceId)
704 {
705     if (remoteDeviceId.empty()) {
706         HILOGW("remote deviceId empty, using rpc");
707         return false;
708     }
709 
710     DmsVersion thresholdDmsVersion = {QOS_THRESHOLD_VERSION, 0, 0};
711     if (DmsVersionManager::IsRemoteDmsVersionLower(remoteDeviceId, thresholdDmsVersion)) {
712         HILOGW("remote dms not support qos, using rpc");
713         return false;
714     }
715     HILOGI("remote device satisfied qos condition");
716     return true;
717 }
718 
NotifyDSchedEventResultFromRemoteInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)719 int32_t DistributedSchedStub::NotifyDSchedEventResultFromRemoteInner(MessageParcel& data,
720     [[maybe_unused]] MessageParcel& reply)
721 {
722     if (!CheckCallingUid()) {
723         HILOGW("request DENIED!");
724         return DMS_PERMISSION_DENIED;
725     }
726 
727     std::string type = "";
728     PARCEL_READ_HELPER(data, String, type);
729     int32_t dSchedEventResult = -1;
730     PARCEL_READ_HELPER(data, Int32, dSchedEventResult);
731     return NotifyDSchedEventResultFromRemote(type, dSchedEventResult);
732 }
733 
NotifyContinuationResultFromRemoteInner(MessageParcel& data, [[maybe_unused]] MessageParcel& reply)734 int32_t DistributedSchedStub::NotifyContinuationResultFromRemoteInner(MessageParcel& data,
735     [[maybe_unused]] MessageParcel& reply)
736 {
737     if (!CheckCallingUid()) {
738         HILOGW("request DENIED!");
739         return DMS_PERMISSION_DENIED;
740     }
741 
742     int32_t sessionId = 0;
743     PARCEL_READ_HELPER(data, Int32, sessionId);
744     bool continuationResult = false;
745     PARCEL_READ_HELPER(data, Bool, continuationResult);
746     std::string dstInfo;
747     PARCEL_READ_HELPER_NORET(data, String, dstInfo);
748     return NotifyContinuationResultFromRemote(sessionId, continuationResult, dstInfo);
749 }
750 
ConnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)751 int32_t DistributedSchedStub::ConnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
752 {
753     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
754         return DMS_PERMISSION_DENIED;
755     }
756     DmsHiTraceChain hiTraceChain(TraceValue::CONNECT_REMOTE_ABILITY);
757     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::CONNECT_REMOTE_ABILITY);
758     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
759     if (want == nullptr) {
760         HILOGW("want readParcelable failed!");
761         return ERR_NULL_OBJECT;
762     }
763     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
764     sptr<IRemoteObject> connect = data.ReadRemoteObject();
765     int32_t callerUid = 0;
766     PARCEL_READ_HELPER(data, Int32, callerUid);
767     int32_t callerPid = 0;
768     PARCEL_READ_HELPER(data, Int32, callerPid);
769     uint32_t accessToken = 0;
770     PARCEL_READ_HELPER(data, Uint32, accessToken);
771     HILOGD("get callerUid = %{public}d, callerPid = %{public}d, AccessTokenID = %{private}s", callerUid, callerPid,
772         GetAnonymStr(std::to_string(accessToken)).c_str());
773     int32_t result = ConnectRemoteAbility(*want, connect, callerUid, callerPid, accessToken);
774     ReportEvent(*want, BehaviorEvent::CONNECT_REMOTE_ABILITY, result, callerUid);
775     HILOGI("result = %{public}d", result);
776     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
777 }
778 
DisconnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)779 int32_t DistributedSchedStub::DisconnectRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
780 {
781     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
782         return DMS_PERMISSION_DENIED;
783     }
784     DmsHiTraceChain hiTraceChain(TraceValue::DISCONNECT_REMOTE_ABILITY);
785     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::DISCONNECT_REMOTE_ABILITY);
786     sptr<IRemoteObject> connect = data.ReadRemoteObject();
787     int32_t callerUid = 0;
788     PARCEL_READ_HELPER(data, Int32, callerUid);
789     uint32_t accessToken = 0;
790     PARCEL_READ_HELPER(data, Uint32, accessToken);
791     HILOGD("get callerUid = %{public}d, AccessTokenID = %{private}s", callerUid,
792         GetAnonymStr(std::to_string(accessToken)).c_str());
793     int32_t result = DisconnectRemoteAbility(connect, callerUid, accessToken);
794     BehaviorEventParam eventParam = { EventCallingType::LOCAL, BehaviorEvent::DISCONNECT_REMOTE_ABILITY, result };
795     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
796     HILOGI("result = %{public}d", result);
797     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
798 }
799 
ReadDataForConnect(MessageParcel& data, CallerInfo& callerInfo, AccountInfo& accountInfo)800 int32_t DistributedSchedStub::ReadDataForConnect(MessageParcel& data, CallerInfo& callerInfo, AccountInfo& accountInfo)
801 {
802     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
803     PARCEL_READ_HELPER(data, Int32, callerInfo.pid);
804     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
805     callerInfo.callerType = CALLER_TYPE_HARMONY;
806     accountInfo.accountType = data.ReadInt32();
807     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
808     callerInfo.callerAppId = data.ReadString();
809     return ERR_NONE;
810 }
811 
ConnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)812 int32_t DistributedSchedStub::ConnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
813 {
814 #ifdef DMSFWK_INTERACTIVE_ADAPTER
815     if (CheckRemoteOsType(IPCSkeleton::GetCallingDeviceID())) {
816         return ConnectAbilityFromRemoteAdapterInner(data, reply);
817     }
818 #endif
819 
820     if (!CheckCallingUid()) {
821         HILOGW("request DENIED!");
822         return DMS_PERMISSION_DENIED;
823     }
824 
825     shared_ptr<AAFwk::Want> want = ReadDistributedWant(data);
826     if (want == nullptr) {
827         HILOGW("want readParcelable failed!");
828         return ERR_NULL_OBJECT;
829     }
830     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
831 
832     AbilityInfo abilityInfo;
833     sptr<IRemoteObject> connect = nullptr;
834     CallerInfo callerInfo;
835     AccountInfo accountInfo;
836     if (GetConnectAbilityFromRemoteExParam(data, abilityInfo, connect, callerInfo, accountInfo) != ERR_OK) {
837         HILOGE("Get connect ability from remote exParam fail!");
838         return INVALID_PARAMETERS_ERR;
839     }
840 
841     std::string package = abilityInfo.bundleName;
842     std::string deviceId = abilityInfo.deviceId;
843     int64_t begin = GetTickCount();
844     int32_t result = ConnectAbilityFromRemote(*want, abilityInfo, connect, callerInfo, accountInfo);
845     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::CONNECT_REMOTE_ABILITY, result,
846         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerInfo.uid };
847     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
848     HILOGW("result = %{public}d", result);
849     int64_t end = GetTickCount();
850     PARCEL_WRITE_HELPER(reply, Int32, result);
851     PARCEL_WRITE_HELPER(reply, Int64, end - begin);
852     PARCEL_WRITE_HELPER(reply, String, package);
853     PARCEL_WRITE_HELPER(reply, String, deviceId);
854     return ERR_NONE;
855 }
856 
DisconnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)857 int32_t DistributedSchedStub::DisconnectAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
858 {
859 #ifdef DMSFWK_INTERACTIVE_ADAPTER
860     if (CheckRemoteOsType(IPCSkeleton::GetCallingDeviceID())) {
861         return DisconnectAbilityFromRemoteAdapterInner(data, reply);
862     }
863 #endif
864 
865     if (!CheckCallingUid()) {
866         HILOGW("request DENIED!");
867         return DMS_PERMISSION_DENIED;
868     }
869 
870     sptr<IRemoteObject> connect = data.ReadRemoteObject();
871     int32_t uid = 0;
872     PARCEL_READ_HELPER(data, Int32, uid);
873     string sourceDeviceId;
874     PARCEL_READ_HELPER(data, String, sourceDeviceId);
875     int32_t result = DisconnectAbilityFromRemote(connect, uid, sourceDeviceId);
876     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::DISCONNECT_REMOTE_ABILITY, result };
877     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
878     HILOGI("result %{public}d", result);
879     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
880 }
881 
NotifyProcessDiedFromRemoteInner(MessageParcel& data, MessageParcel& reply)882 int32_t DistributedSchedStub::NotifyProcessDiedFromRemoteInner(MessageParcel& data, MessageParcel& reply)
883 {
884     if (!CheckCallingUid()) {
885         HILOGW("request DENIED!");
886         return DMS_PERMISSION_DENIED;
887     }
888 
889     int32_t uid = 0;
890     PARCEL_READ_HELPER(data, Int32, uid);
891     int32_t pid = 0;
892     PARCEL_READ_HELPER(data, Int32, pid);
893     string sourceDeviceId;
894     PARCEL_READ_HELPER(data, String, sourceDeviceId);
895     CallerInfo callerInfo;
896     callerInfo.uid = uid;
897     callerInfo.pid = pid;
898     callerInfo.sourceDeviceId = sourceDeviceId;
899     callerInfo.callerType = CALLER_TYPE_HARMONY;
900     int32_t result = NotifyProcessDiedFromRemote(callerInfo);
901     HILOGI("result %{public}d", result);
902     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
903 }
904 
CheckCallingUid()905 bool DistributedSchedStub::CheckCallingUid()
906 {
907     // never allow non-system uid for distributed request
908     auto callingUid = IPCSkeleton::GetCallingUid();
909     return callingUid < HID_HAP;
910 }
911 
EnforceInterfaceToken(MessageParcel& data)912 bool DistributedSchedStub::EnforceInterfaceToken(MessageParcel& data)
913 {
914     u16string interfaceToken = data.ReadInterfaceToken();
915     return interfaceToken == DMS_STUB_INTERFACE_TOKEN;
916 }
917 
918 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
GetMissionInfosInner(MessageParcel& data, MessageParcel& reply)919 int32_t DistributedSchedStub::GetMissionInfosInner(MessageParcel& data, MessageParcel& reply)
920 {
921     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
922     if (!DistributedSchedPermission::GetInstance().IsFoundationCall() &&
923         !DistributedSchedPermission::GetInstance().IsSceneBoardCall()) {
924         return DMS_PERMISSION_DENIED;
925     }
926     std::u16string deviceId = data.ReadString16();
927     int32_t numMissions = 0;
928     PARCEL_READ_HELPER(data, Int32, numMissions);
929 
930     std::vector<MissionInfo> missionInfos;
931     int32_t result = GetMissionInfos(Str16ToStr8(deviceId), numMissions, missionInfos);
932     HILOGI("result = %{public}d", result);
933     if (result == ERR_NONE) {
934         result = MissionInfoConverter::WriteMissionInfosToParcel(reply, missionInfos) ? ERR_NONE : ERR_FLATTEN_OBJECT;
935     }
936     return result;
937 }
938 
GetRemoteMissionSnapshotInfoInner(MessageParcel& data, MessageParcel& reply)939 int32_t DistributedSchedStub::GetRemoteMissionSnapshotInfoInner(MessageParcel& data, MessageParcel& reply)
940 {
941     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
942     if (!DistributedSchedPermission::GetInstance().IsFoundationCall() &&
943         !DistributedSchedPermission::GetInstance().IsSceneBoardCall()) {
944         return DMS_PERMISSION_DENIED;
945     }
946     string networkId = data.ReadString();
947     if (networkId.empty()) {
948         HILOGE("networkId is empty!");
949         return ERR_FLATTEN_OBJECT;
950     }
951     int32_t missionId = 0;
952     PARCEL_READ_HELPER(data, Int32, missionId);
953     if (missionId < 0) {
954         HILOGE("missionId is invalid");
955         return INVALID_PARAMETERS_ERR;
956     }
957     std::unique_ptr<MissionSnapshot> missionSnapshotPtr = std::make_unique<MissionSnapshot>();
958     int32_t errCode = GetRemoteMissionSnapshotInfo(networkId, missionId, missionSnapshotPtr);
959     if (errCode != ERR_NONE) {
960         HILOGE("get mission snapshot failed!");
961         return ERR_NULL_OBJECT;
962     }
963     PARCEL_WRITE_HELPER(reply, Parcelable, missionSnapshotPtr.get());
964     std::string uuid = DnetworkAdapter::GetInstance()->GetUuidByNetworkId(networkId);
965     if (uuid.empty()) {
966         HILOGE("uuid is empty!");
967         return ERR_NULL_OBJECT;
968     }
969     std::unique_ptr<Snapshot> snapshotPtr = make_unique<Snapshot>();
970     SnapshotConverter::ConvertToSnapshot(*missionSnapshotPtr, snapshotPtr);
971     DistributedSchedMissionManager::GetInstance().EnqueueCachedSnapshotInfo(uuid,
972         missionId, std::move(snapshotPtr));
973     return ERR_NONE;
974 }
975 
RegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply)976 int32_t DistributedSchedStub::RegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply)
977 {
978     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
979         return DMS_PERMISSION_DENIED;
980     }
981     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
982     u16string devId = data.ReadString16();
983     if (devId.empty()) {
984         HILOGW("read deviceId failed!");
985         return INVALID_PARAMETERS_ERR;
986     }
987     sptr<IRemoteObject> missionChangedListener = data.ReadRemoteObject();
988     if (missionChangedListener == nullptr) {
989         HILOGW("read IRemoteObject failed!");
990         return ERR_FLATTEN_OBJECT;
991     }
992     int32_t result = RegisterMissionListener(devId, missionChangedListener);
993     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
994 }
995 
RegisterDSchedEventListenerInner(MessageParcel& data, MessageParcel& reply)996 int32_t DistributedSchedStub::RegisterDSchedEventListenerInner(MessageParcel& data, MessageParcel& reply)
997 {
998     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
999     if (!CheckCallingUid()) {
1000         HILOGW("request DENIED!");
1001         return DMS_PERMISSION_DENIED;
1002     }
1003     DSchedEventType type = static_cast<DSchedEventType>(data.ReadUint8());
1004     sptr<IRemoteObject> dSchedEventListener = data.ReadRemoteObject();
1005     if (dSchedEventListener == nullptr) {
1006         HILOGW("read IRemoteObject failed!");
1007         return ERR_FLATTEN_OBJECT;
1008     }
1009     int32_t result = RegisterDSchedEventListener(type, dSchedEventListener);
1010     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1011 }
1012 
UnRegisterDSchedEventListenerInner(MessageParcel& data, MessageParcel& reply)1013 int32_t DistributedSchedStub::UnRegisterDSchedEventListenerInner(MessageParcel& data, MessageParcel& reply)
1014 {
1015     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
1016     if (!CheckCallingUid()) {
1017         HILOGW("request DENIED!");
1018         return DMS_PERMISSION_DENIED;
1019     }
1020     DSchedEventType type = static_cast<DSchedEventType>(data.ReadUint8());
1021     sptr<IRemoteObject> dSchedEventListener = data.ReadRemoteObject();
1022     if (dSchedEventListener == nullptr) {
1023         HILOGW("read IRemoteObject failed!");
1024         return ERR_FLATTEN_OBJECT;
1025     }
1026     int32_t result = UnRegisterDSchedEventListener(type, dSchedEventListener);
1027     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1028 }
1029 
GetContinueInfoInner(MessageParcel& data, MessageParcel& reply)1030 int32_t DistributedSchedStub::GetContinueInfoInner(MessageParcel& data, MessageParcel& reply)
1031 {
1032     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
1033     std::string dstNetworkId;
1034     std::string srcNetworkId;
1035     int32_t result = DSchedContinueManager::GetInstance().GetContinueInfo(dstNetworkId, srcNetworkId);
1036     PARCEL_WRITE_HELPER(reply, String, dstNetworkId);
1037     PARCEL_WRITE_HELPER(reply, String, srcNetworkId);
1038     return result;
1039 }
1040 
GetEncodeDSchedEventNotify(const EventNotify& event, MessageParcel& reply)1041 int32_t DistributedSchedStub::GetEncodeDSchedEventNotify(const EventNotify& event, MessageParcel& reply)
1042 {
1043     PARCEL_WRITE_HELPER(reply, Int32, event.eventResult_);
1044     PARCEL_WRITE_HELPER(reply, String, event.srcNetworkId_);
1045     PARCEL_WRITE_HELPER(reply, String, event.dstNetworkId_);
1046     PARCEL_WRITE_HELPER(reply, String, event.srcBundleName_);
1047     PARCEL_WRITE_HELPER(reply, String, event.srcModuleName_);
1048     PARCEL_WRITE_HELPER(reply, String, event.srcAbilityName_);
1049     PARCEL_WRITE_HELPER(reply, String, event.destBundleName_);
1050     PARCEL_WRITE_HELPER(reply, String, event.destModuleName_);
1051     PARCEL_WRITE_HELPER(reply, String, event.destAbilityName_);
1052     PARCEL_WRITE_HELPER(reply, Int32, event.dSchedEventType_);
1053     PARCEL_WRITE_HELPER(reply, Int32, event.state_);
1054     return ERR_OK;
1055 }
1056 
GetDSchedEventInfoInner(MessageParcel& data, MessageParcel& reply)1057 int32_t DistributedSchedStub::GetDSchedEventInfoInner(MessageParcel& data, MessageParcel& reply)
1058 {
1059     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
1060     DSchedEventType type = static_cast<DSchedEventType>(data.ReadInt32());
1061     std::vector<EventNotify> eventInfos;
1062     int32_t result = GetDSchedEventInfo(type, eventInfos);
1063 
1064     PARCEL_WRITE_HELPER(reply, Int32, result);
1065     PARCEL_WRITE_HELPER(reply, Uint32, eventInfos.size());
1066     for (const auto &event : eventInfos) {
1067         result = GetEncodeDSchedEventNotify(event, reply);
1068         if (result != ERR_OK) {
1069             HILOGE("Get encode Dms event notify failed!");
1070             return DMS_WRITE_FILE_FAILED_ERR;
1071         }
1072     }
1073     return result;
1074 }
1075 
RegisterOnListenerInner(MessageParcel& data, MessageParcel& reply)1076 int32_t DistributedSchedStub::RegisterOnListenerInner(MessageParcel& data, MessageParcel& reply)
1077 {
1078     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1079         return DMS_PERMISSION_DENIED;
1080     }
1081     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
1082     string type = data.ReadString();
1083     if (type.empty()) {
1084         HILOGW("read type failed!");
1085         return ERR_FLATTEN_OBJECT;
1086     }
1087     sptr<IRemoteObject> onListener = data.ReadRemoteObject();
1088     if (onListener == nullptr) {
1089         HILOGW("read IRemoteObject failed!");
1090         return ERR_FLATTEN_OBJECT;
1091     }
1092     int32_t callingUid = data.ReadInt32();
1093     if (callingUid < 0) {
1094         HILOGW("read callingUid failed!");
1095         return ERR_FLATTEN_OBJECT;
1096     }
1097     int32_t result = RegisterOnListener(type, onListener, callingUid);
1098     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1099 }
1100 
RegisterOffListenerInner(MessageParcel& data, MessageParcel& reply)1101 int32_t DistributedSchedStub::RegisterOffListenerInner(MessageParcel& data, MessageParcel& reply)
1102 {
1103     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1104         return DMS_PERMISSION_DENIED;
1105     }
1106     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
1107     string type = data.ReadString();
1108     if (type.empty()) {
1109         HILOGW("read type failed!");
1110         return ERR_FLATTEN_OBJECT;
1111     }
1112     sptr<IRemoteObject> onListener = data.ReadRemoteObject();
1113     if (onListener == nullptr) {
1114         HILOGW("read IRemoteObject failed!");
1115         return ERR_FLATTEN_OBJECT;
1116     }
1117     int32_t callingUid = data.ReadInt32();
1118     if (callingUid < 0) {
1119         HILOGW("read callingUid failed!");
1120         return ERR_FLATTEN_OBJECT;
1121     }
1122     int32_t result = RegisterOffListener(type, onListener, callingUid);
1123     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1124 }
1125 
UnRegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply)1126 int32_t DistributedSchedStub::UnRegisterMissionListenerInner(MessageParcel& data, MessageParcel& reply)
1127 {
1128     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1129         return DMS_PERMISSION_DENIED;
1130     }
1131     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
1132     u16string devId = data.ReadString16();
1133     if (devId.empty()) {
1134         HILOGW("read deviceId failed!");
1135         return INVALID_PARAMETERS_ERR;
1136     }
1137     sptr<IRemoteObject> missionChangedListener = data.ReadRemoteObject();
1138     if (missionChangedListener == nullptr) {
1139         HILOGW("read IRemoteObject failed!");
1140         return ERR_FLATTEN_OBJECT;
1141     }
1142     int32_t result = UnRegisterMissionListener(devId, missionChangedListener);
1143     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1144 }
1145 
StartSyncMissionsFromRemoteInner(MessageParcel& data, MessageParcel& reply)1146 int32_t DistributedSchedStub::StartSyncMissionsFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1147 {
1148     if (!CheckCallingUid()) {
1149         HILOGW("request DENIED!");
1150         return DMS_PERMISSION_DENIED;
1151     }
1152     CallerInfo callerInfo;
1153     if (!CallerInfoUnmarshalling(callerInfo, data)) {
1154         HILOGW("read callerInfo failed!");
1155         return ERR_FLATTEN_OBJECT;
1156     }
1157     std::vector<DstbMissionInfo> missionInfos;
1158     if (StartSyncMissionsFromRemote(callerInfo, missionInfos) != ERR_NONE) {
1159         return ERR_FLATTEN_OBJECT;
1160     }
1161     if (!reply.WriteInt32(VERSION)) {
1162         return ERR_FLATTEN_OBJECT;
1163     }
1164     if (!DstbMissionInfo::WriteDstbMissionInfosToParcel(reply, missionInfos)) {
1165         HILOGE("write mission info failed!");
1166         return ERR_FLATTEN_OBJECT;
1167     }
1168     return ERR_NONE;
1169 }
1170 
StopSyncRemoteMissionsInner(MessageParcel& data, MessageParcel& reply)1171 int32_t DistributedSchedStub::StopSyncRemoteMissionsInner(MessageParcel& data, MessageParcel& reply)
1172 {
1173     HILOGI("[PerformanceTest] called, IPC end = %{public}" PRId64, GetTickCount());
1174     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1175         return DMS_PERMISSION_DENIED;
1176     }
1177     u16string devId = data.ReadString16();
1178     if (devId.empty()) {
1179         HILOGW("read deviceId failed!");
1180         return INVALID_PARAMETERS_ERR;
1181     }
1182     int32_t result = StopSyncRemoteMissions(Str16ToStr8(devId));
1183     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1184 }
1185 
StopSyncMissionsFromRemoteInner(MessageParcel& data, MessageParcel& reply)1186 int32_t DistributedSchedStub::StopSyncMissionsFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1187 {
1188     if (!CheckCallingUid()) {
1189         HILOGW("request DENIED!");
1190         return DMS_PERMISSION_DENIED;
1191     }
1192     CallerInfo callerInfo;
1193     if (!CallerInfoUnmarshalling(callerInfo, data)) {
1194         HILOGW("read callerInfo failed!");
1195         return ERR_FLATTEN_OBJECT;
1196     }
1197     int32_t result = StopSyncMissionsFromRemote(callerInfo);
1198     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1199 }
1200 
NotifyMissionsChangedFromRemoteInner(MessageParcel& data, MessageParcel& reply)1201 int32_t DistributedSchedStub::NotifyMissionsChangedFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1202 {
1203     if (!CheckCallingUid()) {
1204         HILOGW("request DENIED!");
1205         return DMS_PERMISSION_DENIED;
1206     }
1207     int32_t version = data.ReadInt32();
1208     HILOGD("version is %{public}d", version);
1209     std::vector<DstbMissionInfo> missionInfos;
1210     if (!DstbMissionInfo::ReadDstbMissionInfosFromParcel(data, missionInfos)) {
1211         return ERR_FLATTEN_OBJECT;
1212     }
1213     CallerInfo callerInfo;
1214     callerInfo.sourceDeviceId = data.ReadString();
1215     callerInfo.uid = data.ReadInt32();
1216     callerInfo.pid = data.ReadInt32();
1217     callerInfo.dmsVersion = data.ReadInt32();
1218     int32_t result = NotifyMissionsChangedFromRemote(missionInfos, callerInfo);
1219     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1220 }
1221 
StartSyncRemoteMissionsInner(MessageParcel& data, MessageParcel& reply)1222 int32_t DistributedSchedStub::StartSyncRemoteMissionsInner(MessageParcel& data, MessageParcel& reply)
1223 {
1224     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1225         return DMS_PERMISSION_DENIED;
1226     }
1227     HILOGI("[PerformanceTest], IPC end = %{public}" PRId64, GetTickCount());
1228     u16string devId = data.ReadString16();
1229     if (devId.empty()) {
1230         HILOGW("read deviceId failed!");
1231         return INVALID_PARAMETERS_ERR;
1232     }
1233     string deviceId = Str16ToStr8(devId);
1234     bool fixConflict = data.ReadBool();
1235     int64_t tag = data.ReadInt64();
1236     int32_t result = StartSyncRemoteMissions(deviceId, fixConflict, tag);
1237     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1238 }
1239 
SetMissionContinueStateInner(MessageParcel& data, MessageParcel& reply)1240 int32_t DistributedSchedStub::SetMissionContinueStateInner(MessageParcel& data, MessageParcel& reply)
1241 {
1242     if (!CheckCallingUid() && !DistributedSchedPermission::GetInstance().IsSceneBoardCall()) {
1243         HILOGW("request DENIED!");
1244         return DMS_PERMISSION_DENIED;
1245     }
1246 
1247     int32_t missionId = 0;
1248     PARCEL_READ_HELPER(data, Int32, missionId);
1249     int32_t state = 0;
1250     PARCEL_READ_HELPER(data, Int32, state);
1251 
1252     int32_t result = SetMissionContinueState(missionId, static_cast<AAFwk::ContinueState>(state));
1253     HILOGI("result %{public}d", result);
1254     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1255 }
1256 #endif
1257 
CallerInfoUnmarshalling(CallerInfo& callerInfo, MessageParcel& data)1258 bool DistributedSchedStub::CallerInfoUnmarshalling(CallerInfo& callerInfo, MessageParcel& data)
1259 {
1260     int32_t uid = -1;
1261     PARCEL_READ_HELPER_RET(data, Int32, uid, false);
1262     int32_t pid = -1;
1263     PARCEL_READ_HELPER_RET(data, Int32, pid, false);
1264     int32_t callerType = CALLER_TYPE_NONE;
1265     PARCEL_READ_HELPER_RET(data, Int32, callerType, false);
1266     std::string sourceDeviceId;
1267     PARCEL_READ_HELPER_RET(data, String, sourceDeviceId, false);
1268     HILOGI("sourceDeviceId = %{public}s", GetAnonymStr(sourceDeviceId).c_str());
1269     int32_t duid = -1;
1270     PARCEL_READ_HELPER_RET(data, Int32, duid, false);
1271     std::string callerAppId;
1272     PARCEL_READ_HELPER_RET(data, String, callerAppId, false);
1273     int32_t version = -1;
1274     PARCEL_READ_HELPER_RET(data, Int32, version, false);
1275     callerInfo.uid = uid;
1276     callerInfo.pid = pid;
1277     callerInfo.callerType = callerType;
1278     callerInfo.sourceDeviceId = sourceDeviceId;
1279     callerInfo.duid = duid;
1280     callerInfo.callerAppId = callerAppId;
1281     callerInfo.dmsVersion = version;
1282     return true;
1283 }
1284 
StartRemoteAbilityByCallInner(MessageParcel& data, MessageParcel& reply)1285 int32_t DistributedSchedStub::StartRemoteAbilityByCallInner(MessageParcel& data, MessageParcel& reply)
1286 {
1287     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1288         return DMS_PERMISSION_DENIED;
1289     }
1290     DmsHiTraceChain hiTraceChain(TraceValue::START_REMOTE_ABILITY_BYCALL);
1291     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::START_REMOTE_ABILITY_BYCALL);
1292     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
1293     if (want == nullptr) {
1294         HILOGW("want readParcelable failed!");
1295         return ERR_NULL_OBJECT;
1296     }
1297     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
1298     sptr<IRemoteObject> connect = data.ReadRemoteObject();
1299     int32_t callerUid = 0;
1300     PARCEL_READ_HELPER(data, Int32, callerUid);
1301     int32_t callerPid = 0;
1302     PARCEL_READ_HELPER(data, Int32, callerPid);
1303     uint32_t accessToken = 0;
1304     PARCEL_READ_HELPER(data, Uint32, accessToken);
1305     int32_t result = StartRemoteAbilityByCall(*want, connect, callerUid, callerPid, accessToken);
1306     ReportEvent(*want, BehaviorEvent::START_REMOTE_ABILITY_BYCALL, result, callerUid);
1307     HILOGI("result = %{public}d", result);
1308     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1309 }
1310 
ReleaseRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)1311 int32_t DistributedSchedStub::ReleaseRemoteAbilityInner(MessageParcel& data, MessageParcel& reply)
1312 {
1313     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1314         return DMS_PERMISSION_DENIED;
1315     }
1316     DmsHiTraceChain hiTraceChain(TraceValue::RELEASE_REMOTE_ABILITY);
1317     HITRACE_METER_NAME(TraceTag::DSCHED, TraceValue::RELEASE_REMOTE_ABILITY);
1318     sptr<IRemoteObject> connect = data.ReadRemoteObject();
1319     shared_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
1320     if (element == nullptr) {
1321         HILOGE("ReleaseRemoteAbilityInner receive element is nullptr");
1322         return ERR_INVALID_VALUE;
1323     }
1324     int32_t result = ReleaseRemoteAbility(connect, *element);
1325     BehaviorEventParam eventParam = { EventCallingType::LOCAL, BehaviorEvent::RELEASE_REMOTE_ABILITY, result,
1326         element->GetBundleName(), element->GetAbilityName() };
1327     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
1328     HILOGI("result = %{public}d", result);
1329     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1330 }
1331 
StartAbilityByCallFromRemoteInner(MessageParcel& data, MessageParcel& reply)1332 int32_t DistributedSchedStub::StartAbilityByCallFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1333 {
1334     if (!CheckCallingUid()) {
1335         HILOGW("request DENIED!");
1336         return DMS_PERMISSION_DENIED;
1337     }
1338 
1339     sptr<IRemoteObject> connect = data.ReadRemoteObject();
1340     CallerInfo callerInfo;
1341     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
1342     PARCEL_READ_HELPER(data, Int32, callerInfo.pid);
1343     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
1344     AccountInfo accountInfo;
1345     accountInfo.accountType = data.ReadInt32();
1346     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
1347     callerInfo.callerAppId = data.ReadString();
1348     std::string extraInfo = data.ReadString();
1349     if (extraInfo.empty()) {
1350         HILOGW("read extraInfo failed!");
1351         return ERR_NULL_OBJECT;
1352     }
1353     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
1354     if (!extraInfoJson.is_discarded()) {
1355         SaveExtraInfo(extraInfoJson, callerInfo, accountInfo);
1356         HILOGD("parse extra info");
1357     }
1358     shared_ptr<DistributedWant> dstbWant(data.ReadParcelable<DistributedWant>());
1359     shared_ptr<AAFwk::Want> want = nullptr;
1360     if (dstbWant != nullptr) {
1361         want = dstbWant->ToWant();
1362     }
1363     if (want == nullptr) {
1364         HILOGW("want readParcelable failed!");
1365         return ERR_NULL_OBJECT;
1366     }
1367     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
1368     int32_t result = StartAbilityByCallFromRemote(*want, connect, callerInfo, accountInfo);
1369     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::START_REMOTE_ABILITY_BYCALL, result,
1370         want->GetElement().GetBundleName(), want->GetElement().GetAbilityName(), callerInfo.uid };
1371     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
1372     HILOGI("result = %{public}d", result);
1373     PARCEL_WRITE_HELPER(reply, Int32, result);
1374     return ERR_NONE;
1375 }
1376 
ReleaseAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)1377 int32_t DistributedSchedStub::ReleaseAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1378 {
1379     if (!CheckCallingUid()) {
1380         HILOGW("request DENIED!");
1381         return DMS_PERMISSION_DENIED;
1382     }
1383 
1384     sptr<IRemoteObject> connect = data.ReadRemoteObject();
1385     shared_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
1386     if (element == nullptr) {
1387         HILOGE("ReleaseAbilityFromRemoteInner receive element is nullptr");
1388         return ERR_INVALID_VALUE;
1389     }
1390     CallerInfo callerInfo;
1391     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
1392     std::string extraInfo;
1393     PARCEL_READ_HELPER(data, String, extraInfo);
1394     int32_t result = ReleaseAbilityFromRemote(connect, *element, callerInfo);
1395     BehaviorEventParam eventParam = { EventCallingType::REMOTE, BehaviorEvent::RELEASE_REMOTE_ABILITY, result,
1396         element->GetBundleName(), element->GetAbilityName() };
1397     DmsHiSysEventReport::ReportBehaviorEvent(eventParam);
1398     HILOGI("result %{public}d", result);
1399     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1400 }
1401 
1402 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
StartRemoteShareFormInner(MessageParcel& data, MessageParcel& reply)1403 int32_t DistributedSchedStub::StartRemoteShareFormInner(MessageParcel& data, MessageParcel& reply)
1404 {
1405     HILOGD("SHAREFORM:: func call");
1406     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1407         return DMS_PERMISSION_DENIED;
1408     }
1409 
1410     std::string deviceId = "";
1411     PARCEL_READ_HELPER(data, String, deviceId);
1412     shared_ptr<AppExecFwk::FormShareInfo> formShareInfo(data.ReadParcelable<AppExecFwk::FormShareInfo>());
1413     if (formShareInfo == nullptr) {
1414         HILOGW("SHARE_FORM readParcelable failed!");
1415         PARCEL_WRITE_REPLY_NOERROR(reply, Int32, static_cast<int32_t>(ERR_FLATTEN_OBJECT));
1416         return ERR_FLATTEN_OBJECT;
1417     }
1418 
1419     int32_t result = StartRemoteShareForm(deviceId, *formShareInfo);
1420     HILOGI("result = %{public}d", result);
1421     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1422 }
1423 
StartShareFormFromRemoteInner(MessageParcel& data, MessageParcel& reply)1424 int32_t DistributedSchedStub::StartShareFormFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1425 {
1426     HILOGD("SHAREFORM:: func call");
1427     if (!CheckCallingUid()) {
1428         HILOGW("request DENIED!");
1429         PARCEL_WRITE_REPLY_NOERROR(reply, Int32, static_cast<int32_t>(DMS_PERMISSION_DENIED));
1430         return DMS_PERMISSION_DENIED;
1431     }
1432 
1433     std::string deviceId = "";
1434     PARCEL_READ_HELPER(data, String, deviceId);
1435     shared_ptr<AppExecFwk::FormShareInfo> formShareInfo(data.ReadParcelable<AppExecFwk::FormShareInfo>());
1436     if (formShareInfo == nullptr) {
1437         HILOGW("SHARE_FORM readParcelable failed!");
1438         PARCEL_WRITE_REPLY_NOERROR(reply, Int32, static_cast<int32_t>(ERR_FLATTEN_OBJECT));
1439         return ERR_FLATTEN_OBJECT;
1440     }
1441 
1442     int32_t result = StartShareFormFromRemote(deviceId, *formShareInfo);
1443     HILOGI("result = %{public}d", result);
1444     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1445 }
1446 #endif
1447 
GetDistributedComponentListInner(MessageParcel& data, MessageParcel& reply)1448 int32_t DistributedSchedStub::GetDistributedComponentListInner(MessageParcel& data, MessageParcel& reply)
1449 {
1450     if (!CheckCallingUid()) {
1451         HILOGW("request DENIED!");
1452         return DMS_PERMISSION_DENIED;
1453     }
1454     std::vector<std::string> distributedComponents;
1455     int32_t result = GetDistributedComponentList(distributedComponents);
1456     HILOGI("result = %{public}d", result);
1457     PARCEL_WRITE_HELPER(reply, Int32, result);
1458     PARCEL_WRITE_HELPER(reply, StringVector, distributedComponents);
1459     return ERR_NONE;
1460 }
1461 
NotifyStateChangedFromRemoteInner(MessageParcel& data, MessageParcel& reply)1462 int32_t DistributedSchedStub::NotifyStateChangedFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1463 {
1464     if (!CheckCallingUid()) {
1465         HILOGW("request DENIED!");
1466         return DMS_PERMISSION_DENIED;
1467     }
1468     int32_t abilityState = 0;
1469     PARCEL_READ_HELPER(data, Int32, abilityState);
1470     int32_t connectToken = 0;
1471     PARCEL_READ_HELPER(data, Int32, connectToken);
1472     shared_ptr<AppExecFwk::ElementName> element(data.ReadParcelable<AppExecFwk::ElementName>());
1473     if (element == nullptr) {
1474         HILOGE("NotifyStateChangedFromRemoteInner receive element is nullptr");
1475         return ERR_INVALID_VALUE;
1476     }
1477     int32_t result = NotifyStateChangedFromRemote(abilityState, connectToken, *element);
1478     HILOGI("result = %{public}d", result);
1479     PARCEL_WRITE_HELPER(reply, Int32, result);
1480     return ERR_NONE;
1481 }
1482 
StartRemoteFreeInstallInner(MessageParcel& data, MessageParcel& reply)1483 int32_t DistributedSchedStub::StartRemoteFreeInstallInner(MessageParcel& data, MessageParcel& reply)
1484 {
1485     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1486         return DMS_PERMISSION_DENIED;
1487     }
1488     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
1489     if (want == nullptr) {
1490         HILOGE("want readParcelable failed!");
1491         return ERR_NULL_OBJECT;
1492     }
1493     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
1494     int32_t callerUid = 0;
1495     int32_t requestCode = 0;
1496     uint32_t accessToken = 0;
1497     PARCEL_READ_HELPER(data, Int32, callerUid);
1498     PARCEL_READ_HELPER(data, Int32, requestCode);
1499     PARCEL_READ_HELPER(data, Uint32, accessToken);
1500     sptr<IRemoteObject> callback = data.ReadRemoteObject();
1501     if (callback == nullptr) {
1502         HILOGE("read callback failed!");
1503         return ERR_NULL_OBJECT;
1504     }
1505     int32_t result = StartRemoteFreeInstall(*want, callerUid, requestCode, accessToken, callback);
1506     HILOGI("result = %{public}d", result);
1507     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1508     return ERR_NONE;
1509 }
1510 
ReadDataForFreeInstall(MessageParcel& data, CallerInfo &callerInfo, AccountInfo &accountInfo, int64_t &taskId)1511 int32_t DistributedSchedStub::ReadDataForFreeInstall(MessageParcel& data, CallerInfo &callerInfo,
1512     AccountInfo &accountInfo, int64_t &taskId)
1513 {
1514     callerInfo.callerType = CALLER_TYPE_HARMONY;
1515     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
1516     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
1517     accountInfo.accountType = data.ReadInt32();
1518     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
1519     callerInfo.callerAppId = data.ReadString();
1520     PARCEL_READ_HELPER(data, Int64, taskId);
1521     return ERR_NONE;
1522 }
1523 
CreateJsonObject(std::string& extraInfo, CallerInfo& callerInfo, AccountInfo& accountInfo)1524 int32_t DistributedSchedStub::CreateJsonObject(std::string& extraInfo, CallerInfo& callerInfo,
1525     AccountInfo& accountInfo)
1526 {
1527     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
1528     int32_t requestCode = DEFAULT_REQUEST_CODE;
1529     if (!extraInfoJson.is_discarded()) {
1530         SaveExtraInfo(extraInfoJson, callerInfo, accountInfo);
1531         if (extraInfoJson.find(EXTRO_INFO_JSON_KEY_REQUEST_CODE) != extraInfoJson.end() &&
1532             extraInfoJson[EXTRO_INFO_JSON_KEY_REQUEST_CODE].is_number_integer()) {
1533             requestCode = extraInfoJson[EXTRO_INFO_JSON_KEY_REQUEST_CODE];
1534             HILOGD("parse extra info, requestCode = %{public}d", requestCode);
1535         }
1536     }
1537     return requestCode;
1538 }
1539 
StartFreeInstallFromRemoteInner(MessageParcel& data, MessageParcel& reply)1540 int32_t DistributedSchedStub::StartFreeInstallFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1541 {
1542     if (!CheckCallingUid()) {
1543         HILOGW("request DENIED!");
1544         return DMS_PERMISSION_DENIED;
1545     }
1546     shared_ptr<AAFwk::Want> want = ReadDistributedWant(data);
1547     if (want == nullptr) {
1548         HILOGE("want readParcelable failed!");
1549         return ERR_NULL_OBJECT;
1550     }
1551     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
1552     int64_t begin = GetTickCount();
1553     CallerInfo callerInfo = {.accessToken = 0};
1554     AccountInfo accountInfo = {};
1555     int64_t taskId = 0;
1556     int32_t result = ReadDataForFreeInstall(data, callerInfo, accountInfo, taskId);
1557     if (result != ERR_NONE) {
1558         return result;
1559     }
1560     shared_ptr<DistributedWant> cmpDstbWant(data.ReadParcelable<DistributedWant>());
1561     shared_ptr<AAFwk::Want> cmpWant = nullptr;
1562     if (cmpDstbWant != nullptr) {
1563         cmpWant = cmpDstbWant->ToWant();
1564     }
1565     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(cmpWant);
1566     std::string extraInfo = data.ReadString();
1567     if (extraInfo.empty()) {
1568         HILOGD("extra info is empty!");
1569     }
1570     int32_t requestCode = CreateJsonObject(extraInfo, callerInfo, accountInfo);
1571     FreeInstallInfo info = {
1572         .want = *want, .requestCode = requestCode, .callerInfo = callerInfo, .accountInfo = accountInfo};
1573     info.want.RemoveParam(PARAM_FREEINSTALL_APPID);
1574     info.want.SetParam(PARAM_FREEINSTALL_APPID, callerInfo.callerAppId);
1575     info.want.RemoveParam(PARAM_FREEINSTALL_BUNDLENAMES);
1576     if (cmpWant == nullptr) {
1577         return ERR_NULL_OBJECT;
1578     }
1579     info.want.SetParam(
1580         PARAM_FREEINSTALL_BUNDLENAMES, (*cmpWant).GetStringArrayParam(CMPT_PARAM_FREEINSTALL_BUNDLENAMES));
1581     result = StartFreeInstallFromRemote(info, taskId);
1582     HILOGI("result = %{public}d", result);
1583     PARCEL_WRITE_HELPER(reply, Int32, result);
1584     int64_t end = GetTickCount();
1585     PARCEL_WRITE_HELPER(reply, Int64, end - begin);
1586     return ERR_NONE;
1587 }
1588 
NotifyCompleteFreeInstallFromRemoteInner(MessageParcel& data, MessageParcel& reply)1589 int32_t DistributedSchedStub::NotifyCompleteFreeInstallFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1590 {
1591     if (!CheckCallingUid()) {
1592         HILOGW("request DENIED!");
1593         return DMS_PERMISSION_DENIED;
1594     }
1595     int64_t taskId = 0;
1596     int32_t resultCode = 0;
1597     PARCEL_READ_HELPER(data, Int64, taskId);
1598     PARCEL_READ_HELPER(data, Int32, resultCode);
1599     int32_t result = NotifyCompleteFreeInstallFromRemote(taskId, resultCode);
1600     HILOGI("result = %{public}d", result);
1601     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1602 }
1603 
StopRemoteExtensionAbilityInner(MessageParcel& data, MessageParcel& reply)1604 int32_t DistributedSchedStub::StopRemoteExtensionAbilityInner(MessageParcel& data, MessageParcel& reply)
1605 {
1606     if (!DistributedSchedPermission::GetInstance().IsFoundationCall()) {
1607         HILOGW("request DENIED!");
1608         return DMS_PERMISSION_DENIED;
1609     }
1610     shared_ptr<AAFwk::Want> want(data.ReadParcelable<AAFwk::Want>());
1611     if (want == nullptr) {
1612         HILOGW("Stop reomte extension ability want readParcelable failed!");
1613         return ERR_NULL_OBJECT;
1614     }
1615     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
1616     int32_t callerUid = 0;
1617     PARCEL_READ_HELPER(data, Int32, callerUid);
1618     uint32_t accessToken = 0;
1619     PARCEL_READ_HELPER(data, Uint32, accessToken);
1620     int32_t serviceType = 0;
1621     PARCEL_READ_HELPER(data, Int32, serviceType);
1622     HILOGD("get callerUid = %{private}d, AccessTokenID = %{private}s", callerUid,
1623         GetAnonymStr(std::to_string(accessToken)).c_str());
1624     auto result = StopRemoteExtensionAbility(*want, callerUid, accessToken, serviceType);
1625     HILOGD("StartRemoteAbilityInner result = %{public}d", result);
1626     PARCEL_WRITE_REPLY_NOERROR(reply, Int32, result);
1627 }
1628 
StopExtensionAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)1629 int32_t DistributedSchedStub::StopExtensionAbilityFromRemoteInner(MessageParcel& data, MessageParcel& reply)
1630 {
1631     if (!CheckCallingUid()) {
1632         HILOGW("request DENIED!");
1633         return DMS_PERMISSION_DENIED;
1634     }
1635     shared_ptr<DistributedWant> dstbWant(data.ReadParcelable<DistributedWant>());
1636     shared_ptr<AAFwk::Want> want = nullptr;
1637     if (dstbWant != nullptr) {
1638         want = dstbWant->ToWant();
1639     }
1640     if (want == nullptr) {
1641         HILOGW("want readParcelable failed!");
1642         return ERR_NULL_OBJECT;
1643     }
1644     DistributedSchedPermission::GetInstance().RemoveRemoteObjectFromWant(want);
1645     int32_t serviceType = 0;
1646     PARCEL_READ_HELPER(data, Int32, serviceType);
1647     CallerInfo callerInfo;
1648     PARCEL_READ_HELPER(data, Int32, callerInfo.uid);
1649     PARCEL_READ_HELPER(data, String, callerInfo.sourceDeviceId);
1650     callerInfo.callerType = CALLER_TYPE_HARMONY;
1651     AccountInfo accountInfo;
1652     accountInfo.accountType = data.ReadInt32();
1653     PARCEL_READ_HELPER(data, StringVector, &accountInfo.groupIdList);
1654     callerInfo.callerAppId = data.ReadString();
1655     std::string extraInfo = data.ReadString();
1656     if (extraInfo.empty()) {
1657         HILOGD("extra info is empty!");
1658     }
1659     nlohmann::json extraInfoJson = nlohmann::json::parse(extraInfo, nullptr, false);
1660     if (!extraInfoJson.is_discarded() && extraInfoJson.find(EXTRO_INFO_JSON_KEY_ACCESS_TOKEN) != extraInfoJson.end() &&
1661         extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN].is_number_unsigned()) {
1662         uint32_t accessToken = extraInfoJson[EXTRO_INFO_JSON_KEY_ACCESS_TOKEN];
1663         callerInfo.accessToken = accessToken;
1664         HILOGD("parse extra info, accessTokenID = %{private}s", GetAnonymStr(std::to_string(accessToken)).c_str());
1665     }
1666     auto result = StopExtensionAbilityFromRemote(*want, callerInfo, accountInfo, serviceType);
1667     HILOGD("result = %{public}d", result);
1668     PARCEL_WRITE_HELPER(reply, Int32, result);
1669     return ERR_NONE;
1670 }
1671 
1672 #ifdef DMSFWK_INTERACTIVE_ADAPTER
CheckDmsExtensionCallingUid()1673 bool DistributedSchedStub::CheckDmsExtensionCallingUid()
1674 {
1675     // never allow non-system uid for distributed request
1676     constexpr int32_t MULTIUSER_HAP_PER_USER_RANGE_EXT = 100000;
1677     constexpr int32_t HID_HAP_EXT = 10000;
1678     auto callingUid = IPCSkeleton::GetCallingUid();
1679     auto uid = callingUid % MULTIUSER_HAP_PER_USER_RANGE_EXT;
1680     return uid < HID_HAP_EXT;
1681 }
1682 
StartAbilityFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)1683 int32_t DistributedSchedStub::StartAbilityFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)
1684 {
1685     if (!CheckDmsExtensionCallingUid()) {
1686         HILOGW("Start ability from remote adapter request DENIED!");
1687         return DMS_PERMISSION_DENIED;
1688     }
1689 
1690     int32_t result = StartAbilityFromRemoteAdapter(data, reply);
1691     HILOGI("Start ability from remote adapter result = %{public}d", result);
1692     return result;
1693 }
1694 
StopAbilityFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)1695 int32_t DistributedSchedStub::StopAbilityFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)
1696 {
1697     if (!CheckDmsExtensionCallingUid()) {
1698         HILOGW("Stop ability from remote adapter request DENIED!");
1699         return DMS_PERMISSION_DENIED;
1700     }
1701 
1702     int32_t result = StopAbilityFromRemoteAdapter(data, reply);
1703     HILOGI("Stop ability from remote adapter result = %{public}d", result);
1704     return ERR_OK;
1705 }
1706 
ConnectAbilityFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)1707 int32_t DistributedSchedStub::ConnectAbilityFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)
1708 {
1709     if (!CheckDmsExtensionCallingUid()) {
1710         HILOGW("Connect ability from remote adapter request DENIED!");
1711         return DMS_PERMISSION_DENIED;
1712     }
1713 
1714     int32_t result = ConnectAbilityFromRemoteAdapter(data, reply);
1715     HILOGI("Connect ability from remote adapter result = %{public}d", result);
1716     return ERR_OK;
1717 }
1718 
DisconnectAbilityFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)1719 int32_t DistributedSchedStub::DisconnectAbilityFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)
1720 {
1721     if (!CheckDmsExtensionCallingUid()) {
1722         HILOGW("Disconnect ability from remote adapter request DENIED!");
1723         return DMS_PERMISSION_DENIED;
1724     }
1725 
1726     int32_t result = DisconnectAbilityFromRemoteAdapter(data, reply);
1727     HILOGI("Disconnect ability from remote adapter result = %{public}d", result);
1728     return ERR_OK;
1729 }
1730 
NotifyAbilityLifecycleChangedFromRemoteAdapterInner(MessageParcel& data, MessageParcel& reply)1731 int32_t DistributedSchedStub::NotifyAbilityLifecycleChangedFromRemoteAdapterInner(MessageParcel& data,
1732     MessageParcel& reply)
1733 {
1734     if (!CheckDmsExtensionCallingUid()) {
1735         HILOGW("Disconnect ability from remote adapter request DENIED!");
1736         return DMS_PERMISSION_DENIED;
1737     }
1738 
1739     int32_t result = NotifyAbilityLifecycleChangedFromRemoteAdapter(data, reply);
1740     HILOGI("Disconnect ability from remote adapter result = %{public}d", result);
1741     return ERR_OK;
1742 }
1743 #endif
1744 } // namespace DistributedSchedule
1745 } // namespace OHOS
1746