Skip to content

Commit

Permalink
Fix occasional WebView2 Crash when restoring minimized app (0x8007005…
Browse files Browse the repository at this point in the history
…7) (#6667)

* Basic change

* Add comments / clean up

* CR Feedback

Co-authored-by: Dmitriy Komin <[email protected]>
  • Loading branch information
DmitriyKomin and Dmitriy Komin authored Feb 3, 2022
1 parent 73abf14 commit f2797be
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
24 changes: 19 additions & 5 deletions dev/WebView2/WebView2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1255,14 +1255,28 @@ void WebView2::FireCoreWebView2Initialized(winrt::hresult exception)
m_coreWebView2InitializedEventSource(*this, *eventArgs);
}

void WebView2::HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::RoutedEventArgs&) noexcept
void WebView2::HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::RoutedEventArgs&)
{
if (m_coreWebView && m_xamlFocusChangeInfo.m_isPending)
{
// In current CoreWebView2 API, MoveFocus is not expected to fail, so set m_webHasFocus here rather than
// in asynchronous (MOJO/cross-proc) CoreWebView2 GotFocus event.
m_webHasFocus = true;
m_coreWebViewController.MoveFocus(m_xamlFocusChangeInfo.m_storedMoveFocusReason);
try
{
m_coreWebViewController.MoveFocus(m_xamlFocusChangeInfo.m_storedMoveFocusReason);
m_webHasFocus = true;
}
catch (winrt::hresult_error e)
{
// Occasionally, a request to restore the minimized window does not complete. This triggers
// FocusManager to set Xaml Focus to WV2 and consequently into CWV2 MoveFocus() call above,
// which in turn will attempt ::SetFocus() on InputHWND, and that will fail with E_INVALIDARG
// since that HWND remains minimized. Work around by ignoring this error here. Since the app
// is minimized, focus state is not relevant - the next (successful) attempt to restrore the app
// will set focus into WV2/CWV2 correctly.
if (e.code().value != E_INVALIDARG)
{
throw;
}
}
m_xamlFocusChangeInfo.m_isPending = false;
}
}
Expand Down
2 changes: 1 addition & 1 deletion dev/WebView2/WebView2.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class WebView2 :
void HandlePointerCaptureLost(const winrt::Windows::Foundation::IInspectable&, const winrt::PointerRoutedEventArgs& args);
void HandleKeyDown(const winrt::Windows::Foundation::IInspectable&, const winrt::KeyRoutedEventArgs& e);
void HandleGettingFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::GettingFocusEventArgs& args) noexcept;
void HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::RoutedEventArgs&) noexcept;
void HandleGotFocus(const winrt::Windows::Foundation::IInspectable&, const winrt::RoutedEventArgs&);
void HandleAcceleratorKeyActivated(const winrt::Windows::UI::Core::CoreDispatcher&, const winrt::AcceleratorKeyEventArgs& args) noexcept;
void HandleXamlRootChanged();
void HandleSizeChanged(const winrt::IInspectable& /*sender*/, const winrt::SizeChangedEventArgs& args);
Expand Down

0 comments on commit f2797be

Please sign in to comment.