From d4a4f5fb5d24d52cba60e1373e263c7c72e5db61 Mon Sep 17 00:00:00 2001 From: Arseny Smirnov Date: Fri, 25 Feb 2022 23:17:30 +0100 Subject: [PATCH] Proxy some updates about file to DownloadManger. --- td/telegram/Td.cpp | 36 ++++++++++++++++++++++++++----- td/telegram/files/FileManager.cpp | 4 ++++ 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/td/telegram/Td.cpp b/td/telegram/Td.cpp index 85614f9a2..a1f273feb 100644 --- a/td/telegram/Td.cpp +++ b/td/telegram/Td.cpp @@ -3972,11 +3972,10 @@ void Td::init_managers() { country_info_manager_actor_ = register_actor("CountryInfoManager", country_info_manager_.get()); download_manager_ = DownloadManager::create(); download_manager_actor_ = register_actor("DownloadManager", download_manager_.get()); - // TODO: move this callback somewhere else class DownloadManagerCallback final : public DownloadManager::Callback { public: - DownloadManagerCallback(ActorShared<> parent, std::shared_ptr download_file_callback) - : parent_(std::move(parent)), download_file_callback_(std::move(download_file_callback)) { + DownloadManagerCallback(ActorShared<> parent, ActorId download_manager) + : parent_(std::move(parent)), download_manager_(download_manager) { } void update_counters(DownloadManager::Counters counters) final { send_closure(G()->td(), &Td::send_update, counters.to_td_api()); @@ -4005,10 +4004,37 @@ void Td::init_managers() { private: ActorShared<> parent_; - std::shared_ptr download_file_callback_; + ActorId download_manager_; + std::shared_ptr download_file_callback_; + std::shared_ptr make_download_file_callback() { + if (!download_file_callback_) { + class Impl : public FileManager::DownloadCallback { + public: + Impl(ActorId download_manager) : download_manager_(download_manager_) { + } + void on_progress(FileId file_id) final { + auto view = G()->file_manager().get_actor_unsafe()->get_file_view(file_id); + send_closure(download_manager_, &DownloadManager::update_file_download_state, file_id, + view.local_total_size(), view.size(), !view.is_downloading()); + // TODO: handle deleted state? + } + void on_download_ok(FileId file_id) final { + on_progress(file_id); + } + void on_download_error(FileId file_id, Status error) { + on_progress(file_id); + } + + private: + ActorId download_manager_; + }; + download_file_callback_ = std::make_shared(download_manager_); + } + return download_file_callback_; + } }; send_closure_later(download_manager_actor_, &DownloadManager::set_callback, - td::make_unique(create_reference(), download_file_callback_)); + td::make_unique(create_reference(), download_manager_actor_.get())); game_manager_ = make_unique(this, create_reference()); game_manager_actor_ = register_actor("GameManager", game_manager_.get()); diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index d2f09f875..44f138fb6 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -1809,6 +1809,10 @@ void FileManager::try_flush_node_info(FileNodePtr node, const char *source) { VLOG(update_file) << "Send UpdateFile about file " << file_id << " from " << source; context_->on_file_updated(file_id); } + if (info->download_callback_) { + // For DownloadManger. For everybody else it is just an empty function call (I hope). + info->download_callback_->on_progress(file_id); + } } node->on_info_flushed(); }