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 if (themePackManager->IsFirstLoadResource() || themePackManager->UpdateThemeId(currentId)
73 || themePackManager->IsUpdateByUserId(userId)) {
74 RESMGR_HILOGI(RESMGR_TAG, "CreateResourceManagerDef LoadThemeRes");
75 themePackManager->LoadThemeRes(bundleName, moduleName, userId);
76 }
77 return resourceManagerImpl;
78 }
79
80 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
CreateResourceManagerExt(const std::string &bundleName, const int32_t appType)81 std::shared_ptr<ResourceManager> CreateResourceManagerExt(const std::string &bundleName, const int32_t appType)
82 {
83 if (bundleName.empty()) {
84 RESMGR_HILOGE(RESMGR_TAG, "bundleName is empty when CreateResourceManagerExt");
85 return nullptr;
86 }
87 std::lock_guard<std::mutex> lock(resMgrExtLock);
88 std::shared_ptr<ResourceManager> resMgrExt;
89 if (!resMgrExtMgr->Init(resMgrExt, bundleName, appType) || resMgrExt == nullptr) {
90 RESMGR_HILOGE(RESMGR_TAG, "ResourceManagerExt init fail");
91 return nullptr;
92 }
93 return resMgrExt;
94 }
95 #endif
96
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)97 std::shared_ptr<ResourceManager> CreateResourceManager(const std::string &bundleName, const std::string &moduleName,
98 const std::string &hapPath, const std::vector<std::string> &overlayPath,
99 ResConfig &resConfig, int32_t appType, int32_t userId)
100 {
101 if (appType == 0) {
102 return CreateResourceManagerDef(bundleName, moduleName, hapPath, overlayPath, resConfig, userId);
103 } else {
104 #if !defined(__WINNT__) && !defined(__IDE_PREVIEW__) && !defined(__ARKUI_CROSS__)
105 return CreateResourceManagerExt(bundleName, appType);
106 #else
107 return nullptr;
108 #endif
109 }
110 }
111
GetSystemResourceManager()112 ResourceManager *GetSystemResourceManager()
113 {
114 return SystemResourceManager::GetSystemResourceManager();
115 }
116
GetSystemResourceManagerNoSandBox()117 ResourceManager *GetSystemResourceManagerNoSandBox()
118 {
119 return SystemResourceManager::GetSystemResourceManagerNoSandBox();
120 }
121
~ResourceManager()122 ResourceManager::~ResourceManager()
123 {}
124 } // namespace Resource
125 } // namespace Global
126 } // namespace OHOS
127