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 SystemResourceManager::AddSystemResource(impl);
46 return impl;
47 }
48
CreateResourceManagerDef(const std::string & bundleName,const std::string & moduleName,const std::string & hapPath,const std::vector<std::string> & overlayPath,ResConfig & resConfig,int32_t userId)49 std::shared_ptr<ResourceManager> CreateResourceManagerDef(
50 const std::string &bundleName, const std::string &moduleName,
51 const std::string &hapPath, const std::vector<std::string> &overlayPath,
52 ResConfig &resConfig, int32_t userId)
53 {
54 if (bundleName.empty()) {
55 RESMGR_HILOGE(RESMGR_TAG, "bundleName or hapPath is empty when CreateResourceManagerDef");
56 return nullptr;
57 }
58 std::shared_ptr<ResourceManager> resourceManagerImpl(CreateResourceManager());
59 if (resourceManagerImpl == nullptr) {
60 RESMGR_HILOGE(RESMGR_TAG, "CreateResourceManagerDef failed bundleName = %{public}s moduleName = %{public}s",
61 bundleName.c_str(), moduleName.c_str());
62 return nullptr;
63 }
64 resourceManagerImpl->bundleInfo.first = bundleName;
65 resourceManagerImpl->bundleInfo.second = moduleName;
66 resourceManagerImpl->userId = userId;
67 uint32_t currentId = resConfig.GetThemeId();
68 auto themePackManager = ThemePackManager::GetThemePackManager();
69 if (themePackManager->IsFirstLoadResource() || themePackManager->UpdateThemeId(currentId)
70 || themePackManager->IsUpdateByUserId(userId)) {
71 RESMGR_HILOGI(RESMGR_TAG, "CreateResourceManagerDef LoadThemeRes");
72 themePackManager->LoadThemeRes(bundleName, moduleName, userId);
73 }
74 return resourceManagerImpl;
75 }
76
77 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
CreateResourceManagerExt(const std::string & bundleName,const int32_t appType)78 std::shared_ptr<ResourceManager> CreateResourceManagerExt(const std::string &bundleName, const int32_t appType)
79 {
80 if (bundleName.empty()) {
81 RESMGR_HILOGE(RESMGR_TAG, "bundleName is empty when CreateResourceManagerExt");
82 return nullptr;
83 }
84 std::lock_guard<std::mutex> lock(resMgrExtLock);
85 std::shared_ptr<ResourceManager> resMgrExt;
86 if (!resMgrExtMgr->Init(resMgrExt, bundleName, appType) || resMgrExt == nullptr) {
87 RESMGR_HILOGE(RESMGR_TAG, "ResourceManagerExt init fail");
88 return nullptr;
89 }
90 return resMgrExt;
91 }
92 #endif
93
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)94 std::shared_ptr<ResourceManager> CreateResourceManager(const std::string &bundleName, const std::string &moduleName,
95 const std::string &hapPath, const std::vector<std::string> &overlayPath,
96 ResConfig &resConfig, int32_t appType, int32_t userId)
97 {
98 if (appType == 0) {
99 return CreateResourceManagerDef(bundleName, moduleName, hapPath, overlayPath, resConfig, userId);
100 } else {
101 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
102 return CreateResourceManagerExt(bundleName, appType);
103 #else
104 return nullptr;
105 #endif
106 }
107 }
108
GetSystemResourceManager()109 ResourceManager *GetSystemResourceManager()
110 {
111 return SystemResourceManager::GetSystemResourceManager();
112 }
113
GetSystemResourceManagerNoSandBox()114 ResourceManager *GetSystemResourceManagerNoSandBox()
115 {
116 return SystemResourceManager::GetSystemResourceManagerNoSandBox();
117 }
118
~ResourceManager()119 ResourceManager::~ResourceManager()
120 {}
121 } // namespace Resource
122 } // namespace Global
123 } // namespace OHOS
124