Skip to content

Commit ec248d4

Browse files
committed
CEF reworks #2: Miscelanneous
1 parent 2e56bd0 commit ec248d4

File tree

9 files changed

+134
-51
lines changed

9 files changed

+134
-51
lines changed

Client/cefweb/CAjaxResourceHandler.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ bool CAjaxResourceHandler::ReadResponse(void* data_out, int bytes_to_read, int&
8484
}
8585

8686
// Are we done?
87-
if (m_strResponse.length() - m_DataOffset <= 0)
87+
if (m_strResponse.length() - m_DataOffset <= 0) [[unlikely]]
8888
return false;
8989

90-
int copyBytes = std::min((uint)bytes_to_read, m_strResponse.length() - m_DataOffset);
90+
if (bytes_to_read <= 0) [[unlikely]]
91+
return false;
92+
93+
const size_t copyBytes = std::min(static_cast<size_t>(bytes_to_read), m_strResponse.length() - m_DataOffset);
9194

9295
memcpy(data_out, m_strResponse.c_str() + m_DataOffset, copyBytes);
93-
bytes_read = copyBytes;
96+
bytes_read = static_cast<int>(copyBytes);
9497

9598
m_DataOffset += copyBytes;
9699

Client/cefweb/CWebApp.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ void CWebApp::OnBeforeCommandLineProcessing(const CefString& process_type, CefRe
2929
if (!command_line)
3030
return;
3131

32+
if (!g_pCore) [[unlikely]]
33+
return;
34+
3235
const auto pWebCore = static_cast<CWebCore*>(g_pCore->GetWebCore());
3336
if (!pWebCore)
3437
return;
@@ -65,6 +68,9 @@ CefRefPtr<CefResourceHandler> CWebApp::Create(CefRefPtr<CefBrowser> browser, Cef
6568
if (!browser || !frame || !request)
6669
return nullptr;
6770

71+
if (!g_pCore) [[unlikely]]
72+
return nullptr;
73+
6874
const auto pWebCore = static_cast<CWebCore*>(g_pCore->GetWebCore());
6975
if (!pWebCore)
7076
return nullptr;

Client/cefweb/CWebCore.cpp

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ CWebCore::CWebCore()
3131
m_bTestmodeEnabled = false;
3232
m_pXmlConfig = nullptr;
3333
m_pFocusedWebView = nullptr;
34+
m_bGPUEnabled = false;
35+
m_iWhitelistRevision = 0;
36+
m_iBlacklistRevision = 0;
3437

3538
MakeSureXMLNodesExist();
3639
InitialiseWhiteAndBlacklist();
@@ -91,12 +94,14 @@ bool CWebCore::Initialise(bool gpuEnabled)
9194
settings.multi_threaded_message_loop = true;
9295
settings.windowless_rendering_enabled = true;
9396

94-
bool state = CefInitialize(mainArgs, settings, app, sandboxInfo);
95-
96-
// Register custom scheme handler factory
97-
CefRegisterSchemeHandlerFactory("http", "mta", app);
97+
if (const bool state = CefInitialize(mainArgs, settings, app, sandboxInfo); state) [[likely]]
98+
{
99+
// Register custom scheme handler factory only if initialization succeeded
100+
CefRegisterSchemeHandlerFactory("http", "mta", app);
101+
return true;
102+
}
98103

99-
return state;
104+
return false;
100105
}
101106

102107
CWebViewInterface* CWebCore::CreateWebView(unsigned int uiWidth, unsigned int uiHeight, bool bIsLocal, CWebBrowserItem* pWebBrowserRenderItem,
@@ -242,19 +247,17 @@ void CWebCore::WaitForTask(std::function<void(bool)> task, CWebView* webView)
242247
{
243248
std::scoped_lock lock(m_TaskQueueMutex);
244249

245-
// Prevent unbounded queue growth - if queue is too large, oldest tasks will be dropped during pulse
246-
if (m_TaskQueue.size() >= MAX_TASK_QUEUE_SIZE)
250+
// Prevent unbounded queue growth - abort new task if queue is too large
251+
if (m_TaskQueue.size() >= MAX_TASK_QUEUE_SIZE) [[unlikely]]
247252
{
248253
#ifdef MTA_DEBUG
249-
g_pCore->GetConsole()->Printf("Warning: Task queue size limit reached (%d), task will be aborted", MAX_TASK_QUEUE_SIZE);
254+
static constexpr auto WARNING_MSG = "Warning: Task queue size limit reached (%d), aborting new task";
255+
g_pCore->GetConsole()->Printf(WARNING_MSG, MAX_TASK_QUEUE_SIZE);
250256
#endif
251-
// Must still queue the task to fulfill the future, but it will be aborted during processing
252-
// Removing oldest task to make room
253-
if (!m_TaskQueue.empty())
254-
{
255-
m_TaskQueue.front().task(true); // Abort oldest task
256-
m_TaskQueue.pop_front();
257-
}
257+
// Abort the new task immediately to prevent deadlock
258+
// Don't add it to the queue
259+
task(true);
260+
return;
258261
}
259262

260263
m_TaskQueue.emplace_back(TaskEntry{task, webView});
@@ -268,14 +271,14 @@ void CWebCore::RemoveWebViewTasks(CWebView* webView)
268271
{
269272
std::scoped_lock lock(m_TaskQueueMutex);
270273

271-
for (auto iter = m_TaskQueue.begin(); iter != m_TaskQueue.end(); ++iter)
272-
{
273-
if (iter->webView != webView)
274-
continue;
275-
276-
iter->task(true);
277-
iter = m_TaskQueue.erase(iter);
278-
}
274+
std::erase_if(m_TaskQueue, [webView](TaskEntry& entry) {
275+
if (entry.webView == webView)
276+
{
277+
entry.task(true);
278+
return true;
279+
}
280+
return false;
281+
});
279282
}
280283

281284
void CWebCore::DoTaskQueuePulse()
@@ -297,12 +300,12 @@ eURLState CWebCore::GetDomainState(const SString& strURL, bool bOutputDebug)
297300
std::lock_guard<std::recursive_mutex> lock(m_FilterMutex);
298301

299302
// Initialize wildcard whitelist (be careful with modifying) | Todo: Think about the following
300-
static SString wildcardWhitelist[] = {"*.googlevideo.com", "*.google.com", "*.youtube.com", "*.ytimg.com",
301-
"*.vimeocdn.com", "*.gstatic.com", "*.googleapis.com", "*.ggpht.com"};
303+
static constexpr const char* wildcardWhitelist[] = {"*.googlevideo.com", "*.google.com", "*.youtube.com", "*.ytimg.com",
304+
"*.vimeocdn.com", "*.gstatic.com", "*.googleapis.com", "*.ggpht.com"};
302305

303-
for (int i = 0; i < sizeof(wildcardWhitelist) / sizeof(SString); ++i)
306+
for (const auto& pattern : wildcardWhitelist)
304307
{
305-
if (WildcardMatch(wildcardWhitelist[i], strURL))
308+
if (WildcardMatch(pattern, strURL))
306309
return eURLState::WEBPAGE_ALLOWED;
307310
}
308311

@@ -552,7 +555,8 @@ void CWebCore::OnFPSLimitChange(std::uint16_t fps)
552555
dassert(g_pCore->GetNetwork() != nullptr); // Ensure network module is loaded
553556
for (auto& webView : m_WebViews)
554557
{
555-
webView->GetCefBrowser()->GetHost()->SetWindowlessFrameRate(fps);
558+
if (auto browser = webView->GetCefBrowser(); browser) [[likely]]
559+
browser->GetHost()->SetWindowlessFrameRate(fps);
556560
}
557561
}
558562

@@ -610,6 +614,11 @@ bool CWebCore::SetGlobalAudioVolume(float fVolume)
610614
return true;
611615
}
612616

617+
CWebViewInterface* CWebCore::GetFocusedWebView()
618+
{
619+
return m_pFocusedWebView;
620+
}
621+
613622
bool CWebCore::UpdateListsFromMaster()
614623
{
615624
if (!m_pXmlConfig)
@@ -821,7 +830,7 @@ void CWebCore::GetFilterEntriesByType(std::vector<std::pair<SString, bool>>& out
821830
outEntries.push_back(std::pair<SString, bool>(iter->first, iter->second.first));
822831
else if (state == eWebFilterState::WEBFILTER_ALLOWED && iter->second.first == true)
823832
outEntries.push_back(std::pair<SString, bool>(iter->first, iter->second.first));
824-
else
833+
else if (state == eWebFilterState::WEBFILTER_DISALLOWED && iter->second.first == false)
825834
outEntries.push_back(std::pair<SString, bool>(iter->first, iter->second.first));
826835
}
827836
}
@@ -830,6 +839,9 @@ void CWebCore::GetFilterEntriesByType(std::vector<std::pair<SString, bool>>& out
830839
void CWebCore::StaticFetchRevisionFinished(const SHttpDownloadResult& result)
831840
{
832841
CWebCore* pWebCore = static_cast<CWebCore*>(result.pObj);
842+
if (!pWebCore) [[unlikely]]
843+
return;
844+
833845
if (result.bSuccess)
834846
{
835847
SString strData = result.pData;
@@ -870,6 +882,9 @@ void CWebCore::StaticFetchWhitelistFinished(const SHttpDownloadResult& result)
870882
return;
871883

872884
CWebCore* pWebCore = static_cast<CWebCore*>(result.pObj);
885+
if (!pWebCore) [[unlikely]]
886+
return;
887+
873888
if (!pWebCore->m_pXmlConfig)
874889
return;
875890

@@ -913,6 +928,9 @@ void CWebCore::StaticFetchBlacklistFinished(const SHttpDownloadResult& result)
913928
return;
914929

915930
CWebCore* pWebCore = static_cast<CWebCore*>(result.pObj);
931+
if (!pWebCore) [[unlikely]]
932+
return;
933+
916934
if (!pWebCore->m_pXmlConfig)
917935
return;
918936

Client/cefweb/CWebCore.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
class CWebBrowserItem;
3030
class CWebsiteRequests;
3131
class CWebView;
32+
class CWebViewInterface;
3233

3334
class CWebCore : public CWebCoreInterface
3435
{
@@ -88,7 +89,7 @@ class CWebCore : public CWebCoreInterface
8889
void SetTestModeEnabled(bool bEnabled) { m_bTestmodeEnabled = bEnabled; };
8990
void DebugOutputThreadsafe(const SString& message, unsigned char R, unsigned char G, unsigned char B);
9091

91-
CWebViewInterface* GetFocusedWebView() { return (CWebViewInterface*)m_pFocusedWebView; };
92+
CWebViewInterface* GetFocusedWebView();
9293
void SetFocusedWebView(CWebView* pWebView) { m_pFocusedWebView = pWebView; };
9394
void ProcessInputMessage(UINT uMsg, WPARAM wParam, LPARAM lParam);
9495
void ClearTextures();

Client/cefweb/CWebView.cpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ CWebView::~CWebView()
3737
{
3838
if (IsMainThread())
3939
{
40-
if (g_pCore->GetWebCore()->GetFocusedWebView() == this)
41-
g_pCore->GetWebCore()->SetFocusedWebView(nullptr);
40+
if (auto pWebCore = g_pCore->GetWebCore(); pWebCore) [[likely]]
41+
{
42+
if (pWebCore->GetFocusedWebView() == this)
43+
pWebCore->SetFocusedWebView(nullptr);
44+
}
4245
}
4346

4447
// Make sure we don't dead lock the CEF render thread
@@ -936,13 +939,16 @@ CefResourceRequestHandler::ReturnValue CWebView::OnBeforeResourceLoad(CefRefPtr<
936939
void CWebView::OnBeforeClose(CefRefPtr<CefBrowser> browser)
937940
{
938941
// Remove events owned by this webview and invoke left callbacks
939-
g_pCore->GetWebCore()->RemoveWebViewEvents(this);
942+
if (auto pWebCore = g_pCore->GetWebCore(); pWebCore) [[likely]]
943+
{
944+
pWebCore->RemoveWebViewEvents(this);
940945

941-
m_pWebView = nullptr;
946+
// Remove focused web view reference
947+
if (pWebCore->GetFocusedWebView() == this)
948+
pWebCore->SetFocusedWebView(nullptr);
949+
}
942950

943-
// Remove focused web view reference
944-
if (g_pCore->GetWebCore()->GetFocusedWebView() == this)
945-
g_pCore->GetWebCore()->SetFocusedWebView(nullptr);
951+
m_pWebView = nullptr;
946952
}
947953

948954
////////////////////////////////////////////////////////////////////

Client/cefweb/CefWeb.cpp

Lines changed: 49 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,66 @@
33
* PROJECT: Multi Theft Auto v1.0
44
* LICENSE: See LICENSE in the top level directory
55
* FILE: CefWeb.cpp
6+
* PURPOSE: CEF web browser module initialization entry point
67
*
78
* Multi Theft Auto is available from https://www.multitheftauto.com/
89
*
910
*****************************************************************************/
1011

1112
#include "StdInc.h"
12-
#include "SharedUtil.hpp"
13+
#include <memory>
14+
#include <SharedUtil.Memory.h>
1315

14-
CCoreInterface* g_pCore = NULL;
16+
CCoreInterface* g_pCore = nullptr;
1517
CLocalizationInterface* g_pLocalization = nullptr;
1618

17-
extern "C" _declspec(dllexport) CWebCoreInterface* InitWebCoreInterface(CCoreInterface* pCore)
19+
namespace
1820
{
19-
g_pCore = pCore;
20-
g_pLocalization = pCore->GetLocalization();
21+
[[nodiscard]] inline bool InitializeGlobalInterfaces(CCoreInterface* pCore) noexcept
22+
{
23+
if (!pCore) [[unlikely]]
24+
return false;
2125

22-
// Ensure main thread identification is consistent
23-
IsMainThread();
26+
g_pCore = pCore;
27+
g_pLocalization = pCore->GetLocalization();
2428

25-
SetMemoryAllocationFailureHandler();
29+
// Localization is critical for browser GUI (request dialogs, etc.)
30+
if (!g_pLocalization) [[unlikely]]
31+
return false;
2632

27-
CWebCore* pWebCore = new CWebCore;
28-
return pWebCore;
33+
return true;
34+
}
35+
36+
//
37+
// Performs required runtime initialization
38+
// Must be called on the main thread before creating CWebCore
39+
//
40+
inline void PerformRuntimeInitialization() noexcept
41+
{
42+
// Ensure main thread identification is consistent
43+
IsMainThread();
44+
45+
// Set up memory allocation failure handler for CEF processes
46+
SharedUtil::SetMemoryAllocationFailureHandler();
47+
}
48+
} // namespace
49+
50+
//
51+
// DLL export: Initialize the web browser subsystem
52+
// Called by CCore::GetWebCore() during initialization
53+
// Returns CWebCoreInterface pointer on success, nullptr on failure
54+
// Thread safety: Must be called from the main thread only
55+
//
56+
extern "C" _declspec(dllexport) CWebCoreInterface* InitWebCoreInterface(CCoreInterface* pCore) noexcept
57+
{
58+
// Validate and initialize global interfaces
59+
if (!InitializeGlobalInterfaces(pCore)) [[unlikely]]
60+
return nullptr;
61+
62+
// Perform runtime initialization
63+
PerformRuntimeInitialization();
64+
65+
// Create and return the web core instance
66+
// Using make_unique for exception safety, then releasing ownership to caller
67+
return std::make_unique<CWebCore>().release();
2968
}

Client/cefweb/StdInc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
// StdInc.h
22
#include "StdInc.h"
3+
#include "SharedUtil.hpp"

Client/cefweb/premake5.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ project "Client Webbrowser"
3737
"**.cpp"
3838
}
3939

40+
filter {}
41+
4042
links {
4143
"libcef", "CEF", "Psapi.lib", "version.lib", "Winmm.lib", "Ws2_32.lib", "DbgHelp.lib"
4244
}

Client/core/CCore.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1195,7 +1195,14 @@ CWebCoreInterface* CCore::GetWebCore()
11951195
cvars->Get("browser_enable_gpu", gpuEnabled);
11961196

11971197
m_pWebCore = CreateModule<CWebCoreInterface>(m_WebCoreModule, "CefWeb", "cefweb", "InitWebCoreInterface", this);
1198-
m_pWebCore->Initialise(gpuEnabled);
1198+
if (!m_pWebCore) [[unlikely]]
1199+
return nullptr;
1200+
1201+
if (!m_pWebCore->Initialise(gpuEnabled)) [[unlikely]]
1202+
{
1203+
SAFE_DELETE(m_pWebCore);
1204+
return nullptr;
1205+
}
11991206
}
12001207
return m_pWebCore;
12011208
}

0 commit comments

Comments
 (0)