From aad13f6a3f998dc5b699b9e09484ca63bb5be6f1 Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Tue, 25 Feb 2020 22:25:10 +0200 Subject: [core] FileSourceManager::getFileSource() returns PassRefPtr --- bin/cache.cpp | 2 +- bin/offline.cpp | 6 +-- include/mbgl/storage/file_source_manager.hpp | 3 +- platform/android/src/file_source.cpp | 8 ++-- platform/android/src/offline/offline_manager.cpp | 5 ++- platform/android/src/offline/offline_region.cpp | 5 ++- platform/darwin/src/MGLOfflineStorage.mm | 4 +- platform/glfw/main.cpp | 8 ++-- platform/qt/src/qmapboxgl.cpp | 3 +- render-test/file_source.cpp | 2 +- src/mbgl/map/map.cpp | 4 +- src/mbgl/storage/file_source_manager.cpp | 3 +- test/map/map.test.cpp | 9 ++-- test/storage/database_file_source.test.cpp | 3 +- test/storage/main_resource_loader.test.cpp | 52 +++++++++++++++--------- 15 files changed, 69 insertions(+), 48 deletions(-) diff --git a/bin/cache.cpp b/bin/cache.cpp index 1df782f752..69d96c1d42 100644 --- a/bin/cache.cpp +++ b/bin/cache.cpp @@ -90,7 +90,7 @@ int main(int argc, char* argv[]) { } mbgl::util::RunLoop loop; - auto dbfs = mbgl::FileSourceManager::get()->getFileSource( + std::shared_ptr dbfs = mbgl::FileSourceManager::get()->getFileSource( mbgl::FileSourceType::Database, mbgl::ResourceOptions().withCachePath(args::get(cacheValue))); dbfs->forward(resource, response, [&loop] { loop.stop(); }); loop.run(); diff --git a/bin/offline.cpp b/bin/offline.cpp index c18fc31810..97b60c223f 100644 --- a/bin/offline.cpp +++ b/bin/offline.cpp @@ -163,10 +163,10 @@ int main(int argc, char *argv[]) { util::RunLoop loop; - std::shared_ptr fileSource = - std::static_pointer_cast(FileSourceManager::get()->getFileSource( + std::shared_ptr fileSource = std::static_pointer_cast( + std::shared_ptr(FileSourceManager::get()->getFileSource( FileSourceType::Database, - ResourceOptions().withAccessToken(token).withBaseURL(apiBaseURL).withCachePath(output))); + ResourceOptions().withAccessToken(token).withBaseURL(apiBaseURL).withCachePath(output)))); std::unique_ptr region; diff --git a/include/mbgl/storage/file_source_manager.hpp b/include/mbgl/storage/file_source_manager.hpp index 2b2a43cbec..e62d321af7 100644 --- a/include/mbgl/storage/file_source_manager.hpp +++ b/include/mbgl/storage/file_source_manager.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include namespace mbgl { @@ -29,7 +30,7 @@ public: // Returns shared instance of a file source for (type, options) tuple. // Creates new instance via registered factory if needed. If new instance cannot be // created, nullptr would be returned. - std::shared_ptr getFileSource(FileSourceType, const ResourceOptions&) noexcept; + PassRefPtr getFileSource(FileSourceType, const ResourceOptions&) noexcept; // Registers file source factory for a provided FileSourceType type. If factory for the // same type was already registered, will unregister previously registered factory. diff --git a/platform/android/src/file_source.cpp b/platform/android/src/file_source.cpp index 8bbe9acbd2..5a46bffd3e 100644 --- a/platform/android/src/file_source.cpp +++ b/platform/android/src/file_source.cpp @@ -41,10 +41,10 @@ FileSource::FileSource(jni::JNIEnv& _env, const jni::String& accessToken, const // TODO: Split Android FileSource API to smaller interfaces resourceLoader = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::ResourceLoader, resourceOptions); - databaseSource = std::static_pointer_cast( - mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, resourceOptions)); - onlineSource = std::static_pointer_cast( - mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, resourceOptions)); + databaseSource = std::static_pointer_cast(std::shared_ptr( + mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, resourceOptions))); + onlineSource = std::static_pointer_cast(std::shared_ptr( + mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, resourceOptions))); } FileSource::~FileSource() { diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index 98f2b88a7f..51d2f70fe0 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -25,8 +25,9 @@ void handleException(std::exception_ptr exception, // OfflineManager // OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object& jFileSource) - : fileSource(std::static_pointer_cast(mbgl::FileSourceManager::get()->getFileSource( - mbgl::FileSourceType::Database, FileSource::getSharedResourceOptions(env, jFileSource)))) { + : fileSource(std::static_pointer_cast( + std::shared_ptr(mbgl::FileSourceManager::get()->getFileSource( + mbgl::FileSourceType::Database, FileSource::getSharedResourceOptions(env, jFileSource))))) { if (!fileSource) { ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "Offline functionality is disabled."); } diff --git a/platform/android/src/offline/offline_region.cpp b/platform/android/src/offline/offline_region.cpp index 4ee835bab3..de7abef624 100644 --- a/platform/android/src/offline/offline_region.cpp +++ b/platform/android/src/offline/offline_region.cpp @@ -16,8 +16,9 @@ namespace android { OfflineRegion::OfflineRegion(jni::JNIEnv& env, jni::jlong offlineRegionPtr, const jni::Object& jFileSource) : region(reinterpret_cast(offlineRegionPtr)), - fileSource(std::static_pointer_cast(mbgl::FileSourceManager::get()->getFileSource( - mbgl::FileSourceType::Database, FileSource::getSharedResourceOptions(env, jFileSource)))) { + fileSource(std::static_pointer_cast( + std::shared_ptr(mbgl::FileSourceManager::get()->getFileSource( + mbgl::FileSourceType::Database, FileSource::getSharedResourceOptions(env, jFileSource))))) { if (!fileSource) { ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "Offline functionality is disabled."); } diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index abf67d330e..275a58f418 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -233,8 +233,8 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio options.withCachePath(_mbglCachePath) .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); _mbglFileSource = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::ResourceLoader, options); - _mbglOnlineFileSource = std::static_pointer_cast(mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, options)); - _mbglDatabaseFileSource = std::static_pointer_cast(mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, options)); + _mbglOnlineFileSource = std::static_pointer_cast(std::shared_ptr(mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, options))); + _mbglDatabaseFileSource = std::static_pointer_cast(std::shared_ptr(mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, options))); // Observe for changes to the API base URL (and find out the current one). [[MGLAccountManager sharedManager] addObserver:self diff --git a/platform/glfw/main.cpp b/platform/glfw/main.cpp index ded8ee3e1f..784e002a65 100644 --- a/platform/glfw/main.cpp +++ b/platform/glfw/main.cpp @@ -107,7 +107,7 @@ int main(int argc, char *argv[]) { mbgl::ResourceOptions resourceOptions; resourceOptions.withCachePath(cacheDB).withAccessToken(token); - auto onlineFileSource = + std::shared_ptr onlineFileSource = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, resourceOptions); if (!settings.online) { if (onlineFileSource) { @@ -165,7 +165,7 @@ int main(int argc, char *argv[]) { }); // Resource loader controls top-level request processing and can resume / pause all managed sources simultaneously. - auto resourceLoader = + std::shared_ptr resourceLoader = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::ResourceLoader, resourceOptions); view->setPauseResumeCallback([resourceLoader]() { static bool isPaused = false; @@ -180,8 +180,8 @@ int main(int argc, char *argv[]) { }); // Database file source. - auto databaseFileSource = std::static_pointer_cast( - mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, resourceOptions)); + auto databaseFileSource = std::static_pointer_cast(std::shared_ptr( + mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, resourceOptions))); view->setResetCacheCallback([databaseFileSource]() { databaseFileSource->resetDatabase([](std::exception_ptr ex) { if (ex) { diff --git a/platform/qt/src/qmapboxgl.cpp b/platform/qt/src/qmapboxgl.cpp index 65ecf1ce61..5b47174b77 100644 --- a/platform/qt/src/qmapboxgl.cpp +++ b/platform/qt/src/qmapboxgl.cpp @@ -1751,7 +1751,8 @@ QMapboxGLPrivate::QMapboxGLPrivate(QMapboxGL *q, const QMapboxGLSettings &settin mbgl::ResourceTransform::FinishedCallback onFinished) { actorRef.invoke(&mbgl::ResourceTransform::TransformCallback::operator(), kind, url, std::move(onFinished)); }}; - auto fs = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, resourceOptions); + std::shared_ptr fs = + mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, resourceOptions); std::static_pointer_cast(fs)->setResourceTransform(std::move(transform)); } diff --git a/render-test/file_source.cpp b/render-test/file_source.cpp index b93325f901..e81bfb3f17 100644 --- a/render-test/file_source.cpp +++ b/render-test/file_source.cpp @@ -19,7 +19,7 @@ ProxyFileSource::ProxyFileSource(std::shared_ptr defaultResourceLoad : defaultResourceLoader(std::move(defaultResourceLoader_)) { assert(defaultResourceLoader); if (offline) { - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, options); + std::shared_ptr dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, options); dbfs->setProperty(READ_ONLY_MODE_KEY, true); } } diff --git a/src/mbgl/map/map.cpp b/src/mbgl/map/map.cpp index 0d9574a299..7e72595149 100644 --- a/src/mbgl/map/map.cpp +++ b/src/mbgl/map/map.cpp @@ -33,7 +33,9 @@ Map::Map(RendererFrontend& frontend, : impl(std::make_unique( frontend, observer, - FileSourceManager::get() ? FileSourceManager::get()->getFileSource(ResourceLoader, resourceOptions) : nullptr, + FileSourceManager::get() + ? std::shared_ptr(FileSourceManager::get()->getFileSource(ResourceLoader, resourceOptions)) + : nullptr, mapOptions)) {} Map::Map(std::unique_ptr impl_) : impl(std::move(impl_)) {} diff --git a/src/mbgl/storage/file_source_manager.cpp b/src/mbgl/storage/file_source_manager.cpp index 62fd5e8f98..6689d5314a 100644 --- a/src/mbgl/storage/file_source_manager.cpp +++ b/src/mbgl/storage/file_source_manager.cpp @@ -30,8 +30,7 @@ FileSourceManager::FileSourceManager() : impl(std::make_unique()) {} FileSourceManager::~FileSourceManager() = default; -std::shared_ptr FileSourceManager::getFileSource(FileSourceType type, - const ResourceOptions& options) noexcept { +PassRefPtr FileSourceManager::getFileSource(FileSourceType type, const ResourceOptions& options) noexcept { std::lock_guard lock(impl->mutex); // Remove released file sources. diff --git a/test/map/map.test.cpp b/test/map/map.test.cpp index 8f7270529f..b3c73be3d0 100644 --- a/test/map/map.test.cpp +++ b/test/map/map.test.cpp @@ -342,7 +342,8 @@ TEST(Map, Offline) { NetworkStatus::Set(NetworkStatus::Status::Offline); const std::string prefix = "http://127.0.0.1:3000/"; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(Resource::style(prefix + "style.json"), expiredItem("style.json")); dbfs->forward(Resource::source(prefix + "streets.json"), expiredItem("streets.json")); dbfs->forward(Resource::spriteJSON(prefix + "sprite", 1.0), expiredItem("sprite.json")); @@ -882,7 +883,8 @@ TEST(Map, NoContentTiles) { Response response; response.noContent = true; response.expires = util::now() + 1h; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward( Resource::tile("http://example.com/{z}-{x}-{y}.vector.pbf", 1.0, 0, 0, 0, Tileset::Scheme::XYZ), response, [&] { test.map.getStyle().loadJSON(R"STYLE({ @@ -1327,7 +1329,8 @@ TEST(Map, TEST_REQUIRES_SERVER(ExpiredSpriteSheet)) { NetworkStatus::Set(NetworkStatus::Status::Offline); const std::string prefix = "http://127.0.0.1:3000/online/"; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(Resource::style(prefix + "style.json"), makeResponse("style.json")); dbfs->forward(Resource::source(prefix + "streets.json"), makeResponse("streets.json")); dbfs->forward(Resource::spriteJSON(prefix + "sprite", 1.0), makeResponse("sprite.json", true)); diff --git a/test/storage/database_file_source.test.cpp b/test/storage/database_file_source.test.cpp index 579e9a8c57..62e5954605 100644 --- a/test/storage/database_file_source.test.cpp +++ b/test/storage/database_file_source.test.cpp @@ -12,7 +12,8 @@ using namespace mbgl; TEST(DatabaseFileSource, PauseResume) { util::RunLoop loop; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->pause(); const Resource res{Resource::Unknown, "http://127.0.0.1:3000/test", {}, Resource::LoadingMethod::CacheOnly}; diff --git a/test/storage/main_resource_loader.test.cpp b/test/storage/main_resource_loader.test.cpp index b5245dbad8..307f92463c 100644 --- a/test/storage/main_resource_loader.test.cpp +++ b/test/storage/main_resource_loader.test.cpp @@ -263,7 +263,8 @@ TEST(MainResourceLoader, OptionalNonExpired) { response.expires = util::now() + 1h; std::unique_ptr req; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(optionalResource, response, [&] { req = fs.request(optionalResource, [&](Response res) { req.reset(); @@ -294,7 +295,8 @@ TEST(MainResourceLoader, OptionalExpired) { Response response; response.data = std::make_shared("Cached value"); response.expires = util::now() - 1h; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); std::unique_ptr req; dbfs->forward(optionalResource, response, [&] { req = fs.request(optionalResource, [&](Response res) { @@ -356,7 +358,8 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshEtagNotModified)) { response.data = std::make_shared("Cached value"); response.expires = util::now() + 1h; std::unique_ptr req; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(resource, response, [&] { req = fs.request(resource, [&](Response res) { req.reset(); @@ -392,7 +395,8 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshEtagModified)) { response.data = std::make_shared("Cached value"); response.expires = util::now() + 1h; std::unique_ptr req; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(resource, response, [&] { req = fs.request(resource, [&](Response res) { req.reset(); @@ -427,7 +431,8 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheFull)) { response.data = std::make_shared("Cached value"); response.expires = util::now() + 1h; std::unique_ptr req; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(resource, response, [&] { req = fs.request(resource, [&](Response res) { req.reset(); @@ -464,7 +469,8 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshModifiedNotModified) response.data = std::make_shared("Cached value"); response.expires = util::now() + 1h; std::unique_ptr req; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(resource, response, [&] { req = fs.request(resource, [&](Response res) { req.reset(); @@ -501,7 +507,8 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshModifiedModified)) { response.data = std::make_shared("Cached value"); response.expires = util::now() + 1h; std::unique_ptr req; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(resource, response, [&] { req = fs.request(resource, [&](Response res) { req.reset(); @@ -522,10 +529,10 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoCacheRefreshModifiedModified)) { TEST(MainResourceLoader, TEST_REQUIRES_SERVER(SetResourceTransform)) { util::RunLoop loop; - MainResourceLoader fs(ResourceOptions{}); - - auto onlinefs = std::static_pointer_cast( - FileSourceManager::get()->getFileSource(FileSourceType::Network, ResourceOptions{})); + MainResourceLoader resourceLoader(ResourceOptions{}); + std::shared_ptr fs = + FileSourceManager::get()->getFileSource(FileSourceType::Network, ResourceOptions{}); + auto onlinefs = std::static_pointer_cast(fs); // Translates the URL "localhost://test to http://127.0.0.1:3000/test Actor transform( @@ -545,7 +552,7 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(SetResourceTransform)) { const Resource resource1{Resource::Unknown, "localhost://test"}; std::unique_ptr req; - req = fs.request(resource1, [&](Response res) { + req = resourceLoader.request(resource1, [&](Response res) { req.reset(); EXPECT_EQ(nullptr, res.error); ASSERT_TRUE(res.data.get()); @@ -562,7 +569,7 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(SetResourceTransform)) { onlinefs->setResourceTransform({}); const Resource resource2{Resource::Unknown, "http://127.0.0.1:3000/test"}; - req = fs.request(resource2, [&](Response res) { + req = resourceLoader.request(resource2, [&](Response res) { req.reset(); EXPECT_EQ(nullptr, res.error); ASSERT_TRUE(res.data.get()); @@ -579,9 +586,10 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(SetResourceTransform)) { TEST(MainResourceLoader, SetResourceCachePath) { util::RunLoop loop; - MainResourceLoader fs(ResourceOptions{}); - auto dbfs = std::static_pointer_cast( - FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{})); + MainResourceLoader resourceLoader(ResourceOptions{}); + std::shared_ptr fs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + auto dbfs = std::static_pointer_cast(fs); dbfs->setDatabasePath("./new_offline.db", [&loop] { loop.stop(); }); loop.run(); } @@ -604,7 +612,8 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(RespondToStaleMustRevalidate)) { response.mustRevalidate = true; response.etag.emplace("snowfall"); std::unique_ptr req; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(resource, response, [&] { req = fs.request(resource, [&](Response res) { req.reset(); @@ -676,8 +685,10 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(CachedResourceLowPriority)) { using namespace std::chrono_literals; response.expires = util::now() - 1h; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); - auto onlineFs = FileSourceManager::get()->getFileSource(FileSourceType::Network, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr onlineFs = + FileSourceManager::get()->getFileSource(FileSourceType::Network, ResourceOptions{}); // Put existing values into the cache. Resource resource1{Resource::Unknown, "http://127.0.0.1:3000/load/3", {}, Resource::LoadingMethod::All}; @@ -762,7 +773,8 @@ TEST(MainResourceLoader, TEST_REQUIRES_SERVER(NoDoubleDispatch)) { std::unique_ptr req; unsigned responseCount = 0u; - auto dbfs = FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); + std::shared_ptr dbfs = + FileSourceManager::get()->getFileSource(FileSourceType::Database, ResourceOptions{}); dbfs->forward(resource, response, [&] { req = fs.request(resource, [&](Response res) { EXPECT_EQ(nullptr, res.error); -- cgit v1.2.1