summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-11-08 17:58:52 +0200
committerMikhail Pozdnyakov <mikhail.pozdnyakov@mapbox.com>2019-11-12 17:24:50 +0200
commit8451633c17b826b938596a2483840bae3b94c5cc (patch)
tree75e6924628499191aa950f1799754b1f7148c524 /platform
parent2edbe916ab33301032c15a45ee9c8b7f37f0aa57 (diff)
downloadqtlocation-mapboxgl-8451633c17b826b938596a2483840bae3b94c5cc.tar.gz
[android] Consolidate exception handling code in OfflineManager
Diffstat (limited to 'platform')
-rw-r--r--platform/android/src/offline/offline_manager.cpp103
1 files changed, 36 insertions, 67 deletions
diff --git a/platform/android/src/offline/offline_manager.cpp b/platform/android/src/offline/offline_manager.cpp
index 8d76f68522..c45386e3a7 100644
--- a/platform/android/src/offline/offline_manager.cpp
+++ b/platform/android/src/offline/offline_manager.cpp
@@ -7,6 +7,20 @@
namespace mbgl {
namespace android {
+namespace {
+// Reattach, the callback comes from a different thread
+void handleException(std::exception_ptr exception,
+ const jni::Object<OfflineManager::FileSourceCallback>& callback,
+ android::UniqueEnv env = android::AttachEnv()) {
+ if (exception) {
+ OfflineManager::FileSourceCallback::onError(
+ *env, callback, jni::Make<jni::String>(*env, mbgl::util::toString(exception)));
+ } else {
+ OfflineManager::FileSourceCallback::onSuccess(*env, callback);
+ }
+}
+} // namespace
+
// OfflineManager //
OfflineManager::OfflineManager(jni::JNIEnv& env, const jni::Object<FileSource>& jFileSource)
: fileSource(std::static_pointer_cast<DefaultFileSource>(mbgl::FileSource::getSharedFileSource(FileSource::getSharedResourceOptions(env, jFileSource)))) {}
@@ -106,20 +120,11 @@ void OfflineManager::mergeOfflineRegions(jni::JNIEnv& env_, const jni::Object<Fi
void OfflineManager::resetDatabase(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) {
auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_);
- fileSource->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))
- ](std::exception_ptr exception) mutable {
-
- // Reattach, the callback comes from a different thread
- android::UniqueEnv env = android::AttachEnv();
-
- if (exception) {
- OfflineManager::FileSourceCallback::onError(*env, *callback, jni::Make<jni::String>(*env, mbgl::util::toString(exception)));
- } else {
- OfflineManager::FileSourceCallback::onSuccess(*env, *callback);
- }
- });
+ fileSource->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))](
+ std::exception_ptr exception) mutable { handleException(exception, *callback); });
}
void OfflineManager::packDatabase(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) {
@@ -129,74 +134,38 @@ void OfflineManager::packDatabase(jni::JNIEnv& env_, const jni::Object<FileSourc
[
// 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))](
- std::exception_ptr exception) mutable {
- // Reattach, the callback comes from a different thread
- android::UniqueEnv env = android::AttachEnv();
-
- if (exception) {
- OfflineManager::FileSourceCallback::onError(
- *env, *callback, jni::Make<jni::String>(*env, mbgl::util::toString(exception)));
- } else {
- OfflineManager::FileSourceCallback::onSuccess(*env, *callback);
- }
- });
+ std::exception_ptr exception) mutable { handleException(exception, *callback); });
}
void OfflineManager::invalidateAmbientCache(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) {
auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_);
- fileSource->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))
- ](std::exception_ptr exception) mutable {
-
- // Reattach, the callback comes from a different thread
- android::UniqueEnv env = android::AttachEnv();
-
- if (exception) {
- OfflineManager::FileSourceCallback::onError(*env, *callback, jni::Make<jni::String>(*env, mbgl::util::toString(exception)));
- } else {
- OfflineManager::FileSourceCallback::onSuccess(*env, *callback);
- }
- });
+ fileSource->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))](
+ std::exception_ptr exception) mutable { handleException(exception, *callback); });
}
void OfflineManager::clearAmbientCache(jni::JNIEnv& env_, const jni::Object<FileSourceCallback>& callback_) {
auto globalCallback = jni::NewGlobal<jni::EnvAttachingDeleter>(env_, callback_);
- fileSource->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))
- ](std::exception_ptr exception) mutable {
-
- // Reattach, the callback comes from a different thread
- android::UniqueEnv env = android::AttachEnv();
-
- if (exception) {
- OfflineManager::FileSourceCallback::onError(*env, *callback, jni::Make<jni::String>(*env, mbgl::util::toString(exception)));
- } else {
- OfflineManager::FileSourceCallback::onSuccess(*env, *callback);
- }
- });
+ fileSource->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))](
+ std::exception_ptr exception) mutable { handleException(exception, *callback); });
}
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(size_, [
- //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))
- ](std::exception_ptr exception) mutable {
-
- // Reattach, the callback comes from a different thread
- android::UniqueEnv env = android::AttachEnv();
-
- if (exception) {
- OfflineManager::FileSourceCallback::onError(*env, *callback, jni::Make<jni::String>(*env, mbgl::util::toString(exception)));
- } else {
- OfflineManager::FileSourceCallback::onSuccess(*env, *callback);
- }
- });
+ fileSource->setMaximumAmbientCacheSize(
+ size_,
+ [
+ // 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))](
+ std::exception_ptr exception) mutable { handleException(exception, *callback); });
}
// FileSource::FileSourceCallback //