1 /* 2 * Copyright (c) 2021-2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DISTRIBUTED_SCHED_SERVICE_H 17 #define OHOS_DISTRIBUTED_SCHED_SERVICE_H 18 19 #include <memory> 20 #include <mutex> 21 #include <set> 22 #include <unordered_map> 23 24 #include "app_mgr_interface.h" 25 #include "app_state_observer.h" 26 #include "distributed_sched_stub.h" 27 #include "distributed_sched_continuation.h" 28 #include "dms_callback_task.h" 29 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE 30 #include "form_mgr_interface.h" 31 #endif 32 #include "iremote_object.h" 33 #include "iremote_proxy.h" 34 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER 35 #include "mission/distributed_mission_info.h" 36 #include "nocopyable.h" 37 #endif 38 #include "single_instance.h" 39 #include "system_ability.h" 40 41 namespace OHOS { 42 namespace DistributedSchedule { 43 class ConnectAbilitySession; 44 45 enum class ServiceRunningState { 46 STATE_NO_START, 47 STATE_RUNNING 48 }; 49 50 enum class TargetComponent { 51 HARMONY_COMPONENT, 52 }; 53 54 struct ConnectInfo { 55 CallerInfo callerInfo; 56 sptr<IRemoteObject> callbackWrapper; 57 AppExecFwk::ElementName element; 58 }; 59 60 struct ProcessDiedNotifyInfo { 61 std::string remoteDeviceId; 62 CallerInfo callerInfo; 63 TargetComponent targetComponent; 64 }; 65 66 struct CallInfo { 67 int32_t connectToken; 68 std::string remoteDeviceId; 69 }; 70 71 struct ObserverInfo { 72 sptr<AppStateObserver> appStateObserver; 73 std::string srcDeviceId; 74 int32_t connectToken; 75 std::string dstBundleName; 76 std::string dstAbilityName; 77 sptr<IRemoteObject> srcConnect; 78 sptr<IRemoteObject> token; 79 }; 80 81 class DistributedSchedService : public SystemAbility, public DistributedSchedStub { 82 DECLARE_SYSTEM_ABILITY(DistributedSchedService); 83 DECLARE_SINGLE_INSTANCE_BASE(DistributedSchedService); 84 85 public: 86 ~DistributedSchedService() = default; 87 void OnStart() override; 88 void OnStop() override; 89 int32_t Dump(int32_t fd, const std::vector<std::u16string>& args) override; 90 void DeviceOnlineNotify(const std::string& deviceId); 91 void DeviceOfflineNotify(const std::string& deviceId); 92 93 int32_t StartRemoteAbility(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode, 94 uint32_t accessToken) override; 95 int32_t StartAbilityFromRemote(const OHOS::AAFwk::Want& want, 96 const OHOS::AppExecFwk::AbilityInfo& abilityInfo, int32_t requestCode, const CallerInfo& callerInfo, 97 const AccountInfo& accountInfo) override; 98 int32_t SendResultFromRemote(OHOS::AAFwk::Want& want, int32_t requestCode, 99 const CallerInfo& callerInfo, const AccountInfo& accountInfo, int32_t resultCode) override; 100 int32_t ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId, 101 int32_t missionId, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams) override; 102 int32_t ContinueMission(const std::string& srcDeviceId, const std::string& dstDeviceId, 103 const std::string& bundleName, const sptr<IRemoteObject>& callback, 104 const OHOS::AAFwk::WantParams& wantParams) override; 105 int32_t DealDSchedEventResult(const OHOS::AAFwk::Want& want, int32_t status); 106 int32_t StartContinuation(const OHOS::AAFwk::Want& want, int32_t missionId, int32_t callerUid, 107 int32_t status, uint32_t accessToken) override; 108 void NotifyCompleteContinuation(const std::u16string& devId, int32_t sessionId, bool isSuccess) override; 109 int32_t NotifyContinuationResultFromRemote(int32_t sessionId, bool isSuccess) override; 110 int32_t NotifyDSchedEventResultFromRemote(const std::string type, int32_t dSchedEventResult) override; 111 void NotifyContinuationCallbackResult(int32_t missionId, int32_t resultCode); 112 void NotifyDSchedEventCallbackResult(const std::string type, int32_t resultCode); 113 int32_t NotifyFreeInstallResult(const CallbackTaskItem item, int32_t resultCode); 114 int32_t ConnectRemoteAbility(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect, 115 int32_t callerUid, int32_t callerPid, uint32_t accessToken) override; 116 int32_t DisconnectRemoteAbility(const sptr<IRemoteObject>& connect, int32_t callerUid, 117 uint32_t accessToken) override; 118 int32_t ConnectAbilityFromRemote(const OHOS::AAFwk::Want& want, const AppExecFwk::AbilityInfo& abilityInfo, 119 const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo, const AccountInfo& accountInfo) override; 120 int32_t DisconnectAbilityFromRemote(const sptr<IRemoteObject>& connect, 121 int32_t uid, const std::string& sourceDeviceId) override; 122 int32_t NotifyProcessDiedFromRemote(const CallerInfo& callerInfo) override; 123 #ifdef SUPPORT_DISTRIBUTED_MISSION_MANAGER 124 int32_t GetMissionInfos(const std::string& deviceId, int32_t numMissions, 125 std::vector<AAFwk::MissionInfo>& missionInfos) override; 126 int32_t NotifyMissionsChangedFromRemote(const std::vector<DstbMissionInfo>& missionInfos, 127 const CallerInfo& callerInfo) override; 128 int32_t GetRemoteMissionSnapshotInfo(const std::string& networkId, int32_t missionId, 129 std::unique_ptr<AAFwk::MissionSnapshot>& missionSnapshot) override; 130 int32_t StartSyncRemoteMissions(const std::string& devId, bool fixConflict, int64_t tag) override; 131 int32_t StartSyncMissionsFromRemote(const CallerInfo& callerInfo, 132 std::vector<DstbMissionInfo>& missionInfos) override; 133 int32_t StopSyncRemoteMissions(const std::string& devId) override; 134 int32_t StopSyncMissionsFromRemote(const CallerInfo& callerInfo) override; 135 int32_t RegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override; 136 int32_t RegisterDSchedEventListener(const std::string& type, const sptr<IRemoteObject>& obj) override; 137 int32_t UnRegisterDSchedEventListener(const std::string& type, const sptr<IRemoteObject>& obj) override; 138 int32_t RegisterOnListener(const std::string& type, const sptr<IRemoteObject>& obj) override; 139 int32_t RegisterOffListener(const std::string& type, const sptr<IRemoteObject>& obj) override; 140 int32_t UnRegisterMissionListener(const std::u16string& devId, const sptr<IRemoteObject>& obj) override; 141 int32_t SetMissionContinueState(int32_t missionId, const AAFwk::ContinueState &state) override; 142 #endif 143 void ProcessConnectDied(const sptr<IRemoteObject>& connect); 144 void ProcessDeviceOffline(const std::string& deviceId); 145 void DumpConnectInfo(std::string& info); 146 void DumpSessionsLocked(const std::list<ConnectAbilitySession>& sessionsList, std::string& info); 147 void DumpElementLocked(const std::list<AppExecFwk::ElementName>& elementsList, std::string& info); 148 int32_t StartRemoteAbilityByCall(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect, 149 int32_t callerUid, int32_t callerPid, uint32_t accessToken) override; 150 int32_t ReleaseRemoteAbility(const sptr<IRemoteObject>& connect, 151 const AppExecFwk::ElementName &element) override; 152 int32_t StartAbilityByCallFromRemote(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect, 153 const CallerInfo& callerInfo, const AccountInfo& accountInfo) override; 154 int32_t ReleaseAbilityFromRemote(const sptr<IRemoteObject>& connect, const AppExecFwk::ElementName &element, 155 const CallerInfo& callerInfo) override; 156 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE 157 int32_t StartRemoteShareForm(const std::string& remoteDeviceId, 158 const AppExecFwk::FormShareInfo& formShareInfo) override; 159 int32_t StartShareFormFromRemote( 160 const std::string& remoteDeviceId, const AppExecFwk::FormShareInfo& formShareInfo) override; 161 void ProcessFormMgrDied(const wptr<IRemoteObject>& remote); 162 #endif 163 void ProcessCallerDied(const sptr<IRemoteObject>& connect, int32_t deviceType); 164 void ProcessCalleeDied(const sptr<IRemoteObject>& connect); 165 void ProcessCallResult(const sptr<IRemoteObject>& calleeConnect, const sptr<IRemoteObject>& callerConnect); 166 int32_t StartRemoteFreeInstall(const OHOS::AAFwk::Want& want, int32_t callerUid, int32_t requestCode, 167 uint32_t accessToken, const sptr<IRemoteObject>& callback) override; 168 int32_t StartFreeInstallFromRemote(const FreeInstallInfo& info, int64_t taskId) override; 169 int32_t NotifyCompleteFreeInstallFromRemote(int64_t taskId, int32_t resultCode) override; 170 int32_t NotifyCompleteFreeInstall(const FreeInstallInfo& info, int64_t taskId, int32_t resultCode); 171 int32_t GetDistributedComponentList(std::vector<std::string>& distributedComponents) override; 172 void SetContinuationTimeout(int32_t missionId, int32_t timeout); 173 void RemoveContinuationTimeout(int32_t missionId); 174 std::string GetContinuaitonDevice(int32_t missionId); 175 int32_t NotifyStateChangedFromRemote(int32_t abilityState, int32_t connectToken, 176 const AppExecFwk::ElementName& element) override; 177 int32_t NotifyStateChanged(int32_t abilityState, AppExecFwk::ElementName& element, 178 const sptr<IRemoteObject>& token); 179 int32_t StopRemoteExtensionAbility(const OHOS::AAFwk::Want& want, int32_t callerUid, 180 uint32_t accessToken, int32_t extensionType) override; 181 int32_t StopExtensionAbilityFromRemote(const OHOS::AAFwk::Want& remoteWant, const CallerInfo& callerInfo, 182 const AccountInfo& accountInfo, int32_t extensionType) override; 183 private: 184 DistributedSchedService(); 185 bool Init(); 186 void InitCommonEventListener(); 187 void RemoteConnectAbilityMappingLocked(const sptr<IRemoteObject>& connect, const std::string& localDeviceId, 188 const std::string& remoteDeviceId, const AppExecFwk::ElementName& element, const CallerInfo& callerInfo, 189 TargetComponent targetComponent); 190 int32_t DisconnectEachRemoteAbilityLocked(const std::string& localDeviceId, 191 const std::string& remoteDeviceId, const sptr<IRemoteObject>& connect); 192 sptr<IDistributedSched> GetRemoteDms(const std::string& remoteDeviceId); 193 static bool GetLocalDeviceId(std::string& localDeviceId); 194 bool CheckDeviceId(const std::string& localDeviceId, const std::string& remoteDeviceId); 195 bool CheckDeviceIdFromRemote(const std::string& localDeviceId, 196 const std::string& destinationDeviceId, const std::string& sourceDeviceId); 197 void NotifyDeviceOfflineToAppLocked(const sptr<IRemoteObject>& connect, const ConnectAbilitySession& session); 198 int32_t NotifyApp(const sptr<IRemoteObject>& connect, const AppExecFwk::ElementName& element, int32_t errCode); 199 void NotifyProcessDiedAll(const std::list<ProcessDiedNotifyInfo>& notifyList); 200 void NotifyProcessDied(const std::string& remoteDeviceId, const CallerInfo& callerInfo, 201 TargetComponent targetComponent); 202 int32_t CheckDistributedConnectLocked(const CallerInfo& callerInfo) const; 203 void DecreaseConnectLocked(int32_t uid); 204 static int32_t GetUidLocked(const std::list<ConnectAbilitySession>& sessionList); 205 int32_t TryConnectRemoteAbility(const OHOS::AAFwk::Want& want, 206 const sptr<IRemoteObject>& connect, const CallerInfo& callerInfo); 207 int32_t ContinueLocalMissionDealFreeInstall(OHOS::AAFwk::Want& want, int32_t missionId, 208 const std::string& dstDeviceId, const sptr<IRemoteObject>& callback); 209 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE 210 sptr<AppExecFwk::IFormMgr> GetFormMgrProxy(); 211 #endif 212 int32_t CleanMission(int32_t missionId); 213 int32_t SetCallerInfo(int32_t callerUid, std::string localDeviceId, uint32_t accessToken, CallerInfo& callerInfo); 214 int32_t SetWantForContinuation(AAFwk::Want& newWant, int32_t missionId); 215 int32_t ContinueLocalMission(const std::string& dstDeviceId, int32_t missionId, 216 const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams); 217 int32_t ContinueAbilityWithTimeout(const std::string& dstDeviceId, int32_t missionId, 218 const sptr<IRemoteObject>& callback, uint32_t remoteBundleVersion = 0); 219 int32_t ContinueRemoteMission(const std::string& srcDeviceId, const std::string& dstDeviceId, int32_t missionId, 220 const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams); 221 int32_t ContinueRemoteMission(const std::string& srcDeviceId, const std::string& dstDeviceId, 222 const std::string& bundleName, const sptr<IRemoteObject>& callback, const OHOS::AAFwk::WantParams& wantParams); 223 int32_t TryStartRemoteAbilityByCall(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect, 224 const CallerInfo& callerInfo); 225 int32_t StartLocalAbility(const FreeInstallInfo& info, int64_t taskId, int32_t resultCode); 226 int32_t StartAbility(const OHOS::AAFwk::Want& want, int32_t requestCode); 227 int32_t HandleRemoteNotify(const FreeInstallInfo& info, int64_t taskId, int32_t resultCode); 228 void ReportDistributedComponentChange(const CallerInfo& callerInfo, int32_t changeType, 229 int32_t componentType, int32_t deviceType); 230 void ReportDistributedComponentChange(const ConnectInfo& connectInfo, int32_t changeType, 231 int32_t componentType, int32_t deviceType); 232 void HandleLocalCallerDied(const sptr<IRemoteObject>& connect); 233 void SaveCallerComponent(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect, 234 const CallerInfo& callerInfo); 235 void RemoveCallerComponent(const sptr<IRemoteObject>& connect); 236 void ProcessCalleeOffline(const std::string& deviceId); 237 void GetConnectComponentList(std::vector<std::string>& distributedComponents); 238 void GetCallComponentList(std::vector<std::string>& distributedComponents); 239 void ProcessFreeInstallOffline(const std::string& deviceId); 240 bool RegisterAppStateObserver(const OHOS::AAFwk::Want& want, const CallerInfo& callerInfo, 241 const sptr<IRemoteObject>& srcConnect, const sptr<IRemoteObject>& callbackWrapper); 242 void UnregisterAppStateObserver(const sptr<IRemoteObject>& callbackWrapper); 243 sptr<AppExecFwk::IAppMgr> GetAppManager(); 244 int32_t CheckTargetPermission(const OHOS::AAFwk::Want& want, const CallerInfo& callerInfo, 245 const AccountInfo& accountInfo, int32_t flag, bool needQueryExtension); 246 int32_t SaveConnectToken(const OHOS::AAFwk::Want& want, const sptr<IRemoteObject>& connect); 247 void SetCleanMissionFlag(const OHOS::AAFwk::Want& want, int32_t missionId); 248 void RemoveConnectAbilityInfo(const std::string& deviceId); 249 ErrCode QueryOsAccount(int32_t& activeAccountId); 250 251 std::shared_ptr<DSchedContinuation> dschedContinuation_; 252 std::map<sptr<IRemoteObject>, std::list<ConnectAbilitySession>> distributedConnectAbilityMap_; 253 std::map<sptr<IRemoteObject>, ConnectInfo> connectAbilityMap_; 254 std::unordered_map<int32_t, uint32_t> trackingUidMap_; 255 std::mutex distributedLock_; 256 std::mutex connectLock_; 257 sptr<IRemoteObject::DeathRecipient> connectDeathRecipient_; 258 #ifdef SUPPORT_DISTRIBUTED_FORM_SHARE 259 sptr<IRemoteObject::DeathRecipient> formMgrDeathRecipient_; 260 sptr<AppExecFwk::IFormMgr> formMgrProxy_; 261 std::mutex formMgrLock_; 262 #endif 263 std::mutex calleeLock_; 264 std::map<sptr<IRemoteObject>, ConnectInfo> calleeMap_; 265 sptr<IRemoteObject::DeathRecipient> callerDeathRecipient_; 266 std::shared_ptr<DmsCallbackTask> dmsCallbackTask_; 267 std::shared_ptr<AppExecFwk::EventHandler> componentChangeHandler_; 268 std::mutex callerLock_; 269 std::map<sptr<IRemoteObject>, std::list<ConnectAbilitySession>> callerMap_; 270 sptr<IRemoteObject::DeathRecipient> callerDeathRecipientForLocalDevice_; 271 std::mutex observerLock_; 272 std::map<sptr<IRemoteObject>, ObserverInfo> observerMap_; 273 std::mutex callLock_; 274 std::map<sptr<IRemoteObject>, CallInfo> callMap_; 275 std::mutex tokenMutex_; 276 std::mutex registerMutex_; 277 std::atomic<int32_t> token_ {0}; 278 std::map<std::string, sptr<AppStateObserver>> bundleNameMap_; 279 }; 280 281 class ConnectAbilitySession { 282 public: 283 ConnectAbilitySession(const std::string& sourceDeviceId, const std::string& destinationDeviceId, 284 const CallerInfo& callerInfo, TargetComponent targetComponent = TargetComponent::HARMONY_COMPONENT); 285 ~ConnectAbilitySession() = default; 286 GetSourceDeviceId()287 const std::string& GetSourceDeviceId() const 288 { 289 return sourceDeviceId_; 290 } 291 GetDestinationDeviceId()292 const std::string& GetDestinationDeviceId() const 293 { 294 return destinationDeviceId_; 295 } 296 GetElementsList()297 std::list<AppExecFwk::ElementName> GetElementsList() const 298 { 299 return elementsList_; 300 } 301 GetCallerInfo()302 CallerInfo GetCallerInfo() const 303 { 304 return callerInfo_; 305 } 306 GetTargetComponent()307 TargetComponent GetTargetComponent() const 308 { 309 return targetComponent_; 310 } 311 312 bool IsSameCaller(const CallerInfo& callerInfo); 313 void AddElement(const AppExecFwk::ElementName& element); 314 315 private: 316 std::string sourceDeviceId_; 317 std::string destinationDeviceId_; 318 std::list<AppExecFwk::ElementName> elementsList_; 319 CallerInfo callerInfo_; 320 TargetComponent targetComponent_; 321 }; 322 323 class CallerDeathRecipient : public IRemoteObject::DeathRecipient { 324 public: 325 CallerDeathRecipient() = default; CallerDeathRecipient(int32_t deviceType)326 explicit CallerDeathRecipient(int32_t deviceType) 327 { 328 deviceType_ = deviceType; 329 } 330 ~CallerDeathRecipient() override = default; 331 void OnRemoteDied(const wptr<IRemoteObject>& remote) override; 332 private: 333 int32_t deviceType_ = IDistributedSched::CALLEE; 334 }; 335 } // namespace DistributedSchedule 336 } // namespace OHOS 337 #endif // OHOS_DISTRIBUTED_SCHED_SERVICE_H 338