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
Родитель 37bcdfdd45 c412b9b892
Коммит afa8f8b166
6 изменённых файлов: 50 добавлений и 10 удалений

Просмотреть файл

@ -627,6 +627,15 @@ void WebContents::LoadURL(const GURL& url, const mate::Dictionary& options) {
web_contents()->GetController().LoadURLWithParams(params); 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 { GURL WebContents::GetURL() const {
return web_contents()->GetURL(); return web_contents()->GetURL();
} }
@ -988,6 +997,7 @@ void WebContents::BuildPrototype(v8::Isolate* isolate,
.SetMethod("getId", &WebContents::GetID) .SetMethod("getId", &WebContents::GetID)
.SetMethod("equal", &WebContents::Equal) .SetMethod("equal", &WebContents::Equal)
.SetMethod("_loadURL", &WebContents::LoadURL) .SetMethod("_loadURL", &WebContents::LoadURL)
.SetMethod("downloadURL", &WebContents::DownloadURL)
.SetMethod("_getURL", &WebContents::GetURL) .SetMethod("_getURL", &WebContents::GetURL)
.SetMethod("getTitle", &WebContents::GetTitle) .SetMethod("getTitle", &WebContents::GetTitle)
.SetMethod("isLoading", &WebContents::IsLoading) .SetMethod("isLoading", &WebContents::IsLoading)

Просмотреть файл

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

Просмотреть файл

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

Просмотреть файл

@ -242,6 +242,13 @@ const options = {"extraHeaders" : "pragma: no-cache\n"}
webContents.loadURL(url, options) 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()` ### `webContents.getURL()`
Returns URL of the current web page. Returns URL of the current web page.

Просмотреть файл

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

Просмотреть файл

@ -87,12 +87,7 @@ describe 'session module', ->
res.end mockPDF res.end mockPDF
downloadServer.close() downloadServer.close()
it 'can download successfully', (done) -> assertDownload = (event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port) ->
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 state, 'completed'
assert.equal filename, 'mock.pdf' assert.equal filename, 'mock.pdf'
assert.equal url, "http://127.0.0.1:#{port}/" assert.equal url, "http://127.0.0.1:#{port}/"
@ -102,8 +97,33 @@ describe 'session module', ->
assert.equal disposition, contentDisposition assert.equal disposition, contentDisposition
assert fs.existsSync downloadFilePath assert fs.existsSync downloadFilePath
fs.unlinkSync 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) ->
assertDownload event, state, url, mimeType, receivedBytes, totalBytes, disposition, filename, port
done() 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) -> it 'can cancel download', (done) ->
downloadServer.listen 0, '127.0.0.1', -> downloadServer.listen 0, '127.0.0.1', ->
{port} = downloadServer.address() {port} = downloadServer.address()