• 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 "system_resource_manager.h"
17 
18 #include "hilog_wrapper.h"
19 #include "resource_manager.h"
20 #include "utils/utils.h"
21 
22 namespace OHOS {
23 namespace Global {
24 namespace Resource {
25 const std::string SystemResourceManager::SYSTEM_RESOURCE_PATH = "/data/storage/el1/bundle/ohos.global.systemres" \
26     "/ohos.global.systemres/assets/entry/resources.index";
27 
28 const std::string SystemResourceManager::SYSTEM_RESOURCE_PATH_COMPRESSED = "/data/global/" \
29     "systemResources/SystemResources.hap";
30 
31 const std::string SystemResourceManager::SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED = "/sys_prod/app/" \
32     "SystemResourcesOverlay/SystemResourcesOverlay.hap";
33 
34 const std::string SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_PKG_PATH = "/system/app/ohos.global.systemres" \
35     "/SystemResources.hap";
36 
37 const std::string SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_HAP_PATH = "/system/app/SystemResources" \
38     "/SystemResources.hap";
39 
40 const std::string SystemResourceManager::SYSTEM_RESOURCE_EXT_NO_SAND_BOX_HAP_PATH = "/system/app/SystemResources" \
41     "/SystemResourcesExt.hap";
42 
43 ResourceManagerImpl *SystemResourceManager::resourceManager_ = nullptr;
44 
45 std::weak_ptr<ResourceManagerImpl> SystemResourceManager::weakResourceManager_;
46 
47 std::mutex SystemResourceManager::mutex_;
48 
49 std::shared_ptr<ResourceManagerImpl> SystemResourceManager::sysResMgr_ = nullptr;
50 
51 std::mutex SystemResourceManager::sysResMgrMutex_;
52 
53 std::shared_ptr<ResConfigImpl> SystemResourceManager::resConfig_ = nullptr;
54 
55 bool SystemResourceManager::isUpdateAppConfig_ = true;
56 
57 bool SystemResourceManager::isThemeSystemResEnable_ = false;
58 
SystemResourceManager()59 SystemResourceManager::SystemResourceManager()
60 {}
61 
~SystemResourceManager()62 SystemResourceManager::~SystemResourceManager()
63 {}
64 
GetSystemResourceManager()65 ResourceManagerImpl *SystemResourceManager::GetSystemResourceManager()
66 {
67     // SystemAbility is not forked from appspawn, so SystemAbility should load sandbox system resource.
68     return CreateSystemResourceManager(true);
69 }
70 
GetSystemResourceManagerNoSandBox()71 ResourceManagerImpl *SystemResourceManager::GetSystemResourceManagerNoSandBox()
72 {
73     RESMGR_HILOGI(RESMGR_TAG, "GetSystemResourceManagerNoSandBox");
74     // appspawn can only add no sandbox system resource path, so all app that forked from appspawn have
75     // one global resourceManager.
76     return CreateSystemResourceManager(false);
77 }
78 
InitResourceManager(ResourceManagerImpl * impl)79 bool SystemResourceManager::InitResourceManager(ResourceManagerImpl *impl)
80 {
81     if (!impl->Init(true)) {
82         RESMGR_HILOGE(RESMGR_TAG, "init failed");
83         return false;
84     }
85     std::shared_ptr<ResourceManagerImpl> sysResMgr = weakResourceManager_.lock();
86     if (!sysResMgr) {
87         sysResMgr = CreateSystemResourceManager();
88     }
89     if (!impl->AddSystemResource(sysResMgr)) {
90         RESMGR_HILOGE(RESMGR_TAG, "add system resource failed");
91         return false;
92     }
93     return true;
94 }
95 
CreateSystemResourceManager(bool isSandbox)96 ResourceManagerImpl *SystemResourceManager::CreateSystemResourceManager(bool isSandbox)
97 {
98     std::lock_guard<std::mutex> lock(mutex_);
99     if (resourceManager_ == nullptr) {
100         ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl;
101         if (impl == nullptr) {
102             RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when CreateSystemResourceManager");
103             return nullptr;
104         }
105         if (!InitResourceManager(impl)) {
106             delete impl;
107             return nullptr;
108         }
109         resourceManager_ = impl;
110     }
111     return resourceManager_;
112 }
113 
LoadSystemResource(ResourceManagerImpl * impl,bool isSandbox)114 bool SystemResourceManager::LoadSystemResource(ResourceManagerImpl *impl, bool isSandbox)
115 {
116     std::string sysPkgNamePath = SystemResourceManager::SYSTEM_RESOURCE_PATH;
117     std::string sysHapNamePath = SystemResourceManager::SYSTEM_RESOURCE_PATH_COMPRESSED;
118     std::string sysHapExtNamePath = SystemResourceManager::SYSTEM_RESOURCE_EXT_NO_SAND_BOX_HAP_PATH;
119     if (!isSandbox) {
120         sysPkgNamePath = SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_PKG_PATH;
121         sysHapNamePath = SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_HAP_PATH;
122     }
123     if (Utils::IsFileExist(sysPkgNamePath)) {
124         if (Utils::IsFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED)) {
125             vector<string> overlayPaths;
126             overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
127             return impl->AddResource(sysPkgNamePath.c_str(), overlayPaths);
128         }
129         return impl->AddResource(sysPkgNamePath.c_str());
130     }
131 
132     if (Utils::IsFileExist(sysHapNamePath)) {
133         bool result = false;
134         if (Utils::IsFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED)) {
135             vector<string> overlayPaths;
136             overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
137             result = impl->AddResource(sysHapNamePath.c_str(), overlayPaths);
138         } else {
139             result = impl->AddResource(sysHapNamePath.c_str());
140         }
141         if (result && Utils::IsFileExist(sysHapExtNamePath)) {
142             result = impl->AddResource(sysHapExtNamePath.c_str());
143         }
144         return result;
145     }
146     return false;
147 }
148 
ReleaseSystemResourceManager()149 void SystemResourceManager::ReleaseSystemResourceManager()
150 {
151     std::lock_guard<std::mutex> lock(mutex_);
152     if (resourceManager_ != nullptr) {
153         delete resourceManager_;
154         resourceManager_ = nullptr;
155         RESMGR_HILOGI(RESMGR_TAG, "ReleaseSystemResourceManager success");
156     } else {
157         RESMGR_HILOGI(RESMGR_TAG, "SystemResourceManager has been released");
158     }
159 }
160 
AddSystemResource(ResourceManagerImpl * appResMgr)161 bool SystemResourceManager::AddSystemResource(ResourceManagerImpl *appResMgr)
162 {
163     if (!appResMgr) {
164         return false;
165     }
166 
167     std::lock_guard<std::mutex> lock(mutex_);
168     std::shared_ptr<ResourceManagerImpl> sysResMgr = weakResourceManager_.lock();
169     if (!sysResMgr) {
170         sysResMgr = CreateSystemResourceManager();
171     }
172     appResMgr->AddSystemResource(sysResMgr);
173     return true;
174 }
175 
CreateSystemResourceManager()176 std::shared_ptr<ResourceManagerImpl> SystemResourceManager::CreateSystemResourceManager()
177 {
178     std::shared_ptr<ResourceManagerImpl> sysResMgr = std::make_shared<ResourceManagerImpl>();
179     if (!sysResMgr) {
180         return nullptr;
181     }
182     if (!sysResMgr->Init(true)) {
183         return nullptr;
184     }
185 #if !defined(__ARKUI_CROSS__) && !defined(__IDE_PREVIEW__)
186     if (!LoadSystemResource(sysResMgr.get(), false) && !LoadSystemResource(sysResMgr.get(), true)) {
187         return nullptr;
188     }
189 #endif
190     weakResourceManager_ = sysResMgr;
191     return sysResMgr;
192 }
193 
CreateSysResourceManager()194 std::shared_ptr<ResourceManagerImpl> SystemResourceManager::CreateSysResourceManager()
195 {
196     std::lock_guard<std::mutex> lock(sysResMgrMutex_);
197     if (sysResMgr_ != nullptr) {
198         return sysResMgr_;
199     }
200     sysResMgr_ = std::make_shared<ResourceManagerImpl>();
201     if (sysResMgr_ == nullptr) {
202         RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when CreateSysResourceManager");
203         return nullptr;
204     }
205 
206     if (!InitResourceManager(sysResMgr_.get())) {
207         RESMGR_HILOGE(RESMGR_TAG, "init sys resource manager failed");
208         return nullptr;
209     }
210 
211 #if defined(__ARKUI_CROSS__) || defined(__IDE_PREVIEW__)
212     AddSystemResourceForPreview(resourceManager_);
213 #endif
214     if (resConfig_ != nullptr) {
215         UpdateResConfig(*resConfig_, isThemeSystemResEnable_);
216     }
217     return sysResMgr_;
218 }
219 
SaveResConfig(ResConfigImpl & resConfig,bool isThemeSystemResEnable)220 void SystemResourceManager::SaveResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable)
221 {
222     if (resConfig_ == nullptr) {
223         resConfig_ = std::make_shared<ResConfigImpl>();
224     }
225 
226     if (resConfig_ == nullptr) {
227         return;
228     }
229 
230     resConfig_->Copy(resConfig, true);
231     if (isUpdateAppConfig_) {
232         isUpdateAppConfig_ = false;
233         isThemeSystemResEnable_ = isThemeSystemResEnable;
234     }
235 }
236 
UpdateResConfig(ResConfigImpl & resConfig,bool isThemeSystemResEnable)237 void SystemResourceManager::UpdateResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable)
238 {
239     if (sysResMgr_ == nullptr) {
240         return;
241     }
242     auto hapManager = sysResMgr_->GetHapManager();
243     if (hapManager == nullptr) {
244         RESMGR_HILOGE(RESMGR_TAG, "sys resource manager hapManager is null");
245         return;
246     }
247     hapManager->UpdateResConfig(resConfig);
248     hapManager->UpdateAppConfigForSysResManager(resConfig.GetAppDarkRes(), isThemeSystemResEnable);
249 }
250 
UpdateSysResConfig(ResConfigImpl & resConfig,bool isThemeSystemResEnable)251 void SystemResourceManager::UpdateSysResConfig(ResConfigImpl &resConfig, bool isThemeSystemResEnable)
252 {
253     std::lock_guard<std::mutex> lock(sysResMgrMutex_);
254     if (resConfig.IsInvalidResConfig()) {
255         return;
256     }
257 
258     if (sysResMgr_ == nullptr) {
259         SaveResConfig(resConfig, isThemeSystemResEnable);
260         return;
261     }
262     UpdateResConfig(resConfig, isThemeSystemResEnable);
263 }
264 
265 #if defined(__ARKUI_CROSS__) || defined(__IDE_PREVIEW__)
AddSystemResourceForPreview(ResourceManagerImpl * resMgr)266 void SystemResourceManager::AddSystemResourceForPreview(ResourceManagerImpl* resMgr)
267 {
268     if (resMgr == nullptr || sysResMgr_ == nullptr) {
269         return;
270     }
271     sysResMgr_->AddSystemResource(resMgr);
272 }
273 #endif
274 } // namespace Resource
275 } // namespace Global
276 } // namespace OHOS
277