1515#include " CWebDevTools.h"
1616#include < chrono>
1717#include " CWebViewAuth.h" // AUTH: IPC validation helpers
18+ #include < utility>
1819
1920namespace
2021{
@@ -23,6 +24,7 @@ namespace
2324
2425CWebView::CWebView (bool bIsLocal, CWebBrowserItem* pWebBrowserRenderItem, bool bTransparent)
2526{
27+ m_pEventTarget = std::make_shared<FEventTarget>();
2628 m_bIsLocal = bIsLocal;
2729 m_bIsTransparent = bTransparent;
2830 m_pWebBrowserRenderItem = pWebBrowserRenderItem;
@@ -42,6 +44,9 @@ CWebView::~CWebView()
4244{
4345 m_bBeingDestroyed = true ;
4446
47+ if (m_pEventTarget)
48+ m_pEventTarget->Clear (m_pEventsInterface);
49+
4550 if (IsMainThread ())
4651 {
4752 if (auto pWebCore = g_pCore->GetWebCore (); pWebCore)
@@ -86,6 +91,41 @@ CWebView::~CWebView()
8691 OutputDebugLine (" CWebView::~CWebView" );
8792}
8893
94+ void CWebView::SetWebBrowserEvents (CWebBrowserEventsInterface* pInterface)
95+ {
96+ m_pEventsInterface = pInterface;
97+
98+ if (m_pEventTarget)
99+ m_pEventTarget->Assign (pInterface);
100+ }
101+
102+ void CWebView::ClearWebBrowserEvents (CWebBrowserEventsInterface* pInterface)
103+ {
104+ if (m_pEventTarget)
105+ m_pEventTarget->Clear (pInterface);
106+
107+ if (m_pEventsInterface == pInterface)
108+ m_pEventsInterface = nullptr ;
109+ }
110+
111+ void CWebView::QueueBrowserEvent (const char * name, std::function<void (CWebBrowserEventsInterface*)>&& fn)
112+ {
113+ auto target = m_pEventTarget;
114+ if (!target)
115+ return ;
116+
117+ const auto token = target->CreateDispatchToken ();
118+
119+ g_pCore->GetWebCore ()->AddEventToEventQueue (
120+ [target, token, fn = std::move (fn)]() mutable {
121+ if (!target)
122+ return ;
123+
124+ target->Dispatch (token, fn);
125+ },
126+ this , name);
127+ }
128+
89129void CWebView::Initialise ()
90130{
91131 // Initialise the web session (which holds the actual settings) in in-memory mode
@@ -784,7 +824,11 @@ bool CWebView::GetFullPathFromLocal(SString& strPath)
784824 if (aborted)
785825 return ;
786826
787- result = m_pEventsInterface->Events_OnResourcePathCheck (strPath);
827+ auto * events = m_pEventsInterface;
828+ if (!events)
829+ return ;
830+
831+ result = events->Events_OnResourcePathCheck (strPath);
788832 },
789833 this );
790834
@@ -813,8 +857,11 @@ void CWebView::HandleAjaxRequest(const SString& strURL, CAjaxResourceHandler* pH
813857 // Only queue event if not being destroyed to prevent UAF
814858 if (!m_bBeingDestroyed)
815859 {
816- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnAjaxRequest, m_pEventsInterface, pHandler, strURL);
817- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " AjaxResourceRequest" );
860+ QueueBrowserEvent (
861+ " AjaxResourceRequest" ,
862+ [handler = pHandler, url = strURL](CWebBrowserEventsInterface* iface) {
863+ iface->Events_OnAjaxRequest (handler, url);
864+ });
818865 }
819866}
820867
@@ -835,7 +882,11 @@ bool CWebView::VerifyFile(const SString& strPath, CBuffer& outFileData)
835882 if (aborted)
836883 return ;
837884
838- result = m_pEventsInterface->Events_OnResourceFileCheck (strPath, outFileData);
885+ auto * events = m_pEventsInterface;
886+ if (!events)
887+ return ;
888+
889+ result = events->Events_OnResourceFileCheck (strPath, outFileData);
839890 },
840891 this );
841892
@@ -1115,8 +1166,11 @@ void CWebView::OnLoadStart(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> fr
11151166 return ;
11161167
11171168 // Queue event to run on the main thread
1118- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnLoadingStart, m_pEventsInterface, strURL, frame->IsMain ());
1119- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnLoadStart" );
1169+ QueueBrowserEvent (
1170+ " OnLoadStart" ,
1171+ [url = strURL, isMain = frame->IsMain ()](CWebBrowserEventsInterface* iface) {
1172+ iface->Events_OnLoadingStart (url, isMain);
1173+ });
11201174}
11211175
11221176// //////////////////////////////////////////////////////////////////
@@ -1135,8 +1189,11 @@ void CWebView::OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> fram
11351189 SString strURL = UTF16ToMbUTF8 (frame->GetURL ());
11361190
11371191 // Queue event to run on the main thread
1138- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnDocumentReady, m_pEventsInterface, strURL);
1139- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnLoadEnd" );
1192+ QueueBrowserEvent (
1193+ " OnLoadEnd" ,
1194+ [url = strURL](CWebBrowserEventsInterface* iface) {
1195+ iface->Events_OnDocumentReady (url);
1196+ });
11401197 }
11411198}
11421199
@@ -1153,8 +1210,11 @@ void CWebView::OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> fr
11531210 SString strURL = UTF16ToMbUTF8 (frame->GetURL ());
11541211
11551212 // Queue event to run on the main thread
1156- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnLoadingFailed, m_pEventsInterface, strURL, errorCode, SString (errorText));
1157- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnLoadError" );
1213+ QueueBrowserEvent (
1214+ " OnLoadError" ,
1215+ [url = strURL, errorCode, errorDescription = SString (errorText)](CWebBrowserEventsInterface* iface) mutable {
1216+ iface->Events_OnLoadingFailed (url, errorCode, errorDescription);
1217+ });
11581218}
11591219
11601220// //////////////////////////////////////////////////////////////////
@@ -1200,8 +1260,11 @@ bool CWebView::OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame>
12001260 bool bIsMainFrame = frame->IsMain ();
12011261
12021262 // Queue event to run on the main thread
1203- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnNavigate, m_pEventsInterface, SString (request->GetURL ()), bResult, bIsMainFrame);
1204- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnNavigate" );
1263+ QueueBrowserEvent (
1264+ " OnNavigate" ,
1265+ [url = SString (request->GetURL ()), blocked = bResult, isMain = bIsMainFrame](CWebBrowserEventsInterface* iface) mutable {
1266+ iface->Events_OnNavigate (url, blocked, isMain);
1267+ });
12051268
12061269 // Return execution to CEF
12071270 return bResult;
@@ -1264,9 +1327,12 @@ CefResourceRequestHandler::ReturnValue CWebView::OnBeforeResourceLoad(CefRefPtr<
12641327 if (urlState != eURLState::WEBPAGE_ALLOWED)
12651328 {
12661329 // Trigger onClientBrowserResourceBlocked event
1267- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnResourceBlocked, m_pEventsInterface, SString (request->GetURL ()), domain,
1268- urlState == eURLState::WEBPAGE_NOT_LISTED ? 0 : 1 );
1269- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnResourceBlocked" );
1330+ QueueBrowserEvent (
1331+ " OnResourceBlocked" ,
1332+ [url = SString (request->GetURL ()), domain, reason = static_cast <unsigned char >(urlState == eURLState::WEBPAGE_NOT_LISTED ? 0 : 1 )](
1333+ CWebBrowserEventsInterface* iface) mutable {
1334+ iface->Events_OnResourceBlocked (url, domain, reason);
1335+ });
12701336
12711337 return RV_CANCEL; // Block if explicitly forbidden
12721338 }
@@ -1283,9 +1349,11 @@ CefResourceRequestHandler::ReturnValue CWebView::OnBeforeResourceLoad(CefRefPtr<
12831349 }
12841350
12851351 // Trigger onClientBrowserResourceBlocked event
1286- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnResourceBlocked, m_pEventsInterface, SString (request->GetURL ()), " " ,
1287- 2 ); // reason 1 := blocked protocol scheme
1288- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnResourceBlocked" );
1352+ QueueBrowserEvent (
1353+ " OnResourceBlocked" ,
1354+ [url = SString (request->GetURL ())](CWebBrowserEventsInterface* iface) mutable {
1355+ iface->Events_OnResourceBlocked (url, " " , 2 );
1356+ });
12891357
12901358 // Block everything else
12911359 return RV_CANCEL;
@@ -1331,8 +1399,11 @@ bool CWebView::OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame>
13311399 SString strOpenerURL = UTF16ToMbUTF8 (frame->GetURL ());
13321400
13331401 // Queue event to run on the main thread
1334- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnPopup, m_pEventsInterface, strTagetURL, strOpenerURL);
1335- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnBeforePopup" );
1402+ QueueBrowserEvent (
1403+ " OnBeforePopup" ,
1404+ [target = strTagetURL, opener = strOpenerURL](CWebBrowserEventsInterface* iface) {
1405+ iface->Events_OnPopup (target, opener);
1406+ });
13361407
13371408 // Block popups generally
13381409 return true ;
@@ -1356,8 +1427,11 @@ void CWebView::OnAfterCreated(CefRefPtr<CefBrowser> browser)
13561427 m_pWebView = browser;
13571428
13581429 // Call created event callback
1359- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnCreated, m_pEventsInterface);
1360- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnAfterCreated" );
1430+ QueueBrowserEvent (
1431+ " OnAfterCreated" ,
1432+ [](CWebBrowserEventsInterface* iface) {
1433+ iface->Events_OnCreated ();
1434+ });
13611435}
13621436
13631437// //////////////////////////////////////////////////////////////////
@@ -1413,8 +1487,11 @@ void CWebView::OnTitleChange(CefRefPtr<CefBrowser> browser, const CefString& tit
14131487bool CWebView::OnTooltip (CefRefPtr<CefBrowser> browser, CefString& title)
14141488{
14151489 // Queue event to run on the main thread
1416- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnTooltip, m_pEventsInterface, UTF16ToMbUTF8 (title));
1417- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnTooltip" );
1490+ QueueBrowserEvent (
1491+ " OnTooltip" ,
1492+ [tooltip = UTF16ToMbUTF8 (title)](CWebBrowserEventsInterface* iface) mutable {
1493+ iface->Events_OnTooltip (tooltip);
1494+ });
14181495
14191496 return true ;
14201497}
@@ -1454,8 +1531,11 @@ bool CWebView::OnCursorChange(CefRefPtr<CefBrowser> browser, CefCursorHandle cur
14541531 unsigned char cursorIndex = static_cast <unsigned char >(type);
14551532
14561533 // Queue event to run on the main thread
1457- auto func = std::bind (&CWebBrowserEventsInterface::Events_OnChangeCursor, m_pEventsInterface, cursorIndex);
1458- g_pCore->GetWebCore ()->AddEventToEventQueue (func, this , " OnCursorChange" );
1534+ QueueBrowserEvent (
1535+ " OnCursorChange" ,
1536+ [cursorIndex](CWebBrowserEventsInterface* iface) {
1537+ iface->Events_OnChangeCursor (cursorIndex);
1538+ });
14591539
14601540 return false ;
14611541}
0 commit comments