Skip to content

Commit 9ca8816

Browse files
committed
"exit" should be emited for process on exit. Fix nwjs#140.
1 parent 619dbeb commit 9ca8816

6 files changed

Lines changed: 41 additions & 13 deletions

File tree

src/api/api_messages.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,3 +77,6 @@ IPC_MESSAGE_ROUTED1(ShellViewHostMsg_UpdateDraggableRegions,
7777
// The browser want to open a file.
7878
IPC_MESSAGE_CONTROL1(ShellViewMsg_Open,
7979
std::string /* file name */)
80+
81+
// Browser will quit.
82+
IPC_MESSAGE_CONTROL0(ShellViewMsg_WillQuit)

src/api/app/app.cc

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,30 @@
3131
#include "content/public/browser/render_process_host.h"
3232

3333
namespace api {
34+
35+
namespace {
36+
37+
// Get render process host.
38+
content::RenderProcessHost* GetRenderProcessHost() {
39+
content::RenderProcessHost* render_process_host = NULL;
40+
std::vector<content::Shell*> windows = content::Shell::windows();
41+
for (size_t i = 0; i < windows.size(); ++i) {
42+
if (!windows[i]->is_devtools()) {
43+
render_process_host = windows[i]->web_contents()->GetRenderProcessHost();
44+
break;
45+
}
46+
}
47+
48+
return render_process_host;
49+
}
50+
51+
} // namespace
3452

3553
// static
3654
void App::Call(const std::string& method,
3755
const base::ListValue& arguments) {
3856
if (method == "Quit") {
39-
Quit();
57+
Quit(GetRenderProcessHost());
4058
return;
4159
} else if (method == "CloseAllWindows") {
4260
CloseAllWindows();
@@ -82,23 +100,20 @@ void App::CloseAllWindows() {
82100
}
83101

84102
// static
85-
void App::Quit() {
103+
void App::Quit(content::RenderProcessHost* render_process_host) {
104+
// Send the quit message.
105+
render_process_host->Send(new ShellViewMsg_WillQuit());
106+
107+
// Then quit.
86108
MessageLoop::current()->PostTask(FROM_HERE, MessageLoop::QuitClosure());
87109
}
88110

89111
// static
90112
void App::EmitOpenEvent(const std::string& path) {
91113
// Get the app's renderer process.
92-
content::RenderProcessHost* render_process_host = NULL;
93-
std::vector<content::Shell*> windows = content::Shell::windows();
94-
for (size_t i = 0; i < windows.size(); ++i) {
95-
if (!windows[i]->is_devtools()) {
96-
render_process_host = windows[i]->web_contents()->GetRenderProcessHost();
97-
break;
98-
}
99-
}
100-
114+
content::RenderProcessHost* render_process_host = GetRenderProcessHost();
101115
DCHECK(render_process_host != NULL);
116+
102117
render_process_host->Send(new ShellViewMsg_Open(path));
103118
}
104119

src/api/app/app.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class ListValue;
3030
}
3131

3232
namespace content {
33+
class RenderProcessHost;
3334
class Shell;
3435
}
3536

@@ -49,7 +50,7 @@ class App {
4950
static void CloseAllWindows();
5051

5152
// Quit the whole app.
52-
static void Quit();
53+
static void Quit(content::RenderProcessHost* render_view_host);
5354

5455
// Post "open" event.
5556
static void EmitOpenEvent(const std::string& path);

src/nw_shell.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ Shell::~Shell() {
113113
}
114114

115115
if (windows_.empty() && quit_message_loop_)
116-
api::App::Quit();
116+
api::App::Quit(web_contents()->GetRenderProcessHost());
117117
}
118118

119119
void Shell::SendEvent(const std::string& event) {

src/renderer/shell_render_process_observer.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "content/nw/src/api/dispatcher_bindings.h"
2626
#include "webkit/glue/webkit_glue.h"
2727
#include "webkit/support/gc_extension.h"
28+
#include "third_party/node/src/node.h"
2829
#include "third_party/node/src/req_wrap.h"
2930
#include "v8/include/v8.h"
3031

@@ -42,6 +43,7 @@ bool ShellRenderProcessObserver::OnControlMessageReceived(
4243
bool handled = true;
4344
IPC_BEGIN_MESSAGE_MAP(ShellRenderProcessObserver, message)
4445
IPC_MESSAGE_HANDLER(ShellViewMsg_Open, OnOpen)
46+
IPC_MESSAGE_HANDLER(ShellViewMsg_WillQuit, OnWillQuit)
4547
IPC_MESSAGE_UNHANDLED(handled = false)
4648
IPC_END_MESSAGE_MAP()
4749

@@ -76,4 +78,10 @@ void ShellRenderProcessObserver::OnOpen(const std::string& path) {
7678
}
7779
}
7880

81+
void ShellRenderProcessObserver::OnWillQuit() {
82+
// process.emit('exit');
83+
node::EmitExit(node::process);
84+
node::RunAtExit();
85+
}
86+
7987
} // namespace content

src/renderer/shell_render_process_observer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class ShellRenderProcessObserver : public RenderProcessObserver {
4141

4242
private:
4343
void OnOpen(const std::string& path);
44+
void OnWillQuit();
4445

4546
DISALLOW_COPY_AND_ASSIGN(ShellRenderProcessObserver);
4647
};

0 commit comments

Comments
 (0)