Create AssetRequest
This commit is contained in:
@@ -29,6 +29,8 @@
|
||||
'sources': [
|
||||
'../platform/android/cache_database_data.cpp',
|
||||
'../platform/android/log_android.cpp',
|
||||
'../platform/android/asset_request.cpp',
|
||||
'../platform/android/asset_request_baton.cpp',
|
||||
'../platform/default/string_stdlib.cpp',
|
||||
'../platform/default/http_request_baton_curl.cpp',
|
||||
'../platform/default/image.cpp',
|
||||
|
||||
27
include/mbgl/platform/android/asset_request.hpp
Normal file
27
include/mbgl/platform/android/asset_request.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef MBGL_STORAGE_ASSET_REQUEST
|
||||
#define MBGL_STORAGE_ASSET_REQUEST
|
||||
|
||||
#include <mbgl/storage/base_request.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
typedef struct uv_loop_s uv_loop_t;
|
||||
|
||||
struct AssetRequestBaton;
|
||||
|
||||
class AssetRequest : public BaseRequest {
|
||||
public:
|
||||
AssetRequest(const std::string &path, uv_loop_t *loop);
|
||||
~AssetRequest();
|
||||
|
||||
void cancel();
|
||||
|
||||
private:
|
||||
AssetRequestBaton *ptr = nullptr;
|
||||
|
||||
friend struct AssetRequestBaton;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
35
include/mbgl/platform/android/asset_request_baton.hpp
Normal file
35
include/mbgl/platform/android/asset_request_baton.hpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef MBGL_STORAGE_ASSET_REQUEST_BATON
|
||||
#define MBGL_STORAGE_ASSET_REQUEST_BATON
|
||||
|
||||
#include <mbgl/platform/android/asset_request.hpp>
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
struct AssetRequestBaton {
|
||||
AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop);
|
||||
~AssetRequestBaton();
|
||||
|
||||
void cancel();
|
||||
static void file_opened(uv_fs_t *req);
|
||||
static void file_stated(uv_fs_t *req);
|
||||
static void file_read(uv_fs_t *req);
|
||||
static void file_closed(uv_fs_t *req);
|
||||
static void notify_error(uv_fs_t *req);
|
||||
static void cleanup(uv_fs_t *req);
|
||||
|
||||
const unsigned long thread_id;
|
||||
AssetRequest *request = nullptr;
|
||||
uv_fs_t req;
|
||||
uv_file fd = -1;
|
||||
bool canceled = false;
|
||||
std::string body;
|
||||
uv_buf_t buffer;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
37
platform/android/asset_request.cpp
Normal file
37
platform/android/asset_request.cpp
Normal file
@@ -0,0 +1,37 @@
|
||||
#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();
|
||||
}
|
||||
|
||||
}
|
||||
159
platform/android/asset_request_baton.cpp
Normal file
159
platform/android/asset_request_baton.cpp
Normal file
@@ -0,0 +1,159 @@
|
||||
#include <mbgl/platform/android/asset_request_baton.hpp>
|
||||
#include <mbgl/platform/android/asset_request.hpp>
|
||||
#include <mbgl/storage/response.hpp>
|
||||
|
||||
#include <limits>
|
||||
|
||||
namespace mbgl {
|
||||
|
||||
AssetRequestBaton::AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop)
|
||||
: thread_id(uv_thread_self()), request(request_) {
|
||||
req.data = this;
|
||||
uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, file_opened);
|
||||
}
|
||||
|
||||
AssetRequestBaton::~AssetRequestBaton() {
|
||||
}
|
||||
|
||||
void AssetRequestBaton::cancel() {
|
||||
canceled = true;
|
||||
|
||||
// uv_cancel fails frequently when the request has already been started.
|
||||
// In that case, we have to let it complete and check the canceled bool
|
||||
// instead.
|
||||
uv_cancel((uv_req_t *)&req);
|
||||
}
|
||||
|
||||
void AssetRequestBaton::notify_error(uv_fs_t *req) {
|
||||
AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
|
||||
assert(ptr->thread_id == uv_thread_self());
|
||||
|
||||
if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) {
|
||||
ptr->request->response = std::unique_ptr<Response>(new Response);
|
||||
ptr->request->response->code = req->result == UV_ENOENT ? 404 : 500;
|
||||
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
|
||||
ptr->request->response->message = uv_strerror(uv_last_error(req->loop));
|
||||
#else
|
||||
ptr->request->response->message = uv_strerror(int(req->result));
|
||||
#endif
|
||||
ptr->request->notify();
|
||||
}
|
||||
}
|
||||
|
||||
void AssetRequestBaton::file_opened(uv_fs_t *req) {
|
||||
AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
|
||||
assert(ptr->thread_id == uv_thread_self());
|
||||
|
||||
if (req->result < 0) {
|
||||
// Opening failed or was canceled. There isn't much left we can do.
|
||||
notify_error(req);
|
||||
cleanup(req);
|
||||
} else {
|
||||
const uv_file fd = uv_file(req->result);
|
||||
|
||||
// We're going to reuse this handle, so we need to cleanup first.
|
||||
uv_fs_req_cleanup(req);
|
||||
|
||||
if (ptr->canceled || !ptr->request) {
|
||||
// Either the AssetRequest object has been destructed, or the
|
||||
// request was canceled.
|
||||
uv_fs_close(req->loop, req, fd, file_closed);
|
||||
} else {
|
||||
ptr->fd = fd;
|
||||
uv_fs_fstat(req->loop, req, fd, file_stated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AssetRequestBaton::file_stated(uv_fs_t *req) {
|
||||
AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
|
||||
assert(ptr->thread_id == uv_thread_self());
|
||||
|
||||
if (req->result != 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(req);
|
||||
|
||||
uv_fs_req_cleanup(req);
|
||||
uv_fs_close(req->loop, req, ptr->fd, file_closed);
|
||||
} else {
|
||||
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
|
||||
const uv_statbuf_t *stat = static_cast<const uv_statbuf_t *>(req->ptr);
|
||||
#else
|
||||
const uv_stat_t *stat = static_cast<const uv_stat_t *>(req->ptr);
|
||||
#endif
|
||||
if (stat->st_size > std::numeric_limits<int>::max()) {
|
||||
// File is too large for us to open this way because uv_buf's only support unsigned
|
||||
// ints as maximum size.
|
||||
if (ptr->request) {
|
||||
ptr->request->response = std::unique_ptr<Response>(new Response);
|
||||
ptr->request->response->code = UV_EFBIG;
|
||||
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
|
||||
ptr->request->response->message = uv_strerror(uv_err_t {UV_EFBIG, 0});
|
||||
#else
|
||||
ptr->request->response->message = uv_strerror(UV_EFBIG);
|
||||
#endif
|
||||
ptr->request->notify();
|
||||
}
|
||||
|
||||
uv_fs_req_cleanup(req);
|
||||
uv_fs_close(req->loop, req, ptr->fd, file_closed);
|
||||
} else {
|
||||
const unsigned int size = (unsigned int)(stat->st_size);
|
||||
ptr->body.resize(size);
|
||||
ptr->buffer = uv_buf_init(const_cast<char *>(ptr->body.data()), size);
|
||||
uv_fs_req_cleanup(req);
|
||||
#if UV_VERSION_MAJOR == 0 && UV_VERSION_MINOR <= 10
|
||||
uv_fs_read(req->loop, req, ptr->fd, ptr->buffer.base, ptr->buffer.len, -1, file_read);
|
||||
#else
|
||||
uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, file_read);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AssetRequestBaton::file_read(uv_fs_t *req) {
|
||||
AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
|
||||
assert(ptr->thread_id == uv_thread_self());
|
||||
|
||||
if (req->result < 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(req);
|
||||
} else {
|
||||
// File was successfully read.
|
||||
if (ptr->request) {
|
||||
ptr->request->response = std::unique_ptr<Response>(new Response);
|
||||
ptr->request->response->code = 200;
|
||||
ptr->request->response->data = std::move(ptr->body);
|
||||
ptr->request->notify();
|
||||
}
|
||||
}
|
||||
|
||||
uv_fs_req_cleanup(req);
|
||||
uv_fs_close(req->loop, req, ptr->fd, file_closed);
|
||||
}
|
||||
|
||||
void AssetRequestBaton::file_closed(uv_fs_t *req) {
|
||||
assert(((AssetRequestBaton *)req->data)->thread_id == uv_thread_self());
|
||||
|
||||
if (req->result < 0) {
|
||||
// Closing the file failed. But there isn't anything we can do.
|
||||
}
|
||||
|
||||
cleanup(req);
|
||||
}
|
||||
|
||||
void AssetRequestBaton::cleanup(uv_fs_t *req) {
|
||||
AssetRequestBaton *ptr = (AssetRequestBaton *)req->data;
|
||||
assert(ptr->thread_id == uv_thread_self());
|
||||
|
||||
if (ptr->request) {
|
||||
ptr->request->ptr = nullptr;
|
||||
}
|
||||
|
||||
uv_fs_req_cleanup(req);
|
||||
delete ptr;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -4,6 +4,10 @@
|
||||
#include <mbgl/storage/sqlite_store.hpp>
|
||||
#include <mbgl/util/uv-messenger.h>
|
||||
|
||||
#ifdef __ANDROID__
|
||||
#include <mbgl/platform/android/asset_request.hpp>
|
||||
#endif
|
||||
|
||||
#include <uv.h>
|
||||
|
||||
namespace mbgl {
|
||||
@@ -72,6 +76,10 @@ 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, 7) == "asset://") {
|
||||
req = std::make_shared<AssetRequest>(absoluteURL.substr(7), loop);
|
||||
#endif
|
||||
} else {
|
||||
req = std::make_shared<HTTPRequest>(type, absoluteURL, loop, store);
|
||||
}
|
||||
@@ -102,4 +110,4 @@ void FileSource::retryAllPending() {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user