1 /*
2  * Copyright (c) 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 FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_SERVICES_BUNDLEMGR_BUNDLE_RESOURCE_MANAGER_H
17 #define FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_SERVICES_BUNDLEMGR_BUNDLE_RESOURCE_MANAGER_H
18 
19 #include <map>
20 #include <mutex>
21 #include <string>
22 #include <vector>
23 
24 #include "bundle_constants.h"
25 #include "bundle_resource_change_type.h"
26 #include "bundle_resource_rdb.h"
27 #include "bundle_system_state.h"
28 #include "inner_bundle_info.h"
29 #include "resource_info.h"
30 #include "singleton.h"
31 
32 namespace OHOS {
33 namespace AppExecFwk {
34 class BundleResourceManager : public DelayedSingleton<BundleResourceManager> {
35 public:
36     BundleResourceManager();
37 
38     ~BundleResourceManager();
39     /**
40      * delete resource info
41      */
42     bool DeleteResourceInfo(const std::string &key);
43     /**
44      * add all resource info in system, used when system configuration changed, like:
45      * language, colorMode, theme, and so on
46      */
47     bool AddAllResourceInfo(const int32_t userId, const uint32_t type,
48         const int32_t oldUserId = Constants::INVALID_USERID);
49     /**
50      * delete all resource info
51      */
52     bool DeleteAllResourceInfo();
53     /**
54      * add bundle resource info by bundleName, used when bundle state changed, like:
55      * enable or disable
56      */
57     bool AddResourceInfoByBundleName(const std::string &bundleName, const int32_t userId);
58     /**
59      * add launcher ability resource info by abilityName, used when ability state changed, like:
60      * enable or disable
61      */
62     bool AddResourceInfoByAbility(const std::string &bundleName, const std::string &moduleName,
63         const std::string &abilityName, const int32_t userId);
64 
65     bool GetAllResourceName(std::vector<std::string> &keyNames);
66 
67     bool GetBundleResourceInfo(const std::string &bundleName, const uint32_t flags,
68         BundleResourceInfo &bundleResourceInfo, const int32_t appIndex = 0);
69 
70     bool GetLauncherAbilityResourceInfo(const std::string &bundleName, const uint32_t flags,
71         std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfo, const int32_t appIndex = 0);
72 
73     bool GetAllBundleResourceInfo(const uint32_t flags, std::vector<BundleResourceInfo> &bundleResourceInfos);
74 
75     bool GetAllLauncherAbilityResourceInfo(const uint32_t flags,
76         std::vector<LauncherAbilityResourceInfo> &launcherAbilityResourceInfos);
77 
78     bool SaveResourceInfos(std::vector<ResourceInfo> &resourceInfos);
79 
80     void GetTargetBundleName(const std::string &bundleName, std::string &targetBundleName);
81 
82     bool UpdateBundleIcon(const std::string &bundleName, ResourceInfo &resourceInfo);
83 
84     bool AddCloneBundleResourceInfo(const std::string &bundleName, const int32_t appIndex);
85 
86     bool DeleteCloneBundleResourceInfo(const std::string &bundleName, const int32_t appIndex);
87 
88     bool DeleteNotExistResourceInfo();
89 
90 private:
91     bool AddResourceInfo(const int32_t userId, ResourceInfo &resourceInfo);
92 
93     bool AddResourceInfos(const int32_t userId, std::vector<ResourceInfo> &resourceInfos);
94 
95     bool AddResourceInfosByMap(std::map<std::string, std::vector<ResourceInfo>> &resourceInfosMap,
96         const uint32_t tempTaskNumber, const uint32_t type, const int32_t userId, const int32_t oldUserId);
97 
98     void InnerProcessResourceInfoByResourceUpdateType(
99         std::map<std::string, std::vector<ResourceInfo>> &resourceInfosMap, const uint32_t type,
100         const int32_t userId, const int32_t oldUserId);
101 
102     void ProcessResourceInfoWhenParseFailed(ResourceInfo &resourceInfo);
103 
104     void ProcessResourceInfo(const std::vector<ResourceInfo> &resourceInfos, ResourceInfo &resourceInfo);
105 
106     void GetDefaultIcon(ResourceInfo &resourceInfo);
107 
108     uint32_t CheckResourceFlags(const uint32_t flags);
109 
110     void SendBundleResourcesChangedEvent(const int32_t userId, const uint32_t type);
111 
112     void InnerProcessResourceInfoBySystemLanguageChanged(
113         std::map<std::string, std::vector<ResourceInfo>> &resourceInfosMap);
114 
115     void InnerProcessResourceInfoBySystemThemeChanged(
116         std::map<std::string, std::vector<ResourceInfo>> &resourceInfosMap,
117         const int32_t userId);
118 
119     void InnerProcessResourceInfoByUserIdChanged(
120         std::map<std::string, std::vector<ResourceInfo>> &resourceInfosMap,
121         const int32_t userId, const int32_t oldUserId);
122 
123     void DeleteNotExistResourceInfo(const std::map<std::string, std::vector<ResourceInfo>> &resourceInfosMap,
124         const std::vector<std::string> &existResourceNames);
125 
126     bool InnerProcessWhetherThemeExist(const std::string &bundleName, const int32_t userId);
127 
128     bool GetBundleResourceInfoForCloneBundle(const std::string &bundleName,
129         const int32_t appIndex, std::vector<ResourceInfo> &resourceInfos);
130 
131     bool UpdateCloneBundleResourceInfo(const std::string &bundleName, const int32_t appIndex, const uint32_t type);
132 
133     void DeleteNotExistResourceInfo(const std::string &bundleName,
134         const int32_t appIndex, const std::vector<ResourceInfo> &resourceInfos);
135 
136     void ProcessResourceInfoNoNeedToParseOtherIcon(std::vector<ResourceInfo> &resourceInfos);
137 
138     std::shared_ptr<BundleResourceRdb> bundleResourceRdb_;
139     std::mutex mutex_;
140     std::atomic_uint currentTaskNum_ = 0;
141 };
142 } // AppExecFwk
143 } // OHOS
144 #endif // FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_SERVICES_BUNDLEMGR_BUNDLE_RESOURCE_MANAGER_H
145