From 3eedf10eaa1405c3e9a57aa9e96b61308d384326 Mon Sep 17 00:00:00 2001 From: chenzexin Date: Fri, 29 Aug 2025 11:40:13 +0800 Subject: [PATCH] deduplicate Signed-off-by: chenzexin --- .../ability_runtime/context/context_impl.cpp | 28 +++++++++++++- frameworks/native/appkit/app/main_thread.cpp | 4 ++ .../native/appkit/app/ohos_application.cpp | 37 ++++++++++++++++++- .../ability_runtime/context/context_impl.h | 2 +- .../kits/native/appkit/app/ohos_application.h | 16 ++++++++ 5 files changed, 83 insertions(+), 4 deletions(-) diff --git a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp index 48f7491f382..8c755dfe006 100644 --- a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp +++ b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp @@ -962,11 +962,32 @@ void ContextImpl::InitResourceManager(const AppExecFwk::BundleInfo &bundleInfo, return; } + bool hasDeduplicateHar = false; std::shared_ptr resourceManager = InitResourceManagerInner( - bundleInfo, currentBundle, moduleName, inputContext); + bundleInfo, currentBundle, moduleName, inputContext, &hasDeduplicateHar); if (resourceManager == nullptr) { return; } + if (hasDeduplicateHar) { + TAG_LOGD(AAFwkTag::APPKIT, "has deduplicateHar"); + std::string loadPath = ""; + for (const auto& info : bundleInfo.hapModuleInfos) { + if (info.moduleType == AppExecFwk::ModuleType::ENTRY) { + loadPath = info.hapPath.empty() ? info.resourcePath : info.hapPath; + if (loadPath.empty()) { + TAG_LOGE(AAFwkTag::APPKIT, "empty loadPath"); + break; + } + std::regex pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + bundleInfo.name); + loadPath = std::regex_replace(loadPath, pattern, std::string(LOCAL_CODE_PATH)); + if (!resourceManager->AddResource(loadPath.c_str())) { + TAG_LOGE(AAFwkTag::APPKIT, "AddResource failed"); + } + break; + } + } + } + std::shared_ptr src = nullptr; if (inputContext) { src = inputContext->GetResourceManager(); @@ -1001,7 +1022,7 @@ std::shared_ptr ContextImpl::InitOthersResour std::shared_ptr ContextImpl::InitResourceManagerInner( const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName, - std::shared_ptr inputContext) + std::shared_ptr inputContext, bool* deduplicate) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); std::shared_ptr resourceManager = InitOthersResourceManagerInner( @@ -1031,6 +1052,9 @@ std::shared_ptr ContextImpl::InitResourceMana TAG_LOGD(AAFwkTag::APPKIT, "loadPath is empty"); continue; } + if (deduplicate != nullptr) { + *deduplicate = hapModuleInfo.deduplicateHar; + } if (currentBundle) { loadPath = std::regex_replace(loadPath, inner_pattern, LOCAL_CODE_PATH); } else if (bundleInfo.applicationInfo.bundleType == AppExecFwk::BundleType::SHARED) { diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index c3ca7b8e1b3..0c3a5a2a723 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -1633,6 +1633,9 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con for (auto hapModuleInfo : bundleInfo.hapModuleInfos) { pkgContextInfoJsonStringMap[hapModuleInfo.moduleName] = hapModuleInfo.hapPath; + if (hapModuleInfo.deduplicateHar) { + application_->SetDeduplicateHar(hapModuleInfo.deduplicateHar); + } } GetNativeLibPath(bundleInfo, hspList, appLibPaths); @@ -1876,6 +1879,7 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con entryHapModuleInfo.hapPath.empty() ? entryHapModuleInfo.resourcePath : entryHapModuleInfo.hapPath; std::regex inner_pattern(std::string(ABS_CODE_PATH) + std::string(FILE_SEPARATOR) + bundleInfo.name); loadPath = std::regex_replace(loadPath, inner_pattern, LOCAL_CODE_PATH); + application_->SetEntryLoadPath(loadPath); auto res = GetOverlayModuleInfos(bundleInfo.name, moduleName, overlayModuleInfos_); std::vector overlayPaths; if (res == ERR_OK) { diff --git a/frameworks/native/appkit/app/ohos_application.cpp b/frameworks/native/appkit/app/ohos_application.cpp index 657f61beff4..4d1ed9b34bf 100644 --- a/frameworks/native/appkit/app/ohos_application.cpp +++ b/frameworks/native/appkit/app/ohos_application.cpp @@ -53,6 +53,26 @@ namespace AppExecFwk { namespace { constexpr const char* PERSIST_DARKMODE_KEY = "persist.ace.darkmode"; } +void OHOSApplication::LoadDeduplicatedHarResource( + const std::shared_ptr &stageContext) +{ + if (!stageContext) { + TAG_LOGE(AAFwkTag::APPKIT, "null argv"); + return; + } + auto resourceManager = stageContext->GetResourceManager(); + if (!resourceManager) { + TAG_LOGE(AAFwkTag::APPKIT, "null resourceManager"); + return; + } + auto hasDeduplicateHar = GetDeduplicateHar(); + auto loadPath = GetEntryLoadPath(); + if (!loadPath.empty() && hasDeduplicateHar) { + if (!resourceManager->AddResource(loadPath.c_str())) { + TAG_LOGE(AAFwkTag::APPKIT, "AddResource failed"); + } + } +} REGISTER_APPLICATION(OHOSApplication, OHOSApplication) constexpr int32_t APP_ENVIRONMENT_OVERWRITE = 1; using ApplicationConfigurationManager = AbilityRuntime::ApplicationConfigurationManager; @@ -433,6 +453,7 @@ std::shared_ptr OHOSApplication::AddAbilityStage( } else { stageContext->InitHapModuleInfo(abilityInfo); stageContext->SetParentContext(abilityRuntimeContext_); + LoadDeduplicatedHarResource(stageContext); } stageContext->SetConfiguration(GetConfiguration()); @@ -663,7 +684,7 @@ bool OHOSApplication::AddAbilityStage( auto rm = stageContext->CreateModuleContext(hapModuleInfo.moduleName)->GetResourceManager(); stageContext->SetResourceManager(rm); } - + LoadDeduplicatedHarResource(stageContext); auto &runtime = GetSpecifiedRuntime(moduleInfo->arkTSMode); auto abilityStage = AbilityRuntime::AbilityStage::Create(runtime, *moduleInfo); if (abilityStage == nullptr) { @@ -1100,6 +1121,20 @@ bool OHOSApplication::IsMainProcess(const std::string &bundleName, const std::st return false; } +void OHOSApplication::SetEntryLoadPath(const std::string &path) +{ + if (path.empty()) { + TAG_LOGE(AAFwkTag::APPKIT, "empty path"); + return; + } + entryLoadPath_ = path; +} + +void OHOSApplication::SetDeduplicateHar(const bool deduplicate) +{ + deduplicate_ = deduplicate; +} + #ifdef SUPPORT_GRAPHICS bool OHOSApplication::GetDisplayConfig(uint64_t displayId, float &density, std::string &directionStr) { diff --git a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h index 52a48ac299d..bbb4532013c 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h @@ -509,7 +509,7 @@ private: const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName); std::shared_ptr InitResourceManagerInner( const AppExecFwk::BundleInfo &bundleInfo, bool currentBundle, const std::string& moduleName, - std::shared_ptr inputContext = nullptr); + std::shared_ptr inputContext = nullptr, bool* deduplicate = nullptr); void GetOverlayPath(std::shared_ptr &resourceManager, const std::string &bundleName, const std::string &moduleName, std::string &loadPath, bool currentBundle, std::shared_ptr inputContext = nullptr); diff --git a/interfaces/kits/native/appkit/app/ohos_application.h b/interfaces/kits/native/appkit/app/ohos_application.h index 2b1844516d7..7089f4ef584 100644 --- a/interfaces/kits/native/appkit/app/ohos_application.h +++ b/interfaces/kits/native/appkit/app/ohos_application.h @@ -247,6 +247,18 @@ public: void PreloadAppStartup(const BundleInfo &bundleInfo, const std::string &preloadModuleName, std::shared_ptr startupTaskData); + void SetEntryLoadPath(const std::string &path); + const std::string &GetEntryLoadPath() const + { + return entryLoadPath_; + } + + void SetDeduplicateHar(bool deduplicate); + bool GetDeduplicateHar() const + { + return deduplicate_; + } + private: void UpdateAppContextResMgr(const Configuration &config); bool IsUpdateColorNeeded(Configuration &config, AbilityRuntime::SetLevel level); @@ -263,6 +275,8 @@ private: const std::function& callback); bool IsMainProcess(const std::string &bundleName, const std::string &process); void PreloadHybridModule(const HapModuleInfo &hapModuleInfo) const; + void LoadDeduplicatedHarResource( + const std::shared_ptr &stageContext); private: std::shared_ptr abilityRecordMgr_ = nullptr; @@ -271,6 +285,8 @@ private: std::unique_ptr runtime_ = nullptr; std::shared_ptr configuration_ = nullptr; std::map extensionTypeMap_; + std::string entryLoadPath_ = ""; + bool deduplicate_ = false; }; } // namespace AppExecFwk } // namespace OHOS -- Gitee