diff --git a/ui/ozone/platform/wayland/host/xdg_surface_wrapper_impl.cc b/ui/ozone/platform/wayland/host/xdg_surface_wrapper_impl.cc index 3116311fb6f97e..a82227dfe52d86 100644 --- a/ui/ozone/platform/wayland/host/xdg_surface_wrapper_impl.cc +++ b/ui/ozone/platform/wayland/host/xdg_surface_wrapper_impl.cc @@ -70,8 +70,20 @@ void XDGSurfaceWrapperImpl::Configure(void* data, auto* surface = static_cast(data); DCHECK(surface); - surface->wayland_window_->HandleSurfaceConfigure(serial); - surface->wayland_window_->OnSurfaceConfigureEvent(); + surface->OnConfigure(serial); +} + +void XDGSurfaceWrapperImpl::OnConfigure(uint32_t serial) { + // Calls to HandleSurfaceConfigure() might end up hiding the enclosing + // toplevel window, and deleting this object. + auto alive = weak_ptr_factory_.GetWeakPtr(); + + wayland_window_->HandleSurfaceConfigure(serial); + + if (!alive) + return; + + wayland_window_->OnSurfaceConfigureEvent(); } xdg_surface* XDGSurfaceWrapperImpl::xdg_surface() const { diff --git a/ui/ozone/platform/wayland/host/xdg_surface_wrapper_impl.h b/ui/ozone/platform/wayland/host/xdg_surface_wrapper_impl.h index 2571aac25d5422..d90d2fd032f3aa 100644 --- a/ui/ozone/platform/wayland/host/xdg_surface_wrapper_impl.h +++ b/ui/ozone/platform/wayland/host/xdg_surface_wrapper_impl.h @@ -9,6 +9,7 @@ #include +#include "base/memory/weak_ptr.h" #include "ui/ozone/platform/wayland/common/wayland_object.h" namespace gfx { @@ -35,14 +36,16 @@ class XDGSurfaceWrapperImpl : public ShellSurfaceWrapper { bool IsConfigured() override; void SetWindowGeometry(const gfx::Rect& bounds) override; + struct xdg_surface* xdg_surface() const; + + private: // xdg_surface_listener static void Configure(void* data, struct xdg_surface* xdg_surface, uint32_t serial); - struct xdg_surface* xdg_surface() const; + void OnConfigure(uint32_t serial); - private: // Non-owing WaylandWindow that uses this surface wrapper. WaylandWindow* const wayland_window_; WaylandConnection* const connection_; @@ -50,6 +53,8 @@ class XDGSurfaceWrapperImpl : public ShellSurfaceWrapper { bool is_configured_ = false; wl::Object xdg_surface_; + + base::WeakPtrFactory weak_ptr_factory_{this}; }; } // namespace ui