diff --git a/frameworks/native/ability/native/BUILD.gn b/frameworks/native/ability/native/BUILD.gn index d0eaaf7e04..d8c766bc96 100644 --- a/frameworks/native/ability/native/BUILD.gn +++ b/frameworks/native/ability/native/BUILD.gn @@ -359,6 +359,7 @@ ohos_shared_library("extensionkit_native") { ] sources = [ + "${ability_runtime_native_path}/ability/native/app_module_checker.cpp", "${ability_runtime_native_path}/ability/native/extension.cpp", "${ability_runtime_native_path}/ability/native/extension_config_mgr.cpp", "${ability_runtime_native_path}/ability/native/extension_impl.cpp", @@ -382,6 +383,7 @@ ohos_shared_library("extensionkit_native") { "common_event_service:cesfwk_innerkits", "hilog:libhilog", "hitrace:hitrace_meter", + "napi:ace_napi", ] public_deps = [ diff --git a/frameworks/native/ability/native/app_module_checker.cpp b/frameworks/native/ability/native/app_module_checker.cpp new file mode 100644 index 0000000000..ff8e0710ee --- /dev/null +++ b/frameworks/native/ability/native/app_module_checker.cpp @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2023 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 "app_module_checker.h" + +#include "module_checker_delegate.h" +#include "utils/log.h" + +bool AppModuleChecker::CheckModuleLoadable(const char* moduleName) +{ + HILOG_INFO("check blocklist, moduleName = %{public}s, processExtensionType_ = %{public}d", + moduleName, static_cast(processExtensionType_)); + const auto& blockListIter = moduleBlocklist_.find(processExtensionType_); + if (blockListIter == moduleBlocklist_.end()) { + return true; + } + auto blockList = blockListIter->second; + if (blockList.find(moduleName) == blockList.end()) { + return true; + } + return false; +} \ No newline at end of file diff --git a/frameworks/native/ability/native/extension_config_mgr.cpp b/frameworks/native/ability/native/extension_config_mgr.cpp index 19faa3194c..87a4fcc49c 100644 --- a/frameworks/native/ability/native/extension_config_mgr.cpp +++ b/frameworks/native/ability/native/extension_config_mgr.cpp @@ -18,12 +18,12 @@ #include #include +#include "app_module_checker.h" #include "hilog_wrapper.h" namespace OHOS::AbilityRuntime { namespace { constexpr char EXTENSION_BLOCKLIST_FILE_PATH[] = "/system/etc/extension_blocklist_config.json"; - constexpr char BACK_SLASH[] = "/"; } void ExtensionConfigMgr::Init() @@ -66,16 +66,6 @@ void ExtensionConfigMgr::Init() inFile.close(); } -void ExtensionConfigMgr::UpdateBundleExtensionInfo(NativeEngine& engine, AppExecFwk::BundleInfo& bundleInfo) -{ - std::unordered_map extensionInfo; - for (const auto &info : bundleInfo.extensionInfos) { - std::string path = info.moduleName + BACK_SLASH + info.srcEntrance; - extensionInfo.emplace(path, static_cast(info.type)); - } - engine.SetExtensionInfos(std::move(extensionInfo)); -} - void ExtensionConfigMgr::AddBlockListItem(const std::string& name, int32_t type) { HILOG_DEBUG("AddBlockListItem name = %{public}s, type = %{public}d", name.c_str(), type); @@ -87,8 +77,14 @@ void ExtensionConfigMgr::AddBlockListItem(const std::string& name, int32_t type) extensionBlocklist_.emplace(type, iter->second); } -void ExtensionConfigMgr::UpdateBlockListToEngine(NativeEngine& engine) +void ExtensionConfigMgr::UpdateRuntimeModuleChecker(const std::unique_ptr &runtime) { - engine.SetModuleBlocklist(std::forward(extensionBlocklist_)); + if (!runtime) { + HILOG_ERROR("UpdateRuntimeModuleChecker faild, runtime is null"); + return; + } + HILOG_INFO("UpdateRuntimeModuleChecker extensionType_ = %{public}d", extensionType_); + auto moduleChecker = std::make_shared(extensionType_, std::move(extensionBlocklist_)); + runtime->SetModuleLoadChecker(moduleChecker); } } \ No newline at end of file diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index b64ee352b6..e6587a6216 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -1621,7 +1621,6 @@ void MainThread::LoadAllExtensions(NativeEngine &nativeEngine, const std::string }); } application_->SetExtensionTypeMap(extensionTypeMap); - UpdateEngineExtensionBlockList(nativeEngine); } bool MainThread::PrepareAbilityDelegator(const std::shared_ptr &record, bool isStageBased, @@ -1742,7 +1741,8 @@ void MainThread::HandleLaunchAbility(const std::shared_ptr & mainThreadState_ = MainThreadState::RUNNING; std::shared_ptr stageContext = application_->AddAbilityStage(abilityRecord); - UpdateProcessExtensionType(abilityRecord); + SetProcessExtensionType(abilityRecord); + UpdateRuntimeModuleChecker(runtime); #ifdef APP_ABILITY_USE_TWO_RUNNER AbilityThread::AbilityThreadMain(application_, abilityRecord, stageContext); #else @@ -2552,40 +2552,42 @@ void MainThread::NotifyAppFault(const FaultData &faultData) ApplicationDataManager::GetInstance().NotifyExceptionObject(faultErrorObj); } -void MainThread::UpdateProcessExtensionType(const std::shared_ptr &abilityRecord) +void MainThread::SetProcessExtensionType(const std::shared_ptr &abilityRecord) { - auto &runtime = application_->GetRuntime(); - if (!runtime) { - HILOG_ERROR("Get runtime failed"); + if (!extensionConfigMgr_) { + HILOG_ERROR("AddExtensionBlockItem failed, extensionConfigMgr_ is null"); return; } if (!abilityRecord) { - HILOG_ERROR("abilityRecord is nullptr"); + HILOG_ERROR("AddExtensionBlockItem failed, abilityRecord is null"); return; } - auto &abilityInfo = abilityRecord->GetAbilityInfo(); - if (!abilityInfo) { - HILOG_ERROR("Get abilityInfo failed"); + if (!abilityRecord->GetAbilityInfo()) { + HILOG_ERROR("AddExtensionBlockItem failed, abilityInfo is null"); return; } - runtime->UpdateExtensionType(static_cast(abilityInfo->extensionAbilityType)); - HILOG_DEBUG("UpdateExtensionType, type = %{public}d", static_cast(abilityInfo->extensionAbilityType)); + HILOG_INFO("SetProcessExtensionType, type = %{public}d", + static_cast(abilityRecord->GetAbilityInfo()->extensionAbilityType)); + extensionConfigMgr_->SetProcessExtensionType( + static_cast(abilityRecord->GetAbilityInfo()->extensionAbilityType)); } void MainThread::AddExtensionBlockItem(const std::string &extensionName, int32_t type) { if (!extensionConfigMgr_) { + HILOG_ERROR("AddExtensionBlockItem failed, extensionConfigMgr_ is null"); return; } extensionConfigMgr_->AddBlockListItem(extensionName, type); } -void MainThread::UpdateEngineExtensionBlockList(NativeEngine &nativeEngine) +void MainThread::UpdateRuntimeModuleChecker(const std::unique_ptr &runtime) { if (!extensionConfigMgr_) { + HILOG_ERROR("UpdateRuntimeModuleChecker failed, extensionConfigMgr_ is null"); return; } - extensionConfigMgr_->UpdateBlockListToEngine(nativeEngine); + extensionConfigMgr_->UpdateRuntimeModuleChecker(runtime); } int MainThread::GetOverlayModuleInfos(const std::string &bundleName, const std::string &moduleName, diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 30b564a27c..f07f398220 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -42,6 +42,7 @@ #include "js_runtime_utils.h" #include "js_utils.h" #include "js_worker.h" +#include "module_checker_delegate.h" #include "native_engine/impl/ark/ark_native_engine.h" #include "ohos_js_env_logger.h" #include "ohos_js_environment_impl.h" @@ -550,6 +551,7 @@ bool JsRuntime::Initialize(const Options& options) } InitWorkerModule(options); + SetModuleLoadChecker(options.moduleCheckerDelegate); if (!InitLoop()) { HILOG_ERROR("Initialize loop failed."); @@ -967,18 +969,6 @@ void JsRuntime::PreloadSystemModule(const std::string& moduleName) nativeEngine->CallFunction(nativeEngine->GetGlobal(), methodRequireNapiRef_->Get(), &className, 1); } -void JsRuntime::UpdateExtensionType(int32_t extensionType) -{ - auto nativeEngine = GetNativeEnginePointer(); - CHECK_POINTER(nativeEngine); - NativeModuleManager* moduleManager = nativeEngine->GetModuleManager(); - if (moduleManager == nullptr) { - HILOG_ERROR("UpdateExtensionType error, moduleManager is nullptr"); - return; - } - moduleManager->SetProcessExtensionType(extensionType); -} - NativeEngine& JsRuntime::GetNativeEngine() const { return *GetNativeEnginePointer(); @@ -1157,5 +1147,11 @@ void JsRuntime::ReInitJsEnvImpl(const Options& options) CHECK_POINTER(jsEnv_); jsEnv_->ReInitJsEnvImpl(std::make_unique(options.eventRunner)); } + +void JsRuntime::SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate) const +{ + CHECK_POINTER(jsEnv_); + jsEnv_->SetModuleLoadChecker(moduleCheckerDelegate); +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 7f875cc440..0a0a6f5bd7 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -90,7 +90,6 @@ public: bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false); void PreloadSystemModule(const std::string& moduleName) override; - void UpdateExtensionType(int32_t extensionType) override; void StartDebugMode(bool needBreakPoint) override; void StopDebugMode(); bool LoadRepairPatch(const std::string& hqfFile, const std::string& hapPath) override; @@ -116,6 +115,7 @@ public: void ReloadFormComponent(); // Reload ArkTS-Card component void DoCleanWorkAfterStageCleaned() override; + void SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate) const override; private: void FinishPreload() override; diff --git a/interfaces/inner_api/runtime/include/runtime.h b/interfaces/inner_api/runtime/include/runtime.h index 4544f2fbb4..1a758ee246 100644 --- a/interfaces/inner_api/runtime/include/runtime.h +++ b/interfaces/inner_api/runtime/include/runtime.h @@ -27,6 +27,8 @@ struct JsFrames { uintptr_t *nativePointer = nullptr; }; +class ModuleCheckerDelegate; + namespace OHOS { namespace AppExecFwk { class EventRunner; @@ -60,6 +62,7 @@ public: // ArkTsCard start bool isUnique = false; // ArkTsCard end + std::shared_ptr moduleCheckerDelegate; }; static std::unique_ptr Create(const Options& options); @@ -80,10 +83,10 @@ public: virtual bool LoadRepairPatch(const std::string& patchFile, const std::string& baseFile) = 0; virtual bool NotifyHotReloadPage() = 0; virtual bool UnLoadRepairPatch(const std::string& patchFile) = 0; - virtual void UpdateExtensionType(int32_t extensionType) = 0; virtual void RegisterQuickFixQueryFunc(const std::map& moduleAndPath) = 0; virtual void StartProfiler(const std::string &perfCmd) = 0; virtual void DoCleanWorkAfterStageCleaned() = 0; + virtual void SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate) const {} Runtime(const Runtime&) = delete; Runtime(Runtime&&) = delete; diff --git a/interfaces/kits/native/ability/native/app_module_checker.h b/interfaces/kits/native/ability/native/app_module_checker.h new file mode 100644 index 0000000000..4b827092d6 --- /dev/null +++ b/interfaces/kits/native/ability/native/app_module_checker.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 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_FORM_FWK_APP_MODULE_CHECKER_H +#define OHOS_FORM_FWK_APP_MODULE_CHECKER_H + +#include +#include + +#include "module_checker_delegate.h" + +namespace { + constexpr int32_t EXTENSION_TYPE_UNKNOWN = 255; +} + +/** + * @brief Form module load checker. check whether module can be loaded in form + * + */ +class AppModuleChecker : public ModuleCheckerDelegate { +public: + AppModuleChecker(int32_t extensionType, std::unordered_map> &&blocklist) + : processExtensionType_(extensionType), moduleBlocklist_(std::move(blocklist)) {} + ~AppModuleChecker() override = default; + + bool CheckModuleLoadable(const char* moduleName) override; +protected: + int32_t processExtensionType_{EXTENSION_TYPE_UNKNOWN}; + std::unordered_map> moduleBlocklist_; +}; + +#endif /* OHOS_FORM_FWK_APP_MODULE_CHECKER_H */ \ No newline at end of file diff --git a/interfaces/kits/native/ability/native/extension_config_mgr.h b/interfaces/kits/native/ability/native/extension_config_mgr.h index 316a590815..8aa210647d 100644 --- a/interfaces/kits/native/ability/native/extension_config_mgr.h +++ b/interfaces/kits/native/ability/native/extension_config_mgr.h @@ -23,11 +23,15 @@ #include "bundle_info.h" #include "extension_ability_info.h" #include "native_engine/native_engine.h" +#include "runtime.h" namespace OHOS::AbilityRuntime { namespace ExtensionConfigItem { constexpr char ITEM_NAME_BLOCKLIST[] = "blocklist"; } +namespace { + constexpr int32_t EXTENSION_TYPE_UNKNOWN = 255; +} /** * @brief Manage extension configuration. @@ -45,11 +49,14 @@ public: void Init(); /** - * @brief Update bundle extension information + * @brief Set the Process Extension Type object * - * @param engine JS NativeEngine + * @param extensionType */ - void UpdateBundleExtensionInfo(NativeEngine &engine, AppExecFwk::BundleInfo &bundleInfo); + void SetProcessExtensionType(int32_t extensionType) + { + extensionType_ = extensionType; + } /** * @brief Add extension blocklist item @@ -60,15 +67,16 @@ public: void AddBlockListItem(const std::string &name, int32_t type); /** - * @brief Update extension blocklist to native engine + * @brief Update runtime module checker * - * @param engine JS NativeEngine + * @param runtime the runtime pointer */ - void UpdateBlockListToEngine(NativeEngine &engine); + void UpdateRuntimeModuleChecker(const std::unique_ptr &runtime); private: std::unordered_map> blocklistConfig_; std::unordered_map> extensionBlocklist_; + int32_t extensionType_ = EXTENSION_TYPE_UNKNOWN; }; } // namespace OHOS::AbilityRuntime diff --git a/interfaces/kits/native/appkit/app/main_thread.h b/interfaces/kits/native/appkit/app/main_thread.h index 86407309a9..9a128a1af4 100644 --- a/interfaces/kits/native/appkit/app/main_thread.h +++ b/interfaces/kits/native/appkit/app/main_thread.h @@ -474,11 +474,11 @@ private: const AppExecFwk::HapModuleInfo &entryHapModuleInfo); /** - * @brief Update current process extension type + * @brief Set current process extension type * * @param abilityRecord current running ability record */ - void UpdateProcessExtensionType(const std::shared_ptr &abilityRecord); + void SetProcessExtensionType(const std::shared_ptr &abilityRecord); /** * @brief Add Extension block item @@ -489,11 +489,11 @@ private: void AddExtensionBlockItem(const std::string &extensionName, int32_t type); /** - * @brief Update extension block list to nativeEngine + * @brief Update runtime module checker * - * @param nativeEngine nativeEngine instance + * @param runtime runtime the ability runtime */ - void UpdateEngineExtensionBlockList(NativeEngine &nativeEngine); + void UpdateRuntimeModuleChecker(const std::unique_ptr &runtime); static void HandleDumpHeap(bool isPrivate); static void HandleSignal(int signal); diff --git a/js_environment/frameworks/js_environment/src/js_environment.cpp b/js_environment/frameworks/js_environment/src/js_environment.cpp index eb06bb7360..720876d673 100644 --- a/js_environment/frameworks/js_environment/src/js_environment.cpp +++ b/js_environment/frameworks/js_environment/src/js_environment.cpp @@ -239,5 +239,14 @@ void JsEnvironment::ReInitJsEnvImpl(std::unique_ptr impl) JSENV_LOG_I("ReInit jsenv impl."); impl_ = std::move(impl); } + +void JsEnvironment::SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate) +{ + if (engine_ == nullptr) { + JSENV_LOG_E("SetModuleLoadChecker failed, engine_ is null"); + return; + } + engine_->SetModuleLoadChecker(moduleCheckerDelegate); +} } // namespace JsEnv } // namespace OHOS diff --git a/js_environment/interfaces/inner_api/js_environment.h b/js_environment/interfaces/inner_api/js_environment.h index 2a8b91a983..327ff3c4ad 100644 --- a/js_environment/interfaces/inner_api/js_environment.h +++ b/js_environment/interfaces/inner_api/js_environment.h @@ -84,6 +84,8 @@ public: void ReInitJsEnvImpl(std::unique_ptr impl); + void SetModuleLoadChecker(const std::shared_ptr& moduleCheckerDelegate); + private: std::unique_ptr impl_ = nullptr; NativeEngine* engine_ = nullptr; diff --git a/test/mock/frameworks_kits_runtime_test/mock_runtime.h b/test/mock/frameworks_kits_runtime_test/mock_runtime.h index 2886f39b42..bafd1d77cb 100644 --- a/test/mock/frameworks_kits_runtime_test/mock_runtime.h +++ b/test/mock/frameworks_kits_runtime_test/mock_runtime.h @@ -63,10 +63,6 @@ public: { return; } - void UpdateExtensionType(int32_t extensionType) override - { - return; - } bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false) { return true; diff --git a/test/unittest/extension_config_mgr_test/extension_config_mgr_test.cpp b/test/unittest/extension_config_mgr_test/extension_config_mgr_test.cpp index 54b122897d..9d43ffca0b 100644 --- a/test/unittest/extension_config_mgr_test/extension_config_mgr_test.cpp +++ b/test/unittest/extension_config_mgr_test/extension_config_mgr_test.cpp @@ -159,7 +159,7 @@ HWTEST_F(ExtensionConfigMgrTest, AddBlockListItem_0100, TestSize.Level0) EXPECT_TRUE(result); mgr.AddBlockListItem(BLOCK_LIST_ITEM_FILE_ACCESS_EXTENSION, EXTENSION_TYPE_FILE_ACCESS); result = (mgr.extensionBlocklist_.find(EXTENSION_TYPE_FILE_ACCESS) != mgr.extensionBlocklist_.end()); - EXPECT_TRUE(result); + EXPECT_TRUE(result); } /** diff --git a/test/unittest/runtime_test/js_runtime_test.cpp b/test/unittest/runtime_test/js_runtime_test.cpp index 0a6076eaea..9c21de76cd 100755 --- a/test/unittest/runtime_test/js_runtime_test.cpp +++ b/test/unittest/runtime_test/js_runtime_test.cpp @@ -398,24 +398,6 @@ HWTEST_F(JsRuntimeTest, JsRuntimeLoadSystemModulesTest_0100, TestSize.Level0) HILOG_INFO("LoadSystemModule end"); } -/** - * @tc.name: JsRuntimeUpdateExtensionTypeTest_0100 - * @tc.desc: JsRuntime test for UpdateExtensionType. - * @tc.type: FUNC - */ -HWTEST_F(JsRuntimeTest, JsRuntimeUpdateExtensionTypeTest_0100, TestSize.Level0) -{ - HILOG_INFO("UpdateExtensionType start"); - - auto jsRuntime = std::make_unique(); - EXPECT_TRUE(jsRuntime != nullptr); - - int32_t extensionType = 1; - jsRuntime->UpdateExtensionType(extensionType); - - HILOG_INFO("UpdateExtensionType end"); -} - /** * @tc.name: JsRuntimeStartDebugModeTest_0100 * @tc.desc: JsRuntime test for StartDebugMode.