diff --git a/shell/browser/api/electron_api_base_window.cc b/shell/browser/api/electron_api_base_window.cc index e2459d9e947f..27f4279f4cae 100644 --- a/shell/browser/api/electron_api_base_window.cc +++ b/shell/browser/api/electron_api_base_window.cc @@ -362,7 +362,8 @@ void BaseWindow::SetContentView(gin::Handle view) { } void BaseWindow::CloseImmediately() { - window_->CloseImmediately(); + if (!window_->IsClosed()) + window_->CloseImmediately(); } void BaseWindow::Close() { diff --git a/shell/browser/native_window.cc b/shell/browser/native_window.cc index 1f9c4a59f1f8..dfbbbd0770ee 100644 --- a/shell/browser/native_window.cc +++ b/shell/browser/native_window.cc @@ -285,6 +285,10 @@ void NativeWindow::SetShape(const std::vector& rects) { widget()->SetShape(std::make_unique>(rects)); } +bool NativeWindow::IsClosed() const { + return is_closed_; +} + void NativeWindow::SetSize(const gfx::Size& size, bool animate) { SetBounds(gfx::Rect(GetPosition(), size), animate); } @@ -523,23 +527,8 @@ void NativeWindow::NotifyWindowCloseButtonClicked() { CloseImmediately(); } -void NativeWindow::Close() { - if (!IsClosable()) { - WindowList::WindowCloseCancelled(this); - return; - } - - if (!is_closed()) - CloseImpl(); -} - -void NativeWindow::CloseImmediately() { - if (!is_closed()) - CloseImmediatelyImpl(); -} - void NativeWindow::NotifyWindowClosed() { - if (is_closed()) + if (is_closed_) return; is_closed_ = true; diff --git a/shell/browser/native_window.h b/shell/browser/native_window.h index 4028377c2e4b..53ef74c3ac5b 100644 --- a/shell/browser/native_window.h +++ b/shell/browser/native_window.h @@ -81,10 +81,9 @@ class NativeWindow : public base::SupportsUserData, virtual void SetContentView(views::View* view) = 0; - // wrapper around CloseImpl that checks that window_ can be closed - void Close(); - // wrapper around CloseImmediatelyImpl that checks that window_ can be closed - void CloseImmediately(); + virtual void Close() = 0; + virtual void CloseImmediately() = 0; + virtual bool IsClosed() const; virtual void Focus(bool focus) = 0; virtual bool IsFocused() const = 0; virtual void Show() = 0; @@ -427,6 +426,8 @@ class NativeWindow : public base::SupportsUserData, void UpdateBackgroundThrottlingState(); protected: + friend class api::BrowserView; + NativeWindow(const gin_helper::Dictionary& options, NativeWindow* parent); void set_titlebar_overlay_height(int height) { @@ -457,9 +458,6 @@ class NativeWindow : public base::SupportsUserData, void set_content_view(views::View* view) { content_view_ = view; } - virtual void CloseImpl() = 0; - virtual void CloseImmediatelyImpl() = 0; - static inline constexpr base::cstring_view kNativeWindowKey = "__ELECTRON_NATIVE_WINDOW__"; diff --git a/shell/browser/native_window_mac.h b/shell/browser/native_window_mac.h index 1d54844b94dc..a31fb510d32c 100644 --- a/shell/browser/native_window_mac.h +++ b/shell/browser/native_window_mac.h @@ -39,8 +39,8 @@ class NativeWindowMac : public NativeWindow, // NativeWindow: void OnTitleChanged() override; void SetContentView(views::View* view) override; - void CloseImpl() override; - void CloseImmediatelyImpl() override; + void Close() override; + void CloseImmediately() override; void Focus(bool focus) override; bool IsFocused() const override; void Show() override; diff --git a/shell/browser/native_window_mac.mm b/shell/browser/native_window_mac.mm index c3d2d835f06e..8323aa566bcd 100644 --- a/shell/browser/native_window_mac.mm +++ b/shell/browser/native_window_mac.mm @@ -33,6 +33,7 @@ #include "shell/browser/ui/cocoa/root_view_mac.h" #include "shell/browser/ui/cocoa/window_buttons_proxy.h" #include "shell/browser/ui/drag_util.h" +#include "shell/browser/window_list.h" #include "shell/common/gin_converters/gfx_converter.h" #include "shell/common/gin_helper/dictionary.h" #include "shell/common/node_util.h" @@ -336,7 +337,12 @@ void NativeWindowMac::SetContentView(views::View* view) { root_view->DeprecatedLayoutImmediately(); } -void NativeWindowMac::CloseImpl() { +void NativeWindowMac::Close() { + if (!IsClosable()) { + WindowList::WindowCloseCancelled(this); + return; + } + if (is_transitioning_fullscreen()) { SetHasDeferredWindowClose(true); return; @@ -372,7 +378,7 @@ void NativeWindowMac::CloseImpl() { } } -void NativeWindowMac::CloseImmediatelyImpl() { +void NativeWindowMac::CloseImmediately() { // Ensure we're detached from the parent window before closing. RemoveChildFromParentWindow(); @@ -1686,7 +1692,7 @@ bool NativeWindowMac::IsActive() const { } void NativeWindowMac::Cleanup() { - DCHECK(!is_closed()); + DCHECK(!IsClosed()); ui::NativeTheme::GetInstanceForNativeUi()->RemoveObserver(this); display::Screen::GetScreen()->RemoveObserver(this); [window_ cleanup]; diff --git a/shell/browser/native_window_views.cc b/shell/browser/native_window_views.cc index 51203d20c07b..a802eb8ad631 100644 --- a/shell/browser/native_window_views.cc +++ b/shell/browser/native_window_views.cc @@ -31,6 +31,7 @@ #include "shell/browser/ui/views/root_view.h" #include "shell/browser/web_contents_preferences.h" #include "shell/browser/web_view_manager.h" +#include "shell/browser/window_list.h" #include "shell/common/electron_constants.h" #include "shell/common/gin_converters/image_converter.h" #include "shell/common/gin_helper/arguments.h" @@ -532,11 +533,16 @@ void NativeWindowViews::SetContentView(views::View* view) { root_view_.GetMainView()->DeprecatedLayoutImmediately(); } -void NativeWindowViews::CloseImpl() { +void NativeWindowViews::Close() { + if (!IsClosable()) { + WindowList::WindowCloseCancelled(this); + return; + } + widget()->Close(); } -void NativeWindowViews::CloseImmediatelyImpl() { +void NativeWindowViews::CloseImmediately() { widget()->CloseNow(); } diff --git a/shell/browser/native_window_views.h b/shell/browser/native_window_views.h index b101c8bb3232..574b6ff68dee 100644 --- a/shell/browser/native_window_views.h +++ b/shell/browser/native_window_views.h @@ -53,8 +53,8 @@ class NativeWindowViews : public NativeWindow, // NativeWindow: void SetContentView(views::View* view) override; - void CloseImpl() override; - void CloseImmediatelyImpl() override; + void Close() override; + void CloseImmediately() override; void Focus(bool focus) override; bool IsFocused() const override; void Show() override; diff --git a/shell/browser/window_list.cc b/shell/browser/window_list.cc index bd9cfbfe53e2..188402d0e6bb 100644 --- a/shell/browser/window_list.cc +++ b/shell/browser/window_list.cc @@ -84,7 +84,7 @@ void WindowList::CloseAllWindows() { std::ranges::reverse(weak_windows); #endif for (const auto& window : weak_windows) { - if (window) + if (window && !window->IsClosed()) window->Close(); } } @@ -95,7 +95,7 @@ void WindowList::DestroyAllWindows() { ConvertToWeakPtrVector(GetInstance()->windows_); for (const auto& window : weak_windows) { - if (window) + if (window && !window->IsClosed()) window->CloseImmediately(); } }