diff options
author | Leith Bade <leith@mapbox.com> | 2014-12-02 19:40:07 +1100 |
---|---|---|
committer | Leith Bade <leith@mapbox.com> | 2014-12-02 19:41:50 +1100 |
commit | 2b865224127c2b5126e089e6384795282c3601cd (patch) | |
tree | 2d17d5834b1f48c5d49d0f17a5b914fe875c078d /platform/android | |
parent | 9a8c018a32c351954f96cfe587c3f108ce4a986b (diff) | |
download | qtlocation-mapboxgl-2b865224127c2b5126e089e6384795282c3601cd.tar.gz |
Share asset_request between platforms
Diffstat (limited to 'platform/android')
-rw-r--r-- | platform/android/asset_request.cpp | 37 | ||||
-rw-r--r-- | platform/android/asset_request_baton.cpp | 138 | ||||
-rw-r--r-- | platform/android/asset_request_baton_libzip.cpp | 97 |
3 files changed, 97 insertions, 175 deletions
diff --git a/platform/android/asset_request.cpp b/platform/android/asset_request.cpp deleted file mode 100644 index 4148916d26..0000000000 --- a/platform/android/asset_request.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include <mbgl/platform/android/asset_request.hpp> -#include <mbgl/platform/android/asset_request_baton.hpp> -#include <mbgl/storage/response.hpp> - -#include <uv.h> - -#include <cassert> - -#include <unistd.h> - -namespace mbgl { - -AssetRequest::AssetRequest(const std::string &path_, uv_loop_t *loop) - : BaseRequest(path_), ptr(new AssetRequestBaton(this, path, loop)) { -} - -void AssetRequest::cancel() { - assert(thread_id == uv_thread_self()); - - if (ptr) { - ptr->cancel(); - - // When deleting a AssetRequest object with a uv_fs_* call is in progress, we are making sure - // that the callback doesn't accidentally reference this object again. - ptr->request = nullptr; - ptr = nullptr; - } - - notify(); -} - -AssetRequest::~AssetRequest() { - assert(thread_id == uv_thread_self()); - cancel(); -} - -} diff --git a/platform/android/asset_request_baton.cpp b/platform/android/asset_request_baton.cpp deleted file mode 100644 index 84909a50e7..0000000000 --- a/platform/android/asset_request_baton.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include <mbgl/android/jni.hpp> -#include <mbgl/platform/android/asset_request_baton.hpp> -#include <mbgl/platform/android/asset_request.hpp> -#include <mbgl/storage/response.hpp> - -#include <limits> -#include <boost/make_unique.hpp> - -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<Response>(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<char[]> message = boost::make_unique<char[]>(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<char[]> data = boost::make_unique<char[]>(stat.size); - - if (static_cast<zip_uint64_t>(zip_fread(ptr->apk_file, reinterpret_cast<void *>(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<Response>(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 0000000000..80f1d02dd1 --- /dev/null +++ b/platform/android/asset_request_baton_libzip.cpp @@ -0,0 +1,97 @@ +#include <mbgl/android/jni.hpp> +#include <mbgl/storage/asset_request_baton.hpp> +#include <mbgl/storage/asset_request.hpp> +#include <mbgl/storage/response.hpp> + +#include <limits> +#include <boost/make_unique.hpp> + +#include <zip.h> + +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<char[]> message = boost::make_unique<char[]>(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<char[]> data = boost::make_unique<char[]>(stat.size); + + if (static_cast<zip_uint64_t>(zip_fread(apk_file, reinterpret_cast<void *>(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<Response>(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); +} + +} |