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
SystemResourceManager()49 SystemResourceManager::SystemResourceManager()
50 {}
51
~SystemResourceManager()52 SystemResourceManager::~SystemResourceManager()
53 {}
54
GetSystemResourceManager()55 ResourceManagerImpl *SystemResourceManager::GetSystemResourceManager()
56 {
57 // SystemAbility is not forked from appspawn, so SystemAbility should load sandbox system resource.
58 return CreateSystemResourceManager(true);
59 }
60
GetSystemResourceManagerNoSandBox()61 ResourceManagerImpl *SystemResourceManager::GetSystemResourceManagerNoSandBox()
62 {
63 RESMGR_HILOGI(RESMGR_TAG, "GetSystemResourceManagerNoSandBox");
64 // appspawn can only add no sandbox system resource path, so all app that forked from appspawn have
65 // one global resourceManager.
66 return CreateSystemResourceManager(false);
67 }
68
CreateSystemResourceManager(bool isSandbox)69 ResourceManagerImpl *SystemResourceManager::CreateSystemResourceManager(bool isSandbox)
70 {
71 std::lock_guard<std::mutex> lock(mutex_);
72 if (resourceManager_ == nullptr) {
73 ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl;
74 if (impl == nullptr) {
75 RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when CreateSystemResourceManager");
76 return nullptr;
77 }
78 if (!impl->Init(true)) {
79 delete impl;
80 return nullptr;
81 }
82 std::shared_ptr<ResourceManagerImpl> sysResMgr = weakResourceManager_.lock();
83 if (!sysResMgr) {
84 sysResMgr = CreateSystemResourceManager();
85 }
86 if (!impl->AddSystemResource(sysResMgr)) {
87 delete impl;
88 return nullptr;
89 }
90 resourceManager_ = impl;
91 }
92 return resourceManager_;
93 }
94
LoadSystemResource(ResourceManagerImpl * impl,bool isSandbox)95 bool SystemResourceManager::LoadSystemResource(ResourceManagerImpl *impl, bool isSandbox)
96 {
97 std::string sysPkgNamePath = SystemResourceManager::SYSTEM_RESOURCE_PATH;
98 std::string sysHapNamePath = SystemResourceManager::SYSTEM_RESOURCE_PATH_COMPRESSED;
99 std::string sysHapExtNamePath = SystemResourceManager::SYSTEM_RESOURCE_EXT_NO_SAND_BOX_HAP_PATH;
100 if (!isSandbox) {
101 sysPkgNamePath = SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_PKG_PATH;
102 sysHapNamePath = SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_HAP_PATH;
103 }
104 if (Utils::IsFileExist(sysPkgNamePath)) {
105 if (Utils::IsFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED)) {
106 vector<string> overlayPaths;
107 overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
108 return impl->AddResource(sysPkgNamePath.c_str(), overlayPaths);
109 }
110 return impl->AddResource(sysPkgNamePath.c_str());
111 }
112
113 if (Utils::IsFileExist(sysHapNamePath)) {
114 bool result = false;
115 if (Utils::IsFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED)) {
116 vector<string> overlayPaths;
117 overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
118 result = impl->AddResource(sysHapNamePath.c_str(), overlayPaths);
119 } else {
120 result = impl->AddResource(sysHapNamePath.c_str());
121 }
122 if (result && Utils::IsFileExist(sysHapExtNamePath)) {
123 result = impl->AddResource(sysHapExtNamePath.c_str());
124 }
125 return result;
126 }
127 return false;
128 }
129
ReleaseSystemResourceManager()130 void SystemResourceManager::ReleaseSystemResourceManager()
131 {
132 std::lock_guard<std::mutex> lock(mutex_);
133 if (resourceManager_ != nullptr) {
134 delete resourceManager_;
135 resourceManager_ = nullptr;
136 RESMGR_HILOGI(RESMGR_TAG, "ReleaseSystemResourceManager success");
137 } else {
138 RESMGR_HILOGI(RESMGR_TAG, "SystemResourceManager has been released");
139 }
140 }
141
AddSystemResource(ResourceManagerImpl * appResMgr)142 bool SystemResourceManager::AddSystemResource(ResourceManagerImpl *appResMgr)
143 {
144 if (!appResMgr) {
145 return false;
146 }
147
148 std::lock_guard<std::mutex> lock(mutex_);
149 std::shared_ptr<ResourceManagerImpl> sysResMgr = weakResourceManager_.lock();
150 if (!sysResMgr) {
151 sysResMgr = CreateSystemResourceManager();
152 }
153 appResMgr->AddSystemResource(sysResMgr);
154 return true;
155 }
156
CreateSystemResourceManager()157 std::shared_ptr<ResourceManagerImpl> SystemResourceManager::CreateSystemResourceManager()
158 {
159 std::shared_ptr<ResourceManagerImpl> sysResMgr = std::make_shared<ResourceManagerImpl>();
160 if (!sysResMgr) {
161 return nullptr;
162 }
163 if (!sysResMgr->Init(true)) {
164 return nullptr;
165 }
166 #if !defined(__ARKUI_CROSS__)
167 if (!LoadSystemResource(sysResMgr.get(), false) && !LoadSystemResource(sysResMgr.get(), true)) {
168 return nullptr;
169 }
170 #endif
171 weakResourceManager_ = sysResMgr;
172 return sysResMgr;
173 }
174 } // namespace Resource
175 } // namespace Global
176 } // namespace OHOS
177