diff --git a/storage/http_map_files_downloader.cpp b/storage/http_map_files_downloader.cpp index 662487914b..65733d927b 100644 --- a/storage/http_map_files_downloader.cpp +++ b/storage/http_map_files_downloader.cpp @@ -41,9 +41,7 @@ void HttpMapFilesDownloader::Download(QueuedCountry & queuedCountry) CHECK_THREAD_CHECKER(m_checker, ()); m_queue.emplace_back(std::move(queuedCountry)); - - for (auto const subscriber : m_subscribers) - subscriber->OnCountryInQueue(queuedCountry.GetCountryId()); + m_queue.back().OnCountryInQueue(); if (m_queue.size() != 1) return; @@ -70,8 +68,7 @@ void HttpMapFilesDownloader::Download() if (IsDownloadingAllowed()) { - for (auto const subscriber : m_subscribers) - subscriber->OnStartDownloadingCountry(queuedCountry.GetCountryId()); + queuedCountry.OnStartDownloading(); m_request.reset(downloader::HttpRequest::GetFile( urls, path, size, diff --git a/storage/map_files_downloader.hpp b/storage/map_files_downloader.hpp index 0e245ce30d..2387c06294 100644 --- a/storage/map_files_downloader.hpp +++ b/storage/map_files_downloader.hpp @@ -34,8 +34,6 @@ public: virtual void OnStartDownloading() = 0; virtual void OnFinishDownloading() = 0; - virtual void OnCountryInQueue(CountryId const & id) = 0; - virtual void OnStartDownloadingCountry(CountryId const & id) = 0; }; virtual ~MapFilesDownloader() = default; diff --git a/storage/queued_country.cpp b/storage/queued_country.cpp index 92404f14ed..92d5bf8605 100644 --- a/storage/queued_country.cpp +++ b/storage/queued_country.cpp @@ -39,6 +39,16 @@ QueuedCountry::QueuedCountry(platform::CountryFile const & countryFile, CountryI ASSERT(m_diffsDataSource != nullptr, ()); } +void QueuedCountry::Subscribe(Subscriber & subscriber) +{ + m_subscriber = &subscriber; +} + +void QueuedCountry::Unsubscribe() +{ + m_subscriber = nullptr; +} + void QueuedCountry::SetFileType(MapFileType type) { m_fileType = type; @@ -95,26 +105,28 @@ void QueuedCountry::ClarifyDownloadingType() } } -void QueuedCountry::SetOnFinishCallback(DownloadingFinishCallback const & onDownloaded) +void QueuedCountry::OnCountryInQueue() const { - m_downloadingFinishCallback = onDownloaded; + if (m_subscriber != nullptr) + m_subscriber->OnCountryInQueue(*this); } -void QueuedCountry::SetOnProgressCallback(DownloadingProgressCallback const & onProgress) +void QueuedCountry::OnStartDownloading() const { - m_downloadingProgressCallback = onProgress; -} - -void QueuedCountry::OnDownloadFinished(downloader::DownloadStatus status) const -{ - if (m_downloadingFinishCallback) - m_downloadingFinishCallback(*this, status); + if (m_subscriber != nullptr) + m_subscriber->OnStartDownloading(*this); } void QueuedCountry::OnDownloadProgress(downloader::Progress const & progress) const { - if (m_downloadingProgressCallback) - m_downloadingProgressCallback(*this, progress); + if (m_subscriber != nullptr) + m_subscriber->OnDownloadProgress(*this, progress); +} + +void QueuedCountry::OnDownloadFinished(downloader::DownloadStatus status) const +{ + if (m_subscriber != nullptr) + m_subscriber->OnDownloadFinished(*this, status); } bool QueuedCountry::operator==(CountryId const & countryId) const diff --git a/storage/queued_country.hpp b/storage/queued_country.hpp index 27844e2ca2..a6c590e976 100644 --- a/storage/queued_country.hpp +++ b/storage/queued_country.hpp @@ -13,18 +13,34 @@ namespace storage { class QueuedCountry; -using DownloadingFinishCallback = - std::function; +using CountryInQueueCallback = +std::function; +using DownloadingStartCallback = + std::function; using DownloadingProgressCallback = std::function; +using DownloadingFinishCallback = + std::function; class QueuedCountry { public: + class Subscriber + { + public: + virtual void OnCountryInQueue(QueuedCountry const & queuedCountry) = 0; + virtual void OnStartDownloading(QueuedCountry const & queuedCountry) = 0; + virtual void OnDownloadProgress(QueuedCountry const & queuedCountry, downloader::Progress const & progress) = 0; + virtual void OnDownloadFinished(QueuedCountry const & queuedCountry, downloader::DownloadStatus status) = 0; + }; + QueuedCountry(platform::CountryFile const & countryFile, CountryId const & m_countryId, MapFileType type, int64_t currentDataVersion, std::string const & dataDir, diffs::DiffsSourcePtr const & diffs); + void Subscribe(Subscriber & subscriber); + void Unsubscribe(); + void SetFileType(MapFileType type); MapFileType GetFileType() const; @@ -36,11 +52,10 @@ public: void ClarifyDownloadingType(); - void SetOnFinishCallback(DownloadingFinishCallback const & onDownloaded); - void SetOnProgressCallback(DownloadingProgressCallback const & onProgress); - - void OnDownloadFinished(downloader::DownloadStatus status) const; + void OnCountryInQueue() const; + void OnStartDownloading() const; void OnDownloadProgress(downloader::Progress const & progress) const; + void OnDownloadFinished(downloader::DownloadStatus status) const; bool operator==(CountryId const & countryId) const; @@ -52,7 +67,6 @@ private: std::string m_dataDir; diffs::DiffsSourcePtr m_diffsDataSource; - DownloadingFinishCallback m_downloadingFinishCallback; - DownloadingProgressCallback m_downloadingProgressCallback; + Subscriber * m_subscriber; }; } // namespace storage diff --git a/storage/storage.cpp b/storage/storage.cpp index 4e6a73d7e3..bbf1fdcc56 100644 --- a/storage/storage.cpp +++ b/storage/storage.cpp @@ -548,8 +548,7 @@ void Storage::DownloadCountry(CountryId const & countryId, MapFileType type) QueuedCountry queuedCountry(countryFile, countryId, type, GetCurrentDataVersion(), m_dataDir, m_diffsDataSource); - queuedCountry.SetOnFinishCallback(bind(&Storage::OnMapFileDownloadFinished, this, _1, _2)); - queuedCountry.SetOnProgressCallback(bind(&Storage::OnMapFileDownloadProgress, this, _1, _2)); + queuedCountry.Subscribe(*this); m_downloader->DownloadMapFile(queuedCountry); } @@ -670,15 +669,6 @@ void Storage::Unsubscribe(int slotId) } } -void Storage::OnMapFileDownloadFinished(QueuedCountry const & queuedCountry, DownloadStatus status) -{ - CHECK_THREAD_CHECKER(m_threadChecker, ()); - - m_downloadingCountries.erase(queuedCountry.GetCountryId()); - - OnMapDownloadFinished(queuedCountry.GetCountryId(), status, queuedCountry.GetFileType()); -} - void Storage::ReportProgress(CountryId const & countryId, Progress const & p) { CHECK_THREAD_CHECKER(m_threadChecker, ()); @@ -708,8 +698,19 @@ void Storage::ReportProgressForHierarchy(CountryId const & countryId, Progress c ForEachAncestorExceptForTheRoot(countryId, calcProgress); } -void Storage::OnMapFileDownloadProgress(QueuedCountry const & queuedCountry, - Progress const & progress) +void Storage::OnCountryInQueue(QueuedCountry const & queuedCountry) +{ + NotifyStatusChangedForHierarchy(queuedCountry.GetCountryId()); + SaveDownloadQueue(); +} + +void Storage::OnStartDownloading(QueuedCountry const & queuedCountry) +{ + m_downloadingCountries[queuedCountry.GetCountryId()] = {0, kUnknownTotalSize}; + NotifyStatusChangedForHierarchy(queuedCountry.GetCountryId()); +} + +void Storage::OnDownloadProgress(QueuedCountry const & queuedCountry, Progress const & progress) { CHECK_THREAD_CHECKER(m_threadChecker, ()); @@ -721,6 +722,15 @@ void Storage::OnMapFileDownloadProgress(QueuedCountry const & queuedCountry, ReportProgressForHierarchy(queuedCountry.GetCountryId(), progress); } +void Storage::OnDownloadFinished(QueuedCountry const & queuedCountry, DownloadStatus status) +{ + CHECK_THREAD_CHECKER(m_threadChecker, ()); + + m_downloadingCountries.erase(queuedCountry.GetCountryId()); + + OnMapDownloadFinished(queuedCountry.GetCountryId(), status, queuedCountry.GetFileType()); +} + void Storage::RegisterDownloadedFiles(CountryId const & countryId, MapFileType type) { CHECK_THREAD_CHECKER(m_threadChecker, ()); @@ -1355,18 +1365,6 @@ void Storage::OnFinishDownloading() return; } -void Storage::OnCountryInQueue(CountryId const & id) -{ - NotifyStatusChangedForHierarchy(id); - SaveDownloadQueue(); -} - -void Storage::OnStartDownloadingCountry(CountryId const & id) -{ - m_downloadingCountries[id] = {0, kUnknownTotalSize}; - NotifyStatusChangedForHierarchy(id); -} - void Storage::OnDiffStatusReceived(diffs::NameDiffInfoMap && diffs) { m_diffsDataSource->SetDiffInfo(move(diffs)); diff --git a/storage/storage.hpp b/storage/storage.hpp index bd098bac2a..516419ae6e 100644 --- a/storage/storage.hpp +++ b/storage/storage.hpp @@ -147,7 +147,8 @@ struct NodeStatuses // Downloading of only one mwm at a time is supported, so while the // mwm at the top of the queue is being downloaded (or updated by // applying a diff file) all other mwms have to wait. -class Storage : public MapFilesDownloader::Subscriber +class Storage : public MapFilesDownloader::Subscriber, + public QueuedCountry::Subscriber { public: struct StatusCallback; @@ -260,15 +261,18 @@ private: void ReportProgressForHierarchy(CountryId const & countryId, downloader::Progress const & leafProgress); + // QueuedCountry::Subscriber overrides: + void OnCountryInQueue(QueuedCountry const & queuedCountry) override; + void OnStartDownloading(QueuedCountry const & queuedCountry) override; /// Called on the main thread by MapFilesDownloader when /// downloading of a map file succeeds/fails. - void OnMapFileDownloadFinished(QueuedCountry const & queuedCountry, - downloader::DownloadStatus status); + void OnDownloadFinished(QueuedCountry const & queuedCountry, + downloader::DownloadStatus status) override; /// Periodically called on the main thread by MapFilesDownloader /// during the downloading process. - void OnMapFileDownloadProgress(QueuedCountry const & queuedCountry, - downloader::Progress const & progress); + void OnDownloadProgress(QueuedCountry const & queuedCountry, + downloader::Progress const & progress) override; void RegisterDownloadedFiles(CountryId const & countryId, MapFileType type); @@ -563,10 +567,9 @@ public: void SetStartDownloadingCallback(StartDownloadingCallback const & cb); + // MapFilesDownloader::Subscriber overrides: void OnStartDownloading() override; void OnFinishDownloading() override; - void OnCountryInQueue(CountryId const & id) override; - void OnStartDownloadingCountry(CountryId const & id) override; private: friend struct UnitClass_StorageTest_DeleteCountry; diff --git a/storage/storage_tests/fake_map_files_downloader.cpp b/storage/storage_tests/fake_map_files_downloader.cpp index 6d5b223883..10e720b359 100644 --- a/storage/storage_tests/fake_map_files_downloader.cpp +++ b/storage/storage_tests/fake_map_files_downloader.cpp @@ -27,11 +27,11 @@ void FakeMapFilesDownloader::Download(QueuedCountry & queuedCountry) m_queue.push_back(queuedCountry); - for (auto const subscriber : m_subscribers) - subscriber->OnCountryInQueue(queuedCountry.GetCountryId()); - if (m_queue.size() != 1) + { + m_queue.back().OnCountryInQueue(); return; + } for (auto const subscriber : m_subscribers) subscriber->OnStartDownloading(); @@ -81,8 +81,7 @@ void FakeMapFilesDownloader::Download() return; } - for (auto const subscriber : m_subscribers) - subscriber->OnStartDownloadingCountry(queuedCountry.GetCountryId()); + queuedCountry.OnStartDownloading(); ++m_timestamp; m_progress = {0, queuedCountry.GetDownloadSize()};