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