summaryrefslogtreecommitdiff
path: root/platform
diff options
context:
space:
mode:
authorLeith Bade <leith@mapbox.com>2014-12-18 12:39:36 +1100
committerLeith Bade <leith@mapbox.com>2014-12-18 12:39:36 +1100
commit40f859e482e5282d0fbf257a9feaa9d70953a4fd (patch)
tree0fb920677d0ba92cedc64a4df9e1da045e891e75 /platform
parent6f56e88ee599160b336cce222384979a2b517343 (diff)
parenta8dec01230a00f9e701fc6403110c4f5017ba905 (diff)
downloadqtlocation-mapboxgl-40f859e482e5282d0fbf257a9feaa9d70953a4fd.tar.gz
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
Diffstat (limited to 'platform')
-rw-r--r--platform/darwin/application_root.mm18
-rw-r--r--platform/default/application_root.cpp15
-rw-r--r--platform/default/asset_request_libuv.cpp223
3 files changed, 256 insertions, 0 deletions
diff --git a/platform/darwin/application_root.mm b/platform/darwin/application_root.mm
new file mode 100644
index 0000000000..19b872c54d
--- /dev/null
+++ b/platform/darwin/application_root.mm
@@ -0,0 +1,18 @@
+#import <Foundation/Foundation.h>
+
+#include <mbgl/platform/platform.hpp>
+
+namespace mbgl {
+namespace platform {
+
+// Returns the path to the default shader cache on this system.
+std::string applicationRoot() {
+ static const std::string root = []() -> std::string {
+ NSString *path = [[[NSBundle mainBundle] resourceURL] path];
+ return {[path cStringUsingEncoding : NSUTF8StringEncoding],
+ [path lengthOfBytesUsingEncoding:NSUTF8StringEncoding]};
+ }();
+ return root;
+}
+}
+}
diff --git a/platform/default/application_root.cpp b/platform/default/application_root.cpp
new file mode 100644
index 0000000000..f25a44d46b
--- /dev/null
+++ b/platform/default/application_root.cpp
@@ -0,0 +1,15 @@
+#include <mbgl/platform/platform.hpp>
+
+#include <mbgl/util/uv.hpp>
+
+namespace mbgl {
+namespace platform {
+
+// Returns the path the application root.
+std::string applicationRoot() {
+ static const std::string root = uv::cwd();
+ return root;
+}
+
+}
+}
diff --git a/platform/default/asset_request_libuv.cpp b/platform/default/asset_request_libuv.cpp
new file mode 100644
index 0000000000..0e0b7280a7
--- /dev/null
+++ b/platform/default/asset_request_libuv.cpp
@@ -0,0 +1,223 @@
+#include <mbgl/storage/asset_request.hpp>
+#include <mbgl/storage/response.hpp>
+#include <mbgl/platform/platform.hpp>
+#include <mbgl/util/std.hpp>
+
+#include <uv.h>
+
+#include <limits>
+
+namespace mbgl {
+
+struct AssetRequestBaton {
+ AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop);
+ ~AssetRequestBaton();
+
+ 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;
+ AssetRequest *request = nullptr;
+ uv_fs_t req;
+ uv_file fd = -1;
+ bool canceled = false;
+ std::string body;
+ uv_buf_t buffer;
+};
+
+AssetRequestBaton::AssetRequestBaton(AssetRequest *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);
+}
+
+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::notifyError(uv_fs_t *req) {
+ AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(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 AssetRequestBaton::fileOpened(uv_fs_t *req) {
+ AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(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 AssetRequest 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 AssetRequestBaton::fileStated(uv_fs_t *req) {
+ AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(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 AssetRequestBaton::fileRead(uv_fs_t *req) {
+ AssetRequestBaton *ptr = reinterpret_cast<AssetRequestBaton *>(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 AssetRequestBaton::fileClosed(uv_fs_t *req) {
+ assert(std::this_thread::get_id() == (reinterpret_cast<AssetRequestBaton *>(req->data))->threadId);
+
+ 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 = reinterpret_cast<AssetRequestBaton *>(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;
+}
+
+
+AssetRequest::AssetRequest(const std::string &path_, uv_loop_t *loop)
+ : BaseRequest(path_) {
+ if (!path.empty() && path[0] == '/') {
+ // This is an absolute path. We don't allow this. Note that this is not a way to absolutely
+ // prevent access to resources outside the application bundle; e.g. there could be symlinks
+ // in the application bundle that link to outside. We don't care about these.
+ response = util::make_unique<Response>();
+ response->code = 403;
+ response->message = "Path is outside the application bundle";
+ notify();
+ } else {
+ // Note: The AssetRequestBaton object is deleted in AssetRequestBaton::cleanup().
+ ptr = new AssetRequestBaton(this, platform::applicationRoot() + "/" + path, loop);
+ }
+}
+
+void AssetRequest::cancel() {
+ assert(std::this_thread::get_id() == threadId);
+
+ 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(std::this_thread::get_id() == threadId);
+ cancel();
+
+ // Note: The AssetRequestBaton object is deleted in AssetRequestBaton::cleanup().
+}
+
+}