From b1e6d4f64c6aa841650340c87d743b794572bb24 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 26 Nov 2015 20:15:35 +0800 Subject: [PATCH 1/4] Check ELECTRON_RUN_AS_NODE env var --- atom/app/atom_main.cc | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/atom/app/atom_main.cc b/atom/app/atom_main.cc index 26dcb94212..88bb137bae 100644 --- a/atom/app/atom_main.cc +++ b/atom/app/atom_main.cc @@ -36,10 +36,27 @@ #include "base/at_exit.h" #include "base/i18n/icu_util.h" -#if defined(OS_WIN) - namespace { +const char* kRunAsNode = "ELECTRON_RUN_AS_NODE"; +const char* kOldRunAsNode = "ATOM_SHELL_INTERNAL_RUN_AS_NODE"; + +bool IsEnvSet(const char* name) { +#if defined(OS_WIN) + size_t required_size; + getenv_s(&required_size, nullptr, 0, name); + return required_size != 0; +#else + char* indicator = getenv(name); + return indicator && indicator[0] != '\0'; +#endif +} + +bool IsRunAsNode() { + return IsEnvSet(kRunAsNode) || IsEnvSet(kOldRunAsNode); +} + +#if defined(OS_WIN) // Win8.1 supports monitor-specific DPI scaling. bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value) { typedef HRESULT(WINAPI *SetProcessDpiAwarenessPtr)(PROCESS_DPI_AWARENESS); @@ -77,9 +94,11 @@ void EnableHighDPISupport() { SetProcessDPIAwareWrapper(); } } +#endif } // namespace +#if defined(OS_WIN) int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { int argc = 0; wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc); @@ -131,16 +150,12 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { } } - std::string node_indicator, crash_service_indicator; - if (env->GetVar("ATOM_SHELL_INTERNAL_RUN_AS_NODE", &node_indicator) && - node_indicator == "1") { + if (IsRunAsNode()) { // Now that argv conversion is done, we can finally start. base::AtExitManager atexit_manager; base::i18n::InitializeICU(); return atom::NodeMain(argc, argv); - } else if (env->GetVar("ATOM_SHELL_INTERNAL_CRASH_SERVICE", - &crash_service_indicator) && - crash_service_indicator == "1") { + } else if (IsEnvSet("ATOM_SHELL_INTERNAL_CRASH_SERVICE")) { return crash_service::Main(cmd); } @@ -164,8 +179,7 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { #elif defined(OS_LINUX) // defined(OS_WIN) int main(int argc, const char* argv[]) { - char* node_indicator = getenv("ATOM_SHELL_INTERNAL_RUN_AS_NODE"); - if (node_indicator != NULL && strcmp(node_indicator, "1") == 0) { + if (IsRunAsNode()) { base::i18n::InitializeICU(); base::AtExitManager atexit_manager; return atom::NodeMain(argc, const_cast(argv)); @@ -182,8 +196,7 @@ int main(int argc, const char* argv[]) { #else // defined(OS_LINUX) int main(int argc, const char* argv[]) { - char* node_indicator = getenv("ATOM_SHELL_INTERNAL_RUN_AS_NODE"); - if (node_indicator != NULL && strcmp(node_indicator, "1") == 0) { + if (IsRunAsNode()) { return AtomInitializeICUandStartNode(argc, const_cast(argv)); } From 6534a0e616471c6bd577282f84e750c16ff699cf Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 26 Nov 2015 20:37:48 +0800 Subject: [PATCH 2/4] docs: Add Environment Variables --- docs/README.md | 1 + docs/api/environment-variables.md | 32 +++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 docs/api/environment-variables.md diff --git a/docs/README.md b/docs/README.md index 9b6372524b..917f4050b3 100644 --- a/docs/README.md +++ b/docs/README.md @@ -30,6 +30,7 @@ select the tag that matches your version. * [Synopsis](api/synopsis.md) * [Process Object](api/process.md) * [Supported Chrome Command Line Switches](api/chrome-command-line-switches.md) +* [Environment Variables](api/environment-variables.md) ### Custom DOM Elements: diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md new file mode 100644 index 0000000000..76cf4304e5 --- /dev/null +++ b/docs/api/environment-variables.md @@ -0,0 +1,32 @@ +# Environment variables + +Some behaviors of Electron are controlled by environment variables, because they +are initialized earlier than command line and the app's code. + +## `ELECTRON_RUN_AS_NODE` + +Starts the process as a normal Node.js process. + +## `ELECTRON_ENABLE_LOGGING` + +Prints Chrome's internal logging to console. + +## `ELECTRON_ENABLE_STACK_DUMPING` + +When Electron crashed, prints the stack trace to console. + +This environment variable will not work if `crashReporter` is started. + +## `ELECTRON_DEFAULT_ERROR_MODE` _Windows_ + +Shows Windows's crash dialog when Electron crashed. + +This environment variable will not work if `crashReporter` is started. + +## `ELECTRON_FORCE_WINDOW_MENU_BAR` _Linux_ + +Don't use global menu bar on Linux. + +## `ELECTRON_HIDE_INTERNAL_MODULES` + +Turns off compatibility mode for old built-in modules like `require('ipc')`. From 682b48095a3110a6ed891f076cb1983c809a3ddc Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 26 Nov 2015 20:44:07 +0800 Subject: [PATCH 3/4] docs: Add example --- docs/api/environment-variables.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md index 76cf4304e5..b5d3ff3f6f 100644 --- a/docs/api/environment-variables.md +++ b/docs/api/environment-variables.md @@ -3,6 +3,20 @@ Some behaviors of Electron are controlled by environment variables, because they are initialized earlier than command line and the app's code. +Examples on POSIX shells: + +```bash +$ export ELECTRON_ENABLE_LOGGING=true +$ electron +``` + +on Windows console: + +```powershell +> set ELECTRON_ENABLE_LOGGING=true +> electron +``` + ## `ELECTRON_RUN_AS_NODE` Starts the process as a normal Node.js process. From 59402eb23fb1cd87bd92e8bea618c2f2321fe105 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 26 Nov 2015 21:02:55 +0800 Subject: [PATCH 4/4] Add ELECTRON_NO_ATTACH_CONSOLE env var Close #1556. --- atom/app/atom_main.cc | 13 +++++++------ docs/api/environment-variables.md | 4 ++++ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/atom/app/atom_main.cc b/atom/app/atom_main.cc index 88bb137bae..5b5df448df 100644 --- a/atom/app/atom_main.cc +++ b/atom/app/atom_main.cc @@ -5,7 +5,6 @@ #include "atom/app/atom_main.h" #include -#include #if defined(OS_WIN) #include @@ -57,6 +56,12 @@ bool IsRunAsNode() { } #if defined(OS_WIN) +bool IsCygwin() { + std::string os; + scoped_ptr env(base::Environment::Create()); + return env->GetVar("OS", &os) && os == "cygwin"; +} + // Win8.1 supports monitor-specific DPI scaling. bool SetProcessDpiAwarenessWrapper(PROCESS_DPI_AWARENESS value) { typedef HRESULT(WINAPI *SetProcessDpiAwarenessPtr)(PROCESS_DPI_AWARENESS); @@ -103,17 +108,13 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE, wchar_t* cmd, int) { int argc = 0; wchar_t** wargv = ::CommandLineToArgvW(::GetCommandLineW(), &argc); - scoped_ptr env(base::Environment::Create()); - // Make output work in console if we are not in cygiwn. - std::string os; - if (env->GetVar("OS", &os) && os != "cygwin") { + if (!IsCygwin() && !IsEnvSet("ELECTRON_NO_ATTACH_CONSOLE")) { AttachConsole(ATTACH_PARENT_PROCESS); FILE* dontcare; freopen_s(&dontcare, "CON", "w", stdout); freopen_s(&dontcare, "CON", "w", stderr); - freopen_s(&dontcare, "CON", "r", stdin); } // Convert argv to to UTF8 diff --git a/docs/api/environment-variables.md b/docs/api/environment-variables.md index b5d3ff3f6f..6b000aaa10 100644 --- a/docs/api/environment-variables.md +++ b/docs/api/environment-variables.md @@ -37,6 +37,10 @@ Shows Windows's crash dialog when Electron crashed. This environment variable will not work if `crashReporter` is started. +## `ELECTRON_NO_ATTACH_CONSOLE` _Windows_ + +Don't attach to current console session. + ## `ELECTRON_FORCE_WINDOW_MENU_BAR` _Linux_ Don't use global menu bar on Linux.