diff options
Diffstat (limited to 'src/mbgl/storage')
-rw-r--r-- | src/mbgl/storage/asset_file_source.hpp | 5 | ||||
-rw-r--r-- | src/mbgl/storage/file_source.cpp | 37 | ||||
-rw-r--r-- | src/mbgl/storage/file_source_manager.cpp | 71 | ||||
-rw-r--r-- | src/mbgl/storage/http_file_source.hpp | 4 | ||||
-rw-r--r-- | src/mbgl/storage/local_file_source.hpp | 6 | ||||
-rw-r--r-- | src/mbgl/storage/main_resource_loader.hpp | 27 | ||||
-rw-r--r-- | src/mbgl/storage/resource_options.cpp | 10 | ||||
-rw-r--r-- | src/mbgl/storage/resource_transform.cpp | 10 |
8 files changed, 113 insertions, 57 deletions
diff --git a/src/mbgl/storage/asset_file_source.hpp b/src/mbgl/storage/asset_file_source.hpp index cc15dbb60b..6dfd3ce4ad 100644 --- a/src/mbgl/storage/asset_file_source.hpp +++ b/src/mbgl/storage/asset_file_source.hpp @@ -14,8 +14,9 @@ public: ~AssetFileSource() override; std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override; - - static bool acceptsURL(const std::string& url); + bool canRequest(const Resource&) const override; + void pause() override; + void resume() override; private: class Impl; diff --git a/src/mbgl/storage/file_source.cpp b/src/mbgl/storage/file_source.cpp deleted file mode 100644 index 5f60a05278..0000000000 --- a/src/mbgl/storage/file_source.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include <mbgl/storage/file_source.hpp> -#include <mbgl/storage/resource_options.hpp> -#include <mbgl/util/string.hpp> - -#include <mutex> -#include <map> - -namespace mbgl { - -std::shared_ptr<FileSource> FileSource::getSharedFileSource(const ResourceOptions& options) { - static std::mutex mutex; - static std::map<std::string, std::weak_ptr<mbgl::FileSource>> fileSources; - - std::lock_guard<std::mutex> lock(mutex); - - // Purge entries no longer in use. - for (auto it = fileSources.begin(); it != fileSources.end();) { - it = it->second.expired() ? fileSources.erase(it) : ++it; - } - - const auto context = reinterpret_cast<uint64_t>(options.platformContext()); - const std::string key = options.baseURL() + '|' + options.accessToken() + '|' + options.cachePath() + '|' + util::toString(context); - - std::shared_ptr<mbgl::FileSource> fileSource; - auto tuple = fileSources.find(key); - if (tuple != fileSources.end()) { - fileSource = tuple->second.lock(); - } - - if (!fileSource) { - fileSources[key] = fileSource = createPlatformFileSource(options); - } - - return fileSource; -} - -} // namespace mbgl diff --git a/src/mbgl/storage/file_source_manager.cpp b/src/mbgl/storage/file_source_manager.cpp new file mode 100644 index 0000000000..6817717f1a --- /dev/null +++ b/src/mbgl/storage/file_source_manager.cpp @@ -0,0 +1,71 @@ +#include <mbgl/storage/file_source_manager.hpp> +#include <mbgl/storage/resource_options.hpp> +#include <mbgl/util/string.hpp> + +#include <map> +#include <mutex> +#include <tuple> + +namespace mbgl { + +class FileSourceManager::Impl { +public: + using Key = std::tuple<FileSourceType, std::string>; + std::map<Key, std::weak_ptr<FileSource>> fileSources; + std::map<FileSourceType, FileSourceFactory> fileSourceFactories; + std::recursive_mutex mutex; +}; + +FileSourceManager::FileSourceManager() : impl(std::make_unique<Impl>()) {} + +FileSourceManager::~FileSourceManager() = default; + +std::shared_ptr<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->second.expired() ? impl->fileSources.erase(it) : ++it; + } + + const auto context = reinterpret_cast<uint64_t>(options.platformContext()); + const std::string optionsKey = + options.baseURL() + '|' + options.accessToken() + '|' + options.cachePath() + '|' + util::toString(context); + const auto key = std::tie(type, optionsKey); + + std::shared_ptr<FileSource> fileSource; + auto tuple = impl->fileSources.find(key); + if (tuple != impl->fileSources.end()) { + fileSource = tuple->second.lock(); + } + + if (!fileSource) { + auto it = impl->fileSourceFactories.find(type); + if (it != impl->fileSourceFactories.end()) { + assert(it->second); + impl->fileSources[key] = fileSource = it->second(options); + } + } + + 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 diff --git a/src/mbgl/storage/http_file_source.hpp b/src/mbgl/storage/http_file_source.hpp index 09834aa4dc..693ea3414d 100644 --- a/src/mbgl/storage/http_file_source.hpp +++ b/src/mbgl/storage/http_file_source.hpp @@ -1,6 +1,7 @@ #pragma once #include <mbgl/storage/file_source.hpp> +#include <mbgl/storage/resource.hpp> namespace mbgl { @@ -10,6 +11,9 @@ public: ~HTTPFileSource() override; std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override; + bool canRequest(const Resource& resource) const override { + return resource.hasLoadingMethod(Resource::LoadingMethod::Network); + } class Impl; diff --git a/src/mbgl/storage/local_file_source.hpp b/src/mbgl/storage/local_file_source.hpp index 0f065e0b5f..39ebc8c4bd 100644 --- a/src/mbgl/storage/local_file_source.hpp +++ b/src/mbgl/storage/local_file_source.hpp @@ -14,12 +14,12 @@ public: ~LocalFileSource() override; std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override; - - static bool acceptsURL(const std::string& url); + bool canRequest(const Resource&) const override; + void pause() override; + void resume() override; private: class Impl; - std::unique_ptr<util::Thread<Impl>> impl; }; diff --git a/src/mbgl/storage/main_resource_loader.hpp b/src/mbgl/storage/main_resource_loader.hpp new file mode 100644 index 0000000000..f78ff9af2e --- /dev/null +++ b/src/mbgl/storage/main_resource_loader.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include <mbgl/storage/file_source.hpp> + +namespace mbgl { + +class ResourceTransform; +class ResourceOptions; + +class MainResourceLoader final : public FileSource { +public: + explicit MainResourceLoader(const ResourceOptions& options); + ~MainResourceLoader() override; + + bool supportsCacheOnlyRequests() const override; + std::unique_ptr<AsyncRequest> request(const Resource&, Callback) override; + bool canRequest(const Resource&) const override; + void pause() override; + void resume() override; + +private: + friend class ResourceLoaderRequestor; + class Impl; + const std::unique_ptr<Impl> impl; +}; + +} // namespace mbgl diff --git a/src/mbgl/storage/resource_options.cpp b/src/mbgl/storage/resource_options.cpp index 21ecca979a..c56a22540b 100644 --- a/src/mbgl/storage/resource_options.cpp +++ b/src/mbgl/storage/resource_options.cpp @@ -10,7 +10,6 @@ public: std::string cachePath = ":memory:"; std::string assetPath = "."; uint64_t maximumSize = mbgl::util::DEFAULT_MAX_CACHE_SIZE; - bool supportCacheOnlyRequests = true; void* platformContext = nullptr; }; @@ -69,15 +68,6 @@ uint64_t ResourceOptions::maximumCacheSize() const { return impl_->maximumSize; } -ResourceOptions& ResourceOptions::withCacheOnlyRequestsSupport(bool supportCacheOnlyRequests) { - impl_->supportCacheOnlyRequests = supportCacheOnlyRequests; - return *this; -} - -bool ResourceOptions::supportsCacheOnlyRequests() const { - return impl_->supportCacheOnlyRequests; -} - ResourceOptions& ResourceOptions::withPlatformContext(void* context) { impl_->platformContext = context; return *this; diff --git a/src/mbgl/storage/resource_transform.cpp b/src/mbgl/storage/resource_transform.cpp index 6596551e60..eaf10c93fd 100644 --- a/src/mbgl/storage/resource_transform.cpp +++ b/src/mbgl/storage/resource_transform.cpp @@ -2,12 +2,12 @@ namespace mbgl { -ResourceTransform::ResourceTransform(ActorRef<ResourceTransform>, TransformCallback&& callback) - : transformCallback(std::move(callback)) { -} +ResourceTransform::ResourceTransform(TransformCallback callback) : transformCallback(std::move(callback)) {} -void ResourceTransform::transform(Resource::Kind kind, const std::string& url, FinishedCallback&& finished) { - finished(transformCallback(kind, url)); +void ResourceTransform::transform(Resource::Kind kind, const std::string& url, FinishedCallback finished) { + assert(finished); + assert(transformCallback); + transformCallback(kind, url, std::move(finished)); } } // namespace mbgl |