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 HILOG_ERROR("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)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)
56 {
57 if (bundleName.empty()) {
58 HILOG_ERROR("bundleName or hapPath is empty when CreateResourceManagerDef");
59 return nullptr;
60 }
61 std::shared_ptr<ResourceManager> resourceManagerImpl(CreateResourceManager());
62 if (resourceManagerImpl == nullptr) {
63 HILOG_ERROR("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 ThemePackManager::GetThemePackManager()->LoadThemeRes(bundleName, moduleName);
70 return resourceManagerImpl;
71 }
72
73 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
CreateResourceManagerExt(const std::string & bundleName,const int32_t appType)74 std::shared_ptr<ResourceManager> CreateResourceManagerExt(const std::string &bundleName, const int32_t appType)
75 {
76 if (bundleName.empty()) {
77 HILOG_ERROR("bundleName is empty when CreateResourceManagerExt");
78 return nullptr;
79 }
80 std::lock_guard<std::mutex> lock(resMgrExtLock);
81 std::shared_ptr<ResourceManager> resMgrExt;
82 if (!resMgrExtMgr->Init(resMgrExt, bundleName, appType) || resMgrExt == nullptr) {
83 HILOG_ERROR("ResourceManagerExt init fail");
84 return nullptr;
85 }
86 return resMgrExt;
87 }
88 #endif
89
CreateResourceManager(const std::string & bundleName,const std::string & moduleName,const std::string & hapPath,const std::vector<std::string> & overlayPath,ResConfig & resConfig,int32_t appType)90 std::shared_ptr<ResourceManager> CreateResourceManager(const std::string &bundleName, const std::string &moduleName,
91 const std::string &hapPath, const std::vector<std::string> &overlayPath,
92 ResConfig &resConfig, int32_t appType)
93 {
94 if (appType == 0) {
95 return CreateResourceManagerDef(bundleName, moduleName, hapPath, overlayPath, resConfig);
96 } else {
97 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
98 return CreateResourceManagerExt(bundleName, appType);
99 #else
100 return nullptr;
101 #endif
102 }
103 }
104
GetSystemResourceManager()105 ResourceManager *GetSystemResourceManager()
106 {
107 return SystemResourceManager::GetSystemResourceManager();
108 }
109
GetSystemResourceManagerNoSandBox()110 ResourceManager *GetSystemResourceManagerNoSandBox()
111 {
112 return SystemResourceManager::GetSystemResourceManagerNoSandBox();
113 }
114
~ResourceManager()115 ResourceManager::~ResourceManager()
116 {}
117 } // namespace Resource
118 } // namespace Global
119 } // namespace OHOS
120