Set DownloadManager callback in constructor.

This commit is contained in:
levlam 2022-03-02 13:56:04 +03:00
parent 4d7afaedbb
commit 6da151a402
3 changed files with 23 additions and 29 deletions

View File

@ -68,9 +68,11 @@ struct FileDownloadInDb {
class DownloadManagerImpl final : public DownloadManager {
public:
void set_callback(unique_ptr<Callback> callback) final {
callback_ = std::move(callback);
loop();
explicit DownloadManagerImpl(unique_ptr<Callback> callback) : callback_(std::move(callback)) {
}
void start_up() final {
try_start();
}
Status toggle_is_paused(FileId file_id, bool is_paused) final {
@ -445,7 +447,8 @@ class DownloadManagerImpl final : public DownloadManager {
auto it = files_.emplace(download_id, std::move(file_info)).first;
register_file_info(*it->second);
if (it->second->completed_at == 0 && !it->second->is_paused) {
callback_->start_file(it->second->internal_file_id, it->second->priority, it->second->link_token);
callback_->start_file(it->second->internal_file_id, it->second->priority,
actor_shared(this, it->second->link_token));
}
}
@ -474,7 +477,7 @@ class DownloadManagerImpl final : public DownloadManager {
if (is_paused) {
callback_->pause_file(file_info.internal_file_id);
} else {
callback_->start_file(file_info.internal_file_id, file_info.priority, file_info.link_token);
callback_->start_file(file_info.internal_file_id, file_info.priority, actor_shared(this, file_info.link_token));
}
}
@ -554,8 +557,8 @@ class DownloadManagerImpl final : public DownloadManager {
}
};
unique_ptr<DownloadManager> DownloadManager::create() {
return make_unique<DownloadManagerImpl>();
unique_ptr<DownloadManager> DownloadManager::create(unique_ptr<Callback> callback) {
return make_unique<DownloadManagerImpl>(std::move(callback));
}
td_api::object_ptr<td_api::updateFileDownloads> DownloadManager::Counters::get_update_file_downloads_object() const {

View File

@ -21,9 +21,6 @@ namespace td {
class DownloadManager : public Actor {
public:
// creates, but do not starts the actor
static unique_ptr<DownloadManager> create();
struct Counters {
int64 total_size{};
int32 total_count{};
@ -49,7 +46,7 @@ class DownloadManager : public Actor {
virtual ~Callback() = default;
virtual void update_counters(Counters counters) = 0;
virtual void update_file_removed(FileId file_id) = 0;
virtual void start_file(FileId file_id, int8 priority, uint64 link_token) = 0;
virtual void start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) = 0;
virtual void pause_file(FileId file_id) = 0;
virtual void delete_file(FileId file_id) = 0;
virtual FileId dup_file_id(FileId file_id) = 0;
@ -61,14 +58,11 @@ class DownloadManager : public Actor {
bool is_paused) = 0;
};
static unique_ptr<DownloadManager> create(unique_ptr<Callback> callback);
//
// public interface for user
//
// sets callback to handle all updates
virtual void set_callback(unique_ptr<Callback> callback) = 0;
// files are always added in is_paused = false state
virtual Status add_file(FileId file_id, FileSourceId file_source_id, string search_text, int8 priority) = 0;
virtual Status change_search_text(FileId file_id, FileSourceId file_source_id, string search_text) = 0;
virtual Status toggle_is_paused(FileId file_id, bool is_paused) = 0;

View File

@ -3970,12 +3970,9 @@ void Td::init_managers() {
G()->set_contacts_manager(contacts_manager_actor_.get());
country_info_manager_ = make_unique<CountryInfoManager>(this, create_reference());
country_info_manager_actor_ = register_actor("CountryInfoManager", country_info_manager_.get());
download_manager_ = DownloadManager::create();
download_manager_actor_ = register_actor("DownloadManager", download_manager_.get());
class DownloadManagerCallback final : public DownloadManager::Callback {
public:
DownloadManagerCallback(ActorShared<> parent, ActorId<DownloadManager> download_manager)
: parent_(std::move(parent)), download_manager_(download_manager) {
explicit DownloadManagerCallback(ActorShared<> parent) : parent_(std::move(parent)) {
}
void update_counters(DownloadManager::Counters counters) final {
send_closure(G()->td(), &Td::send_update, counters.get_update_file_downloads_object());
@ -3984,9 +3981,10 @@ void Td::init_managers() {
send_closure(G()->td(), &Td::send_update,
td_api::make_object<td_api::updateFileRemovedFromDownloads>(file_id.get()));
}
void start_file(FileId file_id, int8 priority, uint64 link_token) final {
send_closure(G()->file_manager(), &FileManager::download, file_id, make_download_file_callback(link_token),
priority, FileManager::KEEP_DOWNLOAD_OFFSET, FileManager::IGNORE_DOWNLOAD_LIMIT);
void start_file(FileId file_id, int8 priority, ActorShared<DownloadManager> download_manager) final {
send_closure(G()->file_manager(), &FileManager::download, file_id,
make_download_file_callback(std::move(download_manager)), priority,
FileManager::KEEP_DOWNLOAD_OFFSET, FileManager::IGNORE_DOWNLOAD_LIMIT);
}
void pause_file(FileId file_id) final {
send_closure(G()->file_manager(), &FileManager::download, file_id, nullptr, 0, FileManager::KEEP_DOWNLOAD_OFFSET,
@ -4015,8 +4013,8 @@ void Td::init_managers() {
private:
ActorShared<> parent_;
ActorId<DownloadManager> download_manager_;
std::shared_ptr<FileManager::DownloadCallback> make_download_file_callback(uint64 link_token) {
static std::shared_ptr<FileManager::DownloadCallback> make_download_file_callback(
ActorShared<DownloadManager> download_manager) {
class Impl final : public FileManager::DownloadCallback {
public:
explicit Impl(ActorShared<DownloadManager> download_manager) : download_manager_(std::move(download_manager)) {
@ -4042,12 +4040,11 @@ void Td::init_managers() {
// TODO: handle deleted state?
}
};
return std::make_shared<Impl>(ActorShared<DownloadManager>(download_manager_, link_token));
return std::make_shared<Impl>(std::move(download_manager));
}
};
send_closure_later(download_manager_actor_, &DownloadManager::set_callback,
td::make_unique<DownloadManagerCallback>(create_reference(), download_manager_actor_.get()));
download_manager_ = DownloadManager::create(td::make_unique<DownloadManagerCallback>(create_reference()));
download_manager_actor_ = register_actor("DownloadManager", download_manager_.get());
game_manager_ = make_unique<GameManager>(this, create_reference());
game_manager_actor_ = register_actor("GameManager", game_manager_.get());
G()->set_game_manager(game_manager_actor_.get());