Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix occasional WebView2 Crash when restoring minimized app (0x80070057) #6667

Merged
merged 3 commits into from
Feb 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
krschau marked this conversation as resolved.
Show resolved Hide resolved
{
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