diff --git a/content/browser/web_contents/web_contents_impl.cc b/content/browser/web_contents/web_contents_impl.cc index 34aec058693edaf409adb47ae3a3a2b8024f6ca2..1dca3ecc6f512045bef482f72c8028f1aef5c84c 100644 --- a/content/browser/web_contents/web_contents_impl.cc +++ b/content/browser/web_contents/web_contents_impl.cc @@ -8728,6 +8728,9 @@ void WebContentsImpl::BeforeUnloadFiredFromRenderManager( observers_.NotifyObservers(&WebContentsObserver::BeforeUnloadFired, proceed); if (delegate_) delegate_->BeforeUnloadFired(this, proceed, proceed_to_fire_unload); +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + OnBeforeUnloadFired(*proceed_to_fire_unload); +#endif // OHOS_DISPATCH_BEFORE_UNLOAD // Note: |this| might be deleted at this point. } @@ -10651,4 +10654,12 @@ const std::string& WebContentsImpl::SharedRenderProcessToken() { } #endif +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +void WebContentsImpl::OnBeforeUnloadFired(bool proceed) { + if (delegate_) { + delegate_->OnBeforeUnloadFired(proceed); + } +} +#endif // OHOS_DISPATCH_BEFORE_UNLOAD + } // namespace content diff --git a/content/browser/web_contents/web_contents_impl.h b/content/browser/web_contents/web_contents_impl.h index 21a08ccf60a3fd8c8477394a9bb8e8e48a05c97e..c8fb45a474b119cc828dceaefdfab44f7ebd6282 100644 --- a/content/browser/web_contents/web_contents_impl.h +++ b/content/browser/web_contents/web_contents_impl.h @@ -1572,6 +1572,10 @@ class CONTENT_EXPORT WebContentsImpl : public WebContents, const std::string& SharedRenderProcessToken() override; #endif +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + void OnBeforeUnloadFired(bool proceed) override; +#endif // OHOS_DISPATCH_BEFORE_UNLOAD + private: using FrameTreeIterationCallback = base::RepeatingCallback; using RenderViewHostIterationCallback = diff --git a/content/public/browser/web_contents.h b/content/public/browser/web_contents.h index f19018edf32cf7ffe1e4b8aa5cda75acbeb22633..c5e0de3ed491f45c1e3a4d9b3da426d8211060a3 100644 --- a/content/public/browser/web_contents.h +++ b/content/public/browser/web_contents.h @@ -1553,6 +1553,9 @@ class WebContents : public PageNavigator, #ifdef OHOS_DRAG_DROP virtual void ClearContextMenu() = 0; #endif //OHOS_DRAG_DROP +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + virtual void OnBeforeUnloadFired(bool proceed) = 0; +#endif // OHOS_DISPATCH_BEFORE_UNLOAD private: // This interface should only be implemented inside content. friend class WebContentsImpl; diff --git a/content/public/browser/web_contents_delegate.h b/content/public/browser/web_contents_delegate.h index 7748270befbd1289081593cb38b9926ce24453ea..72abcda03c605603a3e8a0e8b5e0e48e0d38356b 100644 --- a/content/public/browser/web_contents_delegate.h +++ b/content/public/browser/web_contents_delegate.h @@ -844,6 +844,9 @@ class CONTENT_EXPORT WebContentsDelegate { virtual void OnShowVideoAssistant(const std::string& videoAssistantItems); virtual void OnReportStatisticLog(const std::string& content); #endif // defined(OHOS_VIDEO_ASSISTANT) +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + virtual void OnBeforeUnloadFired(bool proceed) {} +#endif // OHOS_DISPATCH_BEFORE_UNLOAD protected: virtual ~WebContentsDelegate(); diff --git a/ohos_build/build/config/ohos.json b/ohos_build/build/config/ohos.json index 7ec779d0adfebb63b6f967a79ec9165c504851eb..62491f275d64a10f55c6a9c7081be88d0428c015 100644 --- a/ohos_build/build/config/ohos.json +++ b/ohos_build/build/config/ohos.json @@ -250,6 +250,15 @@ "dependence": "", "default": "true" }, +{ + "name": "OHOS_DISPATCH_BEFORE_UNLOAD", + "owner": "", + "desc": "", + "effect":"main gn blink_core other", + "genCommandline": "default", + "dependence": "", + "default": "true" +}, { "name": "OHOS_DISPLAY_CUTOUT", "owner": "", diff --git a/ohos_nweb/src/capi/nweb_app_client_extension_callback.h b/ohos_nweb/src/capi/nweb_app_client_extension_callback.h index 6e6dba64f2fab5fa146a2e9f9c2ae9835f84cba4..f00616723b8e2a5f0ec7ad093fe443ea2ab4460f 100644 --- a/ohos_nweb/src/capi/nweb_app_client_extension_callback.h +++ b/ohos_nweb/src/capi/nweb_app_client_extension_callback.h @@ -70,6 +70,9 @@ struct NWebAppClientExtensionCallback { void (*OnShowVideoAssistant)(int32_t nweb_id, const char* video_assistant_items); +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + void (*OnBeforeUnloadFired)(bool proceed, int nweb_id); +#endif // OHOS_DISPATCH_BEFORE_UNLOAD }; #endif // OHOS_NWEB_SRC_NWEB_APP_CLIENT_EXTENSION_CALLBACK_H_ diff --git a/ohos_nweb/src/cef_delegate/nweb_delegate.cc b/ohos_nweb/src/cef_delegate/nweb_delegate.cc index ef26c164df069c93517c27b5e0b9dcb3cbccd6fa..e2b7adc676bc8046ed3c4970d4c119125b1bb9c8 100644 --- a/ohos_nweb/src/cef_delegate/nweb_delegate.cc +++ b/ohos_nweb/src/cef_delegate/nweb_delegate.cc @@ -4374,5 +4374,20 @@ void NWebDelegate::CloseDevtools() { } GetBrowser()->GetHost()->CloseDevTools(); } +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +bool NWebDelegate::NeedToFireBeforeUnloadOrUnloadEvents() { + if (GetBrowser().get()) { + return GetBrowser()->NeedToFireBeforeUnloadOrUnloadEvents(); + } + return false; +} + +void NWebDelegate::DispatchBeforeUnload() { + LOG(INFO) << "NWebDelegate::DispatchBeforeUnload"; + if (GetBrowser().get()) { + GetBrowser()->DispatchBeforeUnload(); + } +} +#endif // OHOS_DISPATCH_BEFORE_UNLOAD } // namespace OHOS::NWeb diff --git a/ohos_nweb/src/cef_delegate/nweb_delegate.h b/ohos_nweb/src/cef_delegate/nweb_delegate.h index 55b161c4de6df56591c910077f4e63c667fa1cf2..a8a4fcd39b399210e682ed09505a5bb75dca71b6 100644 --- a/ohos_nweb/src/cef_delegate/nweb_delegate.h +++ b/ohos_nweb/src/cef_delegate/nweb_delegate.h @@ -561,6 +561,11 @@ void NotifyForNextTouchEvent() override; std::unique_ptr param) override; void CloseDevtools() override; +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + bool NeedToFireBeforeUnloadOrUnloadEvents() override; + void DispatchBeforeUnload() override; +#endif // OHOS_DISPATCH_BEFORE_UNLOAD + public: int argc_; const char** argv_; diff --git a/ohos_nweb/src/cef_delegate/nweb_handler_delegate.cc b/ohos_nweb/src/cef_delegate/nweb_handler_delegate.cc index 90a145b3820469fbfb6e2692bbaf92b360c8e68f..bd499043b01d560b2d18dd73f180be0693296eec 100644 --- a/ohos_nweb/src/cef_delegate/nweb_handler_delegate.cc +++ b/ohos_nweb/src/cef_delegate/nweb_handler_delegate.cc @@ -3911,5 +3911,15 @@ void NWebHandlerDelegate::OnRequestOpenDevTools() { web_app_client_extension_listener_->OnRequestOpenDevTools( web_app_client_extension_listener_->nweb_id); } +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +void NWebHandlerDelegate::OnBeforeUnloadFired(CefRefPtr browser, + bool proceed) { + if (web_app_client_extension_listener_ != nullptr && + web_app_client_extension_listener_->OnBeforeUnloadFired != nullptr) { + web_app_client_extension_listener_->OnBeforeUnloadFired( + proceed, web_app_client_extension_listener_->nweb_id); + } +} +#endif // OHOS_DISPATCH_BEFORE_UNLOAD } // namespace OHOS::NWeb diff --git a/ohos_nweb/src/cef_delegate/nweb_handler_delegate.h b/ohos_nweb/src/cef_delegate/nweb_handler_delegate.h index b31b9b0945b85372d5c15b808f7696599430144a..5206f4704b91bb3bb7d216a3b69fcae35422d647 100644 --- a/ohos_nweb/src/cef_delegate/nweb_handler_delegate.h +++ b/ohos_nweb/src/cef_delegate/nweb_handler_delegate.h @@ -781,6 +781,10 @@ class NWebHandlerDelegate : public CefClient, void OnRequestOpenDevTools(); +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + void OnBeforeUnloadFired(CefRefPtr browser, + bool proceed) override; +#endif // OHOS_DISPATCH_BEFORE_UNLOAD private: void CopyImageToClipboard(CefRefPtr image); // List of existing browser windows. Only accessed on the CEF UI thread. diff --git a/ohos_nweb/src/nweb_delegate_interface.h b/ohos_nweb/src/nweb_delegate_interface.h index e32322137399415000637b51913614581b39c105..fe480634595951a7a70168814d6f02c179be0f61 100644 --- a/ohos_nweb/src/nweb_delegate_interface.h +++ b/ohos_nweb/src/nweb_delegate_interface.h @@ -573,6 +573,10 @@ class NWebDelegateInterface std::shared_ptr nweb_delegate, std::unique_ptr param) = 0; virtual void CloseDevtools() = 0; +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + virtual bool NeedToFireBeforeUnloadOrUnloadEvents() = 0; + virtual void DispatchBeforeUnload() = 0; +#endif // OHOS_DISPATCH_BEFORE_UNLOAD }; } // namespace OHOS::NWeb diff --git a/ohos_nweb/src/nweb_impl.cc b/ohos_nweb/src/nweb_impl.cc index c26d0cbfd0c645442e960f6709252926ab13636a..4d85a3ecaeee43ad34411b5fdd3f1ba0d7b253c0 100644 --- a/ohos_nweb/src/nweb_impl.cc +++ b/ohos_nweb/src/nweb_impl.cc @@ -3772,3 +3772,19 @@ int NWebImpl::ScaleGestureChangeV2(int type, } return nweb_delegate_->ScaleGestureChangeV2(type, scale, originScale, centerX, centerY); } +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) +bool NWebImpl::NeedToFireBeforeUnloadOrUnloadEvents() { + if (nweb_delegate_ == nullptr) { + return false; + } + return nweb_delegate_->NeedToFireBeforeUnloadOrUnloadEvents(); +} + +void NWebImpl::DispatchBeforeUnload() { + if (nweb_delegate_ == nullptr) { + return; + } + nweb_delegate_->DispatchBeforeUnload(); +} + +#endif // OHOS_DISPATCH_BEFORE_UNLOAD diff --git a/ohos_nweb/src/nweb_impl.h b/ohos_nweb/src/nweb_impl.h index 31e09544dd3657c59595c441375dfef1fdaf0c0a..d06b40d0109cda9c382b60a1e0cf009f33977971 100644 --- a/ohos_nweb/src/nweb_impl.h +++ b/ohos_nweb/src/nweb_impl.h @@ -599,6 +599,10 @@ class NWebImpl : public NWeb { void OnConfigurationUpdated( std::shared_ptr configuration) override; #endif +#if defined(OHOS_DISPATCH_BEFORE_UNLOAD) + bool NeedToFireBeforeUnloadOrUnloadEvents(); + void DispatchBeforeUnload(); +#endif // OHOS_DISPATCH_BEFORE_UNLOAD private: void ProcessInitArgs(std::shared_ptr init_args);