From fd45affbaba5309af42226c85e4eebd9d0e99a34 Mon Sep 17 00:00:00 2001 From: zhuhan Date: Fri, 26 May 2023 17:08:24 +0800 Subject: [PATCH] bug fix Signed-off-by: zhuhan Change-Id: I0ffcc5e4d3b7539a6cc5bc40b74526f43ad34bf1 --- frameworks/native/runtime/js_worker.cpp | 33 +++++++++++++------------ frameworks/native/runtime/js_worker.h | 12 ++++++--- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/frameworks/native/runtime/js_worker.cpp b/frameworks/native/runtime/js_worker.cpp index e2baf66e44..151632a132 100644 --- a/frameworks/native/runtime/js_worker.cpp +++ b/frameworks/native/runtime/js_worker.cpp @@ -107,7 +107,7 @@ void OffWorkerFunc(NativeEngine* nativeEngine) using Extractor = AbilityBase::Extractor; using ExtractorUtil = AbilityBase::ExtractorUtil; -using BundleMgrProxy = AppExecFwk::BundleMgrProxy; +using IBundleMgr = AppExecFwk::IBundleMgr; std::string AssetHelper::NormalizedFileName(const std::string& fileName) const { @@ -126,7 +126,7 @@ std::string AssetHelper::NormalizedFileName(const std::string& fileName) const return normalizedFilePath; } -void AssetHelper::operator()(const std::string& uri, std::vector& content, std::string &ami) const +void AssetHelper::operator()(const std::string& uri, std::vector& content, std::string &ami) { if (uri.empty()) { HILOG_ERROR("Uri is empty."); @@ -197,23 +197,24 @@ void AssetHelper::operator()(const std::string& uri, std::vector& conte } } -sptr AssetHelper::GetBundleMgrProxy() const +sptr AssetHelper::GetBundleMgrProxy() { - auto systemAbilityManager = - SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (!systemAbilityManager) { - HILOG_ERROR("fail to get system ability mgr."); - return nullptr; - } - - auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); - if (!remoteObject) { - HILOG_ERROR("fail to get bundle manager proxy."); - return nullptr; + if (bundleMgrProxy_ == nullptr) { + auto systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (!systemAbilityManager) { + HILOG_ERROR("fail to get system ability mgr."); + return nullptr; + } + auto remoteObject = systemAbilityManager->GetSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID); + if (!remoteObject) { + HILOG_ERROR("fail to get bundle manager proxy."); + return nullptr; + } + bundleMgrProxy_ = iface_cast(remoteObject); } HILOG_DEBUG("get bundle manager proxy success."); - return iface_cast(remoteObject); + return bundleMgrProxy_; } bool AssetHelper::ReadAmiData(const std::string& ami, std::vector& content) const @@ -243,7 +244,7 @@ bool AssetHelper::ReadAmiData(const std::string& ami, std::vector& cont return true; } -bool AssetHelper::ReadFilePathData(const std::string& filePath, std::vector& content) const +bool AssetHelper::ReadFilePathData(const std::string& filePath, std::vector& content) { auto bundleMgrProxy = GetBundleMgrProxy(); if (!bundleMgrProxy) { diff --git a/frameworks/native/runtime/js_worker.h b/frameworks/native/runtime/js_worker.h index 5bf02f6f31..5700277822 100644 --- a/frameworks/native/runtime/js_worker.h +++ b/frameworks/native/runtime/js_worker.h @@ -29,7 +29,8 @@ int32_t GetContainerId(); void UpdateContainerScope(int32_t id); void RestoreContainerScope(int32_t id); -struct AssetHelper final { +class AssetHelper final { +public: explicit AssetHelper(const std::string& codePath, bool isDebugVersion, bool isBundle) : codePath_(codePath), isDebugVersion_(isDebugVersion), isBundle_(isBundle) { @@ -40,17 +41,20 @@ struct AssetHelper final { std::string NormalizedFileName(const std::string& fileName) const; - void operator()(const std::string& uri, std::vector& content, std::string &ami) const; + void operator()(const std::string& uri, std::vector& content, std::string &ami); - sptr GetBundleMgrProxy() const; + sptr GetBundleMgrProxy(); bool ReadAmiData(const std::string& ami, std::vector& content) const; - bool ReadFilePathData(const std::string& filePath, std::vector& content) const; + bool ReadFilePathData(const std::string& filePath, std::vector& content); std::string codePath_; bool isDebugVersion_ = false; bool isBundle_ = true; + +private: + sptr bundleMgrProxy_ = nullptr; }; } // namespace AbilityRuntime