From 93ab4036e995f4b894783689cea1f2f97ef9b70b Mon Sep 17 00:00:00 2001 From: Leith Bade Date: Sat, 6 Dec 2014 00:50:23 +1100 Subject: [PATCH] Fix commit --- android/cpp/jni.cpp | 6 ++-- android/cpp/native_map_view.cpp | 1 + gyp/mbgl-android.gypi | 1 - include/mbgl/android/native_map_view.hpp | 6 +++- include/mbgl/storage/asset_request_baton.hpp | 6 ++-- .../mbgl/storage/caching_http_file_source.hpp | 3 ++ .../android/asset_request_baton_libzip.cpp | 19 ++++------- platform/default/asset_request_baton_noop.cpp | 4 +-- src/{ => mbgl}/storage/asset_request.cpp | 0 .../mbgl/storage/asset_request.hpp | 0 .../storage/asset_request_baton.cpp | 13 +++++++- src/mbgl/storage/caching_http_file_source.cpp | 6 +++- src/mbgl/storage/file_request_baton.cpp | 32 +++++++++---------- src/mbgl/storage/file_request_baton.hpp | 10 +++--- 14 files changed, 63 insertions(+), 44 deletions(-) rename src/{ => mbgl}/storage/asset_request.cpp (100%) rename {include => src}/mbgl/storage/asset_request.hpp (100%) rename src/{ => mbgl}/storage/asset_request_baton.cpp (70%) diff --git a/android/cpp/jni.cpp b/android/cpp/jni.cpp index 20887520c..0fa7a8a10 100644 --- a/android/cpp/jni.cpp +++ b/android/cpp/jni.cpp @@ -418,7 +418,7 @@ void JNICALL nativeSetAccessToken(JNIEnv* env, jobject obj, jlong nativeMapViewP mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetAccessToken"); assert(nativeMapViewPtr != 0); NativeMapView* nativeMapView = reinterpret_cast(nativeMapViewPtr); - COFFEE_TRY_JNI(env, CPP_TRY_JNI(env, nativeMapView->getMap().setAccessToken(std_string_from_jstring(env, accessToken)))); + COFFEE_TRY_JNI(env, CPP_TRY_JNI(env, nativeMapView->getFileSource().setAccessToken(std_string_from_jstring(env, accessToken)))); } jstring JNICALL nativeGetAccessToken(JNIEnv* env, jobject obj, jlong nativeMapViewPtr) { @@ -426,7 +426,7 @@ jstring JNICALL nativeGetAccessToken(JNIEnv* env, jobject obj, jlong nativeMapVi assert(nativeMapViewPtr != 0); NativeMapView* nativeMapView = reinterpret_cast(nativeMapViewPtr); jstring ret = nullptr; - COFFEE_TRY_JNI(env, CPP_TRY_JNI(env, ret = std_string_to_jstring(env, nativeMapView->getMap().getAccessToken()))); + COFFEE_TRY_JNI(env, CPP_TRY_JNI(env, ret = std_string_to_jstring(env, nativeMapView->getFileSource().getAccessToken()))); return ret; } @@ -699,7 +699,7 @@ void JNICALL nativeSetReachability(JNIEnv* env, jobject obj, jlong nativeMapView mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetReachability"); assert(nativeMapViewPtr != 0); NativeMapView* nativeMapView = reinterpret_cast(nativeMapViewPtr); - COFFEE_TRY_JNI(env, CPP_TRY_JNI(env, nativeMapView->getMap().setReachability(status))); + COFFEE_TRY_JNI(env, CPP_TRY_JNI(env, nativeMapView->getFileSource().setReachability(status))); } } diff --git a/android/cpp/native_map_view.cpp b/android/cpp/native_map_view.cpp index 43bc95598..37f4e3154 100644 --- a/android/cpp/native_map_view.cpp +++ b/android/cpp/native_map_view.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include namespace mbgl { diff --git a/gyp/mbgl-android.gypi b/gyp/mbgl-android.gypi index 21a6771d7..ff537792b 100644 --- a/gyp/mbgl-android.gypi +++ b/gyp/mbgl-android.gypi @@ -39,7 +39,6 @@ '../platform/default/image_reader.cpp', '../platform/default/png_reader.cpp', '../platform/default/jpeg_reader.cpp', - '../platform/default/caching_http_file_source.cpp', ], 'include_dirs': [ '../include', diff --git a/include/mbgl/android/native_map_view.hpp b/include/mbgl/android/native_map_view.hpp index 2ca948496..9ebf69e3a 100644 --- a/include/mbgl/android/native_map_view.hpp +++ b/include/mbgl/android/native_map_view.hpp @@ -4,7 +4,7 @@ #include #include #include -#include +#include #include #include @@ -44,6 +44,10 @@ public: return map; } + mbgl::CachingHTTPFileSource& getFileSource() { + return fileSource; + } + bool initializeDisplay(); void terminateDisplay(); diff --git a/include/mbgl/storage/asset_request_baton.hpp b/include/mbgl/storage/asset_request_baton.hpp index 8506dc9b6..2cbb6c51c 100644 --- a/include/mbgl/storage/asset_request_baton.hpp +++ b/include/mbgl/storage/asset_request_baton.hpp @@ -1,7 +1,6 @@ #ifndef MBGL_STORAGE_ASSET_REQUEST_BATON #define MBGL_STORAGE_ASSET_REQUEST_BATON -#include #include #include @@ -9,6 +8,8 @@ namespace mbgl { +class AssetRequest; + struct AssetRequestBaton { AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop); @@ -19,7 +20,8 @@ struct AssetRequestBaton { bool canceled = false; void cancel(); - static void notify_error(AssetRequestBaton *ptr, const int code, const char *message); + static void notifyError(AssetRequestBaton *ptr, const int code, const char *message); + static void notifySuccess(AssetRequestBaton *ptr, const std::string body); static void cleanup(AssetRequestBaton *ptr); // IMPLEMENT THIS PLATFORM SPECIFIC FUNCTION: diff --git a/include/mbgl/storage/caching_http_file_source.hpp b/include/mbgl/storage/caching_http_file_source.hpp index 66e3335f2..655afa639 100644 --- a/include/mbgl/storage/caching_http_file_source.hpp +++ b/include/mbgl/storage/caching_http_file_source.hpp @@ -29,6 +29,9 @@ public: // Set the Mapbox API access token void setAccessToken(std::string); + // Get the Mapbox API access token + std::string getAccessToken() const; + std::unique_ptr request(ResourceType type, const std::string &url); void prepare(std::function fn); diff --git a/platform/android/asset_request_baton_libzip.cpp b/platform/android/asset_request_baton_libzip.cpp index 205c42bf4..8984aa58d 100644 --- a/platform/android/asset_request_baton_libzip.cpp +++ b/platform/android/asset_request_baton_libzip.cpp @@ -1,6 +1,6 @@ +#include #include #include -#include #include #include @@ -29,7 +29,7 @@ void AssetRequestBaton::run(AssetRequestBaton *ptr) { const int messageSize = zip_error_to_str(nullptr, 0, error, errno); const std::unique_ptr message = mbgl::util::make_unique(messageSize); zip_error_to_str(message.get(), 0, error, errno); - notify_error(ptr, 500, message.get()); + notifyError(ptr, 500, message.get()); cleanup(ptr); return; } @@ -40,7 +40,7 @@ void AssetRequestBaton::run(AssetRequestBaton *ptr) { // 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(ptr, error == ZIP_ER_NOENT ? 404 : 500, zip_strerror(apk)); + notifyError(ptr, error == ZIP_ER_NOENT ? 404 : 500, zip_strerror(apk)); zip_close(apk); apk = nullptr; cleanup(ptr); @@ -51,7 +51,7 @@ void AssetRequestBaton::run(AssetRequestBaton *ptr) { if ((zip_stat(apk, apkFilePath.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(ptr, 500, zip_strerror(apk)); + notifyError(ptr, 500, zip_strerror(apk)); zip_fclose(apkFile); apkFile = nullptr; zip_close(apk); @@ -65,7 +65,7 @@ void AssetRequestBaton::run(AssetRequestBaton *ptr) { if (static_cast(zip_fread(apkFile, reinterpret_cast(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(ptr, 500, zip_file_strerror(apkFile)); + notifyError(ptr, 500, zip_file_strerror(apkFile)); zip_fclose(apkFile); apkFile = nullptr; zip_close(apk); @@ -74,13 +74,8 @@ void AssetRequestBaton::run(AssetRequestBaton *ptr) { return; } - if (ptr->request) { - ptr->request->response = std::unique_ptr(new Response); - ptr->request->response->code = 200; - std::string body(data.get(), stat.size); - ptr->request->response->data = body; - ptr->request->notify(); - } + std::string body(data.get(), stat.size); + notifySuccess(ptr, body); if (zip_fclose(apkFile) != 0) { // Closing the asset failed. But there isn't anything we can do. diff --git a/platform/default/asset_request_baton_noop.cpp b/platform/default/asset_request_baton_noop.cpp index 88550367b..79f69a2da 100644 --- a/platform/default/asset_request_baton_noop.cpp +++ b/platform/default/asset_request_baton_noop.cpp @@ -1,5 +1,5 @@ +#include #include -#include #include namespace mbgl { @@ -15,7 +15,7 @@ void AssetRequestBaton::run(AssetRequestBaton *ptr) { } // Just return a 500 error until implemented properly - notify_error(ptr, 500, "Assets not implemented on this platform."); + notifyError(ptr, 500, "Assets not implemented on this platform."); cleanup(ptr); } diff --git a/src/storage/asset_request.cpp b/src/mbgl/storage/asset_request.cpp similarity index 100% rename from src/storage/asset_request.cpp rename to src/mbgl/storage/asset_request.cpp diff --git a/include/mbgl/storage/asset_request.hpp b/src/mbgl/storage/asset_request.hpp similarity index 100% rename from include/mbgl/storage/asset_request.hpp rename to src/mbgl/storage/asset_request.hpp diff --git a/src/storage/asset_request_baton.cpp b/src/mbgl/storage/asset_request_baton.cpp similarity index 70% rename from src/storage/asset_request_baton.cpp rename to src/mbgl/storage/asset_request_baton.cpp index 00382f21c..85f3a6ff2 100644 --- a/src/storage/asset_request_baton.cpp +++ b/src/mbgl/storage/asset_request_baton.cpp @@ -22,7 +22,7 @@ void AssetRequestBaton::cancel() { canceled = true; } -void AssetRequestBaton::notify_error(AssetRequestBaton *ptr, const int code, const char *message) { +void AssetRequestBaton::notifyError(AssetRequestBaton *ptr, const int code, const char *message) { assert(std::this_thread::get_id() == ptr->threadId); if (ptr->request && !ptr->canceled) { @@ -33,6 +33,17 @@ void AssetRequestBaton::notify_error(AssetRequestBaton *ptr, const int code, con } } +void AssetRequestBaton::notifySuccess(AssetRequestBaton *ptr, const std::string body) { +assert(std::this_thread::get_id() == ptr->threadId); + + if (ptr->request && !ptr->canceled) { + ptr->request->response = std::unique_ptr(new Response); + ptr->request->response->code = 200; + ptr->request->response->data = body; + ptr->request->notify(); + } +} + void AssetRequestBaton::cleanup(AssetRequestBaton *ptr) { assert(std::this_thread::get_id() == ptr->threadId); diff --git a/src/mbgl/storage/caching_http_file_source.cpp b/src/mbgl/storage/caching_http_file_source.cpp index cf09c5811..4f32df311 100644 --- a/src/mbgl/storage/caching_http_file_source.cpp +++ b/src/mbgl/storage/caching_http_file_source.cpp @@ -64,8 +64,12 @@ void CachingHTTPFileSource::setAccessToken(std::string value) { accessToken.swap(value); } +std::string CachingHTTPFileSource::getAccessToken() const { + return accessToken; +} + std::unique_ptr CachingHTTPFileSource::request(ResourceType type, const std::string& url_) { - assert(tstd::this_thread::get_id() == threadId); + assert(std::this_thread::get_id() == threadId); std::string url = url_; diff --git a/src/mbgl/storage/file_request_baton.cpp b/src/mbgl/storage/file_request_baton.cpp index c661cd347..6da10552b 100644 --- a/src/mbgl/storage/file_request_baton.cpp +++ b/src/mbgl/storage/file_request_baton.cpp @@ -10,7 +10,7 @@ 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, file_opened); + uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, fileOpened); } FileRequestBaton::~FileRequestBaton() { @@ -25,7 +25,7 @@ void FileRequestBaton::cancel() { uv_cancel((uv_req_t *)&req); } -void FileRequestBaton::notify_error(uv_fs_t *req) { +void FileRequestBaton::notifyError(uv_fs_t *req) { FileRequestBaton *ptr = reinterpret_cast(req->data); assert(std::this_thread::get_id() == ptr->threadId); @@ -41,13 +41,13 @@ void FileRequestBaton::notify_error(uv_fs_t *req) { } } -void FileRequestBaton::file_opened(uv_fs_t *req) { +void FileRequestBaton::fileOpened(uv_fs_t *req) { FileRequestBaton *ptr = reinterpret_cast(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. - notify_error(req); + notifyError(req); cleanup(req); } else { const uv_file fd = uv_file(req->result); @@ -58,25 +58,25 @@ void FileRequestBaton::file_opened(uv_fs_t *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, file_closed); + uv_fs_close(req->loop, req, fd, fileClosed); } else { ptr->fd = fd; - uv_fs_fstat(req->loop, req, fd, file_stated); + uv_fs_fstat(req->loop, req, fd, fileStated); } } } -void FileRequestBaton::file_stated(uv_fs_t *req) { +void FileRequestBaton::fileStated(uv_fs_t *req) { FileRequestBaton *ptr = reinterpret_cast(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. - notify_error(req); + notifyError(req); uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, file_closed); + 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(req->ptr); @@ -98,29 +98,29 @@ void FileRequestBaton::file_stated(uv_fs_t *req) { } uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, file_closed); + 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(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); + 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, file_read); + uv_fs_read(req->loop, req, ptr->fd, &ptr->buffer, 1, 0, fileRead); #endif } } } -void FileRequestBaton::file_read(uv_fs_t *req) { +void FileRequestBaton::fileRead(uv_fs_t *req) { FileRequestBaton *ptr = reinterpret_cast(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. - notify_error(req); + notifyError(req); } else { // File was successfully read. if (ptr->request) { @@ -132,10 +132,10 @@ void FileRequestBaton::file_read(uv_fs_t *req) { } uv_fs_req_cleanup(req); - uv_fs_close(req->loop, req, ptr->fd, file_closed); + uv_fs_close(req->loop, req, ptr->fd, fileClosed); } -void FileRequestBaton::file_closed(uv_fs_t *req) { +void FileRequestBaton::fileClosed(uv_fs_t *req) { assert(std::this_thread::get_id() == reinterpret_cast(req->data)->threadId); if (req->result < 0) { diff --git a/src/mbgl/storage/file_request_baton.hpp b/src/mbgl/storage/file_request_baton.hpp index 0c3fa16da..ef9c170fe 100644 --- a/src/mbgl/storage/file_request_baton.hpp +++ b/src/mbgl/storage/file_request_baton.hpp @@ -13,11 +13,11 @@ struct FileRequestBaton { ~FileRequestBaton(); 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 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;