]> khamidou — GitHub mirrors - electron.git/commitdiff
app: add will-download event to defaultSession
authorRobo <hop2deep@gmail.com>
Sun, 30 Aug 2015 18:52:01 +0000 (00:22 +0530)
committerRobo <hop2deep@gmail.com>
Mon, 31 Aug 2015 07:27:56 +0000 (12:57 +0530)
atom/browser/api/atom_api_session.cc
atom/browser/api/atom_api_session.h
atom/browser/api/atom_api_web_contents.h
atom/browser/api/lib/app.coffee
docs/api/app.md

index 9961d6cc660119a6d7dcc1fe85f82339cd41539e..0b7640e416b58be3331d781c1b18ce667f2b7083 100644 (file)
@@ -9,6 +9,7 @@
 
 #include "atom/browser/api/atom_api_cookies.h"
 #include "atom/browser/atom_browser_context.h"
+#include "atom/browser/api/atom_api_web_contents.h"
 #include "atom/common/native_mate_converters/callback.h"
 #include "atom/common/native_mate_converters/gurl_converter.h"
 #include "atom/common/native_mate_converters/file_path_converter.h"
@@ -101,6 +102,19 @@ struct Converter<ClearStorageDataOptions> {
   }
 };
 
+template<>
+struct Converter<content::DownloadItem*> {
+  static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
+                                   content::DownloadItem* val) {
+    return mate::ObjectTemplateBuilder(isolate)
+        .SetValue("url", val->GetURL())
+        .SetValue("filename", val->GetSuggestedFilename())
+        .SetValue("mimeType", val->GetMimeType())
+        .SetValue("hasUserGesture", val->HasUserGesture())
+        .Build()->NewInstance();
+  }
+};
+
 }  // namespace mate
 
 namespace atom {
@@ -215,9 +229,28 @@ void SetProxyInIO(net::URLRequestContextGetter* getter,
 Session::Session(AtomBrowserContext* browser_context)
     : browser_context_(browser_context) {
   AttachAsUserData(browser_context);
+  // Observe DownloadManger to get download notifications.
+  auto download_manager =
+      content::BrowserContext::GetDownloadManager(browser_context);
+  download_manager->AddObserver(this);
 }
 
 Session::~Session() {
+  auto download_manager =
+      content::BrowserContext::GetDownloadManager(browser_context_);
+  download_manager->RemoveObserver(this);
+}
+
+void Session::OnDownloadCreated(content::DownloadManager* manager,
+                                    content::DownloadItem* item) {
+  auto web_contents = item->GetWebContents();
+  bool prevent_default = Emit("will-download", item,
+                              api::WebContents::CreateFrom(isolate(),
+                                                           web_contents));
+  if (prevent_default) {
+    item->Cancel(true);
+    item->Remove();
+  }
 }
 
 void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
index b353c61c2109c6da6164127f337420034397a3f7..c06348974ff4c304c0fbf7c8239d652ba64d1b8b 100644 (file)
@@ -8,6 +8,7 @@
 #include <string>
 
 #include "atom/browser/api/trackable_object.h"
+#include "content/public/browser/download_manager.h"
 #include "native_mate/handle.h"
 #include "net/base/completion_callback.h"
 
@@ -27,7 +28,8 @@ class AtomBrowserContext;
 
 namespace api {
 
-class Session: public mate::TrackableObject<Session> {
+class Session: public mate::TrackableObject<Session>,
+               public content::DownloadManager::Observer {
  public:
   using ResolveProxyCallback = base::Callback<void(std::string)>;
 
@@ -41,6 +43,10 @@ class Session: public mate::TrackableObject<Session> {
   explicit Session(AtomBrowserContext* browser_context);
   ~Session();
 
+  // content::DownloadManager::Observer:
+  void OnDownloadCreated(content::DownloadManager* manager,
+                         content::DownloadItem* item) override;
+
   // mate::Wrappable implementations:
   mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
       v8::Isolate* isolate) override;
index 2fbc1d89977735cbe446654628f58a9fdbfb2c29..45027309ea65da9c4461a3103cedf0a5738e2060 100644 (file)
@@ -10,8 +10,8 @@
 
 #include "atom/browser/api/trackable_object.h"
 #include "atom/browser/common_web_contents_delegate.h"
-#include "content/public/common/favicon_url.h"
 #include "content/public/browser/web_contents_observer.h"
+#include "content/public/common/favicon_url.h"
 #include "native_mate/handle.h"
 #include "ui/gfx/image/image.h"
 
index 3e05106d1927b8715e0eeaa1dbbabaa33e29e0fb..f71297c596f0e2836a4122440f4a1c067054ad76 100644 (file)
@@ -32,8 +32,10 @@ app.setAppPath = (path) ->
 app.getAppPath = ->
   appPath
 
-# Be compatible with old API.
-app.once 'ready', -> @emit 'finish-launching'
+app.once 'ready', ->
+  app.defaultSession.__proto__ = EventEmitter.prototype
+  # Be compatible with old API.
+  @emit 'finish-launching'
 app.terminate = app.quit
 app.exit = process.exit
 app.getHomeDir = -> @getPath 'home'
index db34d37ec59e60d0a12f02e8c3030d0ba6148522..8951adf45cfd8e2dc05b9a6742783c14e810e841 100644 (file)
@@ -330,3 +330,35 @@ Sets the application's [dock menu][dock-menu].
 
 [dock-menu]:https://developer.apple.com/library/mac/documentation/Carbon/Conceptual/customizing_docktile/concepts/dockconcepts.html#//apple_ref/doc/uid/TP30000986-CH2-TPXREF103
 [tasks]:http://msdn.microsoft.com/en-us/library/windows/desktop/dd378460(v=vs.85).aspx#tasks
+
+## `app.defaultSession`
+
+The default session of the app available once the (ready)[app.md#event-ready] event is fired.
+
+```javascript
+app.on('ready', function() {
+  var defaultSession = app.defaultSession;
+})
+```
+
+### Event: 'will-download'
+
+* `event` Event
+* `downloadItem` Object
+  * `url` String
+  * `filename` String
+  * `mimeType` String
+  * `hasUserGesture` Boolean
+* `webContents` (WebContents)[web-contents.md]
+
+Fired when a download is about to start. Calling `preventDefault()`
+will cancel the download.
+
+```javascript
+app.defaultSession.on('will-download', function(e, downloadItem, webContents) {
+  e.preventDefault();
+  require('request')(downloadItem.url, function(data) {
+    require('fs').writeFileSync('/somewhere', data);
+  });
+});
+```