Skip to content

Commit b36c602

Browse files
committed
Merge pull request nwjs#1053 from ywkim/app-reopen-event
[Mac] Add app 'reopen' event
2 parents 0b6e407 + 290b59d commit b36c602

File tree

7 files changed

+49
-0
lines changed

7 files changed

+49
-0
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ Zhang Chaobin <zhchbin@gmail.com>
1919
Michael Morrison <mmorrison@wootalyzer.com>
2020
William Towe <willbur1984@gmail.com>
2121
Toni Lähdekorpi <toni@lygon.net>
22+
Youngwook Kim <youngwook.kim@gmail.com>

src/api/api_messages.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ IPC_MESSAGE_ROUTED1(ShellViewHostMsg_UpdateDraggableRegions,
9292
IPC_MESSAGE_CONTROL1(ShellViewMsg_Open,
9393
std::string /* file name */)
9494

95+
// Tell browser we have to reopen.
96+
IPC_MESSAGE_CONTROL0(ShellViewMsg_Reopen)
97+
9598
// clear cache on the renderer side
9699
IPC_MESSAGE_CONTROL0(ShellViewMsg_ClearCache)
97100

src/api/app/app.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,20 @@ void App::EmitOpenEvent(const std::string& path) {
186186
}
187187
}
188188

189+
// static
190+
void App::EmitReopenEvent() {
191+
std::set<RenderProcessHost*> rphs;
192+
std::set<RenderProcessHost*>::iterator it;
193+
194+
GetRenderProcessHosts(rphs);
195+
for (it = rphs.begin(); it != rphs.end(); it++) {
196+
RenderProcessHost* rph = *it;
197+
DCHECK(rph != NULL);
198+
199+
rph->Send(new ShellViewMsg_Reopen());
200+
}
201+
}
202+
189203
void App::ClearCache(content::RenderProcessHost* render_process_host) {
190204
render_process_host->Send(new ShellViewMsg_ClearCache());
191205
nw::RemoveHttpDiskCache(render_process_host->GetBrowserContext(),

src/api/app/app.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class App {
5555
// Post "open" event.
5656
static void EmitOpenEvent(const std::string& path);
5757

58+
// Post "reopen" event.
59+
// (This event is received when the user clicked the icon in the Dock).
60+
static void EmitReopenEvent();
61+
5862
static void ClearCache(content::RenderProcessHost* render_view_host);
5963
private:
6064
App();

src/browser/app_controller_mac.mm

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,10 @@ - (void) applicationDidFinishLaunching: (NSNotification *) note {
6969
standard_menus.BuildWindowMenu();
7070
}
7171

72+
- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication
73+
hasVisibleWindows:(BOOL)flag {
74+
api::App::EmitReopenEvent();
75+
return YES;
76+
}
77+
7278
@end

src/renderer/shell_render_process_observer.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ bool ShellRenderProcessObserver::OnControlMessageReceived(
5050
bool handled = true;
5151
IPC_BEGIN_MESSAGE_MAP(ShellRenderProcessObserver, message)
5252
IPC_MESSAGE_HANDLER(ShellViewMsg_Open, OnOpen)
53+
IPC_MESSAGE_HANDLER(ShellViewMsg_Reopen, OnReopen)
5354
IPC_MESSAGE_HANDLER(ShellViewMsg_ClearCache, OnClearCache)
5455
IPC_MESSAGE_UNHANDLED(handled = false)
5556
IPC_END_MESSAGE_MAP()
@@ -88,6 +89,25 @@ void ShellRenderProcessObserver::OnOpen(const std::string& path) {
8889
}
8990
}
9091

92+
void ShellRenderProcessObserver::OnReopen() {
93+
v8::HandleScope handle_scope;
94+
95+
// the App object is stored in process["_nw_app"].
96+
v8::Local<v8::Object> process = node::g_context->Global()->Get(
97+
node::process_symbol)->ToObject();
98+
v8::Local<v8::String> app_symbol = v8::String::NewSymbol("_nw_app");
99+
if (process->Has(app_symbol)) {
100+
// process["_nw_app"].emit(path).
101+
v8::Local<v8::Object> app = process->Get(app_symbol)->ToObject();
102+
v8::Local<v8::Function> emit = v8::Local<v8::Function>::Cast(
103+
app->Get(v8::String::New("emit")));
104+
v8::Local<v8::Value> argv[] = {
105+
v8::String::New("reopen")
106+
};
107+
emit->Call(app, 1, argv);
108+
}
109+
}
110+
91111
void ShellRenderProcessObserver::OnClearCache() {
92112
if (webkit_initialized_)
93113
WebKit::WebCache::clear();

src/renderer/shell_render_process_observer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ class ShellRenderProcessObserver : public RenderProcessObserver {
4040
virtual void WebKitInitialized() OVERRIDE;
4141
private:
4242
void OnOpen(const std::string& path);
43+
void OnReopen();
4344
void OnClearCache();
4445

4546
bool webkit_initialized_;

0 commit comments

Comments
 (0)