Share asset_request between platforms

This commit is contained in:
Leith Bade
2014-12-02 19:40:07 +11:00
parent 9a8c018a32
commit 2b86522412
12 changed files with 195 additions and 157 deletions

View File

@@ -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',

View File

@@ -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',

View File

@@ -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',

View File

@@ -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',

View File

@@ -1,10 +1,9 @@
#ifndef MBGL_STORAGE_ASSET_REQUEST_BATON
#define MBGL_STORAGE_ASSET_REQUEST_BATON
#include <mbgl/platform/android/asset_request.hpp>
#include <mbgl/storage/asset_request.hpp>
#include <uv.h>
#include <zip.h>
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<uv_async_t> 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);
};

View File

@@ -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;
}
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,26 @@
#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>
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);
}
}

View File

@@ -1,5 +1,5 @@
#include <mbgl/platform/android/asset_request.hpp>
#include <mbgl/platform/android/asset_request_baton.hpp>
#include <mbgl/storage/asset_request.hpp>
#include <mbgl/storage/asset_request_baton.hpp>
#include <mbgl/storage/response.hpp>
#include <uv.h>

View File

@@ -0,0 +1,54 @@
#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>
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::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;
}
}

View File

@@ -2,12 +2,9 @@
#include <mbgl/storage/file_request.hpp>
#include <mbgl/storage/http_request.hpp>
#include <mbgl/storage/sqlite_store.hpp>
#include <mbgl/storage/asset_request.hpp>
#include <mbgl/util/uv-messenger.h>
#ifdef __ANDROID__
#include <mbgl/platform/android/asset_request.hpp>
#endif
#include <uv.h>
namespace mbgl {
@@ -77,10 +74,8 @@ std::unique_ptr<Request> FileSource::request(ResourceType type, const std::strin
if (!req) {
if (absoluteURL.substr(0, 7) == "file://") {
req = std::make_shared<FileRequest>(absoluteURL.substr(7), loop);
#ifdef __ANDROID__
} else if (absoluteURL.substr(0, 8) == "asset://") {
req = std::make_shared<AssetRequest>(absoluteURL.substr(8), loop);
#endif
} else {
req = std::make_shared<HTTPRequest>(type, absoluteURL, loop, store);
}