Merge pull request #3672 from CharlieHess/web-contents-download-url

DownloadURL from webContents
This commit is contained in:
Cheng Zhao 2015-12-04 10:52:11 +08:00
commit afa8f8b166
6 changed files with 50 additions and 10 deletions

View file

@ -627,6 +627,15 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
web_contents()->GetController().LoadURLWithParams(params);
}
void WebContents::DownloadURL(const GURL& url) {
auto browser_context = web_contents()->GetBrowserContext();
auto download_manager =
content::BrowserContext::GetDownloadManager(browser_context);
download_manager->DownloadUrl(
content::DownloadUrlParameters::FromWebContents(web_contents(), url));
}
GURL WebContents::GetURL() const {
return web_contents()->GetURL();
}
@ -988,6 +997,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("getId", &WebContents::GetID)
.SetMethod("equal", &WebContents::Equal)
.SetMethod("_loadURL", &WebContents::LoadURL)
.SetMethod("downloadURL", &WebContents::DownloadURL)
.SetMethod("_getURL", &WebContents::GetURL)
.SetMethod("getTitle", &WebContents::GetTitle)
.SetMethod("isLoading", &WebContents::IsLoading)

View file

@ -57,6 +57,7 @@ class WebContents : public mate::TrackableObject<WebContents>,
int GetID() const;
bool Equal(const WebContents* web_contents) const;
void LoadURL(const GURL& url, const mate::Dictionary& options);
void DownloadURL(const GURL& url);
GURL GetURL() const;
base::string16 GetTitle() const;
bool IsLoading() const;

View file

@ -288,6 +288,7 @@ registerWebViewElement = ->
'replace'
'replaceMisspelling'
'getId'
'downloadURL'
'inspectServiceWorker'
'print'
'printToPDF'

View file

@ -242,6 +242,13 @@ const options = {"extraHeaders" : "pragma: no-cache\n"}
webContents.loadURL(url, options)
```
### `webContents.downloadURL(url)`
* `url` URL
Initiates a download of the resource at `url` without navigating. The
`will-download` event of `session` will be triggered.
### `webContents.getURL()`
Returns URL of the current web page.

View file

@ -262,6 +262,7 @@ describe 'browser-window module', ->
w.loadURL "file://#{fixtures}/pages/window-open.html"
it 'emits when link with target is called', (done) ->
@timeout 10000
w.webContents.once 'new-window', (e, url, frameName) ->
e.preventDefault()
assert.equal url, 'http://host/'

View file

@ -87,23 +87,43 @@ describe 'session module', ->
res.end mockPDF
downloadServer.close()
it 'can download successfully', (done) ->
assertDownload = (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port) ->
assert.equal state, 'completed'
assert.equal filename, 'mock.pdf'
assert.equal url, "http://127.0.0.1:#{port}/"
assert.equal mimeType, 'application/pdf'
assert.equal receivedBytes, mockPDF.length
assert.equal totalBytes, mockPDF.length
assert.equal disposition, contentDisposition
assert fs.existsSync downloadFilePath
fs.unlinkSync downloadFilePath
it 'can download using BrowserWindow.loadURL', (done) ->
downloadServer.listen 0, '127.0.0.1', ->
{port} = downloadServer.address()
ipcRenderer.sendSync 'set-download-option', false
w.loadURL "#{url}:#{port}"
ipcRenderer.once 'download-done', (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) ->
assert.equal state, 'completed'
assert.equal filename, 'mock.pdf'
assert.equal url, "http://127.0.0.1:#{port}/"
assert.equal mimeType, 'application/pdf'
assert.equal receivedBytes, mockPDF.length
assert.equal totalBytes, mockPDF.length
assert.equal disposition, contentDisposition
assert fs.existsSync downloadFilePath
fs.unlinkSync downloadFilePath
assertDownload event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port
done()
it 'can download using WebView.downloadURL', (done) ->
downloadServer.listen 0, '127.0.0.1', ->
{port} = downloadServer.address()
ipcRenderer.sendSync 'set-download-option', false
webview = new WebView
webview.src = "file://#{fixtures}/api/blank.html"
webview.addEventListener 'did-finish-load', ->
webview.downloadURL "#{url}:#{port}/"
ipcRenderer.once 'download-done', (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename) ->
assertDownload event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port
document.body.removeChild(webview)
done()
document.body.appendChild webview
it 'can cancel download', (done) ->
downloadServer.listen 0, '127.0.0.1', ->
{port} = downloadServer.address()