From 4133fc28d9f32154af7d7f35a5e75f0be545a801 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Thu, 30 May 2013 19:12:14 +0800 Subject: [PATCH] Emit 'finish-launching' event when the application has finished launching. --- browser/api/atom_api_app.cc | 4 ++++ browser/api/atom_api_app.h | 1 + browser/atom_application_delegate_mac.mm | 8 +++++++- browser/atom_application_mac.h | 2 -- browser/atom_application_mac.mm | 6 ------ browser/atom_browser_main_parts.cc | 5 +++++ browser/atom_browser_main_parts_mac.mm | 2 +- browser/browser.cc | 4 ++++ browser/browser.h | 3 +++ browser/browser_observer.h | 3 +++ 10 files changed, 28 insertions(+), 10 deletions(-) diff --git a/browser/api/atom_api_app.cc b/browser/api/atom_api_app.cc index 6770dcc4a5..81a96a6982 100644 --- a/browser/api/atom_api_app.cc +++ b/browser/api/atom_api_app.cc @@ -36,6 +36,10 @@ void App::OnOpenFile(bool* prevent_default, const std::string& file_path) { *prevent_default = Emit("open-file", &args); } +void App::OnFinishLaunching() { + Emit("finish-launching"); +} + // static v8::Handle App::New(const v8::Arguments &args) { v8::HandleScope scope; diff --git a/browser/api/atom_api_app.h b/browser/api/atom_api_app.h index 0ee6d68c45..3c174b465c 100644 --- a/browser/api/atom_api_app.h +++ b/browser/api/atom_api_app.h @@ -28,6 +28,7 @@ class App : public EventEmitter, virtual void OnWindowAllClosed() OVERRIDE; virtual void OnOpenFile(bool* prevent_default, const std::string& file_path) OVERRIDE; + virtual void OnFinishLaunching() OVERRIDE; private: static v8::Handle New(const v8::Arguments &args); diff --git a/browser/atom_application_delegate_mac.mm b/browser/atom_application_delegate_mac.mm index 2a18400898..16b8da72be 100644 --- a/browser/atom_application_delegate_mac.mm +++ b/browser/atom_application_delegate_mac.mm @@ -4,21 +4,27 @@ #import "browser/atom_application_delegate_mac.h" +#include "base/strings/sys_string_conversions.h" #import "browser/atom_application_mac.h" +#include "browser/browser.h" @implementation AtomApplicationDelegate - (void)applicationDidFinishLaunching:(NSNotification*)notify { + // Trap the quit message to handleQuitEvent. NSAppleEventManager* em = [NSAppleEventManager sharedAppleEventManager]; [em setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication]; + + atom::Browser::Get()->DidFinishLaunching(); } - (BOOL)application:(NSApplication*)sender openFile:(NSString*)filename { - return [[AtomApplication sharedApplication] openFile:filename]; + std::string filename_str(base::SysNSStringToUTF8(filename)); + return atom::Browser::Get()->OpenFile(filename_str) ? YES : NO; } - (void)handleQuitEvent:(NSAppleEventDescriptor*)event diff --git a/browser/atom_application_mac.h b/browser/atom_application_mac.h index 521ac9ef64..ce57df510c 100644 --- a/browser/atom_application_mac.h +++ b/browser/atom_application_mac.h @@ -18,8 +18,6 @@ // CrAppControlProtocol: - (void)setHandlingSendEvent:(BOOL)handlingSendEvent; -- (BOOL)openFile:(NSString*)file; - - (IBAction)closeAllWindows:(id)sender; @end diff --git a/browser/atom_application_mac.mm b/browser/atom_application_mac.mm index 204f8d822e..3fc91c8102 100644 --- a/browser/atom_application_mac.mm +++ b/browser/atom_application_mac.mm @@ -5,7 +5,6 @@ #import "browser/atom_application_mac.h" #include "base/auto_reset.h" -#include "base/strings/sys_string_conversions.h" #include "browser/browser.h" @implementation AtomApplication @@ -27,11 +26,6 @@ handlingSendEvent_ = handlingSendEvent; } -- (BOOL)openFile:(NSString*)filename { - std::string filename_str(base::SysNSStringToUTF8(filename)); - return atom::Browser::Get()->OpenFile(filename_str) ? YES : NO; -} - - (IBAction)closeAllWindows:(id)sender { atom::Browser::Get()->Quit(); } diff --git a/browser/atom_browser_main_parts.cc b/browser/atom_browser_main_parts.cc index 58d47bc72c..f2fdfc2077 100644 --- a/browser/atom_browser_main_parts.cc +++ b/browser/atom_browser_main_parts.cc @@ -69,6 +69,11 @@ void AtomBrowserMainParts::PreMainMessageLoopRun() { } node_bindings_->RunMessageLoop(); + +#if !defined(OS_MACOSX) + // The corresponding call in OS X is in AtomApplicationDelegate. + Browser::Get()->DidFinishLaunching(); +#endif } } // namespace atom diff --git a/browser/atom_browser_main_parts_mac.mm b/browser/atom_browser_main_parts_mac.mm index 50eb14cd6d..1d4a189187 100644 --- a/browser/atom_browser_main_parts_mac.mm +++ b/browser/atom_browser_main_parts_mac.mm @@ -26,7 +26,7 @@ void AtomBrowserMainParts::PreMainMessageLoopStart() { } void AtomBrowserMainParts::PostDestroyThreads() { - [[[AtomApplication sharedApplication] delegate] release]; + [[AtomApplication sharedApplication] setDelegate:nil]; } } // namespace atom diff --git a/browser/browser.cc b/browser/browser.cc index 4df65b07dd..e2327effad 100644 --- a/browser/browser.cc +++ b/browser/browser.cc @@ -41,6 +41,10 @@ bool Browser::OpenFile(const std::string& file_path) { return prevent_default; } +void Browser::DidFinishLaunching() { + FOR_EACH_OBSERVER(BrowserObserver, observers_, OnFinishLaunching()); +} + void Browser::NotifyAndTerminate() { bool prevent_default = false; FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillQuit(&prevent_default)); diff --git a/browser/browser.h b/browser/browser.h index f0bc48744e..4f76632568 100644 --- a/browser/browser.h +++ b/browser/browser.h @@ -30,6 +30,9 @@ class Browser : public WindowListObserver { // Tell the application to open a file. bool OpenFile(const std::string& file_path); + // Tell the application the loading has been done. + void DidFinishLaunching(); + void AddObserver(BrowserObserver* obs) { observers_.AddObserver(obs); } diff --git a/browser/browser_observer.h b/browser/browser_observer.h index 49c3ea8f6e..690d5463bb 100644 --- a/browser/browser_observer.h +++ b/browser/browser_observer.h @@ -23,6 +23,9 @@ class BrowserObserver { virtual void OnOpenFile(bool* prevent_default, const std::string& file_path) {} + // The browser has finished loading. + virtual void OnFinishLaunching() {} + protected: virtual ~BrowserObserver() {} };