mirror of https://github.com/kiwix/libkiwix.git
Make `Downloader` return shared_ptr instead of raw pointer.
This is dangerous by nature to return raw pointer on internal data.
This commit is contained in:
parent
52ae5c3a5f
commit
0e612de4d1
|
@ -93,15 +93,15 @@ class Downloader
|
||||||
|
|
||||||
void close();
|
void close();
|
||||||
|
|
||||||
Download* startDownload(const std::string& uri, const std::vector<std::pair<std::string, std::string>>& options = {});
|
std::shared_ptr<Download> startDownload(const std::string& uri, const std::vector<std::pair<std::string, std::string>>& options = {});
|
||||||
Download* getDownload(const std::string& did);
|
std::shared_ptr<Download> getDownload(const std::string& did);
|
||||||
|
|
||||||
size_t getNbDownload();
|
size_t getNbDownload();
|
||||||
std::vector<std::string> getDownloadIds();
|
std::vector<std::string> getDownloadIds();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::mutex m_lock;
|
std::mutex m_lock;
|
||||||
std::map<std::string, std::unique_ptr<Download>> m_knownDownloads;
|
std::map<std::string, std::shared_ptr<Download>> m_knownDownloads;
|
||||||
std::shared_ptr<Aria2> mp_aria;
|
std::shared_ptr<Aria2> mp_aria;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -164,37 +164,37 @@ std::vector<std::string> Downloader::getDownloadIds() {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
Download* Downloader::startDownload(const std::string& uri, const std::vector<std::pair<std::string, std::string>>& options)
|
std::shared_ptr<Download> Downloader::startDownload(const std::string& uri, const std::vector<std::pair<std::string, std::string>>& options)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(m_lock);
|
std::unique_lock<std::mutex> lock(m_lock);
|
||||||
for (auto& p: m_knownDownloads) {
|
for (auto& p: m_knownDownloads) {
|
||||||
auto& d = p.second;
|
auto& d = p.second;
|
||||||
auto& uris = d->getUris();
|
auto& uris = d->getUris();
|
||||||
if (std::find(uris.begin(), uris.end(), uri) != uris.end())
|
if (std::find(uris.begin(), uris.end(), uri) != uris.end())
|
||||||
return d.get();
|
return d;
|
||||||
}
|
}
|
||||||
std::vector<std::string> uris = {uri};
|
std::vector<std::string> uris = {uri};
|
||||||
auto gid = mp_aria->addUri(uris, options);
|
auto gid = mp_aria->addUri(uris, options);
|
||||||
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
|
m_knownDownloads[gid] = std::make_shared<Download>(mp_aria, gid);
|
||||||
return m_knownDownloads[gid].get();
|
return m_knownDownloads[gid];
|
||||||
}
|
}
|
||||||
|
|
||||||
Download* Downloader::getDownload(const std::string& did)
|
std::shared_ptr<Download> Downloader::getDownload(const std::string& did)
|
||||||
{
|
{
|
||||||
std::unique_lock<std::mutex> lock(m_lock);
|
std::unique_lock<std::mutex> lock(m_lock);
|
||||||
try {
|
try {
|
||||||
return m_knownDownloads.at(did).get();
|
return m_knownDownloads.at(did);
|
||||||
} catch(std::exception& e) {
|
} catch(std::exception& e) {
|
||||||
for (auto gid : mp_aria->tellActive()) {
|
for (auto gid : mp_aria->tellActive()) {
|
||||||
if (gid == did) {
|
if (gid == did) {
|
||||||
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
|
m_knownDownloads[gid] = std::make_shared<Download>(mp_aria, gid);
|
||||||
return m_knownDownloads[gid].get();
|
return m_knownDownloads[gid];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for (auto gid : mp_aria->tellWaiting()) {
|
for (auto gid : mp_aria->tellWaiting()) {
|
||||||
if (gid == did) {
|
if (gid == did) {
|
||||||
m_knownDownloads[gid] = std::unique_ptr<Download>(new Download(mp_aria, gid));
|
m_knownDownloads[gid] = std::make_shared<Download>(mp_aria, gid);
|
||||||
return m_knownDownloads[gid].get();
|
return m_knownDownloads[gid];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
throw e;
|
throw e;
|
||||||
|
|
Loading…
Reference in New Issue