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 #include "resource_manager.h"
17 
18 #include "hilog_wrapper.h"
19 #include "resource_manager_impl.h"
20 #include "system_resource_manager.h"
21 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
22 #include "resource_manager_ext_mgr.h"
23 #endif
24 #include "theme_pack_manager.h"
25 namespace OHOS {
26 namespace Global {
27 namespace Resource {
28 static std::map<std::string, std::shared_ptr<ResourceManager>> resMgrMap;
29 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
30 static std::mutex resMgrExtLock;
31 static std::shared_ptr<ResourceManagerExtMgr> resMgrExtMgr = std::make_shared<ResourceManagerExtMgr>();
32 #endif
33 
CreateResourceManager()34 ResourceManager *CreateResourceManager()
35 {
36     ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl;
37     if (impl == nullptr) {
38         RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when CreateResourceManager");
39         return nullptr;
40     }
41     if (!impl->Init()) {
42         delete (impl);
43         return nullptr;
44     }
45     ResourceManagerImpl *systemResourceManager = SystemResourceManager::GetSystemResourceManager();
46     if (systemResourceManager != nullptr) {
47         impl->AddSystemResource(systemResourceManager);
48     }
49     return impl;
50 }
51 
CreateResourceManagerDef(const std::string & bundleName,const std::string & moduleName,const std::string & hapPath,const std::vector<std::string> & overlayPath,ResConfig & resConfig,int32_t userId)52 std::shared_ptr<ResourceManager> CreateResourceManagerDef(
53     const std::string &bundleName, const std::string &moduleName,
54     const std::string &hapPath, const std::vector<std::string> &overlayPath,
55     ResConfig &resConfig, int32_t userId)
56 {
57     if (bundleName.empty()) {
58         RESMGR_HILOGE(RESMGR_TAG, "bundleName or hapPath is empty when CreateResourceManagerDef");
59         return nullptr;
60     }
61     std::shared_ptr<ResourceManager> resourceManagerImpl(CreateResourceManager());
62     if (resourceManagerImpl == nullptr) {
63         RESMGR_HILOGE(RESMGR_TAG, "CreateResourceManagerDef failed bundleName = %{public}s moduleName = %{public}s",
64             bundleName.c_str(), moduleName.c_str());
65         return nullptr;
66     }
67     resourceManagerImpl->bundleInfo.first = bundleName;
68     resourceManagerImpl->bundleInfo.second = moduleName;
69     resourceManagerImpl->userId = userId;
70     uint32_t currentId = resConfig.GetThemeId();
71     auto themePackManager = ThemePackManager::GetThemePackManager();
72     themePackManager->SetFlagByUserId(userId);
73     if (themePackManager->IsFirstLoadResource() || themePackManager->UpdateThemeId(currentId)
74         || themePackManager->IsUpdateByUserId(userId)) {
75         RESMGR_HILOGI(RESMGR_TAG, "CreateResourceManagerDef LoadThemeRes");
76         themePackManager->LoadThemeRes(bundleName, moduleName, userId);
77     }
78     return resourceManagerImpl;
79 }
80 
81 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
CreateResourceManagerExt(const std::string & bundleName,const int32_t appType)82 std::shared_ptr<ResourceManager> CreateResourceManagerExt(const std::string &bundleName, const int32_t appType)
83 {
84     if (bundleName.empty()) {
85         RESMGR_HILOGE(RESMGR_TAG, "bundleName is empty when CreateResourceManagerExt");
86         return nullptr;
87     }
88     std::lock_guard<std::mutex> lock(resMgrExtLock);
89     std::shared_ptr<ResourceManager> resMgrExt;
90     if (!resMgrExtMgr->Init(resMgrExt, bundleName, appType) || resMgrExt == nullptr) {
91         RESMGR_HILOGE(RESMGR_TAG, "ResourceManagerExt init fail");
92         return nullptr;
93     }
94     return resMgrExt;
95 }
96 #endif
97 
CreateResourceManager(const std::string & bundleName,const std::string & moduleName,const std::string & hapPath,const std::vector<std::string> & overlayPath,ResConfig & resConfig,int32_t appType,int32_t userId)98 std::shared_ptr<ResourceManager> CreateResourceManager(const std::string &bundleName, const std::string &moduleName,
99     const std::string &hapPath, const std::vector<std::string> &overlayPath,
100     ResConfig &resConfig, int32_t appType, int32_t userId)
101 {
102     if (appType == 0) {
103         return CreateResourceManagerDef(bundleName, moduleName, hapPath, overlayPath, resConfig, userId);
104     } else {
105     #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
106         return CreateResourceManagerExt(bundleName, appType);
107     #else
108         return nullptr;
109     #endif
110     }
111 }
112 
GetSystemResourceManager()113 ResourceManager *GetSystemResourceManager()
114 {
115     return SystemResourceManager::GetSystemResourceManager();
116 }
117 
GetSystemResourceManagerNoSandBox()118 ResourceManager *GetSystemResourceManagerNoSandBox()
119 {
120     return SystemResourceManager::GetSystemResourceManagerNoSandBox();
121 }
122 
~ResourceManager()123 ResourceManager::~ResourceManager()
124 {
125     ThemePackManager::GetThemePackManager()->CheckFlagByUserId(userId);
126 }
127 } // namespace Resource
128 } // namespace Global
129 } // namespace OHOS
130