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 "utils/utils.h"
20
21 namespace OHOS {
22 namespace Global {
23 namespace Resource {
24 const std::string SystemResourceManager::SYSTEM_RESOURCE_PATH = "/data/storage/el1/bundle/ohos.global.systemres" \
25 "/ohos.global.systemres/assets/entry/resources.index";
26
27 const std::string SystemResourceManager::SYSTEM_RESOURCE_PATH_COMPRESSED = "/data/global/" \
28 "systemResources/SystemResources.hap";
29
30 const std::string SystemResourceManager::SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED = "/sys_prod/app/" \
31 "SystemResourcesOverlay/SystemResourcesOverlay.hap";
32
33 const std::string SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_PKG_PATH = "/system/app/ohos.global.systemres" \
34 "/SystemResources.hap";
35
36 const std::string SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_HAP_PATH = "/system/app/SystemResources" \
37 "/SystemResources.hap";
38
39 const std::string SystemResourceManager::SYSTEM_RESOURCE_EXT_NO_SAND_BOX_HAP_PATH = "/system/app/SystemResources" \
40 "/SystemResourcesExt.hap";
41
42 ResourceManagerImpl *SystemResourceManager::resourceManager_ = nullptr;
43
44 std::mutex SystemResourceManager::mutex_;
45
SystemResourceManager()46 SystemResourceManager::SystemResourceManager()
47 {}
48
~SystemResourceManager()49 SystemResourceManager::~SystemResourceManager()
50 {}
51
GetSystemResourceManager()52 ResourceManagerImpl *SystemResourceManager::GetSystemResourceManager()
53 {
54 // SystemAbility is not forked from appspawn, so SystemAbility should load sandbox system resource.
55 bool isCreated = CreateSystemResourceManager(true);
56 if (!isCreated) {
57 return nullptr;
58 }
59 return resourceManager_;
60 }
61
GetSystemResourceManagerNoSandBox()62 ResourceManagerImpl *SystemResourceManager::GetSystemResourceManagerNoSandBox()
63 {
64 RESMGR_HILOGI(RESMGR_TAG, "GetSystemResourceManagerNoSandBox");
65 // appspawn can only add no sandbox system resource path, so all app that forked from appspawn have
66 // one global resourceManager.
67 bool isCreated = CreateSystemResourceManager(false);
68 if (!isCreated) {
69 RESMGR_HILOGW(RESMGR_TAG, "CreateSystemResourceManager failed when GetSystemResourceManagerNoSandBox");
70 return nullptr;
71 }
72 return resourceManager_;
73 }
74
CreateSystemResourceManager(bool isSandbox)75 bool SystemResourceManager::CreateSystemResourceManager(bool isSandbox)
76 {
77 if (resourceManager_ != nullptr) {
78 return true;
79 }
80 std::lock_guard<std::mutex> lock(mutex_);
81 if (resourceManager_ == nullptr) {
82 ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl;
83 if (impl == nullptr) {
84 RESMGR_HILOGE(RESMGR_TAG, "new ResourceManagerImpl failed when CreateSystemResourceManager");
85 return false;
86 }
87 if (!impl->Init(true)) {
88 delete impl;
89 return false;
90 }
91 #if !defined(__ARKUI_CROSS__)
92 if (!LoadSystemResource(impl, isSandbox)) {
93 delete impl;
94 return false;
95 }
96 #endif
97 resourceManager_ = impl;
98 }
99 return true;
100 }
101
LoadSystemResource(ResourceManagerImpl * impl,bool isSandbox)102 bool SystemResourceManager::LoadSystemResource(ResourceManagerImpl *impl, bool isSandbox)
103 {
104 std::string sysPkgNamePath = SystemResourceManager::SYSTEM_RESOURCE_PATH;
105 std::string sysHapNamePath = SystemResourceManager::SYSTEM_RESOURCE_PATH_COMPRESSED;
106 std::string sysHapExtNamePath = SystemResourceManager::SYSTEM_RESOURCE_EXT_NO_SAND_BOX_HAP_PATH;
107 if (!isSandbox) {
108 sysPkgNamePath = SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_PKG_PATH;
109 sysHapNamePath = SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_HAP_PATH;
110 }
111 if (Utils::IsFileExist(sysPkgNamePath)) {
112 if (Utils::IsFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED)) {
113 vector<string> overlayPaths;
114 overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
115 return impl->AddResource(sysPkgNamePath.c_str(), overlayPaths);
116 }
117 return impl->AddResource(sysPkgNamePath.c_str());
118 }
119
120 if (Utils::IsFileExist(sysHapNamePath)) {
121 bool result = false;
122 if (Utils::IsFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED)) {
123 vector<string> overlayPaths;
124 overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
125 result = impl->AddResource(sysHapNamePath.c_str(), overlayPaths);
126 } else {
127 result = impl->AddResource(sysHapNamePath.c_str());
128 }
129 if (result && Utils::IsFileExist(sysHapExtNamePath)) {
130 result = impl->AddResource(sysHapExtNamePath.c_str());
131 }
132 return result;
133 }
134 return false;
135 }
136 } // namespace Resource
137 } // namespace Global
138 } // namespace OHOS
139