Merge branch 'webview-edge-unload'

Simplify and fix wxWebViewEdge initialization.

See https://github.com/wxWidgets/wxWidgets/pull/2188
This commit is contained in:
Vadim Zeitlin
2021-01-25 13:01:45 +01:00
4 changed files with 32 additions and 20 deletions

View File

@@ -242,6 +242,9 @@ public:
// library couldn't be loaded but simply returns NULL // library couldn't be loaded but simply returns NULL
static wxDllType RawLoad(const wxString& libname, int flags = wxDL_DEFAULT); static wxDllType RawLoad(const wxString& libname, int flags = wxDL_DEFAULT);
// attach to an existing handle
void Attach(wxDllType h) { Unload(); m_handle = h; }
// detach the library object from its handle, i.e. prevent the object from // detach the library object from its handle, i.e. prevent the object from
// unloading the library in its dtor -- the caller is now responsible for // unloading the library in its dtor -- the caller is now responsible for
// doing this // doing this

View File

@@ -65,7 +65,6 @@ public:
ICoreWebView2Settings* GetSettings(); ICoreWebView2Settings* GetSettings();
static bool ms_isInitialized;
static wxDynamicLibrary ms_loaderDll; static wxDynamicLibrary ms_loaderDll;
static bool Initialize(); static bool Initialize();

View File

@@ -147,9 +147,23 @@ public:
wxPluginCategory cat = wxDL_PLUGIN_GUI); wxPluginCategory cat = wxDL_PLUGIN_GUI);
/** /**
Detaches this object from its library handle, i.e.\ the object will not Attaches the object to an existing handle.
unload the library any longer in its destructor but it is now the
callers responsibility to do this using Unload(). This allows to give ownership of an existing handle, possibly obtained
from Detach(), to this object, so that it will unload it when destroyed.
@since 3.1.5
*/
void Attach(wxDllType h);
/**
Detaches this object from its library handle.
This means that the object will not unload the library any longer in
its destructor but it is now the callers responsibility to do this
using static Unload().
@see Attach()
*/ */
wxDllType Detach(); wxDllType Detach();

View File

@@ -51,7 +51,6 @@ typedef HRESULT(__stdcall *GetAvailableCoreWebView2BrowserVersionString_t)(
CreateCoreWebView2EnvironmentWithOptions_t wxCreateCoreWebView2EnvironmentWithOptions = NULL; CreateCoreWebView2EnvironmentWithOptions_t wxCreateCoreWebView2EnvironmentWithOptions = NULL;
GetAvailableCoreWebView2BrowserVersionString_t wxGetAvailableCoreWebView2BrowserVersionString = NULL; GetAvailableCoreWebView2BrowserVersionString_t wxGetAvailableCoreWebView2BrowserVersionString = NULL;
bool wxWebViewEdgeImpl::ms_isInitialized = false;
wxDynamicLibrary wxWebViewEdgeImpl::ms_loaderDll; wxDynamicLibrary wxWebViewEdgeImpl::ms_loaderDll;
wxWebViewEdgeImpl::wxWebViewEdgeImpl(wxWebViewEdge* webview): wxWebViewEdgeImpl::wxWebViewEdgeImpl(wxWebViewEdge* webview):
@@ -113,39 +112,36 @@ HRESULT wxWebViewEdgeImpl::OnEnvironmentCreated(
bool wxWebViewEdgeImpl::Initialize() bool wxWebViewEdgeImpl::Initialize()
{ {
if (ms_isInitialized) if (ms_loaderDll.IsLoaded())
return true; 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; return false;
// Try to load functions from loader DLL // Try to load functions from loader DLL
wxDL_INIT_FUNC(wx, CreateCoreWebView2EnvironmentWithOptions, ms_loaderDll); wxDL_INIT_FUNC(wx, CreateCoreWebView2EnvironmentWithOptions, loaderDll);
wxDL_INIT_FUNC(wx, GetAvailableCoreWebView2BrowserVersionString, ms_loaderDll); wxDL_INIT_FUNC(wx, GetAvailableCoreWebView2BrowserVersionString, loaderDll);
if (!wxGetAvailableCoreWebView2BrowserVersionString || !wxCreateCoreWebView2EnvironmentWithOptions) if (!wxGetAvailableCoreWebView2BrowserVersionString || !wxCreateCoreWebView2EnvironmentWithOptions)
return false; return false;
// Check if a Edge browser can be found by the loader DLL // Check if a Edge browser can be found by the loader DLL
wxCoTaskMemPtr<wchar_t> versionStr; wxCoTaskMemPtr<wchar_t> versionStr;
HRESULT hr = wxGetAvailableCoreWebView2BrowserVersionString(NULL, &versionStr); HRESULT hr = wxGetAvailableCoreWebView2BrowserVersionString(NULL, &versionStr);
if (SUCCEEDED(hr) && versionStr) if (FAILED(hr) || !versionStr)
{ {
ms_isInitialized = true;
return true;
}
else
wxLogApiError("GetCoreWebView2BrowserVersionInfo", hr); wxLogApiError("GetCoreWebView2BrowserVersionInfo", hr);
return false;
}
return false; ms_loaderDll.Attach(loaderDll.Detach());
return true;
} }
void wxWebViewEdgeImpl::Uninitialize() void wxWebViewEdgeImpl::Uninitialize()
{ {
if (ms_isInitialized) ms_loaderDll.Unload();
{
ms_loaderDll.Unload();
ms_isInitialized = false;
}
} }
void wxWebViewEdgeImpl::UpdateBounds() void wxWebViewEdgeImpl::UpdateBounds()