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/storage/el1/bundle/" \
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 ResourceManagerImpl *SystemResourceManager::resourceManager_ = nullptr;
40
41 std::mutex SystemResourceManager::mutex_;
42
SystemResourceManager()43 SystemResourceManager::SystemResourceManager()
44 {}
45
~SystemResourceManager()46 SystemResourceManager::~SystemResourceManager()
47 {}
48
GetSystemResourceManager()49 ResourceManagerImpl *SystemResourceManager::GetSystemResourceManager()
50 {
51 // SystemAbility is not forked from appspawn, so SystemAbility should load sandbox system resource.
52 bool isCreated = CreateSystemResourceManager(true);
53 if (!isCreated) {
54 return nullptr;
55 }
56 return resourceManager_;
57 }
58
GetSystemResourceManagerNoSandBox()59 ResourceManagerImpl *SystemResourceManager::GetSystemResourceManagerNoSandBox()
60 {
61 HILOG_INFO("GetSystemResourceManagerNoSandBox");
62 // appspawn can only add no sandbox system resource path, so all app that forked from appspawn have
63 // one global resourceManager.
64 bool isCreated = CreateSystemResourceManager(false);
65 if (!isCreated) {
66 HILOG_WARN("CreateSystemResourceManager failed when GetSystemResourceManagerNoSandBox");
67 return nullptr;
68 }
69 return resourceManager_;
70 }
71
CreateSystemResourceManager(bool isSandbox)72 bool SystemResourceManager::CreateSystemResourceManager(bool isSandbox)
73 {
74 if (resourceManager_ != nullptr) {
75 return true;
76 }
77 std::lock_guard<std::mutex> lock(mutex_);
78 if (resourceManager_ == nullptr) {
79 ResourceManagerImpl *impl = new (std::nothrow) ResourceManagerImpl;
80 if (impl == nullptr) {
81 HILOG_ERROR("new ResourceManagerImpl failed when CreateSystemResourceManager");
82 return false;
83 }
84 if (!impl->Init(true)) {
85 delete impl;
86 return false;
87 }
88 if (!LoadSystemResource(impl, isSandbox)) {
89 delete impl;
90 return false;
91 }
92 resourceManager_ = impl;
93 }
94 return true;
95 }
96
LoadSystemResource(ResourceManagerImpl * impl,bool isSandbox)97 bool SystemResourceManager::LoadSystemResource(ResourceManagerImpl *impl, bool isSandbox)
98 {
99 std::string sysPkgNamePath = SystemResourceManager::SYSTEM_RESOURCE_PATH;
100 std::string sysHapNamePath = SystemResourceManager::SYSTEM_RESOURCE_PATH_COMPRESSED;
101 if (!isSandbox) {
102 sysPkgNamePath = SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_PKG_PATH;
103 sysHapNamePath = SystemResourceManager::SYSTEM_RESOURCE_NO_SAND_BOX_HAP_PATH;
104 }
105 if (Utils::IsFileExist(sysPkgNamePath)) {
106 if (Utils::IsFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED)) {
107 vector<string> overlayPaths;
108 overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
109 return impl->AddResource(sysPkgNamePath.c_str(), overlayPaths);
110 }
111 return impl->AddResource(sysPkgNamePath.c_str());
112 }
113
114 if (Utils::IsFileExist(sysHapNamePath)) {
115 if (Utils::IsFileExist(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED)) {
116 vector<string> overlayPaths;
117 overlayPaths.push_back(SYSTEM_RESOURCE_OVERLAY_PATH_COMPRESSED);
118 return impl->AddResource(sysHapNamePath.c_str(), overlayPaths);
119 }
120 return impl->AddResource(sysHapNamePath.c_str());
121 }
122 return false;
123 }
124 } // namespace Resource
125 } // namespace Global
126 } // namespace OHOS
127