From bec868d122c40ea43db808b132ad9bdc9da33cee Mon Sep 17 00:00:00 2001 From: raul Date: Tue, 4 Jul 2023 22:48:06 +0800 Subject: [PATCH] fixed 29fc08b from https://gitee.com/raulnaruto/ability_ability_runtime_ets/pulls/5893 import support hsp Signed-off-by: raul Change-Id: Ifb5f364ea4d144cccc52ce27644383a8c9484b17 --- .../native/runtime/js_module_reader.cpp | 72 +++++++++++++++---- frameworks/native/runtime/js_module_reader.h | 8 ++- .../native/runtime/js_module_searcher.h | 2 +- frameworks/native/runtime/js_runtime.cpp | 3 +- 4 files changed, 70 insertions(+), 15 deletions(-) diff --git a/frameworks/native/runtime/js_module_reader.cpp b/frameworks/native/runtime/js_module_reader.cpp index 6dd73ff4d6a..568569f7ce0 100755 --- a/frameworks/native/runtime/js_module_reader.cpp +++ b/frameworks/native/runtime/js_module_reader.cpp @@ -30,7 +30,8 @@ namespace OHOS { namespace AbilityRuntime { using IBundleMgr = AppExecFwk::IBundleMgr; -JsModuleReader::JsModuleReader(const std::string& bundleName, const std::string& hapPath) : JsModuleSearcher(bundleName) +JsModuleReader::JsModuleReader(const std::string& bundleName, const std::string& hapPath, bool isFormRender) + : JsModuleSearcher(bundleName), isFormRender_(isFormRender) { if (!hapPath.empty() && hapPath.find(std::string(SYS_ABS_CODE_PATH)) == 0) { isSystemPath_ = true; @@ -41,25 +42,20 @@ JsModuleReader::JsModuleReader(const std::string& bundleName, const std::string& std::vector JsModuleReader::operator()(const std::string& inputPath) const { + HILOG_INFO("JsModuleReader operator start: %{private}s", inputPath.c_str()); HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); std::vector buffer; if (inputPath.empty()) { HILOG_ERROR("inputPath is empty"); return buffer; } - std::string realHapPath; - std::string suffix = std::string(SHARED_FILE_SUFFIX); - if (isSystemPath_) { - realHapPath = GetPresetAppHapPath(inputPath); - } else { - realHapPath = std::string(ABS_CODE_PATH) + inputPath + suffix; - } - if (realHapPath.empty() || - realHapPath.length() < suffix.length() || - realHapPath.compare(realHapPath.length() - suffix.length(), suffix.length(), suffix) != 0) { - HILOG_ERROR("failed to obtain realHapPath"); + + auto realHapPath = GetAppHspPath(inputPath); + if (realHapPath.empty()) { + HILOG_ERROR("realHapPath is empty"); return buffer; } + bool newCreate = false; std::shared_ptr extractor = ExtractorUtil::GetExtractor(realHapPath, newCreate); if (extractor == nullptr) { @@ -76,6 +72,58 @@ std::vector JsModuleReader::operator()(const std::string& inputPath) co return buffer; } +std::string JsModuleReader::GetAppHspPath(const std::string& inputPath) const +{ + if (isFormRender_) { + return GetFormAppHspPath(inputPath); + } + return GetCommonAppHspPath(inputPath); +} + +std::string JsModuleReader::GetFormAppHspPath(const std::string& inputPath) const +{ + std::string realHapPath; + std::string suffix = std::string(SHARED_FILE_SUFFIX); + realHapPath.append("/data/bundles/") + .append(bundleName_).append("/") + .append(GetModuleName(inputPath)) + .append(SHARED_FILE_SUFFIX); + + HILOG_INFO("realHapPath: %{private}s", realHapPath.c_str()); + if (realHapPath.empty() || + realHapPath.length() < suffix.length() || + realHapPath.compare(realHapPath.length() - suffix.length(), suffix.length(), suffix) != 0) { + HILOG_ERROR("failed to obtain realHapPath"); + return realHapPath; + } + return realHapPath; +} + +std::string JsModuleReader::GetModuleName(const std::string& inputPath) const +{ + return inputPath.substr(inputPath.find_last_of("/") + 1); +} + +std::string JsModuleReader::GetCommonAppHspPath(const std::string& inputPath) const +{ + std::string realHapPath; + std::string suffix = std::string(SHARED_FILE_SUFFIX); + if (isSystemPath_) { + realHapPath = GetPresetAppHapPath(inputPath); + } else { + realHapPath = std::string(ABS_CODE_PATH) + inputPath + suffix; + } + + HILOG_INFO("realHapPath: %{private}s", realHapPath.c_str()); + if (realHapPath.empty() || + realHapPath.length() < suffix.length() || + realHapPath.compare(realHapPath.length() - suffix.length(), suffix.length(), suffix) != 0) { + HILOG_ERROR("failed to obtain realHapPath"); + return realHapPath; + } + return realHapPath; +} + std::string JsModuleReader::GetPresetAppHapPath(const std::string& inputPath) const { std::string presetAppHapPath; diff --git a/frameworks/native/runtime/js_module_reader.h b/frameworks/native/runtime/js_module_reader.h index d3635a3918b..8290dfe5049 100755 --- a/frameworks/native/runtime/js_module_reader.h +++ b/frameworks/native/runtime/js_module_reader.h @@ -33,7 +33,7 @@ public: static constexpr char BUNDLE[] = "bundle/"; static constexpr char MERGE_ABC_PATH[] = "ets/modules.abc"; static constexpr char SHARED_FILE_SUFFIX[] = ".hsp"; - JsModuleReader(const std::string& bundleName, const std::string& hapPath); + JsModuleReader(const std::string& bundleName, const std::string& hapPath, bool isFormRender = false); ~JsModuleReader() = default; JsModuleReader(const JsModuleReader&) = default; @@ -45,7 +45,13 @@ public: std::string GetPresetAppHapPath(const std::string& inputPath) const; private: + std::string GetAppHspPath(const std::string& inputPath) const; + std::string GetCommonAppHspPath(const std::string& inputPath) const; + std::string GetFormAppHspPath(const std::string& inputPath) const; + std::string GetModuleName(const std::string& inputPath) const; + bool isSystemPath_ = false; + bool isFormRender_ = false; }; } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/native/runtime/js_module_searcher.h b/frameworks/native/runtime/js_module_searcher.h index d0d6506f182..cea799ca286 100644 --- a/frameworks/native/runtime/js_module_searcher.h +++ b/frameworks/native/runtime/js_module_searcher.h @@ -34,7 +34,7 @@ public: std::string operator()(const std::string& curJsModulePath, const std::string& newJsModuleUri) const; -private: +protected: std::string bundleName_; }; } // namespace AbilityRuntime diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index f07f3982206..4f43ff27280 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -531,7 +531,8 @@ bool JsRuntime::Initialize(const Options& options) panda::JSNApi::SetBundle(vm, options.isBundle); panda::JSNApi::SetBundleName(vm, options.bundleName); - panda::JSNApi::SetHostResolveBufferTracker(vm, JsModuleReader(options.bundleName, options.hapPath)); + panda::JSNApi::SetHostResolveBufferTracker( + vm, JsModuleReader(options.bundleName, options.hapPath, options.isUnique)); isModular = !panda::JSNApi::IsBundle(vm); } } -- Gitee