From 0ce3b25cb3ff030ed07ce9cd985c477fa0d164f6 Mon Sep 17 00:00:00 2001 From: Caoruihong Date: Sun, 22 Jun 2025 01:39:33 +0800 Subject: [PATCH] fix PluginComponent Signed-off-by: Caoruihong --- .../components/plugin/hap_asset_provider.cpp | 102 ------------------ .../plugin/hap_asset_provider_impl.cpp | 8 ++ .../core/components/plugin/plugin_element.cpp | 31 +++++- .../core/components/plugin/plugin_element.h | 3 +- .../plugin/plugin_sub_container.cpp | 26 +++-- .../components/plugin/plugin_sub_container.h | 2 +- .../pattern/plugin/plugin_pattern.cpp | 33 +++++- .../pattern/plugin/plugin_pattern.h | 3 +- .../plugin/mock/mock_plugin_sub_container.cpp | 2 +- .../pattern/plugin/plugin_pattern_test_ng.cpp | 17 ++- 10 files changed, 108 insertions(+), 119 deletions(-) delete mode 100644 frameworks/core/components/plugin/hap_asset_provider.cpp diff --git a/frameworks/core/components/plugin/hap_asset_provider.cpp b/frameworks/core/components/plugin/hap_asset_provider.cpp deleted file mode 100644 index fe536d687f9..00000000000 --- a/frameworks/core/components/plugin/hap_asset_provider.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * 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 "core/components/plugin/hap_asset_provider.h" - -#include "base/log/ace_trace.h" -#include "base/log/log.h" -#include "base/utils/utils.h" - -namespace OHOS::Ace::Plugin { -bool HapAssetProvider::Initialize(const std::string& hapPath, const std::vector& assetBasePaths) -{ - ACE_SCOPED_TRACE("Initialize"); - if (hapPath.empty() || assetBasePaths.empty()) { - return false; - } - - bool newCreate = false; - runtimeExtractor_ = AbilityBase::ExtractorUtil::GetExtractor(hapPath, newCreate); - CHECK_NULL_RETURN_NOLOG(runtimeExtractor_, false); - assetBasePaths_ = assetBasePaths; - hapPath_ = hapPath; - return true; -} - -bool HapAssetProvider::IsValid() const -{ - return true; -} - -std::unique_ptr HapAssetProvider::GetAsMapping(const std::string& assetName) const -{ - ACE_SCOPED_TRACE("GetAsMapping"); - std::lock_guard lock(mutex_); - CHECK_NULL_RETURN(runtimeExtractor_, nullptr); - for (const auto& basePath : assetBasePaths_) { - std::string fileName = basePath + assetName; - bool hasFile = runtimeExtractor_->HasEntry(fileName); - if (!hasFile) { - continue; - } - std::ostringstream osStream; - hasFile = runtimeExtractor_->GetFileBuffer(fileName, osStream); - if (!hasFile) { - continue; - } - return std::make_unique(osStream); - } - return nullptr; -} - -std::string HapAssetProvider::GetAssetPath(const std::string& assetName, bool isAddHapPath) -{ - std::lock_guard lock(mutex_); - CHECK_NULL_RETURN(runtimeExtractor_, ""); - for (const auto& basePath : assetBasePaths_) { - std::string fileName = basePath + assetName; - bool hasFile = runtimeExtractor_->HasEntry(fileName); - if (!hasFile) { - continue; - } - return isAddHapPath? (hapPath_ + "/" + basePath) : fileName; - } - return {}; -} - -void HapAssetProvider::GetAssetList(const std::string& path, std::vector& assetList) -{ - std::lock_guard lock(mutex_); - if (!runtimeExtractor_) { - return; - } - for (const auto& basePath : assetBasePaths_) { - std::string assetPath = basePath + path; - bool res = runtimeExtractor_->IsDirExist(assetPath); - if (!res) { - continue; - } - res = runtimeExtractor_->GetFileList(assetPath, assetList); - if (!res) { - continue; - } - } -} - -bool HapAssetProvider::GetFileInfo(const std::string& /* fileName */, MediaFileInfo& /* fileInfo */) const -{ - return false; -} -} // namespace OHOS::Ace::Plugin \ No newline at end of file diff --git a/frameworks/core/components/plugin/hap_asset_provider_impl.cpp b/frameworks/core/components/plugin/hap_asset_provider_impl.cpp index 71790c54f63..e5467bc9c8f 100644 --- a/frameworks/core/components/plugin/hap_asset_provider_impl.cpp +++ b/frameworks/core/components/plugin/hap_asset_provider_impl.cpp @@ -52,6 +52,14 @@ std::unique_ptr HapAssetProviderImpl::GetAsMapping(const std::stri continue; } std::ostringstream osStream; + auto mapper = runtimeExtractor_->GetSafeData(fileName); + if (!mapper) { + continue; + } + if (mapper->GetDataLen()) { + osStream.write((char*)mapper->GetDataPtr(), mapper->GetDataLen()); + return std::make_unique(osStream); + } hasFile = runtimeExtractor_->GetFileBuffer(fileName, osStream); if (!hasFile) { continue; diff --git a/frameworks/core/components/plugin/plugin_element.cpp b/frameworks/core/components/plugin/plugin_element.cpp index 282999373cf..0063c9692e2 100644 --- a/frameworks/core/components/plugin/plugin_element.cpp +++ b/frameworks/core/components/plugin/plugin_element.cpp @@ -249,7 +249,7 @@ void PluginElement::RunPluginContainer() PluginManager::GetInstance().AddPluginParentContainer(pluginSubContainerId_, parentContainerId); pluginSubContainer_->SetInstanceId(pluginSubContainerId_); pluginSubContainer_->SetPluginWindowId(GetElementId()); - pluginSubContainer_->Initialize(); + pluginSubContainer_->Initialize(GetPackageCodeLanguage(pluginInfo_)); pluginSubContainer_->SetPluginComponent(component_); auto weak = WeakClaim(this); @@ -313,6 +313,33 @@ std::string PluginElement::GetPackagePathByAbsolutePath( return packagePathStr; } +std::string PluginElement::GetPackageCodeLanguage(const RequestPluginInfo& info) const +{ + std::string codeLanguage; + + std::vector strList; + SplitString(info.bundleName, '/', strList); + if (strList.empty()) { + return codeLanguage; + } + + std::vector userIds; + GetActiveAccountIds(userIds); + + auto bms = PluginComponentManager::GetInstance()->GetBundleManager(); + if (!bms) { + return codeLanguage; + } + + AppExecFwk::BundleInfo bundleInfo; + bool ret = bms->GetBundleInfo(strList[0], AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, + userIds.size() > 0 ? userIds[0] : AppExecFwk::Constants::UNSPECIFIED_USERID); + if (!ret) { + return codeLanguage; + } + return bundleInfo.applicationInfo.codeLanguage; +} + std::string PluginElement::GetPackagePathByWant(const WeakPtr& weak, RequestPluginInfo& info) const { std::string packagePathStr; @@ -427,7 +454,7 @@ std::string PluginElement::GetPackagePathByBms(const WeakPtr& wea return packagePathStr; } -void PluginElement::SplitString(const std::string& str, char tag, std::vector& strList) +void PluginElement::SplitString(const std::string& str, char tag, std::vector& strList) const { std::string subStr; for (size_t i = 0; i < str.length(); i++) { diff --git a/frameworks/core/components/plugin/plugin_element.h b/frameworks/core/components/plugin/plugin_element.h index 5d04ac8c78f..fe90865cd28 100644 --- a/frameworks/core/components/plugin/plugin_element.h +++ b/frameworks/core/components/plugin/plugin_element.h @@ -54,8 +54,9 @@ public: private: void RunPluginContainer(); void InitEvent(const RefPtr& component); - void SplitString(const std::string& str, char tag, std::vector& strList); + void SplitString(const std::string& str, char tag, std::vector& strList) const; void ReplaceAll(std::string& str, const std::string& pattern, const std::string& newPattern); + std::string GetPackageCodeLanguage(const RequestPluginInfo& info) const; std::string GetPackagePath(const WeakPtr& weak, RequestPluginInfo& info) const; std::string GetPackagePathByWant(const WeakPtr& weak, RequestPluginInfo& info) const; std::string GetPackagePathByAbsolutePath(const WeakPtr& weak, RequestPluginInfo& info) const; diff --git a/frameworks/core/components/plugin/plugin_sub_container.cpp b/frameworks/core/components/plugin/plugin_sub_container.cpp index b73c1e70303..895278b8eb8 100644 --- a/frameworks/core/components/plugin/plugin_sub_container.cpp +++ b/frameworks/core/components/plugin/plugin_sub_container.cpp @@ -27,6 +27,7 @@ #include "core/components/plugin/plugin_window.h" #include "core/components/plugin/render_plugin.h" #include "bridge/arkts_frontend/arkts_plugin_frontend.h" +#include "ability_info.h" namespace OHOS::Ace { namespace { @@ -39,9 +40,10 @@ const char* GetDeclarativeSharedLibrary() return DECLARATIVE_ARK_ENGINE_SHARED_LIB; } -void PluginSubContainer::Initialize() +void PluginSubContainer::Initialize(const std::string& codeLanguage) { - TAG_LOGI(AceLogTag::ACE_PLUGIN_COMPONENT, "PluginSubContainer initialize start."); + TAG_LOGI(AceLogTag::ACE_PLUGIN_COMPONENT, + "PluginSubContainer initialize start. codeLanguage:%{public}s", codeLanguage.c_str()); ContainerScope scope(instanceId_); auto outSidePipelineContext = outSidePipelineContext_.Upgrade(); @@ -61,15 +63,25 @@ void PluginSubContainer::Initialize() return; } - if (outSidePipelineContext->GetFrontendType() == FrontendType::ARK_TS) { + if (codeLanguage == OHOS::AppExecFwk::Constants::CODE_LANGUAGE_1_2) { + if (outSidePipelineContext->GetFrontendType() != FrontendType::ARK_TS) { + TAG_LOGE(AceLogTag::ACE_PLUGIN_COMPONENT, + "codeLanguage %{public}s is not supported in frontend type %{public}d.", + codeLanguage.c_str(), outSidePipelineContext->GetFrontendType()); + return; + } frontend_ = AceType::MakeRefPtr(container->GetSharedRuntime()); frontend_->Initialize(FrontendType::ARK_TS, taskExecutor_); TAG_LOGI(AceLogTag::ACE_PLUGIN_COMPONENT, "PluginSubContainer initialize end."); return; - } - frontend_ = AceType::MakeRefPtr(); - if (!frontend_) { - return; + } else { + if (outSidePipelineContext->GetFrontendType() == FrontendType::ARK_TS) { + TAG_LOGE(AceLogTag::ACE_PLUGIN_COMPONENT, + "codeLanguage %{public}s is not supported in frontend type %{public}d.", + codeLanguage.c_str(), outSidePipelineContext->GetFrontendType()); + return; + } + frontend_ = AceType::MakeRefPtr(); } // set JS engine,init in JS thread diff --git a/frameworks/core/components/plugin/plugin_sub_container.h b/frameworks/core/components/plugin/plugin_sub_container.h index e6c263f98df..4f094c6823f 100644 --- a/frameworks/core/components/plugin/plugin_sub_container.h +++ b/frameworks/core/components/plugin/plugin_sub_container.h @@ -38,7 +38,7 @@ public: {} ~PluginSubContainer() = default; - void Initialize(); + void Initialize(const std::string& codeLanguage); void RunPlugin(const std::string& path, const std::string& module, const std::string& source, const std::string& moduleResPath, const std::string& data); void RunDecompressedPlugin(const std::string& path, const std::string& module, const std::string& source, diff --git a/frameworks/core/components_ng/pattern/plugin/plugin_pattern.cpp b/frameworks/core/components_ng/pattern/plugin/plugin_pattern.cpp index 1dd42c2e864..acd3f5f09cc 100755 --- a/frameworks/core/components_ng/pattern/plugin/plugin_pattern.cpp +++ b/frameworks/core/components_ng/pattern/plugin/plugin_pattern.cpp @@ -225,7 +225,7 @@ void PluginPattern::CreatePluginSubContainer() PluginManager::GetInstance().AddPluginSubContainer(pluginSubContainerId_, pluginSubContainer_); PluginManager::GetInstance().AddPluginParentContainer(pluginSubContainerId_, parentcontainerId); - pluginSubContainer_->Initialize(); + pluginSubContainer_->Initialize(GetPackageCodeLanguage(GetPluginRequestInfo())); auto weak = WeakClaim(this); pluginSubContainer_->SetPluginPattern(weak); auto pattern = weak.Upgrade(); @@ -406,6 +406,33 @@ void PluginPattern::SplitString(const std::string& str, char tag, std::vector strList; + SplitString(info.bundleName, '/', strList); + if (strList.empty()) { + return codeLanguage; + } + + std::vector userIds; + GetActiveAccountIds(userIds); + + auto bms = PluginComponentManager::GetInstance()->GetBundleManager(); + if (!bms) { + return codeLanguage; + } + + AppExecFwk::BundleInfo bundleInfo; + bool ret = bms->GetBundleInfo(strList[0], AppExecFwk::BundleFlag::GET_BUNDLE_DEFAULT, bundleInfo, + userIds.size() > 0 ? userIds[0] : AppExecFwk::Constants::UNSPECIFIED_USERID); + if (!ret) { + return codeLanguage; + } + return bundleInfo.applicationInfo.codeLanguage; +} + std::string PluginPattern::GetPackagePath(const WeakPtr& weak, RequestPluginInfo& info) const { std::string packagePathStr; @@ -438,7 +465,7 @@ std::string PluginPattern::GetPackagePathByWant(const WeakPtr& we return packagePathStr; } GetAbilityNameByWant(weak, info); - packagePathStr = GerPackagePathByBms(weak, info, strList, userIds); + packagePathStr = GetPackagePathByBms(weak, info, strList, userIds); return packagePathStr; } @@ -503,7 +530,7 @@ void PluginPattern::GetAbilityNameByWant(const WeakPtr& weak, Req } } -std::string PluginPattern::GerPackagePathByBms(const WeakPtr& weak, RequestPluginInfo& info, +std::string PluginPattern::GetPackagePathByBms(const WeakPtr& weak, RequestPluginInfo& info, const std::vector& strList, const std::vector& userIds) const { std::string packagePathStr; diff --git a/frameworks/core/components_ng/pattern/plugin/plugin_pattern.h b/frameworks/core/components_ng/pattern/plugin/plugin_pattern.h index 15db1eb4070..591cf8d8d9e 100644 --- a/frameworks/core/components_ng/pattern/plugin/plugin_pattern.h +++ b/frameworks/core/components_ng/pattern/plugin/plugin_pattern.h @@ -85,11 +85,12 @@ private: bool ISAllowUpdate() const; void SplitString(const std::string& str, char tag, std::vector& strList) const; + std::string GetPackageCodeLanguage(const RequestPluginInfo& info) const; std::string GetPackagePath(const WeakPtr& weak, RequestPluginInfo& info) const; std::string GetPackagePathByWant(const WeakPtr& weak, RequestPluginInfo& info) const; std::string GetPackagePathByAbsolutePath(const WeakPtr& weak, RequestPluginInfo& info) const; void GetAbilityNameByWant(const WeakPtr& weak, RequestPluginInfo& info) const; - std::string GerPackagePathByBms(const WeakPtr& weak, RequestPluginInfo& info, + std::string GetPackagePathByBms(const WeakPtr& weak, RequestPluginInfo& info, const std::vector& strList, const std::vector& userIds) const; #ifndef ARKUI_CAPI_UNITTEST diff --git a/test/unittest/core/pattern/plugin/mock/mock_plugin_sub_container.cpp b/test/unittest/core/pattern/plugin/mock/mock_plugin_sub_container.cpp index f81ba2f0e75..d2f925b0790 100644 --- a/test/unittest/core/pattern/plugin/mock/mock_plugin_sub_container.cpp +++ b/test/unittest/core/pattern/plugin/mock/mock_plugin_sub_container.cpp @@ -25,7 +25,7 @@ const char* GetDeclarativeSharedLibrary() return DECLARATIVE_ARK_ENGINE_SHARED_LIB; } -void PluginSubContainer::Initialize() {} +void PluginSubContainer::Initialize(const std::string& codeLanguage) {} void PluginSubContainer::Destroy() {} diff --git a/test/unittest/core/pattern/plugin/plugin_pattern_test_ng.cpp b/test/unittest/core/pattern/plugin/plugin_pattern_test_ng.cpp index 4f14c407b75..77e1df42ded 100644 --- a/test/unittest/core/pattern/plugin/plugin_pattern_test_ng.cpp +++ b/test/unittest/core/pattern/plugin/plugin_pattern_test_ng.cpp @@ -615,7 +615,22 @@ HWTEST_F(PluginPatternTestNg, GetDrawDelegate, TestSize.Level1) drawRSFrame->DrawRSFrame(rsNode, Rect(100.0, 100.0, 100.0, 100.0)); } -/**s +/** + * @tc.name: PluginCodeLanguageTestNg + * @tc.desc: Test funcions in Plugin Pattern. + * @tc.type: FUNC + */ +HWTEST_F(PluginPatternTestNg, PluginCodeLanguageTestNg, TestSize.Level1) +{ + RefPtr frameNode = CreatePluginParagraph(); + auto pattern = frameNode->GetPattern(); + ASSERT_NE(pattern, nullptr); + auto info = pattern->GetPluginRequestInfo(); + auto codeLanguage = pattern->GetPackageCodeLanguage(info); + EXPECT_EQ(codeLanguage, ""); +} + +/** * @tc.name: PluginLayoutPropertyTestNg * @tc.desc: Test funcions in PluginLayoutProperty. * @tc.type: FUNC -- Gitee