Closed Bug 1521308 Opened 5 years ago Closed 5 years ago

fail to download blob file in webextension

Categories

(Core :: Networking, defect)

63 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: jackyzy823, Unassigned)

References

Details

(Keywords: regression)

Attachments

(1 file)

733 bytes, application/x-zip-compressed
Details
Attached file test.zip

User Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.75 Safari/537.36

Steps to reproduce:

I wrote a WebExtension which using download API to download Blob files.
After some test , i found that it works well only before Firefox 63.0.

The attachment 'test.zip' is for reproducing.

Actual results:

Fail to download the blob files (In all stable version after firefox 63.0 )

Expected results:

Download normally (works under 62.0 and 62.0.b20)

Please describe step by step (!) how I can reproduce the reported problem.
I would also expect an XPI if you report a bug about an extension.

Flags: needinfo?(jackyzy823)

Steps:
1.Download 'test.zip' and unzip it into a folder
2.In Firefox

Menu-> Add-ons -> Extensions -> Debug Add-ons -> Check Enable Add-on Debugging -> Load Temporary Add-on -> select manifest.json or background.js

  1. Then You will see a failed Download item in All Downloads List.
Flags: needinfo?(jackyzy823)

9:46.14 INFO: No more inbound revisions, bisection finished.
9:46.14 INFO: Last good revision: 9353904b5dbcc54653e9688115fa51175e3d0043
9:46.14 INFO: First bad revision: 09917998d963a298ad6214fcc512e73b66259199
9:46.14 INFO: Pushlog:
https://hg.mozilla.org/integration/mozilla-inbound/pushloghtml?fromchange=9353904b5dbcc54653e9688115fa51175e3d0043&tochange=09917998d963a298ad6214fcc512e73b66259199

STR:

  1. Download and extract https://bugzilla.mozilla.org/attachment.cgi?id=9037782
  2. open "about:debugging#addons" in Firefox
  3. check [x]Enable add-on debugging
  4. select "load temporary addon" and load "background.js" from the extracted attachment
  5. a failed download is shown in the Download Panel
Blocks: 1228139
Status: UNCONFIRMED → NEW
Component: Untriaged → Networking
Ever confirmed: true
Flags: needinfo?(amarchesini)
Keywords: regression
Product: Firefox → Core

The reason why this web-extension fails, is that the blobURL is revoked immediately after the creation of the downloadItem object. That is too early because the download has not started yet. Note that browser.downloads.download returns a promise which is resolved when the downloadItem is created, and not when the download is completed!
The downloading is async and when it starts, the blobURL is already gone.

Note the documentation says:

"If you use URL.createObjectURL() to download data created in JavaScript and you want to revoke the object URL (with revokeObjectURL) later (as it is strongly recommend), you need to do that after the download has been completed. To do so, listen to the downloads.onChanged event."

See: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/downloads/download

I would change your code in this way:

let audiourl = URL.createObjectURL(audiodata);
let d = browser.downloads.download({
url: audiourl,
filename: "test123.aac"
});

d.then(downloadId => {
console.log("download started!");

browser.downloads.onChanged.addListener(function handler(delta) {
if (delta.id == downloadId && delta.state && delta.state.current === "complete") {
browser.downloads.onChanged.removeListener(handler);
URL.revokeObjectURL(audiourl);
console.log(Download ${delta.id} has completed.);
}
});
}, e => {
console.log("download failed!");
});

I close the bug as invalid, but if you have questions, or you think I'm wrong, NI me and/or open the bug again. Thanks!

Status: NEW → RESOLVED
Closed: 5 years ago
Flags: needinfo?(amarchesini) → needinfo?(jackyzy823)
Resolution: --- → INVALID

Thanks for your helping. I change my code according to your advice and it works well.

I should read the document more frequently and carefully.

Flags: needinfo?(jackyzy823)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: