2727 #pragma comment(lib, "cef_sandbox.lib")
2828#endif
2929
30+ CWebCore::EventEntry::EventEntry (const std::function<void ()>& callback_, CWebView* pWebView_) : callback(callback_), pWebView(pWebView_) {}
31+
32+ #ifdef MTA_DEBUG
33+ CWebCore::EventEntry::EventEntry (const std::function<void ()>& callback_, CWebView* pWebView_, const SString& name_) : callback(callback_), pWebView(pWebView_), name(name_) {}
34+ #endif
35+
36+ CWebCore::TaskEntry::TaskEntry (std::function<void (bool )> callback, CWebView* webView) : task(callback), webView(webView) {}
37+
3038CWebCore::CWebCore ()
3139{
3240 m_pRequestsGUI = nullptr ;
@@ -363,7 +371,7 @@ void CWebCore::AddEventToEventQueue(std::function<void()> event, CWebView* pWebV
363371 if (pWebView && pWebView->IsBeingDestroyed ())
364372 return ;
365373
366- std::lock_guard<std::mutex> lock (m_EventQueueMutex);
374+ std::scoped_lock lock (m_EventQueueMutex);
367375
368376 // Prevent unbounded queue growth - drop oldest events if queue is too large
369377 if (m_EventQueue.size () >= MAX_EVENT_QUEUE_SIZE)
@@ -386,7 +394,7 @@ void CWebCore::AddEventToEventQueue(std::function<void()> event, CWebView* pWebV
386394
387395void CWebCore::RemoveWebViewEvents (CWebView* pWebView)
388396{
389- std::lock_guard<std::mutex> lock (m_EventQueueMutex);
397+ std::scoped_lock lock (m_EventQueueMutex);
390398
391399 for (auto iter = m_EventQueue.begin (); iter != m_EventQueue.end ();)
392400 {
@@ -401,12 +409,16 @@ void CWebCore::DoEventQueuePulse()
401409{
402410 std::list<EventEntry> eventQueue;
403411 {
404- std::lock_guard<std::mutex> lock (m_EventQueueMutex);
412+ std::scoped_lock lock (m_EventQueueMutex);
405413 std::swap (eventQueue, m_EventQueue);
406414 }
407415
408416 for (auto & event : eventQueue)
409417 {
418+ // Skip event if the associated WebView is being destroyed
419+ if (event.pWebView && event.pWebView ->IsBeingDestroyed ())
420+ continue ;
421+
410422 event.callback ();
411423 }
412424
@@ -473,13 +485,20 @@ void CWebCore::DoTaskQueuePulse()
473485
474486 for (TaskEntry& entry : taskQueue)
475487 {
488+ // Abort task if the associated WebView is being destroyed
489+ if (entry.webView && entry.webView ->IsBeingDestroyed ())
490+ {
491+ entry.task (true );
492+ continue ;
493+ }
494+
476495 entry.task (false );
477496 }
478497}
479498
480499eURLState CWebCore::GetDomainState (const SString& strURL, bool bOutputDebug)
481500{
482- std::lock_guard<std::recursive_mutex> lock (m_FilterMutex);
501+ std::scoped_lock lock (m_FilterMutex);
483502
484503 // Initialize wildcard whitelist (be careful with modifying) | Todo: Think about the following
485504 static constexpr const char * wildcardWhitelist[] = {" *.googlevideo.com" , " *.google.com" , " *.youtube.com" , " *.ytimg.com" ,
@@ -491,8 +510,7 @@ eURLState CWebCore::GetDomainState(const SString& strURL, bool bOutputDebug)
491510 return eURLState::WEBPAGE_ALLOWED;
492511 }
493512
494- google::dense_hash_map<SString, WebFilterPair>::iterator iter = m_Whitelist.find (strURL);
495- if (iter != m_Whitelist.end ())
513+ if (auto iter = m_Whitelist.find (strURL); iter != m_Whitelist.end ())
496514 {
497515 if (iter->second .first == true )
498516 return eURLState::WEBPAGE_ALLOWED;
@@ -583,7 +601,7 @@ void CWebCore::InitialiseWhiteAndBlacklist(bool bAddHardcoded, bool bAddDynamic)
583601
584602void CWebCore::AddAllowedPage (const SString& strURL, eWebFilterType filterType)
585603{
586- std::lock_guard<std::recursive_mutex> lock (m_FilterMutex);
604+ std::scoped_lock lock (m_FilterMutex);
587605
588606 // Prevent unbounded whitelist growth - remove old REQUEST entries if limit reached
589607 if (m_Whitelist.size () >= 50000 )
@@ -603,7 +621,7 @@ void CWebCore::AddAllowedPage(const SString& strURL, eWebFilterType filterType)
603621
604622void CWebCore::AddBlockedPage (const SString& strURL, eWebFilterType filterType)
605623{
606- std::lock_guard<std::recursive_mutex> lock (m_FilterMutex);
624+ std::scoped_lock lock (m_FilterMutex);
607625
608626 // Prevent unbounded whitelist growth - remove old REQUEST entries if limit reached
609627 if (m_Whitelist.size () >= 50000 )
@@ -623,6 +641,13 @@ void CWebCore::AddBlockedPage(const SString& strURL, eWebFilterType filterType)
623641
624642void CWebCore::RequestPages (const std::vector<SString>& pages, WebRequestCallback* pCallback)
625643{
644+ if (m_PendingRequests.size () >= MAX_PENDING_REQUESTS)
645+ {
646+ if (pCallback)
647+ (*pCallback)(false , std::unordered_set<SString>(pages.begin (), pages.end ()));
648+ return ;
649+ }
650+
626651 // Add to pending pages queue
627652 bool bNewItem = false ;
628653 for (const auto & page : pages)
@@ -631,8 +656,9 @@ void CWebCore::RequestPages(const std::vector<SString>& pages, WebRequestCallbac
631656 if (status == eURLState::WEBPAGE_ALLOWED || status == eURLState::WEBPAGE_DISALLOWED)
632657 continue ;
633658
634- m_PendingRequests.insert (page);
635- bNewItem = true ;
659+ const auto [iter, inserted] = m_PendingRequests.insert (page);
660+ if (inserted)
661+ bNewItem = true ;
636662 }
637663
638664 if (bNewItem)
0 commit comments