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
25 namespace OHOS {
26 namespace Global {
27 namespace Resource {
28 static std::map<std::string, std::shared_ptr<ResourceManager>> resMgrMap;
29 static std::mutex resMgrLock;
30 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
31 static std::mutex resMgrExtLock;
32 static std::shared_ptr<ResourceManagerExtMgr> resMgrExtMgr = std::make_shared<ResourceManagerExtMgr>();
33 #endif
34
CreateResourceManager()35 ResourceManager *CreateResourceManager()
36 {
37 ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl;
38 if (impl == nullptr) {
39 HILOG_ERROR("new ResourceManagerImpl failed when CreateResourceManager");
40 return nullptr;
41 }
42 if (!impl->Init()) {
43 delete (impl);
44 return nullptr;
45 }
46 ResourceManagerImpl *systemResourceManager = SystemResourceManager::GetSystemResourceManager();
47 if (systemResourceManager != nullptr) {
48 impl->AddSystemResource(systemResourceManager);
49 }
50 return impl;
51 }
52
CreateResourceManagerDef(const std::string & bundleName,const std::string & moduleName,const std::string & hapPath,const std::vector<std::string> & overlayPath,ResConfig & resConfig)53 std::shared_ptr<ResourceManager> CreateResourceManagerDef(const std::string &bundleName,
54 const std::string &moduleName, const std::string &hapPath, const std::vector<std::string> &overlayPath,
55 ResConfig &resConfig)
56 {
57 if (bundleName.empty() || hapPath.empty()) {
58 HILOG_ERROR("bundleName or hapPath is empty when CreateResourceManagerDef");
59 return nullptr;
60 }
61 std::string resMgrKey(bundleName);
62 if (!moduleName.empty()) {
63 resMgrKey.append("/").append(moduleName);
64 }
65 std::lock_guard<std::mutex> lock(resMgrLock);
66 auto iter = resMgrMap.find(resMgrKey);
67 if (iter != resMgrMap.end()) {
68 return resMgrMap[resMgrKey];
69 }
70 std::shared_ptr<ResourceManager> resourceManagerImpl(CreateResourceManager());
71 if (resourceManagerImpl == nullptr) {
72 HILOG_ERROR("CreateResourceManagerDef failed");
73 return nullptr;
74 }
75 bool result = false;
76 if (!overlayPath.empty()) {
77 result = resourceManagerImpl->AddResource(hapPath, overlayPath);
78 } else {
79 result = resourceManagerImpl->AddResource(hapPath.c_str());
80 }
81 if (!result) {
82 HILOG_ERROR("AddResource failed when CreateResourceManagerDef");
83 return nullptr;
84 }
85 resourceManagerImpl->UpdateResConfig(resConfig);
86 resMgrMap[resMgrKey] = resourceManagerImpl;
87 return resourceManagerImpl;
88 }
89
90 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
CreateResourceManagerExt(const std::string & bundleName,const int32_t appType)91 std::shared_ptr<ResourceManager> CreateResourceManagerExt(const std::string &bundleName, const int32_t appType)
92 {
93 if (bundleName.empty()) {
94 HILOG_ERROR("bundleName is empty when CreateResourceManagerExt");
95 return nullptr;
96 }
97 std::lock_guard<std::mutex> lock(resMgrExtLock);
98 std::shared_ptr<ResourceManager> resMgrExt;
99 if (!resMgrExtMgr->Init(resMgrExt, bundleName, appType) || resMgrExt == nullptr) {
100 HILOG_ERROR("ResourceManagerExt init fail");
101 return nullptr;
102 }
103 return resMgrExt;
104 }
105 #endif
106
CreateResourceManager(const std::string & bundleName,const std::string & moduleName,const std::string & hapPath,const std::vector<std::string> & overlayPath,ResConfig & resConfig,int32_t appType)107 std::shared_ptr<ResourceManager> CreateResourceManager(const std::string &bundleName, const std::string &moduleName,
108 const std::string &hapPath, const std::vector<std::string> &overlayPath, ResConfig &resConfig, int32_t appType)
109 {
110 if (appType == 0) {
111 return CreateResourceManagerDef(bundleName, moduleName, hapPath, overlayPath, resConfig);
112 } else {
113 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
114 return CreateResourceManagerExt(bundleName, appType);
115 #else
116 return nullptr;
117 #endif
118 }
119 }
120
GetSystemResourceManager()121 ResourceManager *GetSystemResourceManager()
122 {
123 return SystemResourceManager::GetSystemResourceManager();
124 }
125
GetSystemResourceManagerNoSandBox()126 ResourceManager *GetSystemResourceManagerNoSandBox()
127 {
128 return SystemResourceManager::GetSystemResourceManagerNoSandBox();
129 }
130
~ResourceManager()131 ResourceManager::~ResourceManager()
132 {}
133 } // namespace Resource
134 } // namespace Global
135 } // namespace OHOS
136