]> khamidou — GitHub mirrors - electron.git/commitdiff
Emit 'open-file' event when OS X is trying to open file with the app.
authorCheng Zhao <zcbenz@gmail.com>
Thu, 30 May 2013 08:03:10 +0000 (16:03 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Thu, 30 May 2013 08:03:10 +0000 (16:03 +0800)
browser/api/atom_api_app.cc
browser/api/atom_api_app.h
browser/atom_application_delegate_mac.mm
browser/atom_application_mac.h
browser/atom_application_mac.mm
browser/atom_browser_main_parts.h
browser/atom_browser_main_parts_mac.mm
browser/browser.cc
browser/browser.h
browser/browser_observer.h

index f0f2d1882bb126cc060b6b6ec057daf3a827ad48..6770dcc4a5a15021e0c405812d0e43fe2d80291e 100644 (file)
@@ -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<v8::Value> App::New(const v8::Arguments &args) {
   v8::HandleScope scope;
index 7d1c06c2f97b53781b0aa9fbfc070174dcfa6958..0ee6d68c4564f06893371984da5999249335c0f0 100644 (file)
@@ -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<v8::Value> New(const v8::Arguments &args);
index 87de8b957b656ee00a2a8472044cd094668dadd5..2a18400898e0f9d46a8eb1453f7985c21f2b50b6 100644 (file)
            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];
index b755004b4cefa88aae86545e43af0642065b629b..521ac9ef64f24eb7c190c17bfc55b5371dc094ec 100644 (file)
   BOOL handlingSendEvent_;
 }
 
++ (AtomApplication*)sharedApplication;
+
 // CrAppProtocol:
 - (BOOL)isHandlingSendEvent;
 
 // CrAppControlProtocol:
 - (void)setHandlingSendEvent:(BOOL)handlingSendEvent;
 
-+ (AtomApplication*)sharedApplication;
+- (BOOL)openFile:(NSString*)file;
 
 - (IBAction)closeAllWindows:(id)sender;
 
index 95b96111f3df17109327609cf5590d3c3af272f5..204f8d822e511dce07c1e6be6e2b32c5bd6fae53 100644 (file)
@@ -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 {
index 06927e068e9fac9946a5a308b2417cbd532f1826..e5704379d2d3cb0b48ec5aa56b2c33d2d394481b 100644 (file)
@@ -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<AtomBrowserBindings> atom_bindings_;
index 1bdbe05284d7d3eab8492b7eabc31592b9efb34c..50eb14cd6d2bf525f896d0357310728c43ea06e9 100644 (file)
@@ -25,4 +25,8 @@ void AtomBrowserMainParts::PreMainMessageLoopStart() {
   [mainNib release];
 }
 
+void AtomBrowserMainParts::PostDestroyThreads() {
+  [[[AtomApplication sharedApplication] delegate] release];
+}
+
 }  // namespace atom
index fafe86ea700ae5931809288cdeb29faa11cc09fd..4df65b07dd3a5285abbf61617aff113ee780a065 100644 (file)
@@ -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));
index 118ce008a3eebdd8574669bf7984f2ca03c695d6..f0bc48744eb4b219714b7e2bde0cb49978ea654b 100644 (file)
@@ -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);
   }
index e79884bba7f2b176f3914f91478e69574cdbb0ac..49c3ea8f6e1a84d00cf5375bd7e14b7fdea1a656 100644 (file)
@@ -5,6 +5,8 @@
 #ifndef ATOM_BROSER_BROWSER_OBSERVER_H_
 #define ATOM_BROSER_BROWSER_OBSERVER_H_
 
+#include <string>
+
 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() {}
 };