blob: 6689d5314a23c73632160cf4d0ec1cde62c91a27 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
#include <mbgl/storage/file_source_manager.hpp>
#include <mbgl/storage/resource_options.hpp>
#include <mbgl/util/string.hpp>
#include <algorithm>
#include <list>
#include <map>
#include <mutex>
#include <tuple>
namespace mbgl {
struct FileSourceInfo {
FileSourceInfo(FileSourceType type_, std::string id_, std::weak_ptr<FileSource> fileSource_)
: type(type_), id(std::move(id_)), fileSource(std::move(fileSource_)) {}
FileSourceType type;
std::string id;
std::weak_ptr<FileSource> fileSource;
};
class FileSourceManager::Impl {
public:
std::list<FileSourceInfo> fileSources;
std::map<FileSourceType, FileSourceFactory> fileSourceFactories;
std::recursive_mutex mutex;
};
FileSourceManager::FileSourceManager() : impl(std::make_unique<Impl>()) {}
FileSourceManager::~FileSourceManager() = default;
PassRefPtr<FileSource> FileSourceManager::getFileSource(FileSourceType type, const ResourceOptions& options) noexcept {
std::lock_guard<std::recursive_mutex> lock(impl->mutex);
// Remove released file sources.
for (auto it = impl->fileSources.begin(); it != impl->fileSources.end();) {
it = it->fileSource.expired() ? impl->fileSources.erase(it) : ++it;
}
const auto context = reinterpret_cast<uint64_t>(options.platformContext());
std::string id =
options.baseURL() + '|' + options.accessToken() + '|' + options.cachePath() + '|' + util::toString(context);
std::shared_ptr<FileSource> fileSource;
auto fileSourceIt = std::find_if(impl->fileSources.begin(), impl->fileSources.end(), [type, &id](const auto& info) {
return info.type == type && info.id == id;
});
if (fileSourceIt != impl->fileSources.end()) {
fileSource = fileSourceIt->fileSource.lock();
}
if (!fileSource) {
auto it = impl->fileSourceFactories.find(type);
if (it != impl->fileSourceFactories.end()) {
assert(it->second);
fileSource = it->second(options);
impl->fileSources.emplace_back(type, std::move(id), fileSource);
}
}
return fileSource;
}
void FileSourceManager::registerFileSourceFactory(FileSourceType type, FileSourceFactory&& factory) noexcept {
assert(factory);
std::lock_guard<std::recursive_mutex> lock(impl->mutex);
impl->fileSourceFactories[type] = std::move(factory);
}
FileSourceManager::FileSourceFactory FileSourceManager::unRegisterFileSourceFactory(FileSourceType type) noexcept {
std::lock_guard<std::recursive_mutex> lock(impl->mutex);
auto it = impl->fileSourceFactories.find(type);
FileSourceFactory factory;
if (it != impl->fileSourceFactories.end()) {
factory = std::move(it->second);
impl->fileSourceFactories.erase(it);
}
return factory;
}
} // namespace mbgl
|