@@ -78,7 +78,7 @@ namespace manifest_keys = extensions::manifest_keys;
78
78
namespace nw {
79
79
80
80
namespace {
81
- std::string g_extension_id;
81
+ std::string g_extension_id, g_extension_root ;
82
82
extensions::Dispatcher* g_dispatcher = NULL ;
83
83
bool g_skip_render_widget_hidden = false ;
84
84
@@ -150,7 +150,7 @@ void WebWorkerStartThreadHook(blink::Frame* frame, const char* path, std::string
150
150
g_dispatcher->script_context_set ().GetByV8Context (v8_context);
151
151
if (!script_context || !script_context->extension ())
152
152
return ;
153
- root_path = script_context-> extension ()-> path (). AsUTF8Unsafe () ;
153
+ root_path = g_extension_root ;
154
154
} else {
155
155
root_path = *script;
156
156
}
@@ -172,29 +172,43 @@ void WebWorkerStartThreadHook(blink::Frame* frame, const char* path, std::string
172
172
void ContextCreationHook (blink::WebLocalFrame* frame, ScriptContext* context) {
173
173
v8::Isolate* isolate = context->isolate ();
174
174
175
- if (g_extension_id.empty ())
176
- g_extension_id = context->extension ()->id ();
175
+ const Extension* extension = context->extension ();
176
+ std::string extension_root;
177
+
178
+ if (g_extension_id.empty () && extension)
179
+ g_extension_id = extension->id ();
177
180
178
181
bool nodejs_enabled = true ;
179
- context->extension ()->manifest ()->GetBoolean (manifest_keys::kNWJSEnableNode , &nodejs_enabled);
182
+ const base::CommandLine& command_line =
183
+ *base::CommandLine::ForCurrentProcess ();
184
+ bool worker_support = command_line.HasSwitch (switches::kEnableNodeWorker );
185
+
186
+ if (extension) {
187
+ extension->manifest ()->GetBoolean (manifest_keys::kNWJSEnableNode , &nodejs_enabled);
188
+ extension_root = extension->path ().AsUTF8Unsafe ();
189
+ } else {
190
+ extension_root = command_line.GetSwitchValuePath (switches::kNWAppPath ).AsUTF8Unsafe ();
191
+ }
192
+ g_extension_root = extension_root;
180
193
181
194
if (!nodejs_enabled)
182
195
return ;
183
196
184
- const base::CommandLine& command_line =
185
- *base::CommandLine::ForCurrentProcess ();
186
- bool worker_support = command_line.HasSwitch (switches::kEnableNodeWorker );
187
-
188
197
blink::set_web_worker_hooks ((void *)WebWorkerStartThreadHook);
189
198
g_web_worker_thread_new_fn = (VoidPtr2Fn)WebWorkerNewThreadHook;
190
199
if (!g_is_node_initialized_fn ())
191
200
g_setup_nwnode_fn (0 , nullptr , worker_support);
192
201
193
202
bool mixed_context = false ;
194
- context->extension ()->manifest ()->GetBoolean (manifest_keys::kNWJSMixedContext , &mixed_context);
203
+ bool node_init_run = false ;
204
+ bool nwjs_guest = nwjs_guest = command_line.HasSwitch (" nwjs-guest" );
205
+
206
+ if (extension)
207
+ extension->manifest ()->GetBoolean (manifest_keys::kNWJSMixedContext , &mixed_context);
195
208
v8::Local<v8::Context> node_context;
196
209
g_get_node_context_fn (&node_context);
197
210
if (node_context.IsEmpty () || mixed_context) {
211
+ node_init_run = true ;
198
212
{
199
213
int argc = 1 ;
200
214
char argv0[] = " node" ;
@@ -203,7 +217,7 @@ void ContextCreationHook(blink::WebLocalFrame* frame, ScriptContext* context) {
203
217
argv[1 ] = argv[2 ] = nullptr ;
204
218
std::string main_fn;
205
219
206
- if (context-> extension () ->manifest ()->GetString (" node-main" , &main_fn)) {
220
+ if (extension && extension ->manifest ()->GetString (" node-main" , &main_fn)) {
207
221
argc = 2 ;
208
222
argv[1 ] = strdup (main_fn.c_str ());
209
223
}
@@ -231,13 +245,13 @@ void ContextCreationHook(blink::WebLocalFrame* frame, ScriptContext* context) {
231
245
script->Run ();
232
246
}
233
247
234
- if (context-> extension () ->manifest ()->GetString (manifest_keys::kNWJSInternalMainFilename , &main_fn)) {
248
+ if (extension && extension ->manifest ()->GetString (manifest_keys::kNWJSInternalMainFilename , &main_fn)) {
235
249
v8::Local<v8::Script> script = v8::Script::Compile (v8::String::NewFromUtf8 (isolate,
236
250
(" global.__filename = '" + main_fn + " ';" ).c_str ()));
237
251
script->Run ();
238
252
}
239
253
{
240
- std::string root_path = context-> extension ()-> path (). AsUTF8Unsafe () ;
254
+ std::string root_path = extension_root ;
241
255
#if defined(OS_WIN)
242
256
base::ReplaceChars (root_path, " \\ " , " \\\\ " , &root_path);
243
257
#endif
@@ -247,12 +261,12 @@ void ContextCreationHook(blink::WebLocalFrame* frame, ScriptContext* context) {
247
261
script->Run ();
248
262
}
249
263
bool content_verification = false ;
250
- if (context-> extension () ->manifest ()->GetBoolean (manifest_keys::kNWJSContentVerifyFlag ,
264
+ if (extension && extension ->manifest ()->GetBoolean (manifest_keys::kNWJSContentVerifyFlag ,
251
265
&content_verification) && content_verification) {
252
266
v8::Local<v8::Script> script =
253
267
v8::Script::Compile (v8::String::NewFromUtf8 (isolate,
254
268
(std::string (" global.__nwjs_cv = true;" ) +
255
- " global.__nwjs_ext_id = '" + context-> extension () ->id () + " ';" ).c_str ()));
269
+ " global.__nwjs_ext_id = '" + extension->id () + " ';" ).c_str ()));
256
270
257
271
script->Run ();
258
272
}
@@ -287,6 +301,11 @@ void ContextCreationHook(blink::WebLocalFrame* frame, ScriptContext* context) {
287
301
v8::Local<v8::Value> key = symbols->Get (i);
288
302
v8::Local<v8::Value> val = node_global->Get (key);
289
303
nw->Set (key, val);
304
+ if (nwjs_guest && !node_init_run) {
305
+ // running in nwjs webview and node was initialized in
306
+ // chromedriver automation extension
307
+ context->v8_context ()->Global ()->Set (key, val);
308
+ }
290
309
}
291
310
g_context->Exit ();
292
311
@@ -295,7 +314,7 @@ void ContextCreationHook(blink::WebLocalFrame* frame, ScriptContext* context) {
295
314
v8::MicrotasksScope microtasks (isolate, v8::MicrotasksScope::kDoNotRunMicrotasks );
296
315
v8::Context::Scope cscope (context->v8_context ());
297
316
// Make node's relative modules work
298
- std::string root_path = context-> extension ()-> path (). AsUTF8Unsafe () ;
317
+ std::string root_path = extension_root ;
299
318
GURL frame_url = ScriptContext::GetDataSourceURLForFrame (frame);
300
319
std::string url_path = frame_url.path ();
301
320
#if defined(OS_WIN)
@@ -421,8 +440,7 @@ void DocumentElementHook(blink::WebLocalFrame* frame,
421
440
frame->setNodeJS (true );
422
441
std::string path = effective_document_url.path ();
423
442
v8::Local<v8::Context> v8_context = frame->mainWorldScriptContext ();
424
- std::string root_path = extension->path ().AsUTF8Unsafe ();
425
- base::FilePath root (extension->path ());
443
+ std::string root_path = g_extension_root;
426
444
if (!v8_context.IsEmpty ()) {
427
445
v8::MicrotasksScope microtasks (v8::Isolate::GetCurrent (), v8::MicrotasksScope::kDoNotRunMicrotasks );
428
446
v8::Context::Scope cscope (v8_context);
0 commit comments