1 /*
2 * Copyright (c) 2022 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 "js_module_reader.h"
17
18 #include "bundle_info.h"
19 #include "bundle_mgr_proxy.h"
20 #include "file_path_utils.h"
21 #include "hilog_wrapper.h"
22 #include "hitrace_meter.h"
23 #include "iservice_registry.h"
24 #include "js_runtime_utils.h"
25 #include "system_ability_definition.h"
26
27 using namespace OHOS::AbilityBase;
28
29 namespace OHOS {
30 namespace AbilityRuntime {
31 using IBundleMgr = AppExecFwk::IBundleMgr;
32
JsModuleReader(const std::string & bundleName,const std::string & hapPath,bool isFormRender)33 JsModuleReader::JsModuleReader(const std::string& bundleName, const std::string& hapPath, bool isFormRender)
34 : JsModuleSearcher(bundleName), isFormRender_(isFormRender)
35 {
36 if (!hapPath.empty() && hapPath.find(std::string(ABS_DATA_CODE_PATH)) != 0) {
37 isSystemPath_ = true;
38 } else {
39 isSystemPath_ = false;
40 }
41 }
42
operator ()(const std::string & inputPath,uint8_t ** buff,size_t * buffSize) const43 bool JsModuleReader::operator()(const std::string& inputPath, uint8_t **buff, size_t *buffSize) const
44 {
45 HILOG_INFO("JsModuleReader operator start: %{private}s", inputPath.c_str());
46 HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
47 if (inputPath.empty() || buff == nullptr || buffSize == nullptr) {
48 HILOG_ERROR("Invalid param");
49 return false;
50 }
51
52 auto realHapPath = GetAppHspPath(inputPath);
53 if (realHapPath.empty()) {
54 HILOG_ERROR("realHapPath is empty");
55 return false;
56 }
57
58 bool newCreate = false;
59 std::shared_ptr<Extractor> extractor = ExtractorUtil::GetExtractor(realHapPath, newCreate);
60 if (extractor == nullptr) {
61 HILOG_ERROR("realHapPath %{private}s GetExtractor failed", realHapPath.c_str());
62 return false;
63 }
64
65 auto data = extractor->GetSafeData(MERGE_ABC_PATH);
66 if (!data) {
67 HILOG_ERROR("get mergeAbc fileBuffer failed");
68 return false;
69 }
70
71 *buff = data->GetDataPtr();
72 *buffSize = data->GetDataLen();
73 return true;
74 }
75
GetAppHspPath(const std::string & inputPath) const76 std::string JsModuleReader::GetAppHspPath(const std::string& inputPath) const
77 {
78 if (isFormRender_) {
79 return GetFormAppHspPath(inputPath);
80 }
81 return GetCommonAppHspPath(inputPath);
82 }
83
GetFormAppHspPath(const std::string & inputPath) const84 std::string JsModuleReader::GetFormAppHspPath(const std::string& inputPath) const
85 {
86 std::string realHapPath;
87 std::string suffix = std::string(SHARED_FILE_SUFFIX);
88 realHapPath.append("/data/bundles/")
89 .append(bundleName_).append("/")
90 .append(GetModuleName(inputPath))
91 .append(SHARED_FILE_SUFFIX);
92
93 HILOG_INFO("realHapPath: %{private}s", realHapPath.c_str());
94 if (realHapPath.empty() ||
95 realHapPath.length() < suffix.length() ||
96 realHapPath.compare(realHapPath.length() - suffix.length(), suffix.length(), suffix) != 0) {
97 HILOG_ERROR("failed to obtain realHapPath");
98 return realHapPath;
99 }
100 return realHapPath;
101 }
102
GetModuleName(const std::string & inputPath) const103 std::string JsModuleReader::GetModuleName(const std::string& inputPath) const
104 {
105 return inputPath.substr(inputPath.find_last_of("/") + 1);
106 }
107
GetCommonAppHspPath(const std::string & inputPath) const108 std::string JsModuleReader::GetCommonAppHspPath(const std::string& inputPath) const
109 {
110 std::string suffix = std::string(SHARED_FILE_SUFFIX);
111 std::string realHapPath = GetPresetAppHapPath(inputPath);
112 if ((realHapPath.find(ABS_DATA_CODE_PATH) == 0) || (realHapPath == inputPath)) {
113 realHapPath = std::string(ABS_CODE_PATH) + inputPath + suffix;
114 }
115
116 HILOG_INFO("realHapPath: %{private}s", realHapPath.c_str());
117 if (realHapPath.empty() ||
118 realHapPath.length() < suffix.length() ||
119 realHapPath.compare(realHapPath.length() - suffix.length(), suffix.length(), suffix) != 0) {
120 HILOG_ERROR("failed to obtain realHapPath");
121 return realHapPath;
122 }
123 return realHapPath;
124 }
125
GetPresetAppHapPath(const std::string & inputPath) const126 std::string JsModuleReader::GetPresetAppHapPath(const std::string& inputPath) const
127 {
128 std::string presetAppHapPath = inputPath;
129 std::string moduleName = inputPath.substr(inputPath.find_last_of("/") + 1);
130 if (moduleName.empty()) {
131 HILOG_ERROR("failed to obtain moduleName.");
132 return presetAppHapPath;
133 }
134 auto systemAbilityManagerClient = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
135 if (!systemAbilityManagerClient) {
136 HILOG_ERROR("fail to get system ability mgr.");
137 return presetAppHapPath;
138 }
139 auto remoteObject = systemAbilityManagerClient->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID);
140 if (!remoteObject) {
141 HILOG_ERROR("fail to get bundle manager proxy.");
142 return presetAppHapPath;
143 }
144 auto bundleMgrProxy = iface_cast<IBundleMgr>(remoteObject);
145 if (inputPath.find_first_of("/") == inputPath.find_last_of("/")) {
146 AppExecFwk::BundleInfo bundleInfo;
147 auto getInfoResult = bundleMgrProxy->GetBundleInfoForSelf(static_cast<int32_t>(AppExecFwk::GetBundleInfoFlag::
148 GET_BUNDLE_INFO_WITH_HAP_MODULE), bundleInfo);
149 if (getInfoResult != 0 || bundleInfo.hapModuleInfos.empty()) {
150 HILOG_ERROR("GetBundleInfoForSelf failed.");
151 return presetAppHapPath;
152 }
153 for (auto hapModuleInfo : bundleInfo.hapModuleInfos) {
154 if (hapModuleInfo.moduleName == moduleName) {
155 presetAppHapPath = hapModuleInfo.hapPath;
156 break;
157 }
158 }
159 } else {
160 std::vector<AppExecFwk::BaseSharedBundleInfo> baseSharedBundleInfos;
161 if (bundleMgrProxy->GetBaseSharedBundleInfos(bundleName_, baseSharedBundleInfos) != 0) {
162 HILOG_ERROR("GetBaseSharedBundleInfos failed.");
163 return presetAppHapPath;
164 }
165 std::string tmpPath = inputPath.substr(inputPath.find_first_of("/") + 1);
166 const std::string sharedBundleName = tmpPath.substr(0, tmpPath.find_first_of("/"));
167 for (const auto &info : baseSharedBundleInfos) {
168 if ((info.bundleName == sharedBundleName) && (info.moduleName == moduleName)) {
169 presetAppHapPath = info.hapPath;
170 break;
171 }
172 }
173 }
174 return presetAppHapPath;
175 }
176 } // namespace AbilityRuntime
177 } // namespace OHOS