diff --git a/android/jni/com/mapswithme/maps/DownloadResourcesLegacyActivity.cpp b/android/jni/com/mapswithme/maps/DownloadResourcesLegacyActivity.cpp index f0b9f0942c..e577336829 100644 --- a/android/jni/com/mapswithme/maps/DownloadResourcesLegacyActivity.cpp +++ b/android/jni/com/mapswithme/maps/DownloadResourcesLegacyActivity.cpp @@ -2,13 +2,12 @@ #include "defines.hpp" -#include "storage/map_files_downloader.hpp" +#include "storage/downloader.hpp" #include "storage/storage.hpp" #include "platform/downloader_defines.hpp" #include "platform/http_request.hpp" #include "platform/platform.hpp" -#include "platform/servers_list.hpp" #include "coding/internal/file_data.hpp" #include "coding/reader_streambuf.hpp" @@ -182,41 +181,28 @@ extern "C" env->CallVoidMethod(*listener, methodID, static_cast(g_totalDownloadedBytes + req.GetProgress().m_bytesDownloaded)); } - static void DownloadURLListFinished(HttpRequest const & req, Callback const & onFinish, Callback const & onProgress) - { - FileToDownload & curFile = g_filesToDownload.back(); - - LOG(LINFO, ("Finished URL list download for", curFile.m_fileName)); - - GetServersList(req, curFile.m_urls); - - Storage const & storage = g_framework->GetStorage(); - for (size_t i = 0; i < curFile.m_urls.size(); ++i) - { - curFile.m_urls[i] = MapFilesDownloader::MakeFullUrlLegacy(curFile.m_urls[i], - curFile.m_fileName, - storage.GetCurrentDataVersion()); - LOG(LDEBUG, (curFile.m_urls[i])); - } - - g_currentRequest.reset(HttpRequest::GetFile(curFile.m_urls, curFile.m_pathOnSdcard, curFile.m_fileSize, onFinish, onProgress, 512 * 1024, false)); - } - JNIEXPORT jint JNICALL Java_com_mapswithme_maps_DownloadResourcesLegacyActivity_nativeStartNextFileDownload(JNIEnv * env, jclass clazz, jobject listener) { if (g_filesToDownload.empty()) return ERR_NO_MORE_FILES; - FileToDownload & curFile = g_filesToDownload.back(); + /// @todo One downloader instance with cached servers. All this routine will be refactored some time. + static auto downloader = storage::GetDownloader(); + downloader->SetDataVersion(g_framework->GetStorage().GetCurrentDataVersion()); - LOG(LDEBUG, ("downloading", curFile.m_fileName, "sized", curFile.m_fileSize, "bytes")); + downloader->EnsureServersListReady([ptr = jni::make_global_ref(listener)]() + { + FileToDownload const & curFile = g_filesToDownload.back(); + LOG(LINFO, ("Downloading file", curFile.m_fileName)); - Callback onFinish(std::bind(&DownloadFileFinished, jni::make_global_ref(listener), _1)); - Callback onProgress(std::bind(&DownloadFileProgress, jni::make_global_ref(listener), _1)); + auto const urls = downloader->MakeUrlListLegacy(curFile.m_fileName); + g_currentRequest.reset(HttpRequest::GetFile(urls, curFile.m_pathOnSdcard, curFile.m_fileSize, + std::bind(&DownloadFileFinished, ptr, _1), + std::bind(&DownloadFileProgress, ptr, _1), + 512 * 1024, false)); + }); - g_currentRequest.reset(HttpRequest::Get(GetPlatform().MetaServerUrl(), - std::bind(&DownloadURLListFinished, _1, onFinish, onProgress))); return ERR_FILE_IN_PROGRESS; } diff --git a/platform/servers_list.cpp b/platform/servers_list.cpp index 4ea08f60aa..2334b61b87 100644 --- a/platform/servers_list.cpp +++ b/platform/servers_list.cpp @@ -39,10 +39,4 @@ void GetServersList(std::string const & src, std::vector & urls) VERIFY(ParseServerList(GetPlatform().DefaultUrlsJSON(), urls), ()); LOG(LWARNING, ("Can't get servers list from request, using default servers:", urls)); } - -void GetServersList(HttpRequest const & request, std::vector & urls) -{ - auto const src = request.GetStatus() == DownloadStatus::Completed ? request.GetData() : ""; - GetServersList(src, urls); -} } // namespace downloader diff --git a/platform/servers_list.hpp b/platform/servers_list.hpp index cdd11c7d96..f9b6360bd6 100644 --- a/platform/servers_list.hpp +++ b/platform/servers_list.hpp @@ -5,8 +5,5 @@ namespace downloader { -class HttpRequest; - void GetServersList(std::string const & src, std::vector & urls); -void GetServersList(HttpRequest const & request, std::vector & urls); } // namespace downloader diff --git a/storage/map_files_downloader.cpp b/storage/map_files_downloader.cpp index b890e4858c..016f8d83bb 100644 --- a/storage/map_files_downloader.cpp +++ b/storage/map_files_downloader.cpp @@ -72,21 +72,16 @@ QueueInterface const & MapFilesDownloader::GetQueue() const return m_pendingRequests; } -// static -std::string MapFilesDownloader::MakeFullUrlLegacy(std::string const & baseUrl, std::string const & fileName, int64_t dataVersion) -{ - return url::Join(baseUrl, downloader::GetFileDownloadUrl(fileName, dataVersion)); -} - void MapFilesDownloader::DownloadAsString(std::string url, std::function && callback, bool forceReset /* = false */) { - auto doDownload = [this, forceReset, url = std::move(url), callback = std::move(callback)]() + EnsureServersListReady([this, forceReset, url = std::move(url), callback = std::move(callback)]() { if ((m_fileRequest && !forceReset) || m_serversList.empty()) return; - m_fileRequest.reset(RequestT::Get(url::Join(m_serversList.back(), url), + // Servers are sorted from best to worst. + m_fileRequest.reset(RequestT::Get(url::Join(m_serversList.front(), url), [this, callback = std::move(callback)](RequestT & request) { bool deleteRequest = true; @@ -101,17 +96,20 @@ void MapFilesDownloader::DownloadAsString(std::string url, std::function && callback) +{ /// @todo Implement logic if m_serversList is "outdated". /// Fetch new servers list on each download request? if (!m_serversList.empty()) { - doDownload(); + callback(); } else if (!m_isServersListRequested) { - RunServersListAsync(std::move(doDownload)); + RunServersListAsync(std::move(callback)); } else { @@ -119,6 +117,11 @@ void MapFilesDownloader::DownloadAsString(std::string url, std::function MapFilesDownloader::MakeUrlListLegacy(std::string const & fileName) const +{ + return MakeUrlList(downloader::GetFileDownloadUrl(fileName, m_dataVersion)); +} + void MapFilesDownloader::SetServersList(ServersList const & serversList) { m_serversList = serversList; @@ -134,7 +137,7 @@ bool MapFilesDownloader::IsDownloadingAllowed() const return m_downloadingPolicy == nullptr || m_downloadingPolicy->IsDownloadingAllowed(); } -std::vector MapFilesDownloader::MakeUrlList(std::string const & relativeUrl) +std::vector MapFilesDownloader::MakeUrlList(std::string const & relativeUrl) const { std::vector urls; urls.reserve(m_serversList.size()); diff --git a/storage/map_files_downloader.hpp b/storage/map_files_downloader.hpp index 6587170623..822ba932ff 100644 --- a/storage/map_files_downloader.hpp +++ b/storage/map_files_downloader.hpp @@ -45,8 +45,6 @@ public: // Parent method must be called into override method. virtual QueueInterface const & GetQueue() const; - static std::string MakeFullUrlLegacy(std::string const & baseUrl, std::string const & fileName, int64_t dataVersion); - /** * @brief Async file download as string buffer (for small files only). * Request can be skipped if current servers list is empty. @@ -60,9 +58,15 @@ public: void SetDownloadingPolicy(DownloadingPolicy * policy); void SetDataVersion(int64_t version) { m_dataVersion = version; } + /// @name Legacy functions for Android resourses downloading routine. + /// @{ + void EnsureServersListReady(std::function && callback); + std::vector MakeUrlListLegacy(std::string const & fileName) const; + /// @} + protected: bool IsDownloadingAllowed() const; - std::vector MakeUrlList(std::string const & relativeUrl); + std::vector MakeUrlList(std::string const & relativeUrl) const; // Synchronously loads list of servers by http client. ServersList LoadServersList();