diff --git a/frameworks/bridge/card_frontend/card_frontend.h b/frameworks/bridge/card_frontend/card_frontend.h index ec223d81fdb..c6ff09cf267 100644 --- a/frameworks/bridge/card_frontend/card_frontend.h +++ b/frameworks/bridge/card_frontend/card_frontend.h @@ -127,11 +127,6 @@ public: return eventHandler_; } - FrontendType GetType() override - { - return type_; - } - // judge frontend is foreground frontend. bool IsForeground() override { @@ -205,7 +200,6 @@ protected: void ParseManifest() const; ColorMode colorMode_ = ColorMode::LIGHT; - FrontendType type_ = FrontendType::JS_CARD; bool foregroundFrontend_ = false; bool pageLoaded_ = false; double density_ = 1.0; @@ -217,7 +211,6 @@ protected: OnFormVisibleCallback onFormVisibleCallback_; mutable std::once_flag onceFlag_; - RefPtr taskExecutor_; RefPtr eventHandler_; Framework::PageIdPool pageIdPool_; std::string formSrc_; diff --git a/frameworks/bridge/declarative_frontend/declarative_frontend.cpp b/frameworks/bridge/declarative_frontend/declarative_frontend.cpp index 8a1ed7780ff..18641fda953 100644 --- a/frameworks/bridge/declarative_frontend/declarative_frontend.cpp +++ b/frameworks/bridge/declarative_frontend/declarative_frontend.cpp @@ -182,6 +182,7 @@ bool DeclarativeFrontend::Initialize(FrontendType type, const RefPtr& page) override {}; + void AddPage(const RefPtr& page) override {} RefPtr GetPage(int32_t /*pageId*/) const override { return nullptr; - }; + } std::string GetCurrentPageUrl() const override; @@ -153,11 +153,6 @@ public: RefPtr GetEventHandler() override { return handler_; - }; - - FrontendType GetType() override - { - return type_; } // judge frontend is foreground frontend. @@ -237,7 +232,6 @@ protected: private: void InitializeFrontendDelegate(const RefPtr& taskExecutor); - FrontendType type_ = FrontendType::DECLARATIVE_JS; RefPtr handler_; RefPtr jsEngine_; RefPtr accessibilityManager_; diff --git a/frameworks/bridge/declarative_frontend/ng/declarative_frontend_ng.cpp b/frameworks/bridge/declarative_frontend/ng/declarative_frontend_ng.cpp index 681bca65531..69e8aec7f79 100644 --- a/frameworks/bridge/declarative_frontend/ng/declarative_frontend_ng.cpp +++ b/frameworks/bridge/declarative_frontend/ng/declarative_frontend_ng.cpp @@ -41,6 +41,7 @@ bool DeclarativeFrontendNG::Initialize(FrontendType type, const RefPtr GetEventHandler() override { return nullptr; - }; - - FrontendType GetType() override - { - return type_; } // judge frontend is foreground frontend. @@ -192,11 +187,9 @@ public: private: void InitializeDelegate(const RefPtr& taskExecutor); - FrontendType type_ = FrontendType::DECLARATIVE_JS; RefPtr jsEngine_; RefPtr delegate_; RefPtr accessibilityManager_; - RefPtr taskExecutor_; std::string pageProfile_; bool foregroundFrontend_ = false; diff --git a/frameworks/bridge/js_frontend/js_frontend.cpp b/frameworks/bridge/js_frontend/js_frontend.cpp index e7b80aabb07..837beb5d552 100644 --- a/frameworks/bridge/js_frontend/js_frontend.cpp +++ b/frameworks/bridge/js_frontend/js_frontend.cpp @@ -235,6 +235,19 @@ RefPtr Frontend::Create() return AceType::MakeRefPtr(); } +bool Frontend::MaybeRelease() +{ + CHECK_RUN_ON(JS); + CHECK_NULL_RETURN(taskExecutor_, true); + if (taskExecutor_->WillRunOnCurrentThread(TaskExecutor::TaskType::JS)) { + LOGI("Destroy Frontend on JS thread."); + return true; + } else { + LOGI("Post Destroy Frontend Task to JS thread."); + return !taskExecutor_->PostTask([this] { delete this; }, TaskExecutor::TaskType::JS); + } +} + JsFrontend::~JsFrontend() noexcept { LOG_DESTROY(); @@ -255,6 +268,7 @@ bool JsFrontend::Initialize(FrontendType type, const RefPtr& taskE { LOGI("JsFrontend initialize begin."); type_ = type; + taskExecutor_ = taskExecutor; ACE_DCHECK(type_ == FrontendType::JS); InitializeFrontendDelegate(taskExecutor); auto weakEngine = AceType::WeakClaim(AceType::RawPtr(jsEngine_)); diff --git a/frameworks/bridge/js_frontend/js_frontend.h b/frameworks/bridge/js_frontend/js_frontend.h index 0153056a556..0a6f493d9d3 100644 --- a/frameworks/bridge/js_frontend/js_frontend.h +++ b/frameworks/bridge/js_frontend/js_frontend.h @@ -60,12 +60,12 @@ public: void PushPage(const std::string& url, const std::string& params) override; // Js frontend manages all pages self. - void AddPage(const RefPtr& page) override {}; + void AddPage(const RefPtr& page) override {} RefPtr GetPage(int32_t pageId) const override { return delegate_->GetPage(pageId); - }; + } WeakPtr GetCurrentReadyPage() const { @@ -130,11 +130,6 @@ public: RefPtr GetEventHandler() override { return handler_; - }; - - FrontendType GetType() override - { - return type_; } // judge frontend is foreground frontend. @@ -179,7 +174,6 @@ public: private: void InitializeFrontendDelegate(const RefPtr& taskExecutor); - FrontendType type_ = FrontendType::JS; RefPtr delegate_; RefPtr handler_; RefPtr jsEngine_; diff --git a/frameworks/bridge/plugin_frontend/plugin_frontend.cpp b/frameworks/bridge/plugin_frontend/plugin_frontend.cpp index f973afb8fda..18695190ff3 100644 --- a/frameworks/bridge/plugin_frontend/plugin_frontend.cpp +++ b/frameworks/bridge/plugin_frontend/plugin_frontend.cpp @@ -176,6 +176,7 @@ void PluginFrontend::Destroy() bool PluginFrontend::Initialize(FrontendType type, const RefPtr& taskExecutor) { type_ = type; + taskExecutor_ = taskExecutor; ACE_DCHECK(type_ == FrontendType::JS_PLUGIN); InitializeFrontendDelegate(taskExecutor); taskExecutor->PostSyncTask( diff --git a/frameworks/bridge/plugin_frontend/plugin_frontend.h b/frameworks/bridge/plugin_frontend/plugin_frontend.h index ec5ce9dc239..e7a79f19e60 100644 --- a/frameworks/bridge/plugin_frontend/plugin_frontend.h +++ b/frameworks/bridge/plugin_frontend/plugin_frontend.h @@ -60,12 +60,12 @@ public: void PushPage(const std::string& url, const std::string& params) override; // Js frontend manages all pages self. - void AddPage(const RefPtr& page) override {}; + void AddPage(const RefPtr& page) override {} RefPtr GetPage(int32_t pageId) const override { return nullptr; - }; + } void TriggerGarbageCollection() override; @@ -119,11 +119,6 @@ public: RefPtr GetEventHandler() override { return handler_; - }; - - FrontendType GetType() override - { - return type_; } // judge frontend is foreground frontend. @@ -216,7 +211,6 @@ public: private: void InitializeFrontendDelegate(const RefPtr& taskExecutor); - FrontendType type_ = FrontendType::DECLARATIVE_JS; RefPtr delegate_; RefPtr handler_; RefPtr jsEngine_; diff --git a/frameworks/bridge/test/unittest/jsfrontend/dom_mock.h b/frameworks/bridge/test/unittest/jsfrontend/dom_mock.h index cc90de2a0a6..eb323d71135 100644 --- a/frameworks/bridge/test/unittest/jsfrontend/dom_mock.h +++ b/frameworks/bridge/test/unittest/jsfrontend/dom_mock.h @@ -30,13 +30,13 @@ public: ~MockPlatformWindow() override = default; // Request next vsync. - void RequestFrame() override {}; + void RequestFrame() override {} // Register Vsync callback. - void RegisterVsyncCallback(AceVsyncCallback&& callback) override {}; + void RegisterVsyncCallback(AceVsyncCallback&& callback) override {} // Attach root render node to container - void SetRootRenderNode(const RefPtr& root) override {}; + void SetRootRenderNode(const RefPtr& root) override {} }; class MockFrontend : public Frontend { @@ -66,11 +66,6 @@ public: return windowConfig_; } - FrontendType GetType() override - { - return FrontendType::JS; - } - bool OnBackPressed() override { return false; diff --git a/frameworks/core/common/frontend.h b/frameworks/core/common/frontend.h index 84ee7a1ad97..af23e64b296 100644 --- a/frameworks/core/common/frontend.h +++ b/frameworks/core/common/frontend.h @@ -110,7 +110,7 @@ public: // Get the stage mode sourceMap. virtual void GetStageSourceMap( - std::unordered_map>& sourceMap) const {}; + std::unordered_map>& sourceMap) const {} virtual void RunPage(int32_t pageId, const std::string& content, const std::string& params) = 0; @@ -124,7 +124,15 @@ public: // Get window config of front end, which is used to calculate the pixel ratio of the real device. virtual WindowConfig& GetWindowConfig() = 0; - virtual FrontendType GetType() = 0; + FrontendType GetType() const + { + return type_; + } + + RefPtr GetTaskExecutor() const + { + return taskExecutor_; + } // inform the frontend that onCreate or onDestroy virtual void UpdateState(State) = 0; @@ -268,7 +276,7 @@ public: virtual void FlushReload() {} // flush frontend for HotReload feature in NG - virtual void HotReload() {}; + virtual void HotReload() {} State GetState() const { @@ -278,6 +286,9 @@ public: virtual void SetErrorEventHandler(std::function&& errorCallback) {} protected: + virtual bool MaybeRelease() override; + FrontendType type_ = FrontendType::JS; + RefPtr taskExecutor_; bool disallowPopLastPage_ = false; FrontendDialogCallback dialogCallback_ = nullptr; State state_ = State::UNDEFINE; diff --git a/frameworks/core/components/test/json/json_frontend.cpp b/frameworks/core/components/test/json/json_frontend.cpp index 2949c616461..29353145e0b 100644 --- a/frameworks/core/components/test/json/json_frontend.cpp +++ b/frameworks/core/components/test/json/json_frontend.cpp @@ -26,10 +26,10 @@ RefPtr Frontend::CreateDefault() return AceType::MakeRefPtr(); } -bool JsonFrontend::Initialize(FrontendType type, const RefPtr&) +bool JsonFrontend::Initialize(FrontendType type, const RefPtr& taskExecutor) { - // This interface is inherited from base class 'Frontend', but we never use 'TaskExecutor'. type_ = type; + taskExecutor_ = taskExecutor; return true; } diff --git a/frameworks/core/components/test/json/json_frontend.h b/frameworks/core/components/test/json/json_frontend.h index 26bd185fe08..36f6f35c1f7 100644 --- a/frameworks/core/components/test/json/json_frontend.h +++ b/frameworks/core/components/test/json/json_frontend.h @@ -120,11 +120,6 @@ public: return windowConfig_; } - FrontendType GetType() override - { - return type_; - } - void SendCallbackMessage(const std::string& callbackId, const std::string& data) const override { // maybe will implement later @@ -172,7 +167,6 @@ public: } private: - FrontendType type_ { FrontendType::JSON }; static constexpr int32_t JSON_DESIGN_WIDTH = 1080; std::unordered_map> pageMap_; RefPtr pipelineContext_; diff --git a/frameworks/core/components_ng/test/pattern/plugin/mock/mock_plugin_frontend.cpp b/frameworks/core/components_ng/test/pattern/plugin/mock/mock_plugin_frontend.cpp index 81442d798e3..179c54fd7ab 100644 --- a/frameworks/core/components_ng/test/pattern/plugin/mock/mock_plugin_frontend.cpp +++ b/frameworks/core/components_ng/test/pattern/plugin/mock/mock_plugin_frontend.cpp @@ -22,6 +22,8 @@ void PluginFrontend::Destroy() {} bool PluginFrontend::Initialize(FrontendType type, const RefPtr& taskExecutor) { + type_ = type; + taskExecutor_ = taskExecutor; return true; } diff --git a/frameworks/core/pipeline/pipeline_base.cpp b/frameworks/core/pipeline/pipeline_base.cpp index c1a8d93e05f..654401a9477 100644 --- a/frameworks/core/pipeline/pipeline_base.cpp +++ b/frameworks/core/pipeline/pipeline_base.cpp @@ -723,6 +723,19 @@ void PipelineBase::RemoveSubWindowVsyncCallback(int32_t subWindowId) subWindowVsyncCallbacks_.erase(subWindowId); } +bool PipelineBase::MaybeRelease() +{ + CHECK_RUN_ON(UI); + CHECK_NULL_RETURN(taskExecutor_, true); + if (taskExecutor_->WillRunOnCurrentThread(TaskExecutor::TaskType::UI)) { + LOGI("Destroy Pipeline on UI thread."); + return true; + } else { + LOGI("Post Destroy Pipeline Task to UI thread."); + return !taskExecutor_->PostTask([this] { delete this; }, TaskExecutor::TaskType::UI); + } +} + void PipelineBase::Destroy() { CHECK_RUN_ON(UI); diff --git a/frameworks/core/pipeline/pipeline_base.h b/frameworks/core/pipeline/pipeline_base.h index add9ead410d..fb04dc23ad0 100644 --- a/frameworks/core/pipeline/pipeline_base.h +++ b/frameworks/core/pipeline/pipeline_base.h @@ -910,6 +910,7 @@ public: } protected: + virtual bool MaybeRelease() override; void TryCallNextFrameLayoutCallback() { if (isForegroundCalled_ && nextFrameLayoutCallback_) { diff --git a/frameworks/core/pipeline_ng/test/mock/mock_pipeline_base.cpp b/frameworks/core/pipeline_ng/test/mock/mock_pipeline_base.cpp index a58a5267cde..36bd5c17eb5 100644 --- a/frameworks/core/pipeline_ng/test/mock/mock_pipeline_base.cpp +++ b/frameworks/core/pipeline_ng/test/mock/mock_pipeline_base.cpp @@ -409,6 +409,10 @@ bool PipelineBase::Animate(const AnimationOption& option, const RefPtr& c } void PipelineBase::Destroy() {} +bool PipelineBase::MaybeRelease() +{ + return AceType::MaybeRelease(); +} void PipelineBase::AddEtsCardTouchEventCallback(int32_t ponitId, EtsCardTouchEventCallback&& callback) {} diff --git a/test/mock/core/common/mock_container.cpp b/test/mock/core/common/mock_container.cpp index 608e9f6163a..0950c910985 100755 --- a/test/mock/core/common/mock_container.cpp +++ b/test/mock/core/common/mock_container.cpp @@ -23,6 +23,10 @@ int32_t g_id = 0; } // namespace RefPtr MockContainer::container_; +bool Frontend::MaybeRelease() +{ + return AceType::MaybeRelease(); +} int32_t Container::CurrentId() { diff --git a/test/mock/core/common/mock_frontend.h b/test/mock/core/common/mock_frontend.h index 6ed4c55e24e..8d54307c47c 100644 --- a/test/mock/core/common/mock_frontend.h +++ b/test/mock/core/common/mock_frontend.h @@ -40,7 +40,6 @@ public: MOCK_METHOD(void, UpdateState, (Frontend::State state), (override)); MOCK_METHOD(RefPtr, GetAccessibilityManager, (), (const override)); MOCK_METHOD(bool, IsForeground, (), (override)); - MOCK_METHOD(FrontendType, GetType, (), (override)); MOCK_METHOD(RefPtr, GetEventHandler, (), (override)); MOCK_METHOD(void, RebuildAllPages, (), (override)); MOCK_METHOD(void, SetColorMode, (ColorMode colorMode), (override));