Browse Source

refactor: check ELECTRON_ENABLE_LOGGING via native implementation (#25623)

Milan Burda 4 years ago
parent
commit
b33f22601e

+ 1 - 0
filenames.gni

@@ -452,6 +452,7 @@ filenames = {
     "shell/common/api/electron_api_clipboard.h",
     "shell/common/api/electron_api_clipboard_mac.mm",
     "shell/common/api/electron_api_command_line.cc",
+    "shell/common/api/electron_api_environment.cc",
     "shell/common/api/electron_api_key_weak_map.h",
     "shell/common/api/electron_api_native_image.cc",
     "shell/common/api/electron_api_native_image.h",

+ 4 - 1
lib/browser/api/web-contents.ts

@@ -461,8 +461,11 @@ const addReturnValueToEvent = (event: any) => {
   });
 };
 
+const commandLine = process._linkedBinding('electron_common_command_line');
+const environment = process._linkedBinding('electron_common_environment');
+
 const loggingEnabled = () => {
-  return process.env.ELECTRON_ENABLE_LOGGING || app.commandLine.hasSwitch('enable-logging');
+  return environment.hasVar('ELECTRON_ENABLE_LOGGING') || commandLine.hasSwitch('enable-logging');
 };
 
 // Add JavaScript wrappers for WebContents class.

+ 45 - 0
shell/common/api/electron_api_environment.cc

@@ -0,0 +1,45 @@
+// Copyright (c) 2020 GitHub, Inc.
+// Use of this source code is governed by the MIT license that can be
+// found in the LICENSE file.
+
+#include "base/environment.h"
+#include "shell/common/gin_helper/dictionary.h"
+#include "shell/common/node_includes.h"
+
+namespace {
+
+v8::Local<v8::Value> GetVar(v8::Isolate* isolate, const std::string& name) {
+  std::string value;
+  if (base::Environment::Create()->GetVar(name, &value)) {
+    return gin::StringToV8(isolate, value);
+  } else {
+    return v8::Null(isolate);
+  }
+}
+
+bool HasVar(const std::string& name) {
+  return base::Environment::Create()->HasVar(name);
+}
+
+bool SetVar(const std::string& name, const std::string& value) {
+  return base::Environment::Create()->SetVar(name, value);
+}
+
+bool UnSetVar(const std::string& name) {
+  return base::Environment::Create()->UnSetVar(name);
+}
+
+void Initialize(v8::Local<v8::Object> exports,
+                v8::Local<v8::Value> unused,
+                v8::Local<v8::Context> context,
+                void* priv) {
+  gin_helper::Dictionary dict(context->GetIsolate(), exports);
+  dict.SetMethod("getVar", &GetVar);
+  dict.SetMethod("hasVar", &HasVar);
+  dict.SetMethod("setVar", &SetVar);
+  dict.SetMethod("unSetVar", &UnSetVar);
+}
+
+}  // namespace
+
+NODE_LINKED_MODULE_CONTEXT_AWARE(electron_common_environment, Initialize)

+ 1 - 0
shell/common/node_bindings.cc

@@ -67,6 +67,7 @@
   V(electron_common_asar)                \
   V(electron_common_clipboard)           \
   V(electron_common_command_line)        \
+  V(electron_common_environment)         \
   V(electron_common_features)            \
   V(electron_common_native_image)        \
   V(electron_common_native_theme)        \

+ 8 - 0
typings/internal-ambient.d.ts

@@ -48,6 +48,13 @@ declare namespace NodeJS {
     isSameOrigin(left: string, right: string): boolean;
   }
 
+  interface EnvironmentBinding {
+    getVar(name: string): string | null;
+    hasVar(name: string): boolean;
+    setVar(name: string, value: string): boolean;
+    unSetVar(name: string): boolean;
+  }
+
   type AsarFileInfo = {
     size: number;
     unpacked: boolean;
@@ -137,6 +144,7 @@ declare namespace NodeJS {
     _linkedBinding(name: 'electron_common_features'): FeaturesBinding;
     _linkedBinding(name: 'electron_browser_app'): { app: Electron.App, App: Function };
     _linkedBinding(name: 'electron_common_command_line'): Electron.CommandLine;
+    _linkedBinding(name: 'electron_common_environment'): EnvironmentBinding;
     _linkedBinding(name: 'electron_browser_desktop_capturer'): {
       createDesktopCapturer(): ElectronInternal.DesktopCapturer;
     };