diff --git a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp index 48f7491f3827b595657bd4cb0c2192c6cf6a481b..8c755dfe00672f4d24b5657bec7bf3ecebee4125 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 c3ca7b8e1b394e644eb657f27197b290d56dfb5d..0c3a5a2a723b8739fec60e3b5b5a45dfc7f4167e 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 657f61beff425bb0ba1d5a46e680987c1b50ac8c..4d1ed9b34bf62c62eae6c048c24268b9271963ca 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 52a48ac299df5faf86d8e24a12f5b5618bd01f6e..bbb4532013c80728fc9f6d25ac095e7fb6f631cd 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 2b1844516d7cde7dbb37f997f3d44b6946bbc5d6..7089f4ef58407f7b32d58c5f3c2bb8790ec51598 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