From bc825de1fe5c5323358242cdf9ea57bee4b8139e Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Mon, 25 Jan 2021 00:10:32 +0100 Subject: [PATCH] Simplify and make more robust wxWebViewEdge initialization Get rid of ms_isInitialized as it must be kept synchronized with ms_loaderDll.IsLoaded() anyhow, and it's simpler to not have it at all rather than ensuring this. Also ensure that calling Initialize() again, after doing it first unsuccessfully, doesn't assert because ms_loaderDll is already loaded, by only leaving it with a valid handle if the initialization succeeded. Closes #19041. --- include/wx/msw/private/webview_edge.h | 1 - src/msw/webview_edge.cpp | 28 ++++++++++++--------------- 2 files changed, 12 insertions(+), 17 deletions(-) diff --git a/include/wx/msw/private/webview_edge.h b/include/wx/msw/private/webview_edge.h index 765127e9ed..3fb204309c 100644 --- a/include/wx/msw/private/webview_edge.h +++ b/include/wx/msw/private/webview_edge.h @@ -65,7 +65,6 @@ public: ICoreWebView2Settings* GetSettings(); - static bool ms_isInitialized; static wxDynamicLibrary ms_loaderDll; static bool Initialize(); diff --git a/src/msw/webview_edge.cpp b/src/msw/webview_edge.cpp index 13e5af4bb6..74694fa8d9 100644 --- a/src/msw/webview_edge.cpp +++ b/src/msw/webview_edge.cpp @@ -51,7 +51,6 @@ typedef HRESULT(__stdcall *GetAvailableCoreWebView2BrowserVersionString_t)( CreateCoreWebView2EnvironmentWithOptions_t wxCreateCoreWebView2EnvironmentWithOptions = NULL; GetAvailableCoreWebView2BrowserVersionString_t wxGetAvailableCoreWebView2BrowserVersionString = NULL; -bool wxWebViewEdgeImpl::ms_isInitialized = false; wxDynamicLibrary wxWebViewEdgeImpl::ms_loaderDll; wxWebViewEdgeImpl::wxWebViewEdgeImpl(wxWebViewEdge* webview): @@ -113,39 +112,36 @@ HRESULT wxWebViewEdgeImpl::OnEnvironmentCreated( bool wxWebViewEdgeImpl::Initialize() { - if (ms_isInitialized) + if (ms_loaderDll.IsLoaded()) return true; - if (!ms_loaderDll.Load("WebView2Loader.dll", wxDL_DEFAULT | wxDL_QUIET)) + wxDynamicLibrary loaderDll; + if (!loaderDll.Load("WebView2Loader.dll", wxDL_DEFAULT | wxDL_QUIET)) return false; // Try to load functions from loader DLL - wxDL_INIT_FUNC(wx, CreateCoreWebView2EnvironmentWithOptions, ms_loaderDll); - wxDL_INIT_FUNC(wx, GetAvailableCoreWebView2BrowserVersionString, ms_loaderDll); + wxDL_INIT_FUNC(wx, CreateCoreWebView2EnvironmentWithOptions, loaderDll); + wxDL_INIT_FUNC(wx, GetAvailableCoreWebView2BrowserVersionString, loaderDll); if (!wxGetAvailableCoreWebView2BrowserVersionString || !wxCreateCoreWebView2EnvironmentWithOptions) return false; // Check if a Edge browser can be found by the loader DLL wxCoTaskMemPtr versionStr; HRESULT hr = wxGetAvailableCoreWebView2BrowserVersionString(NULL, &versionStr); - if (SUCCEEDED(hr) && versionStr) + if (FAILED(hr) || !versionStr) { - ms_isInitialized = true; - return true; - } - else wxLogApiError("GetCoreWebView2BrowserVersionInfo", hr); + return false; + } - return false; + ms_loaderDll.Attach(loaderDll.Detach()); + + return true; } void wxWebViewEdgeImpl::Uninitialize() { - if (ms_isInitialized) - { - ms_loaderDll.Unload(); - ms_isInitialized = false; - } + ms_loaderDll.Unload(); } void wxWebViewEdgeImpl::UpdateBounds()