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