Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ target_sources(
browser-app.hpp
browser-client.cpp
browser-client.hpp
browser-dummy-client.cpp
browser-dummy-client.hpp
browser-scheme.cpp
browser-scheme.hpp
browser-version.h
Expand Down
70 changes: 70 additions & 0 deletions browser-app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,62 @@ CefRefPtr<CefBrowserProcessHandler> BrowserApp::GetBrowserProcessHandler()
return this;
}

CefRefPtr<CefClient> BrowserApp::GetDefaultClient()
{
return GetDummy();
}

void BrowserApp::OnRegisterCustomSchemes(CefRawPtr<CefSchemeRegistrar> registrar)
{
registrar->AddCustomScheme("http", CEF_SCHEME_OPTION_STANDARD | CEF_SCHEME_OPTION_CORS_ENABLED);
}

void BrowserApp::OnContextInitialized()
{
// Without a default client, CefBrowser is unmanaged, allowing full-blown Chromium windows outside of our control
// We don't actually want those, so define a dummy client which will automatically close any such windows
dummy = new BrowserDummyClient();
Comment thread
WizardCM marked this conversation as resolved.

CefRefPtr<CefRequestContext> requestContext = CefRequestContext::GetGlobalContext();
CefString errorMessage;
CefRefPtr<CefValue> optionValue = CefValue::Create();

constexpr std::array<std::string_view, 20> kBrowserFeaturesToDisable{
"autofill.credit_card_enabled",
"autofill.enabled",
"autofill.iban_enabled",
"autofill.payment_card_benefits",
"autofill.payment_cvc_storage",
"autofill.profile_enabled",
"autologin.enabled",
"browser_labs_enabled",
"credentials_enable_autosignin",
"credentials_enable_service",
"payments.can_make_payment_enabled",
"printing.enabled",
"search.suggest_enabled",
"shopping_list_enabled",
"side_panel.google_search_side_panel_enabled",
"side_search.enabled",
"signin.allowed",
"signin.allowed_on_next_startup",
"translate",
"url_keyed_anonymized_data_collection.enabled"};

constexpr std::array<std::string_view, 2> kBrowserFeaturesToEnable{"extensions.block_external_extensions",
Comment thread
PatTheMav marked this conversation as resolved.
"extensions.disabled"};

optionValue->SetBool(false);
for (std::string_view feature : kBrowserFeaturesToDisable) {
requestContext->SetPreference(feature.data(), optionValue.get(), errorMessage);
}

optionValue->SetBool(true);
for (std::string_view feature : kBrowserFeaturesToEnable) {
requestContext->SetPreference(feature.data(), optionValue.get(), errorMessage);
}
}

void BrowserApp::OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_line)
{
#ifdef _WIN32
Expand Down Expand Up @@ -82,16 +133,35 @@ void BrowserApp::OnBeforeCommandLineProcessing(const CefString &, CefRefPtr<CefC
disableFeatures += ",EnableWindowsGamingInputDataFetcher";
#endif
disableFeatures += ",WebBluetooth";
disableFeatures += ",MediaRouter";
disableFeatures += ",CalculateNativeWinOcclusion";
disableFeatures += ",LiveCaption";
// https://github.com/chromiumembedded/cef/issues/3966
disableFeatures += ".StorageNotificationService";
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?

Suggested change
disableFeatures += ".StorageNotificationService";
disableFeatures += ",StorageNotificationService";

command_line->AppendSwitchWithValue("disable-features", disableFeatures);
} else {
command_line->AppendSwitchWithValue("disable-features", "WebBluetooth,"
#ifdef _WIN32
"EnableWindowsGamingInputDataFetcher,"
#endif
"MediaRouter,"
"CalculateNativeWinOcclusion,"
"LiveCaption,"
"StorageNotificationService,"
"HardwareMediaKeyHandling");
}

if (command_line->HasSwitch("disable-blink-features")) {
std::string disableBlinkFeatures = command_line->GetSwitchValue("disable-blink-features");
disableBlinkFeatures += ",DocumentPictureInPictureAPI";
command_line->AppendSwitchWithValue("disable-blink-features", disableBlinkFeatures);
} else {
command_line->AppendSwitchWithValue("disable-blink-features", "DocumentPictureInPictureAPI");
}

command_line->AppendSwitchWithValue("autoplay-policy", "no-user-gesture-required");
command_line->AppendSwitch("disable-extensions");
command_line->AppendSwitch("hide-crash-restore-bubble");
#ifdef __APPLE__
command_line->AppendSwitch("use-mock-keychain");
#elif !defined(_WIN32)
Expand Down
7 changes: 7 additions & 0 deletions browser-app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <unordered_map>
#include <functional>
#include "cef-headers.hpp"
#include "browser-dummy-client.hpp"

typedef std::function<void(CefRefPtr<CefBrowser>)> BrowserFunc;

Expand Down Expand Up @@ -85,6 +86,8 @@ class BrowserApp : public CefApp, public CefRenderProcessHandler, public CefBrow

virtual CefRefPtr<CefRenderProcessHandler> GetRenderProcessHandler() override;
virtual CefRefPtr<CefBrowserProcessHandler> GetBrowserProcessHandler() override;
virtual CefRefPtr<CefClient> GetDefaultClient() override;
virtual void OnContextInitialized() override;
virtual void OnBeforeChildProcessLaunch(CefRefPtr<CefCommandLine> command_line) override;
virtual void OnRegisterCustomSchemes(CefRawPtr<CefSchemeRegistrar> registrar) override;
virtual void OnBeforeCommandLineProcessing(const CefString &process_type,
Expand All @@ -106,5 +109,9 @@ class BrowserApp : public CefApp, public CefRenderProcessHandler, public CefBrow
QTimer frameTimer;
#endif

CefRefPtr<BrowserDummyClient> dummy = nullptr;

BrowserDummyClient *GetDummy() const { return dummy.get(); };

IMPLEMENT_REFCOUNTING(BrowserApp);
};
11 changes: 11 additions & 0 deletions browser-client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,17 @@ void BrowserClient::OnLoadEnd(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame> frame,
}
}

