fix: browser API chrome.downloads.download 代码及Mock修正#1410
Draft
cyfung1031 wants to merge 4 commits intomainfrom
Draft
fix: browser API chrome.downloads.download 代码及Mock修正#1410cyfung1031 wants to merge 4 commits intomainfrom
chrome.downloads.download 代码及Mock修正#1410cyfung1031 wants to merge 4 commits intomainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
该 PR 旨在修复 chrome.downloads.download 在存在 onDeterminingFilename 监听器时可能忽略 filename 的 Chromium 已知问题(Close #1409),通过在 service worker 侧引入统一的下载封装来稳定处理文件名覆盖,并同步完善 chrome.downloads 的 mock 与单元测试,确保行为可回归验证。
Changes:
- 新增
startDownload下载封装:集中处理onDeterminingFilename+onChanged,并为调用方提供完成/中断回调。 - 将 service worker 中相关下载调用(如备份导出、GM_download)改为使用新封装,以规避文件名覆盖失效问题。
- 重写
chrome.downloadsmock,补齐 Promise/callback 形态与事件模拟,并新增对应单测覆盖。
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| src/app/service/service_worker/synchronize.ts | 备份导出下载改为走 startDownload 封装 |
| src/app/service/service_worker/gm_api/gm_api.ts | GM_download 下载流程改为使用 startDownload + 回调通知 |
| src/app/service/service_worker/download.ts | 新增下载封装:注册/卸载监听、文件名覆盖与完成/中断回调 |
| src/app/service/service_worker/download.test.ts | 为下载封装与 mock 下载行为新增单测覆盖 |
| src/app/service/content/gm_api/gm_api.ts | 优化 native->browser 下载链路的 Object URL 释放时机(覆盖更多回包分支) |
| packages/chrome-extension-mock/index.ts | chromeMock.init 时重置 downloads mock 状态 |
| packages/chrome-extension-mock/downloads.ts | 重写 downloads mock:支持 Promise/callback、事件与文件名决策流程 |
Comment on lines
+200
to
+203
| mDownloadId = undefined; | ||
| console.error("chrome.runtime.lastError in chrome.downloads.download", chrome.runtime.lastError); | ||
| } | ||
| if (mDownloadId == undefined) detachDownloadCallback(); |
Comment on lines
1046
to
+1072
| @@ -1060,30 +1061,44 @@ export default class GMApi extends GM_Base { | |||
| } as GMTypes.DownloadDetails<string>, | |||
| ]); | |||
| if (aborted) return; | |||
| let released = false; | |||
| const releaseResources = () => { | |||
| if (released) return; | |||
| released = true; | |||
| setTimeout(() => { | |||
| // 释放不需要的 URL | |||
| URL.revokeObjectURL(url); | |||
| }, 1); | |||
| }; | |||
Comment on lines
+53
to
+58
| download(options: chrome.downloads.DownloadOptions, callback?: Callback<number>) { | ||
| this.clearLastError(); | ||
| if (!options?.url) { | ||
| const error = new Error("The download url is required."); | ||
| if (callback) { | ||
| this.withLastError(error.message, () => callback(undefined as unknown as number)); |
Collaborator
Author
There was a problem hiding this comment.
mock 的 typescript 问题不处理
Comment on lines
+118
to
+141
| let filenameConfirmed = false; | ||
|
|
||
| if (!responseMap.has(id)) { | ||
| // onDeterminingFilename did not triggered. Fallback to chrome.downloads.search | ||
| const downloadItem = (await chrome.downloads.search({ id: id }))?.[0]; | ||
| if (downloadItem && downloadItem.id === id) { | ||
| responseMap.set(id, { | ||
| downloadItem: { | ||
| bytesReceived: downloadItem.bytesReceived, | ||
| fileSize: downloadItem.fileSize, | ||
| totalBytes: downloadItem.totalBytes, | ||
| }, | ||
| }); | ||
| } | ||
| } else { | ||
| // onDeterminingFilename was triggered | ||
| filenameConfirmed = true; | ||
| } | ||
|
|
||
| const downloadItem = responseMap.get(id); | ||
|
|
||
| await notifyDownloadCallback(entry.callback, { | ||
| donwloadId: id, | ||
| state: state === "interrupted" && filenameConfirmed ? "save_cancelled" : state, |
Collaborator
Author
|
先研究一下 Copilot 的说法 DRAFT PR |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist / 检查清单
Description / 描述
Close #1409
chrome.downloads.onDeterminingFilename。这是官方已知的 Chromium Bug。 https://issues.chromium.org/issues/40706258 。实作起来可以解决问题,所以就先这样吧。chrome.downloads.onDeterminingFilename,重新包装了chrome.downloads.download相关的操作。chrome.downloads的 mockScreenshots / 截图
Filename for chrome.downloads.download is ignored if onDeterminingFilename listener exists
https://issues.chromium.org/issues/40734759