diff --git a/ui/touch_selection/touch_selection_controller.cc b/ui/touch_selection/touch_selection_controller.cc index 84427cbef95ff..b595a23bdc0a5 100644 --- a/ui/touch_selection/touch_selection_controller.cc +++ b/ui/touch_selection/touch_selection_controller.cc @@ -123,26 +123,26 @@ void TouchSelectionController::OnSelectionBoundsChanged( // Ensure that |response_pending_input_event_| is cleared after the method // completes, while also making its current value available for the duration - // of the call. - InputEventType causal_input_event = response_pending_input_event_; - response_pending_input_event_ = InputEventType::kNone; - base::AutoReset auto_reset_response_pending_input_event( - &response_pending_input_event_, causal_input_event); + // of the call. This cannot use base::AutoReset: the client callouts below may + // synchronously destroy |this| (e.g. an Android embedder closing the + // WebContents from inside OnSelectionEvent), and AutoReset::scoped_variable_ + // is RAW_PTR_EXCLUSION, so its destructor would write into freed memory. + base::WeakPtr self = weak_factory_.GetWeakPtr(); if ((start_orientation_ == TouchHandleOrientation::LEFT || start_orientation_ == TouchHandleOrientation::RIGHT) && (end_orientation_ == TouchHandleOrientation::RIGHT || end_orientation_ == TouchHandleOrientation::LEFT)) { OnSelectionChanged(); - return; - } - - if (start_orientation_ == TouchHandleOrientation::CENTER) { + } else if (start_orientation_ == TouchHandleOrientation::CENTER) { OnInsertionChanged(); - return; + } else { + HideHandles(); } - HideHandles(); + if (self) { + response_pending_input_event_ = InputEventType::kNone; + } } void TouchSelectionController::OnViewportChanged( diff --git a/ui/touch_selection/touch_selection_controller.h b/ui/touch_selection/touch_selection_controller.h index f2af9cd165946..ba65fabadfc9e 100644 --- a/ui/touch_selection/touch_selection_controller.h +++ b/ui/touch_selection/touch_selection_controller.h @@ -6,6 +6,7 @@ #define UI_TOUCH_SELECTION_TOUCH_SELECTION_CONTROLLER_H_ #include "base/memory/raw_ptr.h" +#include "base/memory/weak_ptr.h" #include "base/time/time.h" #include "ui/events/types/event_type.h" #include "ui/gfx/geometry/point.h" @@ -297,6 +298,8 @@ class UI_TOUCH_SELECTION_EXPORT TouchSelectionController // Whether a swipe-to-move-cursor gesture is activated. bool swipe_to_move_cursor_activated_ = false; + + base::WeakPtrFactory weak_factory_{this}; }; } // namespace ui