1686862fbSopenharmony_ci/*
2686862fbSopenharmony_ci * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
3686862fbSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4686862fbSopenharmony_ci * you may not use this file except in compliance with the License.
5686862fbSopenharmony_ci * You may obtain a copy of the License at
6686862fbSopenharmony_ci *
7686862fbSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8686862fbSopenharmony_ci *
9686862fbSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10686862fbSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11686862fbSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12686862fbSopenharmony_ci * See the License for the specific language governing permissions and
13686862fbSopenharmony_ci * limitations under the License.
14686862fbSopenharmony_ci */
15686862fbSopenharmony_ci
16686862fbSopenharmony_ci#include "distributed_sched_service.h"
17686862fbSopenharmony_ci
18686862fbSopenharmony_ci#include <algorithm>
19686862fbSopenharmony_ci#include <cinttypes>
20686862fbSopenharmony_ci#include <dlfcn.h>
21686862fbSopenharmony_ci#include <unistd.h>
22686862fbSopenharmony_ci
23686862fbSopenharmony_ci#include "ability_manager_client.h"
24686862fbSopenharmony_ci#include "ability_manager_errors.h"
25686862fbSopenharmony_ci#include "bool_wrapper.h"
26686862fbSopenharmony_ci#include "datetime_ex.h"
27686862fbSopenharmony_ci#include "element_name.h"
28686862fbSopenharmony_ci#include "file_ex.h"
29686862fbSopenharmony_ci#include "ipc_skeleton.h"
30686862fbSopenharmony_ci#include "iservice_registry.h"
31686862fbSopenharmony_ci#include "os_account_manager.h"
32686862fbSopenharmony_ci#include "parameters.h"
33686862fbSopenharmony_ci#include "string_ex.h"
34686862fbSopenharmony_ci#include "system_ability_definition.h"
35686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTEDCOMPONENT_TO_MEMMGR
36686862fbSopenharmony_ci#include "mem_mgr_client.h"
37686862fbSopenharmony_ci#endif
38686862fbSopenharmony_ci#ifdef EFFICIENCY_MANAGER_ENABLE
39686862fbSopenharmony_ci#include "res_type.h"
40686862fbSopenharmony_ci#include "res_sched_client.h"
41686862fbSopenharmony_ci#endif
42686862fbSopenharmony_ci
43686862fbSopenharmony_ci#include "ability_connection_wrapper_stub.h"
44686862fbSopenharmony_ci#include "adapter/dnetwork_adapter.h"
45686862fbSopenharmony_ci#include "bundle/bundle_manager_internal.h"
46686862fbSopenharmony_ci#include "connect_death_recipient.h"
47686862fbSopenharmony_ci#include "datashare_manager.h"
48686862fbSopenharmony_ci#include "dfx/distributed_radar.h"
49686862fbSopenharmony_ci#include "dfx/distributed_sched_dumper.h"
50686862fbSopenharmony_ci#include "dfx/distributed_ue.h"
51686862fbSopenharmony_ci#include "dfx/dms_continue_time_dumper.h"
52686862fbSopenharmony_ci#include "distributed_sched_adapter.h"
53686862fbSopenharmony_ci#include "distributed_sched_permission.h"
54686862fbSopenharmony_ci#include "distributed_sched_utils.h"
55686862fbSopenharmony_ci#include "dms_callback_task.h"
56686862fbSopenharmony_ci#include "dms_constant.h"
57686862fbSopenharmony_ci#include "dms_free_install_callback.h"
58686862fbSopenharmony_ci#include "dms_token_callback.h"
59686862fbSopenharmony_ci#include "dms_version_manager.h"
60686862fbSopenharmony_ci#include "dsched_continue_manager.h"
61686862fbSopenharmony_ci#include "dtbschedmgr_device_info_storage.h"
62686862fbSopenharmony_ci#include "dtbschedmgr_log.h"
63686862fbSopenharmony_ci#include "parcel_helper.h"
64686862fbSopenharmony_ci#include "switch_status_dependency.h"
65686862fbSopenharmony_ci#ifdef SUPPORT_COMMON_EVENT_SERVICE
66686862fbSopenharmony_ci#include "common_event_listener.h"
67686862fbSopenharmony_ci#endif
68686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
69686862fbSopenharmony_ci#include "form_mgr_death_recipient.h"
70686862fbSopenharmony_ci#endif
71686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
72686862fbSopenharmony_ci#include "mission/distributed_bm_storage.h"
73686862fbSopenharmony_ci#include "mission/distributed_mission_info.h"
74686862fbSopenharmony_ci#include "mission/dms_continue_send_manager.h"
75686862fbSopenharmony_ci#include "mission/dms_continue_recv_manager.h"
76686862fbSopenharmony_ci#include "mission/distributed_sched_mission_manager.h"
77686862fbSopenharmony_ci#include "mission/dsched_sync_e2e.h"
78686862fbSopenharmony_ci#include "mission/wifi_state_listener.h"
79686862fbSopenharmony_ci#endif
80686862fbSopenharmony_ci
81686862fbSopenharmony_cinamespace OHOS {
82686862fbSopenharmony_cinamespace DistributedSchedule {
83686862fbSopenharmony_ciusing namespace AAFwk;
84686862fbSopenharmony_ciusing namespace AccountSA;
85686862fbSopenharmony_ciusing namespace AppExecFwk;
86686862fbSopenharmony_ciusing namespace Constants;
87686862fbSopenharmony_ciusing namespace DistributedHardware;
88686862fbSopenharmony_ci
89686862fbSopenharmony_cinamespace {
90686862fbSopenharmony_ciconst std::string TAG = "DistributedSchedService";
91686862fbSopenharmony_ciconst std::string DMS_SRC_NETWORK_ID = "dmsSrcNetworkId";
92686862fbSopenharmony_ciconst int DEFAULT_REQUEST_CODE = -1;
93686862fbSopenharmony_ciconst std::u16string CONNECTION_CALLBACK_INTERFACE_TOKEN = u"ohos.abilityshell.DistributedConnection";
94686862fbSopenharmony_ciconst std::u16string COMPONENT_CHANGE_INTERFACE_TOKEN = u"ohos.rms.DistributedComponent";
95686862fbSopenharmony_ciconst std::u16string ABILITY_MANAGER_SERVICE_TOKEN = u"ohos.aafwk.AbilityManager";
96686862fbSopenharmony_ciconst std::u16string ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN = u"ohos.IAtomicServiceStatusCallback";
97686862fbSopenharmony_ciconst std::string BUNDLE_NAME_KEY = "bundleName";
98686862fbSopenharmony_ciconst std::string VERSION_CODE_KEY = "version";
99686862fbSopenharmony_ciconst std::string PID_KEY = "pid";
100686862fbSopenharmony_ciconst std::string UID_KEY = "uid";
101686862fbSopenharmony_ciconst std::string COMPONENT_TYPE_KEY = "componentType";
102686862fbSopenharmony_ciconst std::string DEVICE_TYPE_KEY = "deviceType";
103686862fbSopenharmony_ciconst std::string CHANGE_TYPE_KEY = "changeType";
104686862fbSopenharmony_ciconst std::string DMS_HIPLAY_ACTION = "ohos.ability.action.deviceSelect";
105686862fbSopenharmony_ciconst std::string DMS_VERSION_ID = "dmsVersion";
106686862fbSopenharmony_ciconst std::string DMS_UID_SPEC_BUNDLE_NAME = "dmsCallerUidBundleName";
107686862fbSopenharmony_ciconst std::string DMS_CONNECT_TOKEN = "connectToken";
108686862fbSopenharmony_ciconst std::string DMS_MISSION_ID = "dmsMissionId";
109686862fbSopenharmony_ciconst std::string SUPPORT_CONTINUE_PAGE_STACK_KEY = "ohos.extra.param.key.supportContinuePageStack";
110686862fbSopenharmony_ciconst std::string SUPPORT_CONTINUE_SOURCE_EXIT_KEY = "ohos.extra.param.key.supportContinueSourceExit";
111686862fbSopenharmony_ciconst std::string SUPPORT_CONTINUE_MODULE_NAME_UPDATE_KEY = "ohos.extra.param.key.supportContinueModuleNameUpdate";
112686862fbSopenharmony_ciconst std::string DSCHED_EVENT_KEY = "IDSchedEventListener";
113686862fbSopenharmony_ciconst std::string DMSDURATION_SAVETIME = "ohos.dschedule.SaveDataTime";
114686862fbSopenharmony_ciconst std::string DMS_CONTINUE_SESSION_ID = "ohos.dms.continueSessionId";
115686862fbSopenharmony_ciconst std::string DMS_PERSISTENT_ID = "ohos.dms.persistentId";
116686862fbSopenharmony_ciconst std::string PKG_NAME = "DBinderBus_Dms_" + std::to_string(getprocpid());
117686862fbSopenharmony_ciconst std::string BOOT_COMPLETED_EVENT = "usual.event.BOOT_COMPLETED";
118686862fbSopenharmony_ciconst std::string COMMON_EVENT_WIFI_SEMI_STATE = "usual.event.wifi.SEMI_STATE";
119686862fbSopenharmony_ciconstexpr int32_t DEFAULT_DMS_MISSION_ID = -1;
120686862fbSopenharmony_ciconstexpr int32_t DEFAULT_DMS_CONNECT_TOKEN = -1;
121686862fbSopenharmony_ciconstexpr int32_t BIND_CONNECT_RETRY_TIMES = 3;
122686862fbSopenharmony_ciconstexpr int32_t BIND_CONNECT_TIMEOUT = 500; // 500ms
123686862fbSopenharmony_ciconstexpr int32_t MAX_DISTRIBUTED_CONNECT_NUM = 600;
124686862fbSopenharmony_ciconstexpr int32_t INVALID_CALLER_UID = -1;
125686862fbSopenharmony_ciconstexpr int32_t IASS_CALLBACK_ON_REMOTE_FREE_INSTALL_DONE = 1;
126686862fbSopenharmony_ciconstexpr int32_t DISTRIBUTED_COMPONENT_ADD = 1;
127686862fbSopenharmony_ciconstexpr int32_t DISTRIBUTED_COMPONENT_REMOVE = 2;
128686862fbSopenharmony_ciconstexpr int32_t START_PERMISSION = 0;
129686862fbSopenharmony_ciconstexpr int32_t CALL_PERMISSION = 1;
130686862fbSopenharmony_ciconstexpr int32_t SEND_RESULT_PERMISSION = 2;
131686862fbSopenharmony_ciconstexpr int64_t CONTINUATION_TIMEOUT = 20000; // 20s
132686862fbSopenharmony_ci// BundleDistributedManager set timeout to 3s, so we set 1s longer
133686862fbSopenharmony_ciconstexpr int64_t CHECK_REMOTE_INSTALL_ABILITY = 40000;
134686862fbSopenharmony_ciconstexpr int32_t MAX_TOKEN_NUM = 100000000;
135686862fbSopenharmony_ciconstexpr uint32_t MAX_MODULENAME_LEN = 2048;
136686862fbSopenharmony_ciconstexpr int32_t DMSDURATION_BEGINTIME = 0;
137686862fbSopenharmony_ciconstexpr int32_t DMSDURATION_ENDTIME = 1;
138686862fbSopenharmony_ciconstexpr int32_t DMSDURATION_TOTALTIME = 2;
139686862fbSopenharmony_ciconstexpr int32_t DMSDURATION_DSTTOSRCRPCTIME = 3;
140686862fbSopenharmony_ciconstexpr int32_t DMSDURATION_SRCTODSTRPCTIME = 5;
141686862fbSopenharmony_ciconstexpr int32_t DMSDURATION_STARTABILITY = 6;
142686862fbSopenharmony_ciconstexpr int32_t HID_HAP = 10000; /* first hap user */
143686862fbSopenharmony_ciconstexpr int32_t WINDOW_MANAGER_SERVICE_ID = 4606;
144686862fbSopenharmony_ciconstexpr int32_t SEMI_WIFI_ID = 1010;
145686862fbSopenharmony_ciDataShareManager &dataShareManager = DataShareManager::GetInstance();
146686862fbSopenharmony_ci}
147686862fbSopenharmony_ci
148686862fbSopenharmony_ciIMPLEMENT_SINGLE_INSTANCE(DistributedSchedService);
149686862fbSopenharmony_ciconst bool REGISTER_RESULT = SystemAbility::MakeAndRegisterAbility(&DistributedSchedService::GetInstance());
150686862fbSopenharmony_ci
151686862fbSopenharmony_ciDistributedSchedService::DistributedSchedService() : SystemAbility(DISTRIBUTED_SCHED_SA_ID, true)
152686862fbSopenharmony_ci{
153686862fbSopenharmony_ci}
154686862fbSopenharmony_ci
155686862fbSopenharmony_civoid DistributedSchedService::OnStart(const SystemAbilityOnDemandReason &startReason)
156686862fbSopenharmony_ci{
157686862fbSopenharmony_ci    HILOGI("OnStart reason %{public}s, reasonId_:%{public}d", startReason.GetName().c_str(), startReason.GetId());
158686862fbSopenharmony_ci    if (!DoStart()) {
159686862fbSopenharmony_ci        HILOGI("OnStart dms service failed.");
160686862fbSopenharmony_ci        return;
161686862fbSopenharmony_ci    }
162686862fbSopenharmony_ci    Publish(this);
163686862fbSopenharmony_ci    HandleBootStart(startReason);
164686862fbSopenharmony_ci}
165686862fbSopenharmony_ci
166686862fbSopenharmony_civoid DistributedSchedService::OnStop(const SystemAbilityOnDemandReason &stopReason)
167686862fbSopenharmony_ci{
168686862fbSopenharmony_ci    HILOGI("OnStart reason %{public}s, reasonId_:%{public}d", stopReason.GetName().c_str(), stopReason.GetId());
169686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
170686862fbSopenharmony_ci    DMSContinueSendMgr::GetInstance().UnInit();
171686862fbSopenharmony_ci    DMSContinueRecvMgr::GetInstance().UnInit();
172686862fbSopenharmony_ci    RemoveSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID);
173686862fbSopenharmony_ci    DistributedSchedAdapter::GetInstance().UnRegisterMissionListener(missionFocusedListener_);
174686862fbSopenharmony_ci#endif
175686862fbSopenharmony_ci
176686862fbSopenharmony_ci#ifdef DMSFWK_INTERACTIVE_ADAPTER
177686862fbSopenharmony_ci    dlclose(dllHandle_);
178686862fbSopenharmony_ci    dllHandle_ = nullptr;
179686862fbSopenharmony_ci#endif
180686862fbSopenharmony_ci    dataShareManager.UnregisterObserver(SwitchStatusDependency::GetInstance().CONTINUE_SWITCH_STATUS_KEY);
181686862fbSopenharmony_ci    HILOGI("OnStop dms service end");
182686862fbSopenharmony_ci}
183686862fbSopenharmony_ci
184686862fbSopenharmony_civoid DistributedSchedService::OnActive(const SystemAbilityOnDemandReason &activeReason)
185686862fbSopenharmony_ci{
186686862fbSopenharmony_ci    HILOGI("OnStart reason %{public}s, reasonId_:%{public}d", activeReason.GetName().c_str(), activeReason.GetId());
187686862fbSopenharmony_ci    DoStart();
188686862fbSopenharmony_ci}
189686862fbSopenharmony_ci
190686862fbSopenharmony_civoid DistributedSchedService::HandleBootStart(const SystemAbilityOnDemandReason &startReason)
191686862fbSopenharmony_ci{
192686862fbSopenharmony_ci    std::vector<DistributedHardware::DmDeviceInfo> dmDeviceInfoList;
193686862fbSopenharmony_ci    int32_t errCode = DeviceManager::GetInstance().GetTrustedDeviceList(PKG_NAME, "", dmDeviceInfoList);
194686862fbSopenharmony_ci    if (errCode != ERR_OK) {
195686862fbSopenharmony_ci        HILOGE("Get device manager trusted device list fail, errCode %{public}d", errCode);
196686862fbSopenharmony_ci    }
197686862fbSopenharmony_ci    if (startReason.GetName() == BOOT_COMPLETED_EVENT && dmDeviceInfoList.empty()) {
198686862fbSopenharmony_ci        HILOGI("UnloadSystemAbility dms");
199686862fbSopenharmony_ci        auto samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
200686862fbSopenharmony_ci        if (samgrProxy == nullptr) {
201686862fbSopenharmony_ci            HILOGE("get samgr failed");
202686862fbSopenharmony_ci            return;
203686862fbSopenharmony_ci        }
204686862fbSopenharmony_ci        int32_t ret = samgrProxy->UnloadSystemAbility(DISTRIBUTED_SCHED_SA_ID);
205686862fbSopenharmony_ci        if (ret != ERR_OK) {
206686862fbSopenharmony_ci            HILOGE("remove system ability failed");
207686862fbSopenharmony_ci            return;
208686862fbSopenharmony_ci        }
209686862fbSopenharmony_ci        HILOGI("UnloadSystemAbility dms ok");
210686862fbSopenharmony_ci    }
211686862fbSopenharmony_ci}
212686862fbSopenharmony_ci
213686862fbSopenharmony_cibool DistributedSchedService::DoStart()
214686862fbSopenharmony_ci{
215686862fbSopenharmony_ci#ifdef DMS_SERVICE_DISABLE
216686862fbSopenharmony_ci    HILOGI("DMS service disabled, exiting.");
217686862fbSopenharmony_ci    _exit(0);
218686862fbSopenharmony_ci#endif
219686862fbSopenharmony_ci    HILOGI("Dms service DoStart enter.");
220686862fbSopenharmony_ci    if (!Init()) {
221686862fbSopenharmony_ci        HILOGE("failed to init DistributedSchedService");
222686862fbSopenharmony_ci        return false;
223686862fbSopenharmony_ci    }
224686862fbSopenharmony_ci    FuncContinuationCallback continuationCallback = [this] (int32_t missionId) {
225686862fbSopenharmony_ci        HILOGW("continuationCallback timeout.");
226686862fbSopenharmony_ci        NotifyContinuationCallbackResult(missionId, CONTINUE_ABILITY_TIMEOUT_ERR);
227686862fbSopenharmony_ci    };
228686862fbSopenharmony_ci
229686862fbSopenharmony_ci    DmsCallbackTaskInitCallbackFunc freeCallback = [this] (int64_t taskId) {
230686862fbSopenharmony_ci        HILOGW("DmsCallbackTaskInitCallbackFunc timeout, taskId:%{public}" PRId64 ".", taskId);
231686862fbSopenharmony_ci        NotifyCompleteFreeInstallFromRemote(taskId, AAFwk::FREE_INSTALL_TIMEOUT);
232686862fbSopenharmony_ci    };
233686862fbSopenharmony_ci    dschedContinuation_ = std::make_shared<DSchedContinuation>();
234686862fbSopenharmony_ci    collaborateCbMgr_ = std::make_shared<DSchedCollaborationCallbackMgr>();
235686862fbSopenharmony_ci    dmsCallbackTask_ = std::make_shared<DmsCallbackTask>();
236686862fbSopenharmony_ci    dschedContinuation_->Init(continuationCallback);
237686862fbSopenharmony_ci    collaborateCbMgr_->Init();
238686862fbSopenharmony_ci    dmsCallbackTask_->Init(freeCallback);
239686862fbSopenharmony_ci
240686862fbSopenharmony_ci#ifdef DMSFWK_INTERACTIVE_ADAPTER
241686862fbSopenharmony_ci    HILOGI("Get dms interactive adapter proxy enter.");
242686862fbSopenharmony_ci    int32_t ret = GetDmsInteractiveAdapterProxy();
243686862fbSopenharmony_ci    if (ret != ERR_OK) {
244686862fbSopenharmony_ci        HILOGE("Get remote dms interactive adapter proxy fail, ret %{public}d.", ret);
245686862fbSopenharmony_ci    }
246686862fbSopenharmony_ci#endif
247686862fbSopenharmony_ci    HILOGI("OnStart dms service success.");
248686862fbSopenharmony_ci    return true;
249686862fbSopenharmony_ci}
250686862fbSopenharmony_ci
251686862fbSopenharmony_ciint32_t DistributedSchedService::Dump(int32_t fd, const std::vector<std::u16string>& args)
252686862fbSopenharmony_ci{
253686862fbSopenharmony_ci    std::vector<std::string> argsInStr8;
254686862fbSopenharmony_ci    for (const auto& arg : args) {
255686862fbSopenharmony_ci        argsInStr8.emplace_back(Str16ToStr8(arg));
256686862fbSopenharmony_ci    }
257686862fbSopenharmony_ci    std::string result;
258686862fbSopenharmony_ci    DistributedSchedDumper::Dump(argsInStr8, result);
259686862fbSopenharmony_ci
260686862fbSopenharmony_ci    if (!SaveStringToFd(fd, result)) {
261686862fbSopenharmony_ci        HILOGE("save to fd failed");
262686862fbSopenharmony_ci        return DMS_WRITE_FILE_FAILED_ERR;
263686862fbSopenharmony_ci    }
264686862fbSopenharmony_ci    return ERR_OK;
265686862fbSopenharmony_ci}
266686862fbSopenharmony_ci
267686862fbSopenharmony_civoid DistributedSchedService::DeviceOnlineNotify(const std::string& networkId)
268686862fbSopenharmony_ci{
269686862fbSopenharmony_ci    DistributedSchedAdapter::GetInstance().DeviceOnline(networkId);
270686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
271686862fbSopenharmony_ci    DistributedSchedMissionManager::GetInstance().DeviceOnlineNotify(networkId);
272686862fbSopenharmony_ci    if (!DMSContinueRecvMgr::GetInstance().CheckRegSoftbusListener() &&
273686862fbSopenharmony_ci        DistributedHardware::DeviceManager::GetInstance().IsSameAccount(networkId)) {
274686862fbSopenharmony_ci        HILOGI("DMSContinueRecvMgr need init");
275686862fbSopenharmony_ci        DMSContinueRecvMgr::GetInstance().Init();
276686862fbSopenharmony_ci    }
277686862fbSopenharmony_ci#endif
278686862fbSopenharmony_ci}
279686862fbSopenharmony_ci
280686862fbSopenharmony_civoid DistributedSchedService::DeviceOfflineNotify(const std::string& networkId)
281686862fbSopenharmony_ci{
282686862fbSopenharmony_ci    DistributedSchedAdapter::GetInstance().DeviceOffline(networkId);
283686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
284686862fbSopenharmony_ci    DMSContinueRecvMgr::GetInstance().NotifyDeviceOffline(networkId);
285686862fbSopenharmony_ci    DistributedSchedMissionManager::GetInstance().DeviceOfflineNotify(networkId);
286686862fbSopenharmony_ci#endif
287686862fbSopenharmony_ci}
288686862fbSopenharmony_ci
289686862fbSopenharmony_cibool DistributedSchedService::Init()
290686862fbSopenharmony_ci{
291686862fbSopenharmony_ci    HILOGD("ready to init.");
292686862fbSopenharmony_ci    InitDeviceCfg();
293686862fbSopenharmony_ci    DmsContinueTime::GetInstance().Init();
294686862fbSopenharmony_ci    DnetworkAdapter::GetInstance()->Init();
295686862fbSopenharmony_ci    if (!DtbschedmgrDeviceInfoStorage::GetInstance().Init()) {
296686862fbSopenharmony_ci        HILOGW("DtbschedmgrDeviceInfoStorage init failed.");
297686862fbSopenharmony_ci    }
298686862fbSopenharmony_ci    InitDataShareManager();
299686862fbSopenharmony_ci    InitMissionManager();
300686862fbSopenharmony_ci
301686862fbSopenharmony_ci    DistributedSchedAdapter::GetInstance().Init();
302686862fbSopenharmony_ci    if (SwitchStatusDependency::GetInstance().IsContinueSwitchOn()) {
303686862fbSopenharmony_ci        DSchedContinueManager::GetInstance().Init();
304686862fbSopenharmony_ci    }
305686862fbSopenharmony_ci    connectDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new ConnectDeathRecipient());
306686862fbSopenharmony_ci    callerDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new CallerDeathRecipient());
307686862fbSopenharmony_ci    callerDeathRecipientForLocalDevice_ = sptr<IRemoteObject::DeathRecipient>(
308686862fbSopenharmony_ci        new CallerDeathRecipient(IDistributedSched::CALLER));
309686862fbSopenharmony_ci    if (componentChangeHandler_ == nullptr) {
310686862fbSopenharmony_ci        auto runner = AppExecFwk::EventRunner::Create("DmsComponentChange");
311686862fbSopenharmony_ci        componentChangeHandler_ = std::make_shared<AppExecFwk::EventHandler>(runner);
312686862fbSopenharmony_ci    }
313686862fbSopenharmony_ci    return true;
314686862fbSopenharmony_ci}
315686862fbSopenharmony_ci
316686862fbSopenharmony_civoid DistributedSchedService::InitMissionManager()
317686862fbSopenharmony_ci{
318686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
319686862fbSopenharmony_ci    if (!AddSystemAbilityListener(WINDOW_MANAGER_SERVICE_ID)) {
320686862fbSopenharmony_ci        HILOGE("Add System Ability Listener failed!");
321686862fbSopenharmony_ci    }
322686862fbSopenharmony_ci    DistributedSchedMissionManager::GetInstance().Init();
323686862fbSopenharmony_ci    DistributedSchedMissionManager::GetInstance().InitDataStorage();
324686862fbSopenharmony_ci    InitCommonEventListener();
325686862fbSopenharmony_ci    InitWifiStateListener();
326686862fbSopenharmony_ci    InitWifiSemiStateListener();
327686862fbSopenharmony_ci    DMSContinueSendMgr::GetInstance().Init();
328686862fbSopenharmony_ci    DMSContinueRecvMgr::GetInstance().Init();
329686862fbSopenharmony_ci#endif
330686862fbSopenharmony_ci}
331686862fbSopenharmony_ci
332686862fbSopenharmony_civoid DistributedSchedService::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
333686862fbSopenharmony_ci{
334686862fbSopenharmony_ci    HILOGI("OnAddSystemAbility systemAbilityId:%{public}d added!", systemAbilityId);
335686862fbSopenharmony_ci    missionFocusedListener_ = sptr<DistributedMissionFocusedListener>(new DistributedMissionFocusedListener());
336686862fbSopenharmony_ci    int32_t ret = DistributedSchedAdapter::GetInstance().RegisterMissionListener(missionFocusedListener_);
337686862fbSopenharmony_ci    if (ret != ERR_OK) {
338686862fbSopenharmony_ci        HILOGE("get RegisterMissionListener failed, ret: %{public}d", ret);
339686862fbSopenharmony_ci    }
340686862fbSopenharmony_ci}
341686862fbSopenharmony_ci
342686862fbSopenharmony_civoid DistributedSchedService::InitDataShareManager()
343686862fbSopenharmony_ci{
344686862fbSopenharmony_ci    DataShareManager::ObserverCallback observerCallback = [this]() {
345686862fbSopenharmony_ci        dataShareManager.SetCurrentContinueSwitch(SwitchStatusDependency::GetInstance().IsContinueSwitchOn());
346686862fbSopenharmony_ci        HILOGD("dsMgr IsCurrentContinueSwitchOn : %{public}d", dataShareManager.IsCurrentContinueSwitchOn());
347686862fbSopenharmony_ci        int32_t missionId = GetCurrentMissionId();
348686862fbSopenharmony_ci        if (missionId <= 0) {
349686862fbSopenharmony_ci            HILOGW("GetCurrentMissionId failed, init end. ret: %{public}d", missionId);
350686862fbSopenharmony_ci            return;
351686862fbSopenharmony_ci        }
352686862fbSopenharmony_ci        DmsUE::GetInstance().ChangedSwitchState(dataShareManager.IsCurrentContinueSwitchOn(), ERR_OK);
353686862fbSopenharmony_ci        if (dataShareManager.IsCurrentContinueSwitchOn()) {
354686862fbSopenharmony_ci            DMSContinueSendMgr::GetInstance().NotifyMissionFocused(missionId, FocusedReason::INIT);
355686862fbSopenharmony_ci            DSchedContinueManager::GetInstance().Init();
356686862fbSopenharmony_ci        } else {
357686862fbSopenharmony_ci            DMSContinueSendMgr::GetInstance().NotifyMissionUnfocused(missionId, UnfocusedReason::NORMAL);
358686862fbSopenharmony_ci            DMSContinueRecvMgr::GetInstance().OnContinueSwitchOff();
359686862fbSopenharmony_ci            DSchedContinueManager::GetInstance().UnInit();
360686862fbSopenharmony_ci        };
361686862fbSopenharmony_ci    };
362686862fbSopenharmony_ci    dataShareManager.SetCurrentContinueSwitch(SwitchStatusDependency::GetInstance().IsContinueSwitchOn());
363686862fbSopenharmony_ci    HILOGD("dsMgr IsCurrentContinueSwitchOn : %{public}d", dataShareManager.IsCurrentContinueSwitchOn());
364686862fbSopenharmony_ci    dataShareManager.RegisterObserver(SwitchStatusDependency::GetInstance().CONTINUE_SWITCH_STATUS_KEY,
365686862fbSopenharmony_ci        observerCallback);
366686862fbSopenharmony_ci    DmsUE::GetInstance().OriginalSwitchState(SwitchStatusDependency::GetInstance().IsContinueSwitchOn(), ERR_OK);
367686862fbSopenharmony_ci    HILOGI("Init data share manager, register observer end.");
368686862fbSopenharmony_ci}
369686862fbSopenharmony_ci
370686862fbSopenharmony_civoid DistributedSchedService::InitCommonEventListener()
371686862fbSopenharmony_ci{
372686862fbSopenharmony_ci    HILOGI("InitCommonEventListener called");
373686862fbSopenharmony_ci#ifdef SUPPORT_COMMON_EVENT_SERVICE
374686862fbSopenharmony_ci    DmsBmStorage::GetInstance();
375686862fbSopenharmony_ci    EventFwk::MatchingSkills matchingSkills;
376686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_LOCKED);
377686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_UNLOCKED);
378686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_OFF);
379686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_SCREEN_ON);
380686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_USER_SWITCHED);
381686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_ADDED);
382686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED);
383686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_CHANGED);
384686862fbSopenharmony_ci    EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
385686862fbSopenharmony_ci    auto applyMonitor = std::make_shared<CommonEventListener>(subscribeInfo);
386686862fbSopenharmony_ci    EventFwk::CommonEventManager::SubscribeCommonEvent(applyMonitor);
387686862fbSopenharmony_ci    DmsBmStorage::GetInstance()->UpdateDistributedData();
388686862fbSopenharmony_ci    if (DmsKvSyncE2E::GetInstance()->CheckDeviceCfg()) {
389686862fbSopenharmony_ci        DmsKvSyncE2E::GetInstance()->PushAndPullData();
390686862fbSopenharmony_ci    }
391686862fbSopenharmony_ci#endif
392686862fbSopenharmony_ci}
393686862fbSopenharmony_ci
394686862fbSopenharmony_civoid DistributedSchedService::InitWifiStateListener()
395686862fbSopenharmony_ci{
396686862fbSopenharmony_ci    HILOGI("InitWifiStateListener called");
397686862fbSopenharmony_ci    EventFwk::MatchingSkills matchingSkills;
398686862fbSopenharmony_ci    matchingSkills.AddEvent(EventFwk::CommonEventSupport::COMMON_EVENT_WIFI_POWER_STATE);
399686862fbSopenharmony_ci    EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
400686862fbSopenharmony_ci    auto wifiStateListener = std::make_shared<WifiStateListener>(subscribeInfo);
401686862fbSopenharmony_ci    wifiStateListener->InitWifiState();
402686862fbSopenharmony_ci    if (!EventFwk::CommonEventManager::SubscribeCommonEvent(wifiStateListener)) {
403686862fbSopenharmony_ci        HILOGE("SubscribeCommonEvent wifiStateListener failed!");
404686862fbSopenharmony_ci    }
405686862fbSopenharmony_ci}
406686862fbSopenharmony_ci
407686862fbSopenharmony_civoid DistributedSchedService::InitWifiSemiStateListener()
408686862fbSopenharmony_ci{
409686862fbSopenharmony_ci    HILOGI("InitWifiSemiStateListener called");
410686862fbSopenharmony_ci    EventFwk::MatchingSkills matchingSkills;
411686862fbSopenharmony_ci    matchingSkills.AddEvent(COMMON_EVENT_WIFI_SEMI_STATE);
412686862fbSopenharmony_ci    EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills);
413686862fbSopenharmony_ci    subscribeInfo.SetPublisherUid(SEMI_WIFI_ID);
414686862fbSopenharmony_ci    auto wifiStateListener = std::make_shared<WifiStateListener>(subscribeInfo);
415686862fbSopenharmony_ci    if (!EventFwk::CommonEventManager::SubscribeCommonEvent(wifiStateListener)) {
416686862fbSopenharmony_ci        HILOGE("SubscribeCommonEvent wifiSemiStateListener failed!");
417686862fbSopenharmony_ci    }
418686862fbSopenharmony_ci}
419686862fbSopenharmony_ci
420686862fbSopenharmony_civoid DistributedSchedService::InitDeviceCfg()
421686862fbSopenharmony_ci{
422686862fbSopenharmony_ci    HILOGI("called");
423686862fbSopenharmony_ci    DmsKvSyncE2E::GetInstance()->SetDeviceCfg();
424686862fbSopenharmony_ci}
425686862fbSopenharmony_ci
426686862fbSopenharmony_civoid DistributedSchedService::DurationStart(const std::string srcDeviceId, const std::string dstDeviceId)
427686862fbSopenharmony_ci{
428686862fbSopenharmony_ci    DmsContinueTime::GetInstance().Init();
429686862fbSopenharmony_ci    std::string strBeginTime = DmsContinueTime::GetInstance().GetCurrentTime();
430686862fbSopenharmony_ci    DmsContinueTime::GetInstance().SetDurationStrTime(DMSDURATION_BEGINTIME, strBeginTime);
431686862fbSopenharmony_ci    DmsContinueTime::GetInstance().SetNetWorkId(srcDeviceId, dstDeviceId);
432686862fbSopenharmony_ci}
433686862fbSopenharmony_ci
434686862fbSopenharmony_ci#ifdef DMSFWK_INTERACTIVE_ADAPTER
435686862fbSopenharmony_cibool DistributedSchedService::CheckRemoteOsType(const std::string& netwokId)
436686862fbSopenharmony_ci{
437686862fbSopenharmony_ci    auto devInfo = DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(netwokId);
438686862fbSopenharmony_ci    if (devInfo == nullptr) {
439686862fbSopenharmony_ci        HILOGE("GetDeviceInfoById failed, netwokId: %{public}s.", GetAnonymStr(netwokId).c_str());
440686862fbSopenharmony_ci        return false;
441686862fbSopenharmony_ci    }
442686862fbSopenharmony_ci
443686862fbSopenharmony_ci    HILOGI("Remote device OsType %{public}d, netwokId: %{public}s.", devInfo->GetDeviceOSType(),
444686862fbSopenharmony_ci        GetAnonymStr(netwokId).c_str());
445686862fbSopenharmony_ci    return (devInfo->GetDeviceOSType() == Constants::HO_OS_TYPE_EX);
446686862fbSopenharmony_ci}
447686862fbSopenharmony_ci
448686862fbSopenharmony_ciint32_t DistributedSchedService::GetDmsInteractiveAdapterProxy()
449686862fbSopenharmony_ci{
450686862fbSopenharmony_ci    HILOGI("Get remote dms interactive adapter proxy.");
451686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
452686862fbSopenharmony_ci#if (defined(__aarch64__) || defined(__x86_64__))
453686862fbSopenharmony_ci    char resolvedPath[100] = "/system/lib64/libdms_interactive_adapter.z.so";
454686862fbSopenharmony_ci#else
455686862fbSopenharmony_ci    char resolvedPath[100] = "/system/lib/libdms_interactive_adapter.z.so";
456686862fbSopenharmony_ci#endif
457686862fbSopenharmony_ci    int32_t (*GetDmsInteractiveAdapter)(const sptr<IRemoteObject> &callerToken,
458686862fbSopenharmony_ci        IDmsInteractiveAdapter &dmsAdpHandle) = nullptr;
459686862fbSopenharmony_ci
460686862fbSopenharmony_ci    dllHandle_ = dlopen(resolvedPath, RTLD_LAZY);
461686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
462686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object fail, resolvedPath [%{public}s].", resolvedPath);
463686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
464686862fbSopenharmony_ci    }
465686862fbSopenharmony_ci
466686862fbSopenharmony_ci    int32_t ret = ERR_OK;
467686862fbSopenharmony_ci    do {
468686862fbSopenharmony_ci        GetDmsInteractiveAdapter = reinterpret_cast<int32_t (*)(const sptr<IRemoteObject> &callerToken,
469686862fbSopenharmony_ci            IDmsInteractiveAdapter &dmsAdpHandle)>(dlsym(dllHandle_, "GetDmsInteractiveAdapter"));
470686862fbSopenharmony_ci        if (GetDmsInteractiveAdapter == nullptr) {
471686862fbSopenharmony_ci            HILOGE("Link the GetDmsInteractiveAdapter symbol in dms interactive adapter fail.");
472686862fbSopenharmony_ci            ret = NOT_FIND_SERVICE_REGISTRY;
473686862fbSopenharmony_ci            break;
474686862fbSopenharmony_ci        }
475686862fbSopenharmony_ci
476686862fbSopenharmony_ci        int32_t ret = GetDmsInteractiveAdapter(this, dmsAdapetr_);
477686862fbSopenharmony_ci        if (ret != ERR_OK) {
478686862fbSopenharmony_ci            HILOGE("Init remote dms interactive adapter proxy fail, ret %{public}d.", ret);
479686862fbSopenharmony_ci            ret = INVALID_PARAMETERS_ERR;
480686862fbSopenharmony_ci            break;
481686862fbSopenharmony_ci        }
482686862fbSopenharmony_ci        HILOGI("Init remote dms interactive adapter proxy success.");
483686862fbSopenharmony_ci        ret = ERR_OK;
484686862fbSopenharmony_ci    } while (false);
485686862fbSopenharmony_ci
486686862fbSopenharmony_ci    if (ret != ERR_OK) {
487686862fbSopenharmony_ci        HILOGE("Get remote dms interactive adapter proxy fail, dlclose handle.");
488686862fbSopenharmony_ci        dlclose(dllHandle_);
489686862fbSopenharmony_ci        dllHandle_ = nullptr;
490686862fbSopenharmony_ci    }
491686862fbSopenharmony_ci    return ret;
492686862fbSopenharmony_ci}
493686862fbSopenharmony_ci
494686862fbSopenharmony_ciint32_t DistributedSchedService::StartRemoteAbilityAdapter(const OHOS::AAFwk::Want& want,
495686862fbSopenharmony_ci    int32_t callerUid, int32_t requestCode, uint32_t accessToken)
496686862fbSopenharmony_ci{
497686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
498686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
499686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
500686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
501686862fbSopenharmony_ci    }
502686862fbSopenharmony_ci    if (dmsAdapetr_.StartRemoteAbilityAdapter == nullptr) {
503686862fbSopenharmony_ci        HILOGE("Dms interactive start remote ability adapter handle is null.");
504686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
505686862fbSopenharmony_ci    }
506686862fbSopenharmony_ci
507686862fbSopenharmony_ci    int32_t ret = dmsAdapetr_.StartRemoteAbilityAdapter(want, callerUid, requestCode, accessToken);
508686862fbSopenharmony_ci    if (ret != ERR_OK) {
509686862fbSopenharmony_ci        HILOGE("Dms interactive adapter start remote ability adapter fail, ret %{public}d.", ret);
510686862fbSopenharmony_ci    }
511686862fbSopenharmony_ci    return ret;
512686862fbSopenharmony_ci}
513686862fbSopenharmony_ci
514686862fbSopenharmony_ciint32_t DistributedSchedService::ConnectRemoteAbilityAdapter(const OHOS::AAFwk::Want& want,
515686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, int32_t callerUid, int32_t callerPid, uint32_t accessToken)
516686862fbSopenharmony_ci{
517686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
518686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
519686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
520686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
521686862fbSopenharmony_ci    }
522686862fbSopenharmony_ci    if (dmsAdapetr_.ConnectRemoteAbilityAdapter == nullptr) {
523686862fbSopenharmony_ci        HILOGE("Dms interactive connect remote ability adapter handle is null.");
524686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
525686862fbSopenharmony_ci    }
526686862fbSopenharmony_ci
527686862fbSopenharmony_ci    int32_t ret = dmsAdapetr_.ConnectRemoteAbilityAdapter(want, connect, callerUid, callerPid, accessToken);
528686862fbSopenharmony_ci    if (ret != ERR_OK) {
529686862fbSopenharmony_ci        HILOGE("Dms interactive adapter connect remote ability adapter fail, ret %{public}d.", ret);
530686862fbSopenharmony_ci    }
531686862fbSopenharmony_ci    return ret;
532686862fbSopenharmony_ci}
533686862fbSopenharmony_ci
534686862fbSopenharmony_ciint32_t DistributedSchedService::DisconnectRemoteAbilityAdapter(const sptr<IRemoteObject>& connect, int32_t callerUid,
535686862fbSopenharmony_ci    uint32_t accessToken)
536686862fbSopenharmony_ci{
537686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
538686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
539686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
540686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
541686862fbSopenharmony_ci    }
542686862fbSopenharmony_ci    if (dmsAdapetr_.DisconnectRemoteAbilityAdapter == nullptr) {
543686862fbSopenharmony_ci        HILOGE("Dms interactive disconnect remote ability adapter handle is null.");
544686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
545686862fbSopenharmony_ci    }
546686862fbSopenharmony_ci
547686862fbSopenharmony_ci    int32_t ret = dmsAdapetr_.DisconnectRemoteAbilityAdapter(connect, callerUid, accessToken);
548686862fbSopenharmony_ci    if (ret != ERR_OK) {
549686862fbSopenharmony_ci        HILOGE("Dms interactive adapter disconnect remote ability adapter fail, ret %{public}d.", ret);
550686862fbSopenharmony_ci    }
551686862fbSopenharmony_ci    return ret;
552686862fbSopenharmony_ci}
553686862fbSopenharmony_ci
554686862fbSopenharmony_ciint32_t DistributedSchedService::StartAbilityFromRemoteAdapter(MessageParcel& data, MessageParcel& reply)
555686862fbSopenharmony_ci{
556686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
557686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
558686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
559686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
560686862fbSopenharmony_ci    }
561686862fbSopenharmony_ci    if (dmsAdapetr_.StartAbilityFromRemoteAdapter == nullptr) {
562686862fbSopenharmony_ci        HILOGE("Dms interactive start ability from remote adapter handle is null.");
563686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
564686862fbSopenharmony_ci    }
565686862fbSopenharmony_ci
566686862fbSopenharmony_ci    int32_t ret = dmsAdapetr_.StartAbilityFromRemoteAdapter(data, reply);
567686862fbSopenharmony_ci    if (ret != ERR_OK) {
568686862fbSopenharmony_ci        HILOGE("Dms interactive adapter start ability from remote adapter fail, ret %{public}d.", ret);
569686862fbSopenharmony_ci    }
570686862fbSopenharmony_ci    return ret;
571686862fbSopenharmony_ci}
572686862fbSopenharmony_ci
573686862fbSopenharmony_ciint32_t DistributedSchedService::StopAbilityFromRemoteAdapter(MessageParcel& data, MessageParcel& reply)
574686862fbSopenharmony_ci{
575686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
576686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
577686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
578686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
579686862fbSopenharmony_ci    }
580686862fbSopenharmony_ci    if (dmsAdapetr_.StopAbilityFromRemoteAdapter == nullptr) {
581686862fbSopenharmony_ci        HILOGE("Dms interactive stop ability from remote adapter handle is null.");
582686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
583686862fbSopenharmony_ci    }
584686862fbSopenharmony_ci
585686862fbSopenharmony_ci    int32_t ret = dmsAdapetr_.StopAbilityFromRemoteAdapter(data, reply);
586686862fbSopenharmony_ci    if (ret != ERR_OK) {
587686862fbSopenharmony_ci        HILOGE("Dms interactive adapter stop ability from remote adapter fail, ret %{public}d.", ret);
588686862fbSopenharmony_ci    }
589686862fbSopenharmony_ci    return ret;
590686862fbSopenharmony_ci}
591686862fbSopenharmony_ci
592686862fbSopenharmony_ciint32_t DistributedSchedService::ConnectAbilityFromRemoteAdapter(MessageParcel& data, MessageParcel& reply)
593686862fbSopenharmony_ci{
594686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
595686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
596686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
597686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
598686862fbSopenharmony_ci    }
599686862fbSopenharmony_ci    if (dmsAdapetr_.ConnectAbilityFromRemoteAdapter == nullptr) {
600686862fbSopenharmony_ci        HILOGE("Dms interactive connect ability from remote adapter handle is null.");
601686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
602686862fbSopenharmony_ci    }
603686862fbSopenharmony_ci
604686862fbSopenharmony_ci    int32_t ret = dmsAdapetr_.ConnectAbilityFromRemoteAdapter(data, reply);
605686862fbSopenharmony_ci    if (ret != ERR_OK) {
606686862fbSopenharmony_ci        HILOGE("Dms interactive adapter connect ability from remote adapter fail, ret %{public}d.", ret);
607686862fbSopenharmony_ci    }
608686862fbSopenharmony_ci    return ret;
609686862fbSopenharmony_ci}
610686862fbSopenharmony_ci
611686862fbSopenharmony_ciint32_t DistributedSchedService::DisconnectAbilityFromRemoteAdapter(MessageParcel& data, MessageParcel& reply)
612686862fbSopenharmony_ci{
613686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
614686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
615686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
616686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
617686862fbSopenharmony_ci    }
618686862fbSopenharmony_ci    if (dmsAdapetr_.DisconnectAbilityFromRemoteAdapter == nullptr) {
619686862fbSopenharmony_ci        HILOGE("Dms interactive disconnect ability from remote adapter handle is null.");
620686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
621686862fbSopenharmony_ci    }
622686862fbSopenharmony_ci
623686862fbSopenharmony_ci    int32_t ret = dmsAdapetr_.DisconnectAbilityFromRemoteAdapter(data, reply);
624686862fbSopenharmony_ci    if (ret != ERR_OK) {
625686862fbSopenharmony_ci        HILOGE("Dms interactive adapter disconnect ability from remote adapter fail, ret %{public}d.", ret);
626686862fbSopenharmony_ci    }
627686862fbSopenharmony_ci    return ret;
628686862fbSopenharmony_ci}
629686862fbSopenharmony_ci
630686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyAbilityLifecycleChangedFromRemoteAdapter(MessageParcel& data,
631686862fbSopenharmony_ci    MessageParcel& reply)
632686862fbSopenharmony_ci{
633686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
634686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
635686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
636686862fbSopenharmony_ci        return NOT_FIND_SERVICE_REGISTRY;
637686862fbSopenharmony_ci    }
638686862fbSopenharmony_ci    if (dmsAdapetr_.NotifyAbilityLifecycleChangedFromRemoteAdapter == nullptr) {
639686862fbSopenharmony_ci        HILOGE("Dms interactive disconnect ability from remote adapter handle is null.");
640686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
641686862fbSopenharmony_ci    }
642686862fbSopenharmony_ci
643686862fbSopenharmony_ci    int32_t ret = dmsAdapetr_.NotifyAbilityLifecycleChangedFromRemoteAdapter(data, reply);
644686862fbSopenharmony_ci    if (ret != ERR_OK) {
645686862fbSopenharmony_ci        HILOGE("Dms interactive adapter disconnect ability from remote adapter fail, ret %{public}d.", ret);
646686862fbSopenharmony_ci    }
647686862fbSopenharmony_ci    return ret;
648686862fbSopenharmony_ci}
649686862fbSopenharmony_ci
650686862fbSopenharmony_civoid DistributedSchedService::OnDeviceOnlineEx(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo)
651686862fbSopenharmony_ci{
652686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
653686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
654686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
655686862fbSopenharmony_ci        return;
656686862fbSopenharmony_ci    }
657686862fbSopenharmony_ci    if (dmsAdapetr_.OnDeviceOnlineEx == nullptr) {
658686862fbSopenharmony_ci        HILOGE("Dms interactive on device online extention handle is null.");
659686862fbSopenharmony_ci        return;
660686862fbSopenharmony_ci    }
661686862fbSopenharmony_ci
662686862fbSopenharmony_ci    dmsAdapetr_.OnDeviceOnlineEx(deviceInfo);
663686862fbSopenharmony_ci}
664686862fbSopenharmony_ci
665686862fbSopenharmony_civoid DistributedSchedService::OnDeviceOfflineEx(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo)
666686862fbSopenharmony_ci{
667686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
668686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
669686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
670686862fbSopenharmony_ci        return;
671686862fbSopenharmony_ci    }
672686862fbSopenharmony_ci    if (dmsAdapetr_.OnDeviceOfflineEx == nullptr) {
673686862fbSopenharmony_ci        HILOGE("Dms interactive on device online extention handle is null.");
674686862fbSopenharmony_ci        return;
675686862fbSopenharmony_ci    }
676686862fbSopenharmony_ci
677686862fbSopenharmony_ci    dmsAdapetr_.OnDeviceOfflineEx(deviceInfo);
678686862fbSopenharmony_ci}
679686862fbSopenharmony_ci
680686862fbSopenharmony_civoid DistributedSchedService::OnDeviceInfoChangedEx(const OHOS::DistributedHardware::DmDeviceInfo& deviceInfo)
681686862fbSopenharmony_ci{
682686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(dmsAdapetrLock_);
683686862fbSopenharmony_ci    if (dllHandle_ == nullptr) {
684686862fbSopenharmony_ci        HILOGE("Open dms interactive adapter shared object failed");
685686862fbSopenharmony_ci        return;
686686862fbSopenharmony_ci    }
687686862fbSopenharmony_ci    if (dmsAdapetr_.OnDeviceInfoChangedEx == nullptr) {
688686862fbSopenharmony_ci        HILOGE("Dms interactive on device online extention handle is null.");
689686862fbSopenharmony_ci        return;
690686862fbSopenharmony_ci    }
691686862fbSopenharmony_ci
692686862fbSopenharmony_ci    dmsAdapetr_.OnDeviceInfoChangedEx(deviceInfo);
693686862fbSopenharmony_ci}
694686862fbSopenharmony_ci#endif // DMSFWK_INTERACTIVE_ADAPTER
695686862fbSopenharmony_ci
696686862fbSopenharmony_ciint32_t DistributedSchedService::GetCallerInfo(const std::string &localDeviceId, int32_t callerUid,
697686862fbSopenharmony_ci    uint32_t accessToken, CallerInfo &callerInfo)
698686862fbSopenharmony_ci{
699686862fbSopenharmony_ci    callerInfo.sourceDeviceId = localDeviceId;
700686862fbSopenharmony_ci    callerInfo.uid = callerUid;
701686862fbSopenharmony_ci    callerInfo.accessToken = accessToken;
702686862fbSopenharmony_ci    if (!BundleManagerInternal::GetCallerAppIdFromBms(callerInfo.uid, callerInfo.callerAppId)) {
703686862fbSopenharmony_ci        HILOGE("GetCallerAppIdFromBms failed");
704686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
705686862fbSopenharmony_ci    }
706686862fbSopenharmony_ci    if (!BundleManagerInternal::GetBundleNameListFromBms(callerInfo.uid, callerInfo.bundleNames)) {
707686862fbSopenharmony_ci        HILOGE("GetBundleNameListFromBms failed");
708686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
709686862fbSopenharmony_ci    }
710686862fbSopenharmony_ci    callerInfo.extraInfoJson[DMS_VERSION_ID] = DMS_VERSION;
711686862fbSopenharmony_ci    return ERR_OK;
712686862fbSopenharmony_ci}
713686862fbSopenharmony_ci
714686862fbSopenharmony_ciint32_t DistributedSchedService::StartRemoteAbility(const OHOS::AAFwk::Want& want,
715686862fbSopenharmony_ci    int32_t callerUid, int32_t requestCode, uint32_t accessToken)
716686862fbSopenharmony_ci{
717686862fbSopenharmony_ci    std::string localDeviceId;
718686862fbSopenharmony_ci    std::string deviceId = want.GetElement().GetDeviceID();
719686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || !CheckDeviceId(localDeviceId, deviceId)) {
720686862fbSopenharmony_ci        HILOGE("check deviceId failed");
721686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
722686862fbSopenharmony_ci    }
723686862fbSopenharmony_ci#ifdef DMSFWK_INTERACTIVE_ADAPTER
724686862fbSopenharmony_ci    if (CheckRemoteOsType(deviceId)) {
725686862fbSopenharmony_ci        return StartRemoteAbilityAdapter(want, callerUid, requestCode, accessToken);
726686862fbSopenharmony_ci    }
727686862fbSopenharmony_ci#endif // DMSFWK_INTERACTIVE_ADAPTER
728686862fbSopenharmony_ci
729686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
730686862fbSopenharmony_ci    if (remoteDms == nullptr) {
731686862fbSopenharmony_ci        HILOGE("get remoteDms failed");
732686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
733686862fbSopenharmony_ci    }
734686862fbSopenharmony_ci
735686862fbSopenharmony_ci    CallerInfo callerInfo;
736686862fbSopenharmony_ci    int32_t ret = GetCallerInfo(localDeviceId, callerUid, accessToken, callerInfo);
737686862fbSopenharmony_ci    if (ret != ERR_OK) {
738686862fbSopenharmony_ci        HILOGE("Get local device caller info fail, ret: %{public}d.", ret);
739686862fbSopenharmony_ci        return ret;
740686862fbSopenharmony_ci    }
741686862fbSopenharmony_ci    AccountInfo accountInfo;
742686862fbSopenharmony_ci    ret = DistributedSchedPermission::GetInstance().GetAccountInfo(deviceId, callerInfo, accountInfo);
743686862fbSopenharmony_ci    if (ret != ERR_OK) {
744686862fbSopenharmony_ci        HILOGE("GetAccountInfo fail, ret: %{public}d.", ret);
745686862fbSopenharmony_ci        return ret;
746686862fbSopenharmony_ci    }
747686862fbSopenharmony_ci    AAFwk::Want* newWant = const_cast<Want*>(&want);
748686862fbSopenharmony_ci    newWant->SetParam(DMS_SRC_NETWORK_ID, localDeviceId);
749686862fbSopenharmony_ci    AppExecFwk::AbilityInfo abilityInfo;
750686862fbSopenharmony_ci
751686862fbSopenharmony_ci    HILOGI("[PerformanceTest] StartRemoteAbility transact begin");
752686862fbSopenharmony_ci    if (!DmsContinueTime::GetInstance().GetPull()) {
753686862fbSopenharmony_ci        int64_t begin = GetTickCount();
754686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationBegin(DMSDURATION_SRCTODSTRPCTIME, begin);
755686862fbSopenharmony_ci    }
756686862fbSopenharmony_ci    int32_t result = remoteDms->StartAbilityFromRemote(*newWant, abilityInfo, requestCode, callerInfo, accountInfo);
757686862fbSopenharmony_ci    if (!DmsContinueTime::GetInstance().GetPull()) {
758686862fbSopenharmony_ci        int64_t end = GetTickCount();
759686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationBegin(DMSDURATION_STARTABILITY, end);
760686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationEnd(DMSDURATION_SRCTODSTRPCTIME, end);
761686862fbSopenharmony_ci    }
762686862fbSopenharmony_ci    HILOGI("[PerformanceTest] StartRemoteAbility transact end");
763686862fbSopenharmony_ci    return result;
764686862fbSopenharmony_ci}
765686862fbSopenharmony_ci
766686862fbSopenharmony_ciint32_t DistributedSchedService::StartAbilityFromRemote(const OHOS::AAFwk::Want& want,
767686862fbSopenharmony_ci    const OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t requestCode,
768686862fbSopenharmony_ci    const CallerInfo& callerInfo, const AccountInfo& accountInfo)
769686862fbSopenharmony_ci{
770686862fbSopenharmony_ci    std::string localDeviceId;
771686862fbSopenharmony_ci    std::string timeInfo;
772686862fbSopenharmony_ci    std::string deviceId = want.GetElement().GetDeviceID();
773686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) ||
774686862fbSopenharmony_ci        !CheckDeviceIdFromRemote(localDeviceId, deviceId, callerInfo.sourceDeviceId)) {
775686862fbSopenharmony_ci        HILOGE("check deviceId failed");
776686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
777686862fbSopenharmony_ci    }
778686862fbSopenharmony_ci    if (DmsContinueTime::GetInstance().GetPull()) {
779686862fbSopenharmony_ci        timeInfo = want.GetStringParam(DMSDURATION_SAVETIME);
780686862fbSopenharmony_ci        DmsContinueTime::GetInstance().ReadDurationInfo(timeInfo.c_str());
781686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetSrcBundleName(want.GetElement().GetBundleName());
782686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetSrcAbilityName(want.GetElement().GetAbilityName());
783686862fbSopenharmony_ci    }
784686862fbSopenharmony_ci    int32_t result = CheckTargetPermission(want, callerInfo, accountInfo, START_PERMISSION, true);
785686862fbSopenharmony_ci    if (result != ERR_OK) {
786686862fbSopenharmony_ci        HILOGE("CheckTargetPermission failed!!");
787686862fbSopenharmony_ci        return result;
788686862fbSopenharmony_ci    }
789686862fbSopenharmony_ci
790686862fbSopenharmony_ci    return StartAbility(want, requestCode);
791686862fbSopenharmony_ci}
792686862fbSopenharmony_ci
793686862fbSopenharmony_ciint32_t DistributedSchedService::SendResultFromRemote(OHOS::AAFwk::Want& want, int32_t requestCode,
794686862fbSopenharmony_ci    const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t resultCode)
795686862fbSopenharmony_ci{
796686862fbSopenharmony_ci    std::string localDeviceId;
797686862fbSopenharmony_ci    std::string deviceId = want.GetStringParam(DMS_SRC_NETWORK_ID);
798686862fbSopenharmony_ci    want.RemoveParam(DMS_SRC_NETWORK_ID);
799686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) ||
800686862fbSopenharmony_ci        !CheckDeviceIdFromRemote(localDeviceId, deviceId, callerInfo.sourceDeviceId)) {
801686862fbSopenharmony_ci        HILOGE("check deviceId failed");
802686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
803686862fbSopenharmony_ci    }
804686862fbSopenharmony_ci    if (want.GetElement().GetBundleName().empty() && want.GetElement().GetAbilityName().empty()) {
805686862fbSopenharmony_ci        HILOGW("Remote died abnormal");
806686862fbSopenharmony_ci        int32_t missionId = want.GetIntParam(DMS_MISSION_ID, DEFAULT_DMS_MISSION_ID);
807686862fbSopenharmony_ci        ErrCode ret = AAFwk::AbilityManagerClient::GetInstance()->Connect();
808686862fbSopenharmony_ci        if (ret != ERR_OK) {
809686862fbSopenharmony_ci            HILOGE("connect ability server failed %{public}d", ret);
810686862fbSopenharmony_ci            return ret;
811686862fbSopenharmony_ci        }
812686862fbSopenharmony_ci        MissionInfo missionInfo;
813686862fbSopenharmony_ci        ret = AAFwk::AbilityManagerClient::GetInstance()->GetMissionInfo("", missionId, missionInfo);
814686862fbSopenharmony_ci        if (ret != ERR_OK) {
815686862fbSopenharmony_ci            HILOGE("SendResult failed %{public}d", ret);
816686862fbSopenharmony_ci            return ret;
817686862fbSopenharmony_ci        }
818686862fbSopenharmony_ci        std::string bundleName = missionInfo.want.GetElement().GetBundleName();
819686862fbSopenharmony_ci        std::string abilityName = missionInfo.want.GetElement().GetAbilityName();
820686862fbSopenharmony_ci        HILOGD("bundlename: %{public}s, ability is %{public}s", bundleName.c_str(), abilityName.c_str());
821686862fbSopenharmony_ci        ElementName element{"", bundleName, abilityName};
822686862fbSopenharmony_ci        want.SetElement(element);
823686862fbSopenharmony_ci    }
824686862fbSopenharmony_ci    int32_t result = CheckTargetPermission(want, callerInfo, accountInfo, SEND_RESULT_PERMISSION, false);
825686862fbSopenharmony_ci    if (result != ERR_OK) {
826686862fbSopenharmony_ci        HILOGE("CheckTargetPermission failed!!");
827686862fbSopenharmony_ci        return result;
828686862fbSopenharmony_ci    }
829686862fbSopenharmony_ci    ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->Connect();
830686862fbSopenharmony_ci    if (err != ERR_OK) {
831686862fbSopenharmony_ci        HILOGE("connect ability server failed %{public}d", err);
832686862fbSopenharmony_ci        return err;
833686862fbSopenharmony_ci    }
834686862fbSopenharmony_ci    err = AAFwk::AbilityManagerClient::GetInstance()->SendResultToAbility(requestCode, resultCode, want);
835686862fbSopenharmony_ci    if (err != ERR_OK) {
836686862fbSopenharmony_ci        HILOGE("SendResult failed %{public}d", err);
837686862fbSopenharmony_ci    }
838686862fbSopenharmony_ci    return err;
839686862fbSopenharmony_ci}
840686862fbSopenharmony_ci
841686862fbSopenharmony_civoid DistributedSchedService::RemoveContinuationTimeout(int32_t missionId)
842686862fbSopenharmony_ci{
843686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
844686862fbSopenharmony_ci        HILOGE("continuation object null!");
845686862fbSopenharmony_ci        return;
846686862fbSopenharmony_ci    }
847686862fbSopenharmony_ci    dschedContinuation_->RemoveTimeOut(missionId);
848686862fbSopenharmony_ci}
849686862fbSopenharmony_ci
850686862fbSopenharmony_civoid DistributedSchedService::SetContinuationTimeout(int32_t missionId, int32_t timeout)
851686862fbSopenharmony_ci{
852686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
853686862fbSopenharmony_ci        HILOGE("continuation object null!");
854686862fbSopenharmony_ci        return;
855686862fbSopenharmony_ci    }
856686862fbSopenharmony_ci    dschedContinuation_->SetTimeOut(missionId, timeout);
857686862fbSopenharmony_ci}
858686862fbSopenharmony_ci
859686862fbSopenharmony_cistd::string DistributedSchedService::GetContinuaitonDevice(int32_t missionId)
860686862fbSopenharmony_ci{
861686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
862686862fbSopenharmony_ci        HILOGE("continuation object null!");
863686862fbSopenharmony_ci        return "";
864686862fbSopenharmony_ci    }
865686862fbSopenharmony_ci    return dschedContinuation_->GetTargetDevice(missionId);
866686862fbSopenharmony_ci}
867686862fbSopenharmony_ci
868686862fbSopenharmony_ciint32_t DistributedSchedService::ContinueLocalMissionDealFreeInstall(OHOS::AAFwk::Want& want, int32_t missionId,
869686862fbSopenharmony_ci    const std::string& dstDeviceId, const sptr<IRemoteObject>& callback)
870686862fbSopenharmony_ci{
871686862fbSopenharmony_ci    bool isFreeInstall = want.GetBoolParam("isFreeInstall", false);
872686862fbSopenharmony_ci    if (!isFreeInstall) {
873686862fbSopenharmony_ci        HILOGE("remote not installed but support freeInstall, try again with freeInstall flag");
874686862fbSopenharmony_ci        return CONTINUE_REMOTE_UNINSTALLED_SUPPORT_FREEINSTALL;
875686862fbSopenharmony_ci    }
876686862fbSopenharmony_ci
877686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
878686862fbSopenharmony_ci        HILOGE("continuation object null!");
879686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
880686862fbSopenharmony_ci    }
881686862fbSopenharmony_ci    dschedContinuation_->PushCallback(missionId, callback, dstDeviceId, true);
882686862fbSopenharmony_ci    SetContinuationTimeout(missionId, CHECK_REMOTE_INSTALL_ABILITY);
883686862fbSopenharmony_ci
884686862fbSopenharmony_ci    want.SetDeviceId(dstDeviceId);
885686862fbSopenharmony_ci    if (!BundleManagerInternal::CheckIfRemoteCanInstall(want, missionId)) {
886686862fbSopenharmony_ci        HILOGE("call CheckIfRemoteCanInstall failed");
887686862fbSopenharmony_ci        RemoveContinuationTimeout(missionId);
888686862fbSopenharmony_ci        dschedContinuation_->PopCallback(missionId);
889686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
890686862fbSopenharmony_ci    }
891686862fbSopenharmony_ci    return ERR_OK;
892686862fbSopenharmony_ci}
893686862fbSopenharmony_ci
894686862fbSopenharmony_ciint32_t DistributedSchedService::ContinueLocalMission(const std::string& dstDeviceId, int32_t missionId,
895686862fbSopenharmony_ci    const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)
896686862fbSopenharmony_ci{
897686862fbSopenharmony_ci    if (!dataShareManager.IsCurrentContinueSwitchOn()) {
898686862fbSopenharmony_ci        HILOGE("ContinueSwitch status is off");
899686862fbSopenharmony_ci        return DMS_PERMISSION_DENIED;
900686862fbSopenharmony_ci    }
901686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
902686862fbSopenharmony_ci        HILOGE("continuation object null!");
903686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
904686862fbSopenharmony_ci    }
905686862fbSopenharmony_ci    if (dschedContinuation_->IsInContinuationProgress(missionId)) {
906686862fbSopenharmony_ci        HILOGE("ContinueLocalMission already in progress!");
907686862fbSopenharmony_ci        return CONTINUE_ALREADY_IN_PROGRESS;
908686862fbSopenharmony_ci    }
909686862fbSopenharmony_ci    DmsVersion thresholdDmsVersion = {3, 2, 0};
910686862fbSopenharmony_ci    if (DmsVersionManager::IsRemoteDmsVersionLower(dstDeviceId, thresholdDmsVersion)) {
911686862fbSopenharmony_ci        HILOGI("remote dms is low version");
912686862fbSopenharmony_ci        return ContinueAbilityWithTimeout(dstDeviceId, missionId, callback);
913686862fbSopenharmony_ci    }
914686862fbSopenharmony_ci
915686862fbSopenharmony_ci    MissionInfo missionInfo;
916686862fbSopenharmony_ci    int32_t result = AbilityManagerClient::GetInstance()->GetMissionInfo("", missionId, missionInfo);
917686862fbSopenharmony_ci    if (result != ERR_OK) {
918686862fbSopenharmony_ci        HILOGE("get missionInfo failed");
919686862fbSopenharmony_ci        return NO_MISSION_INFO_FOR_MISSION_ID;
920686862fbSopenharmony_ci    }
921686862fbSopenharmony_ci    if (missionInfo.continueState != AAFwk::ContinueState::CONTINUESTATE_ACTIVE) {
922686862fbSopenharmony_ci        HILOGE("Mission continue state set to INACTIVE. Can't continue. Mission id: %{public}d", missionId);
923686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
924686862fbSopenharmony_ci    }
925686862fbSopenharmony_ci    std::string bundleName = missionInfo.want.GetBundle();
926686862fbSopenharmony_ci    if (!DmsKvSyncE2E::GetInstance()->CheckBundleContinueConfig(bundleName)) {
927686862fbSopenharmony_ci        HILOGI("App does not allow continue in config file, bundle name %{public}s, missionId: %{public}d",
928686862fbSopenharmony_ci            bundleName.c_str(), missionId);
929686862fbSopenharmony_ci        return REMOTE_DEVICE_BIND_ABILITY_ERR;
930686862fbSopenharmony_ci    }
931686862fbSopenharmony_ci    missionInfo.want.SetParams(wantParams);
932686862fbSopenharmony_ci    DistributedBundleInfo remoteBundleInfo;
933686862fbSopenharmony_ci    result = BundleManagerInternal::CheckRemoteBundleInfoForContinuation(dstDeviceId,
934686862fbSopenharmony_ci        bundleName, remoteBundleInfo);
935686862fbSopenharmony_ci    if (result == ERR_OK) {
936686862fbSopenharmony_ci        return ContinueAbilityWithTimeout(dstDeviceId, missionId, callback, remoteBundleInfo.versionCode);
937686862fbSopenharmony_ci    }
938686862fbSopenharmony_ci    if (result == CONTINUE_REMOTE_UNINSTALLED_UNSUPPORT_FREEINSTALL) {
939686862fbSopenharmony_ci        HILOGE("remote not installed and app not support free install");
940686862fbSopenharmony_ci        return result;
941686862fbSopenharmony_ci    }
942686862fbSopenharmony_ci
943686862fbSopenharmony_ci    return ContinueLocalMissionDealFreeInstall(missionInfo.want, missionId, dstDeviceId, callback);
944686862fbSopenharmony_ci}
945686862fbSopenharmony_ci
946686862fbSopenharmony_ciint32_t DistributedSchedService::ContinueAbilityWithTimeout(const std::string& dstDeviceId, int32_t missionId,
947686862fbSopenharmony_ci    const sptr<IRemoteObject>& callback, uint32_t remoteBundleVersion)
948686862fbSopenharmony_ci{
949686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
950686862fbSopenharmony_ci        HILOGE("continuation object null!");
951686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
952686862fbSopenharmony_ci    }
953686862fbSopenharmony_ci    bool isPushSucceed = dschedContinuation_->PushCallback(missionId, callback, dstDeviceId, false);
954686862fbSopenharmony_ci    if (!isPushSucceed) {
955686862fbSopenharmony_ci        HILOGE("Callback already in progress!");
956686862fbSopenharmony_ci        return CONTINUE_ALREADY_IN_PROGRESS;
957686862fbSopenharmony_ci    }
958686862fbSopenharmony_ci    SetContinuationTimeout(missionId, CONTINUATION_TIMEOUT);
959686862fbSopenharmony_ci    int64_t saveDataBegin = GetTickCount();
960686862fbSopenharmony_ci    DmsContinueTime::GetInstance().SetSaveDataDurationBegin(saveDataBegin);
961686862fbSopenharmony_ci    DmsRadar::GetInstance().SaveDataDmsContinue("ContinueAbility", ERR_OK);
962686862fbSopenharmony_ci    int32_t result = AbilityManagerClient::GetInstance()->ContinueAbility(dstDeviceId, missionId, remoteBundleVersion);
963686862fbSopenharmony_ci    HILOGI("result: %{public}d!", result);
964686862fbSopenharmony_ci    if (result == ERR_INVALID_VALUE) {
965686862fbSopenharmony_ci        return MISSION_FOR_CONTINUING_IS_NOT_ALIVE;
966686862fbSopenharmony_ci    }
967686862fbSopenharmony_ci    return result;
968686862fbSopenharmony_ci}
969686862fbSopenharmony_ci
970686862fbSopenharmony_ciint32_t DistributedSchedService::ContinueRemoteMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
971686862fbSopenharmony_ci    int32_t missionId, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)
972686862fbSopenharmony_ci{
973686862fbSopenharmony_ci    if (!dataShareManager.IsCurrentContinueSwitchOn()) {
974686862fbSopenharmony_ci        HILOGE("ContinueSwitch status is off");
975686862fbSopenharmony_ci        return DMS_PERMISSION_DENIED;
976686862fbSopenharmony_ci    }
977686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(srcDeviceId);
978686862fbSopenharmony_ci    if (remoteDms == nullptr) {
979686862fbSopenharmony_ci        HILOGE("get remote dms null!");
980686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
981686862fbSopenharmony_ci    }
982686862fbSopenharmony_ci
983686862fbSopenharmony_ci    MissionInfo missionInfo;
984686862fbSopenharmony_ci    int32_t ret = AAFwk::AbilityManagerClient::GetInstance()->GetMissionInfo("", missionId, missionInfo);
985686862fbSopenharmony_ci    if (ret != ERR_OK) {
986686862fbSopenharmony_ci        HILOGE("GeGetMissionInfo failed %{public}d", ret);
987686862fbSopenharmony_ci        return ret;
988686862fbSopenharmony_ci    }
989686862fbSopenharmony_ci    std::string bundleName = missionInfo.want.GetElement().GetBundleName();
990686862fbSopenharmony_ci    std::string abilityName = missionInfo.want.GetElement().GetAbilityName();
991686862fbSopenharmony_ci    HILOGD("bundlename: %{public}s, ability is %{public}s", bundleName.c_str(), abilityName.c_str());
992686862fbSopenharmony_ci    DmsUE::GetInstance().TriggerDmsContinue(bundleName, abilityName, srcDeviceId, ERR_OK);
993686862fbSopenharmony_ci
994686862fbSopenharmony_ci    std::string peerUdid = DtbschedmgrDeviceInfoStorage::GetInstance().GetUdidByNetworkId(srcDeviceId);
995686862fbSopenharmony_ci    DmsRadar::GetInstance().ClickIconDmsContinue("ContinueMission", ERR_OK, peerUdid, bundleName, bundleName);
996686862fbSopenharmony_ci
997686862fbSopenharmony_ci    int32_t result = remoteDms->ContinueMission(srcDeviceId, dstDeviceId, missionId, callback, wantParams);
998686862fbSopenharmony_ci    HILOGI("ContinueRemoteMission result: %{public}d!", result);
999686862fbSopenharmony_ci    return result;
1000686862fbSopenharmony_ci}
1001686862fbSopenharmony_ci
1002686862fbSopenharmony_ciint32_t DistributedSchedService::ContinueRemoteMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
1003686862fbSopenharmony_ci    const std::string& bundleName, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)
1004686862fbSopenharmony_ci{
1005686862fbSopenharmony_ci    HILOGI("ContinueRemoteMission srcDeviceId: %{public}s. dstDeviceId: %{public}s. bundleName: %{public}s.",
1006686862fbSopenharmony_ci        GetAnonymStr(srcDeviceId).c_str(), GetAnonymStr(dstDeviceId).c_str(), bundleName.c_str());
1007686862fbSopenharmony_ci    if (DmsContinueTime::GetInstance().GetPull()) {
1008686862fbSopenharmony_ci        int64_t begin = GetTickCount();
1009686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationBegin(DMSDURATION_DSTTOSRCRPCTIME, begin);
1010686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationBegin(DMSDURATION_TOTALTIME, begin);
1011686862fbSopenharmony_ci    }
1012686862fbSopenharmony_ci
1013686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(srcDeviceId);
1014686862fbSopenharmony_ci    if (remoteDms == nullptr) {
1015686862fbSopenharmony_ci        HILOGE("get remote dms null!");
1016686862fbSopenharmony_ci        if (dschedContinuation_ == nullptr) {
1017686862fbSopenharmony_ci            HILOGE("continuation object null!");
1018686862fbSopenharmony_ci            return INVALID_PARAMETERS_ERR;
1019686862fbSopenharmony_ci        }
1020686862fbSopenharmony_ci        int32_t dSchedEventresult = dschedContinuation_->NotifyDSchedEventResult(INVALID_REMOTE_PARAMETERS_ERR);
1021686862fbSopenharmony_ci        HILOGD("NotifyDSchedEventResult result:%{public}d", dSchedEventresult);
1022686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
1023686862fbSopenharmony_ci    }
1024686862fbSopenharmony_ci    int32_t result = remoteDms->ContinueMission(srcDeviceId, dstDeviceId, bundleName, callback, wantParams);
1025686862fbSopenharmony_ci    HILOGI("ContinueRemoteMission result: %{public}d!", result);
1026686862fbSopenharmony_ci    if (DmsContinueTime::GetInstance().GetPull()) {
1027686862fbSopenharmony_ci        int64_t end = GetTickCount();
1028686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationEnd(DMSDURATION_DSTTOSRCRPCTIME, end);
1029686862fbSopenharmony_ci    }
1030686862fbSopenharmony_ci    if (result != ERR_OK) {
1031686862fbSopenharmony_ci        if (dschedContinuation_ == nullptr) {
1032686862fbSopenharmony_ci            HILOGE("continuation object null!");
1033686862fbSopenharmony_ci            return INVALID_PARAMETERS_ERR;
1034686862fbSopenharmony_ci        }
1035686862fbSopenharmony_ci        int32_t dSchedEventresult = dschedContinuation_->NotifyDSchedEventResult(result);
1036686862fbSopenharmony_ci        HILOGD("NotifyDSchedEventResult result:%{public}d", dSchedEventresult);
1037686862fbSopenharmony_ci    }
1038686862fbSopenharmony_ci    return result;
1039686862fbSopenharmony_ci}
1040686862fbSopenharmony_ci
1041686862fbSopenharmony_ciint32_t DistributedSchedService::ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
1042686862fbSopenharmony_ci    int32_t missionId, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)
1043686862fbSopenharmony_ci{
1044686862fbSopenharmony_ci    HILOGI("ContinueMission called");
1045686862fbSopenharmony_ci    if (srcDeviceId.empty() || dstDeviceId.empty() || callback == nullptr) {
1046686862fbSopenharmony_ci        HILOGE("srcDeviceId or dstDeviceId or callback is null!");
1047686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1048686862fbSopenharmony_ci    }
1049686862fbSopenharmony_ci    std::string localDevId;
1050686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDevId)) {
1051686862fbSopenharmony_ci        HILOGE("get local deviceId failed!");
1052686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1053686862fbSopenharmony_ci    }
1054686862fbSopenharmony_ci    DurationStart(srcDeviceId, dstDeviceId);
1055686862fbSopenharmony_ci
1056686862fbSopenharmony_ci    if (srcDeviceId == localDevId) {
1057686862fbSopenharmony_ci        if (DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(dstDeviceId) == nullptr) {
1058686862fbSopenharmony_ci            HILOGE("GetDeviceInfoById failed, dstDeviceId: %{public}s.", GetAnonymStr(dstDeviceId).c_str());
1059686862fbSopenharmony_ci            return INVALID_REMOTE_PARAMETERS_ERR;
1060686862fbSopenharmony_ci        }
1061686862fbSopenharmony_ci        return ContinueLocalMission(dstDeviceId, missionId, callback, wantParams);
1062686862fbSopenharmony_ci    } else if (dstDeviceId == localDevId) {
1063686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetPull(true);
1064686862fbSopenharmony_ci        if (DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(srcDeviceId) == nullptr) {
1065686862fbSopenharmony_ci            HILOGE("GetDeviceInfoById failed, srcDeviceId: %{public}s.", GetAnonymStr(srcDeviceId).c_str());
1066686862fbSopenharmony_ci            return INVALID_REMOTE_PARAMETERS_ERR;
1067686862fbSopenharmony_ci        }
1068686862fbSopenharmony_ci        return ContinueRemoteMission(srcDeviceId, dstDeviceId, missionId, callback, wantParams);
1069686862fbSopenharmony_ci    } else {
1070686862fbSopenharmony_ci        HILOGE("source or target device must be local!");
1071686862fbSopenharmony_ci        return OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET;
1072686862fbSopenharmony_ci    }
1073686862fbSopenharmony_ci}
1074686862fbSopenharmony_ci
1075686862fbSopenharmony_ciint32_t DistributedSchedService::ProcessContinueLocalMission(const std::string& srcDeviceId,
1076686862fbSopenharmony_ci    const std::string& dstDeviceId, const std::string& bundleName, const sptr<IRemoteObject>& callback,
1077686862fbSopenharmony_ci    const OHOS::AAFwk::WantParams& wantParams)
1078686862fbSopenharmony_ci{
1079686862fbSopenharmony_ci    HILOGI("ProcessContinueLocalMission called.");
1080686862fbSopenharmony_ci    if (DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(dstDeviceId) == nullptr) {
1081686862fbSopenharmony_ci        HILOGE("GetDeviceInfoById failed, dstDeviceId: %{public}s.", GetAnonymStr(dstDeviceId).c_str());
1082686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
1083686862fbSopenharmony_ci    }
1084686862fbSopenharmony_ci
1085686862fbSopenharmony_ci    int32_t missionId = 1;
1086686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
1087686862fbSopenharmony_ci    int32_t ret = DMSContinueSendMgr::GetInstance().GetMissionIdByBundleName(bundleName, missionId);
1088686862fbSopenharmony_ci    if (ret != ERR_OK) {
1089686862fbSopenharmony_ci        HILOGE("get missionId failed");
1090686862fbSopenharmony_ci        return ret;
1091686862fbSopenharmony_ci    }
1092686862fbSopenharmony_ci#endif
1093686862fbSopenharmony_ci
1094686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1095686862fbSopenharmony_ci        HILOGE("continuation object null!");
1096686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1097686862fbSopenharmony_ci    }
1098686862fbSopenharmony_ci    dschedContinuation_->continueEvent_.srcNetworkId_ = srcDeviceId;
1099686862fbSopenharmony_ci    dschedContinuation_->continueEvent_.dstNetworkId_ = dstDeviceId;
1100686862fbSopenharmony_ci    return ContinueLocalMission(dstDeviceId, missionId, callback, wantParams);
1101686862fbSopenharmony_ci}
1102686862fbSopenharmony_ci
1103686862fbSopenharmony_ciint32_t DistributedSchedService::ProcessContinueRemoteMission(const std::string& srcDeviceId,
1104686862fbSopenharmony_ci    const std::string& dstDeviceId, const std::string& bundleName, const sptr<IRemoteObject>& callback,
1105686862fbSopenharmony_ci    const OHOS::AAFwk::WantParams& wantParams)
1106686862fbSopenharmony_ci{
1107686862fbSopenharmony_ci    HILOGI("ProcessContinueRemoteMission start.");
1108686862fbSopenharmony_ci    if (DtbschedmgrDeviceInfoStorage::GetInstance().GetDeviceInfoById(srcDeviceId) == nullptr) {
1109686862fbSopenharmony_ci        HILOGE("GetDeviceInfoById failed, srcDeviceId: %{public}s.", GetAnonymStr(srcDeviceId).c_str());
1110686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
1111686862fbSopenharmony_ci    }
1112686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1113686862fbSopenharmony_ci        HILOGE("continuation object null!");
1114686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1115686862fbSopenharmony_ci    }
1116686862fbSopenharmony_ci    dschedContinuation_->continueEvent_.srcNetworkId_ = dstDeviceId;
1117686862fbSopenharmony_ci    dschedContinuation_->continueEvent_.dstNetworkId_ = srcDeviceId;
1118686862fbSopenharmony_ci    HILOGI("ProcessContinueRemoteMission end.");
1119686862fbSopenharmony_ci    return ContinueRemoteMission(srcDeviceId, dstDeviceId, bundleName, callback, wantParams);
1120686862fbSopenharmony_ci}
1121686862fbSopenharmony_ci
1122686862fbSopenharmony_ciint32_t DistributedSchedService::ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId,
1123686862fbSopenharmony_ci    const std::string& bundleName, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams)
1124686862fbSopenharmony_ci{
1125686862fbSopenharmony_ci    HILOGI("ContinueMission srcDeviceId: %{public}s. dstDeviceId: %{public}s. bundleName: %{public}s.",
1126686862fbSopenharmony_ci        GetAnonymStr(srcDeviceId).c_str(), GetAnonymStr(dstDeviceId).c_str(), bundleName.c_str());
1127686862fbSopenharmony_ci    if (srcDeviceId.empty() || dstDeviceId.empty() || callback == nullptr) {
1128686862fbSopenharmony_ci        HILOGE("srcDeviceId or dstDeviceId or callback is null!");
1129686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1130686862fbSopenharmony_ci    }
1131686862fbSopenharmony_ci    std::string localDevId;
1132686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDevId)) {
1133686862fbSopenharmony_ci        HILOGE("get local deviceId failed!");
1134686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1135686862fbSopenharmony_ci    }
1136686862fbSopenharmony_ci    DurationStart(srcDeviceId, dstDeviceId);
1137686862fbSopenharmony_ci
1138686862fbSopenharmony_ci    if (srcDeviceId == localDevId) {
1139686862fbSopenharmony_ci        return ProcessContinueLocalMission(srcDeviceId, dstDeviceId, bundleName, callback, wantParams);
1140686862fbSopenharmony_ci    } else if (dstDeviceId == localDevId) {
1141686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetPull(true);
1142686862fbSopenharmony_ci        return ProcessContinueRemoteMission(srcDeviceId, dstDeviceId, bundleName, callback, wantParams);
1143686862fbSopenharmony_ci    } else {
1144686862fbSopenharmony_ci        HILOGE("source or target device must be local!");
1145686862fbSopenharmony_ci        return OPERATION_DEVICE_NOT_INITIATOR_OR_TARGET;
1146686862fbSopenharmony_ci    }
1147686862fbSopenharmony_ci}
1148686862fbSopenharmony_ci
1149686862fbSopenharmony_ciint32_t DistributedSchedService::SetWantForContinuation(AAFwk::Want& newWant, int32_t missionId)
1150686862fbSopenharmony_ci{
1151686862fbSopenharmony_ci    std::string devId;
1152686862fbSopenharmony_ci    if (!GetLocalDeviceId(devId)) {
1153686862fbSopenharmony_ci        HILOGE("StartContinuation get local deviceId failed!");
1154686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
1155686862fbSopenharmony_ci    }
1156686862fbSopenharmony_ci
1157686862fbSopenharmony_ci    newWant.SetParam("sessionId", missionId);
1158686862fbSopenharmony_ci    newWant.SetParam("deviceId", devId);
1159686862fbSopenharmony_ci    std::string strInfo = DmsContinueTime::GetInstance().WriteDurationInfo(
1160686862fbSopenharmony_ci        DmsContinueTime::GetInstance().GetSaveDataDuration());
1161686862fbSopenharmony_ci    newWant.SetParam(DMSDURATION_SAVETIME, strInfo);
1162686862fbSopenharmony_ci    BundleInfo localBundleInfo;
1163686862fbSopenharmony_ci    if (BundleManagerInternal::GetLocalBundleInfo(newWant.GetBundle(), localBundleInfo) != ERR_OK) {
1164686862fbSopenharmony_ci        HILOGE("get local bundle info failed");
1165686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1166686862fbSopenharmony_ci    }
1167686862fbSopenharmony_ci    newWant.SetParam(VERSION_CODE_KEY, static_cast<int32_t>(localBundleInfo.versionCode));
1168686862fbSopenharmony_ci
1169686862fbSopenharmony_ci    bool isPageStackContinue = newWant.GetBoolParam(SUPPORT_CONTINUE_PAGE_STACK_KEY, true);
1170686862fbSopenharmony_ci    std::string moduleName = newWant.GetStringParam(SUPPORT_CONTINUE_MODULE_NAME_UPDATE_KEY);
1171686862fbSopenharmony_ci    if (!isPageStackContinue && !moduleName.empty() && moduleName.length() <= MAX_MODULENAME_LEN) {
1172686862fbSopenharmony_ci        HILOGD("set application moduleName = %{private}s!", moduleName.c_str());
1173686862fbSopenharmony_ci        OHOS::AppExecFwk::ElementName element = newWant.GetElement();
1174686862fbSopenharmony_ci        newWant.SetElementName(element.GetDeviceID(), element.GetBundleName(), element.GetAbilityName(), moduleName);
1175686862fbSopenharmony_ci    }
1176686862fbSopenharmony_ci    HILOGD("local version = %{public}u!", localBundleInfo.versionCode);
1177686862fbSopenharmony_ci    return ERR_OK;
1178686862fbSopenharmony_ci}
1179686862fbSopenharmony_ci
1180686862fbSopenharmony_ciint32_t DistributedSchedService::DealDSchedEventResult(const OHOS::AAFwk::Want& want, int32_t status)
1181686862fbSopenharmony_ci{
1182686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1183686862fbSopenharmony_ci        HILOGE("continuation object null!");
1184686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1185686862fbSopenharmony_ci    }
1186686862fbSopenharmony_ci    DmsContinueTime::GetInstance().SetSrcBundleName(want.GetElement().GetBundleName());
1187686862fbSopenharmony_ci    DmsContinueTime::GetInstance().SetSrcAbilityName(dschedContinuation_->continueEvent_.srcAbilityName_);
1188686862fbSopenharmony_ci    if (status != ERR_OK) {
1189686862fbSopenharmony_ci        HILOGD("want deviceId result:%{public}s", GetAnonymStr(want.GetElement().GetDeviceID()).c_str());
1190686862fbSopenharmony_ci        std::string deviceId = want.GetElement().GetDeviceID();
1191686862fbSopenharmony_ci        sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
1192686862fbSopenharmony_ci        if (remoteDms == nullptr) {
1193686862fbSopenharmony_ci            HILOGE("NotifyCompleteContinuation get remote dms null!");
1194686862fbSopenharmony_ci            return INVALID_REMOTE_PARAMETERS_ERR;
1195686862fbSopenharmony_ci        }
1196686862fbSopenharmony_ci        int dSchedEventresult = remoteDms->NotifyDSchedEventResultFromRemote(DSCHED_EVENT_KEY, status);
1197686862fbSopenharmony_ci        HILOGI("NotifyDSchedEventResultFromRemote result:%{public}d", dSchedEventresult);
1198686862fbSopenharmony_ci    }
1199686862fbSopenharmony_ci    return ERR_OK;
1200686862fbSopenharmony_ci}
1201686862fbSopenharmony_ci
1202686862fbSopenharmony_cibool DistributedSchedService::GetIsFreeInstall(int32_t missionId)
1203686862fbSopenharmony_ci{
1204686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1205686862fbSopenharmony_ci        return false;
1206686862fbSopenharmony_ci    }
1207686862fbSopenharmony_ci    return dschedContinuation_->IsFreeInstall(missionId);
1208686862fbSopenharmony_ci}
1209686862fbSopenharmony_ci
1210686862fbSopenharmony_ciint32_t DistributedSchedService::StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId,
1211686862fbSopenharmony_ci    int32_t callerUid, int32_t status, uint32_t accessToken)
1212686862fbSopenharmony_ci{
1213686862fbSopenharmony_ci    HILOGD("[PerformanceTest] StartContinuation begin");
1214686862fbSopenharmony_ci    DealDSchedEventResult(want, status);
1215686862fbSopenharmony_ci    if (status != ERR_OK) {
1216686862fbSopenharmony_ci        HILOGE("continuation has been rejected, status: %{public}d", status);
1217686862fbSopenharmony_ci        NotifyContinuationCallbackResult(missionId, status);
1218686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
1219686862fbSopenharmony_ci    }
1220686862fbSopenharmony_ci    auto flags = want.GetFlags();
1221686862fbSopenharmony_ci    if ((flags & AAFwk::Want::FLAG_ABILITY_CONTINUATION) == 0) {
1222686862fbSopenharmony_ci        HILOGE("StartContinuation want continuation flags invalid!");
1223686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
1224686862fbSopenharmony_ci    }
1225686862fbSopenharmony_ci    HILOGD("StartContinuation: devId = %{private}s, bundleName = %{private}s, abilityName = %{private}s",
1226686862fbSopenharmony_ci        GetAnonymStr(want.GetElement().GetDeviceID()).c_str(), want.GetElement().GetBundleName().c_str(),
1227686862fbSopenharmony_ci        want.GetElement().GetAbilityName().c_str());
1228686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1229686862fbSopenharmony_ci        HILOGE("StartContinuation continuation object null!");
1230686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
1231686862fbSopenharmony_ci    }
1232686862fbSopenharmony_ci    if (!dschedContinuation_->IsInContinuationProgress(missionId)) {
1233686862fbSopenharmony_ci        dschedContinuation_->SetTimeOut(missionId, CONTINUATION_TIMEOUT);
1234686862fbSopenharmony_ci    }
1235686862fbSopenharmony_ci    AAFwk::Want newWant = want;
1236686862fbSopenharmony_ci    int result = SetWantForContinuation(newWant, missionId);
1237686862fbSopenharmony_ci    if (result != ERR_OK) {
1238686862fbSopenharmony_ci        HILOGE("set new want failed");
1239686862fbSopenharmony_ci        return result;
1240686862fbSopenharmony_ci    }
1241686862fbSopenharmony_ci    bool flag = GetIsFreeInstall(missionId);
1242686862fbSopenharmony_ci    SetCleanMissionFlag(want, missionId);
1243686862fbSopenharmony_ci    if (flag) {
1244686862fbSopenharmony_ci        result = StartRemoteFreeInstall(newWant, callerUid, DEFAULT_REQUEST_CODE, accessToken, nullptr);
1245686862fbSopenharmony_ci        if (result != ERR_OK) {
1246686862fbSopenharmony_ci            HILOGE("continue free install failed, result = %{public}d", result);
1247686862fbSopenharmony_ci            return result;
1248686862fbSopenharmony_ci        }
1249686862fbSopenharmony_ci    } else {
1250686862fbSopenharmony_ci        result = StartRemoteAbility(newWant, callerUid, DEFAULT_REQUEST_CODE, accessToken);
1251686862fbSopenharmony_ci        DmsRadar::GetInstance().SaveDataDmsRemoteWant("StartRemoteAbility", result);
1252686862fbSopenharmony_ci        if (result != ERR_OK) {
1253686862fbSopenharmony_ci            HILOGE("continue ability failed, errorCode = %{public}d", result);
1254686862fbSopenharmony_ci            return result;
1255686862fbSopenharmony_ci        }
1256686862fbSopenharmony_ci    }
1257686862fbSopenharmony_ci    HILOGD("[PerformanceTest] StartContinuation end");
1258686862fbSopenharmony_ci    return result;
1259686862fbSopenharmony_ci}
1260686862fbSopenharmony_ci
1261686862fbSopenharmony_civoid DistributedSchedService::NotifyCompleteContinuation(const std::u16string& devId,
1262686862fbSopenharmony_ci    int32_t sessionId, bool isSuccess)
1263686862fbSopenharmony_ci{
1264686862fbSopenharmony_ci    if (!isSuccess) {
1265686862fbSopenharmony_ci        HILOGE("NotifyCompleteContinuation failed!");
1266686862fbSopenharmony_ci    }
1267686862fbSopenharmony_ci    if (sessionId <= 0) {
1268686862fbSopenharmony_ci        HILOGE("NotifyCompleteContinuation sessionId invalid!");
1269686862fbSopenharmony_ci        return;
1270686862fbSopenharmony_ci    }
1271686862fbSopenharmony_ci    std::string deviceId = Str16ToStr8(devId);
1272686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
1273686862fbSopenharmony_ci    if (remoteDms == nullptr) {
1274686862fbSopenharmony_ci        HILOGE("NotifyCompleteContinuation get remote dms null!");
1275686862fbSopenharmony_ci        return;
1276686862fbSopenharmony_ci    }
1277686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1278686862fbSopenharmony_ci        HILOGE("continuation object null!");
1279686862fbSopenharmony_ci        return;
1280686862fbSopenharmony_ci    }
1281686862fbSopenharmony_ci    int dSchedEventResult = dschedContinuation_->NotifyDSchedEventResult(ERR_OK);
1282686862fbSopenharmony_ci    HILOGD("NotifyDSchedEventResult result:%{public}d", dSchedEventResult);
1283686862fbSopenharmony_ci
1284686862fbSopenharmony_ci    std::string dstInfo("");
1285686862fbSopenharmony_ci    if (DmsContinueTime::GetInstance().GetPull()) {
1286686862fbSopenharmony_ci        int64_t end = GetTickCount();
1287686862fbSopenharmony_ci        std::string strEndTime = DmsContinueTime::GetInstance().GetCurrentTime();
1288686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationEnd(DMSDURATION_STARTABILITY, end);
1289686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationEnd(DMSDURATION_TOTALTIME, end);
1290686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationStrTime(DMSDURATION_ENDTIME, strEndTime);
1291686862fbSopenharmony_ci
1292686862fbSopenharmony_ci        std::string bundleName = DmsContinueTime::GetInstance().GetDstInfo().bundleName;
1293686862fbSopenharmony_ci        std::string abilityName = DmsContinueTime::GetInstance().GetDstInfo().abilityName;
1294686862fbSopenharmony_ci        std::string srcNetworkId = dschedContinuation_->continueInfo_.srcNetworkId_;
1295686862fbSopenharmony_ci        DmsUE::GetInstance().DmsContinueComplete(bundleName, abilityName, srcNetworkId, dSchedEventResult);
1296686862fbSopenharmony_ci
1297686862fbSopenharmony_ci        DmsContinueTime::GetInstance().AppendInfo();
1298686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetPull(false);
1299686862fbSopenharmony_ci    } else {
1300686862fbSopenharmony_ci        dstInfo = DmsContinueTime::GetInstance().WriteDstInfo(DmsContinueTime::GetInstance().GetDstInfo().bundleName,
1301686862fbSopenharmony_ci            DmsContinueTime::GetInstance().GetDstInfo().abilityName);
1302686862fbSopenharmony_ci    }
1303686862fbSopenharmony_ci
1304686862fbSopenharmony_ci    remoteDms->NotifyContinuationResultFromRemote(sessionId, isSuccess, dstInfo);
1305686862fbSopenharmony_ci    dschedContinuation_->continueInfo_.srcNetworkId_ = "";
1306686862fbSopenharmony_ci    dschedContinuation_->continueInfo_.dstNetworkId_ = "";
1307686862fbSopenharmony_ci    DmsRadar::GetInstance().ClickIconDmsRecvOver("NotifyContinuationResultFromRemote", dSchedEventResult);
1308686862fbSopenharmony_ci}
1309686862fbSopenharmony_ci
1310686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess,
1311686862fbSopenharmony_ci    const std::string dstInfo)
1312686862fbSopenharmony_ci{
1313686862fbSopenharmony_ci    if (sessionId <= 0) {
1314686862fbSopenharmony_ci        HILOGE("NotifyContinuationResultFromRemote sessionId:%{public}d invalid!", sessionId);
1315686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
1316686862fbSopenharmony_ci    }
1317686862fbSopenharmony_ci    if (dstInfo.length() != 0) {
1318686862fbSopenharmony_ci        int64_t end = GetTickCount();
1319686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationEnd(DMSDURATION_STARTABILITY, end);
1320686862fbSopenharmony_ci        std::string strEndTime = DmsContinueTime::GetInstance().GetCurrentTime();
1321686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDurationStrTime(DMSDURATION_ENDTIME, strEndTime);
1322686862fbSopenharmony_ci        DmsContinueTime::GetInstance().ReadDstInfo(dstInfo.c_str());
1323686862fbSopenharmony_ci        DmsContinueTime::GetInstance().AppendInfo();
1324686862fbSopenharmony_ci    }
1325686862fbSopenharmony_ci
1326686862fbSopenharmony_ci    int32_t missionId = sessionId;
1327686862fbSopenharmony_ci    NotifyContinuationCallbackResult(missionId, isSuccess ? 0 : NOTIFYCOMPLETECONTINUATION_FAILED);
1328686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1329686862fbSopenharmony_ci        HILOGW("continuation object null!");
1330686862fbSopenharmony_ci        return ERR_OK;
1331686862fbSopenharmony_ci    }
1332686862fbSopenharmony_ci    dschedContinuation_->continueInfo_.srcNetworkId_ = "";
1333686862fbSopenharmony_ci    dschedContinuation_->continueInfo_.dstNetworkId_ = "";
1334686862fbSopenharmony_ci    return ERR_OK;
1335686862fbSopenharmony_ci}
1336686862fbSopenharmony_ci
1337686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult)
1338686862fbSopenharmony_ci{
1339686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(dSchedEventResult);
1340686862fbSopenharmony_ci    return ERR_OK;
1341686862fbSopenharmony_ci}
1342686862fbSopenharmony_ci
1343686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
1344686862fbSopenharmony_cisptr<IFormMgr> DistributedSchedService::GetFormMgrProxy()
1345686862fbSopenharmony_ci{
1346686862fbSopenharmony_ci    HILOGD("GetFormMgrProxy begin.");
1347686862fbSopenharmony_ci    std::lock_guard<std::mutex> lock(formMgrLock_);
1348686862fbSopenharmony_ci    if (formMgrProxy_ != nullptr) {
1349686862fbSopenharmony_ci        HILOGD("get fms proxy success.");
1350686862fbSopenharmony_ci        return formMgrProxy_;
1351686862fbSopenharmony_ci    }
1352686862fbSopenharmony_ci
1353686862fbSopenharmony_ci    auto systemAbilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
1354686862fbSopenharmony_ci    if (systemAbilityMgr == nullptr) {
1355686862fbSopenharmony_ci        HILOGE("system ability manager is nullptr.");
1356686862fbSopenharmony_ci        return nullptr;
1357686862fbSopenharmony_ci    }
1358686862fbSopenharmony_ci
1359686862fbSopenharmony_ci    auto remoteObj = systemAbilityMgr->GetSystemAbility(FORM_MGR_SERVICE_ID);
1360686862fbSopenharmony_ci    if (remoteObj == nullptr) {
1361686862fbSopenharmony_ci        HILOGE("failed to get form manager service");
1362686862fbSopenharmony_ci        return nullptr;
1363686862fbSopenharmony_ci    }
1364686862fbSopenharmony_ci
1365686862fbSopenharmony_ci    formMgrDeathRecipient_ = sptr<IRemoteObject::DeathRecipient>(new FormMgrDeathRecipient());
1366686862fbSopenharmony_ci    if (formMgrDeathRecipient_ == nullptr) {
1367686862fbSopenharmony_ci        HILOGE("failed to create FormMgrDeathRecipient!");
1368686862fbSopenharmony_ci        return nullptr;
1369686862fbSopenharmony_ci    }
1370686862fbSopenharmony_ci
1371686862fbSopenharmony_ci    if ((remoteObj->IsProxyObject()) && (!remoteObj->AddDeathRecipient(formMgrDeathRecipient_))) {
1372686862fbSopenharmony_ci        HILOGE("add death recipient to FormMgrService failed.");
1373686862fbSopenharmony_ci        return nullptr;
1374686862fbSopenharmony_ci    }
1375686862fbSopenharmony_ci
1376686862fbSopenharmony_ci    formMgrProxy_ = iface_cast<IFormMgr>(remoteObj);
1377686862fbSopenharmony_ci    return formMgrProxy_;
1378686862fbSopenharmony_ci}
1379686862fbSopenharmony_ci
1380686862fbSopenharmony_civoid DistributedSchedService::ProcessFormMgrDied(const wptr<IRemoteObject>& remote)
1381686862fbSopenharmony_ci{
1382686862fbSopenharmony_ci    std::lock_guard<std::mutex> lock(formMgrLock_);
1383686862fbSopenharmony_ci    if (formMgrProxy_ == nullptr) {
1384686862fbSopenharmony_ci        return;
1385686862fbSopenharmony_ci    }
1386686862fbSopenharmony_ci
1387686862fbSopenharmony_ci    auto serviceRemote = formMgrProxy_->AsObject();
1388686862fbSopenharmony_ci    if ((serviceRemote != nullptr) && (serviceRemote == remote.promote())) {
1389686862fbSopenharmony_ci        serviceRemote->RemoveDeathRecipient(formMgrDeathRecipient_);
1390686862fbSopenharmony_ci        formMgrProxy_ = nullptr;
1391686862fbSopenharmony_ci    }
1392686862fbSopenharmony_ci}
1393686862fbSopenharmony_ci#endif
1394686862fbSopenharmony_ci
1395686862fbSopenharmony_civoid DistributedSchedService::NotifyContinuationCallbackResult(int32_t missionId, int32_t resultCode)
1396686862fbSopenharmony_ci{
1397686862fbSopenharmony_ci    HILOGD("Continuation result is: %{public}d", resultCode);
1398686862fbSopenharmony_ci
1399686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1400686862fbSopenharmony_ci        HILOGE("continuation object null!");
1401686862fbSopenharmony_ci        return;
1402686862fbSopenharmony_ci    }
1403686862fbSopenharmony_ci
1404686862fbSopenharmony_ci    int32_t result = 0;
1405686862fbSopenharmony_ci    if (dschedContinuation_->IsInContinuationProgress(missionId)) {
1406686862fbSopenharmony_ci        if (resultCode == ERR_OK && dschedContinuation_->IsCleanMission(missionId)) {
1407686862fbSopenharmony_ci            result = AbilityManagerClient::GetInstance()->CleanMission(missionId);
1408686862fbSopenharmony_ci            HILOGD("clean mission result:%{public}d", result);
1409686862fbSopenharmony_ci        }
1410686862fbSopenharmony_ci        result = dschedContinuation_->NotifyMissionCenterResult(missionId, resultCode);
1411686862fbSopenharmony_ci    } else {
1412686862fbSopenharmony_ci        result = AbilityManagerClient::GetInstance()->NotifyContinuationResult(missionId, resultCode);
1413686862fbSopenharmony_ci        dschedContinuation_->RemoveTimeOut(missionId);
1414686862fbSopenharmony_ci    }
1415686862fbSopenharmony_ci    HILOGD("NotifyContinuationCallbackResult result:%{public}d", result);
1416686862fbSopenharmony_ci}
1417686862fbSopenharmony_ci
1418686862fbSopenharmony_civoid DistributedSchedService::NotifyDSchedEventCallbackResult(int32_t resultCode)
1419686862fbSopenharmony_ci{
1420686862fbSopenharmony_ci    HILOGD("Continuation result is: %{public}d", resultCode);
1421686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1422686862fbSopenharmony_ci        HILOGE("continuation object null!");
1423686862fbSopenharmony_ci        return;
1424686862fbSopenharmony_ci    }
1425686862fbSopenharmony_ci    int dSchedEventresult = dschedContinuation_->NotifyDSchedEventResult(resultCode);
1426686862fbSopenharmony_ci    HILOGD("NotifyDSchedEventResult result:%{public}d", dSchedEventresult);
1427686862fbSopenharmony_ci}
1428686862fbSopenharmony_ci
1429686862fbSopenharmony_civoid DistributedSchedService::NotifyDSchedEventCallbackResult(int32_t resultCode, const EventNotify& event)
1430686862fbSopenharmony_ci{
1431686862fbSopenharmony_ci    HILOGD("Continuation result is: %{public}d", resultCode);
1432686862fbSopenharmony_ci    switch (event.dSchedEventType_) {
1433686862fbSopenharmony_ci        case DMS_CONTINUE:
1434686862fbSopenharmony_ci            NotifyContinuateEventResult(resultCode, event);
1435686862fbSopenharmony_ci            break;
1436686862fbSopenharmony_ci        case DMS_COLLABORATION:
1437686862fbSopenharmony_ci            NotifyCollaborateEventResult(resultCode, event);
1438686862fbSopenharmony_ci            break;
1439686862fbSopenharmony_ci        default:
1440686862fbSopenharmony_ci            HILOGE("Event does not carry specific operation type, eventType %{public}d.", event.dSchedEventType_);
1441686862fbSopenharmony_ci            return;
1442686862fbSopenharmony_ci    }
1443686862fbSopenharmony_ci}
1444686862fbSopenharmony_ci
1445686862fbSopenharmony_civoid DistributedSchedService::NotifyContinuateEventResult(int32_t resultCode, const EventNotify& event)
1446686862fbSopenharmony_ci{
1447686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
1448686862fbSopenharmony_ci        HILOGE("continuation object null!");
1449686862fbSopenharmony_ci        return;
1450686862fbSopenharmony_ci    }
1451686862fbSopenharmony_ci    dschedContinuation_->continueEvent_ = event;
1452686862fbSopenharmony_ci    int dSchedEventresult = dschedContinuation_->NotifyDSchedEventResult(resultCode);
1453686862fbSopenharmony_ci    HILOGD("NotifyDSchedEventResult result:%{public}d", dSchedEventresult);
1454686862fbSopenharmony_ci    if (event.state_ == DMS_DSCHED_EVENT_FINISH || resultCode != ERR_OK) {
1455686862fbSopenharmony_ci        dschedContinuation_->continueEvent_ = EventNotify();
1456686862fbSopenharmony_ci    }
1457686862fbSopenharmony_ci}
1458686862fbSopenharmony_ci
1459686862fbSopenharmony_civoid DistributedSchedService::NotifyCollaborateEventResult(int32_t resultCode, const EventNotify& event)
1460686862fbSopenharmony_ci{
1461686862fbSopenharmony_ci    if (collaborateCbMgr_ == nullptr) {
1462686862fbSopenharmony_ci        HILOGE("collaborate callback manager is null.");
1463686862fbSopenharmony_ci        return;
1464686862fbSopenharmony_ci    }
1465686862fbSopenharmony_ci    int32_t collaborateEventRet = collaborateCbMgr_->NotifyDSchedEventResult(resultCode, event);
1466686862fbSopenharmony_ci    HILOGD("NotifyDSchedEventResult result: %{public}d", collaborateEventRet);
1467686862fbSopenharmony_ci}
1468686862fbSopenharmony_ci
1469686862fbSopenharmony_civoid DistributedSchedService::NotifyCollaborateEventWithSessions(
1470686862fbSopenharmony_ci    const std::list<ConnectAbilitySession> &sessionsList, DSchedEventState state, int32_t ret)
1471686862fbSopenharmony_ci{
1472686862fbSopenharmony_ci    for (const auto &session : sessionsList) {
1473686862fbSopenharmony_ci        for (const auto &element : session.GetElementsList()) {
1474686862fbSopenharmony_ci            EventNotify tempEvent;
1475686862fbSopenharmony_ci            GetCurSrcCollaborateEvent(session.GetCallerInfo(), element, state, ret, tempEvent);
1476686862fbSopenharmony_ci            NotifyDSchedEventCallbackResult(ret, tempEvent);
1477686862fbSopenharmony_ci        }
1478686862fbSopenharmony_ci    }
1479686862fbSopenharmony_ci}
1480686862fbSopenharmony_ci
1481686862fbSopenharmony_civoid DistributedSchedService::GetCurSrcCollaborateEvent(const CallerInfo &callerInfo,
1482686862fbSopenharmony_ci    const AppExecFwk::ElementName &element, DSchedEventState state, int32_t ret, EventNotify &event)
1483686862fbSopenharmony_ci{
1484686862fbSopenharmony_ci    std::string callingBundleName;
1485686862fbSopenharmony_ci    if (!BundleManagerInternal::GetSpecifyBundleNameFromBms(callerInfo.uid, callingBundleName)) {
1486686862fbSopenharmony_ci        HILOGE("Get specify bundle name for from Bms fail, connect session caller uid %{public}d.", callerInfo.uid);
1487686862fbSopenharmony_ci    }
1488686862fbSopenharmony_ci
1489686862fbSopenharmony_ci    event.eventResult_ = ret;
1490686862fbSopenharmony_ci    event.srcNetworkId_ = callerInfo.sourceDeviceId;
1491686862fbSopenharmony_ci    event.dstNetworkId_ = element.GetDeviceID();
1492686862fbSopenharmony_ci    event.srcBundleName_ = callingBundleName;
1493686862fbSopenharmony_ci    event.srcModuleName_ = "";
1494686862fbSopenharmony_ci    event.srcAbilityName_ = "";
1495686862fbSopenharmony_ci    event.destBundleName_ = element.GetBundleName();
1496686862fbSopenharmony_ci    event.destModuleName_ = element.GetModuleName();
1497686862fbSopenharmony_ci    event.destAbilityName_ = element.GetAbilityName();
1498686862fbSopenharmony_ci    event.dSchedEventType_ = DMS_COLLABORATION;
1499686862fbSopenharmony_ci    event.state_ = state;
1500686862fbSopenharmony_ci}
1501686862fbSopenharmony_ci
1502686862fbSopenharmony_civoid DistributedSchedService::GetCurDestCollaborateEvent(const CallerInfo &callerInfo,
1503686862fbSopenharmony_ci    const AppExecFwk::ElementName &element, DSchedEventState state, int32_t ret, EventNotify &event)
1504686862fbSopenharmony_ci{
1505686862fbSopenharmony_ci    std::string callerBundleName;
1506686862fbSopenharmony_ci    if (callerInfo.extraInfoJson.find(DMS_UID_SPEC_BUNDLE_NAME) != callerInfo.extraInfoJson.end() &&
1507686862fbSopenharmony_ci        callerInfo.extraInfoJson[DMS_UID_SPEC_BUNDLE_NAME].is_string()) {
1508686862fbSopenharmony_ci        callerBundleName = callerInfo.extraInfoJson[DMS_UID_SPEC_BUNDLE_NAME];
1509686862fbSopenharmony_ci    }
1510686862fbSopenharmony_ci
1511686862fbSopenharmony_ci    event.eventResult_ = ret;
1512686862fbSopenharmony_ci    event.srcNetworkId_ = callerInfo.sourceDeviceId;
1513686862fbSopenharmony_ci    event.dstNetworkId_ = element.GetDeviceID();
1514686862fbSopenharmony_ci    event.srcBundleName_ = callerBundleName;
1515686862fbSopenharmony_ci    event.srcModuleName_ = "";
1516686862fbSopenharmony_ci    event.srcAbilityName_ = "";
1517686862fbSopenharmony_ci    event.destBundleName_ = element.GetBundleName();
1518686862fbSopenharmony_ci    event.destModuleName_ = element.GetModuleName();
1519686862fbSopenharmony_ci    event.destAbilityName_ = element.GetAbilityName();
1520686862fbSopenharmony_ci    event.dSchedEventType_ = DMS_COLLABORATION;
1521686862fbSopenharmony_ci    event.state_ = state;
1522686862fbSopenharmony_ci}
1523686862fbSopenharmony_ci
1524686862fbSopenharmony_civoid DistributedSchedService::RemoteConnectAbilityMappingLocked(const sptr<IRemoteObject>& connect,
1525686862fbSopenharmony_ci    const std::string& localDeviceId, const std::string& remoteDeviceId, const AppExecFwk::ElementName& element,
1526686862fbSopenharmony_ci    const CallerInfo& callerInfo, TargetComponent targetComponent)
1527686862fbSopenharmony_ci{
1528686862fbSopenharmony_ci    if (connect == nullptr) {
1529686862fbSopenharmony_ci        return;
1530686862fbSopenharmony_ci    }
1531686862fbSopenharmony_ci    auto itConnect = distributedConnectAbilityMap_.find(connect);
1532686862fbSopenharmony_ci    if (itConnect == distributedConnectAbilityMap_.end()) {
1533686862fbSopenharmony_ci        // add uid's connect number
1534686862fbSopenharmony_ci        uint32_t number = ++trackingUidMap_[callerInfo.uid];
1535686862fbSopenharmony_ci        HILOGD("uid %{public}d has %{public}u connection(s), targetComponent: %{public}d.",
1536686862fbSopenharmony_ci            callerInfo.uid, number, targetComponent);
1537686862fbSopenharmony_ci        // new connect, add death recipient
1538686862fbSopenharmony_ci        connect->AddDeathRecipient(connectDeathRecipient_);
1539686862fbSopenharmony_ci        ReportDistributedComponentChange(callerInfo, DISTRIBUTED_COMPONENT_ADD, IDistributedSched::CONNECT,
1540686862fbSopenharmony_ci            IDistributedSched::CALLER);
1541686862fbSopenharmony_ci    }
1542686862fbSopenharmony_ci    auto& sessionsList = distributedConnectAbilityMap_[connect];
1543686862fbSopenharmony_ci    for (auto& session : sessionsList) {
1544686862fbSopenharmony_ci        if (remoteDeviceId == session.GetDestinationDeviceId()) {
1545686862fbSopenharmony_ci            session.AddElement(element);
1546686862fbSopenharmony_ci            // already added session for remote device
1547686862fbSopenharmony_ci            return;
1548686862fbSopenharmony_ci        }
1549686862fbSopenharmony_ci    }
1550686862fbSopenharmony_ci    // connect to another remote device, add a new session to list
1551686862fbSopenharmony_ci    auto& session = sessionsList.emplace_back(localDeviceId, remoteDeviceId, callerInfo, targetComponent);
1552686862fbSopenharmony_ci    session.AddElement(element);
1553686862fbSopenharmony_ci    HILOGD("add connection success");
1554686862fbSopenharmony_ci}
1555686862fbSopenharmony_ci
1556686862fbSopenharmony_ciint32_t DistributedSchedService::CheckDistributedConnectLocked(const CallerInfo& callerInfo) const
1557686862fbSopenharmony_ci{
1558686862fbSopenharmony_ci    if (callerInfo.uid < 0) {
1559686862fbSopenharmony_ci        HILOGE("uid %{public}d is invalid.", callerInfo.uid);
1560686862fbSopenharmony_ci        return BIND_ABILITY_UID_INVALID_ERR;
1561686862fbSopenharmony_ci    }
1562686862fbSopenharmony_ci    auto it = trackingUidMap_.find(callerInfo.uid);
1563686862fbSopenharmony_ci    if (it != trackingUidMap_.end() && it->second >= MAX_DISTRIBUTED_CONNECT_NUM) {
1564686862fbSopenharmony_ci        HILOGE("uid %{public}d connected too much abilities, it maybe leak.", callerInfo.uid);
1565686862fbSopenharmony_ci        return BIND_ABILITY_LEAK_ERR;
1566686862fbSopenharmony_ci    }
1567686862fbSopenharmony_ci    return ERR_OK;
1568686862fbSopenharmony_ci}
1569686862fbSopenharmony_ci
1570686862fbSopenharmony_civoid DistributedSchedService::DecreaseConnectLocked(int32_t uid)
1571686862fbSopenharmony_ci{
1572686862fbSopenharmony_ci    if (uid < 0) {
1573686862fbSopenharmony_ci        HILOGE("DecreaseConnectLocked invalid uid %{public}d", uid);
1574686862fbSopenharmony_ci        return;
1575686862fbSopenharmony_ci    }
1576686862fbSopenharmony_ci    auto it = trackingUidMap_.find(uid);
1577686862fbSopenharmony_ci    if (it != trackingUidMap_.end()) {
1578686862fbSopenharmony_ci        auto& conns = it->second;
1579686862fbSopenharmony_ci        if (conns > 0) {
1580686862fbSopenharmony_ci            conns--;
1581686862fbSopenharmony_ci        }
1582686862fbSopenharmony_ci        if (conns == 0) {
1583686862fbSopenharmony_ci            HILOGD("DecreaseConnectLocked uid %{public}d connection(s) is 0", uid);
1584686862fbSopenharmony_ci            trackingUidMap_.erase(it);
1585686862fbSopenharmony_ci        }
1586686862fbSopenharmony_ci    }
1587686862fbSopenharmony_ci}
1588686862fbSopenharmony_ci
1589686862fbSopenharmony_ciint32_t DistributedSchedService::GetUidLocked(const std::list<ConnectAbilitySession>& sessionsList)
1590686862fbSopenharmony_ci{
1591686862fbSopenharmony_ci    if (!sessionsList.empty()) {
1592686862fbSopenharmony_ci        return sessionsList.front().GetCallerInfo().uid;
1593686862fbSopenharmony_ci    }
1594686862fbSopenharmony_ci    return INVALID_CALLER_UID;
1595686862fbSopenharmony_ci}
1596686862fbSopenharmony_ci
1597686862fbSopenharmony_ciint32_t DistributedSchedService::ConnectRemoteAbility(const OHOS::AAFwk::Want& want,
1598686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, int32_t callerUid, int32_t callerPid, uint32_t accessToken)
1599686862fbSopenharmony_ci{
1600686862fbSopenharmony_ci    std::string localDeviceId;
1601686862fbSopenharmony_ci    std::string remoteDeviceId = want.GetElement().GetDeviceID();
1602686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || !CheckDeviceId(localDeviceId, remoteDeviceId)) {
1603686862fbSopenharmony_ci        HILOGE("ConnectRemoteAbility check deviceId failed");
1604686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1605686862fbSopenharmony_ci    }
1606686862fbSopenharmony_ci
1607686862fbSopenharmony_ci#ifdef DMSFWK_INTERACTIVE_ADAPTER
1608686862fbSopenharmony_ci    if (CheckRemoteOsType(remoteDeviceId)) {
1609686862fbSopenharmony_ci        return ConnectRemoteAbilityAdapter(want, connect, callerUid, callerPid, accessToken);
1610686862fbSopenharmony_ci    }
1611686862fbSopenharmony_ci#endif // DMSFWK_INTERACTIVE_ADAPTER
1612686862fbSopenharmony_ci
1613686862fbSopenharmony_ci    CallerInfo callerInfo = { callerUid, callerPid, CALLER_TYPE_HARMONY, localDeviceId };
1614686862fbSopenharmony_ci    callerInfo.accessToken = accessToken;
1615686862fbSopenharmony_ci    {
1616686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(distributedLock_);
1617686862fbSopenharmony_ci        int32_t checkResult = CheckDistributedConnectLocked(callerInfo);
1618686862fbSopenharmony_ci        if (checkResult != ERR_OK) {
1619686862fbSopenharmony_ci            return checkResult;
1620686862fbSopenharmony_ci        }
1621686862fbSopenharmony_ci    }
1622686862fbSopenharmony_ci    if (!BundleManagerInternal::GetCallerAppIdFromBms(callerInfo.uid, callerInfo.callerAppId)) {
1623686862fbSopenharmony_ci        HILOGE("GetCallerAppIdFromBms failed");
1624686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1625686862fbSopenharmony_ci    }
1626686862fbSopenharmony_ci    if (!BundleManagerInternal::GetBundleNameListFromBms(callerInfo.uid, callerInfo.bundleNames)) {
1627686862fbSopenharmony_ci        HILOGE("GetBundleNameListFromBms failed");
1628686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1629686862fbSopenharmony_ci    }
1630686862fbSopenharmony_ci    callerInfo.extraInfoJson[DMS_VERSION_ID] = DMS_VERSION;
1631686862fbSopenharmony_ci    HILOGD("[PerformanceTest] ConnectRemoteAbility begin");
1632686862fbSopenharmony_ci    int32_t result = TryConnectRemoteAbility(want, connect, callerInfo);
1633686862fbSopenharmony_ci    if (result != ERR_OK) {
1634686862fbSopenharmony_ci        HILOGE("ConnectRemoteAbility result is %{public}d", result);
1635686862fbSopenharmony_ci    }
1636686862fbSopenharmony_ci    HILOGD("[PerformanceTest] ConnectRemoteAbility end");
1637686862fbSopenharmony_ci    return result;
1638686862fbSopenharmony_ci}
1639686862fbSopenharmony_ci
1640686862fbSopenharmony_ciint32_t DistributedSchedService::TryConnectRemoteAbility(const OHOS::AAFwk::Want& want,
1641686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo)
1642686862fbSopenharmony_ci{
1643686862fbSopenharmony_ci    AppExecFwk::AbilityInfo abilityInfo;
1644686862fbSopenharmony_ci    AccountInfo accountInfo;
1645686862fbSopenharmony_ci    std::string remoteDeviceId = want.GetElement().GetDeviceID();
1646686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(remoteDeviceId);
1647686862fbSopenharmony_ci    if (remoteDms == nullptr || connect == nullptr) {
1648686862fbSopenharmony_ci        HILOGE("TryConnectRemoteAbility invalid parameters");
1649686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1650686862fbSopenharmony_ci    }
1651686862fbSopenharmony_ci    int32_t ret = DistributedSchedPermission::GetInstance().GetAccountInfo(remoteDeviceId, callerInfo, accountInfo);
1652686862fbSopenharmony_ci    if (ret != ERR_OK) {
1653686862fbSopenharmony_ci        HILOGE("GetAccountInfo failed");
1654686862fbSopenharmony_ci        return ret;
1655686862fbSopenharmony_ci    }
1656686862fbSopenharmony_ci    int32_t retryTimes = BIND_CONNECT_RETRY_TIMES;
1657686862fbSopenharmony_ci    int32_t result = REMOTE_DEVICE_BIND_ABILITY_ERR;
1658686862fbSopenharmony_ci    while (retryTimes--) {
1659686862fbSopenharmony_ci        int64_t start = GetTickCount();
1660686862fbSopenharmony_ci        HILOGD("[PerformanceTest] ConnectRemoteAbility begin");
1661686862fbSopenharmony_ci        result = remoteDms->ConnectAbilityFromRemote(want, abilityInfo, connect, callerInfo, accountInfo);
1662686862fbSopenharmony_ci        HILOGD("[PerformanceTest] ConnectRemoteAbility end");
1663686862fbSopenharmony_ci        if (result == ERR_OK) {
1664686862fbSopenharmony_ci            std::lock_guard<std::mutex> autoLock(distributedLock_);
1665686862fbSopenharmony_ci            RemoteConnectAbilityMappingLocked(connect, callerInfo.sourceDeviceId, remoteDeviceId,
1666686862fbSopenharmony_ci                want.GetElement(), callerInfo, TargetComponent::HARMONY_COMPONENT);
1667686862fbSopenharmony_ci            break;
1668686862fbSopenharmony_ci        }
1669686862fbSopenharmony_ci        if (result == INVALID_REMOTE_PARAMETERS_ERR || result == REMOTE_DEVICE_BIND_ABILITY_ERR) {
1670686862fbSopenharmony_ci            break;
1671686862fbSopenharmony_ci        }
1672686862fbSopenharmony_ci        int64_t elapsedTime = GetTickCount() - start;
1673686862fbSopenharmony_ci        if (elapsedTime > BIND_CONNECT_TIMEOUT) {
1674686862fbSopenharmony_ci            HILOGW("ConnectRemoteAbility timeout, elapsedTime is %{public}" PRId64 " ms", elapsedTime);
1675686862fbSopenharmony_ci            break;
1676686862fbSopenharmony_ci        }
1677686862fbSopenharmony_ci    }
1678686862fbSopenharmony_ci    return result;
1679686862fbSopenharmony_ci}
1680686862fbSopenharmony_ci
1681686862fbSopenharmony_civoid DistributedSchedService::ProcessCallerDied(const sptr<IRemoteObject>& connect, int32_t deviceType)
1682686862fbSopenharmony_ci{
1683686862fbSopenharmony_ci    if (connect == nullptr) {
1684686862fbSopenharmony_ci        HILOGE("ProcessCallerDied connect is null");
1685686862fbSopenharmony_ci        return;
1686686862fbSopenharmony_ci    }
1687686862fbSopenharmony_ci    HILOGI("Caller Died DeviceType : %{public}d", deviceType);
1688686862fbSopenharmony_ci    if (deviceType == IDistributedSched::CALLER) {
1689686862fbSopenharmony_ci        HandleLocalCallerDied(connect);
1690686862fbSopenharmony_ci        return;
1691686862fbSopenharmony_ci    }
1692686862fbSopenharmony_ci    sptr<IRemoteObject> callbackWrapper = connect;
1693686862fbSopenharmony_ci    CallerInfo callerInfo;
1694686862fbSopenharmony_ci    AppExecFwk::ElementName element;
1695686862fbSopenharmony_ci    EventNotify tempEvent;
1696686862fbSopenharmony_ci    {
1697686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(calleeLock_);
1698686862fbSopenharmony_ci        auto itConnect = calleeMap_.find(connect);
1699686862fbSopenharmony_ci        if (itConnect != calleeMap_.end()) {
1700686862fbSopenharmony_ci            callbackWrapper = itConnect->second.callbackWrapper;
1701686862fbSopenharmony_ci            element = itConnect->second.element;
1702686862fbSopenharmony_ci            callerInfo = itConnect->second.callerInfo;
1703686862fbSopenharmony_ci            ReportDistributedComponentChange(itConnect->second, DISTRIBUTED_COMPONENT_REMOVE,
1704686862fbSopenharmony_ci                IDistributedSched::CALL, IDistributedSched::CALLEE);
1705686862fbSopenharmony_ci
1706686862fbSopenharmony_ci            GetCurDestCollaborateEvent(callerInfo, element, DMS_DSCHED_EVENT_STOP, ERR_OK, tempEvent);
1707686862fbSopenharmony_ci            NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
1708686862fbSopenharmony_ci            calleeMap_.erase(itConnect);
1709686862fbSopenharmony_ci        } else {
1710686862fbSopenharmony_ci            HILOGW("ProcessCallerDied connect not found");
1711686862fbSopenharmony_ci        }
1712686862fbSopenharmony_ci    }
1713686862fbSopenharmony_ci    UnregisterAppStateObserver(callbackWrapper);
1714686862fbSopenharmony_ci    int32_t result = DistributedSchedAdapter::GetInstance().ReleaseAbility(callbackWrapper, element);
1715686862fbSopenharmony_ci    if (result != ERR_OK) {
1716686862fbSopenharmony_ci        HILOGW("ProcessCallerDied failed, error: %{public}d", result);
1717686862fbSopenharmony_ci    }
1718686862fbSopenharmony_ci    GetCurDestCollaborateEvent(callerInfo, element, DMS_DSCHED_EVENT_FINISH, result, tempEvent);
1719686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(result, tempEvent);
1720686862fbSopenharmony_ci}
1721686862fbSopenharmony_ci
1722686862fbSopenharmony_civoid DistributedSchedService::HandleLocalCallerDied(const sptr<IRemoteObject>& connect)
1723686862fbSopenharmony_ci{
1724686862fbSopenharmony_ci    {
1725686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callerLock_);
1726686862fbSopenharmony_ci        auto it = callerMap_.find(connect);
1727686862fbSopenharmony_ci        if (it != callerMap_.end()) {
1728686862fbSopenharmony_ci            std::list<ConnectAbilitySession> sessionsList = it->second;
1729686862fbSopenharmony_ci            if (!sessionsList.empty()) {
1730686862fbSopenharmony_ci                ReportDistributedComponentChange(sessionsList.front().GetCallerInfo(), DISTRIBUTED_COMPONENT_REMOVE,
1731686862fbSopenharmony_ci                    IDistributedSched::CALL, IDistributedSched::CALLER);
1732686862fbSopenharmony_ci            }
1733686862fbSopenharmony_ci            NotifyCollaborateEventWithSessions(sessionsList, DMS_DSCHED_EVENT_FINISH, ERR_OK);
1734686862fbSopenharmony_ci            callerMap_.erase(it);
1735686862fbSopenharmony_ci            HILOGI("remove connection success");
1736686862fbSopenharmony_ci        } else {
1737686862fbSopenharmony_ci            HILOGW("HandleLocalCallerDied connect not found");
1738686862fbSopenharmony_ci        }
1739686862fbSopenharmony_ci    }
1740686862fbSopenharmony_ci    {
1741686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callLock_);
1742686862fbSopenharmony_ci        for (auto iter = callMap_.begin(); iter != callMap_.end(); iter++) {
1743686862fbSopenharmony_ci            if (iter->first == connect) {
1744686862fbSopenharmony_ci                callMap_.erase(iter);
1745686862fbSopenharmony_ci                HILOGI("remove callMap_ connect success");
1746686862fbSopenharmony_ci                break;
1747686862fbSopenharmony_ci            }
1748686862fbSopenharmony_ci        }
1749686862fbSopenharmony_ci    }
1750686862fbSopenharmony_ci}
1751686862fbSopenharmony_ci
1752686862fbSopenharmony_civoid DistributedSchedService::ProcessCalleeDied(const sptr<IRemoteObject>& connect)
1753686862fbSopenharmony_ci{
1754686862fbSopenharmony_ci    if (connect == nullptr) {
1755686862fbSopenharmony_ci        HILOGE("ProcessCalleeDied connect is null");
1756686862fbSopenharmony_ci        return;
1757686862fbSopenharmony_ci    }
1758686862fbSopenharmony_ci    sptr<IRemoteObject> callbackWrapper;
1759686862fbSopenharmony_ci    CallerInfo callerInfo;
1760686862fbSopenharmony_ci    AppExecFwk::ElementName element;
1761686862fbSopenharmony_ci    EventNotify tempEvent;
1762686862fbSopenharmony_ci    {
1763686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(calleeLock_);
1764686862fbSopenharmony_ci        auto itConnect = calleeMap_.find(connect);
1765686862fbSopenharmony_ci        if (itConnect != calleeMap_.end()) {
1766686862fbSopenharmony_ci            ReportDistributedComponentChange(itConnect->second, DISTRIBUTED_COMPONENT_REMOVE,
1767686862fbSopenharmony_ci                IDistributedSched::CALL, IDistributedSched::CALLEE);
1768686862fbSopenharmony_ci            callbackWrapper = itConnect->second.callbackWrapper;
1769686862fbSopenharmony_ci            callerInfo = itConnect->second.callerInfo;
1770686862fbSopenharmony_ci            element = itConnect->second.element;
1771686862fbSopenharmony_ci            calleeMap_.erase(itConnect);
1772686862fbSopenharmony_ci        } else {
1773686862fbSopenharmony_ci            HILOGW("ProcessCalleeDied connect not found");
1774686862fbSopenharmony_ci            return;
1775686862fbSopenharmony_ci        }
1776686862fbSopenharmony_ci    }
1777686862fbSopenharmony_ci    GetCurDestCollaborateEvent(callerInfo, element, DMS_DSCHED_EVENT_STOP, ERR_OK, tempEvent);
1778686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
1779686862fbSopenharmony_ci
1780686862fbSopenharmony_ci    UnregisterAppStateObserver(callbackWrapper);
1781686862fbSopenharmony_ci    GetCurDestCollaborateEvent(callerInfo, element, DMS_DSCHED_EVENT_FINISH, ERR_OK, tempEvent);
1782686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
1783686862fbSopenharmony_ci}
1784686862fbSopenharmony_ci
1785686862fbSopenharmony_civoid DistributedSchedService::ProcessCallResult(const sptr<IRemoteObject>& calleeConnect,
1786686862fbSopenharmony_ci    const sptr<IRemoteObject>& callerConnect)
1787686862fbSopenharmony_ci{
1788686862fbSopenharmony_ci    sptr<IRemoteObject> token;
1789686862fbSopenharmony_ci    AbilityManagerClient::GetInstance()->GetAbilityTokenByCalleeObj(calleeConnect, token);
1790686862fbSopenharmony_ci    if (token == nullptr) {
1791686862fbSopenharmony_ci        return;
1792686862fbSopenharmony_ci    }
1793686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(observerLock_);
1794686862fbSopenharmony_ci    for (auto iter = observerMap_.begin(); iter != observerMap_.end(); iter++) {
1795686862fbSopenharmony_ci        if (iter->second.srcConnect == callerConnect) {
1796686862fbSopenharmony_ci            iter->second.token = token;
1797686862fbSopenharmony_ci            return;
1798686862fbSopenharmony_ci        }
1799686862fbSopenharmony_ci    }
1800686862fbSopenharmony_ci    HILOGE("observerMap can not find callerConnect");
1801686862fbSopenharmony_ci}
1802686862fbSopenharmony_ci
1803686862fbSopenharmony_ciint32_t DistributedSchedService::TryStartRemoteAbilityByCall(const OHOS::AAFwk::Want& want,
1804686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo)
1805686862fbSopenharmony_ci{
1806686862fbSopenharmony_ci    std::string remoteDeviceId = want.GetElement().GetDeviceID();
1807686862fbSopenharmony_ci    HILOGD("[PerformanceTest] TryStartRemoteAbilityByCall get remote DMS");
1808686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(remoteDeviceId);
1809686862fbSopenharmony_ci    if (remoteDms == nullptr) {
1810686862fbSopenharmony_ci        HILOGE("TryStartRemoteAbilityByCall get remote DMS failed, remoteDeviceId: %{public}s",
1811686862fbSopenharmony_ci            GetAnonymStr(remoteDeviceId).c_str());
1812686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1813686862fbSopenharmony_ci    }
1814686862fbSopenharmony_ci    HILOGD("[PerformanceTest] TryStartRemoteAbilityByCall RPC begin");
1815686862fbSopenharmony_ci    AccountInfo accountInfo;
1816686862fbSopenharmony_ci    int32_t ret = DistributedSchedPermission::GetInstance().GetAccountInfo(remoteDeviceId, callerInfo, accountInfo);
1817686862fbSopenharmony_ci    if (ret != ERR_OK) {
1818686862fbSopenharmony_ci        HILOGE("GetAccountInfo failed");
1819686862fbSopenharmony_ci        return ret;
1820686862fbSopenharmony_ci    }
1821686862fbSopenharmony_ci    AAFwk::Want remoteWant = want;
1822686862fbSopenharmony_ci    int32_t connectToken = SaveConnectToken(want, connect);
1823686862fbSopenharmony_ci    remoteWant.SetParam(DMS_CONNECT_TOKEN, connectToken);
1824686862fbSopenharmony_ci    HILOGD("connectToken is %{public}s", GetAnonymStr(std::to_string(connectToken)).c_str());
1825686862fbSopenharmony_ci    int32_t result = remoteDms->StartAbilityByCallFromRemote(remoteWant, connect, callerInfo, accountInfo);
1826686862fbSopenharmony_ci    HILOGD("[PerformanceTest] TryStartRemoteAbilityByCall RPC end");
1827686862fbSopenharmony_ci    if (result == ERR_OK) {
1828686862fbSopenharmony_ci        SaveCallerComponent(want, connect, callerInfo);
1829686862fbSopenharmony_ci    } else {
1830686862fbSopenharmony_ci        HILOGE("TryStartRemoteAbilityByCall failed, result : %{public}d", result);
1831686862fbSopenharmony_ci    }
1832686862fbSopenharmony_ci    return result;
1833686862fbSopenharmony_ci}
1834686862fbSopenharmony_ci
1835686862fbSopenharmony_civoid DistributedSchedService::SaveCallerComponent(const OHOS::AAFwk::Want& want,
1836686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo)
1837686862fbSopenharmony_ci{
1838686862fbSopenharmony_ci    if (connect == nullptr) {
1839686862fbSopenharmony_ci        HILOGW("connect is nullptr");
1840686862fbSopenharmony_ci        return;
1841686862fbSopenharmony_ci    }
1842686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(callerLock_);
1843686862fbSopenharmony_ci    auto itConnect = callerMap_.find(connect);
1844686862fbSopenharmony_ci    if (itConnect == callerMap_.end()) {
1845686862fbSopenharmony_ci        connect->AddDeathRecipient(callerDeathRecipientForLocalDevice_);
1846686862fbSopenharmony_ci        ReportDistributedComponentChange(callerInfo, DISTRIBUTED_COMPONENT_ADD, IDistributedSched::CALL,
1847686862fbSopenharmony_ci            IDistributedSched::CALLER);
1848686862fbSopenharmony_ci    }
1849686862fbSopenharmony_ci    auto& sessionsList = callerMap_[connect];
1850686862fbSopenharmony_ci    std::string remoteDeviceId = want.GetElement().GetDeviceID();
1851686862fbSopenharmony_ci    for (auto& session : sessionsList) {
1852686862fbSopenharmony_ci        if (remoteDeviceId == session.GetDestinationDeviceId()) {
1853686862fbSopenharmony_ci            session.AddElement(want.GetElement());
1854686862fbSopenharmony_ci            // already added session for remote device
1855686862fbSopenharmony_ci            return;
1856686862fbSopenharmony_ci        }
1857686862fbSopenharmony_ci    }
1858686862fbSopenharmony_ci    // connect to another remote device, add a new session to list
1859686862fbSopenharmony_ci    auto& session = sessionsList.emplace_back(callerInfo.sourceDeviceId, remoteDeviceId, callerInfo);
1860686862fbSopenharmony_ci    session.AddElement(want.GetElement());
1861686862fbSopenharmony_ci
1862686862fbSopenharmony_ci    HILOGD("add connection success");
1863686862fbSopenharmony_ci    EventNotify tempEvent;
1864686862fbSopenharmony_ci    GetCurSrcCollaborateEvent(callerInfo, want.GetElement(), DMS_DSCHED_EVENT_PROCESSING, ERR_OK, tempEvent);
1865686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
1866686862fbSopenharmony_ci}
1867686862fbSopenharmony_ci
1868686862fbSopenharmony_civoid DistributedSchedService::RemoveCallerComponent(const sptr<IRemoteObject>& connect)
1869686862fbSopenharmony_ci{
1870686862fbSopenharmony_ci    if (connect == nullptr) {
1871686862fbSopenharmony_ci        HILOGW("connect is nullptr");
1872686862fbSopenharmony_ci        return;
1873686862fbSopenharmony_ci    }
1874686862fbSopenharmony_ci    {
1875686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callerLock_);
1876686862fbSopenharmony_ci        auto it = callerMap_.find(connect);
1877686862fbSopenharmony_ci        if (it != callerMap_.end()) {
1878686862fbSopenharmony_ci            connect->RemoveDeathRecipient(callerDeathRecipientForLocalDevice_);
1879686862fbSopenharmony_ci            std::list<ConnectAbilitySession> sessionsList = it->second;
1880686862fbSopenharmony_ci            if (!sessionsList.empty()) {
1881686862fbSopenharmony_ci                ReportDistributedComponentChange(sessionsList.front().GetCallerInfo(), DISTRIBUTED_COMPONENT_REMOVE,
1882686862fbSopenharmony_ci                    IDistributedSched::CALL, IDistributedSched::CALLER);
1883686862fbSopenharmony_ci            }
1884686862fbSopenharmony_ci            NotifyCollaborateEventWithSessions(sessionsList, DMS_DSCHED_EVENT_FINISH, ERR_OK);
1885686862fbSopenharmony_ci            callerMap_.erase(it);
1886686862fbSopenharmony_ci            HILOGI("remove connection success");
1887686862fbSopenharmony_ci        } else {
1888686862fbSopenharmony_ci            HILOGW("RemoveCallerComponent connect not found");
1889686862fbSopenharmony_ci        }
1890686862fbSopenharmony_ci    }
1891686862fbSopenharmony_ci    {
1892686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callLock_);
1893686862fbSopenharmony_ci        for (auto iter = callMap_.begin(); iter != callMap_.end(); iter++) {
1894686862fbSopenharmony_ci            if (iter->first == connect) {
1895686862fbSopenharmony_ci                callMap_.erase(iter);
1896686862fbSopenharmony_ci                HILOGI("remove callMap_ connect success");
1897686862fbSopenharmony_ci                break;
1898686862fbSopenharmony_ci            }
1899686862fbSopenharmony_ci        }
1900686862fbSopenharmony_ci    }
1901686862fbSopenharmony_ci}
1902686862fbSopenharmony_ci
1903686862fbSopenharmony_civoid DistributedSchedService::ProcessCalleeOffline(const std::string& deviceId)
1904686862fbSopenharmony_ci{
1905686862fbSopenharmony_ci    {
1906686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callerLock_);
1907686862fbSopenharmony_ci        for (auto iter = callerMap_.begin(); iter != callerMap_.end();) {
1908686862fbSopenharmony_ci            std::list<ConnectAbilitySession>& sessionsList = iter->second;
1909686862fbSopenharmony_ci            auto itSession = std::find_if(sessionsList.begin(), sessionsList.end(), [&deviceId](const auto& session) {
1910686862fbSopenharmony_ci                return session.GetDestinationDeviceId() == deviceId;
1911686862fbSopenharmony_ci            });
1912686862fbSopenharmony_ci            CallerInfo callerInfo;
1913686862fbSopenharmony_ci            if (itSession != sessionsList.end()) {
1914686862fbSopenharmony_ci                callerInfo = itSession->GetCallerInfo();
1915686862fbSopenharmony_ci                for (const auto &element : itSession->GetElementsList()) {
1916686862fbSopenharmony_ci                    EventNotify tempEvent;
1917686862fbSopenharmony_ci                    GetCurSrcCollaborateEvent(callerInfo, element, DMS_DSCHED_EVENT_FINISH, ERR_OK, tempEvent);
1918686862fbSopenharmony_ci                    NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
1919686862fbSopenharmony_ci                }
1920686862fbSopenharmony_ci                sessionsList.erase(itSession);
1921686862fbSopenharmony_ci            }
1922686862fbSopenharmony_ci
1923686862fbSopenharmony_ci            if (sessionsList.empty()) {
1924686862fbSopenharmony_ci                if (iter->first != nullptr) {
1925686862fbSopenharmony_ci                    iter->first->RemoveDeathRecipient(callerDeathRecipientForLocalDevice_);
1926686862fbSopenharmony_ci                }
1927686862fbSopenharmony_ci                ReportDistributedComponentChange(callerInfo, DISTRIBUTED_COMPONENT_REMOVE,
1928686862fbSopenharmony_ci                    IDistributedSched::CALL, IDistributedSched::CALLER);
1929686862fbSopenharmony_ci                iter = callerMap_.erase(iter);
1930686862fbSopenharmony_ci            } else {
1931686862fbSopenharmony_ci                iter++;
1932686862fbSopenharmony_ci            }
1933686862fbSopenharmony_ci        }
1934686862fbSopenharmony_ci    }
1935686862fbSopenharmony_ci    {
1936686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callLock_);
1937686862fbSopenharmony_ci        for (auto iter = callMap_.begin(); iter != callMap_.end();) {
1938686862fbSopenharmony_ci            if (iter->second.remoteDeviceId == deviceId) {
1939686862fbSopenharmony_ci                iter = callMap_.erase(iter);
1940686862fbSopenharmony_ci                HILOGI("remove callMap_ connect success");
1941686862fbSopenharmony_ci            } else {
1942686862fbSopenharmony_ci                iter++;
1943686862fbSopenharmony_ci            }
1944686862fbSopenharmony_ci        }
1945686862fbSopenharmony_ci    }
1946686862fbSopenharmony_ci}
1947686862fbSopenharmony_ci
1948686862fbSopenharmony_ciint32_t DistributedSchedService::SaveConnectToken(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect)
1949686862fbSopenharmony_ci{
1950686862fbSopenharmony_ci    int32_t tToken = -1;
1951686862fbSopenharmony_ci    {
1952686862fbSopenharmony_ci        std::lock_guard<std::mutex> tokenLock(tokenMutex_);
1953686862fbSopenharmony_ci        tToken = token_.load();
1954686862fbSopenharmony_ci        if (++tToken > MAX_TOKEN_NUM) {
1955686862fbSopenharmony_ci            tToken = 1;
1956686862fbSopenharmony_ci        }
1957686862fbSopenharmony_ci        token_.store(tToken);
1958686862fbSopenharmony_ci    }
1959686862fbSopenharmony_ci    {
1960686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callLock_);
1961686862fbSopenharmony_ci        callMap_[connect] = {tToken, want.GetElement().GetDeviceID()};
1962686862fbSopenharmony_ci        HILOGI("add connect success");
1963686862fbSopenharmony_ci    }
1964686862fbSopenharmony_ci    return tToken;
1965686862fbSopenharmony_ci}
1966686862fbSopenharmony_ci
1967686862fbSopenharmony_ciint32_t DistributedSchedService::StartRemoteAbilityByCall(const OHOS::AAFwk::Want& want,
1968686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, int32_t callerUid, int32_t callerPid, uint32_t accessToken)
1969686862fbSopenharmony_ci{
1970686862fbSopenharmony_ci    if (connect == nullptr) {
1971686862fbSopenharmony_ci        HILOGE("StartRemoteAbilityByCall connect is null");
1972686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1973686862fbSopenharmony_ci    }
1974686862fbSopenharmony_ci    std::string localDeviceId;
1975686862fbSopenharmony_ci    std::string remoteDeviceId = want.GetElement().GetDeviceID();
1976686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || !CheckDeviceId(localDeviceId, remoteDeviceId)) {
1977686862fbSopenharmony_ci        HILOGE("StartRemoteAbilityByCall check deviceId failed");
1978686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1979686862fbSopenharmony_ci    }
1980686862fbSopenharmony_ci    CallerInfo callerInfo = { callerUid, callerPid };
1981686862fbSopenharmony_ci    callerInfo.sourceDeviceId = localDeviceId;
1982686862fbSopenharmony_ci    callerInfo.accessToken = accessToken;
1983686862fbSopenharmony_ci    if (!BundleManagerInternal::GetCallerAppIdFromBms(callerInfo.uid, callerInfo.callerAppId)) {
1984686862fbSopenharmony_ci        HILOGE("GetCallerAppIdFromBms failed");
1985686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1986686862fbSopenharmony_ci    }
1987686862fbSopenharmony_ci    if (!BundleManagerInternal::GetBundleNameListFromBms(callerInfo.uid, callerInfo.bundleNames)) {
1988686862fbSopenharmony_ci        HILOGE("GetBundleNameListFromBms failed");
1989686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
1990686862fbSopenharmony_ci    }
1991686862fbSopenharmony_ci    callerInfo.extraInfoJson[DMS_VERSION_ID] = DMS_VERSION;
1992686862fbSopenharmony_ci    std::string uidSpecBundleName;
1993686862fbSopenharmony_ci    if (!BundleManagerInternal::GetSpecifyBundleNameFromBms(callerInfo.uid, uidSpecBundleName)) {
1994686862fbSopenharmony_ci        HILOGE("Get specify bundle name for from Bms fail, connect session caller uid %{public}d.", callerInfo.uid);
1995686862fbSopenharmony_ci    }
1996686862fbSopenharmony_ci    callerInfo.extraInfoJson[DMS_UID_SPEC_BUNDLE_NAME] = uidSpecBundleName;
1997686862fbSopenharmony_ci
1998686862fbSopenharmony_ci    EventNotify tempEvent;
1999686862fbSopenharmony_ci    GetCurSrcCollaborateEvent(callerInfo, want.GetElement(), DMS_DSCHED_EVENT_START, ERR_OK, tempEvent);
2000686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
2001686862fbSopenharmony_ci
2002686862fbSopenharmony_ci    int32_t ret = TryStartRemoteAbilityByCall(want, connect, callerInfo);
2003686862fbSopenharmony_ci    if (ret != ERR_OK) {
2004686862fbSopenharmony_ci        {
2005686862fbSopenharmony_ci            std::lock_guard<std::mutex> autoLock(callLock_);
2006686862fbSopenharmony_ci            callMap_.erase(connect);
2007686862fbSopenharmony_ci        }
2008686862fbSopenharmony_ci        HILOGE("StartRemoteAbilityByCall result is %{public}d", ret);
2009686862fbSopenharmony_ci    }
2010686862fbSopenharmony_ci    return ret;
2011686862fbSopenharmony_ci}
2012686862fbSopenharmony_ci
2013686862fbSopenharmony_ciint32_t DistributedSchedService::ReleaseRemoteAbility(const sptr<IRemoteObject>& connect,
2014686862fbSopenharmony_ci    const AppExecFwk::ElementName &element)
2015686862fbSopenharmony_ci{
2016686862fbSopenharmony_ci    if (connect == nullptr) {
2017686862fbSopenharmony_ci        HILOGE("ReleaseRemoteAbility connect is null");
2018686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2019686862fbSopenharmony_ci    }
2020686862fbSopenharmony_ci    if (element.GetDeviceID().empty()) {
2021686862fbSopenharmony_ci        HILOGE("ReleaseRemoteAbility remote deviceId empty");
2022686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2023686862fbSopenharmony_ci    }
2024686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(element.GetDeviceID());
2025686862fbSopenharmony_ci    if (remoteDms == nullptr) {
2026686862fbSopenharmony_ci        HILOGE("ReleaseRemoteAbility get remote dms failed, devId: %{public}s",
2027686862fbSopenharmony_ci            GetAnonymStr(element.GetDeviceID()).c_str());
2028686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2029686862fbSopenharmony_ci    }
2030686862fbSopenharmony_ci    CallerInfo callerInfo;
2031686862fbSopenharmony_ci    if (!GetLocalDeviceId(callerInfo.sourceDeviceId)) {
2032686862fbSopenharmony_ci        HILOGE("ReleaseRemoteAbility get local deviceId failed");
2033686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2034686862fbSopenharmony_ci    }
2035686862fbSopenharmony_ci    int32_t result = remoteDms->ReleaseAbilityFromRemote(connect, element, callerInfo);
2036686862fbSopenharmony_ci    if (result == ERR_OK) {
2037686862fbSopenharmony_ci        RemoveCallerComponent(connect);
2038686862fbSopenharmony_ci    } else {
2039686862fbSopenharmony_ci        HILOGE("ReleaseRemoteAbility result is %{public}d", result);
2040686862fbSopenharmony_ci    }
2041686862fbSopenharmony_ci    return result;
2042686862fbSopenharmony_ci}
2043686862fbSopenharmony_ci
2044686862fbSopenharmony_ciint32_t DistributedSchedService::StartAbilityByCallFromRemote(const OHOS::AAFwk::Want& want,
2045686862fbSopenharmony_ci    const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo, const AccountInfo& accountInfo)
2046686862fbSopenharmony_ci{
2047686862fbSopenharmony_ci    HILOGD("[PerformanceTest] DistributedSchedService StartAbilityByCallFromRemote begin");
2048686862fbSopenharmony_ci    if (connect == nullptr) {
2049686862fbSopenharmony_ci        HILOGE("StartAbilityByCallFromRemote connect is null");
2050686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2051686862fbSopenharmony_ci    }
2052686862fbSopenharmony_ci
2053686862fbSopenharmony_ci    EventNotify tempEvent;
2054686862fbSopenharmony_ci    GetCurDestCollaborateEvent(callerInfo, want.GetElement(), DMS_DSCHED_EVENT_START, ERR_OK, tempEvent);
2055686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
2056686862fbSopenharmony_ci
2057686862fbSopenharmony_ci    std::string localDeviceId;
2058686862fbSopenharmony_ci    std::string destinationDeviceId = want.GetElement().GetDeviceID();
2059686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) ||
2060686862fbSopenharmony_ci        !CheckDeviceIdFromRemote(localDeviceId, destinationDeviceId, callerInfo.sourceDeviceId)) {
2061686862fbSopenharmony_ci        HILOGE("StartAbilityByCallFromRemote check deviceId failed");
2062686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2063686862fbSopenharmony_ci    }
2064686862fbSopenharmony_ci    int32_t result = CheckTargetPermission(want, callerInfo, accountInfo, CALL_PERMISSION, false);
2065686862fbSopenharmony_ci    if (result != ERR_OK) {
2066686862fbSopenharmony_ci        HILOGE("CheckTargetPermission failed!!");
2067686862fbSopenharmony_ci        return result;
2068686862fbSopenharmony_ci    }
2069686862fbSopenharmony_ci
2070686862fbSopenharmony_ci    sptr<IRemoteObject> callbackWrapper;
2071686862fbSopenharmony_ci    {
2072686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(calleeLock_);
2073686862fbSopenharmony_ci        auto itConnect = calleeMap_.find(connect);
2074686862fbSopenharmony_ci        if (itConnect != calleeMap_.end()) {
2075686862fbSopenharmony_ci            callbackWrapper = itConnect->second.callbackWrapper;
2076686862fbSopenharmony_ci        } else {
2077686862fbSopenharmony_ci            callbackWrapper = new AbilityConnectionWrapperStub(connect, localDeviceId);
2078686862fbSopenharmony_ci        }
2079686862fbSopenharmony_ci    }
2080686862fbSopenharmony_ci    int32_t errCode = DistributedSchedAdapter::GetInstance().StartAbilityByCall(want, callbackWrapper, this);
2081686862fbSopenharmony_ci    HILOGD("[PerformanceTest] StartAbilityByCallFromRemote end");
2082686862fbSopenharmony_ci    if (errCode == ERR_OK) {
2083686862fbSopenharmony_ci        {
2084686862fbSopenharmony_ci            std::lock_guard<std::mutex> autoLock(calleeLock_);
2085686862fbSopenharmony_ci            ConnectInfo connectInfo {callerInfo, callbackWrapper, want.GetElement()};
2086686862fbSopenharmony_ci            ReportDistributedComponentChange(connectInfo, DISTRIBUTED_COMPONENT_ADD,
2087686862fbSopenharmony_ci                IDistributedSched::CALL, IDistributedSched::CALLEE);
2088686862fbSopenharmony_ci            calleeMap_.emplace(connect, connectInfo);
2089686862fbSopenharmony_ci        }
2090686862fbSopenharmony_ci        connect->AddDeathRecipient(callerDeathRecipient_);
2091686862fbSopenharmony_ci        if (!RegisterAppStateObserver(want, callerInfo, connect, callbackWrapper)) {
2092686862fbSopenharmony_ci            HILOGE("RegisterAppStateObserver failed");
2093686862fbSopenharmony_ci        }
2094686862fbSopenharmony_ci    }
2095686862fbSopenharmony_ci
2096686862fbSopenharmony_ci    GetCurDestCollaborateEvent(callerInfo, want.GetElement(), DMS_DSCHED_EVENT_PROCESSING, ERR_OK, tempEvent);
2097686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
2098686862fbSopenharmony_ci    return errCode;
2099686862fbSopenharmony_ci}
2100686862fbSopenharmony_ci
2101686862fbSopenharmony_ciint32_t DistributedSchedService::ReleaseAbilityFromRemote(const sptr<IRemoteObject>& connect,
2102686862fbSopenharmony_ci    const AppExecFwk::ElementName &element, const CallerInfo& callerInfo)
2103686862fbSopenharmony_ci{
2104686862fbSopenharmony_ci    if (connect == nullptr) {
2105686862fbSopenharmony_ci        HILOGE("ReleaseAbilityFromRemote connect is null");
2106686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2107686862fbSopenharmony_ci    }
2108686862fbSopenharmony_ci    EventNotify tempEvent;
2109686862fbSopenharmony_ci    GetCurDestCollaborateEvent(callerInfo, element, DMS_DSCHED_EVENT_STOP, ERR_OK, tempEvent);
2110686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(ERR_OK, tempEvent);
2111686862fbSopenharmony_ci
2112686862fbSopenharmony_ci    HILOGD("[PerformanceTest] ReleaseAbilityFromRemote begin");
2113686862fbSopenharmony_ci    std::string localDeviceId;
2114686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || localDeviceId.empty() ||
2115686862fbSopenharmony_ci        callerInfo.sourceDeviceId.empty() || localDeviceId == callerInfo.sourceDeviceId) {
2116686862fbSopenharmony_ci        HILOGE("ReleaseAbilityFromRemote check deviceId failed");
2117686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2118686862fbSopenharmony_ci    }
2119686862fbSopenharmony_ci
2120686862fbSopenharmony_ci    sptr<IRemoteObject> callbackWrapper;
2121686862fbSopenharmony_ci    {
2122686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(calleeLock_);
2123686862fbSopenharmony_ci        auto itConnect = calleeMap_.find(connect);
2124686862fbSopenharmony_ci        if (itConnect == calleeMap_.end()) {
2125686862fbSopenharmony_ci            HILOGE("ReleaseAbilityFromRemote callee not found");
2126686862fbSopenharmony_ci            return INVALID_REMOTE_PARAMETERS_ERR;
2127686862fbSopenharmony_ci        }
2128686862fbSopenharmony_ci        callbackWrapper = itConnect->second.callbackWrapper;
2129686862fbSopenharmony_ci        ReportDistributedComponentChange(itConnect->second, DISTRIBUTED_COMPONENT_REMOVE,
2130686862fbSopenharmony_ci            IDistributedSched::CALL, IDistributedSched::CALLEE);
2131686862fbSopenharmony_ci        calleeMap_.erase(itConnect);
2132686862fbSopenharmony_ci        connect->RemoveDeathRecipient(callerDeathRecipient_);
2133686862fbSopenharmony_ci    }
2134686862fbSopenharmony_ci    UnregisterAppStateObserver(callbackWrapper);
2135686862fbSopenharmony_ci    int32_t result = DistributedSchedAdapter::GetInstance().ReleaseAbility(callbackWrapper, element);
2136686862fbSopenharmony_ci    HILOGD("[PerformanceTest] ReleaseAbilityFromRemote end");
2137686862fbSopenharmony_ci    if (result != ERR_OK) {
2138686862fbSopenharmony_ci        HILOGE("ReleaseAbilityFromRemote failed, error: %{public}d", result);
2139686862fbSopenharmony_ci    }
2140686862fbSopenharmony_ci    GetCurDestCollaborateEvent(callerInfo, element, DMS_DSCHED_EVENT_FINISH, result, tempEvent);
2141686862fbSopenharmony_ci    NotifyDSchedEventCallbackResult(result, tempEvent);
2142686862fbSopenharmony_ci    return result;
2143686862fbSopenharmony_ci}
2144686862fbSopenharmony_ci
2145686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_FORM_SHARE
2146686862fbSopenharmony_ciint32_t DistributedSchedService::StartRemoteShareForm(const std::string& remoteDeviceId,
2147686862fbSopenharmony_ci    const OHOS::AppExecFwk::FormShareInfo& formShareInfo)
2148686862fbSopenharmony_ci{
2149686862fbSopenharmony_ci    HILOGD("SHAREFORM:: func call");
2150686862fbSopenharmony_ci
2151686862fbSopenharmony_ci    if (remoteDeviceId.empty()) {
2152686862fbSopenharmony_ci        HILOGE("StartRemoteShareForm input params error");
2153686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2154686862fbSopenharmony_ci    }
2155686862fbSopenharmony_ci
2156686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(remoteDeviceId);
2157686862fbSopenharmony_ci    if (remoteDms == nullptr) {
2158686862fbSopenharmony_ci        HILOGE("StartRemoteShareForm get remote DMS failed, remoteDeviceId: %{public}s",
2159686862fbSopenharmony_ci            GetAnonymStr(remoteDeviceId).c_str());
2160686862fbSopenharmony_ci        return GET_REMOTE_DMS_FAIL;
2161686862fbSopenharmony_ci    }
2162686862fbSopenharmony_ci    std::string localDeviceId = "";
2163686862fbSopenharmony_ci    GetLocalDeviceId(localDeviceId);
2164686862fbSopenharmony_ci    OHOS::AppExecFwk::FormShareInfo formShareInfoCopy;
2165686862fbSopenharmony_ci    formShareInfoCopy.formId = formShareInfo.formId;
2166686862fbSopenharmony_ci    formShareInfoCopy.formName = formShareInfo.formName;
2167686862fbSopenharmony_ci    formShareInfoCopy.bundleName = formShareInfo.bundleName;
2168686862fbSopenharmony_ci    formShareInfoCopy.moduleName = formShareInfo.moduleName;
2169686862fbSopenharmony_ci    formShareInfoCopy.abilityName = formShareInfo.abilityName;
2170686862fbSopenharmony_ci    formShareInfoCopy.formTempFlag = formShareInfo.formTempFlag;
2171686862fbSopenharmony_ci    formShareInfoCopy.dimensionId = formShareInfo.dimensionId;
2172686862fbSopenharmony_ci    formShareInfoCopy.providerShareData = formShareInfo.providerShareData;
2173686862fbSopenharmony_ci    formShareInfoCopy.deviceId = localDeviceId;
2174686862fbSopenharmony_ci    int32_t result = remoteDms->StartShareFormFromRemote(remoteDeviceId, formShareInfoCopy);
2175686862fbSopenharmony_ci    HILOGD("[PerformanceTest] StartRemoteShareForm RPC end");
2176686862fbSopenharmony_ci    if (result != ERR_OK) {
2177686862fbSopenharmony_ci        HILOGE("StartRemoteShareForm failed, result : %{public}d", result);
2178686862fbSopenharmony_ci    }
2179686862fbSopenharmony_ci    return result;
2180686862fbSopenharmony_ci}
2181686862fbSopenharmony_ci
2182686862fbSopenharmony_ciint32_t DistributedSchedService::StartShareFormFromRemote(
2183686862fbSopenharmony_ci    const std::string& remoteDeviceId, const OHOS::AppExecFwk::FormShareInfo& formShareInfo)
2184686862fbSopenharmony_ci{
2185686862fbSopenharmony_ci    HILOGD("SHAREFORM:: func call begin");
2186686862fbSopenharmony_ci    std::string localDeviceId = "";
2187686862fbSopenharmony_ci    GetLocalDeviceId(localDeviceId);
2188686862fbSopenharmony_ci    if (CheckDeviceId(localDeviceId, remoteDeviceId)) {
2189686862fbSopenharmony_ci        HILOGE("localId is %{public}s != %{public}s", GetAnonymStr(localDeviceId).c_str(),
2190686862fbSopenharmony_ci            GetAnonymStr(remoteDeviceId).c_str());
2191686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2192686862fbSopenharmony_ci    }
2193686862fbSopenharmony_ci
2194686862fbSopenharmony_ci    auto formMgr = GetFormMgrProxy();
2195686862fbSopenharmony_ci    if (formMgr == nullptr) {
2196686862fbSopenharmony_ci        HILOGE("get form mgr proxy failed.");
2197686862fbSopenharmony_ci        return NOT_FIND_SERVICE_PROXY;
2198686862fbSopenharmony_ci    }
2199686862fbSopenharmony_ci
2200686862fbSopenharmony_ci    auto result = formMgr->RecvFormShareInfoFromRemote(formShareInfo);
2201686862fbSopenharmony_ci    HILOGD("SHAREFORM:: func call end");
2202686862fbSopenharmony_ci    return result;
2203686862fbSopenharmony_ci}
2204686862fbSopenharmony_ci#endif
2205686862fbSopenharmony_ci
2206686862fbSopenharmony_ciint32_t DistributedSchedService::GetDistributedComponentList(std::vector<std::string>& distributedComponents)
2207686862fbSopenharmony_ci{
2208686862fbSopenharmony_ci    GetConnectComponentList(distributedComponents);
2209686862fbSopenharmony_ci    GetCallComponentList(distributedComponents);
2210686862fbSopenharmony_ci    return ERR_OK;
2211686862fbSopenharmony_ci}
2212686862fbSopenharmony_ci
2213686862fbSopenharmony_civoid DistributedSchedService::GetConnectComponentList(std::vector<std::string>& distributedComponents)
2214686862fbSopenharmony_ci{
2215686862fbSopenharmony_ci    {
2216686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(distributedLock_);
2217686862fbSopenharmony_ci        for (const auto& iter : distributedConnectAbilityMap_) {
2218686862fbSopenharmony_ci            if (iter.second.empty()) {
2219686862fbSopenharmony_ci                continue;
2220686862fbSopenharmony_ci            }
2221686862fbSopenharmony_ci            CallerInfo callerInfo = iter.second.front().GetCallerInfo();
2222686862fbSopenharmony_ci            nlohmann::json componentInfoJson;
2223686862fbSopenharmony_ci            componentInfoJson[PID_KEY] = callerInfo.pid;
2224686862fbSopenharmony_ci            componentInfoJson[UID_KEY] = callerInfo.uid;
2225686862fbSopenharmony_ci            componentInfoJson[BUNDLE_NAME_KEY] =
2226686862fbSopenharmony_ci                callerInfo.bundleNames.empty() ? std::string() : callerInfo.bundleNames.front();
2227686862fbSopenharmony_ci            componentInfoJson[COMPONENT_TYPE_KEY] = IDistributedSched::CONNECT;
2228686862fbSopenharmony_ci            componentInfoJson[DEVICE_TYPE_KEY] = IDistributedSched::CALLER;
2229686862fbSopenharmony_ci            std::string componentInfo = componentInfoJson.dump();
2230686862fbSopenharmony_ci            distributedComponents.emplace_back(componentInfo);
2231686862fbSopenharmony_ci        }
2232686862fbSopenharmony_ci    }
2233686862fbSopenharmony_ci    {
2234686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(connectLock_);
2235686862fbSopenharmony_ci        for (const auto& iter : connectAbilityMap_) {
2236686862fbSopenharmony_ci            ConnectInfo connectInfo = iter.second;
2237686862fbSopenharmony_ci            nlohmann::json componentInfoJson;
2238686862fbSopenharmony_ci            componentInfoJson[UID_KEY] = BundleManagerInternal::GetUidFromBms(connectInfo.element.GetBundleName());
2239686862fbSopenharmony_ci            componentInfoJson[BUNDLE_NAME_KEY] = connectInfo.element.GetBundleName();
2240686862fbSopenharmony_ci            componentInfoJson[COMPONENT_TYPE_KEY] = IDistributedSched::CONNECT;
2241686862fbSopenharmony_ci            componentInfoJson[DEVICE_TYPE_KEY] = IDistributedSched::CALLEE;
2242686862fbSopenharmony_ci            std::string componentInfo = componentInfoJson.dump();
2243686862fbSopenharmony_ci            distributedComponents.emplace_back(componentInfo);
2244686862fbSopenharmony_ci        }
2245686862fbSopenharmony_ci    }
2246686862fbSopenharmony_ci}
2247686862fbSopenharmony_ci
2248686862fbSopenharmony_civoid DistributedSchedService::GetCallComponentList(std::vector<std::string>& distributedComponents)
2249686862fbSopenharmony_ci{
2250686862fbSopenharmony_ci    {
2251686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callerLock_);
2252686862fbSopenharmony_ci        for (const auto& iter : callerMap_) {
2253686862fbSopenharmony_ci            if (iter.second.empty()) {
2254686862fbSopenharmony_ci                continue;
2255686862fbSopenharmony_ci            }
2256686862fbSopenharmony_ci            CallerInfo callerInfo = iter.second.front().GetCallerInfo();
2257686862fbSopenharmony_ci            nlohmann::json componentInfoJson;
2258686862fbSopenharmony_ci            componentInfoJson[PID_KEY] = callerInfo.pid;
2259686862fbSopenharmony_ci            componentInfoJson[UID_KEY] = callerInfo.uid;
2260686862fbSopenharmony_ci            componentInfoJson[BUNDLE_NAME_KEY] =
2261686862fbSopenharmony_ci                callerInfo.bundleNames.empty() ? std::string() : callerInfo.bundleNames.front();
2262686862fbSopenharmony_ci            componentInfoJson[COMPONENT_TYPE_KEY] = IDistributedSched::CALL;
2263686862fbSopenharmony_ci            componentInfoJson[DEVICE_TYPE_KEY] = IDistributedSched::CALLER;
2264686862fbSopenharmony_ci            std::string componentInfo = componentInfoJson.dump();
2265686862fbSopenharmony_ci            distributedComponents.emplace_back(componentInfo);
2266686862fbSopenharmony_ci        }
2267686862fbSopenharmony_ci    }
2268686862fbSopenharmony_ci    {
2269686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(calleeLock_);
2270686862fbSopenharmony_ci        for (const auto& iter : calleeMap_) {
2271686862fbSopenharmony_ci            ConnectInfo connectInfo = iter.second;
2272686862fbSopenharmony_ci            nlohmann::json componentInfoJson;
2273686862fbSopenharmony_ci            componentInfoJson[UID_KEY] = BundleManagerInternal::GetUidFromBms(connectInfo.element.GetBundleName());
2274686862fbSopenharmony_ci            componentInfoJson[BUNDLE_NAME_KEY] = connectInfo.element.GetBundleName();
2275686862fbSopenharmony_ci            componentInfoJson[COMPONENT_TYPE_KEY] = IDistributedSched::CALL;
2276686862fbSopenharmony_ci            componentInfoJson[DEVICE_TYPE_KEY] = IDistributedSched::CALLEE;
2277686862fbSopenharmony_ci            std::string componentInfo = componentInfoJson.dump();
2278686862fbSopenharmony_ci            distributedComponents.emplace_back(componentInfo);
2279686862fbSopenharmony_ci        }
2280686862fbSopenharmony_ci    }
2281686862fbSopenharmony_ci}
2282686862fbSopenharmony_ci
2283686862fbSopenharmony_civoid DistributedSchedService::ReportDistributedComponentChange(const CallerInfo& callerInfo, int32_t changeType,
2284686862fbSopenharmony_ci    int32_t componentType, int32_t deviceType)
2285686862fbSopenharmony_ci{
2286686862fbSopenharmony_ci#if defined(EFFICIENCY_MANAGER_ENABLE) || defined(SUPPORT_DISTRIBUTEDCOMPONENT_TO_MEMMGR)
2287686862fbSopenharmony_ci    HILOGI("caller report");
2288686862fbSopenharmony_ci    auto func = [this, callerInfo, changeType, componentType, deviceType]() {
2289686862fbSopenharmony_ci#ifdef EFFICIENCY_MANAGER_ENABLE
2290686862fbSopenharmony_ci        std::unordered_map<std::string, std::string> payload;
2291686862fbSopenharmony_ci        payload[PID_KEY] = std::to_string(callerInfo.pid);
2292686862fbSopenharmony_ci        payload[UID_KEY] = std::to_string(callerInfo.uid);
2293686862fbSopenharmony_ci        payload[BUNDLE_NAME_KEY] =
2294686862fbSopenharmony_ci            callerInfo.bundleNames.empty() ? std::string() : callerInfo.bundleNames.front();
2295686862fbSopenharmony_ci        payload[COMPONENT_TYPE_KEY] = std::to_string(componentType);
2296686862fbSopenharmony_ci        payload[DEVICE_TYPE_KEY] = std::to_string(deviceType);
2297686862fbSopenharmony_ci        payload[CHANGE_TYPE_KEY] = std::to_string(changeType);
2298686862fbSopenharmony_ci        uint32_t type = ResourceSchedule::ResType::RES_TYPE_REPORT_DISTRIBUTE_COMPONENT_CHANGE;
2299686862fbSopenharmony_ci        ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, 0, payload);
2300686862fbSopenharmony_ci#endif
2301686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTEDCOMPONENT_TO_MEMMGR
2302686862fbSopenharmony_ci        Memory::MemMgrClient::GetInstance().NotifyDistDevStatus(callerInfo.pid, callerInfo.uid,
2303686862fbSopenharmony_ci            callerInfo.bundleNames.empty() ? std::string() : callerInfo.bundleNames.front(),
2304686862fbSopenharmony_ci            changeType == DISTRIBUTED_COMPONENT_ADD);
2305686862fbSopenharmony_ci#endif
2306686862fbSopenharmony_ci    };
2307686862fbSopenharmony_ci    if (componentChangeHandler_ != nullptr) {
2308686862fbSopenharmony_ci        componentChangeHandler_->PostTask(func);
2309686862fbSopenharmony_ci        return;
2310686862fbSopenharmony_ci    }
2311686862fbSopenharmony_ci    HILOGE("HandleDistributedComponentChange handler postTask failed");
2312686862fbSopenharmony_ci#endif
2313686862fbSopenharmony_ci}
2314686862fbSopenharmony_ci
2315686862fbSopenharmony_civoid DistributedSchedService::ReportDistributedComponentChange(const ConnectInfo& connectInfo, int32_t changeType,
2316686862fbSopenharmony_ci    int32_t componentType, int32_t deviceType)
2317686862fbSopenharmony_ci{
2318686862fbSopenharmony_ci#ifdef EFFICIENCY_MANAGER_ENABLE
2319686862fbSopenharmony_ci    HILOGI("callee report");
2320686862fbSopenharmony_ci    auto func = [this, connectInfo, changeType, componentType, deviceType]() {
2321686862fbSopenharmony_ci        std::unordered_map<std::string, std::string> payload;
2322686862fbSopenharmony_ci        payload[UID_KEY] = std::to_string(BundleManagerInternal::GetUidFromBms(connectInfo.element.GetBundleName()));
2323686862fbSopenharmony_ci        payload[BUNDLE_NAME_KEY] = connectInfo.element.GetBundleName();
2324686862fbSopenharmony_ci        payload[COMPONENT_TYPE_KEY] = std::to_string(componentType);
2325686862fbSopenharmony_ci        payload[DEVICE_TYPE_KEY] = std::to_string(deviceType);
2326686862fbSopenharmony_ci        payload[CHANGE_TYPE_KEY] = std::to_string(changeType);
2327686862fbSopenharmony_ci        uint32_t type = ResourceSchedule::ResType::RES_TYPE_REPORT_DISTRIBUTE_COMPONENT_CHANGE;
2328686862fbSopenharmony_ci        ResourceSchedule::ResSchedClient::GetInstance().ReportData(type, 0, payload);
2329686862fbSopenharmony_ci    };
2330686862fbSopenharmony_ci    if (componentChangeHandler_ != nullptr) {
2331686862fbSopenharmony_ci        componentChangeHandler_->PostTask(func);
2332686862fbSopenharmony_ci        return;
2333686862fbSopenharmony_ci    }
2334686862fbSopenharmony_ci    HILOGE("HandleDistributedComponentChange handler postTask failed");
2335686862fbSopenharmony_ci#endif
2336686862fbSopenharmony_ci}
2337686862fbSopenharmony_ci
2338686862fbSopenharmony_cisptr<IDistributedSched> DistributedSchedService::GetRemoteDms(const std::string& remoteDeviceId)
2339686862fbSopenharmony_ci{
2340686862fbSopenharmony_ci    if (remoteDeviceId.empty()) {
2341686862fbSopenharmony_ci        HILOGE("GetRemoteDms remoteDeviceId is empty");
2342686862fbSopenharmony_ci        return nullptr;
2343686862fbSopenharmony_ci    }
2344686862fbSopenharmony_ci    HILOGD("GetRemoteDms connect deviceid is %s", GetAnonymStr(remoteDeviceId).c_str());
2345686862fbSopenharmony_ci    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
2346686862fbSopenharmony_ci    if (samgr == nullptr) {
2347686862fbSopenharmony_ci        HILOGE("GetRemoteDms failed to connect to systemAbilityMgr!");
2348686862fbSopenharmony_ci        return nullptr;
2349686862fbSopenharmony_ci    }
2350686862fbSopenharmony_ci    HILOGI("[PerformanceTest] GetRemoteDms begin");
2351686862fbSopenharmony_ci    auto object = samgr->CheckSystemAbility(DISTRIBUTED_SCHED_SA_ID, remoteDeviceId);
2352686862fbSopenharmony_ci    HILOGI("[PerformanceTest] GetRemoteDms end");
2353686862fbSopenharmony_ci    if (object == nullptr) {
2354686862fbSopenharmony_ci        HILOGE("GetRemoteDms failed to get remote DistributedSched %{private}s", GetAnonymStr(remoteDeviceId).c_str());
2355686862fbSopenharmony_ci        return nullptr;
2356686862fbSopenharmony_ci    }
2357686862fbSopenharmony_ci    return iface_cast<IDistributedSched>(object);
2358686862fbSopenharmony_ci}
2359686862fbSopenharmony_ci
2360686862fbSopenharmony_cibool DistributedSchedService::GetLocalDeviceId(std::string& localDeviceId)
2361686862fbSopenharmony_ci{
2362686862fbSopenharmony_ci    if (!DtbschedmgrDeviceInfoStorage::GetInstance().GetLocalDeviceId(localDeviceId)) {
2363686862fbSopenharmony_ci        HILOGE("GetLocalDeviceId failed");
2364686862fbSopenharmony_ci        return false;
2365686862fbSopenharmony_ci    }
2366686862fbSopenharmony_ci    return true;
2367686862fbSopenharmony_ci}
2368686862fbSopenharmony_ci
2369686862fbSopenharmony_cibool DistributedSchedService::CheckDeviceId(const std::string& localDeviceId, const std::string& remoteDeviceId)
2370686862fbSopenharmony_ci{
2371686862fbSopenharmony_ci    // remoteDeviceId must not same with localDeviceId
2372686862fbSopenharmony_ci    if (localDeviceId.empty() || remoteDeviceId.empty() || localDeviceId == remoteDeviceId) {
2373686862fbSopenharmony_ci        HILOGE("check deviceId failed");
2374686862fbSopenharmony_ci        return false;
2375686862fbSopenharmony_ci    }
2376686862fbSopenharmony_ci    return true;
2377686862fbSopenharmony_ci}
2378686862fbSopenharmony_ci
2379686862fbSopenharmony_cibool DistributedSchedService::CheckDeviceIdFromRemote(const std::string& localDeviceId,
2380686862fbSopenharmony_ci    const std::string& destinationDeviceId, const std::string& sourceDeviceId)
2381686862fbSopenharmony_ci{
2382686862fbSopenharmony_ci    if (localDeviceId.empty() || destinationDeviceId.empty() || sourceDeviceId.empty()) {
2383686862fbSopenharmony_ci        HILOGE("CheckDeviceIdFromRemote failed");
2384686862fbSopenharmony_ci        return false;
2385686862fbSopenharmony_ci    }
2386686862fbSopenharmony_ci    // destinationDeviceId set by remote must be same with localDeviceId
2387686862fbSopenharmony_ci    if (localDeviceId != destinationDeviceId) {
2388686862fbSopenharmony_ci        HILOGE("destinationDeviceId is not same with localDeviceId");
2389686862fbSopenharmony_ci        return false;
2390686862fbSopenharmony_ci    }
2391686862fbSopenharmony_ci    HILOGD("CheckDeviceIdFromRemote sourceDeviceId %s", GetAnonymStr(sourceDeviceId).c_str());
2392686862fbSopenharmony_ci    HILOGD("CheckDeviceIdFromRemote localDeviceId %s", GetAnonymStr(localDeviceId).c_str());
2393686862fbSopenharmony_ci    HILOGD("CheckDeviceIdFromRemote destinationDeviceId %s", GetAnonymStr(destinationDeviceId).c_str());
2394686862fbSopenharmony_ci
2395686862fbSopenharmony_ci    if (sourceDeviceId == destinationDeviceId || sourceDeviceId == localDeviceId) {
2396686862fbSopenharmony_ci        HILOGE("destinationDeviceId is different with localDeviceId and destinationDeviceId");
2397686862fbSopenharmony_ci        return false;
2398686862fbSopenharmony_ci    }
2399686862fbSopenharmony_ci
2400686862fbSopenharmony_ci    if (sourceDeviceId != IPCSkeleton::GetCallingDeviceID()) {
2401686862fbSopenharmony_ci        HILOGE("sourceDeviceId is not correct");
2402686862fbSopenharmony_ci        return false;
2403686862fbSopenharmony_ci    }
2404686862fbSopenharmony_ci    return true;
2405686862fbSopenharmony_ci}
2406686862fbSopenharmony_ci
2407686862fbSopenharmony_ciint32_t DistributedSchedService::ConnectAbilityFromRemote(const OHOS::AAFwk::Want& want,
2408686862fbSopenharmony_ci    const AppExecFwk::AbilityInfo& abilityInfo, const sptr<IRemoteObject>& connect,
2409686862fbSopenharmony_ci    const CallerInfo& callerInfo, const AccountInfo& accountInfo)
2410686862fbSopenharmony_ci{
2411686862fbSopenharmony_ci    HILOGD("[PerformanceTest] DistributedSchedService ConnectAbilityFromRemote begin");
2412686862fbSopenharmony_ci    if (connect == nullptr) {
2413686862fbSopenharmony_ci        HILOGE("ConnectAbilityFromRemote connect is null");
2414686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2415686862fbSopenharmony_ci    }
2416686862fbSopenharmony_ci    HILOGD("ConnectAbilityFromRemote uid is %{public}d, pid is %{public}d, AccessTokenID is %{public}s",
2417686862fbSopenharmony_ci        callerInfo.uid, callerInfo.pid, GetAnonymStr(std::to_string(callerInfo.accessToken)).c_str());
2418686862fbSopenharmony_ci    std::string localDeviceId;
2419686862fbSopenharmony_ci    std::string destinationDeviceId = want.GetElement().GetDeviceID();
2420686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) ||
2421686862fbSopenharmony_ci        !CheckDeviceIdFromRemote(localDeviceId, destinationDeviceId, callerInfo.sourceDeviceId)) {
2422686862fbSopenharmony_ci        HILOGE("ConnectAbilityFromRemote check deviceId failed");
2423686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2424686862fbSopenharmony_ci    }
2425686862fbSopenharmony_ci    int32_t result = CheckTargetPermission(want, callerInfo, accountInfo, START_PERMISSION, true);
2426686862fbSopenharmony_ci    if (result != ERR_OK) {
2427686862fbSopenharmony_ci        HILOGE("CheckTargetPermission failed!!");
2428686862fbSopenharmony_ci        return result;
2429686862fbSopenharmony_ci    }
2430686862fbSopenharmony_ci
2431686862fbSopenharmony_ci    HILOGD("ConnectAbilityFromRemote callerType is %{public}d", callerInfo.callerType);
2432686862fbSopenharmony_ci    sptr<IRemoteObject> callbackWrapper = connect;
2433686862fbSopenharmony_ci    std::map<sptr<IRemoteObject>, ConnectInfo>::iterator itConnect;
2434686862fbSopenharmony_ci    if (callerInfo.callerType == CALLER_TYPE_HARMONY) {
2435686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(connectLock_);
2436686862fbSopenharmony_ci        itConnect = connectAbilityMap_.find(connect);
2437686862fbSopenharmony_ci        if (itConnect != connectAbilityMap_.end()) {
2438686862fbSopenharmony_ci            callbackWrapper = itConnect->second.callbackWrapper;
2439686862fbSopenharmony_ci        } else {
2440686862fbSopenharmony_ci            callbackWrapper = new AbilityConnectionWrapperStub(connect);
2441686862fbSopenharmony_ci        }
2442686862fbSopenharmony_ci    }
2443686862fbSopenharmony_ci    int32_t errCode = DistributedSchedAdapter::GetInstance().ConnectAbility(want, callbackWrapper, this);
2444686862fbSopenharmony_ci    HILOGD("[PerformanceTest] ConnectAbilityFromRemote end");
2445686862fbSopenharmony_ci    if (errCode == ERR_OK) {
2446686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(connectLock_);
2447686862fbSopenharmony_ci        if (itConnect == connectAbilityMap_.end()) {
2448686862fbSopenharmony_ci            ConnectInfo connectInfo {callerInfo, callbackWrapper, want.GetElement()};
2449686862fbSopenharmony_ci            ReportDistributedComponentChange(connectInfo, DISTRIBUTED_COMPONENT_ADD,
2450686862fbSopenharmony_ci                IDistributedSched::CONNECT, IDistributedSched::CALLEE);
2451686862fbSopenharmony_ci            connectAbilityMap_.emplace(connect, connectInfo);
2452686862fbSopenharmony_ci        }
2453686862fbSopenharmony_ci    }
2454686862fbSopenharmony_ci    return errCode;
2455686862fbSopenharmony_ci}
2456686862fbSopenharmony_ci
2457686862fbSopenharmony_ciint32_t DistributedSchedService::DisconnectEachRemoteAbilityLocked(const std::string& localDeviceId,
2458686862fbSopenharmony_ci    const std::string& remoteDeviceId, const sptr<IRemoteObject>& connect)
2459686862fbSopenharmony_ci{
2460686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(remoteDeviceId);
2461686862fbSopenharmony_ci    if (remoteDms == nullptr) {
2462686862fbSopenharmony_ci        HILOGE("DisconnectRemoteAbility get remote dms failed");
2463686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2464686862fbSopenharmony_ci    }
2465686862fbSopenharmony_ci    int32_t result = remoteDms->DisconnectAbilityFromRemote(connect, IPCSkeleton::GetCallingUid(), localDeviceId);
2466686862fbSopenharmony_ci    if (result != ERR_OK) {
2467686862fbSopenharmony_ci        HILOGE("DisconnectEachRemoteAbilityLocked result is %{public}d", result);
2468686862fbSopenharmony_ci    }
2469686862fbSopenharmony_ci    return result;
2470686862fbSopenharmony_ci}
2471686862fbSopenharmony_ci
2472686862fbSopenharmony_ciint32_t DistributedSchedService::DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid,
2473686862fbSopenharmony_ci    uint32_t accessToken)
2474686862fbSopenharmony_ci{
2475686862fbSopenharmony_ci    if (connect == nullptr) {
2476686862fbSopenharmony_ci        HILOGE("DisconnectRemoteAbility connect is null");
2477686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2478686862fbSopenharmony_ci    }
2479686862fbSopenharmony_ci
2480686862fbSopenharmony_ci    std::list<ConnectAbilitySession> sessionsList;
2481686862fbSopenharmony_ci    {
2482686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(distributedLock_);
2483686862fbSopenharmony_ci        auto it = distributedConnectAbilityMap_.find(connect);
2484686862fbSopenharmony_ci        if (it != distributedConnectAbilityMap_.end()) {
2485686862fbSopenharmony_ci            sessionsList = it->second;
2486686862fbSopenharmony_ci            int32_t uid = GetUidLocked(sessionsList);
2487686862fbSopenharmony_ci            // also decrease number when erase connect
2488686862fbSopenharmony_ci            DecreaseConnectLocked(uid);
2489686862fbSopenharmony_ci            connect->RemoveDeathRecipient(connectDeathRecipient_);
2490686862fbSopenharmony_ci            if (!sessionsList.empty()) {
2491686862fbSopenharmony_ci                ReportDistributedComponentChange(sessionsList.front().GetCallerInfo(), DISTRIBUTED_COMPONENT_REMOVE,
2492686862fbSopenharmony_ci                    IDistributedSched::CONNECT, IDistributedSched::CALLER);
2493686862fbSopenharmony_ci            }
2494686862fbSopenharmony_ci            distributedConnectAbilityMap_.erase(it);
2495686862fbSopenharmony_ci            HILOGI("remove connection success");
2496686862fbSopenharmony_ci        } else {
2497686862fbSopenharmony_ci#ifdef DMSFWK_INTERACTIVE_ADAPTER
2498686862fbSopenharmony_ci            return DisconnectRemoteAbilityAdapter(connect, callerUid, accessToken);
2499686862fbSopenharmony_ci#endif // DMSFWK_INTERACTIVE_ADAPTER
2500686862fbSopenharmony_ci        }
2501686862fbSopenharmony_ci    }
2502686862fbSopenharmony_ci    if (!sessionsList.empty()) {
2503686862fbSopenharmony_ci        for (const auto& session : sessionsList) {
2504686862fbSopenharmony_ci            if (session.GetTargetComponent() == TargetComponent::HARMONY_COMPONENT) {
2505686862fbSopenharmony_ci                DisconnectEachRemoteAbilityLocked(session.GetSourceDeviceId(),
2506686862fbSopenharmony_ci                    session.GetDestinationDeviceId(), connect);
2507686862fbSopenharmony_ci            } else {
2508686862fbSopenharmony_ci                HILOGW("DisconnectRemoteAbility non-harmony component");
2509686862fbSopenharmony_ci            }
2510686862fbSopenharmony_ci        }
2511686862fbSopenharmony_ci        return ERR_OK;
2512686862fbSopenharmony_ci    }
2513686862fbSopenharmony_ci    return NO_CONNECT_CALLBACK_ERR;
2514686862fbSopenharmony_ci}
2515686862fbSopenharmony_ci
2516686862fbSopenharmony_ciint32_t DistributedSchedService::DisconnectAbilityFromRemote(const sptr<IRemoteObject>& connect,
2517686862fbSopenharmony_ci    int32_t uid, const std::string& sourceDeviceId)
2518686862fbSopenharmony_ci{
2519686862fbSopenharmony_ci    if (connect == nullptr) {
2520686862fbSopenharmony_ci        HILOGE("DisconnectAbilityFromRemote connect is null");
2521686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2522686862fbSopenharmony_ci    }
2523686862fbSopenharmony_ci
2524686862fbSopenharmony_ci    HILOGD("[PerformanceTest] DisconnectAbilityFromRemote begin");
2525686862fbSopenharmony_ci    std::string localDeviceId;
2526686862fbSopenharmony_ci    AppExecFwk::AbilityInfo abilityInfo;
2527686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || localDeviceId.empty() ||
2528686862fbSopenharmony_ci        sourceDeviceId.empty() || localDeviceId == sourceDeviceId) {
2529686862fbSopenharmony_ci        HILOGE("DisconnectAbilityFromRemote check deviceId failed");
2530686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
2531686862fbSopenharmony_ci    }
2532686862fbSopenharmony_ci
2533686862fbSopenharmony_ci    sptr<IRemoteObject> callbackWrapper = connect;
2534686862fbSopenharmony_ci    {
2535686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(connectLock_);
2536686862fbSopenharmony_ci        auto itConnect = connectAbilityMap_.find(connect);
2537686862fbSopenharmony_ci        if (itConnect != connectAbilityMap_.end()) {
2538686862fbSopenharmony_ci            callbackWrapper = itConnect->second.callbackWrapper;
2539686862fbSopenharmony_ci            ReportDistributedComponentChange(itConnect->second, DISTRIBUTED_COMPONENT_REMOVE,
2540686862fbSopenharmony_ci                IDistributedSched::CONNECT, IDistributedSched::CALLEE);
2541686862fbSopenharmony_ci            connectAbilityMap_.erase(itConnect);
2542686862fbSopenharmony_ci        } else {
2543686862fbSopenharmony_ci            if (!IPCSkeleton::IsLocalCalling()) {
2544686862fbSopenharmony_ci                HILOGE("DisconnectAbilityFromRemote connect not found");
2545686862fbSopenharmony_ci                return INVALID_REMOTE_PARAMETERS_ERR;
2546686862fbSopenharmony_ci            }
2547686862fbSopenharmony_ci        }
2548686862fbSopenharmony_ci    }
2549686862fbSopenharmony_ci    int32_t result = DistributedSchedAdapter::GetInstance().DisconnectAbility(callbackWrapper);
2550686862fbSopenharmony_ci    HILOGD("[PerformanceTest] DisconnectAbilityFromRemote end");
2551686862fbSopenharmony_ci    return result;
2552686862fbSopenharmony_ci}
2553686862fbSopenharmony_ci
2554686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyProcessDiedFromRemote(const CallerInfo& callerInfo)
2555686862fbSopenharmony_ci{
2556686862fbSopenharmony_ci    HILOGI("NotifyProcessDiedFromRemote called");
2557686862fbSopenharmony_ci    int32_t errCode = ERR_OK;
2558686862fbSopenharmony_ci    {
2559686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(connectLock_);
2560686862fbSopenharmony_ci        for (auto iter = connectAbilityMap_.begin(); iter != connectAbilityMap_.end();) {
2561686862fbSopenharmony_ci            ConnectInfo& connectInfo = iter->second;
2562686862fbSopenharmony_ci            if (callerInfo.sourceDeviceId == connectInfo.callerInfo.sourceDeviceId
2563686862fbSopenharmony_ci                && callerInfo.uid == connectInfo.callerInfo.uid
2564686862fbSopenharmony_ci                && callerInfo.pid == connectInfo.callerInfo.pid
2565686862fbSopenharmony_ci                && callerInfo.callerType == connectInfo.callerInfo.callerType) {
2566686862fbSopenharmony_ci                HILOGI("NotifyProcessDiedFromRemote erase connection success");
2567686862fbSopenharmony_ci                int32_t ret = DistributedSchedAdapter::GetInstance().DisconnectAbility(connectInfo.callbackWrapper);
2568686862fbSopenharmony_ci                if (ret != ERR_OK) {
2569686862fbSopenharmony_ci                    errCode = ret;
2570686862fbSopenharmony_ci                }
2571686862fbSopenharmony_ci                ReportDistributedComponentChange(connectInfo, DISTRIBUTED_COMPONENT_REMOVE,
2572686862fbSopenharmony_ci                    IDistributedSched::CONNECT, IDistributedSched::CALLEE);
2573686862fbSopenharmony_ci                iter = connectAbilityMap_.erase(iter);
2574686862fbSopenharmony_ci            } else {
2575686862fbSopenharmony_ci                iter++;
2576686862fbSopenharmony_ci            }
2577686862fbSopenharmony_ci        }
2578686862fbSopenharmony_ci    }
2579686862fbSopenharmony_ci    return errCode;
2580686862fbSopenharmony_ci}
2581686862fbSopenharmony_ci
2582686862fbSopenharmony_civoid DistributedSchedService::RemoveConnectAbilityInfo(const std::string& deviceId)
2583686862fbSopenharmony_ci{
2584686862fbSopenharmony_ci    {
2585686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(distributedLock_);
2586686862fbSopenharmony_ci        for (auto iter = distributedConnectAbilityMap_.begin(); iter != distributedConnectAbilityMap_.end();) {
2587686862fbSopenharmony_ci            std::list<ConnectAbilitySession>& sessionsList = iter->second;
2588686862fbSopenharmony_ci            int32_t uid = GetUidLocked(sessionsList);
2589686862fbSopenharmony_ci            auto itSession = std::find_if(sessionsList.begin(), sessionsList.end(), [&deviceId](const auto& session) {
2590686862fbSopenharmony_ci                return session.GetDestinationDeviceId() == deviceId;
2591686862fbSopenharmony_ci            });
2592686862fbSopenharmony_ci            CallerInfo callerInfo;
2593686862fbSopenharmony_ci            if (itSession != sessionsList.end()) {
2594686862fbSopenharmony_ci                NotifyDeviceOfflineToAppLocked(iter->first, *itSession);
2595686862fbSopenharmony_ci                callerInfo = itSession->GetCallerInfo();
2596686862fbSopenharmony_ci                sessionsList.erase(itSession);
2597686862fbSopenharmony_ci            }
2598686862fbSopenharmony_ci
2599686862fbSopenharmony_ci            if (sessionsList.empty()) {
2600686862fbSopenharmony_ci                if (iter->first != nullptr) {
2601686862fbSopenharmony_ci                    iter->first->RemoveDeathRecipient(connectDeathRecipient_);
2602686862fbSopenharmony_ci                }
2603686862fbSopenharmony_ci                DecreaseConnectLocked(uid);
2604686862fbSopenharmony_ci                ReportDistributedComponentChange(callerInfo, DISTRIBUTED_COMPONENT_REMOVE,
2605686862fbSopenharmony_ci                    IDistributedSched::CONNECT, IDistributedSched::CALLER);
2606686862fbSopenharmony_ci                iter = distributedConnectAbilityMap_.erase(iter);
2607686862fbSopenharmony_ci            } else {
2608686862fbSopenharmony_ci                iter++;
2609686862fbSopenharmony_ci            }
2610686862fbSopenharmony_ci        }
2611686862fbSopenharmony_ci    }
2612686862fbSopenharmony_ci
2613686862fbSopenharmony_ci    {
2614686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(connectLock_);
2615686862fbSopenharmony_ci        for (auto iter = connectAbilityMap_.begin(); iter != connectAbilityMap_.end();) {
2616686862fbSopenharmony_ci            ConnectInfo& connectInfo = iter->second;
2617686862fbSopenharmony_ci            if (deviceId == connectInfo.callerInfo.sourceDeviceId) {
2618686862fbSopenharmony_ci                DistributedSchedAdapter::GetInstance().DisconnectAbility(connectInfo.callbackWrapper);
2619686862fbSopenharmony_ci                ReportDistributedComponentChange(connectInfo, DISTRIBUTED_COMPONENT_REMOVE,
2620686862fbSopenharmony_ci                    IDistributedSched::CONNECT, IDistributedSched::CALLEE);
2621686862fbSopenharmony_ci                connectAbilityMap_.erase(iter++);
2622686862fbSopenharmony_ci                HILOGI("ProcessDeviceOffline erase connection success");
2623686862fbSopenharmony_ci            } else {
2624686862fbSopenharmony_ci                iter++;
2625686862fbSopenharmony_ci            }
2626686862fbSopenharmony_ci        }
2627686862fbSopenharmony_ci    }
2628686862fbSopenharmony_ci}
2629686862fbSopenharmony_ci
2630686862fbSopenharmony_ciErrCode DistributedSchedService::QueryOsAccount(int32_t& activeAccountId)
2631686862fbSopenharmony_ci{
2632686862fbSopenharmony_ci#ifdef OS_ACCOUNT_PART
2633686862fbSopenharmony_ci    std::vector<int32_t> ids;
2634686862fbSopenharmony_ci    ErrCode err = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
2635686862fbSopenharmony_ci    if (err != ERR_OK || ids.empty()) {
2636686862fbSopenharmony_ci        HILOGE("QueryActiveOsAccountIds passing param invalid or return error!, err : %{public}d", err);
2637686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2638686862fbSopenharmony_ci    }
2639686862fbSopenharmony_ci    activeAccountId = ids[0];
2640686862fbSopenharmony_ci#endif
2641686862fbSopenharmony_ci    return ERR_OK;
2642686862fbSopenharmony_ci}
2643686862fbSopenharmony_ci
2644686862fbSopenharmony_civoid DistributedSchedService::ProcessDeviceOffline(const std::string& deviceId)
2645686862fbSopenharmony_ci{
2646686862fbSopenharmony_ci    HILOGI("ProcessDeviceOffline called");
2647686862fbSopenharmony_ci    std::string localDeviceId;
2648686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || !CheckDeviceId(localDeviceId, deviceId)) {
2649686862fbSopenharmony_ci        HILOGE("ProcessDeviceOffline check deviceId failed");
2650686862fbSopenharmony_ci        return;
2651686862fbSopenharmony_ci    }
2652686862fbSopenharmony_ci    RemoveConnectAbilityInfo(deviceId);
2653686862fbSopenharmony_ci    ProcessCalleeOffline(deviceId);
2654686862fbSopenharmony_ci    ProcessFreeInstallOffline(deviceId);
2655686862fbSopenharmony_ci}
2656686862fbSopenharmony_ci
2657686862fbSopenharmony_civoid DistributedSchedService::ProcessFreeInstallOffline(const std::string& deviceId)
2658686862fbSopenharmony_ci{
2659686862fbSopenharmony_ci    if (dmsCallbackTask_ == nullptr) {
2660686862fbSopenharmony_ci        HILOGE("callbackTask object null!");
2661686862fbSopenharmony_ci        return;
2662686862fbSopenharmony_ci    }
2663686862fbSopenharmony_ci    dmsCallbackTask_->NotifyDeviceOffline(deviceId);
2664686862fbSopenharmony_ci}
2665686862fbSopenharmony_ci
2666686862fbSopenharmony_civoid DistributedSchedService::NotifyDeviceOfflineToAppLocked(const sptr<IRemoteObject>& connect,
2667686862fbSopenharmony_ci    const ConnectAbilitySession& session)
2668686862fbSopenharmony_ci{
2669686862fbSopenharmony_ci    std::list<AppExecFwk::ElementName> elementsList = session.GetElementsList();
2670686862fbSopenharmony_ci    for (const auto& element : elementsList) {
2671686862fbSopenharmony_ci        int32_t errCode = NotifyApp(connect, element, DEVICE_OFFLINE_ERR);
2672686862fbSopenharmony_ci        if (errCode != ERR_NONE) {
2673686862fbSopenharmony_ci            HILOGW("ProcessDeviceOffline notify failed, errCode = %{public}d", errCode);
2674686862fbSopenharmony_ci        }
2675686862fbSopenharmony_ci    }
2676686862fbSopenharmony_ci}
2677686862fbSopenharmony_ci
2678686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyApp(const sptr<IRemoteObject>& connect,
2679686862fbSopenharmony_ci    const AppExecFwk::ElementName& element, int32_t errCode)
2680686862fbSopenharmony_ci{
2681686862fbSopenharmony_ci    if (connect == nullptr) {
2682686862fbSopenharmony_ci        return OBJECT_NULL;
2683686862fbSopenharmony_ci    }
2684686862fbSopenharmony_ci    MessageParcel data;
2685686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN)) {
2686686862fbSopenharmony_ci        return ERR_FLATTEN_OBJECT;
2687686862fbSopenharmony_ci    }
2688686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Parcelable, &element);
2689686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Int32, errCode);
2690686862fbSopenharmony_ci    MessageParcel reply;
2691686862fbSopenharmony_ci    MessageOption option;
2692686862fbSopenharmony_ci    return connect->SendRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
2693686862fbSopenharmony_ci}
2694686862fbSopenharmony_ci
2695686862fbSopenharmony_civoid DistributedSchedService::ProcessConnectDied(const sptr<IRemoteObject>& connect)
2696686862fbSopenharmony_ci{
2697686862fbSopenharmony_ci    if (connect == nullptr) {
2698686862fbSopenharmony_ci        HILOGE("ProcessConnectDied connect is null");
2699686862fbSopenharmony_ci        return;
2700686862fbSopenharmony_ci    }
2701686862fbSopenharmony_ci
2702686862fbSopenharmony_ci    std::list<ProcessDiedNotifyInfo> notifyList;
2703686862fbSopenharmony_ci    {
2704686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(distributedLock_);
2705686862fbSopenharmony_ci        auto it = distributedConnectAbilityMap_.find(connect);
2706686862fbSopenharmony_ci        if (it == distributedConnectAbilityMap_.end()) {
2707686862fbSopenharmony_ci            return;
2708686862fbSopenharmony_ci        }
2709686862fbSopenharmony_ci        std::list<ConnectAbilitySession>& connectSessionsList = it->second;
2710686862fbSopenharmony_ci        if (connectSessionsList.empty()) {
2711686862fbSopenharmony_ci            return;
2712686862fbSopenharmony_ci        }
2713686862fbSopenharmony_ci        CallerInfo callerInfo = connectSessionsList.front().GetCallerInfo();
2714686862fbSopenharmony_ci        std::set<std::string> processedDeviceSet;
2715686862fbSopenharmony_ci        // to reduce the number of communications between devices, clean all the died process's connections
2716686862fbSopenharmony_ci        for (auto iter = distributedConnectAbilityMap_.begin(); iter != distributedConnectAbilityMap_.end();) {
2717686862fbSopenharmony_ci            std::list<ConnectAbilitySession>& sessionsList = iter->second;
2718686862fbSopenharmony_ci            if (!sessionsList.empty() && sessionsList.front().IsSameCaller(callerInfo)) {
2719686862fbSopenharmony_ci                for (const auto& session : sessionsList) {
2720686862fbSopenharmony_ci                    std::string remoteDeviceId = session.GetDestinationDeviceId();
2721686862fbSopenharmony_ci                    TargetComponent targetComponent = session.GetTargetComponent();
2722686862fbSopenharmony_ci                    // the same session can connect different types component on the same device
2723686862fbSopenharmony_ci                    std::string key = remoteDeviceId + std::to_string(static_cast<int32_t>(targetComponent));
2724686862fbSopenharmony_ci                    // just notify one time for same remote device
2725686862fbSopenharmony_ci                    auto [_, isSuccess] = processedDeviceSet.emplace(key);
2726686862fbSopenharmony_ci                    if (isSuccess) {
2727686862fbSopenharmony_ci                        ProcessDiedNotifyInfo notifyInfo = { remoteDeviceId, callerInfo, targetComponent };
2728686862fbSopenharmony_ci                        notifyList.push_back(notifyInfo);
2729686862fbSopenharmony_ci                    }
2730686862fbSopenharmony_ci                }
2731686862fbSopenharmony_ci                DecreaseConnectLocked(callerInfo.uid);
2732686862fbSopenharmony_ci                if (iter->first != nullptr) {
2733686862fbSopenharmony_ci                    iter->first->RemoveDeathRecipient(connectDeathRecipient_);
2734686862fbSopenharmony_ci                }
2735686862fbSopenharmony_ci                ReportDistributedComponentChange(callerInfo, DISTRIBUTED_COMPONENT_REMOVE,
2736686862fbSopenharmony_ci                    IDistributedSched::CONNECT, IDistributedSched::CALLER);
2737686862fbSopenharmony_ci                iter = distributedConnectAbilityMap_.erase(iter);
2738686862fbSopenharmony_ci            } else {
2739686862fbSopenharmony_ci                iter++;
2740686862fbSopenharmony_ci            }
2741686862fbSopenharmony_ci        }
2742686862fbSopenharmony_ci    }
2743686862fbSopenharmony_ci    NotifyProcessDiedAll(notifyList);
2744686862fbSopenharmony_ci}
2745686862fbSopenharmony_ci
2746686862fbSopenharmony_civoid DistributedSchedService::NotifyProcessDiedAll(const std::list<ProcessDiedNotifyInfo>& notifyList)
2747686862fbSopenharmony_ci{
2748686862fbSopenharmony_ci    for (auto it = notifyList.begin(); it != notifyList.end(); ++it) {
2749686862fbSopenharmony_ci        NotifyProcessDied(it->remoteDeviceId, it->callerInfo, it->targetComponent);
2750686862fbSopenharmony_ci    }
2751686862fbSopenharmony_ci}
2752686862fbSopenharmony_ci
2753686862fbSopenharmony_civoid DistributedSchedService::NotifyProcessDied(const std::string& remoteDeviceId,
2754686862fbSopenharmony_ci    const CallerInfo& callerInfo, TargetComponent targetComponent)
2755686862fbSopenharmony_ci{
2756686862fbSopenharmony_ci    if (targetComponent != TargetComponent::HARMONY_COMPONENT) {
2757686862fbSopenharmony_ci        HILOGD("NotifyProcessDied not harmony component, no need to notify");
2758686862fbSopenharmony_ci        return;
2759686862fbSopenharmony_ci    }
2760686862fbSopenharmony_ci
2761686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(remoteDeviceId);
2762686862fbSopenharmony_ci    if (remoteDms == nullptr) {
2763686862fbSopenharmony_ci        HILOGE("NotifyProcessDied get remote dms failed");
2764686862fbSopenharmony_ci        return;
2765686862fbSopenharmony_ci    }
2766686862fbSopenharmony_ci    int32_t result = remoteDms->NotifyProcessDiedFromRemote(callerInfo);
2767686862fbSopenharmony_ci    HILOGI("NotifyProcessDied result is %{public}d", result);
2768686862fbSopenharmony_ci}
2769686862fbSopenharmony_ci
2770686862fbSopenharmony_ciConnectAbilitySession::ConnectAbilitySession(const std::string& sourceDeviceId, const std::string& destinationDeviceId,
2771686862fbSopenharmony_ci    const CallerInfo& callerInfo, TargetComponent targetComponent)
2772686862fbSopenharmony_ci    : sourceDeviceId_(sourceDeviceId),
2773686862fbSopenharmony_ci      destinationDeviceId_(destinationDeviceId),
2774686862fbSopenharmony_ci      callerInfo_(callerInfo),
2775686862fbSopenharmony_ci      targetComponent_(targetComponent)
2776686862fbSopenharmony_ci{
2777686862fbSopenharmony_ci}
2778686862fbSopenharmony_ci
2779686862fbSopenharmony_civoid ConnectAbilitySession::AddElement(const AppExecFwk::ElementName& element)
2780686862fbSopenharmony_ci{
2781686862fbSopenharmony_ci    for (const auto& elementName : elementsList_) {
2782686862fbSopenharmony_ci        if (elementName == element) {
2783686862fbSopenharmony_ci            return;
2784686862fbSopenharmony_ci        }
2785686862fbSopenharmony_ci    }
2786686862fbSopenharmony_ci    elementsList_.emplace_back(element);
2787686862fbSopenharmony_ci}
2788686862fbSopenharmony_ci
2789686862fbSopenharmony_cibool ConnectAbilitySession::IsSameCaller(const CallerInfo& callerInfo)
2790686862fbSopenharmony_ci{
2791686862fbSopenharmony_ci    return (callerInfo.uid == callerInfo_.uid &&
2792686862fbSopenharmony_ci            callerInfo.pid == callerInfo_.pid &&
2793686862fbSopenharmony_ci            callerInfo.sourceDeviceId == callerInfo_.sourceDeviceId &&
2794686862fbSopenharmony_ci            callerInfo.callerType == callerInfo_.callerType);
2795686862fbSopenharmony_ci}
2796686862fbSopenharmony_ci
2797686862fbSopenharmony_civoid DistributedSchedService::DumpConnectInfo(std::string& info)
2798686862fbSopenharmony_ci{
2799686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(distributedLock_);
2800686862fbSopenharmony_ci    info += "connected remote abilities:\n";
2801686862fbSopenharmony_ci    if (!distributedConnectAbilityMap_.empty()) {
2802686862fbSopenharmony_ci        for (const auto& distributedConnect : distributedConnectAbilityMap_) {
2803686862fbSopenharmony_ci            const std::list<ConnectAbilitySession> sessionsList = distributedConnect.second;
2804686862fbSopenharmony_ci            DumpSessionsLocked(sessionsList, info);
2805686862fbSopenharmony_ci        }
2806686862fbSopenharmony_ci    } else {
2807686862fbSopenharmony_ci        info += "  <none info>\n";
2808686862fbSopenharmony_ci    }
2809686862fbSopenharmony_ci}
2810686862fbSopenharmony_ci
2811686862fbSopenharmony_civoid DistributedSchedService::DumpSessionsLocked(const std::list<ConnectAbilitySession>& sessionsList,
2812686862fbSopenharmony_ci    std::string& info)
2813686862fbSopenharmony_ci{
2814686862fbSopenharmony_ci    for (const auto& session : sessionsList) {
2815686862fbSopenharmony_ci        info += "  ";
2816686862fbSopenharmony_ci        info += "SourceDeviceId: ";
2817686862fbSopenharmony_ci        info += session.GetSourceDeviceId();
2818686862fbSopenharmony_ci        info += ", ";
2819686862fbSopenharmony_ci        info += "DestinationDeviceId: ";
2820686862fbSopenharmony_ci        info += session.GetDestinationDeviceId();
2821686862fbSopenharmony_ci        info += ", ";
2822686862fbSopenharmony_ci        info += "CallerUid: ";
2823686862fbSopenharmony_ci        info += std::to_string(session.GetCallerInfo().uid);
2824686862fbSopenharmony_ci        info += ", ";
2825686862fbSopenharmony_ci        info += "CallerPid: ";
2826686862fbSopenharmony_ci        info += std::to_string(session.GetCallerInfo().pid);
2827686862fbSopenharmony_ci        info += ", ";
2828686862fbSopenharmony_ci        info += "CallerType: ";
2829686862fbSopenharmony_ci        info += std::to_string(session.GetCallerInfo().callerType);
2830686862fbSopenharmony_ci        DumpElementLocked(session.GetElementsList(), info);
2831686862fbSopenharmony_ci        info += "\n";
2832686862fbSopenharmony_ci    }
2833686862fbSopenharmony_ci}
2834686862fbSopenharmony_ci
2835686862fbSopenharmony_civoid DistributedSchedService::DumpElementLocked(const std::list<AppExecFwk::ElementName>& elementsList,
2836686862fbSopenharmony_ci    std::string& info)
2837686862fbSopenharmony_ci{
2838686862fbSopenharmony_ci    for (const auto& element : elementsList) {
2839686862fbSopenharmony_ci        info += ", ";
2840686862fbSopenharmony_ci        info += "BundleName: ";
2841686862fbSopenharmony_ci        info += element.GetBundleName();
2842686862fbSopenharmony_ci        info += ", ";
2843686862fbSopenharmony_ci        info += "AbilityName: ";
2844686862fbSopenharmony_ci        info += element.GetAbilityName();
2845686862fbSopenharmony_ci    }
2846686862fbSopenharmony_ci}
2847686862fbSopenharmony_ci
2848686862fbSopenharmony_ci#ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER
2849686862fbSopenharmony_ciint32_t DistributedSchedService::GetMissionInfos(const std::string& deviceId, int32_t numMissions,
2850686862fbSopenharmony_ci    std::vector<MissionInfo>& missionInfos)
2851686862fbSopenharmony_ci{
2852686862fbSopenharmony_ci    return DistributedSchedMissionManager::GetInstance().GetMissionInfos(deviceId, numMissions, missionInfos);
2853686862fbSopenharmony_ci}
2854686862fbSopenharmony_ci
2855686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyMissionsChangedFromRemote(const std::vector<DstbMissionInfo>& missionInfos,
2856686862fbSopenharmony_ci    const CallerInfo& callerInfo)
2857686862fbSopenharmony_ci{
2858686862fbSopenharmony_ci    return DistributedSchedMissionManager::GetInstance()
2859686862fbSopenharmony_ci        .NotifyMissionsChangedFromRemote(callerInfo, missionInfos);
2860686862fbSopenharmony_ci}
2861686862fbSopenharmony_ci
2862686862fbSopenharmony_ciint32_t DistributedSchedService::GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId,
2863686862fbSopenharmony_ci    std::unique_ptr<MissionSnapshot>& missionSnapshot)
2864686862fbSopenharmony_ci{
2865686862fbSopenharmony_ci    return DistributedSchedMissionManager::GetInstance()
2866686862fbSopenharmony_ci        .GetRemoteMissionSnapshotInfo(networkId, missionId, missionSnapshot);
2867686862fbSopenharmony_ci}
2868686862fbSopenharmony_ci
2869686862fbSopenharmony_ciint32_t DistributedSchedService::RegisterMissionListener(const std::u16string& devId,
2870686862fbSopenharmony_ci    const sptr<IRemoteObject>& obj)
2871686862fbSopenharmony_ci{
2872686862fbSopenharmony_ci    return DistributedSchedMissionManager::GetInstance().RegisterMissionListener(devId, obj);
2873686862fbSopenharmony_ci}
2874686862fbSopenharmony_ci
2875686862fbSopenharmony_ciint32_t DistributedSchedService::RegisterOnListener(const std::string& type,
2876686862fbSopenharmony_ci    const sptr<IRemoteObject>& obj, int32_t callingUid)
2877686862fbSopenharmony_ci{
2878686862fbSopenharmony_ci    return DMSContinueRecvMgr::GetInstance().RegisterOnListener(type, obj);
2879686862fbSopenharmony_ci}
2880686862fbSopenharmony_ci
2881686862fbSopenharmony_ciint32_t DistributedSchedService::RegisterOffListener(const std::string& type,
2882686862fbSopenharmony_ci    const sptr<IRemoteObject>& obj, int32_t callingUid)
2883686862fbSopenharmony_ci{
2884686862fbSopenharmony_ci    return DMSContinueRecvMgr::GetInstance().RegisterOffListener(type, obj);
2885686862fbSopenharmony_ci}
2886686862fbSopenharmony_ci
2887686862fbSopenharmony_ciint32_t DistributedSchedService::UnRegisterMissionListener(const std::u16string& devId,
2888686862fbSopenharmony_ci    const sptr<IRemoteObject>& obj)
2889686862fbSopenharmony_ci{
2890686862fbSopenharmony_ci    return DistributedSchedMissionManager::GetInstance().UnRegisterMissionListener(devId, obj);
2891686862fbSopenharmony_ci}
2892686862fbSopenharmony_ci
2893686862fbSopenharmony_ciint32_t DistributedSchedService::StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag)
2894686862fbSopenharmony_ci{
2895686862fbSopenharmony_ci    return DistributedSchedMissionManager::GetInstance().StartSyncRemoteMissions(devId, fixConflict, tag);
2896686862fbSopenharmony_ci}
2897686862fbSopenharmony_ci
2898686862fbSopenharmony_ciint32_t DistributedSchedService::StopSyncRemoteMissions(const std::string& devId)
2899686862fbSopenharmony_ci{
2900686862fbSopenharmony_ci    return DistributedSchedMissionManager::GetInstance().StopSyncRemoteMissions(devId, false, true);
2901686862fbSopenharmony_ci}
2902686862fbSopenharmony_ci
2903686862fbSopenharmony_ciint32_t DistributedSchedService::StartSyncMissionsFromRemote(const CallerInfo& callerInfo,
2904686862fbSopenharmony_ci    std::vector<DstbMissionInfo>& missionInfos)
2905686862fbSopenharmony_ci{
2906686862fbSopenharmony_ci    return DistributedSchedMissionManager::GetInstance().StartSyncMissionsFromRemote(callerInfo, missionInfos);
2907686862fbSopenharmony_ci}
2908686862fbSopenharmony_ci
2909686862fbSopenharmony_ciint32_t DistributedSchedService::StopSyncMissionsFromRemote(const CallerInfo& callerInfo)
2910686862fbSopenharmony_ci{
2911686862fbSopenharmony_ci    DistributedSchedMissionManager::GetInstance().StopSyncMissionsFromRemote(callerInfo.sourceDeviceId);
2912686862fbSopenharmony_ci    return ERR_NONE;
2913686862fbSopenharmony_ci}
2914686862fbSopenharmony_ci
2915686862fbSopenharmony_ciint32_t DistributedSchedService::SetMissionContinueState(int32_t missionId, const AAFwk::ContinueState &state)
2916686862fbSopenharmony_ci{
2917686862fbSopenharmony_ci    return DMSContinueSendMgr::GetInstance().SetMissionContinueState(missionId, state);
2918686862fbSopenharmony_ci}
2919686862fbSopenharmony_ci#endif
2920686862fbSopenharmony_ci
2921686862fbSopenharmony_ciint32_t DistributedSchedService::RegisterDSchedEventListener(const DSchedEventType& type,
2922686862fbSopenharmony_ci    const sptr<IRemoteObject>& callback)
2923686862fbSopenharmony_ci{
2924686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr || collaborateCbMgr_ == nullptr) {
2925686862fbSopenharmony_ci        HILOGE("object null!");
2926686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2927686862fbSopenharmony_ci    }
2928686862fbSopenharmony_ci    bool ret = false;
2929686862fbSopenharmony_ci    switch (type) {
2930686862fbSopenharmony_ci        case DMS_CONTINUE:
2931686862fbSopenharmony_ci            ret = dschedContinuation_->PushCallback(callback);
2932686862fbSopenharmony_ci            break;
2933686862fbSopenharmony_ci        case DMS_COLLABORATION:
2934686862fbSopenharmony_ci            ret = collaborateCbMgr_->PushCallback(callback);
2935686862fbSopenharmony_ci            break;
2936686862fbSopenharmony_ci        case DMS_ALL:
2937686862fbSopenharmony_ci            ret = dschedContinuation_->PushCallback(callback) && collaborateCbMgr_->PushCallback(callback);
2938686862fbSopenharmony_ci            break;
2939686862fbSopenharmony_ci        default:
2940686862fbSopenharmony_ci            HILOGE("Not support register Dms event listener with event type: %{public}d.", type);
2941686862fbSopenharmony_ci            return INVALID_PARAMETERS_ERR;
2942686862fbSopenharmony_ci    }
2943686862fbSopenharmony_ci
2944686862fbSopenharmony_ci    if (!ret) {
2945686862fbSopenharmony_ci        HILOGE("The callback does not exist, type: %{public}d", type);
2946686862fbSopenharmony_ci        return CALLBACK_HAS_NOT_REGISTERED;
2947686862fbSopenharmony_ci    }
2948686862fbSopenharmony_ci    HILOGD("Push %{public}d callback success.", type);
2949686862fbSopenharmony_ci    return ERR_OK;
2950686862fbSopenharmony_ci}
2951686862fbSopenharmony_ci
2952686862fbSopenharmony_ciint32_t DistributedSchedService::UnRegisterDSchedEventListener(const DSchedEventType& type,
2953686862fbSopenharmony_ci    const sptr<IRemoteObject>& callback)
2954686862fbSopenharmony_ci{
2955686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr || collaborateCbMgr_ == nullptr) {
2956686862fbSopenharmony_ci        HILOGE("object null!");
2957686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2958686862fbSopenharmony_ci    }
2959686862fbSopenharmony_ci    bool result = 0;
2960686862fbSopenharmony_ci    switch (type) {
2961686862fbSopenharmony_ci        case DMS_CONTINUE:
2962686862fbSopenharmony_ci            result = dschedContinuation_->CleanupCallback(callback);
2963686862fbSopenharmony_ci            break;
2964686862fbSopenharmony_ci        case DMS_COLLABORATION:
2965686862fbSopenharmony_ci            result = collaborateCbMgr_->CleanupCallback(callback);
2966686862fbSopenharmony_ci            break;
2967686862fbSopenharmony_ci        case DMS_ALL:
2968686862fbSopenharmony_ci            result = dschedContinuation_->CleanupCallback(callback) && collaborateCbMgr_->CleanupCallback(callback);
2969686862fbSopenharmony_ci            break;
2970686862fbSopenharmony_ci        default:
2971686862fbSopenharmony_ci            break;
2972686862fbSopenharmony_ci    }
2973686862fbSopenharmony_ci
2974686862fbSopenharmony_ci    if (!result) {
2975686862fbSopenharmony_ci        HILOGI("The callback does not exist,type: %{public}d", type);
2976686862fbSopenharmony_ci    } else {
2977686862fbSopenharmony_ci        HILOGI("Clearing the callback succeeded.");
2978686862fbSopenharmony_ci    }
2979686862fbSopenharmony_ci    return 0;
2980686862fbSopenharmony_ci}
2981686862fbSopenharmony_ci
2982686862fbSopenharmony_ciint32_t DistributedSchedService::GetContinueInfo(std::string& dstNetworkId, std::string& srcNetworkId)
2983686862fbSopenharmony_ci{
2984686862fbSopenharmony_ci    HILOGI("GetContinueInfo called");
2985686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
2986686862fbSopenharmony_ci        HILOGE("continuation object null!");
2987686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
2988686862fbSopenharmony_ci    }
2989686862fbSopenharmony_ci    dstNetworkId = dschedContinuation_->continueInfo_.dstNetworkId_;
2990686862fbSopenharmony_ci    srcNetworkId = dschedContinuation_->continueInfo_.srcNetworkId_;
2991686862fbSopenharmony_ci    HILOGI("GetContinueInfo dstNetworkId: %{public}s, srcNetworkId: %{public}s",
2992686862fbSopenharmony_ci        GetAnonymStr(dstNetworkId).c_str(), GetAnonymStr(srcNetworkId).c_str());
2993686862fbSopenharmony_ci    return 0;
2994686862fbSopenharmony_ci}
2995686862fbSopenharmony_ci
2996686862fbSopenharmony_ciint32_t DistributedSchedService::GetDSchedEventInfo(const DSchedEventType &type, std::vector<EventNotify> &events)
2997686862fbSopenharmony_ci{
2998686862fbSopenharmony_ci    int32_t callingUid = CheckCallingUid() ? DEFAULT_REQUEST_CODE : IPCSkeleton::GetCallingUid();
2999686862fbSopenharmony_ci    HILOGI("GetDSchedEventInfo called, uid %{public}d, dms eventType %{public}d.", callingUid, type);
3000686862fbSopenharmony_ci    switch (type) {
3001686862fbSopenharmony_ci        case DMS_CONTINUE:
3002686862fbSopenharmony_ci            GetContinueEventInfo(callingUid, events);
3003686862fbSopenharmony_ci            break;
3004686862fbSopenharmony_ci        case DMS_COLLABORATION:
3005686862fbSopenharmony_ci            GetCollaborateEventInfo(callingUid, events);
3006686862fbSopenharmony_ci            break;
3007686862fbSopenharmony_ci        case DMS_ALL:
3008686862fbSopenharmony_ci            GetContinueEventInfo(callingUid, events);
3009686862fbSopenharmony_ci            GetCollaborateEventInfo(callingUid, events);
3010686862fbSopenharmony_ci            break;
3011686862fbSopenharmony_ci        default:
3012686862fbSopenharmony_ci            HILOGI("Get dms event info not support eventType %{public}d.", type);
3013686862fbSopenharmony_ci            return INVALID_PARAMETERS_ERR;
3014686862fbSopenharmony_ci    }
3015686862fbSopenharmony_ci    HILOGI("GetDSchedEventInfo end, uid %{public}d, eventType %{public}d, events size %{public}zu.",
3016686862fbSopenharmony_ci        callingUid, type, events.size());
3017686862fbSopenharmony_ci    return ERR_OK;
3018686862fbSopenharmony_ci}
3019686862fbSopenharmony_ci
3020686862fbSopenharmony_cibool DistributedSchedService::CheckCallingUid()
3021686862fbSopenharmony_ci{
3022686862fbSopenharmony_ci    // never allow non-system uid for distributed request
3023686862fbSopenharmony_ci    auto callingUid = IPCSkeleton::GetCallingUid();
3024686862fbSopenharmony_ci    return callingUid < HID_HAP;
3025686862fbSopenharmony_ci}
3026686862fbSopenharmony_ci
3027686862fbSopenharmony_civoid DistributedSchedService::GetContinueEventInfo(int32_t callingUid, std::vector<EventNotify> &events)
3028686862fbSopenharmony_ci{
3029686862fbSopenharmony_ci    if (callingUid == DEFAULT_REQUEST_CODE && dschedContinuation_ != nullptr) {
3030686862fbSopenharmony_ci        events.emplace_back(dschedContinuation_->continueEvent_);
3031686862fbSopenharmony_ci        return;
3032686862fbSopenharmony_ci    }
3033686862fbSopenharmony_ci
3034686862fbSopenharmony_ci    std::vector<std::string> bundleNames;
3035686862fbSopenharmony_ci    if (!BundleManagerInternal::GetBundleNameListFromBms(callingUid, bundleNames)) {
3036686862fbSopenharmony_ci        HILOGE("Get bundle name from Bms failed");
3037686862fbSopenharmony_ci        return;
3038686862fbSopenharmony_ci    }
3039686862fbSopenharmony_ci    for (const auto &bundleName : bundleNames) {
3040686862fbSopenharmony_ci        HILOGD("Get bundle name %{public}s from Bms.", bundleName.c_str());
3041686862fbSopenharmony_ci        if (bundleName == dschedContinuation_->continueEvent_.srcBundleName_ ||
3042686862fbSopenharmony_ci            bundleName == dschedContinuation_->continueEvent_.destBundleName_) {
3043686862fbSopenharmony_ci            events.emplace_back(dschedContinuation_->continueEvent_);
3044686862fbSopenharmony_ci        }
3045686862fbSopenharmony_ci    }
3046686862fbSopenharmony_ci}
3047686862fbSopenharmony_ci
3048686862fbSopenharmony_civoid DistributedSchedService::GetCollaborateEventInfo(int32_t callingUid, std::vector<EventNotify> &events)
3049686862fbSopenharmony_ci{
3050686862fbSopenharmony_ci    if (callingUid == DEFAULT_REQUEST_CODE) {
3051686862fbSopenharmony_ci        GetCollaborateEventsByCallers(callingUid, "", events);
3052686862fbSopenharmony_ci        GetCollaborateEventsByCallees(callingUid, "", events);
3053686862fbSopenharmony_ci        return;
3054686862fbSopenharmony_ci    }
3055686862fbSopenharmony_ci
3056686862fbSopenharmony_ci    std::string callingBundleName;
3057686862fbSopenharmony_ci    if (!BundleManagerInternal::GetSpecifyBundleNameFromBms(callingUid, callingBundleName)) {
3058686862fbSopenharmony_ci        HILOGE("Get specify bundle name for from Bms fail, uid %{public}d.", callingUid);
3059686862fbSopenharmony_ci        return;
3060686862fbSopenharmony_ci    }
3061686862fbSopenharmony_ci    GetCollaborateEventsByCallers(callingUid, callingBundleName, events);
3062686862fbSopenharmony_ci    GetCollaborateEventsByCallees(callingUid, callingBundleName, events);
3063686862fbSopenharmony_ci}
3064686862fbSopenharmony_ci
3065686862fbSopenharmony_civoid DistributedSchedService::GetCollaborateEventsByCallers(int32_t callingUid, const std::string &callingBundleName,
3066686862fbSopenharmony_ci    std::vector<EventNotify> &events)
3067686862fbSopenharmony_ci{
3068686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(callerLock_);
3069686862fbSopenharmony_ci    for (const auto &iter : callerMap_) {
3070686862fbSopenharmony_ci        for (const auto &connectSession : iter.second) {
3071686862fbSopenharmony_ci            auto bundleNames = connectSession.GetCallerInfo().bundleNames;
3072686862fbSopenharmony_ci            if (callingUid != DEFAULT_REQUEST_CODE && callingUid != connectSession.GetCallerInfo().uid &&
3073686862fbSopenharmony_ci                std::count(bundleNames.begin(), bundleNames.end(), callingBundleName) == 0) {
3074686862fbSopenharmony_ci                HILOGE("Connect session callerInfo uid %{public}d is different from callingUid %{public}d, "
3075686862fbSopenharmony_ci                    "callingbundle %{public}s.", connectSession.GetCallerInfo().uid,
3076686862fbSopenharmony_ci                    callingUid, callingBundleName.c_str());
3077686862fbSopenharmony_ci                continue;
3078686862fbSopenharmony_ci            }
3079686862fbSopenharmony_ci            for (const auto &element : connectSession.GetElementsList()) {
3080686862fbSopenharmony_ci                EventNotify tempEvent;
3081686862fbSopenharmony_ci                GetCurSrcCollaborateEvent(connectSession.GetCallerInfo(), element,
3082686862fbSopenharmony_ci                    DMS_DSCHED_EVENT_PROCESSING, ERR_OK, tempEvent);
3083686862fbSopenharmony_ci                events.emplace_back(tempEvent);
3084686862fbSopenharmony_ci            }
3085686862fbSopenharmony_ci        }
3086686862fbSopenharmony_ci    }
3087686862fbSopenharmony_ci}
3088686862fbSopenharmony_ci
3089686862fbSopenharmony_civoid DistributedSchedService::GetCollaborateEventsByCallees(int32_t callingUid, const std::string &callingBundleName,
3090686862fbSopenharmony_ci    std::vector<EventNotify> &events)
3091686862fbSopenharmony_ci{
3092686862fbSopenharmony_ci    std::lock_guard<std::mutex> autoLock(calleeLock_);
3093686862fbSopenharmony_ci    for (const auto &iter : calleeMap_) {
3094686862fbSopenharmony_ci        if (callingUid != DEFAULT_REQUEST_CODE && callingBundleName != iter.second.element.GetBundleName()) {
3095686862fbSopenharmony_ci            HILOGE("Connect session calleeInfo destBundleName %{public}s is different from "
3096686862fbSopenharmony_ci                "callingBundleName %{public}s, callingUid %{public}d.", iter.second.element.GetBundleName().c_str(),
3097686862fbSopenharmony_ci                callingBundleName.c_str(), callingUid);
3098686862fbSopenharmony_ci            continue;
3099686862fbSopenharmony_ci        }
3100686862fbSopenharmony_ci
3101686862fbSopenharmony_ci        EventNotify tempEvent;
3102686862fbSopenharmony_ci        GetCurDestCollaborateEvent(iter.second.callerInfo, iter.second.element,
3103686862fbSopenharmony_ci            DMS_DSCHED_EVENT_PROCESSING, ERR_OK, tempEvent);
3104686862fbSopenharmony_ci        events.emplace_back(tempEvent);
3105686862fbSopenharmony_ci    }
3106686862fbSopenharmony_ci}
3107686862fbSopenharmony_ci
3108686862fbSopenharmony_civoid CallerDeathRecipient::OnRemoteDied(const wptr<IRemoteObject>& remote)
3109686862fbSopenharmony_ci{
3110686862fbSopenharmony_ci    HILOGI("CallerDeathRecipient OnRemoteDied called");
3111686862fbSopenharmony_ci    DistributedSchedAdapter::GetInstance().ProcessCallerDied(remote.promote(), deviceType_);
3112686862fbSopenharmony_ci}
3113686862fbSopenharmony_ci
3114686862fbSopenharmony_ciint32_t DistributedSchedService::SetCallerInfo(
3115686862fbSopenharmony_ci    int32_t callerUid, std::string localDeviceId, uint32_t accessToken, CallerInfo& callerInfo)
3116686862fbSopenharmony_ci{
3117686862fbSopenharmony_ci    callerInfo.uid = callerUid;
3118686862fbSopenharmony_ci    callerInfo.callerType = CALLER_TYPE_HARMONY;
3119686862fbSopenharmony_ci    callerInfo.sourceDeviceId = localDeviceId;
3120686862fbSopenharmony_ci    callerInfo.accessToken = accessToken;
3121686862fbSopenharmony_ci    if (!BundleManagerInternal::GetCallerAppIdFromBms(callerInfo.uid, callerInfo.callerAppId)) {
3122686862fbSopenharmony_ci        HILOGE("GetCallerAppIdFromBms failed");
3123686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3124686862fbSopenharmony_ci    }
3125686862fbSopenharmony_ci    if (!BundleManagerInternal::GetBundleNameListFromBms(callerInfo.uid, callerInfo.bundleNames)) {
3126686862fbSopenharmony_ci        HILOGE("GetBundleNameListFromBms failed");
3127686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3128686862fbSopenharmony_ci    }
3129686862fbSopenharmony_ci    callerInfo.extraInfoJson[DMS_VERSION_ID] = DMS_VERSION;
3130686862fbSopenharmony_ci    return ERR_OK;
3131686862fbSopenharmony_ci}
3132686862fbSopenharmony_ci
3133686862fbSopenharmony_ciint32_t DistributedSchedService::StartRemoteFreeInstall(const OHOS::AAFwk::Want& want, int32_t callerUid,
3134686862fbSopenharmony_ci    int32_t requestCode, uint32_t accessToken, const sptr<IRemoteObject>& callback)
3135686862fbSopenharmony_ci{
3136686862fbSopenharmony_ci    HILOGI("called");
3137686862fbSopenharmony_ci    std::string localDeviceId;
3138686862fbSopenharmony_ci    std::string deviceId = want.GetElement().GetDeviceID();
3139686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || !CheckDeviceId(localDeviceId, deviceId)) {
3140686862fbSopenharmony_ci        HILOGE("check deviceId failed");
3141686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3142686862fbSopenharmony_ci    }
3143686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
3144686862fbSopenharmony_ci    if (remoteDms == nullptr) {
3145686862fbSopenharmony_ci        HILOGE("get remoteDms failed");
3146686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3147686862fbSopenharmony_ci    }
3148686862fbSopenharmony_ci    if (dmsCallbackTask_ == nullptr) {
3149686862fbSopenharmony_ci        HILOGE("callbackTask object null!");
3150686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3151686862fbSopenharmony_ci    }
3152686862fbSopenharmony_ci    int64_t taskId = dmsCallbackTask_->GenerateTaskId();
3153686862fbSopenharmony_ci    LaunchType launchType = LaunchType::FREEINSTALL_START;
3154686862fbSopenharmony_ci    if (((want.GetFlags() & AAFwk::Want::FLAG_ABILITY_CONTINUATION) != 0)) {
3155686862fbSopenharmony_ci        launchType = LaunchType::FREEINSTALL_CONTINUE;
3156686862fbSopenharmony_ci    }
3157686862fbSopenharmony_ci    if (dmsCallbackTask_->PushCallback(taskId, callback, deviceId, launchType, want) != ERR_OK) {
3158686862fbSopenharmony_ci        HILOGE("Push callback failed!");
3159686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3160686862fbSopenharmony_ci    }
3161686862fbSopenharmony_ci    if (launchType == LaunchType::FREEINSTALL_CONTINUE) {
3162686862fbSopenharmony_ci        dmsCallbackTask_->SetContinuationMissionMap(taskId, want.GetIntParam("sessionId", -1));
3163686862fbSopenharmony_ci    }
3164686862fbSopenharmony_ci
3165686862fbSopenharmony_ci    CallerInfo callerInfo;
3166686862fbSopenharmony_ci    if (SetCallerInfo(callerUid, localDeviceId, accessToken, callerInfo) != ERR_OK) {
3167686862fbSopenharmony_ci        HILOGE("SetCallerInfo failed");
3168686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3169686862fbSopenharmony_ci    }
3170686862fbSopenharmony_ci    AccountInfo accountInfo = {};
3171686862fbSopenharmony_ci    if ((DistributedSchedPermission::GetInstance().GetAccountInfo(deviceId, callerInfo, accountInfo)) != ERR_OK) {
3172686862fbSopenharmony_ci        HILOGE("GetAccountInfo failed");
3173686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3174686862fbSopenharmony_ci    }
3175686862fbSopenharmony_ci    AAFwk::Want* newWant = const_cast<Want*>(&want);
3176686862fbSopenharmony_ci    newWant->SetParam(DMS_SRC_NETWORK_ID, localDeviceId);
3177686862fbSopenharmony_ci    FreeInstallInfo info = {*newWant, requestCode, callerInfo, accountInfo};
3178686862fbSopenharmony_ci    int32_t result = remoteDms->StartFreeInstallFromRemote(info, taskId);
3179686862fbSopenharmony_ci    if (result != ERR_OK) {
3180686862fbSopenharmony_ci        HILOGE("result = %{public}d", result);
3181686862fbSopenharmony_ci        CallbackTaskItem item = dmsCallbackTask_->PopCallback(taskId);
3182686862fbSopenharmony_ci        NotifyFreeInstallResult(item, result);
3183686862fbSopenharmony_ci    }
3184686862fbSopenharmony_ci    return result;
3185686862fbSopenharmony_ci}
3186686862fbSopenharmony_ci
3187686862fbSopenharmony_ciint32_t DistributedSchedService::StartFreeInstallFromRemote(const FreeInstallInfo& info, int64_t taskId)
3188686862fbSopenharmony_ci{
3189686862fbSopenharmony_ci    HILOGI("begin taskId : %{public} " PRId64 ". ", taskId);
3190686862fbSopenharmony_ci    std::string localDeviceId;
3191686862fbSopenharmony_ci    std::string deviceId = info.want.GetElement().GetDeviceID();
3192686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) ||
3193686862fbSopenharmony_ci        !CheckDeviceIdFromRemote(localDeviceId, deviceId, info.callerInfo.sourceDeviceId)) {
3194686862fbSopenharmony_ci        HILOGE("check deviceId failed");
3195686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3196686862fbSopenharmony_ci    }
3197686862fbSopenharmony_ci
3198686862fbSopenharmony_ci    ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->Connect();
3199686862fbSopenharmony_ci    if (err != ERR_OK) {
3200686862fbSopenharmony_ci        HILOGE("connect ability server failed %{public}d", err);
3201686862fbSopenharmony_ci        return err;
3202686862fbSopenharmony_ci    }
3203686862fbSopenharmony_ci    int32_t activeAccountId = -1;
3204686862fbSopenharmony_ci    err = QueryOsAccount(activeAccountId);
3205686862fbSopenharmony_ci    if (err != ERR_OK) {
3206686862fbSopenharmony_ci        return err;
3207686862fbSopenharmony_ci    }
3208686862fbSopenharmony_ci
3209686862fbSopenharmony_ci    sptr<DmsFreeInstallCallback> callback(new DmsFreeInstallCallback(taskId, info));
3210686862fbSopenharmony_ci    err = AAFwk::AbilityManagerClient::GetInstance()->FreeInstallAbilityFromRemote(
3211686862fbSopenharmony_ci        info.want, callback, activeAccountId, info.requestCode);
3212686862fbSopenharmony_ci    if (err != ERR_OK) {
3213686862fbSopenharmony_ci        HILOGE("FreeInstallAbilityFromRemote failed %{public}d", err);
3214686862fbSopenharmony_ci    }
3215686862fbSopenharmony_ci    return err;
3216686862fbSopenharmony_ci}
3217686862fbSopenharmony_ci
3218686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyCompleteFreeInstall(
3219686862fbSopenharmony_ci    const FreeInstallInfo& info, int64_t taskId, int32_t resultCode)
3220686862fbSopenharmony_ci{
3221686862fbSopenharmony_ci    HILOGI("taskId = %{public}" PRId64 ".", taskId);
3222686862fbSopenharmony_ci    if (taskId <= 0) {
3223686862fbSopenharmony_ci        HILOGE("taskId invalid!");
3224686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3225686862fbSopenharmony_ci    }
3226686862fbSopenharmony_ci    if (resultCode != ERR_OK) {
3227686862fbSopenharmony_ci        HILOGE("free install failed, resultCode : %{public}d", resultCode);
3228686862fbSopenharmony_ci        return HandleRemoteNotify(info, taskId, resultCode);
3229686862fbSopenharmony_ci    }
3230686862fbSopenharmony_ci    int32_t result = StartLocalAbility(info, taskId, resultCode);
3231686862fbSopenharmony_ci    return HandleRemoteNotify(info, taskId, result);
3232686862fbSopenharmony_ci}
3233686862fbSopenharmony_ci
3234686862fbSopenharmony_ciint32_t DistributedSchedService::StartLocalAbility(const FreeInstallInfo& info, int64_t taskId, int32_t resultCode)
3235686862fbSopenharmony_ci{
3236686862fbSopenharmony_ci    std::string localDeviceId;
3237686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId)) {
3238686862fbSopenharmony_ci        HILOGE("get local deviceId failed");
3239686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3240686862fbSopenharmony_ci    }
3241686862fbSopenharmony_ci    int32_t result = CheckTargetPermission(info.want, info.callerInfo, info.accountInfo, START_PERMISSION, true);
3242686862fbSopenharmony_ci    if (result != ERR_OK) {
3243686862fbSopenharmony_ci        HILOGE("CheckTargetPermission failed!!");
3244686862fbSopenharmony_ci        return result;
3245686862fbSopenharmony_ci    }
3246686862fbSopenharmony_ci
3247686862fbSopenharmony_ci    AAFwk::Want* want = const_cast<Want*>(&info.want);
3248686862fbSopenharmony_ci    want->RemoveFlags(OHOS::AAFwk::Want::FLAG_INSTALL_ON_DEMAND);
3249686862fbSopenharmony_ci    return StartAbility(*want, info.requestCode);
3250686862fbSopenharmony_ci}
3251686862fbSopenharmony_ci
3252686862fbSopenharmony_ciint32_t DistributedSchedService::StartAbility(const OHOS::AAFwk::Want& want, int32_t requestCode)
3253686862fbSopenharmony_ci{
3254686862fbSopenharmony_ci    if (!dataShareManager.IsCurrentContinueSwitchOn()) {
3255686862fbSopenharmony_ci        HILOGE("ContinueSwitch status is off");
3256686862fbSopenharmony_ci        return DMS_PERMISSION_DENIED;
3257686862fbSopenharmony_ci    }
3258686862fbSopenharmony_ci    ErrCode err = AAFwk::AbilityManagerClient::GetInstance()->Connect();
3259686862fbSopenharmony_ci    if (err != ERR_OK) {
3260686862fbSopenharmony_ci        HILOGE("connect ability server failed %{public}d", err);
3261686862fbSopenharmony_ci        return err;
3262686862fbSopenharmony_ci    }
3263686862fbSopenharmony_ci    int32_t activeAccountId = -1;
3264686862fbSopenharmony_ci    err = QueryOsAccount(activeAccountId);
3265686862fbSopenharmony_ci    if (err != ERR_OK) {
3266686862fbSopenharmony_ci        return err;
3267686862fbSopenharmony_ci    }
3268686862fbSopenharmony_ci    if (want.GetBoolParam(Want::PARAM_RESV_FOR_RESULT, false)) {
3269686862fbSopenharmony_ci        HILOGI("StartAbilityForResult start, flag is %{public}d", want.GetFlags());
3270686862fbSopenharmony_ci        sptr<IRemoteObject> dmsTokenCallback(new DmsTokenCallback());
3271686862fbSopenharmony_ci        err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, dmsTokenCallback, requestCode,
3272686862fbSopenharmony_ci            activeAccountId);
3273686862fbSopenharmony_ci    } else {
3274686862fbSopenharmony_ci        HILOGI("StartAbility start, flag is %{public}d", want.GetFlags());
3275686862fbSopenharmony_ci        if (DmsContinueTime::GetInstance().GetPull()) {
3276686862fbSopenharmony_ci            int64_t begin = GetTickCount();
3277686862fbSopenharmony_ci            DmsContinueTime::GetInstance().SetDurationBegin(DMSDURATION_STARTABILITY, begin);
3278686862fbSopenharmony_ci        }
3279686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDstAbilityName(want.GetElement().GetAbilityName());
3280686862fbSopenharmony_ci        DmsContinueTime::GetInstance().SetDstBundleName(want.GetElement().GetBundleName());
3281686862fbSopenharmony_ci        DmsRadar::GetInstance().ClickIconDmsStartAbility("StartAbility", err);
3282686862fbSopenharmony_ci        err = AAFwk::AbilityManagerClient::GetInstance()->StartAbility(want, requestCode, activeAccountId);
3283686862fbSopenharmony_ci    }
3284686862fbSopenharmony_ci    if (err != ERR_OK) {
3285686862fbSopenharmony_ci        HILOGE("StartAbility failed %{public}d", err);
3286686862fbSopenharmony_ci    }
3287686862fbSopenharmony_ci    return err;
3288686862fbSopenharmony_ci}
3289686862fbSopenharmony_ci
3290686862fbSopenharmony_ciint32_t DistributedSchedService::HandleRemoteNotify(const FreeInstallInfo& info, int64_t taskId, int32_t resultCode)
3291686862fbSopenharmony_ci{
3292686862fbSopenharmony_ci    HILOGI("begin taskId = %{public}" PRId64 ", resultCode = %{public}d", taskId, resultCode);
3293686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(info.callerInfo.sourceDeviceId);
3294686862fbSopenharmony_ci    if (remoteDms == nullptr) {
3295686862fbSopenharmony_ci        HILOGE("get remote dms null!");
3296686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3297686862fbSopenharmony_ci    }
3298686862fbSopenharmony_ci    if (taskId <= 0) {
3299686862fbSopenharmony_ci        HILOGE("taskId invalid!");
3300686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3301686862fbSopenharmony_ci    }
3302686862fbSopenharmony_ci    return remoteDms->NotifyCompleteFreeInstallFromRemote(taskId, resultCode);
3303686862fbSopenharmony_ci}
3304686862fbSopenharmony_ci
3305686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyCompleteFreeInstallFromRemote(int64_t taskId, int32_t resultCode)
3306686862fbSopenharmony_ci{
3307686862fbSopenharmony_ci    HILOGI("begin taskId = %{public}" PRId64 ", resultCode = %{public}d", taskId, resultCode);
3308686862fbSopenharmony_ci    if (dmsCallbackTask_ == nullptr || dschedContinuation_ == nullptr) {
3309686862fbSopenharmony_ci        HILOGE("callbackTask object null!");
3310686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3311686862fbSopenharmony_ci    }
3312686862fbSopenharmony_ci
3313686862fbSopenharmony_ci    LaunchType launchType = dmsCallbackTask_->GetLaunchType(taskId);
3314686862fbSopenharmony_ci    CallbackTaskItem item = dmsCallbackTask_->PopCallback(taskId);
3315686862fbSopenharmony_ci    if (launchType == LaunchType::FREEINSTALL_START) {
3316686862fbSopenharmony_ci        return NotifyFreeInstallResult(item, resultCode);
3317686862fbSopenharmony_ci    }
3318686862fbSopenharmony_ci
3319686862fbSopenharmony_ci    if (resultCode == ERR_OK) {
3320686862fbSopenharmony_ci        HILOGD("continue free install success, waiting for continue result callback.");
3321686862fbSopenharmony_ci        dmsCallbackTask_->PopContinuationMissionMap(taskId);
3322686862fbSopenharmony_ci        return ERR_OK;
3323686862fbSopenharmony_ci    }
3324686862fbSopenharmony_ci
3325686862fbSopenharmony_ci    int32_t missionId = dmsCallbackTask_->GetContinuaionMissionId(taskId);
3326686862fbSopenharmony_ci    NotifyContinuationCallbackResult(missionId, CONTINUE_FREE_INSTALL_FAILED);
3327686862fbSopenharmony_ci    dmsCallbackTask_->PopContinuationMissionMap(taskId);
3328686862fbSopenharmony_ci    return ERR_OK;
3329686862fbSopenharmony_ci}
3330686862fbSopenharmony_ci
3331686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyFreeInstallResult(const CallbackTaskItem item, int32_t resultCode)
3332686862fbSopenharmony_ci{
3333686862fbSopenharmony_ci    HILOGI("taskId : %{public} " PRId64 ". ", item.taskId);
3334686862fbSopenharmony_ci    if (item.callback == nullptr) {
3335686862fbSopenharmony_ci        HILOGE("item callback null!");
3336686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3337686862fbSopenharmony_ci    }
3338686862fbSopenharmony_ci    MessageParcel data;
3339686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(ATOMIC_SERVICE_STATUS_CALLBACK_TOKEN)) {
3340686862fbSopenharmony_ci        HILOGE("Write interface token failed.");
3341686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3342686862fbSopenharmony_ci    }
3343686862fbSopenharmony_ci
3344686862fbSopenharmony_ci    if (!data.WriteInt32(resultCode)) {
3345686862fbSopenharmony_ci        HILOGE("Write resultCode error.");
3346686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3347686862fbSopenharmony_ci    }
3348686862fbSopenharmony_ci
3349686862fbSopenharmony_ci    if (!data.WriteParcelable(&item.want)) {
3350686862fbSopenharmony_ci        HILOGE("Write want error.");
3351686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3352686862fbSopenharmony_ci    }
3353686862fbSopenharmony_ci
3354686862fbSopenharmony_ci    int32_t userId = 0;
3355686862fbSopenharmony_ci    if (!data.WriteInt32(userId)) {
3356686862fbSopenharmony_ci        HILOGE("Write userId error.");
3357686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3358686862fbSopenharmony_ci    }
3359686862fbSopenharmony_ci
3360686862fbSopenharmony_ci    MessageParcel reply;
3361686862fbSopenharmony_ci    MessageOption option;
3362686862fbSopenharmony_ci    return item.callback->SendRequest(IASS_CALLBACK_ON_REMOTE_FREE_INSTALL_DONE, data, reply, option);
3363686862fbSopenharmony_ci}
3364686862fbSopenharmony_ci
3365686862fbSopenharmony_cibool DistributedSchedService::RegisterAppStateObserver(const OHOS::AAFwk::Want& want, const CallerInfo& callerInfo,
3366686862fbSopenharmony_ci    const sptr<IRemoteObject>& srcConnect, const sptr<IRemoteObject>& callbackWrapper)
3367686862fbSopenharmony_ci{
3368686862fbSopenharmony_ci    HILOGD("register app state observer called");
3369686862fbSopenharmony_ci    int32_t connectToken = want.GetIntParam(DMS_CONNECT_TOKEN, DEFAULT_DMS_CONNECT_TOKEN);
3370686862fbSopenharmony_ci    HILOGD("Get connectToken = %{private}s", GetAnonymStr(std::to_string(connectToken)).c_str());
3371686862fbSopenharmony_ci    if (connectToken == DEFAULT_DMS_CONNECT_TOKEN) {
3372686862fbSopenharmony_ci        return false;
3373686862fbSopenharmony_ci    }
3374686862fbSopenharmony_ci    sptr<AppExecFwk::IAppMgr> appObject = GetAppManager();
3375686862fbSopenharmony_ci    if (appObject == nullptr) {
3376686862fbSopenharmony_ci        HILOGE("failed to get app manager service");
3377686862fbSopenharmony_ci        return false;
3378686862fbSopenharmony_ci    }
3379686862fbSopenharmony_ci    sptr<AppStateObserver> appStateObserver;
3380686862fbSopenharmony_ci    std::string bundleName = want.GetElement().GetBundleName();
3381686862fbSopenharmony_ci    {
3382686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(registerMutex_);
3383686862fbSopenharmony_ci        if (!bundleNameMap_.count(bundleName)) {
3384686862fbSopenharmony_ci            std::vector<std::string> bundleNameList = {bundleName};
3385686862fbSopenharmony_ci            appStateObserver = sptr<AppStateObserver>(new (std::nothrow) AppStateObserver());
3386686862fbSopenharmony_ci            bundleNameMap_[bundleName] = appStateObserver;
3387686862fbSopenharmony_ci            int ret = appObject->RegisterApplicationStateObserver(appStateObserver, bundleNameList);
3388686862fbSopenharmony_ci            if (ret != ERR_OK) {
3389686862fbSopenharmony_ci                HILOGE("failed to register application state observer, ret = %{public}d", ret);
3390686862fbSopenharmony_ci                return false;
3391686862fbSopenharmony_ci            }
3392686862fbSopenharmony_ci        }
3393686862fbSopenharmony_ci        appStateObserver = bundleNameMap_[bundleName];
3394686862fbSopenharmony_ci    }
3395686862fbSopenharmony_ci    HILOGI("register application state observer success");
3396686862fbSopenharmony_ci    {
3397686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(observerLock_);
3398686862fbSopenharmony_ci        Want* newWant = const_cast<Want*>(&want);
3399686862fbSopenharmony_ci        newWant->RemoveParam(DMS_MISSION_ID);
3400686862fbSopenharmony_ci        newWant->RemoveParam(DMS_CONNECT_TOKEN);
3401686862fbSopenharmony_ci        observerMap_[callbackWrapper] = {appStateObserver, callerInfo.sourceDeviceId, connectToken,
3402686862fbSopenharmony_ci            want.GetElement().GetBundleName(), want.GetElement().GetAbilityName(), srcConnect};
3403686862fbSopenharmony_ci        HILOGI("add observerMap_ success");
3404686862fbSopenharmony_ci    }
3405686862fbSopenharmony_ci    return true;
3406686862fbSopenharmony_ci}
3407686862fbSopenharmony_ci
3408686862fbSopenharmony_civoid DistributedSchedService::UnregisterAppStateObserver(const sptr<IRemoteObject>& callbackWrapper)
3409686862fbSopenharmony_ci{
3410686862fbSopenharmony_ci    HILOGD("unregister app state observer called");
3411686862fbSopenharmony_ci    if (callbackWrapper == nullptr) {
3412686862fbSopenharmony_ci        HILOGD("callbackWrapper is nullptr");
3413686862fbSopenharmony_ci        return;
3414686862fbSopenharmony_ci    }
3415686862fbSopenharmony_ci    bool unRegisterFlag = true;
3416686862fbSopenharmony_ci    std::string bundleName;
3417686862fbSopenharmony_ci    sptr<AppStateObserver> appStateObserver;
3418686862fbSopenharmony_ci    {
3419686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(observerLock_);
3420686862fbSopenharmony_ci        auto it = observerMap_.find(callbackWrapper);
3421686862fbSopenharmony_ci        if (it == observerMap_.end()) {
3422686862fbSopenharmony_ci            HILOGE("state observer not found");
3423686862fbSopenharmony_ci            return;
3424686862fbSopenharmony_ci        }
3425686862fbSopenharmony_ci        appStateObserver = it->second.appStateObserver;
3426686862fbSopenharmony_ci        bundleName = it->second.dstBundleName;
3427686862fbSopenharmony_ci        observerMap_.erase(it);
3428686862fbSopenharmony_ci        for (auto iter = observerMap_.begin(); iter != observerMap_.end(); iter++) {
3429686862fbSopenharmony_ci            if (iter->second.dstBundleName == bundleName) {
3430686862fbSopenharmony_ci                unRegisterFlag = false;
3431686862fbSopenharmony_ci                break;
3432686862fbSopenharmony_ci            }
3433686862fbSopenharmony_ci        }
3434686862fbSopenharmony_ci        HILOGI("remove app state observer success");
3435686862fbSopenharmony_ci    }
3436686862fbSopenharmony_ci    if (unRegisterFlag) {
3437686862fbSopenharmony_ci        {
3438686862fbSopenharmony_ci            std::lock_guard<std::mutex> autoLock(registerMutex_);
3439686862fbSopenharmony_ci            bundleNameMap_.erase(bundleName);
3440686862fbSopenharmony_ci        }
3441686862fbSopenharmony_ci        sptr<AppExecFwk::IAppMgr> appObject = GetAppManager();
3442686862fbSopenharmony_ci        if (appObject == nullptr) {
3443686862fbSopenharmony_ci            HILOGE("failed to get app manager service");
3444686862fbSopenharmony_ci            return;
3445686862fbSopenharmony_ci        }
3446686862fbSopenharmony_ci        int ret = appObject->UnregisterApplicationStateObserver(appStateObserver);
3447686862fbSopenharmony_ci        if (ret != ERR_OK) {
3448686862fbSopenharmony_ci            HILOGE("failed to unregister application state observer, ret = %{public}d", ret);
3449686862fbSopenharmony_ci            return;
3450686862fbSopenharmony_ci        }
3451686862fbSopenharmony_ci    }
3452686862fbSopenharmony_ci    HILOGI("unregister application state observer success");
3453686862fbSopenharmony_ci}
3454686862fbSopenharmony_ci
3455686862fbSopenharmony_cisptr<AppExecFwk::IAppMgr> DistributedSchedService::GetAppManager()
3456686862fbSopenharmony_ci{
3457686862fbSopenharmony_ci    auto samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
3458686862fbSopenharmony_ci    if (samgr == nullptr) {
3459686862fbSopenharmony_ci        HILOGE("system ability manager is nullptr.");
3460686862fbSopenharmony_ci        return nullptr;
3461686862fbSopenharmony_ci    }
3462686862fbSopenharmony_ci
3463686862fbSopenharmony_ci    sptr<AppExecFwk::IAppMgr> appObject =
3464686862fbSopenharmony_ci        iface_cast<AppExecFwk::IAppMgr>(samgr->GetSystemAbility(APP_MGR_SERVICE_ID));
3465686862fbSopenharmony_ci    if (appObject == nullptr) {
3466686862fbSopenharmony_ci        HILOGE("failed to get app manager service");
3467686862fbSopenharmony_ci        return nullptr;
3468686862fbSopenharmony_ci    }
3469686862fbSopenharmony_ci    return appObject;
3470686862fbSopenharmony_ci}
3471686862fbSopenharmony_ci
3472686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyStateChanged(int32_t abilityState, AppExecFwk::ElementName& element,
3473686862fbSopenharmony_ci    const sptr<IRemoteObject>& token)
3474686862fbSopenharmony_ci{
3475686862fbSopenharmony_ci    std::string srcDeviceId = "";
3476686862fbSopenharmony_ci    int32_t connectToken = 0;
3477686862fbSopenharmony_ci    {
3478686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(observerLock_);
3479686862fbSopenharmony_ci        for (auto iter = observerMap_.begin(); iter != observerMap_.end(); iter++) {
3480686862fbSopenharmony_ci            if (iter->second.dstBundleName == element.GetBundleName() &&
3481686862fbSopenharmony_ci                iter->second.dstAbilityName == element.GetAbilityName() && token == iter->second.token) {
3482686862fbSopenharmony_ci                srcDeviceId = iter->second.srcDeviceId;
3483686862fbSopenharmony_ci                connectToken = iter->second.connectToken;
3484686862fbSopenharmony_ci                HILOGD("get srcDeviceId and missionId success");
3485686862fbSopenharmony_ci                break;
3486686862fbSopenharmony_ci            }
3487686862fbSopenharmony_ci        }
3488686862fbSopenharmony_ci    }
3489686862fbSopenharmony_ci    HILOGD("Get connectToken = %{private}s", GetAnonymStr(std::to_string(connectToken)).c_str());
3490686862fbSopenharmony_ci    std::string localDeviceId;
3491686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || !CheckDeviceId(localDeviceId, srcDeviceId)) {
3492686862fbSopenharmony_ci        HILOGE("check deviceId failed");
3493686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3494686862fbSopenharmony_ci    }
3495686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(srcDeviceId);
3496686862fbSopenharmony_ci    if (remoteDms == nullptr) {
3497686862fbSopenharmony_ci        HILOGE("get remoteDms failed");
3498686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3499686862fbSopenharmony_ci    }
3500686862fbSopenharmony_ci    element.SetDeviceID(localDeviceId);
3501686862fbSopenharmony_ci    return remoteDms->NotifyStateChangedFromRemote(abilityState, connectToken, element);
3502686862fbSopenharmony_ci}
3503686862fbSopenharmony_ci
3504686862fbSopenharmony_ciint32_t DistributedSchedService::NotifyStateChangedFromRemote(int32_t abilityState, int32_t connectToken,
3505686862fbSopenharmony_ci    const AppExecFwk::ElementName& element)
3506686862fbSopenharmony_ci{
3507686862fbSopenharmony_ci    HILOGD("Get connectToken = %{private}s", GetAnonymStr(std::to_string(connectToken)).c_str());
3508686862fbSopenharmony_ci    sptr<IRemoteObject> connect;
3509686862fbSopenharmony_ci    {
3510686862fbSopenharmony_ci        std::lock_guard<std::mutex> autoLock(callLock_);
3511686862fbSopenharmony_ci        for (auto iter = callMap_.begin(); iter != callMap_.end(); iter++) {
3512686862fbSopenharmony_ci            if (iter->second.connectToken == connectToken) {
3513686862fbSopenharmony_ci                connect = iter->first;
3514686862fbSopenharmony_ci                break;
3515686862fbSopenharmony_ci            }
3516686862fbSopenharmony_ci        }
3517686862fbSopenharmony_ci        HILOGD("get connect success");
3518686862fbSopenharmony_ci    }
3519686862fbSopenharmony_ci    if (connect == nullptr) {
3520686862fbSopenharmony_ci        HILOGE("NotifyStateChangedFromRemote connect is null");
3521686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3522686862fbSopenharmony_ci    }
3523686862fbSopenharmony_ci    MessageParcel data;
3524686862fbSopenharmony_ci    if (!data.WriteInterfaceToken(CONNECTION_CALLBACK_INTERFACE_TOKEN)) {
3525686862fbSopenharmony_ci        HILOGE("Write interface token failed.");
3526686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3527686862fbSopenharmony_ci    }
3528686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Parcelable, &element);
3529686862fbSopenharmony_ci    PARCEL_WRITE_HELPER(data, Int32, abilityState);
3530686862fbSopenharmony_ci    MessageParcel reply;
3531686862fbSopenharmony_ci    MessageOption option(MessageOption::TF_ASYNC);
3532686862fbSopenharmony_ci    HILOGD("ON_REMOTE_STATE_CHANGED start");
3533686862fbSopenharmony_ci    int32_t result = connect->SendRequest(IAbilityConnection::ON_REMOTE_STATE_CHANGED, data, reply, option);
3534686862fbSopenharmony_ci    HILOGD("ON_REMOTE_STATE_CHANGED end, %{public}d", result);
3535686862fbSopenharmony_ci    return ERR_OK;
3536686862fbSopenharmony_ci}
3537686862fbSopenharmony_ci
3538686862fbSopenharmony_ciint32_t DistributedSchedService::CheckTargetPermission(const OHOS::AAFwk::Want& want,
3539686862fbSopenharmony_ci    const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t flag,
3540686862fbSopenharmony_ci    bool needQueryExtension)
3541686862fbSopenharmony_ci{
3542686862fbSopenharmony_ci    DistributedSchedPermission& permissionInstance = DistributedSchedPermission::GetInstance();
3543686862fbSopenharmony_ci    AppExecFwk::AbilityInfo targetAbility;
3544686862fbSopenharmony_ci    bool result = permissionInstance.GetTargetAbility(want, targetAbility, needQueryExtension);
3545686862fbSopenharmony_ci    if (!result) {
3546686862fbSopenharmony_ci        HILOGE("GetTargetAbility can not find the target ability");
3547686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3548686862fbSopenharmony_ci    }
3549686862fbSopenharmony_ci    HILOGD("target ability info bundleName:%{public}s abilityName:%{public}s visible:%{public}d",
3550686862fbSopenharmony_ci        targetAbility.bundleName.c_str(), targetAbility.name.c_str(), targetAbility.visible);
3551686862fbSopenharmony_ci    HILOGD("callerType:%{public}d accountType:%{public}d callerUid:%{public}d AccessTokenID:%{public}s",
3552686862fbSopenharmony_ci        callerInfo.callerType, accountInfo.accountType, callerInfo.uid,
3553686862fbSopenharmony_ci        GetAnonymStr(std::to_string(callerInfo.accessToken)).c_str());
3554686862fbSopenharmony_ci    if (flag == START_PERMISSION) {
3555686862fbSopenharmony_ci        HILOGD("start CheckStartPermission");
3556686862fbSopenharmony_ci        return permissionInstance.CheckStartPermission(want, callerInfo, accountInfo, targetAbility, true);
3557686862fbSopenharmony_ci    } else if (flag == CALL_PERMISSION) {
3558686862fbSopenharmony_ci        HILOGD("Collaboration start check get caller permission");
3559686862fbSopenharmony_ci        return permissionInstance.CheckGetCallerPermission(want, callerInfo, accountInfo, targetAbility);
3560686862fbSopenharmony_ci    } else if (flag == SEND_RESULT_PERMISSION) {
3561686862fbSopenharmony_ci        HILOGD("Collaboration start check send result permission");
3562686862fbSopenharmony_ci        return permissionInstance.CheckSendResultPermission(want, callerInfo, accountInfo, targetAbility);
3563686862fbSopenharmony_ci    }
3564686862fbSopenharmony_ci    HILOGE("CheckTargetPermission denied!!");
3565686862fbSopenharmony_ci    return DMS_PERMISSION_DENIED;
3566686862fbSopenharmony_ci}
3567686862fbSopenharmony_ci
3568686862fbSopenharmony_ciint32_t DistributedSchedService::CheckTargetPermission4DiffBundle(const OHOS::AAFwk::Want& want,
3569686862fbSopenharmony_ci    const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t flag,
3570686862fbSopenharmony_ci    bool needQueryExtension)
3571686862fbSopenharmony_ci{
3572686862fbSopenharmony_ci    DistributedSchedPermission& permissionInstance = DistributedSchedPermission::GetInstance();
3573686862fbSopenharmony_ci    AppExecFwk::AbilityInfo targetAbility;
3574686862fbSopenharmony_ci    bool result = permissionInstance.GetTargetAbility(want, targetAbility, needQueryExtension);
3575686862fbSopenharmony_ci    if (!result) {
3576686862fbSopenharmony_ci        HILOGE("GetTargetAbility can not find the target ability");
3577686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3578686862fbSopenharmony_ci    }
3579686862fbSopenharmony_ci    HILOGD("target ability info bundleName:%{public}s abilityName:%{public}s visible:%{public}d",
3580686862fbSopenharmony_ci        targetAbility.bundleName.c_str(), targetAbility.name.c_str(), targetAbility.visible);
3581686862fbSopenharmony_ci    HILOGD("callerType:%{public}d accountType:%{public}d callerUid:%{public}d AccessTokenID:%{public}s",
3582686862fbSopenharmony_ci        callerInfo.callerType, accountInfo.accountType, callerInfo.uid,
3583686862fbSopenharmony_ci        GetAnonymStr(std::to_string(callerInfo.accessToken)).c_str());
3584686862fbSopenharmony_ci    if (flag == START_PERMISSION) {
3585686862fbSopenharmony_ci        HILOGD("start CheckStartPermission");
3586686862fbSopenharmony_ci        return permissionInstance.CheckStartPermission(want, callerInfo, accountInfo, targetAbility, false);
3587686862fbSopenharmony_ci    } else if (flag == CALL_PERMISSION) {
3588686862fbSopenharmony_ci        HILOGD("Collaboration start check get caller permission");
3589686862fbSopenharmony_ci        return permissionInstance.CheckGetCallerPermission(want, callerInfo, accountInfo, targetAbility);
3590686862fbSopenharmony_ci    } else if (flag == SEND_RESULT_PERMISSION) {
3591686862fbSopenharmony_ci        HILOGD("Collaboration start check send result permission");
3592686862fbSopenharmony_ci        return permissionInstance.CheckSendResultPermission(want, callerInfo, accountInfo, targetAbility);
3593686862fbSopenharmony_ci    }
3594686862fbSopenharmony_ci    HILOGE("CheckTargetPermission denied!!");
3595686862fbSopenharmony_ci    return DMS_PERMISSION_DENIED;
3596686862fbSopenharmony_ci}
3597686862fbSopenharmony_ci
3598686862fbSopenharmony_ciint32_t DistributedSchedService::StopRemoteExtensionAbility(const OHOS::AAFwk::Want& want, int32_t callerUid,
3599686862fbSopenharmony_ci    uint32_t accessToken, int32_t extensionType)
3600686862fbSopenharmony_ci{
3601686862fbSopenharmony_ci    std::string localDeviceId;
3602686862fbSopenharmony_ci    std::string deviceId = want.GetDeviceId();
3603686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) || !CheckDeviceId(localDeviceId, deviceId)) {
3604686862fbSopenharmony_ci        HILOGE("CheckDeviceId failed");
3605686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3606686862fbSopenharmony_ci    }
3607686862fbSopenharmony_ci    sptr<IDistributedSched> remoteDms = GetRemoteDms(deviceId);
3608686862fbSopenharmony_ci    if (remoteDms == nullptr) {
3609686862fbSopenharmony_ci        HILOGE("GetRemoteDms failed");
3610686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3611686862fbSopenharmony_ci    }
3612686862fbSopenharmony_ci    CallerInfo callerInfo;
3613686862fbSopenharmony_ci    callerInfo.sourceDeviceId = localDeviceId;
3614686862fbSopenharmony_ci    callerInfo.uid = callerUid;
3615686862fbSopenharmony_ci    callerInfo.accessToken = accessToken;
3616686862fbSopenharmony_ci    if (!BundleManagerInternal::GetCallerAppIdFromBms(callerInfo.uid, callerInfo.callerAppId)) {
3617686862fbSopenharmony_ci        HILOGE("GetCallerAppIdFromBms failed");
3618686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3619686862fbSopenharmony_ci    }
3620686862fbSopenharmony_ci    if (!BundleManagerInternal::GetBundleNameListFromBms(callerInfo.uid, callerInfo.bundleNames)) {
3621686862fbSopenharmony_ci        HILOGE("GetBundleNameListFromBms failed");
3622686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3623686862fbSopenharmony_ci    }
3624686862fbSopenharmony_ci    AccountInfo accountInfo = {};
3625686862fbSopenharmony_ci    if ((DistributedSchedPermission::GetInstance().GetAccountInfo(deviceId, callerInfo, accountInfo)) != ERR_OK) {
3626686862fbSopenharmony_ci        HILOGE("GetAccountInfo failed");
3627686862fbSopenharmony_ci        return INVALID_PARAMETERS_ERR;
3628686862fbSopenharmony_ci    }
3629686862fbSopenharmony_ci    AAFwk::Want remoteWant = want;
3630686862fbSopenharmony_ci    remoteWant.SetParam(DMS_SRC_NETWORK_ID, localDeviceId);
3631686862fbSopenharmony_ci    return remoteDms->StopExtensionAbilityFromRemote(remoteWant, callerInfo, accountInfo, extensionType);
3632686862fbSopenharmony_ci}
3633686862fbSopenharmony_ci
3634686862fbSopenharmony_ciint32_t DistributedSchedService::StopExtensionAbilityFromRemote(const OHOS::AAFwk::Want& remoteWant,
3635686862fbSopenharmony_ci    const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t extensionType)
3636686862fbSopenharmony_ci{
3637686862fbSopenharmony_ci    std::string localDeviceId;
3638686862fbSopenharmony_ci    std::string destinationDeviceId = remoteWant.GetElement().GetDeviceID();
3639686862fbSopenharmony_ci    if (!GetLocalDeviceId(localDeviceId) ||
3640686862fbSopenharmony_ci        !CheckDeviceIdFromRemote(localDeviceId, destinationDeviceId, callerInfo.sourceDeviceId)) {
3641686862fbSopenharmony_ci        HILOGE("check deviceId failed");
3642686862fbSopenharmony_ci        return INVALID_REMOTE_PARAMETERS_ERR;
3643686862fbSopenharmony_ci    }
3644686862fbSopenharmony_ci
3645686862fbSopenharmony_ci    int32_t permissionValid = CheckTargetPermission(remoteWant, callerInfo, accountInfo, START_PERMISSION, true);
3646686862fbSopenharmony_ci    if (permissionValid != ERR_OK) {
3647686862fbSopenharmony_ci        HILOGE("CheckTargetPermission failed!!");
3648686862fbSopenharmony_ci        return DMS_PERMISSION_DENIED;
3649686862fbSopenharmony_ci    }
3650686862fbSopenharmony_ci    Want want = remoteWant;
3651686862fbSopenharmony_ci    want.RemoveParam(DMS_SRC_NETWORK_ID);
3652686862fbSopenharmony_ci    sptr<IRemoteObject> callerToken(new DmsTokenCallback());
3653686862fbSopenharmony_ci
3654686862fbSopenharmony_ci    int32_t activeAccountId = -1;
3655686862fbSopenharmony_ci    ErrCode err = QueryOsAccount(activeAccountId);
3656686862fbSopenharmony_ci    if (err != ERR_OK) {
3657686862fbSopenharmony_ci        return err;
3658686862fbSopenharmony_ci    }
3659686862fbSopenharmony_ci
3660686862fbSopenharmony_ci    return AAFwk::AbilityManagerClient::GetInstance()->StopExtensionAbility(
3661686862fbSopenharmony_ci        want, callerToken, activeAccountId, static_cast<AppExecFwk::ExtensionAbilityType>(extensionType));
3662686862fbSopenharmony_ci}
3663686862fbSopenharmony_ci
3664686862fbSopenharmony_civoid DistributedSchedService::SetCleanMissionFlag(const OHOS::AAFwk::Want& want, int32_t missionId)
3665686862fbSopenharmony_ci{
3666686862fbSopenharmony_ci    auto value =  want.GetParams().GetParam(SUPPORT_CONTINUE_SOURCE_EXIT_KEY);
3667686862fbSopenharmony_ci    IBoolean *ao = IBoolean::Query(value);
3668686862fbSopenharmony_ci    bool isCleanMission = true;
3669686862fbSopenharmony_ci    if (ao != nullptr) {
3670686862fbSopenharmony_ci        isCleanMission = AAFwk::Boolean::Unbox(ao);
3671686862fbSopenharmony_ci    }
3672686862fbSopenharmony_ci    if (dschedContinuation_ == nullptr) {
3673686862fbSopenharmony_ci        HILOGW("continuation object null!");
3674686862fbSopenharmony_ci        return;
3675686862fbSopenharmony_ci    }
3676686862fbSopenharmony_ci    dschedContinuation_->SetCleanMissionFlag(missionId, isCleanMission);
3677686862fbSopenharmony_ci}
3678686862fbSopenharmony_ci} // namespace DistributedSchedule
3679686862fbSopenharmony_ci} // namespace OHOS