From c826f327deb6cee066cfdd923256b4d7bfbd3877 Mon Sep 17 00:00:00 2001 From: dy_study Date: Tue, 9 Aug 2022 19:59:59 +0800 Subject: [PATCH] =?UTF-8?q?IssueNo:#I5825N=20Description:=20=E4=B8=8D?= =?UTF-8?q?=E8=A7=A3=E5=8E=8BHAP=E5=8A=A0=E8=BD=BD=E5=BA=94=E7=94=A8?= =?UTF-8?q?=E4=BB=A3=E7=A0=81=20Sig:SIG=5FApplicationFramework=20Feature?= =?UTF-8?q?=20or=20Bugfix:Feature=20Binary=20Source:=20No?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: dy_study Change-Id: I4255164c1df585e91f789f5b31d04c0eed976034 --- ability_runtime.gni | 1 + .../native/ability_runtime/js_ability.cpp | 2 +- .../native/form_runtime/js_form_extension.cpp | 2 +- .../ability/native/js_service_extension.cpp | 2 +- .../native/js_static_subscriber_extension.cpp | 2 +- .../runner_runtime/js_test_runner.cpp | 8 +- .../ability_runtime/app/js_ability_stage.cpp | 4 +- frameworks/native/appkit/app/main_thread.cpp | 2 + .../native/runtime/js_module_searcher.cpp | 91 ++++++++++--------- .../native/runtime/js_module_searcher.h | 9 ++ frameworks/native/runtime/js_runtime.cpp | 57 +++++++++--- .../native/runtime/js_runtime_utils.cpp | 42 +++++++++ .../native/runtime/runtime_extractor.cpp | 38 ++++++++ interfaces/inner_api/runtime/BUILD.gn | 30 +++++- .../inner_api/runtime/include/js_runtime.h | 12 +-- .../runtime/include/js_runtime_utils.h | 5 + .../inner_api/runtime/include/runtime.h | 1 + .../runtime/include/runtime_extractor.h | 38 ++++++++ .../runner_runtime/js_test_runner.h | 1 + .../common/include}/ability_constants.h | 2 + 20 files changed, 277 insertions(+), 72 deletions(-) create mode 100755 frameworks/native/runtime/runtime_extractor.cpp create mode 100755 interfaces/inner_api/runtime/include/runtime_extractor.h rename {interfaces/kits/native/appkit/ability_runtime/context => services/common/include}/ability_constants.h (95%) diff --git a/ability_runtime.gni b/ability_runtime.gni index 811145eef4..bebd583ee0 100644 --- a/ability_runtime.gni +++ b/ability_runtime.gni @@ -25,6 +25,7 @@ ace_engine_path = "//foundation/arkui/ace_engine" bundlefwk_path = "//foundation/bundlemanager/bundle_framework" bundlefwk_inner_api_path = "${bundlefwk_path}/interfaces/inner_api" +bundlefwk_services_path = "${bundlefwk_path}/services" multimedia_path = "//foundation/multimedia/image_standard" multimodalinput_path = "//foundation/multimodalinput/input" diff --git a/frameworks/native/ability/native/ability_runtime/js_ability.cpp b/frameworks/native/ability/native/ability_runtime/js_ability.cpp index c0fb7a8028..b1670cd8d8 100644 --- a/frameworks/native/ability/native/ability_runtime/js_ability.cpp +++ b/frameworks/native/ability/native/ability_runtime/js_ability.cpp @@ -113,7 +113,7 @@ void JsAbility::Init(const std::shared_ptr &abilityInfo, auto &engine = jsRuntime_.GetNativeEngine(); jsAbilityObj_ = - jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo->compileMode == AppExecFwk::CompileMode::ES_MODULE); + jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo->compileMode == AppExecFwk::CompileMode::ES_MODULE, abilityInfo->hapPath); if (jsAbilityObj_ == nullptr) { HILOG_ERROR("Failed to get AbilityStage object"); return; diff --git a/frameworks/native/ability/native/form_runtime/js_form_extension.cpp b/frameworks/native/ability/native/form_runtime/js_form_extension.cpp index 63889b3f34..fa8740fdb7 100644 --- a/frameworks/native/ability/native/form_runtime/js_form_extension.cpp +++ b/frameworks/native/ability/native/form_runtime/js_form_extension.cpp @@ -92,7 +92,7 @@ void JsFormExtension::Init(const std::shared_ptr &record, HandleScope handleScope(jsRuntime_); auto& engine = jsRuntime_.GetNativeEngine(); - jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->compileMode == CompileMode::ES_MODULE); + jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->compileMode == CompileMode::ES_MODULE, abilityInfo_->hapPath); if (jsObj_ == nullptr) { HILOG_ERROR("Failed to get jsObj_"); return; diff --git a/frameworks/native/ability/native/js_service_extension.cpp b/frameworks/native/ability/native/js_service_extension.cpp index 8376805f17..a17304d315 100644 --- a/frameworks/native/ability/native/js_service_extension.cpp +++ b/frameworks/native/ability/native/js_service_extension.cpp @@ -90,7 +90,7 @@ void JsServiceExtension::Init(const std::shared_ptr &record, HandleScope handleScope(jsRuntime_); auto& engine = jsRuntime_.GetNativeEngine(); - jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->compileMode == CompileMode::ES_MODULE); + jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->compileMode == CompileMode::ES_MODULE, abilityInfo_->hapPath); if (jsObj_ == nullptr) { HILOG_ERROR("Failed to get jsObj_"); return; diff --git a/frameworks/native/ability/native/js_static_subscriber_extension.cpp b/frameworks/native/ability/native/js_static_subscriber_extension.cpp index 4a25b834ff..aaecad01e8 100644 --- a/frameworks/native/ability/native/js_static_subscriber_extension.cpp +++ b/frameworks/native/ability/native/js_static_subscriber_extension.cpp @@ -92,7 +92,7 @@ void JsStaticSubscriberExtension::Init(const std::shared_ptr HandleScope handleScope(jsRuntime_); auto& engine = jsRuntime_.GetNativeEngine(); - jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->compileMode == CompileMode::ES_MODULE); + jsObj_ = jsRuntime_.LoadModule(moduleName, srcPath, abilityInfo_->compileMode == CompileMode::ES_MODULE, abilityInfo_->hapPath); if (jsObj_ == nullptr) { HILOG_ERROR("Failed to get jsObj_"); return; diff --git a/frameworks/native/appkit/ability_delegator/runner_runtime/js_test_runner.cpp b/frameworks/native/appkit/ability_delegator/runner_runtime/js_test_runner.cpp index dfaf7d7d7d..40e9169e85 100644 --- a/frameworks/native/appkit/ability_delegator/runner_runtime/js_test_runner.cpp +++ b/frameworks/native/appkit/ability_delegator/runner_runtime/js_test_runner.cpp @@ -63,11 +63,13 @@ JsTestRunner::JsTestRunner( srcPath.append(".abc"); srcPath_ = srcPath; } - HILOG_INFO("JsTestRunner srcPath is %{public}s", srcPath_.c_str()); + HILOG_DEBUG("JsTestRunner srcPath is %{public}s", srcPath_.c_str()); if (!isFaJsModel) { std::string moduleName; + hapPath_ = bundleInfo.hapModuleInfos.back().hapPath; + HILOG_DEBUG("JsTestRunner hapPath is %{public}s", hapPath_.c_str()); jsTestRunnerObj_ = jsRuntime_.LoadModule(moduleName, srcPath_, - bundleInfo.hapModuleInfos.back().compileMode == AppExecFwk::CompileMode::ES_MODULE); + bundleInfo.hapModuleInfos.back().compileMode == AppExecFwk::CompileMode::ES_MODULE, hapPath_); } } @@ -87,7 +89,7 @@ bool JsTestRunner::Initialize() HILOG_ERROR("mgmtResult init error"); return false; } - if (!jsRuntime_.RunSandboxScript(srcPath_)) { + if (!jsRuntime_.RunSandboxScript(srcPath_, hapPath_)) { HILOG_ERROR("RunScript srcPath_ err"); return false; } diff --git a/frameworks/native/appkit/ability_runtime/app/js_ability_stage.cpp b/frameworks/native/appkit/ability_runtime/app/js_ability_stage.cpp index 6ea951aaa6..f2971a1a50 100644 --- a/frameworks/native/appkit/ability_runtime/app/js_ability_stage.cpp +++ b/frameworks/native/appkit/ability_runtime/app/js_ability_stage.cpp @@ -69,7 +69,7 @@ std::shared_ptr JsAbilityStage::Create( std::string moduleName(hapModuleInfo.moduleName); moduleName.append("::").append("AbilityStage"); auto moduleObj = - jsRuntime.LoadModule(moduleName, srcPath, hapModuleInfo.compileMode == AppExecFwk::CompileMode::ES_MODULE); + jsRuntime.LoadModule(moduleName, srcPath, hapModuleInfo.compileMode == AppExecFwk::CompileMode::ES_MODULE, hapModuleInfo.hapPath); return std::make_shared(jsRuntime, std::move(moduleObj)); } @@ -82,7 +82,7 @@ std::shared_ptr JsAbilityStage::Create( std::string moduleName(hapModuleInfo.moduleName); moduleName.append("::").append("AbilityStage"); moduleObj = - jsRuntime.LoadModule(moduleName, srcPath, hapModuleInfo.compileMode == AppExecFwk::CompileMode::ES_MODULE); + jsRuntime.LoadModule(moduleName, srcPath, hapModuleInfo.compileMode == AppExecFwk::CompileMode::ES_MODULE, hapModuleInfo.hapPath); HILOG_INFO("JsAbilityStage srcPath is %{public}s", srcPath.c_str()); } return std::make_shared(jsRuntime, std::move(moduleObj)); diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index 26787b18fc..3e8b431a61 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -937,6 +937,7 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con AbilityRuntime::Runtime::Options options; options.bundleName = appInfo.bundleName; options.codePath = LOCAL_CODE_PATH; + options.hapPath = bundleInfo.hapModuleInfos.back().hapPath; options.eventRunner = mainHandler_->GetEventRunner(); options.loadAce = true; std::string nativeLibraryPath = appInfo.nativeLibraryPath; @@ -1225,6 +1226,7 @@ bool MainThread::PrepareAbilityDelegator(const std::shared_ptr & AbilityRuntime::Runtime::Options options; options.codePath = LOCAL_CODE_PATH; options.eventRunner = mainHandler_->GetEventRunner(); + options.hapPath = bundleInfo.hapModuleInfos.back().hapPath; options.loadAce = false; if (bundleInfo.hapModuleInfos.empty() || bundleInfo.hapModuleInfos.front().abilityInfos.empty()) { HILOG_ERROR("Failed to abilityInfos"); diff --git a/frameworks/native/runtime/js_module_searcher.cpp b/frameworks/native/runtime/js_module_searcher.cpp index 2c44e47f98..d7af2b2e61 100644 --- a/frameworks/native/runtime/js_module_searcher.cpp +++ b/frameworks/native/runtime/js_module_searcher.cpp @@ -20,6 +20,7 @@ #include #include "hilog_wrapper.h" +#include "js_runtime_utils.h" namespace OHOS { namespace AbilityRuntime { @@ -49,11 +50,6 @@ inline bool StringEndWith(const std::string& str, const char* endStr, size_t end return ((len >= endStrLen) && (str.compare(len - endStrLen, endStrLen, endStr) == 0)); } -inline bool StringStartWith(const std::string& str, const char* startStr, size_t startStrLen) -{ - return ((str.length() >= startStrLen) && (str.compare(0, startStrLen, startStr) == 0)); -} - void SplitString(const std::string& str, std::vector& out, size_t pos = 0, const char* seps = "\\/") { if (str.empty() || pos >= str.length()) { @@ -101,44 +97,9 @@ inline std::string StripString(const std::string& str, const char* charSet = " \ std::string JsModuleSearcher::operator()(const std::string& curJsModulePath, const std::string& newJsModuleUri) const { - HILOG_INFO("Search JS module (%{public}s, %{public}s) begin", - curJsModulePath.c_str(), newJsModuleUri.c_str()); - - std::string newJsModulePath; - - if (curJsModulePath.empty() || newJsModuleUri.empty()) { - return newJsModulePath; - } - std::string normalizeUri = newJsModuleUri; - replace(normalizeUri.begin(), normalizeUri.end(), '\\', '/'); - - switch (normalizeUri[0]) { - case '.': { - newJsModulePath = MakeNewJsModulePath(curJsModulePath, normalizeUri); - break; - } - case '@': { - newJsModulePath = ParseOhmUri(curJsModulePath, normalizeUri); - if (newJsModulePath.empty()) { - newJsModulePath = FindNpmPackage(curJsModulePath, normalizeUri); - } - break; - } - default: { - newJsModulePath = FindNpmPackage(curJsModulePath, normalizeUri); - break; - } - } - - FixExtName(newJsModulePath); - - HILOG_INFO("Search JS module (%{public}s, %{public}s) => %{public}s end", - curJsModulePath.c_str(), normalizeUri.c_str(), newJsModulePath.c_str()); - - return newJsModulePath; + return ParseJsModuleUri(curJsModulePath, newJsModuleUri); } - void JsModuleSearcher::FixExtName(std::string& path) { if (path.empty()) { @@ -381,5 +342,53 @@ std::string JsModuleSearcher::ParseOhmUri(const std::string& curJsModulePath, co return FindNpmPackageInTopLevel(moduleInstallPath, JoinString(pathVector, '/', index + 1)); } + +std::string JsModuleSearcher::ParseJsModuleUri(const std::string& curJsModulePath, const std::string& newJsModuleUri) const +{ + HILOG_DEBUG("Search JS module ParseJsModuleUri (%{public}s, %{public}s) begin", curJsModulePath.c_str(), + newJsModuleUri.c_str()); + + std::string newJsModulePath; + if (curJsModulePath.empty() || newJsModuleUri.empty()) { + return newJsModulePath; + } + std::string normalizeUri = newJsModuleUri; + replace(normalizeUri.begin(), normalizeUri.end(), '\\', '/'); + + switch (normalizeUri[0]) { + case '.': { + newJsModulePath = MakeNewJsModulePath(curJsModulePath, normalizeUri); + break; + } + case '@': { + newJsModulePath = ParseOhmUri(curJsModulePath, normalizeUri); + if (newJsModulePath.empty()) { + newJsModulePath = FindNpmPackage(curJsModulePath, normalizeUri); + } + break; + } + default: { + newJsModulePath = FindNpmPackage(curJsModulePath, normalizeUri); + break; + } + } + + FixExtName(newJsModulePath); + HILOG_DEBUG("Search JS module ParseJsModuleUri (%{public}s, %{public}s) => %{public}s end", + curJsModulePath.c_str(), normalizeUri.c_str(), newJsModulePath.c_str()); + return newJsModulePath; +} + +bool JsModuleSearcher::GetABCFileBuffer( + const std::string& curJsModulePath, const std::string& newJsModuleUri, std::ostream &dest) const +{ + std::string newJsModulePath = ParseJsModuleUri(curJsModulePath, newJsModuleUri); + + if (!GetABCFile(hapPath_, newJsModulePath, dest)) { + HILOG_ERROR("Get abc file failed"); + return false; + } + return true; +} } // namespace AbilityRuntime } // namespace OHOS \ No newline at end of file diff --git a/frameworks/native/runtime/js_module_searcher.h b/frameworks/native/runtime/js_module_searcher.h index ce76271abd..03790f2d47 100644 --- a/frameworks/native/runtime/js_module_searcher.h +++ b/frameworks/native/runtime/js_module_searcher.h @@ -16,6 +16,7 @@ #ifndef OHOS_ABILITY_RUNTIME_JS_MODULE_SEARCHER_H #define OHOS_ABILITY_RUNTIME_JS_MODULE_SEARCHER_H +#include #include namespace OHOS { @@ -23,6 +24,10 @@ namespace AbilityRuntime { class JsModuleSearcher final { public: explicit JsModuleSearcher(const std::string& bundleName) : bundleName_(bundleName) {} + explicit JsModuleSearcher(const std::string& bundleName, const std::string& hapPath) { + bundleName_ = bundleName; + hapPath_ = hapPath; + } ~JsModuleSearcher() = default; JsModuleSearcher(const JsModuleSearcher&) = default; @@ -31,6 +36,8 @@ public: JsModuleSearcher& operator=(JsModuleSearcher&&) = default; std::string operator()(const std::string& curJsModulePath, const std::string& newJsModuleUri) const; + bool GetABCFileBuffer( + const std::string& curJsModulePath, const std::string& newJsModuleUri, std::ostream &dest) const; private: static void FixExtName(std::string& path); @@ -42,8 +49,10 @@ private: static std::string FindNpmPackage(const std::string& curJsModulePath, const std::string& npmPackage); std::string ParseOhmUri(const std::string& curJsModulePath, const std::string& newJsModuleUri) const; + std::string ParseJsModuleUri(const std::string& curJsModulePath, const std::string& newJsModuleUri) const; std::string bundleName_; + std::string hapPath_; }; } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 7517137f3a..a9d7f1659b 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -22,6 +22,7 @@ #include #include + #include "connect_server_manager.h" #include "event_handler.h" #include "hdc_register.h" @@ -106,14 +107,25 @@ public: debugMode_ = true; } - bool RunScript(const std::string& path) override + bool RunScript(const std::string& path, const std::string& hapPath) override { - return nativeEngine_->RunScriptPath(path.c_str()) != nullptr; + bool result = false; + if (!hapPath.empty()) { + std::ostringstream outStreamForABC; + if (!GetABCFile(hapPath, path, outStreamForABC)) { + HILOG_ERROR("Get abc file failed"); + return result; + } + result = nativeEngine_->RunScriptPath(path.c_str()) != nullptr; //, (void *)outStreamForABC + } else { + result = nativeEngine_->RunScriptPath(path.c_str()) != nullptr; + } + return result; } - NativeValue* LoadJsModule(const std::string& path) override + NativeValue* LoadJsModule(const std::string& path, const std::string& hapPath) override { - if (!RunScript(path)) { + if (!RunScript(path, hapPath)) { HILOG_ERROR("Failed to run script: %{public}s", path.c_str()); return nullptr; } @@ -177,7 +189,11 @@ private: if (!options.preload) { bundleName_ = options.bundleName; - panda::JSNApi::SetHostResolvePathTracker(vm_, JsModuleSearcher(options.bundleName)); + if (!options.hapPath.empty()) { + panda::JSNApi::SetHostResolvePathTracker(vm_, JsModuleSearcher(options.bundleName, options.hapPath)); + } else { + panda::JSNApi::SetHostResolvePathTracker(vm_, JsModuleSearcher(options.bundleName)); + } } return JsRuntime::Initialize(options); } @@ -226,7 +242,7 @@ void InitSyscapModule(NativeEngine& engine, NativeObject& globalObject) bool MakeFilePath(const std::string& codePath, const std::string& modulePath, std::string& fileName) { std::string path(codePath); - path.append("/").append(modulePath); + path.append("/").append(modulePath);// phone/./ets/MainAbility6/MainAbility6.abc if (path.length() > PATH_MAX) { HILOG_ERROR("Path length(%{public}d) longer than MAX(%{public}d)", (int32_t)path.length(), PATH_MAX); return false; @@ -468,13 +484,13 @@ void JsRuntime::Deinitialize() nativeEngine_.reset(); } -NativeValue* JsRuntime::LoadJsBundle(const std::string& path) +NativeValue* JsRuntime::LoadJsBundle(const std::string& path, const std::string& hapPath) { NativeObject* globalObj = ConvertNativeValueTo(nativeEngine_->GetGlobal()); NativeValue* exports = nativeEngine_->CreateObject(); globalObj->SetProperty("exports", exports); - if (!RunScript(path)) { + if (!RunScript(path, hapPath)) { HILOG_ERROR("Failed to run script: %{public}s", path.c_str()); return nullptr; } @@ -494,8 +510,8 @@ NativeValue* JsRuntime::LoadJsBundle(const std::string& path) return exportObj; } -std::unique_ptr JsRuntime::LoadModule( - const std::string& moduleName, const std::string& modulePath, bool esmodule) +std::unique_ptr JsRuntime::LoadModule(const std::string& moduleName, + const std::string& modulePath, bool esmodule, const std::string& hapPath) { HILOG_INFO("JsRuntime::LoadModule(%{public}s, %{public}s, %{public}s)", moduleName.c_str(), modulePath.c_str(), esmodule ? "true" : "false"); @@ -514,7 +530,7 @@ std::unique_ptr JsRuntime::LoadModule( return std::unique_ptr(); } - classValue = esmodule ? LoadJsModule(fileName) : LoadJsBundle(fileName); + classValue = esmodule ? LoadJsModule(fileName, hapPath) : LoadJsBundle(fileName, hapPath); if (classValue == nullptr) { return std::unique_ptr(); } @@ -550,12 +566,23 @@ std::unique_ptr JsRuntime::LoadSystemModule( return std::unique_ptr(nativeEngine_->CreateReference(instanceValue, 1)); } -bool JsRuntime::RunScript(const std::string& path) +bool JsRuntime::RunScript(const std::string& path, const std::string& hapPath) { - return nativeEngine_->RunScript(path.c_str()) != nullptr; + bool result = false; + if (!hapPath.empty()) { + std::ostringstream outStreamForABC; + if (!GetABCFile(hapPath, path, outStreamForABC)) { + HILOG_ERROR("Get abc file failed"); + return result; + } + result = nativeEngine_->RunScript(path.c_str()) != nullptr; //, (void *)outStreamForABC + } else { + result = nativeEngine_->RunScript(path.c_str()) != nullptr; + } + return result; } -bool JsRuntime::RunSandboxScript(const std::string& path) +bool JsRuntime::RunSandboxScript(const std::string& path, const std::string& hapPath) { std::string fileName; if (!MakeFilePath(codePath_, path, fileName)) { @@ -563,7 +590,7 @@ bool JsRuntime::RunSandboxScript(const std::string& path) return false; } - if (!RunScript(fileName)) { + if (!RunScript(fileName, hapPath)) { HILOG_ERROR("Failed to run script: %{public}s", fileName.c_str()); return false; } diff --git a/frameworks/native/runtime/js_runtime_utils.cpp b/frameworks/native/runtime/js_runtime_utils.cpp index 109a756ccf..31f3c65258 100644 --- a/frameworks/native/runtime/js_runtime_utils.cpp +++ b/frameworks/native/runtime/js_runtime_utils.cpp @@ -15,8 +15,12 @@ #include "js_runtime_utils.h" +#include + +#include "ability_constants.h" #include "hilog_wrapper.h" #include "js_runtime.h" +#include "runtime_extractor.h" namespace OHOS { namespace AbilityRuntime { @@ -38,6 +42,11 @@ std::unique_ptr CreateAsyncTaskWithLastParam(NativeEngine& engine, Na } // namespace // Help Functions +bool StringStartWith(const std::string& str, const char* startStr, size_t startStrLen) +{ + return ((str.length() >= startStrLen) && (str.compare(0, startStrLen, startStr) == 0)); +} + NativeValue* CreateJsError(NativeEngine& engine, int32_t errCode, const std::string& message) { return engine.CreateError(CreateJsValue(engine, errCode), CreateJsValue(engine, message)); @@ -237,5 +246,38 @@ std::unique_ptr CreateAsyncTaskWithLastParam(NativeEngine& engine, Na return CreateAsyncTaskWithLastParam(engine, lastParam, std::unique_ptr(), std::unique_ptr(), result); } + +bool GetABCFile(const std::string& hapPath, const std::string& srcPath, std::ostream &dest) +{ + if (hapPath.empty() || srcPath.empty()) { + HILOG_ERROR("GetABCFile::hapPath or srcPath is nullptr"); + return false; + } + + std::string loadPath; + if (!StringStartWith(hapPath, Constants::SYSTEM_APP_PATH, sizeof(Constants::SYSTEM_APP_PATH) - 1)) { + std::regex hapPattern(std::string(Constants::ABS_CODE_PATH) + std::string(Constants::FILE_SEPARATOR)); + loadPath = std::regex_replace(hapPath, hapPattern, ""); + loadPath = std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR) + + loadPath.substr(loadPath.find(std::string(Constants::FILE_SEPARATOR)) + 1); + } else { + loadPath = hapPath; + } + RuntimeExtractor runtimeExtractor(loadPath); + if (!runtimeExtractor.Init()) { + HILOG_ERROR("GetABCFile::Runtime extractor init failed"); + return false; + } + + std::regex srcPattern(std::string(Constants::LOCAL_CODE_PATH) + std::string(Constants::FILE_SEPARATOR)); + std::string relativePath = std::regex_replace(srcPath, srcPattern, ""); + relativePath = relativePath.substr(relativePath.find(std::string(Constants::FILE_SEPARATOR)) + 1); + if (!runtimeExtractor.ExtractABCFile(relativePath, dest)) { + HILOG_ERROR("GetABCFile::Extract abc file failed"); + return false; + } + + return true; +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/native/runtime/runtime_extractor.cpp b/frameworks/native/runtime/runtime_extractor.cpp new file mode 100755 index 0000000000..2b51e23cc6 --- /dev/null +++ b/frameworks/native/runtime/runtime_extractor.cpp @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "runtime_extractor.h" + +#include "hilog_wrapper.h" + +namespace OHOS { +namespace AbilityRuntime { +RuntimeExtractor::RuntimeExtractor(const std::string &source) : AppExecFwk::BaseExtractor(source) +{ + HILOG_DEBUG("RuntimeExtractor is created"); +} + +RuntimeExtractor::~RuntimeExtractor() +{ + HILOG_DEBUG("RuntimeExtractor destroyed"); +} + +bool RuntimeExtractor::ExtractABCFile(const std::string& srcPath, std::ostream &dest) const +{ + HILOG_DEBUG("%{public}s: hapPath is %{public}s", __func__, srcPath.c_str()); + return AppExecFwk::BaseExtractor::ExtractByName(srcPath, dest); +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/interfaces/inner_api/runtime/BUILD.gn b/interfaces/inner_api/runtime/BUILD.gn index c1ee9caa04..b1740a1f35 100644 --- a/interfaces/inner_api/runtime/BUILD.gn +++ b/interfaces/inner_api/runtime/BUILD.gn @@ -48,7 +48,6 @@ ohos_shared_library("runtime") { configs = [ ":runtime_config", "//arkcompiler/ets_runtime:ark_jsruntime_public_config", - "${ability_runtime_services_path}/common:common_config", ] public_configs = [ ":runtime_public_config" ] @@ -56,6 +55,7 @@ ohos_shared_library("runtime") { deps = [ "//arkcompiler/ets_runtime:libark_jsruntime", "//foundation/arkui/napi:ace_napi_ark", + ":runtime_extractor" ] external_deps = [ @@ -93,3 +93,31 @@ ohos_shared_library("runtime") { subsystem_name = "ability" part_name = "ability_runtime" } + +ohos_source_set("runtime_extractor") { + include_dirs = [ "include" ] + + sources = [ + "${ability_runtime_native_path}/runtime/runtime_extractor.cpp", + ] + + configs = [ + "${ability_runtime_services_path}/common:common_config", + ] + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + + public_deps = [ + "${bundlefwk_services_path}/bundlemgr:parser_common", + ] + + external_deps = [ + "hiviewdfx_hilog_native:libhilog", + ] + + subsystem_name = "ability" + part_name = "ability_runtime" +} diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index a97f8884cc..d7d7191835 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -55,8 +55,8 @@ public: return Language::JS; } - std::unique_ptr LoadModule( - const std::string& moduleName, const std::string& modulePath, bool esmodule = false); + std::unique_ptr LoadModule(const std::string& moduleName, + const std::string& modulePath, bool esmodule = false, const std::string& hapPath = nullptr); std::unique_ptr LoadSystemModule( const std::string& moduleName, NativeValue* const* argv = nullptr, size_t argc = 0); void PostTask(const std::function& task, const std::string& name, int64_t delayTime); @@ -65,8 +65,8 @@ public: std::string BuildJsStackTrace() override; void NotifyApplicationState(bool isBackground) override; - bool RunSandboxScript(const std::string& path); - virtual bool RunScript(const std::string& path) = 0; + bool RunSandboxScript(const std::string& path, const std::string& hapPath); + virtual bool RunScript(const std::string& path, const std::string& hapPath = nullptr) = 0; void PreloadSystemModule(const std::string& moduleName) override; @@ -76,8 +76,8 @@ protected: virtual bool Initialize(const Options& options); void Deinitialize(); - virtual NativeValue* LoadJsBundle(const std::string& path); - virtual NativeValue* LoadJsModule(const std::string& path) = 0; + virtual NativeValue* LoadJsBundle(const std::string& path, const std::string& hapPath); + virtual NativeValue* LoadJsModule(const std::string& path, const std::string& hapPath) = 0; bool isArkEngine_ = false; bool debugMode_ = false; diff --git a/interfaces/inner_api/runtime/include/js_runtime_utils.h b/interfaces/inner_api/runtime/include/js_runtime_utils.h index 0cb2b9b855..706cce830d 100644 --- a/interfaces/inner_api/runtime/include/js_runtime_utils.h +++ b/interfaces/inner_api/runtime/include/js_runtime_utils.h @@ -18,6 +18,7 @@ #include #include +#include #include #include "native_engine/native_engine.h" @@ -111,6 +112,8 @@ NativeValue* CreateNativeArray(NativeEngine& engine, const std::vector& data) return arrayValue; } +bool StringStartWith(const std::string& str, const char* startStr, size_t startStrLen); +bool StringEndWith(const std::string& str, const char* endStr, size_t endStrLen); NativeValue* CreateJsError(NativeEngine& engine, int32_t errCode, const std::string& message = std::string()); void BindNativeFunction(NativeEngine& engine, NativeObject& object, const char* name, NativeCallback func); void BindNativeProperty(NativeObject& object, const char* name, NativeCallback getter); @@ -184,6 +187,8 @@ std::unique_ptr CreateAsyncTaskWithLastParam(NativeEngine& engine, Na std::unique_ptr CreateAsyncTaskWithLastParam(NativeEngine& engine, NativeValue* lastParam, nullptr_t, nullptr_t, NativeValue** result); + +bool GetABCFile(const std::string& hapPath, const std::string& srcPath, std::ostream &dest); } // namespace AbilityRuntime } // namespace OHOS diff --git a/interfaces/inner_api/runtime/include/runtime.h b/interfaces/inner_api/runtime/include/runtime.h index 090bfc5694..5fb3ccfd58 100644 --- a/interfaces/inner_api/runtime/include/runtime.h +++ b/interfaces/inner_api/runtime/include/runtime.h @@ -34,6 +34,7 @@ public: std::string bundleName; std::string codePath; std::string packagePath; + std::string hapPath; std::shared_ptr eventRunner; bool loadAce = true; bool preload = false; diff --git a/interfaces/inner_api/runtime/include/runtime_extractor.h b/interfaces/inner_api/runtime/include/runtime_extractor.h new file mode 100755 index 0000000000..a76a9272dc --- /dev/null +++ b/interfaces/inner_api/runtime/include/runtime_extractor.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_ABILITY_RUNTIME_RUNTIME_EXTRACTOR_H +#define OHOS_ABILITY_RUNTIME_RUNTIME_EXTRACTOR_H + +#include "base_extractor.h" + +namespace OHOS { +namespace AbilityRuntime { +class RuntimeExtractor : public AppExecFwk::BaseExtractor { +public: + explicit RuntimeExtractor(const std::string &source); + virtual ~RuntimeExtractor() override; + + /** + * @brief Extract the abc file of a hap to dest stream. + * @param srcPath Indicates the src path of the abc file in hap. + * @param dest Indicates the obtained std::ostream object. + * @return Returns true if the Profile is successfully extracted; returns false otherwise. + */ + bool ExtractABCFile(const std::string& srcPath, std::ostream &dest) const; +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_RUNTIME_EXTRACTOR_H diff --git a/interfaces/kits/native/appkit/ability_delegator/runner_runtime/js_test_runner.h b/interfaces/kits/native/appkit/ability_delegator/runner_runtime/js_test_runner.h index fd6025cb91..11a7e22f0b 100644 --- a/interfaces/kits/native/appkit/ability_delegator/runner_runtime/js_test_runner.h +++ b/interfaces/kits/native/appkit/ability_delegator/runner_runtime/js_test_runner.h @@ -69,6 +69,7 @@ private: JsRuntime &jsRuntime_; std::unique_ptr jsTestRunnerObj_; std::string srcPath_; + std::string hapPath_; bool isFaJsModel_ = false; }; } // namespace RunnerRuntime diff --git a/interfaces/kits/native/appkit/ability_runtime/context/ability_constants.h b/services/common/include/ability_constants.h similarity index 95% rename from interfaces/kits/native/appkit/ability_runtime/context/ability_constants.h rename to services/common/include/ability_constants.h index cd3a983357..29707af6b3 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/ability_constants.h +++ b/services/common/include/ability_constants.h @@ -23,6 +23,8 @@ constexpr const char* ABS_CODE_PATH = "/data/app/el1/bundle/public"; constexpr const char* LOCAL_CODE_PATH = "/data/storage/el1/bundle"; constexpr const char* LOCAL_BUNDLES = "/data/bundles"; constexpr const char* FILE_SEPARATOR = "/"; + +constexpr const char SYSTEM_APP_PATH[] = "/system/app"; } // namespace Constants } // namespace AbilityRuntime } // namespace OHOS