diff --git a/adapter/ohos/entrance/ui_content_impl.cpp b/adapter/ohos/entrance/ui_content_impl.cpp index a08e04ec719e8ce4afa2162541de6d8f8e9d56ee..e4b41d5c5d67b56a732e230bdab71cdf7736df26 100644 --- a/adapter/ohos/entrance/ui_content_impl.cpp +++ b/adapter/ohos/entrance/ui_content_impl.cpp @@ -57,6 +57,7 @@ const std::string START_PARAMS_KEY = "__startParams"; } // namespace static std::atomic gInstanceId = 0; +static std::atomic gSubWindowInstanceId = 100000; static std::atomic gSubInstanceId = 1000000; const std::string SUBWINDOW_PREFIX = "ARK_APP_SUBWINDOW_"; const int32_t REQUEST_CODE = -1; @@ -102,6 +103,12 @@ extern "C" ACE_EXPORT void* OHOS_ACE_CreateUIContent(void* context, void* runtim return new UIContentImpl(reinterpret_cast(context), runtime); } +extern "C" ACE_EXPORT void* OHOS_ACE_CreateSubWindowUIContent(void* ability) +{ + LOGI("Ace lib loaded, Create SubWindowUIContent."); + return new UIContentImpl(reinterpret_cast(ability)); +} + class OccupiedAreaChangeListener : public OHOS::Rosen::IOccupiedAreaChangeListener { public: explicit OccupiedAreaChangeListener(int32_t instanceId) : instanceId_(instanceId) {} @@ -186,8 +193,18 @@ UIContentImpl::UIContentImpl(OHOS::AbilityRuntime::Context* context, void* runti auto object = AbilityRuntime::ConvertNativeValueTo(ref->Get()); auto weak = static_cast*>(object->GetNativePointer()); context_ = *weak; + LOGI("Create UIContentImpl successfully."); +} - LOGI("Create UIContentImpl."); +UIContentImpl::UIContentImpl(OHOS::AppExecFwk::Ability* ability) +{ + if (ability == nullptr) { + LOGE("ability is nullptr"); + return; + } + auto weak = static_cast>(ability->GetAbilityContext()); + context_ = weak; + LOGI("Create UIContentImpl successfully."); } void UIContentImpl::Initialize(OHOS::Rosen::Window* window, const std::string& url, NativeValue* storage) @@ -384,7 +401,11 @@ void UIContentImpl::CommonInitialize(OHOS::Rosen::Window* window, const std::str auto pluginUtils = std::make_shared(); PluginManager::GetInstance().SetAceAbility(nullptr, pluginUtils); // create container - instanceId_ = gInstanceId.fetch_add(1, std::memory_order_relaxed); + if (runtime_) { + instanceId_ = gInstanceId.fetch_add(1, std::memory_order_relaxed); + } else { + instanceId_ = gSubWindowInstanceId.fetch_add(1, std::memory_order_relaxed); + } auto container = AceType::MakeRefPtr(instanceId_, FrontendType::DECLARATIVE_JS, true, context_, info, std::make_unique([context = context_] { auto sharedContext = context.lock(); @@ -420,8 +441,12 @@ void UIContentImpl::CommonInitialize(OHOS::Rosen::Window* window, const std::str // Mark the relationship between windowId and containerId, it is 1:1 SubwindowManager::GetInstance()->AddContainerId(window->GetWindowId(), instanceId_); AceEngine::Get().AddContainer(instanceId_, container); - container->GetSettings().SetUsingSharedRuntime(true); - container->SetSharedRuntime(runtime_); + if (runtime_) { + container->GetSettings().SetUsingSharedRuntime(true); + container->SetSharedRuntime(runtime_); + } else { + container->GetSettings().SetUsingSharedRuntime(false); + } container->SetPageProfile(pageProfile); container->Initialize(); ContainerScope scope(instanceId_); @@ -475,7 +500,6 @@ void UIContentImpl::CommonInitialize(OHOS::Rosen::Window* window, const std::str // set view Platform::AceContainer::SetView(flutterAceView, density, 0, 0, window_->GetWindowId(), callback); Platform::FlutterAceView::SurfaceChanged(flutterAceView, 0, 0, deviceHeight >= deviceWidth ? 0 : 1); - auto nativeEngine = reinterpret_cast(runtime_); // Set sdk version in module json mode if (isModelJson) { auto pipeline = container->GetPipelineContext(); @@ -484,12 +508,15 @@ void UIContentImpl::CommonInitialize(OHOS::Rosen::Window* window, const std::str pipeline->SetMinPlatformVersion(appInfo->minCompatibleVersionCode); } } - if (!storage) { - container->SetLocalStorage(nullptr, context->GetBindingObject()->Get()); - } else { - LOGI("SetLocalStorage %{public}d", storage->TypeOf()); - container->SetLocalStorage( - nativeEngine->CreateReference(storage, 1), context->GetBindingObject()->Get()); + if (runtime_) { + auto nativeEngine = reinterpret_cast(runtime_); + if (!storage) { + container->SetLocalStorage(nullptr, context->GetBindingObject()->Get()); + } else { + LOGI("SetLocalStorage %{public}d", storage->TypeOf()); + container->SetLocalStorage( + nativeEngine->CreateReference(storage, 1), context->GetBindingObject()->Get()); + } } InitWindowCallback(info); } diff --git a/adapter/ohos/entrance/ui_content_impl.h b/adapter/ohos/entrance/ui_content_impl.h index 26fbf5c62a0c3414d8df719268857b22cc77d7c8..89a5278f9530ed3d7b31afd065696036b868d560 100644 --- a/adapter/ohos/entrance/ui_content_impl.h +++ b/adapter/ohos/entrance/ui_content_impl.h @@ -37,6 +37,7 @@ namespace OHOS::Ace { class ACE_FORCE_EXPORT UIContentImpl : public UIContent { public: UIContentImpl(OHOS::AbilityRuntime::Context* context, void* runtime); + UIContentImpl(OHOS::AppExecFwk::Ability* ability); ~UIContentImpl() = default; // UI content lifecycles diff --git a/interfaces/innerkits/ace/ui_content.cpp b/interfaces/innerkits/ace/ui_content.cpp index fb5dc7a25fa6f2a33dab73a4fc3502074ba74bee..ed9b5fa127ef94f41e64f747d29a5628c9f7a2dc 100644 --- a/interfaces/innerkits/ace/ui_content.cpp +++ b/interfaces/innerkits/ace/ui_content.cpp @@ -20,7 +20,9 @@ namespace OHOS::Ace { using CreateFunc = UIContent* (*)(void*, void*); +using CreateFunction = UIContent* (*)(void*); constexpr char UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateUIContent"; +constexpr char SUB_WINDOW_UI_CONTENT_CREATE_FUNC[] = "OHOS_ACE_CreateSubWindowUIContent"; UIContent* CreateUIContent(void* context, void* runtime) { @@ -43,6 +45,27 @@ UIContent* CreateUIContent(void* context, void* runtime) return content; } +UIContent* CreateUIContent(void* ability) +{ + void* handle = dlopen("libace.z.so", RTLD_LAZY); + if (handle == nullptr) { + return nullptr; + } + + auto entry = reinterpret_cast(dlsym(handle, SUB_WINDOW_UI_CONTENT_CREATE_FUNC)); + if (entry == nullptr) { + dlclose(handle); + return nullptr; + } + + auto content = entry(ability); + if (content == nullptr) { + dlclose(handle); + } + + return content; +} + std::unique_ptr UIContent::Create(OHOS::AbilityRuntime::Context* context, NativeEngine* runtime) { std::unique_ptr content; @@ -50,4 +73,11 @@ std::unique_ptr UIContent::Create(OHOS::AbilityRuntime::Context* cont return content; } +std::unique_ptr UIContent::Create(OHOS::AppExecFwk::Ability* ability) +{ + std::unique_ptr content; + content.reset(CreateUIContent(reinterpret_cast(ability))); + return content; +} + } // namespace OHOS::Ace diff --git a/interfaces/innerkits/ace/ui_content.h b/interfaces/innerkits/ace/ui_content.h index ba539290b5b4a87571a6b0491ecc9a25bcbabe92..d6c961555a0393bfa311bc3fe0f3f8b1cba56964 100644 --- a/interfaces/innerkits/ace/ui_content.h +++ b/interfaces/innerkits/ace/ui_content.h @@ -29,6 +29,7 @@ class Context; namespace AppExecFwk { class Configuration; +class Ability; } namespace Rosen { @@ -59,6 +60,7 @@ namespace OHOS::Ace { class ACE_EXPORT UIContent { public: static std::unique_ptr Create(OHOS::AbilityRuntime::Context* context, NativeEngine* runtime); + static std::unique_ptr Create(OHOS::AppExecFwk::Ability* ability); virtual ~UIContent() = default;