Repo created
This commit is contained in:
parent
4af19165ec
commit
68073add76
12458 changed files with 12350765 additions and 2 deletions
28
libs/storage/background_downloading/downloader_adapter_ios.h
Normal file
28
libs/storage/background_downloading/downloader_adapter_ios.h
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
#include "storage/background_downloading/downloader_queue.hpp"
|
||||
#include "storage/map_files_downloader_with_ping.hpp"
|
||||
|
||||
namespace storage
|
||||
{
|
||||
class BackgroundDownloaderAdapter : public MapFilesDownloaderWithPing
|
||||
{
|
||||
public:
|
||||
// MapFilesDownloader overrides:
|
||||
void Remove(CountryId const & countryId) override;
|
||||
|
||||
void Clear() override;
|
||||
|
||||
QueueInterface const & GetQueue() const override;
|
||||
|
||||
private:
|
||||
// MapFilesDownloaderWithServerList overrides:
|
||||
void Download(QueuedCountry && queuedCountry) override;
|
||||
|
||||
// Trying to download mwm from different servers recursively.
|
||||
void DownloadFromLastUrl(CountryId const & countryId, std::string const & downloadPath,
|
||||
std::vector<std::string> && urls);
|
||||
|
||||
BackgroundDownloaderQueue<uint64_t> m_queue;
|
||||
};
|
||||
} // namespace storage
|
||||
137
libs/storage/background_downloading/downloader_adapter_ios.mm
Normal file
137
libs/storage/background_downloading/downloader_adapter_ios.mm
Normal file
|
|
@ -0,0 +1,137 @@
|
|||
#import "storage/background_downloading/downloader_adapter_ios.h"
|
||||
|
||||
#import "platform/background_downloader_ios.h"
|
||||
|
||||
#include "storage/downloader.hpp"
|
||||
|
||||
#include "base/assert.hpp"
|
||||
#include "base/file_name_utils.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
|
||||
@interface NSError (ToDownloaderError)
|
||||
|
||||
- (downloader::DownloadStatus)toDownloaderError;
|
||||
|
||||
@end
|
||||
|
||||
@implementation NSError (ToDownloaderError)
|
||||
|
||||
- (downloader::DownloadStatus)toDownloaderError {
|
||||
return self.code == NSURLErrorFileDoesNotExist ? downloader::DownloadStatus::FileNotFound
|
||||
: downloader::DownloadStatus::Failed;
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
namespace storage
|
||||
{
|
||||
|
||||
void BackgroundDownloaderAdapter::Remove(CountryId const & countryId)
|
||||
{
|
||||
MapFilesDownloader::Remove(countryId);
|
||||
|
||||
if (!m_queue.Contains(countryId))
|
||||
return;
|
||||
|
||||
BackgroundDownloader * downloader = [BackgroundDownloader sharedBackgroundMapDownloader];
|
||||
auto const taskIdentifier = m_queue.GetTaskInfoForCountryId(countryId);
|
||||
if (taskIdentifier)
|
||||
[downloader cancelTaskWithIdentifier:*taskIdentifier];
|
||||
m_queue.Remove(countryId);
|
||||
}
|
||||
|
||||
void BackgroundDownloaderAdapter::Clear()
|
||||
{
|
||||
MapFilesDownloader::Clear();
|
||||
|
||||
BackgroundDownloader * downloader = [BackgroundDownloader sharedBackgroundMapDownloader];
|
||||
[downloader clear];
|
||||
m_queue.Clear();
|
||||
}
|
||||
|
||||
QueueInterface const & BackgroundDownloaderAdapter::GetQueue() const
|
||||
{
|
||||
if (m_queue.IsEmpty())
|
||||
return MapFilesDownloader::GetQueue();
|
||||
|
||||
return m_queue;
|
||||
}
|
||||
|
||||
void BackgroundDownloaderAdapter::Download(QueuedCountry && queuedCountry)
|
||||
{
|
||||
if (!IsDownloadingAllowed())
|
||||
{
|
||||
queuedCountry.OnDownloadFinished(downloader::DownloadStatus::Failed);
|
||||
return;
|
||||
}
|
||||
|
||||
auto const countryId = queuedCountry.GetCountryId();
|
||||
auto urls = MakeUrlList(queuedCountry.GetRelativeUrl());
|
||||
// Get urls order from worst to best.
|
||||
std::reverse(urls.begin(), urls.end());
|
||||
auto const path = queuedCountry.GetFileDownloadPath();
|
||||
|
||||
// The order is important here: add to the queue first, notify start downloading then.
|
||||
// Infinite recursion possible here, otherwise:
|
||||
// OnStartDownloading -> NotifyStatusChanged -> processCountryEvent -> configDialog (?!)
|
||||
// -> downloadNode for the same country if autodownload enabled -> Download.
|
||||
m_queue.Append(QueuedCountry(queuedCountry));
|
||||
|
||||
queuedCountry.OnStartDownloading();
|
||||
|
||||
DownloadFromLastUrl(countryId, path, std::move(urls));
|
||||
}
|
||||
|
||||
void BackgroundDownloaderAdapter::DownloadFromLastUrl(CountryId const & countryId,
|
||||
std::string const & downloadPath,
|
||||
std::vector<std::string> && urls)
|
||||
{
|
||||
if (urls.empty())
|
||||
return;
|
||||
|
||||
NSURL * url = [NSURL URLWithString:@(urls.back().c_str())];
|
||||
assert(url != nil);
|
||||
urls.pop_back();
|
||||
|
||||
auto onFinish = [this, countryId, downloadPath, urls = std::move(urls)](NSError *error) mutable
|
||||
{
|
||||
downloader::DownloadStatus status = error ? [error toDownloaderError] : downloader::DownloadStatus::Completed;
|
||||
|
||||
if (!m_queue.Contains(countryId))
|
||||
return;
|
||||
|
||||
if (status == downloader::DownloadStatus::Failed && !urls.empty())
|
||||
{
|
||||
DownloadFromLastUrl(countryId, downloadPath, std::move(urls));
|
||||
}
|
||||
else
|
||||
{
|
||||
auto const country = m_queue.GetCountryById(countryId);
|
||||
m_queue.Remove(countryId);
|
||||
country.OnDownloadFinished(status);
|
||||
}
|
||||
};
|
||||
|
||||
auto onProgress = [this, countryId](int64_t totalWritten, int64_t totalExpected)
|
||||
{
|
||||
if (!m_queue.Contains(countryId))
|
||||
return;
|
||||
|
||||
auto const & country = m_queue.GetCountryById(countryId);
|
||||
country.OnDownloadProgress({totalWritten, totalExpected});
|
||||
};
|
||||
|
||||
BackgroundDownloader * downloader = [BackgroundDownloader sharedBackgroundMapDownloader];
|
||||
NSUInteger taskId = [downloader downloadWithUrl:url completion:onFinish progress:onProgress];
|
||||
|
||||
m_queue.SetTaskInfoForCountryId(countryId, taskId);
|
||||
}
|
||||
|
||||
std::unique_ptr<MapFilesDownloader> GetDownloader()
|
||||
{
|
||||
return std::make_unique<BackgroundDownloaderAdapter>();
|
||||
}
|
||||
|
||||
} // namespace storage
|
||||
81
libs/storage/background_downloading/downloader_queue.hpp
Normal file
81
libs/storage/background_downloading/downloader_queue.hpp
Normal file
|
|
@ -0,0 +1,81 @@
|
|||
#pragma once
|
||||
|
||||
#include "storage/downloader_queue_interface.hpp"
|
||||
#include "storage/queued_country.hpp"
|
||||
#include "storage/storage_defines.hpp"
|
||||
|
||||
#include <cstdint>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
|
||||
namespace storage
|
||||
{
|
||||
template <typename TaskInfo>
|
||||
class BackgroundDownloaderQueue : public QueueInterface
|
||||
{
|
||||
public:
|
||||
using ForEachTaskInfoTypeFunction = std::function<void(TaskInfo const & info)>;
|
||||
|
||||
bool IsEmpty() const override { return m_queue.empty(); }
|
||||
|
||||
size_t Count() const override { return m_queue.size(); }
|
||||
|
||||
bool Contains(CountryId const & country) const override { return m_queue.find(country) != m_queue.cend(); }
|
||||
|
||||
void ForEachCountry(ForEachCountryFunction const & fn) const override
|
||||
{
|
||||
for (auto const & item : m_queue)
|
||||
fn(item.second.m_queuedCountry);
|
||||
}
|
||||
|
||||
void ForEachTaskInfo(ForEachTaskInfoTypeFunction const & fn) const
|
||||
{
|
||||
for (auto const & item : m_queue)
|
||||
if (item.second.m_taskInfo)
|
||||
fn(*item.second.m_taskInfo);
|
||||
}
|
||||
|
||||
void Append(QueuedCountry && country)
|
||||
{
|
||||
auto const countryId = country.GetCountryId();
|
||||
auto const result = m_queue.emplace(countryId, std::move(country));
|
||||
result.first->second.m_queuedCountry.OnCountryInQueue();
|
||||
}
|
||||
|
||||
void SetTaskInfoForCountryId(CountryId const & countryId, TaskInfo const & taskInfo)
|
||||
{
|
||||
auto const it = m_queue.find(countryId);
|
||||
CHECK(it != m_queue.cend(), ());
|
||||
|
||||
it->second.m_taskInfo = taskInfo;
|
||||
}
|
||||
|
||||
std::optional<TaskInfo> GetTaskInfoForCountryId(CountryId const & countryId) const
|
||||
{
|
||||
auto const it = m_queue.find(countryId);
|
||||
if (it == m_queue.cend())
|
||||
return {};
|
||||
|
||||
return it->second.m_taskInfo;
|
||||
}
|
||||
|
||||
QueuedCountry & GetCountryById(CountryId const & countryId) { return m_queue.at(countryId).m_queuedCountry; }
|
||||
|
||||
void Remove(CountryId const & countryId) { m_queue.erase(countryId); }
|
||||
|
||||
void Clear() { m_queue.clear(); }
|
||||
|
||||
private:
|
||||
struct TaskData
|
||||
{
|
||||
explicit TaskData(QueuedCountry && country) : m_queuedCountry(std::move(country)) {}
|
||||
|
||||
QueuedCountry m_queuedCountry;
|
||||
std::optional<TaskInfo> m_taskInfo;
|
||||
};
|
||||
|
||||
std::unordered_map<CountryId, TaskData> m_queue;
|
||||
};
|
||||
} // namespace storage
|
||||
Loading…
Add table
Add a link
Reference in a new issue