diff --git a/third_party/blink/renderer/platform/widget/widget_base.cc b/third_party/blink/renderer/platform/widget/widget_base.cc index cf9999d58b386..4f0013ae2a1fe 100644 --- a/third_party/blink/renderer/platform/widget/widget_base.cc +++ b/third_party/blink/renderer/platform/widget/widget_base.cc @@ -1518,9 +1518,15 @@ void WidgetBase::ImeSetComposition( } ImeEventGuard guard(weak_ptr_factory_.GetWeakPtr()); + // SetComposition() dispatches JS events and can run a nested message loop + // (e.g. via window.print()) during which |this| may be destroyed. + base::WeakPtr weak_this = weak_ptr_factory_.GetWeakPtr(); if (!frame_widget->SetComposition(text, ime_text_spans, replacement_range, selection_start, selection_end, ime_state)) { + if (!weak_this) { + return; + } // If we failed to set the composition text, then we need to let the browser // process to cancel the input method's ongoing composition session, to make // sure we are in a consistent state. @@ -1529,6 +1535,9 @@ void WidgetBase::ImeSetComposition( host->ImeCancelComposition(); } } + if (!weak_this) { + return; + } UpdateCompositionInfo(false /* not an immediate request */); } @@ -1549,9 +1558,15 @@ void WidgetBase::ImeCommitText(const String& text, } ImeEventGuard guard(weak_ptr_factory_.GetWeakPtr()); + // CommitText() dispatches JS events and can run a nested message loop during + // which |this| may be destroyed. + base::WeakPtr weak_this = weak_ptr_factory_.GetWeakPtr(); input_handler_.set_handling_input_event(true); frame_widget->CommitText(text, ime_text_spans, replacement_range, relative_cursor_pos); + if (!weak_this) { + return; + } input_handler_.set_handling_input_event(false); UpdateCompositionInfo(false /* not an immediate request */); } @@ -1569,8 +1584,14 @@ void WidgetBase::ImeFinishComposingText(bool keep_selection) { } ImeEventGuard guard(weak_ptr_factory_.GetWeakPtr()); + // FinishComposingText() dispatches JS events and can run a nested message + // loop during which |this| may be destroyed. + base::WeakPtr weak_this = weak_ptr_factory_.GetWeakPtr(); input_handler_.set_handling_input_event(true); frame_widget->FinishComposingText(keep_selection); + if (!weak_this) { + return; + } input_handler_.set_handling_input_event(false); UpdateCompositionInfo(false /* not an immediate request */); }