Implement user agent access for Edge

This commit is contained in:
Tobias Taschner
2021-03-16 22:16:33 +01:00
committed by Tobias Taschner
parent 3d568c7105
commit 2487a48b66
3 changed files with 30 additions and 0 deletions

View File

@@ -58,6 +58,7 @@ public:
wxVector<wxString> m_pendingUserScripts;
wxVector<wxString> m_userScriptIds;
wxString m_scriptMsgHandlerName;
wxString m_customUserAgent;
// WebView Events tokens
EventRegistrationToken m_navigationStartingToken = { };

View File

@@ -86,6 +86,8 @@ public:
virtual void EnableAccessToDevTools(bool enable = true) wxOVERRIDE;
virtual bool IsAccessToDevToolsEnabled() const wxOVERRIDE;
virtual bool SetUserAgent(const wxString& userAgent) wxOVERRIDE;
virtual bool RunScript(const wxString& javascript, wxString* output = NULL) const wxOVERRIDE;
virtual bool AddScriptMessageHandler(const wxString& name) wxOVERRIDE;
virtual bool RemoveScriptMessageHandler(const wxString& name) wxOVERRIDE;

View File

@@ -29,6 +29,7 @@
#ifdef __VISUALC__
#include <wrl/event.h>
using namespace Microsoft::WRL;
#include "WebView2EnvironmentOptions.h"
#else
#include <wx/msw/wrl/event.h>
#endif // !__VISUALC__
@@ -91,11 +92,23 @@ bool wxWebViewEdgeImpl::Create()
m_historyPosition = -1;
wxString userDataPath = wxStandardPaths::Get().GetUserLocalDataDir();
#ifdef __VISUALC__
auto options =
Make<CoreWebView2EnvironmentOptions>();
if (!m_customUserAgent.empty())
options->put_AdditionalBrowserArguments(
wxString::Format("--user-agent=\"%s\"", m_customUserAgent).wc_str());
#endif
HRESULT hr = wxCreateCoreWebView2EnvironmentWithOptions(
ms_browserExecutableDir.wc_str(),
userDataPath.wc_str(),
#ifdef __VISUALC__
options.Get(),
#else
nullptr,
#endif
Callback<ICoreWebView2CreateCoreWebView2EnvironmentCompletedHandler>(this,
&wxWebViewEdgeImpl::OnEnvironmentCreated).Get());
if (FAILED(hr))
@@ -778,6 +791,20 @@ bool wxWebViewEdge::IsAccessToDevToolsEnabled() const
return true;
}
bool wxWebViewEdge::SetUserAgent(const wxString& userAgent)
{
m_impl->m_customUserAgent = userAgent;
// Can currently only be set before Create()
wxCHECK_MSG(!m_impl->m_webViewController, false, "Can't be called after Create()");
if (m_impl->m_webViewController)
return false;
else
return true;
// TODO: As of Edge SDK 1.0.790 an experimental API to set the user agent
// is available. Reimplement using m_impl->GetSettings() when it's stable.
}
void* wxWebViewEdge::GetNativeBackend() const
{
return m_impl->m_webView;