1 /* 2 * Copyright (c) 2024-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef OHOS_DSCHED_COLLAB_MANAGER_H 17 #define OHOS_DSCHED_COLLAB_MANAGER_H 18 19 #include <atomic> 20 #include <map> 21 #include <string> 22 23 #include "dsched_collab.h" 24 #include "dsched_data_buffer.h" 25 #include "idata_listener.h" 26 #include "inner_socket.h" 27 #include "iremote_object.h" 28 #include "single_instance.h" 29 #include "tokenid_kit.h" 30 #include "want.h" 31 32 namespace OHOS { 33 namespace DistributedSchedule { 34 namespace { 35 constexpr int32_t COLLAB_TIMEOUT = 10000; 36 constexpr int32_t BACKGROUND_TIMEOUT = 5000; 37 } 38 39 typedef enum { 40 ACCEPT = 0, 41 REJECT = 1, 42 ON_COLLABORATE_ERR = 10, 43 } CollaborateResult; 44 45 class DSchedCollabManager { 46 DECLARE_SINGLE_INSTANCE_BASE(DSchedCollabManager); 47 public: 48 explicit DSchedCollabManager(); 49 ~DSchedCollabManager(); 50 51 int32_t GetSinkCollabVersion(DSchedCollabInfo &info); 52 int32_t CollabMission(DSchedCollabInfo &info); 53 int32_t NotifyStartAbilityResult(const std::string& collabToken, const int32_t &result, 54 const int32_t &sinkPid, const int32_t &sinkUid, const int32_t &sinkAccessTokenId); 55 int32_t NotifySinkPrepareResult(const DSchedCollabInfo &dSchedCollabInfo, const int32_t &result); 56 int32_t NotifySinkRejectReason(const std::string& collabToken, const std::string& reason); 57 int32_t NotifyAbilityDied(const std::string &bundleName, const int32_t &pid); 58 int32_t NotifySessionClose(const std::string &collabToken); 59 int32_t CleanUpSession(const std::string &collabToken); 60 int32_t CheckCollabRelation(const CollabInfo *sourceInfo, const CollabInfo *sinkInfo); 61 int32_t ReleaseAbilityLink(const std::string &bundleName, const int32_t &pid); 62 int32_t CancleReleaseAbilityLink(const std::string &bundleName, const int32_t &pid); 63 void NotifyWifiOpen(); 64 bool GetWifiStatus(); 65 66 void Init(); 67 void UnInit(); 68 void NotifyAllConnectDecision(std::string peerDeviceId, bool isSupport); 69 void OnDataRecv(int32_t softbusSessionId, std::shared_ptr<DSchedDataBuffer> dataBuffer); 70 void OnShutdown(int32_t socket, bool isSelfCalled); 71 72 std::shared_ptr<DSchedCollab> GetDSchedCollabByTokenId(const std::string &tokenId); 73 74 private: 75 void StartEvent(); 76 void HandleGetSinkCollabVersion(const DSchedCollabInfo &info); 77 void HandleCollabPrepareResult(const DSchedCollabInfo &dSchedCollabInfo, const int32_t &result); 78 int32_t HandleCloseSessions(const std::string &bundleName, const int32_t &pid); 79 void HandleReleaseAbilityLink(const std::string &bundleName, const int32_t &pid, const std::string &collabToken); 80 void HandleDataRecv(const int32_t &softbusSessionId, std::shared_ptr<DSchedDataBuffer> dataBuffer); 81 void NotifyDataRecv(const int32_t &softbusSessionId, int32_t command, const std::string& jsonStr, 82 std::shared_ptr<DSchedDataBuffer> dataBuffer, const std::string& collabToken); 83 void WaitAllConnectDecision(const std::string &peerDeviceId, const std::shared_ptr<DSchedCollab> &dCollab); 84 void SetTimeOut(const std::string &collabToken, int32_t timeout); 85 void RemoveTimeout(const std::string &collabToken); 86 bool IsSessionExists(const DSchedCollabInfo &info); 87 std::string GenerateCollabToken(const std::string &sourceDeviceId); 88 int32_t CheckSrcCollabRelation(const CollabInfo *sourceInfo, const DSchedCollabInfo *collabInfo); 89 int32_t CheckSinkCollabRelation(const CollabInfo *sinkInfo, const DSchedCollabInfo *collabInfo); 90 int32_t ConvertCollaborateResult(int32_t result); 91 bool IsStartForeground(DSchedCollabInfo &info); 92 93 class SoftbusListener : public IDataListener { 94 void OnBind(int32_t socket, PeerSocketInfo info); 95 void OnShutdown(int32_t socket, bool isSelfCalled); 96 void OnDataRecv(int32_t socket, std::shared_ptr<DSchedDataBuffer> dataBuffer); 97 }; 98 99 private: 100 std::thread eventThread_; 101 std::condition_variable eventCon_; 102 std::mutex eventMutex_; 103 std::shared_ptr<OHOS::AppExecFwk::EventHandler> eventHandler_; 104 std::shared_ptr<DSchedCollabManager::SoftbusListener> softbusListener_; 105 std::map<std::string, std::shared_ptr<DSchedCollab>> collabs_; 106 std::mutex collabMutex_; 107 std::shared_mutex collabReadMutex_; 108 109 #ifdef DMSFWK_ALL_CONNECT_MGR 110 std::mutex connectDecisionMutex_; 111 std::condition_variable connectDecisionCond_; 112 std::map<std::string, std::atomic<bool>> peerConnectDecision_; 113 static constexpr int32_t CONNECT_DECISION_TIME_OUT = 10; 114 #endif 115 }; 116 } // namespace DistributedSchedule 117 } // namespace OHOS 118 #endif // OHOS_DSCHED_COLLAB_MANAGER_H