diff --git a/browser/api/atom_api_app.cc b/browser/api/atom_api_app.cc index f0f2d1882bb1..6770dcc4a5a1 100644 --- a/browser/api/atom_api_app.cc +++ b/browser/api/atom_api_app.cc @@ -4,6 +4,7 @@ #include "browser/api/atom_api_app.h" +#include "base/values.h" #include "base/command_line.h" #include "browser/browser.h" #include "vendor/node/src/node.h" @@ -29,6 +30,12 @@ void App::OnWindowAllClosed() { Emit("window-all-closed"); } +void App::OnOpenFile(bool* prevent_default, const std::string& file_path) { + base::ListValue args; + args.AppendString(file_path); + *prevent_default = Emit("open-file", &args); +} + // 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 7d1c06c2f97b..0ee6d68c4564 100644 --- a/browser/api/atom_api_app.h +++ b/browser/api/atom_api_app.h @@ -26,6 +26,8 @@ class App : public EventEmitter, // BrowserObserver implementations: virtual void OnWillQuit(bool* prevent_default) OVERRIDE; virtual void OnWindowAllClosed() OVERRIDE; + virtual void OnOpenFile(bool* prevent_default, + const std::string& file_path) 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 87de8b957b65..2a18400898e0 100644 --- a/browser/atom_application_delegate_mac.mm +++ b/browser/atom_application_delegate_mac.mm @@ -16,6 +16,11 @@ andEventID:kAEQuitApplication]; } +- (BOOL)application:(NSApplication*)sender + openFile:(NSString*)filename { + return [[AtomApplication sharedApplication] openFile:filename]; +} + - (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent { [[AtomApplication sharedApplication] closeAllWindows:self]; diff --git a/browser/atom_application_mac.h b/browser/atom_application_mac.h index b755004b4cef..521ac9ef64f2 100644 --- a/browser/atom_application_mac.h +++ b/browser/atom_application_mac.h @@ -10,13 +10,15 @@ BOOL handlingSendEvent_; } ++ (AtomApplication*)sharedApplication; + // CrAppProtocol: - (BOOL)isHandlingSendEvent; // CrAppControlProtocol: - (void)setHandlingSendEvent:(BOOL)handlingSendEvent; -+ (AtomApplication*)sharedApplication; +- (BOOL)openFile:(NSString*)file; - (IBAction)closeAllWindows:(id)sender; diff --git a/browser/atom_application_mac.mm b/browser/atom_application_mac.mm index 95b96111f3df..204f8d822e51 100644 --- a/browser/atom_application_mac.mm +++ b/browser/atom_application_mac.mm @@ -5,10 +5,15 @@ #import "browser/atom_application_mac.h" #include "base/auto_reset.h" +#include "base/strings/sys_string_conversions.h" #include "browser/browser.h" @implementation AtomApplication ++ (AtomApplication*)sharedApplication { + return (AtomApplication*)[super sharedApplication]; +} + - (BOOL)isHandlingSendEvent { return handlingSendEvent_; } @@ -22,8 +27,9 @@ handlingSendEvent_ = handlingSendEvent; } -+ (AtomApplication*)sharedApplication { - return (AtomApplication*)[super sharedApplication]; +- (BOOL)openFile:(NSString*)filename { + std::string filename_str(base::SysNSStringToUTF8(filename)); + return atom::Browser::Get()->OpenFile(filename_str) ? YES : NO; } - (IBAction)closeAllWindows:(id)sender { diff --git a/browser/atom_browser_main_parts.h b/browser/atom_browser_main_parts.h index 06927e068e9f..e5704379d2d3 100644 --- a/browser/atom_browser_main_parts.h +++ b/browser/atom_browser_main_parts.h @@ -31,6 +31,7 @@ class AtomBrowserMainParts : public brightray::BrowserMainParts { virtual void PostEarlyInitialization() OVERRIDE; virtual void PreMainMessageLoopStart() OVERRIDE; virtual void PreMainMessageLoopRun() OVERRIDE; + virtual void PostDestroyThreads() OVERRIDE; private: scoped_ptr atom_bindings_; diff --git a/browser/atom_browser_main_parts_mac.mm b/browser/atom_browser_main_parts_mac.mm index 1bdbe05284d7..50eb14cd6d2b 100644 --- a/browser/atom_browser_main_parts_mac.mm +++ b/browser/atom_browser_main_parts_mac.mm @@ -25,4 +25,8 @@ void AtomBrowserMainParts::PreMainMessageLoopStart() { [mainNib release]; } +void AtomBrowserMainParts::PostDestroyThreads() { + [[[AtomApplication sharedApplication] delegate] release]; +} + } // namespace atom diff --git a/browser/browser.cc b/browser/browser.cc index fafe86ea700a..4df65b07dd3a 100644 --- a/browser/browser.cc +++ b/browser/browser.cc @@ -32,6 +32,15 @@ void Browser::Quit() { window_list->CloseAllWindows(); } +bool Browser::OpenFile(const std::string& file_path) { + bool prevent_default = false; + FOR_EACH_OBSERVER(BrowserObserver, + observers_, + OnOpenFile(&prevent_default, file_path)); + + return prevent_default; +} + 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 118ce008a3ee..f0bc48744eb4 100644 --- a/browser/browser.h +++ b/browser/browser.h @@ -27,6 +27,9 @@ class Browser : public WindowListObserver { // Quit the application immediately without cleanup work. void Terminate(); + // Tell the application to open a file. + bool OpenFile(const std::string& file_path); + void AddObserver(BrowserObserver* obs) { observers_.AddObserver(obs); } diff --git a/browser/browser_observer.h b/browser/browser_observer.h index e79884bba7f2..49c3ea8f6e1a 100644 --- a/browser/browser_observer.h +++ b/browser/browser_observer.h @@ -5,6 +5,8 @@ #ifndef ATOM_BROSER_BROWSER_OBSERVER_H_ #define ATOM_BROSER_BROWSER_OBSERVER_H_ +#include + namespace atom { class BrowserObserver { @@ -16,6 +18,11 @@ class BrowserObserver { // method will not be called, instead it will call OnWillQuit. virtual void OnWindowAllClosed() {} + // The browser has opened a file by double clicking in Finder or dragging the + // file to the Dock icon. (OS X only) + virtual void OnOpenFile(bool* prevent_default, + const std::string& file_path) {} + protected: virtual ~BrowserObserver() {} };