10
10
#include " atom/browser/api/atom_api_web_contents.h"
11
11
12
12
#include " atom/browser/api/atom_api_debugger.h"
13
+ #include " atom/browser/api/atom_api_download_item.h"
13
14
#include " atom/browser/api/atom_api_session.h"
14
15
#include " atom/browser/api/atom_api_web_request.h"
15
16
#include " atom/browser/api/atom_api_window.h"
@@ -125,6 +126,16 @@ using guest_view::GuestViewManager;
125
126
126
127
namespace mate {
127
128
129
+ template <>
130
+ struct Converter <content::DownloadItem*> {
131
+ static v8::Local<v8::Value> ToV8 (
132
+ v8::Isolate* isolate, content::DownloadItem* val) {
133
+ if (!val)
134
+ return v8::Null (isolate);
135
+ return atom::api::DownloadItem::Create (isolate, val).ToV8 ();
136
+ }
137
+ };
138
+
128
139
template <>
129
140
struct Converter <atom::SetSizeParams> {
130
141
static bool FromV8 (v8::Isolate* isolate,
@@ -369,7 +380,8 @@ WebContents::WebContents(v8::Isolate* isolate,
369
380
request_id_(0 ),
370
381
enable_devtools_(true ),
371
382
is_being_destroyed_(false ),
372
- guest_delegate_(nullptr ) {
383
+ guest_delegate_(nullptr ),
384
+ weak_ptr_factory_(this ) {
373
385
if (type == REMOTE) {
374
386
Init (isolate);
375
387
AttachAsUserData (web_contents);
@@ -388,7 +400,8 @@ WebContents::WebContents(v8::Isolate* isolate,
388
400
request_id_(0 ),
389
401
enable_devtools_(true ),
390
402
is_being_destroyed_(false ),
391
- guest_delegate_(nullptr ) {
403
+ guest_delegate_(nullptr ),
404
+ weak_ptr_factory_(this ) {
392
405
CreateWebContents (isolate, options, create_params);
393
406
}
394
407
@@ -397,7 +410,8 @@ WebContents::WebContents(v8::Isolate* isolate, const mate::Dictionary& options)
397
410
request_id_(0 ),
398
411
enable_devtools_(true ),
399
412
is_being_destroyed_(false ),
400
- guest_delegate_(nullptr ) {
413
+ guest_delegate_(nullptr ),
414
+ weak_ptr_factory_(this ) {
401
415
mate::Handle <api::Session> session = SessionFromOptions (isolate, options);
402
416
403
417
content::WebContents::CreateParams create_params (session->browser_context ());
@@ -1604,16 +1618,37 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
1604
1618
web_contents ()->GetController ().LoadURLWithParams (params);
1605
1619
}
1606
1620
1621
+ void OnDownloadStarted (base::Callback<void (content::DownloadItem*)> callback,
1622
+ content::DownloadItem* item,
1623
+ content::DownloadInterruptReason reason) {
1624
+ content::BrowserThread::PostTask (content::BrowserThread::UI, FROM_HERE,
1625
+ base::Bind (callback, item));
1626
+ }
1627
+
1607
1628
void WebContents::DownloadURL (const GURL& url,
1608
- bool prompt_for_location ) {
1629
+ mate::Arguments* args ) {
1609
1630
auto browser_context = web_contents ()->GetBrowserContext ();
1610
1631
auto download_manager =
1611
1632
content::BrowserContext::GetDownloadManager (browser_context);
1612
1633
1613
1634
auto params = content::DownloadUrlParameters::CreateForWebContentsMainFrame (
1614
1635
web_contents (), url, NO_TRAFFIC_ANNOTATION_YET);
1615
- if (prompt_for_location)
1616
- params->set_prompt (prompt_for_location);
1636
+
1637
+ bool prompt_for_location = false ;
1638
+ if (args->GetNext (&prompt_for_location) && prompt_for_location) {
1639
+ prompt_for_location = true ;
1640
+ }
1641
+ params->set_prompt (prompt_for_location);
1642
+
1643
+ base::string16 suggested_name;
1644
+ if (args->GetNext (&suggested_name)) {
1645
+ params->set_suggested_name (suggested_name);
1646
+ }
1647
+
1648
+ base::Callback<void (content::DownloadItem*)> callback;
1649
+ if (args && args->GetNext (&callback)) {
1650
+ params->set_callback (base::Bind (OnDownloadStarted, callback));
1651
+ }
1617
1652
1618
1653
download_manager->DownloadUrl (std::move (params));
1619
1654
}
0 commit comments