]> khamidou — GitHub mirrors - electron.git/commitdiff
Add 'open-url' event for app API. Fixes #36.
authorCheng Zhao <zcbenz@gmail.com>
Wed, 10 Jul 2013 08:10:38 +0000 (16:10 +0800)
committerCheng Zhao <zcbenz@gmail.com>
Wed, 10 Jul 2013 10:59:21 +0000 (18:59 +0800)
browser/api/atom_api_app.cc
browser/api/atom_api_app.h
browser/atom_application_mac.mm
browser/browser.cc
browser/browser.h
browser/browser_observer.h
browser/default_app/main.js

index afd2fd503abf5cdec175172df81055ba904ee5a4..221bce1d3538eb7c2e2474097d756d5729d3e852 100644 (file)
@@ -36,6 +36,12 @@ void App::OnOpenFile(bool* prevent_default, const std::string& file_path) {
   *prevent_default = Emit("open-file", &args);
 }
 
+void App::OnOpenURL(const std::string& url) {
+  base::ListValue args;
+  args.AppendString(url);
+  Emit("open-url", &args);
+}
+
 void App::OnWillFinishLaunching() {
   Emit("will-finish-launching");
 }
index 0ae86351a51b448c2b64b825b12b438d7a5ce2d0..339178ffc6199e69cecbaeeb446c298defe34dad 100644 (file)
@@ -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 OnOpenURL(const std::string& url) OVERRIDE;
   virtual void OnWillFinishLaunching() OVERRIDE;
   virtual void OnFinishLaunching() OVERRIDE;
 
index 3fc91c810245a34d87f1ffd4ea0e40ac7d5fa12d..7cfde6038057a1f967ebbcd39fbbb61db8fff055 100644 (file)
@@ -5,6 +5,7 @@
 #import "browser/atom_application_mac.h"
 
 #include "base/auto_reset.h"
+#include "base/strings/sys_string_conversions.h"
 #include "browser/browser.h"
 
 @implementation AtomApplication
   handlingSendEvent_ = handlingSendEvent;
 }
 
+- (void)awakeFromNib {
+  [[NSAppleEventManager sharedAppleEventManager]
+      setEventHandler:self
+          andSelector:@selector(handleURLEvent:withReplyEvent:)
+        forEventClass:kInternetEventClass
+           andEventID:kAEGetURL];
+}
+
 - (IBAction)closeAllWindows:(id)sender {
   atom::Browser::Get()->Quit();
 }
 
+- (void)handleURLEvent:(NSAppleEventDescriptor*)event
+        withReplyEvent:(NSAppleEventDescriptor*)replyEvent {
+  NSString* url = [
+      [event paramDescriptorForKeyword:keyDirectObject] stringValue];
+  atom::Browser::Get()->OpenURL(base::SysNSStringToUTF8(url));
+}
+
 @end
index 58c6ab61feb615a43a12996f53f281b8c7f4f1d8..c4a2d50cdf8faaefe65e18880b4394c1b379c2c7 100644 (file)
@@ -42,6 +42,10 @@ bool Browser::OpenFile(const std::string& file_path) {
   return prevent_default;
 }
 
+void Browser::OpenURL(const std::string& url) {
+  FOR_EACH_OBSERVER(BrowserObserver, observers_, OnOpenURL(url));
+}
+
 void Browser::WillFinishLaunching() {
   FOR_EACH_OBSERVER(BrowserObserver, observers_, OnWillFinishLaunching());
 }
index a91583748d9972adad645a1fc4f46bb28af63ba3..d0189fcd2b95493879c8db3883a324070ec58f1f 100644 (file)
@@ -36,6 +36,9 @@ class Browser : public WindowListObserver {
   // Tell the application to open a file.
   bool OpenFile(const std::string& file_path);
 
+  // Tell the application to open a url.
+  void OpenURL(const std::string& url);
+
   // Tell the application the loading has been done.
   void WillFinishLaunching();
   void DidFinishLaunching();
index fb46b677e70357abd9f1caa4f8e245e1bcf43915..20b13ad47646bf1dc8420ec1bf627366e53b23b0 100644 (file)
@@ -23,6 +23,9 @@ class BrowserObserver {
   virtual void OnOpenFile(bool* prevent_default,
                           const std::string& file_path) {}
 
+  // Browser is used to open a url.
+  virtual void OnOpenURL(const std::string& url) {}
+
   // The browser has finished loading.
   virtual void OnWillFinishLaunching() {}
   virtual void OnFinishLaunching() {}
index 246dd660ab2343b8db7ab9505306edd677bc1624..ba00cc92694a4804c5c58a4e6204a5294491af1d 100644 (file)
@@ -1,4 +1,5 @@
 var app = require('app');
+var dialog = require('dialog');
 var delegate = require('atom-delegate');
 var ipc = require('ipc');
 var Menu = require('menu');
@@ -13,6 +14,10 @@ app.on('window-all-closed', function() {
   app.terminate();
 });
 
+app.on('open-url', function(event, url) {
+  dialog.showMessageBox({message: url, buttons: ['OK']});
+});
+
 delegate.browserMainParts.preMainMessageLoopRun = function() {
   app.commandLine.appendSwitch('js-flags', '--harmony_collections');