app: add will-download event to defaultSession
This commit is contained in:
parent
0f2ef3feb2
commit
aed487ef40
5 changed files with 77 additions and 4 deletions
|
@ -9,6 +9,7 @@
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_cookies.h"
|
#include "atom/browser/api/atom_api_cookies.h"
|
||||||
#include "atom/browser/atom_browser_context.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/callback.h"
|
||||||
#include "atom/common/native_mate_converters/gurl_converter.h"
|
#include "atom/common/native_mate_converters/gurl_converter.h"
|
||||||
#include "atom/common/native_mate_converters/file_path_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 mate
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
@ -215,9 +229,28 @@ void SetProxyInIO(net::URLRequestContextGetter* getter,
|
||||||
Session::Session(AtomBrowserContext* browser_context)
|
Session::Session(AtomBrowserContext* browser_context)
|
||||||
: browser_context_(browser_context) {
|
: browser_context_(browser_context) {
|
||||||
AttachAsUserData(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() {
|
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) {
|
void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
|
#include "content/public/browser/download_manager.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
#include "net/base/completion_callback.h"
|
#include "net/base/completion_callback.h"
|
||||||
|
|
||||||
|
@ -27,7 +28,8 @@ class AtomBrowserContext;
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
|
||||||
class Session: public mate::TrackableObject<Session> {
|
class Session: public mate::TrackableObject<Session>,
|
||||||
|
public content::DownloadManager::Observer {
|
||||||
public:
|
public:
|
||||||
using ResolveProxyCallback = base::Callback<void(std::string)>;
|
using ResolveProxyCallback = base::Callback<void(std::string)>;
|
||||||
|
|
||||||
|
@ -41,6 +43,10 @@ class Session: public mate::TrackableObject<Session> {
|
||||||
explicit Session(AtomBrowserContext* browser_context);
|
explicit Session(AtomBrowserContext* browser_context);
|
||||||
~Session();
|
~Session();
|
||||||
|
|
||||||
|
// content::DownloadManager::Observer:
|
||||||
|
void OnDownloadCreated(content::DownloadManager* manager,
|
||||||
|
content::DownloadItem* item) override;
|
||||||
|
|
||||||
// mate::Wrappable implementations:
|
// mate::Wrappable implementations:
|
||||||
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
mate::ObjectTemplateBuilder GetObjectTemplateBuilder(
|
||||||
v8::Isolate* isolate) override;
|
v8::Isolate* isolate) override;
|
||||||
|
|
|
@ -10,8 +10,8 @@
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
#include "atom/browser/common_web_contents_delegate.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/browser/web_contents_observer.h"
|
||||||
|
#include "content/public/common/favicon_url.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
#include "ui/gfx/image/image.h"
|
#include "ui/gfx/image/image.h"
|
||||||
|
|
||||||
|
|
|
@ -32,8 +32,10 @@ app.setAppPath = (path) ->
|
||||||
app.getAppPath = ->
|
app.getAppPath = ->
|
||||||
appPath
|
appPath
|
||||||
|
|
||||||
|
app.once 'ready', ->
|
||||||
|
app.defaultSession.__proto__ = EventEmitter.prototype
|
||||||
# Be compatible with old API.
|
# Be compatible with old API.
|
||||||
app.once 'ready', -> @emit 'finish-launching'
|
@emit 'finish-launching'
|
||||||
app.terminate = app.quit
|
app.terminate = app.quit
|
||||||
app.exit = process.exit
|
app.exit = process.exit
|
||||||
app.getHomeDir = -> @getPath 'home'
|
app.getHomeDir = -> @getPath 'home'
|
||||||
|
|
|
@ -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
|
[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
|
[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);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue