Waiting for file to be closed in spawned browser - browser

I invoke a instance of a browser like this:
if (bResult)
{
DWORD dwResult;
strProgram.Format(_T("%s \"%s\""), (LPCTSTR)it->strPath, (LPCTSTR)m_strTempPreviewHTML);
theApp.ExecuteProgram(strProgram, dwResult);
}
For normal applications my main executable will wait until the process has finished. But this is not so with a browser.
Is it possible to wait until the file has been closed in the spawned browser?

Related

Dart don't buffer process stdout (tell process that it is running from a terminal)

I'm trying to run a binary file width Process.start in dart and redirect its output the stdout and pipe stdin to the process.
Here is my code:
import 'dart:io';
import 'dart:convert';
main() async {
Process.start('./test.bin', []).then((p) {
p.stdout.listen((bytes) {
stdout.add(bytes);
stdout.flush();
});
stdin.pipe(p.stdin);
});
}
The problem is that it only flushes the process's stdout after the process terminates.
After digging around on the internet I found this: "This is caused by libc which by default puts stdout int buffered mode when it is not connected to a terminal."
How could I tell the process that it is running from a terminal?
My goal with this project is to have a terminal in a webapp that interacts with this process running on the backend, so it will be running on a terminal but the process is obviously unaware of that.
I'm answering my own question in case anyone finds this post.
I ended up writing the backend in nodejs with the help of this library (https://www.npmjs.com/package/node-pty)
But I found a similar library for dart as well (https://pub.dev/packages/pty)

what's the difference difference between phantom.exit and .close in phantom.js?

If I'm doing web scraping using phantom.js to click some buttons and links what's the convenient one to terminate program with ?
http://phantomjs.org/api/webpage/method/close.html
http://phantomjs.org/api/phantom/method/exit.html
PhantomJS .exit() is a core method of phantom Object and you can use it to quit the phantomjs program instance (like Nodejs process.exit).
This is the convenient method to terminate your program.
console.log('Quitting Phantomjs');
phantom.exit();
.close() method is part of Web Page Module and is used to close a web page.
var webPage = require('webpage');
var page = webPage.create();
page.open('http://github.com', function (status) {
if (status === "success") {
// do stuff
page.close();
}
})
Short answer: .close() clears memory, .exit() may not.
.close() belongs to phantomjs' webpage.
.exit() belongs to phantomjs process itself.
If you do a lot of automated tasks, opening pages and not closing them -
this can cause memory leak.
So preferred way to terminate is to 1. close the page and 2. exit phantom after that.

How to send "CTRL+C" to child process in Node.js?

I tried to spawn child process - vvp (https://linux.die.net/man/1/vvp). At the certain time, I need to send CTRL+C to that process.
I am expecting that simulation will be interrupted and I get the interactive prompt. And after that I can continue the simulation by send command to the child process.
So, I tried something like this:
var child = require('child_process');
var fs = require('fs');
var vcdGen = child.spawn('vvp', ['qqq'], {});
vcdGen.stdout.on('data', function(data) {
console.log(data.toString())
});
setTimeout(function() {
vcdGen.kill('SIGINT');
}, 400);
In that case, a child process was stopped.
I also tried vcdGen.stdin.write('\x03') instead of vcdGen.kill('SIGINT'); but it isn't work.
Maybe it's because of Windows?
Is there any way to achieve the same behaviour as I got in cmd?
kill only really supports a rude process kill on Windows - the application signal model in Windows and *nix isn't compatible. You can't pass Ctrl+C through standard input, because it never comes through standard input - it's a function of the console subsystem (and thus you can only use it if the process has an attached console). It creates a new thread in the child process to do its work.
There's no supported way to do this programmatically. It's a feature for the user, not the applications. The only way to do this would be to do the same thing the console subsystem does - create a new thread in the target application and let it do the signalling. But the best way would be to simply use coöperative signalling instead - though that of course requires you to change the target application to understand the signal.
If you want to go the entirely unsupported route, have a look at https://stackoverflow.com/a/1179124/3032289.
If you want to find a middle ground, there's a way to send a signal to yourself, of course. Which also means that you can send Ctrl+C to a process if your consoles are attached. Needless to say, this is very tricky - you'd probably want to create a native host process that does nothing but create a console and run the actual program you want to run. Your host process would then listen for an event, and when the event is signalled, call GenerateConsoleCtrlEvent.

issue with fork() from a firebreath npapi plugin

I am trying to fork() a new process so that I can call a separate console application.
The fork does happen fine and I get a new process id but the process is in sleeping state and does not get active at all even if the browser exits.
I just took the sample plugin project and modified the echo method to do the fork.
A regular console application works fine with the fork code.
Is there something different that has to be taken into account for a firebreath plugin app?
Can someone suggest what might be the issue?
The platform is archlinux 64 bit.
FB::variant PluginTestVZAPI::echo(const FB::variant& msg)
{
static int n(0);
fire_echo("So far, you clicked this many times: ", n++);
// fork
pid_t pid = fork();
if(pid == 0) // Child
{
m_host->htmlLog("child process");
}
else if (pid < 0) // Failed to fork
{
m_host->htmlLog("Failed to fork");
m_host->htmlLog(boost::lexical_cast<std::string>(pid));
}
else // Parent
{
m_host->htmlLog("Parent process");
}
m_host->htmlLog("Child Process PID = " + boost::lexical_cast<std::string>(pid));
// end fork
// return "foobar";
return msg;
}
I can't be certain but if I were you I'd try removing the htmlLog calls -- there is no way for you to access the DOM from the child process, so htmlLog won't work at all and it is quite possible that trying to use it in a forked process is causing it to go into an inactive state while it tries (unsuccessfully) to communicate with a browser process that doesn't know about it.
I don't know for certain if this can work or not, but I'd be more than a bit nervous about forking a process that is already a child process of something else; the browser owns the plugin process and communicates with it via IPC, so if you fork that process there could be a lot of code that you don't know about still running and trying to talk to the browser through a now-defunct IPC connection.
My recommendation would be to launch a seperate process, but that's just me. At the very least, you absolutely cannot use anything FireBreath provides for communicating with the browser from the child process.

How to keep Node uv_run from exiting when hosting own thread in an add on?

I have a custom server that runs in its own posix thread in a native Node Add On.
What is the proper way to keep the node process running the uv_run event loop? In other words, if I start the server in my Add On via a script, my process will exit at the end of the script instead of keeping the event loop running.
I've tried adding a SignalWatcher via process.on and that still exits. I didn't see anything else in the process object for doing this from script.
In node.cc, there is this comment:
// Create all the objects, load modules, do everything.
// so your next reading stop should be node::Load()!
Load(process_l);
// All our arguments are loaded. We've evaluated all of the scripts. We
// might even have created TCP servers. Now we enter the main eventloop. If
// there are no watchers on the loop (except for the ones that were
// uv_unref'd) then this function exits. As long as there are active
// watchers, it blocks.
uv_run(uv_default_loop());
EmitExit(process_l);
What does the Add On have to do?
I've tried calling uv_ref(uv_default_loop()) in the main thread in my Add On when starting the server/pthread but the process still exits.
Note: I can bind to a TCP/UDP port or set a timer and that will keep uv_run from exiting, but I would like to do this the "correct" way.

Resources