From 8c0f443c754480c329919f1a8f01998198014d25 Mon Sep 17 00:00:00 2001 From: Kevin Sawicki Date: Thu, 11 Oct 2012 13:07:07 -0700 Subject: [PATCH] Call window.shutdown when native window is closed The root view will now be serialized and saved to local storage when the window is closed or the application is terminated. --- native/atom_application.mm | 5 +++++ native/atom_cef_render_process_handler.h | 3 +-- native/atom_cef_render_process_handler.mm | 15 +++++++++++++++ native/atom_window_controller.mm | 8 +++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/native/atom_application.mm b/native/atom_application.mm index 8eae08035..a081fd726 100644 --- a/native/atom_application.mm +++ b/native/atom_application.mm @@ -168,6 +168,11 @@ } - (void)applicationWillTerminate:(NSNotification *)notification { + NSInteger windowCount = [[self windows] count]; + for(int i = 0; i < windowCount; i++) { + NSWindow *window = [[self windows] objectAtIndex:i]; + [window performClose:0]; + } CefShutdown(); } diff --git a/native/atom_cef_render_process_handler.h b/native/atom_cef_render_process_handler.h index 8c4f37e85..2a12b80dd 100644 --- a/native/atom_cef_render_process_handler.h +++ b/native/atom_cef_render_process_handler.h @@ -13,10 +13,9 @@ class AtomCefRenderProcessHandler : public CefRenderProcessHandler { virtual bool OnProcessMessageReceived(CefRefPtr browser, CefProcessId source_process, CefRefPtr message) OVERRIDE; - - void Reload(CefRefPtr browser); + void Shutdown(CefRefPtr browser); bool CallMessageReceivedHandler(CefRefPtr context, CefRefPtr message); IMPLEMENT_REFCOUNTING(AtomCefRenderProcessHandler); diff --git a/native/atom_cef_render_process_handler.mm b/native/atom_cef_render_process_handler.mm index 220dfaee7..373d45563 100644 --- a/native/atom_cef_render_process_handler.mm +++ b/native/atom_cef_render_process_handler.mm @@ -27,6 +27,10 @@ bool AtomCefRenderProcessHandler::OnProcessMessageReceived(CefRefPtr Reload(browser); return true; } + else if (name == "shutdown") { + Shutdown(browser); + return true; + } else { return CallMessageReceivedHandler(browser->GetMainFrame()->GetV8Context(), message); } @@ -47,6 +51,17 @@ void AtomCefRenderProcessHandler::Reload(CefRefPtr browser) { context->Exit(); } +void AtomCefRenderProcessHandler::Shutdown(CefRefPtr browser) { + CefRefPtr context = browser->GetMainFrame()->GetV8Context(); + CefRefPtr global = context->GetGlobal(); + + context->Enter(); + CefV8ValueList arguments; + CefRefPtr shutdownFunction = global->GetValue("shutdown"); + shutdownFunction->ExecuteFunction(global, arguments); + context->Exit(); +} + bool AtomCefRenderProcessHandler::CallMessageReceivedHandler(CefRefPtr context, CefRefPtr message) { context->Enter(); diff --git a/native/atom_window_controller.mm b/native/atom_window_controller.mm index 2d44b9d33..34ae0caca 100644 --- a/native/atom_window_controller.mm +++ b/native/atom_window_controller.mm @@ -143,8 +143,14 @@ } } -- (BOOL)windowShouldClose:(id)window { +- (void)windowWillClose:(NSNotification *)notification { + if (_cefClient && _cefClient->GetBrowser()) { + _cefClient->GetBrowser()->SendProcessMessage(PID_RENDERER, CefProcessMessage::Create("shutdown")); + } [self autorelease]; +} + +- (BOOL)windowShouldClose:(id)window { return YES; }