新增 Chrome 下载接管扩展与 C++ 本地通信组件

实现链接右键发送、手动地址输入和默认关闭的可选 GET 自动接管,支持站点与文件类型过滤。通过 Native Messaging 和当前用户命名管道在任务入库后确认接管,失败恢复浏览器下载并持久化交接状态。保存请求编号、Referer 与 User-Agent,补充本地注册入口、集成包说明及扩展/stdio/IPC/HTTP 回归测试。
This commit is contained in:
tongtong committed 2026-11-16 15:44:44 +08:00
1 parent 6c810c3b4f
commit 028fec64be
41 files changed
+1081 -18

No files matched your search

+52 -7
View File
@@ -23,6 +23,10 @@ namespace
std::wstring GetDefaultSaveDir()
{
PWSTR known=nullptr;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Downloads,KF_FLAG_DONT_VERIFY,nullptr,&known))) {
std::wstring directory(known); CoTaskMemFree(known); return directory;
}
wchar_t profile[MAX_PATH] = { 0 };
if (::GetEnvironmentVariableW(L"USERPROFILE", profile, MAX_PATH) == 0)
return L".";
@@ -118,6 +122,12 @@ namespace
if (name.empty())
name = L"download";
auto stem=name.substr(0,name.find(L'.'));
bool reserved=_wcsicmp(stem.c_str(),L"CON")==0 || _wcsicmp(stem.c_str(),L"PRN")==0 || _wcsicmp(stem.c_str(),L"AUX")==0 || _wcsicmp(stem.c_str(),L"NUL")==0;
if (stem.size()==4 && stem[3]>=L'1' && stem[3]<=L'9')
reserved=reserved || _wcsnicmp(stem.c_str(),L"COM",3)==0 || _wcsnicmp(stem.c_str(),L"LPT",3)==0;
if (reserved) name=L"_"+name;
if (name.size() > 120)
{
size_t dot = name.find_last_of(L'.');
@@ -236,6 +246,7 @@ void DownloadEngine::Start()
void DownloadEngine::Stop()
{
ready_=false;
{
std::lock_guard<std::mutex> lock(cmd_mutex_);
stop_ = true;
@@ -258,6 +269,13 @@ void DownloadEngine::AddTask(const std::wstring& url, const std::wstring& save_d
cmd_cv_.notify_all();
}
std::shared_ptr<ExternalSubmission> DownloadEngine::SubmitExternal(ExternalDownload request) {
Command cmd; cmd.kind=Command::kAdd; cmd.url=request.url; cmd.external=std::move(request);
auto state=std::make_shared<ExternalSubmission>(); cmd.submission=state;
{ std::lock_guard lock(cmd_mutex_); commands_.push_back(std::move(cmd)); }
cmd_cv_.notify_all(); return state;
}
void DownloadEngine::PauseTask(int64_t id)
{
Command cmd;
@@ -351,6 +369,7 @@ void DownloadEngine::ThreadMain()
PostEvent(event);
}
ready_=store_open_;
while (!stop_)
{
DrainCommands();
@@ -390,6 +409,7 @@ void DownloadEngine::ThreadMain()
[this]() { return stop_.load() || !commands_.empty(); });
}
ready_=false;
store_.Close();
}
@@ -423,25 +443,41 @@ void DownloadEngine::DrainCommands()
void DownloadEngine::HandleAdd(const Command& cmd)
{
std::unique_lock<std::mutex> decision;
if (cmd.submission) {
decision=std::unique_lock<std::mutex>(cmd.submission->mutex);
if (cmd.submission->canceled) return;
}
auto reply=[&](const std::string& status,const std::wstring& message,int64_t id=0) {
if (!cmd.submission) return;
cmd.submission->status=status; cmd.submission->message=platform::ToUtf8(message);
cmd.submission->task_id=id; cmd.submission->done=true; cmd.submission->changed.notify_all();
};
auto reject=[&](const std::wstring& message) { reply("rejected",message); PostRejected(message,cmd.external.request_id); };
std::wstring url = Trim(cmd.url);
if (url.empty())
{
PostRejected(L"下载链接不能为空");
reject(L"下载链接不能为空");
return;
}
http::UrlParts parts;
if (!transport_->ParseUrl(url, parts))
{
PostRejected(L"仅支持有效的 HTTP/HTTPS 链接");
reject(L"仅支持有效的 HTTP/HTTPS 链接");
return;
}
for (const auto& entry : tasks_)
{
if (!cmd.external.request_id.empty() && entry.second.request_id==cmd.external.request_id && entry.second.url!=url) {
reject(L"请求编号已用于其他下载地址"); return;
}
if (entry.second.url == url)
{
PostRejected(L"链接已存在");
if (!cmd.external.request_id.empty() && entry.second.request_id==cmd.external.request_id)
reply("accepted",L"此请求已接管",entry.first);
else { reply("duplicate",L"链接已存在",entry.first); PostRejected(L"链接已存在",cmd.external.request_id); }
return;
}
}
@@ -453,19 +489,24 @@ void DownloadEngine::HandleAdd(const Command& cmd)
dir.pop_back();
if (!EnsureDirectory(dir))
{
PostRejected(L"保存目录不可用");
reject(L"保存目录不可用");
return;
}
TaskInfo task;
task.url = url;
task.file_name = FileNameFromUrl(url,*transport_);
task.file_name = FileNameFromUrl(url,*transport_);
if (!cmd.external.filename.empty()) {
auto slash=cmd.external.filename.find_last_of(L"/\\");
task.file_name=SanitizeFileName(cmd.external.filename.substr(slash==std::wstring::npos?0:slash+1));
}
task.request_id=cmd.external.request_id; task.referrer=cmd.external.referrer; task.user_agent=cmd.external.user_agent;
task.save_path = PickUniquePath(dir, task.file_name, tasks_);
task.state = TaskState::kQueued;
if (!store_open_ || !store_.Insert(task))
{
PostRejected(L"无法写入任务数据库");
reject(L"无法写入任务数据库");
return;
}
@@ -475,7 +516,9 @@ void DownloadEngine::HandleAdd(const Command& cmd)
EngineEvent* event = new EngineEvent;
event->type = EngineEventType::kTaskAdded;
event->task = task;
event->request_id=cmd.external.request_id;
PostEvent(event);
reply("accepted",L"任务已保存并交给 TTDownloader",task.id);
}
void DownloadEngine::HandlePause(int64_t id)
@@ -644,6 +687,7 @@ DownloadEngine::AttemptResult DownloadEngine::RunAttempt(TaskInfo& task,
http::Request request;
request.url=task.url;
request.range_offset=offset;
request.referrer=task.referrer; request.user_agent=task.user_agent;
auto opened=transport_->Open(request);
if (!opened.stream) {
error=opened.error;
@@ -813,10 +857,11 @@ void DownloadEngine::PostUpdate(const TaskInfo& task, double speed)
PostEvent(event);
}
void DownloadEngine::PostRejected(const std::wstring& message)
void DownloadEngine::PostRejected(const std::wstring& message,const std::wstring& request_id)
{
EngineEvent* event = new EngineEvent;
event->type = EngineEventType::kTaskRejected;
event->message = message;
event->request_id=request_id;
PostEvent(event);
}