• 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 
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 
AddSystemResource(ResourceManagerImpl * appResMgr)130 bool SystemResourceManager::AddSystemResource(ResourceManagerImpl *appResMgr)
131 {
132     if (!appResMgr) {
133         return false;
134     }
135 
136     std::lock_guard<std::mutex> lock(mutex_);
137     std::shared_ptr<ResourceManagerImpl> sysResMgr = weakResourceManager_.lock();
138     if (!sysResMgr) {
139         sysResMgr = CreateSystemResourceManager();
140     }
141     appResMgr->AddSystemResource(sysResMgr);
142     return true;
143 }
144 
CreateSystemResourceManager()145 std::shared_ptr<ResourceManagerImpl> SystemResourceManager::CreateSystemResourceManager()
146 {
147     std::shared_ptr<ResourceManagerImpl> sysResMgr = std::make_shared<ResourceManagerImpl>();
148     if (!sysResMgr) {
149         return nullptr;
150     }
151     if (!sysResMgr->Init(true)) {
152         return nullptr;
153     }
154 #if !defined(__ARKUI_CROSS__)
155     if (!LoadSystemResource(sysResMgr.get(), false) && !LoadSystemResource(sysResMgr.get(), true)) {
156         return nullptr;
157     }
158 #endif
159     weakResourceManager_ = sysResMgr;
160     return sysResMgr;
161 }
162 
163 } // namespace Resource
164 } // namespace Global
165 } // namespace OHOS
166