Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

setTimeout not run in webview's Node vm #5622

Closed
JesonRondo opened this issue Jan 10, 2017 · 7 comments
Closed

setTimeout not run in webview's Node vm #5622

JesonRondo opened this issue Jan 10, 2017 · 7 comments
Assignees

Comments

@JesonRondo
Copy link

nw ver 0.19.5

It doesn't work when i use setTimeout in a sandbox with node vm module, in webview.

But it works in the normal page.

here is the key code

// index.html
<webview id="sandbox" allownw src="about:blank"></webview>
<script>
  const sandbox = document.getElementById('sandbox')
  setTimeout(function() {
    sandbox.showDevTools(true)
    sandbox.setAttribute('src', 'http://127.0.0.1:8080/sandbox.html')
  }, 1000)
</script>

<script>
  const vm = require('vm')
  vm.runInNewContext('setTimeout(function(){console.log("outer sandbox")}, 10)', {
    console: console,
    setTimeout: setTimeout,
  })
  // can console log outer sandbox
</script>
// sandbox.html
<script>
  const vm = require('vm')
  vm.runInNewContext('setTimeout(function(){console.log("in sandbox")}, 10)', {
    console: console,
    setTimeout: setTimeout,
  })
  // can not console log in sandbox
</script>
@rogerwang rogerwang self-assigned this Jan 10, 2017
@rogerwang
Copy link
Member

The timeout callback was fired, but the function call is blocked by Blink because it was defined with the previous window context before navigation thus invalid.

The fix will be available in the next nightly build.

@rogerwang
Copy link
Member

@RohitSharma27
Copy link

From his comments, it looks like the final setTimeout isn't working, which will only come into the picture after navigation.

@rogerwang
Copy link
Member

@RohitSharma27 It's the Node context which is initialized in the previous window. Then when vm.runInNewContext() is called after navigation, it's executed in the Node context.

@RohitSharma27
Copy link

So, when we say that webview runs in a separate process, does it mean that it will have it's own node process or will it have it's own separate node context running in the background process or will it share the node context with already created one inside the background page?
In case it shares it with previously created node context then I don't see the problem. And, if a new node context is created for the webview then, how come it was initialized in the previous window. I mean this script will only be executed when the new node context is already in place (since this script is inside the sandbox.html which is rendered in the webview).
I am sorry. I am just trying to make sense here. There must be something that I am missing.

@rogerwang
Copy link
Member

Node always runs in the same (renderer) process with DOM. So as webview runs in an additional separate process, there is a Node running inside of it.

In OP's sample, the webview is loaded with 'about:blank' first, then it was navigated to the remote url. Before the fix, Node was initialized within the previous context (about:blank), after the navigation, it's still there and reused.

@RohitSharma27
Copy link

@rogerwang This totally makes sense now. Thanks for the efforts.

wanghongjuan added a commit to wanghongjuan/nw.js that referenced this issue Apr 19, 2018
- Add test for issue nwjs#5622
- This test is failed on v0.19.5, passed on v0.30.0
wanghongjuan added a commit to wanghongjuan/nw.js that referenced this issue Apr 19, 2018
- Add test for issue nwjs#5622
- This test is failed on v0.19.5, passed on v0.30.0
@wanghongjuan wanghongjuan self-assigned this Apr 19, 2018
wanghongjuan added a commit to wanghongjuan/nw.js that referenced this issue Apr 26, 2018
- Add test for issue nwjs#5622
- This test is failed on v0.19.5, passed on v0.30.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants