Compare commits

...

3 Commits

Author SHA1 Message Date
Shelley Vohr
0ece28ff3e Update shell/common/platform_util_linux.cc
Co-authored-by: Charles Kerr <charles@charleskerr.com>
2021-06-10 18:36:36 +02:00
Shelley Vohr
fcef5a5333 test: fixup GDK_BACKEND test 2021-06-10 12:31:17 +02:00
Shelley Vohr
711d8a4bf4 fix: don't propagate GDK_BACKEND to subprocs 2021-06-10 09:41:12 +02:00
6 changed files with 106 additions and 0 deletions

View File

@@ -375,8 +375,24 @@ void ElectronBrowserMainParts::PostDestroyThreads() {
fake_browser_process_->PostDestroyThreads();
}
#if defined(OS_LINUX)
// static
absl::optional<std::string>& ElectronBrowserMainParts::GetGDKBackend() {
static absl::optional<std::string> gdk_backend;
return gdk_backend;
}
#endif
void ElectronBrowserMainParts::ToolkitInitialized() {
#if defined(OS_LINUX)
// This is set by Chromium here:
// https://chromium-review.googlesource.com/c/chromium/src/+/2586184
// and can detrimentally affect external app behaviors, so we want to
// check if the user has set it so we can use it later.
std::string backend;
if (base::Environment::Create()->GetVar("GDK_BACKEND", &backend))
GetGDKBackend() = backend;
auto linux_ui = BuildGtkUi();
linux_ui->Initialize();

View File

@@ -6,6 +6,7 @@
#define SHELL_BROWSER_ELECTRON_BROWSER_MAIN_PARTS_H_
#include <memory>
#include <string>
#include "base/callback.h"
#include "base/metrics/field_trial.h"
@@ -88,6 +89,11 @@ class ElectronBrowserMainParts : public content::BrowserMainParts {
Browser* browser() { return browser_.get(); }
BrowserProcessImpl* browser_process() { return fake_browser_process_.get(); }
#if defined(OS_LINUX)
// Used by platform_util to set GDK_BACKEND.
static absl::optional<std::string>& GetGDKBackend();
#endif
protected:
// content::BrowserMainParts:
int PreEarlyInitialization() override;

View File

@@ -19,6 +19,7 @@
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "shell/browser/electron_browser_main_parts.h"
#include "shell/common/platform_util_internal.h"
#include "ui/gtk/gtk_util.h"
#include "url/gurl.h"
@@ -122,6 +123,16 @@ bool XDGUtil(const std::vector<std::string>& argv,
// bring up a new terminal if necessary. See "man mailcap".
options.environment["MM_NOTTTY"] = "1";
// If the user set a GDK_BACKEND value of their own, use that,
// otherwise unset it becuase Chromium is setting GDK_BACKEND
// during GTK initialization and we want to respect user preference.
// Setting values in EnvironmentMap to an empty-string
// will make sure that they get unset from the environment via
// AlterEnvironment().
const absl::optional<std::string>& gdk_backend =
electron::ElectronBrowserMainParts::GetGDKBackend();
options.environment["GDK_BACKEND"] = gdk_backend ? *gdk_backend : std::string{};
base::Process process = base::LaunchProcess(argv, options);
if (!process.IsValid())
return false;

View File

@@ -367,6 +367,66 @@ describe('web security', () => {
});
});
ifdescribe(process.platform === 'linux')('subprocesses', () => {
let appProcess: ChildProcess.ChildProcessWithoutNullStreams | undefined;
afterEach(() => {
if (appProcess && !appProcess.killed) {
appProcess.kill();
appProcess = undefined;
}
});
it('does not propagate GDK_BACKEND', async () => {
const appPath = path.join(fixturesPath, 'api', 'gdk-backend-check');
appProcess = ChildProcess.spawn(process.execPath, [appPath], {
env: {
GDK_BACKEND: ''
}
});
let output = '';
appProcess.stdout.on('data', (data) => { output += data; });
let stderr = '';
appProcess.stderr.on('data', (data) => { stderr += data; });
const [code, signal] = await emittedOnce(appProcess, 'exit');
if (code !== 0) {
throw new Error(`Process exited with code "${code}" signal "${signal}" output "${output}" stderr "${stderr}"`);
}
output = output.replace(/(\r\n|\n|\r)/gm, '');
const backend = String(process.env.GDK_BACKEND);
expect(output).to.not.equal(backend);
expect(output).to.be.empty();
});
it('successfully honors GDK_BACKEND set in the subproc', async () => {
const appPath = path.join(fixturesPath, 'api', 'gdk-backend-check');
appProcess = ChildProcess.spawn(process.execPath, [appPath], {
env: {
GDK_BACKEND: 'wayland'
}
});
let output = '';
appProcess.stdout.on('data', (data) => { output += data; });
let stderr = '';
appProcess.stderr.on('data', (data) => { stderr += data; });
const [code, signal] = await emittedOnce(appProcess, 'exit');
if (code !== 0) {
throw new Error(`Process exited with code "${code}" signal "${signal}" output "${output}" stderr "${stderr}"`);
}
output = output.replace(/(\r\n|\n|\r)/gm, '');
const backend = String(process.env.GDK_BACKEND);
expect(output).to.not.equal(backend);
expect(output).to.equal('wayland');
});
});
describe('command line switches', () => {
let appProcess: ChildProcess.ChildProcessWithoutNullStreams | undefined;
afterEach(() => {

View File

@@ -0,0 +1,8 @@
const { app } = require('electron');
app.whenReady().then(() => {
process.stdout.write(String(process.env.GDK_BACKEND));
process.stdout.end();
app.quit();
});

View File

@@ -0,0 +1,5 @@
{
"name": "electron-test-gdk-backend-check",
"main": "main.js"
}