• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     void DecreaseInstanceKeyRefCountByBundleNameAndInstanceKey(const std::string& bundleName,
45         const std::string& instanceKey);
46 
47     // Locks appInfoMutex_
48     void SetCurrentUserId(int32_t userId);
49     bool IsMultiInstance(const std::string& bundleName);
50     uint32_t GetMaxInstanceCount(const std::string& bundleName);
51     bool IsValidInstanceKey(const std::string& bundleName, const std::string& instanceKey);
52     void RefreshAppInfo(const std::string& bundleName);
53 
54     // Locks mutex_/appInfoMutex_
55     void FillInstanceKeyIfNeed(const sptr<SceneSession>& sceneSession);
56     bool MultiInstancePendingSessionActivation(SessionInfo& sessionInfo);
57     std::string CreateNewInstanceKey(const std::string& bundleName, const std::string& instanceKey = "");
58     void RemoveInstanceKey(const std::string& bundleName, const std::string& instanceKey);
59 
60     // Locks mutex_
61     uint32_t GetInstanceCount(const std::string& bundleName);
62     std::string GetLastInstanceKey(const std::string& bundleName);
63     bool IsInstanceKeyExist(const std::string& bundleName, const std::string& instanceKey);
64     AppExecFwk::ApplicationInfo GetApplicationInfo(const std::string& bundleName) const;
65 
66 private:
67     uint32_t FindMinimumAvailableInstanceId(const std::string& bundleName, uint32_t maxInstanceCount);
68     void AddInstanceId(const std::string& bundleName, uint32_t instanceId);
69     void RemoveInstanceId(const std::string& bundleName, uint32_t instanceId);
70     bool ConvertInstanceKeyToInstanceId(const std::string& instanceKey, uint32_t& instanceId) const;
71 
72     std::shared_mutex mutex_;
73     std::unordered_map<std::string, std::vector<uint32_t>> bundleInstanceIdListMap_;
74     std::unordered_map<std::string, std::bitset<MAX_INSTANCE_COUNT>> bundleInstanceUsageMap_;
75     // Above guarded by mutex_
76 
77     mutable std::shared_mutex appInfoMutex_;
78     std::unordered_map<std::string, AppExecFwk::ApplicationInfo> appInfoMap_;
79     // Above guarded by appInfoMutex_
80 
81     // Guarded by SceneSessionManager sceneSessionMapMutex_
82     std::unordered_map<std::string, int32_t> instanceKeyRefCountMap_;
83 
84     sptr<AppExecFwk::IBundleMgr> bundleMgr_;
85     std::shared_ptr<TaskScheduler> taskScheduler_;
86     int32_t userId_ = 0;
87 };
88 } // namespace OHOS::Rosen
89 
90 #endif // OHOS_ROSEN_WINDOW_SCENE_MULTI_INSTANCE_MANAGER_H