Merge branch 'master' of github.com:mapbox/mapbox-gl-native into android-mason

Conflicts:
	gyp/mbgl-ios.gypi
	gyp/mbgl-osx.gypi
	platform/default/asset_request_libuv.cpp
	src/mbgl/storage/caching_http_file_source.cpp
	src/mbgl/storage/file_request.cpp
	src/mbgl/storage/file_request.hpp
	src/mbgl/storage/file_request_baton.hpp
This commit is contained in:
Leith Bade
2014-12-18 12:39:36 +11:00
18 changed files with 175 additions and 135 deletions

View File

@@ -1,5 +1,5 @@
#include <mbgl/storage/caching_http_file_source.hpp>
#include <mbgl/storage/file_request.hpp>
#include <mbgl/storage/asset_request.hpp>
#include <mbgl/storage/http_request.hpp>
#include <mbgl/storage/sqlite_store.hpp>
#include <mbgl/storage/asset_request.hpp>
@@ -18,6 +18,8 @@ CachingHTTPFileSource::~CachingHTTPFileSource() {
}
void CachingHTTPFileSource::setLoop(uv_loop_t* loop_) {
assert(!loop);
threadId = std::this_thread::get_id();
store = !path.empty() ? util::ptr<SQLiteStore>(new SQLiteStore(loop_, path)) : nullptr;
loop = loop_;
@@ -52,6 +54,8 @@ void CachingHTTPFileSource::clearLoop() {
}
store.reset();
loop = nullptr;
}
void CachingHTTPFileSource::setBase(std::string value) {
@@ -96,9 +100,7 @@ std::unique_ptr<Request> CachingHTTPFileSource::request(ResourceType type, const
}
if (!req) {
if (url.substr(0, 7) == "file://") {
req = std::make_shared<FileRequest>(url.substr(7), loop);
} else if (url.substr(0, 8) == "asset://") {
if (url.substr(0, 8) == "asset://") {
req = std::make_shared<AssetRequest>(url.substr(8), loop);
} else {
req = std::make_shared<HTTPRequest>(type, url, loop, store);

View File

@@ -1,37 +0,0 @@
#include <mbgl/storage/file_request.hpp>
#include <mbgl/storage/file_request_baton.hpp>
#include <mbgl/storage/response.hpp>
#include <uv.h>
#include <cassert>
#include <unistd.h>
namespace mbgl {
FileRequest::FileRequest(const std::string &path_, uv_loop_t *loop)
: BaseRequest(path_), ptr(new FileRequestBaton(this, path, loop)) {
}
void FileRequest::cancel() {
assert(std::this_thread::get_id() == threadId);
if (ptr) {
ptr->cancel();
// When deleting a FileRequest 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();
}
FileRequest::~FileRequest() {
assert(std::this_thread::get_id() == threadId);
cancel();
}
}

View File

@@ -1,27 +0,0 @@
#ifndef MBGL_STORAGE_FILE_REQUEST
#define MBGL_STORAGE_FILE_REQUEST
#include <mbgl/storage/base_request.hpp>
namespace mbgl {
typedef struct uv_loop_s uv_loop_t;
struct FileRequestBaton;
class FileRequest : public BaseRequest {
public:
FileRequest(const std::string &path, uv_loop_t *loop);
~FileRequest();
void cancel();
private:
FileRequestBaton *ptr = nullptr;
friend struct FileRequestBaton;
};
}
#endif

View File

@@ -1,161 +0,0 @@
#include <mbgl/storage/file_request_baton.hpp>
#include <mbgl/storage/file_request.hpp>
#include <mbgl/storage/response.hpp>
#include <mbgl/util/std.hpp>
#include <limits>
namespace mbgl {
FileRequestBaton::FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop)
: threadId(std::this_thread::get_id()), request(request_) {
req.data = this;
uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, fileOpened);
}
FileRequestBaton::~FileRequestBaton() {
}
void FileRequestBaton::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 FileRequestBaton::notifyError(uv_fs_t *req) {
FileRequestBaton *ptr = reinterpret_cast<FileRequestBaton *>(req->data);
assert(std::this_thread::get_id() == ptr->threadId);
if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) {
ptr->request->response = util::make_unique<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 FileRequestBaton::fileOpened(uv_fs_t *req) {
FileRequestBaton *ptr = reinterpret_cast<FileRequestBaton *>(req->data);
assert(std::this_thread::get_id() == ptr->threadId);
if (req->result < 0) {
// Opening failed or was canceled. There isn't much left we can do.
notifyError(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 FileRequest object has been destructed, or the
// request was canceled.
uv_fs_close(req->loop, req, fd, fileClosed);
} else {
ptr->fd = fd;
uv_fs_fstat(req->loop, req, fd, fileStated);
}
}
}
void FileRequestBaton::fileStated(uv_fs_t *req) {
FileRequestBaton *ptr = reinterpret_cast<FileRequestBaton *>(req->data);
assert(std::this_thread::get_id() == ptr->threadId);
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.
notifyError(req);
uv_fs_req_cleanup(req);
uv_fs_close(req->loop, req, ptr->fd, fileClosed);
} 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 = util::make_unique<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, fileClosed);
} 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, fileRead);
#else
uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, fileRead);
#endif
}
}
}
void FileRequestBaton::fileRead(uv_fs_t *req) {
FileRequestBaton *ptr = reinterpret_cast<FileRequestBaton *>(req->data);
assert(std::this_thread::get_id() == ptr->threadId);
if (req->result < 0 || ptr->canceled || !ptr->request) {
// Reading failed or was canceled. We already have an open file handle
// though, which we'll have to close.
notifyError(req);
} else {
// File was successfully read.
if (ptr->request) {
ptr->request->response = util::make_unique<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, fileClosed);
}
void FileRequestBaton::fileClosed(uv_fs_t *req) {
assert(std::this_thread::get_id() == reinterpret_cast<FileRequestBaton *>(req->data)->threadId);
if (req->result < 0) {
// Closing the file failed. But there isn't anything we can do.
}
cleanup(req);
}
void FileRequestBaton::cleanup(uv_fs_t *req) {
FileRequestBaton *ptr = reinterpret_cast<FileRequestBaton *>(req->data);
assert(std::this_thread::get_id() == ptr->threadId);
if (ptr->request) {
ptr->request->ptr = nullptr;
}
uv_fs_req_cleanup(req);
delete ptr;
ptr = nullptr;
}
}

View File

@@ -1,36 +0,0 @@
#ifndef MBGL_STORAGE_FILE_REQUEST_BATON
#define MBGL_STORAGE_FILE_REQUEST_BATON
#include <mbgl/storage/file_request.hpp>
#include <thread>
#include <uv.h>
namespace mbgl {
struct FileRequestBaton {
FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop);
~FileRequestBaton();
void cancel();
static void fileOpened(uv_fs_t *req);
static void fileStated(uv_fs_t *req);
static void fileRead(uv_fs_t *req);
static void fileClosed(uv_fs_t *req);
static void notifyError(uv_fs_t *req);
static void cleanup(uv_fs_t *req);
const std::thread::id threadId;
FileRequest *request = nullptr;
uv_fs_t req;
uv_file fd = -1;
bool canceled = false;
std::string body;
uv_buf_t buffer;
};
}
#endif