void BrowserClient::OnLoadError(CefRefPtr<CefBrowser>, [[maybe_unused]] CefRefPtr<CefFrame> frame,
CefLoadHandler::ErrorCode, const CefString &, const CefString &)
{
#if CHROME_VERSION_BUILD > 6533
// CEF doesn't currently provide a way to properly disable/override the default Chrome error page
// https://github.com/obsproject/obs-studio/issues/13499
// FIXME: https://github.com/chromiumembedded/cef/issues/3852
frame->LoadURL("about:blank");
#endif
}

bool BrowserClient::OnConsoleMessage(CefRefPtr<CefBrowser>, cef_log_severity_t level, const CefString &message,
const CefString &source, int line)
{
Expand Down
4 changes: 4 additions & 0 deletions browser-client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,9 @@ class BrowserClient : public CefClient,
/* CefLoadHandler */
virtual void OnLoadEnd(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame, int httpStatusCode) override;

virtual void OnLoadError(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefLoadHandler::ErrorCode errorCode, const CefString &errorText,
const CefString &failedUrl) override;

IMPLEMENT_REFCOUNTING(BrowserClient);
};
71 changes: 71 additions & 0 deletions browser-dummy-client.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

#include "browser-dummy-client.hpp"

CefRefPtr<CefCommandHandler> BrowserDummyClient::GetCommandHandler()
{
return this;
}

CefRefPtr<CefRequestHandler> BrowserDummyClient::GetRequestHandler()
{
return this;
}

CefRefPtr<CefLifeSpanHandler> BrowserDummyClient::GetLifeSpanHandler()
{
return this;
}

bool BrowserDummyClient::OnChromeCommand(CefRefPtr<CefBrowser>, int, cef_window_open_disposition_t)
{
return true;
}

bool BrowserDummyClient::IsChromeAppMenuItemVisible(CefRefPtr<CefBrowser>, int)
{
return false;
}

bool BrowserDummyClient::IsChromeToolbarButtonVisible(cef_chrome_toolbar_button_type_t)
{
return false;
}

bool BrowserDummyClient::IsChromePageActionIconVisible(cef_chrome_page_action_icon_type_t)
{
return false;
}

bool BrowserDummyClient::IsChromeAppMenuItemEnabled(CefRefPtr<CefBrowser>, int)
{
return false;
}

bool BrowserDummyClient::OnBeforePopup(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>,
#if CHROME_VERSION_BUILD >= 6834
int,
#endif
const CefString &, const CefString &, cef_window_open_disposition_t, bool,
const CefPopupFeatures &, CefWindowInfo &, CefRefPtr<CefClient> &,
CefBrowserSettings &, CefRefPtr<CefDictionaryValue> &, bool *)
{
return true;
}

void BrowserDummyClient::OnAfterCreated(CefRefPtr<CefBrowser> browser)
{
if (browser && browser->GetHost()) {
browser->GetHost()->CloseBrowser(false);
}
}

bool BrowserDummyClient::OnOpenURLFromTab(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, const CefString &,
CefRequestHandler::WindowOpenDisposition, bool)
{
return true;
}

