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