diff --git a/ui/ozone/platform/headless/headless_window.cc b/ui/ozone/platform/headless/headless_window.cc index d11ccc52615c9..12b6a0c05b0a2 100644 --- a/ui/ozone/platform/headless/headless_window.cc +++ b/ui/ozone/platform/headless/headless_window.cc @@ -78,18 +78,26 @@ void HeadlessWindow::SetFullscreen(bool fullscreen, int64_t target_display_id) { return; } + auto weak_this = weak_ptr_factory_.GetWeakPtr(); if (fullscreen) { if (window_state_ != PlatformWindowState::kMaximized && window_state_ != PlatformWindowState::kFullScreen) { restored_bounds_ = bounds_; } ZoomWindowBounds(); + // Dispatching OnBoundsChanged() may have synchronously destroyed |this|. + if (!weak_this) { + return; + } UpdateWindowState(PlatformWindowState::kFullScreen); } else { if (window_state_ != PlatformWindowState::kFullScreen) { return; } RestoreWindowBounds(); + if (!weak_this) { + return; + } UpdateWindowState(PlatformWindowState::kNormal); } } @@ -102,20 +110,33 @@ void HeadlessWindow::Maximize() { if (window_state_ != PlatformWindowState::kMaximized && window_state_ != PlatformWindowState::kFullScreen) { restored_bounds_ = bounds_; + auto weak_this = weak_ptr_factory_.GetWeakPtr(); ZoomWindowBounds(); + if (!weak_this) { + return; + } UpdateWindowState(PlatformWindowState::kMaximized); } } void HeadlessWindow::Minimize() { if (window_state_ != PlatformWindowState::kMinimized) { + auto weak_this = weak_ptr_factory_.GetWeakPtr(); // Minimized window retains its size and position, however, it's made // hidden by Aura. if (window_state_ == PlatformWindowState::kMaximized || window_state_ == PlatformWindowState::kFullScreen) { RestoreWindowBounds(); + if (!weak_this) { + return; + } } UpdateWindowState(PlatformWindowState::kMinimized); + // Dispatching OnWindowStateChanged() may have synchronously destroyed + // |this|. + if (!weak_this) { + return; + } // Minimized windows are inactive. Aura activates minimized windows // when restoring. If we don't deactivate the window here, the subsequent // activation will be optimized away, causing https://crbug.com/358998544. @@ -125,7 +146,11 @@ void HeadlessWindow::Minimize() { void HeadlessWindow::Restore() { if (window_state_ != PlatformWindowState::kNormal) { + auto weak_this = weak_ptr_factory_.GetWeakPtr(); RestoreWindowBounds(); + if (!weak_this) { + return; + } UpdateWindowState(PlatformWindowState::kNormal); } } diff --git a/ui/ozone/platform/headless/headless_window.h b/ui/ozone/platform/headless/headless_window.h index 646e398135bfd..16d63d3b3bf41 100644 --- a/ui/ozone/platform/headless/headless_window.h +++ b/ui/ozone/platform/headless/headless_window.h @@ -9,6 +9,7 @@ #include "base/memory/raw_ptr.h" #include "base/memory/scoped_refptr.h" +#include "base/memory/weak_ptr.h" #include "ui/gfx/geometry/point.h" #include "ui/gfx/geometry/rect.h" #include "ui/gfx/image/image_skia.h" @@ -90,6 +91,8 @@ class HeadlessWindow : public PlatformWindow { std::optional restored_bounds_; PlatformWindowState window_state_ = PlatformWindowState::kUnknown; ActivationState activation_state_ = ActivationState::kUnknown; + + base::WeakPtrFactory weak_ptr_factory_{this}; }; } // namespace ui