bool BrowserDummyClient::OnBeforeBrowse(CefRefPtr<CefBrowser>, CefRefPtr<CefFrame>, CefRefPtr<CefRequest>, bool, bool)
{
return true;
}
47 changes: 47 additions & 0 deletions browser-dummy-client.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#pragma once

#include "cef-headers.hpp"

class BrowserDummyClient : public CefClient,
public CefCommandHandler,
public CefRequestHandler,
public CefLifeSpanHandler {
public:
virtual CefRefPtr<CefCommandHandler> GetCommandHandler() override;
virtual CefRefPtr<CefRequestHandler> GetRequestHandler() override;
virtual CefRefPtr<CefLifeSpanHandler> GetLifeSpanHandler() override;

/* CefCommandHandler */
virtual bool OnChromeCommand(CefRefPtr<CefBrowser> browser, int command_id,
cef_window_open_disposition_t disposition) override;
virtual bool IsChromeAppMenuItemVisible(CefRefPtr<CefBrowser> browser, int command_id) override;
virtual bool IsChromeToolbarButtonVisible(cef_chrome_toolbar_button_type_t button_type) override;

virtual bool IsChromePageActionIconVisible(cef_chrome_page_action_icon_type_t icon_type) override;

virtual bool IsChromeAppMenuItemEnabled(CefRefPtr<CefBrowser> browser, int command_id) override;

/* CefLifeSpanHandler */
virtual bool OnBeforePopup(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
#if CHROME_VERSION_BUILD >= 6834
int,
#endif
const CefString &target_url, const CefString &target_frame_name,
cef_window_open_disposition_t target_disposition, bool user_gesture,
const CefPopupFeatures &popupFeatures, CefWindowInfo &windowInfo,
CefRefPtr<CefClient> &client, CefBrowserSettings &settings,
CefRefPtr<CefDictionaryValue> &extra_info, bool *no_javascript_access) override;

virtual void OnAfterCreated(CefRefPtr<CefBrowser> browser) override;

/* CefRequestHandler */
virtual bool OnOpenURLFromTab(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
const CefString &target_url,
CefRequestHandler::WindowOpenDisposition target_disposition,
bool user_gesture) override;

virtual bool OnBeforeBrowse(CefRefPtr<CefBrowser> browser, CefRefPtr<CefFrame> frame,
CefRefPtr<CefRequest> request, bool user_gesture, bool is_redirect) override;

IMPLEMENT_REFCOUNTING(BrowserDummyClient);
};
3 changes: 2 additions & 1 deletion cmake/os-linux.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ add_executable(OBS::browser-helper ALIAS browser-helper)

target_sources(
browser-helper PRIVATE # cmake-format: sortable
browser-app.cpp browser-app.hpp cef-headers.hpp obs-browser-page/obs-browser-page-main.cpp)
browser-app.cpp browser-app.hpp browser-dummy-client.hpp browser-dummy-client.cpp
cef-headers.hpp obs-browser-page/obs-browser-page-main.cpp)

target_include_directories(browser-helper PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/deps"
"${CMAKE_CURRENT_SOURCE_DIR}/obs-browser-page")
Expand Down
3 changes: 2 additions & 1 deletion cmake/os-macos.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ foreach(helper IN LISTS helper_suffixes)

target_sources(
${target_name} PRIVATE # cmake-format: sortable
browser-app.cpp browser-app.hpp cef-headers.hpp obs-browser-page/obs-browser-page-main.cpp)
browser-app.cpp browser-app.hpp browser-dummy-client.hpp browser-dummy-client.cpp
cef-headers.hpp obs-browser-page/obs-browser-page-main.cpp)

target_compile_definitions(${target_name} PRIVATE ENABLE_BROWSER_SHARED_TEXTURE)

Expand Down
4 changes: 2 additions & 2 deletions cmake/os-windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ add_executable(OBS::browser-helper ALIAS obs-browser-helper)
target_sources(
obs-browser-helper
PRIVATE # cmake-format: sortable
browser-app.cpp browser-app.hpp cef-headers.hpp obs-browser-page.manifest
obs-browser-page/obs-browser-page-main.cpp)
browser-app.cpp browser-app.hpp browser-dummy-client.hpp browser-dummy-client.cpp
cef-headers.hpp obs-browser-page.manifest obs-browser-page/obs-browser-page-main.cpp)

configure_file(cmake/windows/obs-module-helper.rc.in obs-browser-page.rc)
target_sources(obs-browser-helper PRIVATE obs-browser-page.rc)
Expand Down
Loading
Loading