Browse Source

Add nodeIntegrationInWorker option

Cheng Zhao 8 years ago
parent
commit
a49af26e39

+ 4 - 0
atom/browser/web_contents_preferences.cc

@@ -97,6 +97,10 @@ void WebContentsPreferences::AppendExtraCommandLineSwitches(
   command_line->AppendSwitchASCII(switches::kNodeIntegration,
                                   node_integration ? "true" : "false");
 
+  // Whether to enable node integration in Worker.
+  if (web_preferences.GetBoolean(options::kNodeIntegrationInWorker, &b) && b)
+    command_line->AppendSwitch(switches::kNodeIntegrationInWorker);
+
   // If the `sandbox` option was passed to the BrowserWindow's webPreferences,
   // pass `--enable-sandbox` to the renderer so it won't have any node.js
   // integration.

+ 6 - 0
atom/common/options_switches.cc

@@ -122,6 +122,9 @@ const char kBlinkFeatures[] = "blinkFeatures";
 // Disable blink features.
 const char kDisableBlinkFeatures[] = "disableBlinkFeatures";
 
+// Enable the node integration in WebWorker.
+const char kNodeIntegrationInWorker[] = "nodeIntegrationInWorker";
+
 }  // namespace options
 
 namespace switches {
@@ -164,6 +167,9 @@ const char kOpenerID[]         = "opener-id";
 const char kScrollBounce[]     = "scroll-bounce";
 const char kHiddenPage[]       = "hidden-page";
 
+// Command switch passed to renderer process to control nodeIntegration.
+const char kNodeIntegrationInWorker[]  = "node-integration-in-worker";
+
 // Widevine options
 // Path to Widevine CDM binaries.
 const char kWidevineCdmPath[] = "widevine-cdm-path";

+ 2 - 0
atom/common/options_switches.h

@@ -62,6 +62,7 @@ extern const char kOpenerID[];
 extern const char kScrollBounce[];
 extern const char kBlinkFeatures[];
 extern const char kDisableBlinkFeatures[];
+extern const char kNodeIntegrationInWorker[];
 
 }   // namespace options
 
@@ -89,6 +90,7 @@ extern const char kGuestInstanceID[];
 extern const char kOpenerID[];
 extern const char kScrollBounce[];
 extern const char kHiddenPage[];
+extern const char kNodeIntegrationInWorker[];
 
 extern const char kWidevineCdmPath[];
 extern const char kWidevineCdmVersion[];

+ 8 - 2
atom/renderer/atom_renderer_client.cc

@@ -441,12 +441,18 @@ void AtomRendererClient::AddSupportedKeySystems(
 
 void AtomRendererClient::DidInitializeWorkerContextOnWorkerThread(
     v8::Local<v8::Context> context) {
-  WebWorkerObserver::GetCurrent()->ContextCreated(context);
+  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
+          switches::kNodeIntegrationInWorker)) {
+    WebWorkerObserver::GetCurrent()->ContextCreated(context);
+  }
 }
 
 void AtomRendererClient::WillDestroyWorkerContextOnWorkerThread(
     v8::Local<v8::Context> context) {
-  WebWorkerObserver::GetCurrent()->ContextWillDestroy(context);
+  if (base::CommandLine::ForCurrentProcess()->HasSwitch(
+          switches::kNodeIntegrationInWorker)) {
+    WebWorkerObserver::GetCurrent()->ContextWillDestroy(context);
+  }
 }
 
 v8::Local<v8::Context> AtomRendererClient::GetContext(