diff --git a/atom/browser/api/atom_api_app.cc b/atom/browser/api/atom_api_app.cc index 0c3623583c62..81ec3fefd510 100644 --- a/atom/browser/api/atom_api_app.cc +++ b/atom/browser/api/atom_api_app.cc @@ -180,8 +180,8 @@ void App::OnWindowAllClosed() { Emit("window-all-closed"); } -void App::OnQuit() { - Emit("quit"); +void App::OnQuit(const int code) { + Emit("quit", code); if (process_singleton_.get()) { process_singleton_->Cleanup(); @@ -344,7 +344,7 @@ mate::ObjectTemplateBuilder App::GetObjectTemplateBuilder( auto browser = base::Unretained(Browser::Get()); return mate::ObjectTemplateBuilder(isolate) .SetMethod("quit", base::Bind(&Browser::Quit, browser)) - .SetMethod("_exit", base::Bind(&Browser::Exit, browser)) + .SetMethod("exit", base::Bind(&Browser::Exit, browser)) .SetMethod("focus", base::Bind(&Browser::Focus, browser)) .SetMethod("getVersion", base::Bind(&Browser::GetVersion, browser)) .SetMethod("setVersion", base::Bind(&Browser::SetVersion, browser)) diff --git a/atom/browser/api/atom_api_app.h b/atom/browser/api/atom_api_app.h index a6f99d65e0af..f04a19e67cdf 100644 --- a/atom/browser/api/atom_api_app.h +++ b/atom/browser/api/atom_api_app.h @@ -42,7 +42,7 @@ class App : public AtomBrowserClient::Delegate, void OnBeforeQuit(bool* prevent_default) override; void OnWillQuit(bool* prevent_default) override; void OnWindowAllClosed() override; - void OnQuit() override; + void OnQuit(int code) override; void OnOpenFile(bool* prevent_default, const std::string& file_path) override; void OnOpenURL(const std::string& url) override; void OnActivate(bool has_visible_windows) override; diff --git a/atom/browser/api/lib/app.coffee b/atom/browser/api/lib/app.coffee index fc0a78298c4d..9a7fc6e71141 100644 --- a/atom/browser/api/lib/app.coffee +++ b/atom/browser/api/lib/app.coffee @@ -34,17 +34,12 @@ app.setAppPath = (path) -> app.getAppPath = -> appPath -appExitCode = undefined -app.exit = (exitCode) -> - appExitCode = exitCode - app._exit(exitCode) - # Map process.exit to app.exit, which quits gracefully. process.exit = app.exit # Emit a process 'exit' event on app quit. -app.on 'quit', -> - process.emit 'exit', appExitCode +app.on 'quit', (event, exitCode) -> + process.emit 'exit', exitCode # Routes the events to webContents. for name in ['login', 'certificate-error', 'select-client-certificate'] diff --git a/atom/browser/atom_browser_main_parts.cc b/atom/browser/atom_browser_main_parts.cc index fd72f5e4ae9a..eadd52ac44c8 100644 --- a/atom/browser/atom_browser_main_parts.cc +++ b/atom/browser/atom_browser_main_parts.cc @@ -61,6 +61,10 @@ bool AtomBrowserMainParts::SetExitCode(int code) { return true; } +int AtomBrowserMainParts::GetExitCode() { + return exit_code_ != nullptr ? *exit_code_ : 0; +} + base::Closure AtomBrowserMainParts::RegisterDestructionCallback( const base::Closure& callback) { auto iter = destructors_.insert(destructors_.end(), callback); diff --git a/atom/browser/atom_browser_main_parts.h b/atom/browser/atom_browser_main_parts.h index fbc59f7f811d..c1c0c89c6786 100644 --- a/atom/browser/atom_browser_main_parts.h +++ b/atom/browser/atom_browser_main_parts.h @@ -34,6 +34,9 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { // Sets the exit code, will fail if the the message loop is not ready. bool SetExitCode(int code); + // Gets the exit code + int GetExitCode(); + // Register a callback that should be destroyed before JavaScript environment // gets destroyed. // Returns a closure that can be used to remove |callback| from the list. diff --git a/atom/browser/browser.cc b/atom/browser/browser.cc index c77f359760c9..ee00efa07110 100644 --- a/atom/browser/browser.cc +++ b/atom/browser/browser.cc @@ -72,7 +72,8 @@ void Browser::Shutdown() { is_shutdown_ = true; is_quiting_ = true; - FOR_EACH_OBSERVER(BrowserObserver, observers_, OnQuit()); + int exitCode = AtomBrowserMainParts::Get()->GetExitCode(); + FOR_EACH_OBSERVER(BrowserObserver, observers_, OnQuit(exitCode)); if (base::MessageLoop::current()) { base::MessageLoop::current()->PostTask( diff --git a/atom/browser/browser_observer.h b/atom/browser/browser_observer.h index f6d76bc13fb3..f3ae2e364de4 100644 --- a/atom/browser/browser_observer.h +++ b/atom/browser/browser_observer.h @@ -24,7 +24,7 @@ class BrowserObserver { virtual void OnWindowAllClosed() {} // The browser is quitting. - virtual void OnQuit() {} + virtual void OnQuit(const int code) {} // The browser has opened a file by double clicking in Finder or dragging the // file to the Dock icon. (OS X only) diff --git a/spec/api-app-spec.coffee b/spec/api-app-spec.coffee index adb64d363eb1..7b7e5fa483df 100644 --- a/spec/api-app-spec.coffee +++ b/spec/api-app-spec.coffee @@ -44,6 +44,7 @@ describe 'app module', -> output = '' appProcess.stdout.on 'data', (data) -> output += data appProcess.on 'close', (code) -> + console.log output assert.notEqual output.indexOf('Exit event with code: 123'), -1 assert.equal code, 123 done() diff --git a/spec/fixtures/api/quit-app/main.js b/spec/fixtures/api/quit-app/main.js index 4bdeb93a5e30..85939acd7348 100644 --- a/spec/fixtures/api/quit-app/main.js +++ b/spec/fixtures/api/quit-app/main.js @@ -5,5 +5,5 @@ app.on('ready', function () { }) process.on('exit', function (code) { - console.log('Exit event with code: ' + code) + console.log('Exit event with code: ' + JSON.stringify(code, null, 2)) }) diff --git a/spec/static/index.html b/spec/static/index.html index ea86f6ee302d..2053ca41355c 100644 --- a/spec/static/index.html +++ b/spec/static/index.html @@ -56,6 +56,7 @@ mocha.ui('bdd').reporter(isCi ? 'tap' : 'html'); var query = Mocha.utils.parseQuery(window.location.search || ''); + query.grep = 'app.exit' if (query.grep) mocha.grep(query.grep); if (query.invert) mocha.invert();