1 /* 2 * Copyright (c) 2024 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_ROSEN_WINDOW_SCENE_MULTI_INSTANCE_MANAGER_H 17 #define OHOS_ROSEN_WINDOW_SCENE_MULTI_INSTANCE_MANAGER_H 18 19 #include <bitset> 20 #include <cstdint> 21 #include <unordered_map> 22 #include <string> 23 #include <shared_mutex> 24 #include <vector> 25 #include <refbase.h> 26 #include "common/include/task_scheduler.h" 27 #include "session/host/include/scene_session.h" 28 #include "wm_common.h" 29 30 namespace OHOS::AppExecFwk { 31 class IBundleMgr; 32 struct ApplicationInfo; 33 } // namespace OHOS::AppExecFwk 34 35 namespace OHOS::Rosen { 36 static constexpr uint32_t MAX_INSTANCE_COUNT = 10; 37 class MultiInstanceManager { 38 public: 39 static MultiInstanceManager& GetInstance(); 40 static bool IsSupportMultiInstance(const SystemSessionConfig& systemConfig); 41 void Init(const sptr<AppExecFwk::IBundleMgr>& bundleMgr, const std::shared_ptr<TaskScheduler>& taskScheduler); 42 void IncreaseInstanceKeyRefCount(const sptr<SceneSession>& sceneSession); 43 void DecreaseInstanceKeyRefCount(const sptr<SceneSession>& sceneSession); 44 45 // Locks appInfoMutex_ 46 void SetCurrentUserId(int32_t userId); 47 bool IsMultiInstance(const std::string& bundleName); 48 uint32_t GetMaxInstanceCount(const std::string& bundleName); 49 bool IsValidInstanceKey(const std::string& bundleName, const std::string& instanceKey); 50 void RefreshAppInfo(const std::string& bundleName); 51 52 // Locks mutex_/appInfoMutex_ 53 void FillInstanceKeyIfNeed(const sptr<SceneSession>& sceneSession); 54 bool MultiInstancePendingSessionActivation(SessionInfo& sessionInfo); 55 std::string CreateNewInstanceKey(const std::string& bundleName, const std::string& instanceKey = ""); 56 57 // Locks mutex_ 58 uint32_t GetInstanceCount(const std::string& bundleName); 59 std::string GetLastInstanceKey(const std::string& bundleName); 60 bool IsInstanceKeyExist(const std::string& bundleName, const std::string& instanceKey); 61 62 private: 63 uint32_t FindMinimumAvailableInstanceId(const std::string& bundleName, uint32_t maxInstanceCount); 64 void RemoveInstanceKey(const std::string& bundleName, const std::string& instanceKey); 65 void AddInstanceId(const std::string& bundleName, uint32_t instanceId); 66 void RemoveInstanceId(const std::string& bundleName, uint32_t instanceId); 67 bool ConvertInstanceKeyToInstanceId(const std::string& instanceKey, uint32_t& instanceId) const; 68 69 std::shared_mutex mutex_; 70 std::unordered_map<std::string, std::vector<uint32_t>> bundleInstanceIdListMap_; 71 std::unordered_map<std::string, std::bitset<MAX_INSTANCE_COUNT>> bundleInstanceUsageMap_; 72 // Above guarded by mutex_ 73 74 std::shared_mutex appInfoMutex_; 75 std::unordered_map<std::string, AppExecFwk::ApplicationInfo> appInfoMap_; 76 // Above guarded by appInfoMutex_ 77 78 // Guarded by SceneSessionManager sceneSessionMapMutex_ 79 std::unordered_map<std::string, int32_t> instanceKeyRefCountMap_; 80 81 sptr<AppExecFwk::IBundleMgr> bundleMgr_; 82 std::shared_ptr<TaskScheduler> taskScheduler_; 83 int32_t userId_ = 0; 84 }; 85 } // namespace OHOS::Rosen 86 87 #endif // OHOS_ROSEN_WINDOW_SCENE_MULTI_INSTANCE_MANAGER_H