diff options
author | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-02-27 11:39:01 +0200 |
---|---|---|
committer | Mikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com> | 2020-02-27 12:21:50 +0200 |
commit | 382a6000da3e77fe32da060380b584a548c2e2dd (patch) | |
tree | 2ad662dda93489047499fca6be37ee939783dc5c /platform/android | |
parent | 56616ad4ec15e08dc49fd5c24f9c6026058055af (diff) | |
download | qtlocation-mapboxgl-upstream/mikhail_improve_DatabaseFileSource_API.tar.gz |
[glfw][android][darwin] Access DatabaseFileSource only with JointDatabaseStorage interfaceupstream/mikhail_improve_DatabaseFileSource_API
Diffstat (limited to 'platform/android')
-rw-r--r-- | platform/android/src/file_source.cpp | 7 | ||||
-rw-r--r-- | platform/android/src/file_source.hpp | 2 | ||||
-rw-r--r-- | platform/android/src/offline/offline_manager.cpp | 126 | ||||
-rw-r--r-- | platform/android/src/offline/offline_manager.hpp | 2 | ||||
-rw-r--r-- | platform/android/src/offline/offline_region.cpp | 125 | ||||
-rw-r--r-- | platform/android/src/offline/offline_region.hpp | 2 |
6 files changed, 133 insertions, 131 deletions
diff --git a/platform/android/src/file_source.cpp b/platform/android/src/file_source.cpp index f95066546a..c8e57e66ff 100644 --- a/platform/android/src/file_source.cpp +++ b/platform/android/src/file_source.cpp @@ -41,8 +41,7 @@ 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::DatabaseFileSource>(std::shared_ptr<mbgl::FileSource>( - mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, resourceOptions))); + databaseStorage = mbgl::FileSourceManager::get()->getDatabaseStorage(resourceOptions); onlineSource = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, resourceOptions); } @@ -110,7 +109,7 @@ void FileSource::setResourceTransform(jni::JNIEnv& env, const jni::Object<FileSo void FileSource::setResourceCachePath(jni::JNIEnv& env, const jni::String& path, const jni::Object<FileSource::ResourcesCachePathChangeCallback>& _callback) { - if (!databaseSource) { + if (!databaseStorage) { ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "Offline functionality is disabled."); return; } @@ -132,7 +131,7 @@ void FileSource::setResourceCachePath(jni::JNIEnv& env, pathChangeCallback = {}; }); - databaseSource->setDatabasePath(newPath + DATABASE_FILE, pathChangeCallback); + databaseStorage->setDatabasePath(newPath + DATABASE_FILE, pathChangeCallback); } void FileSource::resume(jni::JNIEnv&) { diff --git a/platform/android/src/file_source.hpp b/platform/android/src/file_source.hpp index 3bf0fed2eb..a93a7d9182 100644 --- a/platform/android/src/file_source.hpp +++ b/platform/android/src/file_source.hpp @@ -74,7 +74,7 @@ private: mbgl::ResourceOptions resourceOptions; std::unique_ptr<Actor<ResourceTransform::TransformCallback>> resourceTransform; std::function<void()> pathChangeCallback; - std::shared_ptr<mbgl::DatabaseFileSource> databaseSource; + std::shared_ptr<mbgl::JointDatabaseStorage> databaseStorage; std::shared_ptr<mbgl::FileSource> onlineSource; std::shared_ptr<mbgl::FileSource> resourceLoader; }; diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp index 51d2f70fe0..a5108203f7 100644 --- a/platform/android/src/offline/offline_manager.cpp +++ b/platform/android/src/offline/offline_manager.cpp @@ -25,10 +25,9 @@ void handleException(std::exception_ptr exception, // OfflineManager // OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource) - : fileSource(std::static_pointer_cast<mbgl::DatabaseFileSource>( - std::shared_ptr<mbgl::FileSource>(mbgl::FileSourceManager::get()->getFileSource( - mbgl::FileSourceType::Database, FileSource::getSharedResourceOptions(env, jFileSource))))) { - if (!fileSource) { + : databaseStorage( + FileSourceManager::get()->getDatabaseStorage(FileSource::getSharedResourceOptions(env, jFileSource))) { + if (!databaseStorage) { ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "Offline functionality is disabled."); } } @@ -36,30 +35,29 @@ OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object<FileSource>& OfflineManager::~OfflineManager() {} void OfflineManager::setOfflineMapboxTileCountLimit(jni::JNIEnv&, jni::jlong limit) { - fileSource->setOfflineMapboxTileCountLimit(limit); + databaseStorage->setOfflineMapboxTileCountLimit(limit); } void OfflineManager::listOfflineRegions(jni::JNIEnv& env_, const jni::Object<FileSource>& jFileSource_, const jni::Object<ListOfflineRegionsCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); - fileSource->listOfflineRegions([ - //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), - jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) - ](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (regions) { - OfflineManager::ListOfflineRegionsCallback::onList( - *env, *jFileSource, *callback, *regions); - } else { - OfflineManager::ListOfflineRegionsCallback::onError( - *env, *callback, regions.error()); - } - }); + databaseStorage->listOfflineRegions( + [ + // Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the + // meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), + jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource))]( + mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (regions) { + OfflineManager::ListOfflineRegionsCallback::onList(*env, *jFileSource, *callback, *regions); + } else { + OfflineManager::ListOfflineRegionsCallback::onError(*env, *callback, regions.error()); + } + }); } void OfflineManager::createOfflineRegion(jni::JNIEnv& env_, @@ -79,24 +77,24 @@ void OfflineManager::createOfflineRegion(jni::JNIEnv& env_, auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); // Create region - fileSource->createOfflineRegion(definition, metadata, [ - //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), - jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) - ](mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> region) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (region) { - OfflineManager::CreateOfflineRegionCallback::onCreate( - *env, *jFileSource, *callback, *region - ); - } else { - OfflineManager::CreateOfflineRegionCallback::onError( - *env, *callback, region.error()); - } - }); + databaseStorage->createOfflineRegion( + definition, + metadata, + [ + // Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the + // meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), + jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource))]( + mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> region) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (region) { + OfflineManager::CreateOfflineRegionCallback::onCreate(*env, *jFileSource, *callback, *region); + } else { + OfflineManager::CreateOfflineRegionCallback::onError(*env, *callback, region.error()); + } + }); } void OfflineManager::mergeOfflineRegions(jni::JNIEnv& env_, const jni::Object<FileSource>& jFileSource_, @@ -106,29 +104,29 @@ void OfflineManager::mergeOfflineRegions(jni::JNIEnv& env_, const jni::Object<Fi auto globalFilesource = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, jFileSource_); auto path = jni::Make<std::string>(env_, jString_); - fileSource->mergeOfflineRegions(path, [ - //Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), - jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource)) - ](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { - - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (regions) { - OfflineManager::MergeOfflineRegionsCallback::onMerge( - *env, *jFileSource, *callback, *regions); - } else { - OfflineManager::MergeOfflineRegionsCallback::onError( - *env, *callback, regions.error()); - } - }); + databaseStorage->mergeOfflineRegions( + path, + [ + // Keep a shared ptr to a global reference of the callback and file source so they are not GC'd in the + // meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)), + jFileSource = std::make_shared<decltype(globalFilesource)>(std::move(globalFilesource))]( + mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> regions) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (regions) { + OfflineManager::MergeOfflineRegionsCallback::onMerge(*env, *jFileSource, *callback, *regions); + } else { + OfflineManager::MergeOfflineRegionsCallback::onError(*env, *callback, regions.error()); + } + }); } void OfflineManager::resetDatabase(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->resetDatabase( + databaseStorage->resetDatabase( [ // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))]( @@ -138,7 +136,7 @@ void OfflineManager::resetDatabase(jni::JNIEnv& env_, const jni::Object<FileSour void OfflineManager::packDatabase(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->packDatabase( + databaseStorage->packDatabase( [ // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))]( @@ -148,7 +146,7 @@ void OfflineManager::packDatabase(jni::JNIEnv& env_, const jni::Object<FileSourc void OfflineManager::invalidateAmbientCache(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->invalidateAmbientCache( + databaseStorage->invalidateAmbientCache( [ // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))]( @@ -158,7 +156,7 @@ void OfflineManager::invalidateAmbientCache(jni::JNIEnv& env_, const jni::Object void OfflineManager::clearAmbientCache(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->clearAmbientCache( + databaseStorage->clearAmbientCache( [ // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))]( @@ -168,7 +166,7 @@ void OfflineManager::clearAmbientCache(jni::JNIEnv& env_, const jni::Object<File void OfflineManager::setMaximumAmbientCacheSize(jni::JNIEnv& env_, const jni::jlong size_, const jni::Object<FileSourceCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->setMaximumAmbientCacheSize( + databaseStorage->setMaximumAmbientCacheSize( size_, [ // Keep a shared ptr to a global reference of the callback so they are not GC'd in the meanwhile @@ -177,7 +175,7 @@ void OfflineManager::setMaximumAmbientCacheSize(jni::JNIEnv& env_, const jni::jl } void OfflineManager::runPackDatabaseAutomatically(jni::JNIEnv&, jboolean autopack) { - fileSource->runPackDatabaseAutomatically(autopack); + databaseStorage->runPackDatabaseAutomatically(autopack); } // FileSource::FileSourceCallback // @@ -328,7 +326,7 @@ void OfflineManager::putResourceWithUrl(jni::JNIEnv& env, response.expires = Timestamp(mbgl::Seconds(expires)); } - fileSource->put(resource, response); + databaseStorage->put(resource, response); } } // namespace android diff --git a/platform/android/src/offline/offline_manager.hpp b/platform/android/src/offline/offline_manager.hpp index 84111a7423..62d6b75b06 100644 --- a/platform/android/src/offline/offline_manager.hpp +++ b/platform/android/src/offline/offline_manager.hpp @@ -104,7 +104,7 @@ public: void runPackDatabaseAutomatically(jni::JNIEnv&, jboolean autopack); private: - std::shared_ptr<mbgl::DatabaseFileSource> fileSource; + std::shared_ptr<JointDatabaseStorage> databaseStorage; }; } // namespace android diff --git a/platform/android/src/offline/offline_region.cpp b/platform/android/src/offline/offline_region.cpp index de7abef624..53c1c7d014 100644 --- a/platform/android/src/offline/offline_region.cpp +++ b/platform/android/src/offline/offline_region.cpp @@ -16,10 +16,9 @@ namespace android { OfflineRegion::OfflineRegion(jni::JNIEnv& env, jni::jlong offlineRegionPtr, const jni::Object<FileSource>& jFileSource) : region(reinterpret_cast<mbgl::OfflineRegion*>(offlineRegionPtr)), - fileSource(std::static_pointer_cast<mbgl::DatabaseFileSource>( - std::shared_ptr<mbgl::FileSource>(mbgl::FileSourceManager::get()->getFileSource( - mbgl::FileSourceType::Database, FileSource::getSharedResourceOptions(env, jFileSource))))) { - if (!fileSource) { + databaseStorage( + FileSourceManager::get()->getDatabaseStorage(FileSource::getSharedResourceOptions(env, jFileSource))) { + if (!databaseStorage) { ThrowNew(env, jni::FindClass(env, "java/lang/IllegalStateException"), "Offline functionality is disabled."); } } @@ -69,7 +68,8 @@ void OfflineRegion::setOfflineRegionObserver(jni::JNIEnv& env_, const jni::Objec }; // Set the observer - fileSource->setOfflineRegionObserver(*region, std::make_unique<Observer>(jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback))); + databaseStorage->setOfflineRegionObserver( + *region, std::make_unique<Observer>(jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback))); } void OfflineRegion::setOfflineRegionDownloadState(jni::JNIEnv&, jni::jint jState) { @@ -87,83 +87,88 @@ void OfflineRegion::setOfflineRegionDownloadState(jni::JNIEnv&, jni::jint jState return; } - fileSource->setOfflineRegionDownloadState(*region, state); + databaseStorage->setOfflineRegionDownloadState(*region, state); } void OfflineRegion::getOfflineRegionStatus(jni::JNIEnv& env_, const jni::Object<OfflineRegionStatusCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->getOfflineRegionStatus(*region, [ - //Ensure the object is not gc'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)) - ](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (status) { - OfflineRegionStatusCallback::onStatus(*env, *callback, std::move(*status)); - } else { - OfflineRegionStatusCallback::onError(*env, *callback, status.error()); - } - }); + databaseStorage->getOfflineRegionStatus( + *region, + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))]( + mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (status) { + OfflineRegionStatusCallback::onStatus(*env, *callback, std::move(*status)); + } else { + OfflineRegionStatusCallback::onError(*env, *callback, status.error()); + } + }); } void OfflineRegion::deleteOfflineRegion(jni::JNIEnv& env_, const jni::Object<OfflineRegionDeleteCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->deleteOfflineRegion(std::move(*region), - [ - // Ensure the object is not gc'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>( - std::move(globalCallback))](std::exception_ptr error) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (error) { - OfflineRegionDeleteCallback::onError(*env, *callback, error); - } else { - OfflineRegionDeleteCallback::onDelete(*env, *callback); - } - }); + databaseStorage->deleteOfflineRegion(std::move(*region), + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>( + std::move(globalCallback))](std::exception_ptr error) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (error) { + OfflineRegionDeleteCallback::onError(*env, *callback, error); + } else { + OfflineRegionDeleteCallback::onDelete(*env, *callback); + } + }); } void OfflineRegion::invalidateOfflineRegion(jni::JNIEnv& env_, const jni::Object<OfflineRegionInvalidateCallback>& callback_) { auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->invalidateOfflineRegion(*region, - [ - // Ensure the object is not gc'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>( - std::move(globalCallback))](std::exception_ptr error) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (error) { - OfflineRegionInvalidateCallback::onError(*env, *callback, error); - } else { - OfflineRegionInvalidateCallback::onInvalidate(*env, *callback); - } - }); + databaseStorage->invalidateOfflineRegion(*region, + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>( + std::move(globalCallback))](std::exception_ptr error) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (error) { + OfflineRegionInvalidateCallback::onError(*env, *callback, error); + } else { + OfflineRegionInvalidateCallback::onInvalidate(*env, *callback); + } + }); } void OfflineRegion::updateOfflineRegionMetadata(jni::JNIEnv& env_, const jni::Array<jni::jbyte>& jMetadata, const jni::Object<OfflineRegionUpdateMetadataCallback>& callback_) { auto metadata = OfflineRegion::metadata(env_, jMetadata); auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_); - fileSource->updateOfflineMetadata(region->getID(), metadata, [ - //Ensure the object is not gc'd in the meanwhile - callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback)) - ](mbgl::expected<mbgl::OfflineRegionMetadata, std::exception_ptr> data) mutable { - // Reattach, the callback comes from a different thread - android::UniqueEnv env = android::AttachEnv(); - - if (data) { - OfflineRegionUpdateMetadataCallback::onUpdate(*env, *callback, std::move(*data)); - } else { - OfflineRegionUpdateMetadataCallback::onError(*env, *callback, data.error()); - } - }); + databaseStorage->updateOfflineMetadata( + region->getID(), + metadata, + [ + // Ensure the object is not gc'd in the meanwhile + callback = std::make_shared<decltype(globalCallback)>(std::move(globalCallback))]( + mbgl::expected<mbgl::OfflineRegionMetadata, std::exception_ptr> data) mutable { + // Reattach, the callback comes from a different thread + android::UniqueEnv env = android::AttachEnv(); + + if (data) { + OfflineRegionUpdateMetadataCallback::onUpdate(*env, *callback, std::move(*data)); + } else { + OfflineRegionUpdateMetadataCallback::onError(*env, *callback, data.error()); + } + }); } jni::Local<jni::Object<OfflineRegion>> OfflineRegion::New(jni::JNIEnv& env, diff --git a/platform/android/src/offline/offline_region.hpp b/platform/android/src/offline/offline_region.hpp index 6844008bb4..34fa156476 100644 --- a/platform/android/src/offline/offline_region.hpp +++ b/platform/android/src/offline/offline_region.hpp @@ -85,7 +85,7 @@ public: private: std::unique_ptr<mbgl::OfflineRegion> region; - std::shared_ptr<mbgl::DatabaseFileSource> fileSource; + std::shared_ptr<mbgl::JointDatabaseStorage> databaseStorage; }; } // namespace android |