From 2b865224127c2b5126e089e6384795282c3601cd Mon Sep 17 00:00:00 2001 From: Leith Bade Date: Tue, 2 Dec 2014 19:40:07 +1100 Subject: [PATCH] Share asset_request between platforms --- gyp/mbgl-android.gypi | 3 +- gyp/mbgl-ios.gypi | 1 + gyp/mbgl-linux.gypi | 1 + gyp/mbgl-osx.gypi | 1 + .../android => storage}/asset_request.hpp | 0 .../asset_request_baton.hpp | 20 +-- platform/android/asset_request_baton.cpp | 138 ------------------ .../android/asset_request_baton_libzip.cpp | 97 ++++++++++++ platform/default/asset_request_baton_noop.cpp | 26 ++++ .../android => src/storage}/asset_request.cpp | 4 +- src/storage/asset_request_baton.cpp | 54 +++++++ src/storage/file_source.cpp | 7 +- 12 files changed, 195 insertions(+), 157 deletions(-) rename include/mbgl/{platform/android => storage}/asset_request.hpp (100%) rename include/mbgl/{platform/android => storage}/asset_request_baton.hpp (79%) delete mode 100644 platform/android/asset_request_baton.cpp create mode 100644 platform/android/asset_request_baton_libzip.cpp create mode 100644 platform/default/asset_request_baton_noop.cpp rename {platform/android => src/storage}/asset_request.cpp (87%) create mode 100644 src/storage/asset_request_baton.cpp diff --git a/gyp/mbgl-android.gypi b/gyp/mbgl-android.gypi index 10ea93338..ff537792b 100644 --- a/gyp/mbgl-android.gypi +++ b/gyp/mbgl-android.gypi @@ -32,8 +32,7 @@ 'sources': [ '../platform/android/cache_database_data.cpp', '../platform/android/log_android.cpp', - '../platform/android/asset_request.cpp', - '../platform/android/asset_request_baton.cpp', + '../platform/android/asset_request_baton_libzip.cpp', '../platform/default/string_stdlib.cpp', '../platform/default/http_request_baton_curl.cpp', '../platform/default/image.cpp', diff --git a/gyp/mbgl-ios.gypi b/gyp/mbgl-ios.gypi index dd1c9c0c2..c2cb2eed5 100644 --- a/gyp/mbgl-ios.gypi +++ b/gyp/mbgl-ios.gypi @@ -36,6 +36,7 @@ '../platform/darwin/string_nsstring.mm', '../platform/darwin/http_request_baton_cocoa.mm', '../platform/darwin/image.mm', + '../platform/default/asset_request_baton_noop.cpp', ], 'include_dirs': [ '../include', diff --git a/gyp/mbgl-linux.gypi b/gyp/mbgl-linux.gypi index aee928685..7a249d863 100644 --- a/gyp/mbgl-linux.gypi +++ b/gyp/mbgl-linux.gypi @@ -35,6 +35,7 @@ '../platform/default/image_reader.cpp', '../platform/default/png_reader.cpp', '../platform/default/jpeg_reader.cpp', + '../platform/default/asset_request_baton_noop.cpp', ], 'include_dirs': [ '../include', diff --git a/gyp/mbgl-osx.gypi b/gyp/mbgl-osx.gypi index 76c22f5ad..0ca00a81e 100644 --- a/gyp/mbgl-osx.gypi +++ b/gyp/mbgl-osx.gypi @@ -11,6 +11,7 @@ '../platform/darwin/string_nsstring.mm', '../platform/darwin/http_request_baton_cocoa.mm', '../platform/darwin/image.mm', + '../platform/default/asset_request_baton_noop.cpp', ], 'include_dirs': [ '../include', diff --git a/include/mbgl/platform/android/asset_request.hpp b/include/mbgl/storage/asset_request.hpp similarity index 100% rename from include/mbgl/platform/android/asset_request.hpp rename to include/mbgl/storage/asset_request.hpp diff --git a/include/mbgl/platform/android/asset_request_baton.hpp b/include/mbgl/storage/asset_request_baton.hpp similarity index 79% rename from include/mbgl/platform/android/asset_request_baton.hpp rename to include/mbgl/storage/asset_request_baton.hpp index 1acbfbae0..b9c337259 100644 --- a/include/mbgl/platform/android/asset_request_baton.hpp +++ b/include/mbgl/storage/asset_request_baton.hpp @@ -1,10 +1,9 @@ #ifndef MBGL_STORAGE_ASSET_REQUEST_BATON #define MBGL_STORAGE_ASSET_REQUEST_BATON -#include +#include #include -#include namespace mbgl { @@ -12,18 +11,21 @@ struct AssetRequestBaton { AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop); ~AssetRequestBaton(); - void cancel(); - static void run(uv_async_t *async); - static void notify_error(uv_async_t *async, const int code, const char *message); - static void cleanup(uv_async_t *async); - const unsigned long thread_id; AssetRequest *request = nullptr; std::unique_ptr async_run; - struct zip *apk = nullptr; - struct zip_file *apk_file = nullptr; std::string path; bool canceled = false; + + void cancel(); + static void notify_error(uv_async_t *async, const int code, const char *message); + static void cleanup(uv_async_t *async); + + // IMPLEMENT THIS PLATFORM SPECIFIC FUNCTION: + + // Called to load the asset. Platform-specific implementation. + static void run(uv_async_t *async); + }; diff --git a/platform/android/asset_request_baton.cpp b/platform/android/asset_request_baton.cpp deleted file mode 100644 index 84909a50e..000000000 --- a/platform/android/asset_request_baton.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include -#include -#include -#include - -#include -#include - -namespace mbgl { - -AssetRequestBaton::AssetRequestBaton(AssetRequest *request_, const std::string &path_, uv_loop_t *loop) - : thread_id(uv_thread_self()), - request(request_), - async_run(new uv_async_t()), - path(path_) { - - uv_async_init(loop, async_run.get(), run); - async_run->data = this; - - uv_async_send(async_run.get()); -} - -AssetRequestBaton::~AssetRequestBaton() { - uv_close((uv_handle_t *)async_run.get(), nullptr); -} - -void AssetRequestBaton::cancel() { - canceled = true; -} - -void AssetRequestBaton::notify_error(uv_async_t *async, const int code, const char *message) { - AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; - assert(ptr->thread_id == uv_thread_self()); - - if (ptr->request && !ptr->canceled) { - ptr->request->response = std::unique_ptr(new Response); - ptr->request->response->code = code; - ptr->request->response->message = message; - ptr->request->notify(); - } -} - -void AssetRequestBaton::run(uv_async_t *async) { - AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; - assert(ptr->thread_id == uv_thread_self()); - - if (ptr->canceled || !ptr->request) { - // Either the AssetRequest object has been destructed, or the - // request was canceled. - cleanup(async); - return; - } - - int error = 0; - ptr->apk = zip_open(mbgl::android::apk_path.c_str(), 0, &error); - if ((ptr->apk == nullptr) || ptr->canceled || !ptr->request) { - // Opening the APK failed or was canceled. There isn't much left we can do. - const int message_size = zip_error_to_str(nullptr, 0, error, errno); - const std::unique_ptr message = boost::make_unique(message_size); - zip_error_to_str(message.get(), 0, error, errno); - notify_error(async, 500, message.get()); - cleanup(async); - return; - } - - std::string apk_file_path = "assets/" + ptr->path; - ptr->apk_file = zip_fopen(ptr->apk, apk_file_path.c_str(), ZIP_FL_NOCASE); - if ((ptr->apk_file == nullptr) || ptr->canceled || !ptr->request) { - // Opening the asset failed or was canceled. We already have an open file handle - // though, which we'll have to close. - zip_error_get(ptr->apk, &error, nullptr); - notify_error(async, error == ZIP_ER_NOENT ? 404 : 500, zip_strerror(ptr->apk)); - zip_close(ptr->apk); - ptr->apk = nullptr; - cleanup(async); - return; - } - - struct zip_stat stat; - if ((zip_stat(ptr->apk, apk_file_path.c_str(), ZIP_FL_NOCASE, &stat) != 0) || ptr->canceled || !ptr->request) { - // Stating failed or was canceled. We already have an open file handle - // though, which we'll have to close. - notify_error(async, 500, zip_strerror(ptr->apk)); - zip_fclose(ptr->apk_file); - ptr->apk_file = nullptr; - zip_close(ptr->apk); - ptr->apk = nullptr; - cleanup(async); - return; - } - - const std::unique_ptr data = boost::make_unique(stat.size); - - if (static_cast(zip_fread(ptr->apk_file, reinterpret_cast(data.get()), stat.size)) != stat.size || ptr->canceled || !ptr->request) { - // Reading failed or was canceled. We already have an open file handle - // though, which we'll have to close. - notify_error(async, 500, zip_file_strerror(ptr->apk_file)); - zip_fclose(ptr->apk_file); - ptr->apk_file = nullptr; - zip_close(ptr->apk); - ptr->apk = nullptr; - cleanup(async); - return; - } - - if (ptr->request) { - ptr->request->response = std::unique_ptr(new Response); - ptr->request->response->code = 200; - std::string body(data.get(), stat.size); - ptr->request->response->data = body; - ptr->request->notify(); - } - - if (zip_fclose(ptr->apk_file) != 0) { - // Closing the asset failed. But there isn't anything we can do. - } - ptr->apk_file = nullptr; - - if (zip_close(ptr->apk) != 0) { - // Closing the APK failed. But there isn't anything we can do. - } - ptr->apk = nullptr; - - cleanup(async); -} - -void AssetRequestBaton::cleanup(uv_async_t *async) { - AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; - assert(ptr->thread_id == uv_thread_self()); - - if (ptr->request) { - ptr->request->ptr = nullptr; - } - - delete ptr; -} - -} diff --git a/platform/android/asset_request_baton_libzip.cpp b/platform/android/asset_request_baton_libzip.cpp new file mode 100644 index 000000000..80f1d02dd --- /dev/null +++ b/platform/android/asset_request_baton_libzip.cpp @@ -0,0 +1,97 @@ +#include +#include +#include +#include + +#include +#include + +#include + +namespace mbgl { + +void AssetRequestBaton::run(uv_async_t *async) { + AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; + assert(ptr->thread_id == uv_thread_self()); + + if (ptr->canceled || !ptr->request) { + // Either the AssetRequest object has been destructed, or the + // request was canceled. + cleanup(async); + return; + } + + int error = 0; + struct zip *apk = zip_open(mbgl::android::apk_path.c_str(), 0, &error); + if ((apk == nullptr) || ptr->canceled || !ptr->request) { + // Opening the APK failed or was canceled. There isn't much left we can do. + const int message_size = zip_error_to_str(nullptr, 0, error, errno); + const std::unique_ptr message = boost::make_unique(message_size); + zip_error_to_str(message.get(), 0, error, errno); + notify_error(async, 500, message.get()); + cleanup(async); + return; + } + + std::string apk_file_path = "assets/" + ptr->path; + struct zip_file *apk_file = zip_fopen(apk, apk_file_path.c_str(), ZIP_FL_NOCASE); + if ((apk_file == nullptr) || ptr->canceled || !ptr->request) { + // Opening the asset failed or was canceled. We already have an open file handle + // though, which we'll have to close. + zip_error_get(apk, &error, nullptr); + notify_error(async, error == ZIP_ER_NOENT ? 404 : 500, zip_strerror(apk)); + zip_close(apk); + apk = nullptr; + cleanup(async); + return; + } + + struct zip_stat stat; + if ((zip_stat(apk, apk_file_path.c_str(), ZIP_FL_NOCASE, &stat) != 0) || ptr->canceled || !ptr->request) { + // Stating failed or was canceled. We already have an open file handle + // though, which we'll have to close. + notify_error(async, 500, zip_strerror(apk)); + zip_fclose(apk_file); + apk_file = nullptr; + zip_close(apk); + apk = nullptr; + cleanup(async); + return; + } + + const std::unique_ptr data = boost::make_unique(stat.size); + + if (static_cast(zip_fread(apk_file, reinterpret_cast(data.get()), stat.size)) != stat.size || ptr->canceled || !ptr->request) { + // Reading failed or was canceled. We already have an open file handle + // though, which we'll have to close. + notify_error(async, 500, zip_file_strerror(apk_file)); + zip_fclose(apk_file); + apk_file = nullptr; + zip_close(apk); + apk = nullptr; + cleanup(async); + return; + } + + if (ptr->request) { + ptr->request->response = std::unique_ptr(new Response); + ptr->request->response->code = 200; + std::string body(data.get(), stat.size); + ptr->request->response->data = body; + ptr->request->notify(); + } + + if (zip_fclose(apk_file) != 0) { + // Closing the asset failed. But there isn't anything we can do. + } + apk_file = nullptr; + + if (zip_close(apk) != 0) { + // Closing the APK failed. But there isn't anything we can do. + } + apk = nullptr; + + cleanup(async); +} + +} diff --git a/platform/default/asset_request_baton_noop.cpp b/platform/default/asset_request_baton_noop.cpp new file mode 100644 index 000000000..33e8ad739 --- /dev/null +++ b/platform/default/asset_request_baton_noop.cpp @@ -0,0 +1,26 @@ +#include +#include +#include +#include + +#include +#include + +namespace mbgl { + +void AssetRequestBaton::run(uv_async_t *async) { + AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; + assert(ptr->thread_id == uv_thread_self()); + + if (ptr->canceled || !ptr->request) { + // Either the AssetRequest object has been destructed, or the + // request was canceled. + cleanup(async); + return; + } + + notify_error(async, 500, "Assets not implemented on this platform."); + cleanup(async); +} + +} diff --git a/platform/android/asset_request.cpp b/src/storage/asset_request.cpp similarity index 87% rename from platform/android/asset_request.cpp rename to src/storage/asset_request.cpp index 4148916d2..0d0eed2c7 100644 --- a/platform/android/asset_request.cpp +++ b/src/storage/asset_request.cpp @@ -1,5 +1,5 @@ -#include -#include +#include +#include #include #include diff --git a/src/storage/asset_request_baton.cpp b/src/storage/asset_request_baton.cpp new file mode 100644 index 000000000..918324108 --- /dev/null +++ b/src/storage/asset_request_baton.cpp @@ -0,0 +1,54 @@ +#include +#include +#include +#include + +#include +#include + +namespace mbgl { + +AssetRequestBaton::AssetRequestBaton(AssetRequest *request_, const std::string &path_, uv_loop_t *loop) + : thread_id(uv_thread_self()), + request(request_), + async_run(new uv_async_t()), + path(path_) { + + uv_async_init(loop, async_run.get(), run); + async_run->data = this; + + uv_async_send(async_run.get()); +} + +AssetRequestBaton::~AssetRequestBaton() { + uv_close((uv_handle_t *)async_run.get(), nullptr); +} + +void AssetRequestBaton::cancel() { + canceled = true; +} + +void AssetRequestBaton::notify_error(uv_async_t *async, const int code, const char *message) { + AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; + assert(ptr->thread_id == uv_thread_self()); + + if (ptr->request && !ptr->canceled) { + ptr->request->response = std::unique_ptr(new Response); + ptr->request->response->code = code; + ptr->request->response->message = message; + ptr->request->notify(); + } +} + +void AssetRequestBaton::cleanup(uv_async_t *async) { + AssetRequestBaton *ptr = (AssetRequestBaton *)async->data; + assert(ptr->thread_id == uv_thread_self()); + + if (ptr->request) { + ptr->request->ptr = nullptr; + } + + delete ptr; +} + +} diff --git a/src/storage/file_source.cpp b/src/storage/file_source.cpp index a2ddb28e0..a8b249ee3 100644 --- a/src/storage/file_source.cpp +++ b/src/storage/file_source.cpp @@ -2,12 +2,9 @@ #include #include #include +#include #include -#ifdef __ANDROID__ - #include -#endif - #include namespace mbgl { @@ -77,10 +74,8 @@ std::unique_ptr FileSource::request(ResourceType type, const std::strin if (!req) { if (absoluteURL.substr(0, 7) == "file://") { req = std::make_shared(absoluteURL.substr(7), loop); -#ifdef __ANDROID__ } else if (absoluteURL.substr(0, 8) == "asset://") { req = std::make_shared(absoluteURL.substr(8), loop); -#endif } else { req = std::make_shared(type, absoluteURL, loop, store); }