browser(firefox): close browser when pipe disconnects (#4437)

This commit is contained in:
Dmitry Gozman 2020-11-13 14:51:40 -08:00 committed by GitHub
parent 9404d2abb6
commit 2e65f78874
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 32 additions and 9 deletions

View File

@ -1,2 +1,2 @@
1206
Changed: yurys@chromium.org Thu 12 Nov 2020 10:23:24 AM PST
1207
Changed: dgozman@gmail.com Fri Nov 13 14:41:15 PST 2020

View File

@ -79,6 +79,8 @@ CommandLineHandler.prototype = {
loadFrameScript();
dump(`Juggler listening on ws://127.0.0.1:${this._server.port}/${token}\n`);
} else if (jugglerPipeFlag) {
let browserHandler;
let pipeStopped = false;
const pipe = Cc['@mozilla.org/juggler/remotedebuggingpipe;1'].getService(Ci.nsIRemoteDebuggingPipe);
const connection = {
QueryInterface: ChromeUtils.generateQI([Ci.nsIRemoteDebuggingPipeClient]),
@ -86,20 +88,28 @@ CommandLineHandler.prototype = {
if (this.onmessage)
this.onmessage({ data: message });
},
disconnected() {
if (browserHandler)
browserHandler['Browser.close']();
},
send(message) {
if (pipeStopped) {
// We are missing the response to Browser.close,
// but everything works fine. Once we actually need it,
// we have to stop the pipe after the response is sent.
return;
}
pipe.sendMessage(message);
},
};
pipe.init(connection);
const dispatcher = new Dispatcher(connection);
const browserHandler = new BrowserHandler(dispatcher.rootSession(), dispatcher, targetRegistry, () => {
browserHandler = new BrowserHandler(dispatcher.rootSession(), dispatcher, targetRegistry, () => {
if (silent)
Services.startup.exitLastWindowClosingSurvivalArea();
// Send response to the Browser.close, and then stop in the next microtask.
Promise.resolve().then(() => {
connection.onclose();
pipe.stop();
});
connection.onclose();
pipe.stop();
pipeStopped = true;
});
dispatcher.rootSession().setHandler(browserHandler);
loadFrameScript();

View File

@ -8,6 +8,7 @@
interface nsIRemoteDebuggingPipeClient : nsISupports
{
void receiveMessage(in AString message);
void disconnected();
};
[scriptable, uuid(b7bfb66b-fd46-4aa2-b4ad-396177186d94)]

View File

@ -151,8 +151,13 @@ void nsRemoteDebuggingPipe::ReaderLoop() {
std::vector<char> line;
while (!m_terminated) {
size_t size = ReadBytes(buffer.data(), bufSize, false);
if (!size)
if (!size) {
nsCOMPtr<nsIRunnable> runnable = NewRunnableMethod<>(
"nsRemoteDebuggingPipe::Disconnected",
this, &nsRemoteDebuggingPipe::Disconnected);
NS_DispatchToMainThread(runnable.forget());
break;
}
size_t start = 0;
size_t end = line.size();
line.insert(line.end(), buffer.begin(), buffer.begin() + size);
@ -191,6 +196,12 @@ void nsRemoteDebuggingPipe::ReceiveMessage(const nsCString& aMessage) {
}
}
void nsRemoteDebuggingPipe::Disconnected() {
MOZ_RELEASE_ASSERT(NS_IsMainThread(), "Remote debugging pipe must be used on the Main thread.");
if (mClient)
mClient->Disconnected();
}
nsresult nsRemoteDebuggingPipe::SendMessage(const nsAString& aMessage) {
MOZ_RELEASE_ASSERT(NS_IsMainThread(), "Remote debugging pipe must be used on the Main thread.");
if (!mClient) {

View File

@ -22,6 +22,7 @@ class nsRemoteDebuggingPipe final : public nsIRemoteDebuggingPipe {
private:
void ReaderLoop();
void ReceiveMessage(const nsCString& aMessage);
void Disconnected();
~nsRemoteDebuggingPipe();
RefPtr<nsIRemoteDebuggingPipeClient> mClient;