Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use async save dialog for anchor download attribute #16612

Merged
merged 1 commit into from Jan 31, 2019

Conversation

poiru
Copy link
Contributor

@poiru poiru commented Jan 30, 2019

On macOS, the synchronous save dialog does not work properly and breaks
in weird ways when the dialog is expanded/collapsed (see #14606). This
works around the problem for the dialog shown for <a download=...> by
switching to the asynchronous API.

To test this:

main.js:

const { app, BrowserWindow, session } = require('electron')
const path = require('path')

function createWindow() {
  win = new BrowserWindow({
    width: 600,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
    },
  })

  session.defaultSession.on('will-download', (event, item) => {
    // Test with and without this commented out.
    //item.setSavePath(path.join(app.getPath('desktop'), 'test-download.txt'))
  })

  win.loadURL(`file://${__dirname}/test.html`)
}

app.on('ready', createWindow)

test.html:

<html>
<head>
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; img-src 'self' data:">
</head>
<body>
<a href="data:application/xml;charset=utf-8,foo" download="download.txt">Save</a>
</body>

Notes: Fix broken save dialog on macOS for <a> downloads

On macOS, the synchronous save dialog does not work properly and breaks
in weird ways when the dialog is expanded/collapsed (see #14606). This
works around the problem for the dialog shown for `<a download=...>` by
switching to the asynchronous API.

To test this:

main.js:
```
const { app, BrowserWindow, session } = require('electron')
const path = require('path')

function createWindow() {
  win = new BrowserWindow({
    width: 600,
    height: 600,
    webPreferences: {
      nodeIntegration: false,
    },
  })

  session.defaultSession.on('will-download', (event, item) => {
    // Test with and without this commented out.
    //item.setSavePath(path.join(app.getPath('desktop'), 'test-download.txt'))
  })

  win.loadURL(`file://${__dirname}/test.html`)
}

app.on('ready', createWindow)
```

test.html:
```
<html>
<head>
  <meta http-equiv="Content-Security-Policy" content="default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; img-src 'self' data:">
</head>
<body>
<a href="data:application/xml;charset=utf-8,foo" download="download.txt">Save</a>
</body>
```
@poiru poiru requested review from zcbenz and a team January 30, 2019 15:54

auto dialog_callback =
base::Bind(&AtomDownloadManagerDelegate::OnDownloadSaveDialogDone,
base::Unretained(this), download_id, callback);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm using base::Unretained(this) here, but I think it should be fine since AtomDownloadManagerDelegate should exist until shutdown. @zcbenz, is this safe?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah it should be fine in this case.

@zcbenz zcbenz merged commit 927aac3 into master Jan 31, 2019
@release-clerk
Copy link

release-clerk bot commented Jan 31, 2019

Release Notes Persisted

Fix broken save dialog on macOS for <a> downloads

@zcbenz zcbenz deleted the download-async-save-dialog branch January 31, 2019 02:07
@trop
Copy link
Contributor

trop bot commented Jan 31, 2019

I was unable to backport this PR to "4-0-x" cleanly;
you will need to perform this backport manually.

@trop
Copy link
Contributor

trop bot commented Jan 31, 2019

I have automatically backported this PR to "5-0-x", please check out #16640

poiru added a commit that referenced this pull request Jan 31, 2019
I believe the existing code was fine, but better be safe than sorry.
This typo was introduced in #16612.
trop-bot pushed a commit to trop-bot/electron that referenced this pull request Jan 31, 2019
I believe the existing code was fine, but better be safe than sorry.
This typo was introduced in electron#16612.
poiru added a commit that referenced this pull request Jan 31, 2019
Backport of #16612 and #16646

Notes: Fix broken save dialog on macOS for `<a>` downloads
poiru added a commit that referenced this pull request Jan 31, 2019
Backport of #16612 and #16646

Notes: Fix broken save dialog on macOS for `<a>` downloads
poiru added a commit that referenced this pull request Jan 31, 2019
I believe the existing code was fine, but better be safe than sorry.
This typo was introduced in #16612.
trop-bot pushed a commit to trop-bot/electron that referenced this pull request Jan 31, 2019
I believe the existing code was fine, but better be safe than sorry.
This typo was introduced in electron#16612.
zcbenz pushed a commit that referenced this pull request Jan 31, 2019
…one (#16646)

I believe the existing code was fine, but better be safe than sorry.
This typo was introduced in #16612.
trop-bot pushed a commit to trop-bot/electron that referenced this pull request Jan 31, 2019
I believe the existing code was fine, but better be safe than sorry.
This typo was introduced in electron#16612.
codebytere pushed a commit that referenced this pull request Jan 31, 2019
…one (#16650)

I believe the existing code was fine, but better be safe than sorry.
This typo was introduced in #16612.
MarshallOfSound pushed a commit that referenced this pull request Feb 4, 2019
)

Backport of #16612 and #16646

Notes: Fix broken save dialog on macOS for `<a>` downloads
@sofianguy sofianguy added this to 5.0.0-beta.2 in 5.0.x Feb 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
No open projects
5.0.x
Fixed in 5.0.0-beta.2
Development

Successfully merging this pull request may close these issues.

None yet

2 participants