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