From 3a8036413f22f32b4b25c019ef2835df25e6c175 Mon Sep 17 00:00:00 2001 From: Mike Morris Date: Mon, 15 Dec 2014 14:32:48 -0500 Subject: [PATCH 1/6] fix CachingHTTPFileSource::clearLoop --- src/mbgl/storage/caching_http_file_source.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/mbgl/storage/caching_http_file_source.cpp b/src/mbgl/storage/caching_http_file_source.cpp index d676357f8..ddd65118f 100644 --- a/src/mbgl/storage/caching_http_file_source.cpp +++ b/src/mbgl/storage/caching_http_file_source.cpp @@ -17,6 +17,8 @@ CachingHTTPFileSource::~CachingHTTPFileSource() { } void CachingHTTPFileSource::setLoop(uv_loop_t* loop_) { + assert(!loop); + thread_id = std::this_thread::get_id(); store = !path.empty() ? util::ptr(new SQLiteStore(loop_, path)) : nullptr; loop = loop_; @@ -51,6 +53,8 @@ void CachingHTTPFileSource::clearLoop() { } store.reset(); + + loop = nullptr; } void CachingHTTPFileSource::setBase(std::string value) { From c3d2f3e66429ee2f199fdbb8e3db0405de9a724b Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Wed, 12 Nov 2014 12:37:06 -0800 Subject: [PATCH 2/6] indicate submodule setup in common header of install info --- README.md | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 3c8fb2fd3..6f02773c7 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,11 @@ implemented in C++11, currently targeting iOS, OS X, and Ubuntu Linux. We try to link to as many system-provided libraries as possible. When these are unavailable or too outdated, we run a thin build-script layer called [Mason](https://github.com/mapbox/mason) to automate builds, and load precompiled binary packages when possible. +Be sure to pull down all submodules first: + + git submodule init + git submodule update + ## OS X On OS X, we are using `zlib`, `SQLite3`, `libcurl` and `libpng` provided by the operating system. In addition to that, you need to have the Boost headers installed. To install all prerequisites, use [Homebrew](http://brew.sh/) and type `brew install pkg-config boost`. @@ -43,12 +48,7 @@ iOS makes use of a Cocoa-specific API called [`mapbox-gl-cocoa`](https://github. If you intend to develop here, `mapbox-gl-cocoa` is included as a submodule of the overall build setup. -To pull down the submodule(s), run: - - git submodule init - git submodule update - -Then, you can run `make iproj` to create and open an Xcode project with an iOS-specific view controller housing. This will automatically install required dependencies as well. +You can run `make iproj` to create and open an Xcode project with an iOS-specific view controller housing. This will automatically install required dependencies as well. Note that if you are doing OS X development as well, to toggle from OS X back to iOS, you will need to `make iproj` again. From 02e43c6d30553a676b912abe1391b82219290333 Mon Sep 17 00:00:00 2001 From: Jon Rowe Date: Sun, 7 Dec 2014 14:38:20 +1100 Subject: [PATCH 3/6] Update READMD.md for OS X Yosemite --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6f02773c7..d1f5211c1 100644 --- a/README.md +++ b/README.md @@ -117,9 +117,9 @@ The demo applications use Mapbox vector tiles, which **require a Mapbox account For iOS and OS X use of the demo apps in Xcode, setup the access token by editing the scheme for the application target, then adding an environment variable with the name `MAPBOX_ACCESS_TOKEN`. -![edit scheme](https://cloud.githubusercontent.com/assets/98601/3647749/30f74f26-1102-11e4-84af-f1be853b4e38.png) +![edit scheme](https://cloud.githubusercontent.com/assets/98601/5460702/c4610262-8519-11e4-873a-8597821da468.png) -![setting access token in Xcode scheme](https://cloud.githubusercontent.com/assets/52399/3543326/9e7cfbb8-0860-11e4-8def-3684a9028b61.png) +![setting access token in Xcode scheme](https://cloud.githubusercontent.com/assets/162976/5349358/0a086f00-7f8c-11e4-8433-bdbaccda2b58.png) For Linux, set the environment variable `MAPBOX_ACCESS_TOKEN` to your token. From 936e09651424f111ac2ac64e2e60e2d13b082ebf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 17 Dec 2014 12:14:32 +0100 Subject: [PATCH 4/6] Make file request overridable by platform implementations refs #579: Android can now rename AssetRequest to FileRequest and implement it differently --- gyp/mbgl-ios.gypi | 2 + gyp/mbgl-linux.gypi | 2 + gyp/mbgl-osx.gypi | 2 + gyp/styles.gypi | 14 +++-- .../mbgl/storage/file_request.hpp | 2 +- linux/main.cpp | 2 +- linux/mapboxgl-app.gyp | 1 + .../default/file_request_libuv.cpp | 51 ++++++++++++++++++- src/mbgl/storage/file_request.cpp | 37 -------------- src/mbgl/storage/file_request_baton.hpp | 36 ------------- 10 files changed, 70 insertions(+), 79 deletions(-) rename {src => include}/mbgl/storage/file_request.hpp (98%) rename src/mbgl/storage/file_request_baton.cpp => platform/default/file_request_libuv.cpp (80%) delete mode 100644 src/mbgl/storage/file_request.cpp delete mode 100644 src/mbgl/storage/file_request_baton.hpp diff --git a/gyp/mbgl-ios.gypi b/gyp/mbgl-ios.gypi index df6896e1e..da05619a3 100644 --- a/gyp/mbgl-ios.gypi +++ b/gyp/mbgl-ios.gypi @@ -40,9 +40,11 @@ '../platform/darwin/string_nsstring.mm', '../platform/darwin/http_request_baton_cocoa.mm', '../platform/darwin/image.mm', + '../platform/default/file_request_libuv.cpp', ], 'include_dirs': [ '../include', + '../src', ], 'xcode_settings': { 'OTHER_CPLUSPLUSFLAGS': [ '<@(uv_cflags)' ], diff --git a/gyp/mbgl-linux.gypi b/gyp/mbgl-linux.gypi index fdea368ae..51df45ecc 100644 --- a/gyp/mbgl-linux.gypi +++ b/gyp/mbgl-linux.gypi @@ -34,6 +34,7 @@ '../platform/default/shader_cache_tmp.cpp', '../platform/default/log_stderr.cpp', '../platform/default/string_stdlib.cpp', + '../platform/default/file_request_libuv.cpp', '../platform/default/http_request_baton_curl.cpp', '../platform/default/image.cpp', '../platform/default/image_reader.cpp', @@ -42,6 +43,7 @@ ], 'include_dirs': [ '../include', + '../src', ], 'link_settings': { 'libraries': [ diff --git a/gyp/mbgl-osx.gypi b/gyp/mbgl-osx.gypi index 4e8a131da..cc6db2eb2 100644 --- a/gyp/mbgl-osx.gypi +++ b/gyp/mbgl-osx.gypi @@ -15,9 +15,11 @@ '../platform/darwin/string_nsstring.mm', '../platform/darwin/http_request_baton_cocoa.mm', '../platform/darwin/image.mm', + '../platform/default/file_request_libuv.cpp', ], 'include_dirs': [ '../include', + '../src', ], 'xcode_settings': { 'OTHER_CPLUSPLUSFLAGS': [ '<@(uv_cflags)' ], diff --git a/gyp/styles.gypi b/gyp/styles.gypi index 02cdea6e0..e1cd344fe 100644 --- a/gyp/styles.gypi +++ b/gyp/styles.gypi @@ -18,9 +18,17 @@ 'type': 'none', 'hard_dependency': 1, 'dependencies': [ 'touch_styles' ], # required for xcode http://openradar.appspot.com/7232149 - 'direct_dependent_settings': { - 'mac_bundle_resources': [ '../styles/styles' ], - } + 'conditions': [ + ['OS == "mac"', { + 'direct_dependent_settings': { + 'mac_bundle_resources': [ '../styles/styles' ], + } + }, { + 'direct_dependent_settings': { + 'copies': [{ 'files': [ '../styles/styles' ], 'destination': '<(PRODUCT_DIR)' }], + } + }] + ], } ] } diff --git a/src/mbgl/storage/file_request.hpp b/include/mbgl/storage/file_request.hpp similarity index 98% rename from src/mbgl/storage/file_request.hpp rename to include/mbgl/storage/file_request.hpp index 2f883728f..3de2d5b60 100644 --- a/src/mbgl/storage/file_request.hpp +++ b/include/mbgl/storage/file_request.hpp @@ -24,4 +24,4 @@ private: } -#endif \ No newline at end of file +#endif diff --git a/linux/main.cpp b/linux/main.cpp index 42a98d019..c5b2569ba 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -83,7 +83,7 @@ int main(int argc, char *argv[]) { // Load style if (style.empty()) - style = std::string("file://") + uv::cwd() + std::string("/../../styles/styles/bright-v6.json"); + style = std::string("file://") + uv::cwd() + std::string("/styles/bright-v6.json"); map.setStyleURL(style); diff --git a/linux/mapboxgl-app.gyp b/linux/mapboxgl-app.gyp index fbeabd74e..4127b5c27 100644 --- a/linux/mapboxgl-app.gyp +++ b/linux/mapboxgl-app.gyp @@ -48,6 +48,7 @@ 'dependencies': [ '../mapboxgl.gyp:mbgl-standalone', '../mapboxgl.gyp:mbgl-linux', + '../mapboxgl.gyp:bundle_styles', '../mapboxgl.gyp:copy_certificate_bundle', ], 'copies': [{ diff --git a/src/mbgl/storage/file_request_baton.cpp b/platform/default/file_request_libuv.cpp similarity index 80% rename from src/mbgl/storage/file_request_baton.cpp rename to platform/default/file_request_libuv.cpp index 0a9c5f6f5..f874bed8d 100644 --- a/src/mbgl/storage/file_request_baton.cpp +++ b/platform/default/file_request_libuv.cpp @@ -1,12 +1,34 @@ -#include #include #include #include +#include + #include namespace mbgl { +struct FileRequestBaton { + FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop); + ~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 cleanup(uv_fs_t *req); + + const std::thread::id thread_id; + FileRequest *request = nullptr; + uv_fs_t req; + uv_file fd = -1; + bool canceled = false; + std::string body; + uv_buf_t buffer; +}; + FileRequestBaton::FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop) : thread_id(std::this_thread::get_id()), request(request_) { req.data = this; @@ -157,4 +179,31 @@ void FileRequestBaton::cleanup(uv_fs_t *req) { delete ptr; } + +FileRequest::FileRequest(const std::string &path_, uv_loop_t *loop) + : BaseRequest(path_), ptr(new FileRequestBaton(this, path, loop)) { +} + +void FileRequest::cancel() { + assert(thread_id == std::this_thread::get_id()); + + 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(thread_id == std::this_thread::get_id()); + cancel(); + + // Note: The FileRequestBaton object is deleted in FileRequestBaton::cleanup(). +} + } diff --git a/src/mbgl/storage/file_request.cpp b/src/mbgl/storage/file_request.cpp deleted file mode 100644 index 9f74c7b41..000000000 --- a/src/mbgl/storage/file_request.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include - -#include - -#include - -#include - -namespace mbgl { - -FileRequest::FileRequest(const std::string &path_, uv_loop_t *loop) - : BaseRequest(path_), ptr(new FileRequestBaton(this, path, loop)) { -} - -void FileRequest::cancel() { - assert(thread_id == std::this_thread::get_id()); - - 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(thread_id == std::this_thread::get_id()); - cancel(); -} - -} diff --git a/src/mbgl/storage/file_request_baton.hpp b/src/mbgl/storage/file_request_baton.hpp deleted file mode 100644 index 897c88061..000000000 --- a/src/mbgl/storage/file_request_baton.hpp +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef MBGL_STORAGE_FILE_REQUEST_BATON -#define MBGL_STORAGE_FILE_REQUEST_BATON - -#include -#include - -#include - -namespace mbgl { - -struct FileRequestBaton { - FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop); - ~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 cleanup(uv_fs_t *req); - - const std::thread::id thread_id; - FileRequest *request = nullptr; - uv_fs_t req; - uv_file fd = -1; - bool canceled = false; - std::string body; - uv_buf_t buffer; -}; - - -} - - -#endif From b5e9998b15d8a51bdca9cdb2d0981f69629f532a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 17 Dec 2014 14:59:05 +0100 Subject: [PATCH 5/6] change file:// to asset:// to indicate that they aren't arbitrary files refs #579 --- gyp/mbgl-ios.gypi | 3 +- gyp/mbgl-linux.gypi | 3 +- gyp/mbgl-osx.gypi | 3 +- include/mbgl/platform/platform.hpp | 2 + include/mbgl/storage/asset_request.hpp | 27 ++++++++ include/mbgl/storage/file_request.hpp | 27 -------- ios/mapbox-gl-cocoa | 2 +- linux/main.cpp | 4 +- macosx/main.mm | 4 +- platform/darwin/application_root.mm | 18 +++++ platform/default/application_root.cpp | 14 ++++ ...uest_libuv.cpp => asset_request_libuv.cpp} | 67 +++++++++++-------- src/mbgl/storage/caching_http_file_source.cpp | 6 +- 13 files changed, 114 insertions(+), 66 deletions(-) create mode 100644 include/mbgl/storage/asset_request.hpp delete mode 100644 include/mbgl/storage/file_request.hpp create mode 100644 platform/darwin/application_root.mm create mode 100644 platform/default/application_root.cpp rename platform/default/{file_request_libuv.cpp => asset_request_libuv.cpp} (70%) diff --git a/gyp/mbgl-ios.gypi b/gyp/mbgl-ios.gypi index da05619a3..bd96486d6 100644 --- a/gyp/mbgl-ios.gypi +++ b/gyp/mbgl-ios.gypi @@ -39,8 +39,9 @@ '../platform/darwin/log_nslog.mm', '../platform/darwin/string_nsstring.mm', '../platform/darwin/http_request_baton_cocoa.mm', + '../platform/darwin/application_root.mm', '../platform/darwin/image.mm', - '../platform/default/file_request_libuv.cpp', + '../platform/default/asset_request_libuv.cpp', ], 'include_dirs': [ '../include', diff --git a/gyp/mbgl-linux.gypi b/gyp/mbgl-linux.gypi index 51df45ecc..d9268d920 100644 --- a/gyp/mbgl-linux.gypi +++ b/gyp/mbgl-linux.gypi @@ -34,8 +34,9 @@ '../platform/default/shader_cache_tmp.cpp', '../platform/default/log_stderr.cpp', '../platform/default/string_stdlib.cpp', - '../platform/default/file_request_libuv.cpp', + '../platform/default/asset_request_libuv.cpp', '../platform/default/http_request_baton_curl.cpp', + '../platform/default/application_root.cpp', '../platform/default/image.cpp', '../platform/default/image_reader.cpp', '../platform/default/png_reader.cpp', diff --git a/gyp/mbgl-osx.gypi b/gyp/mbgl-osx.gypi index cc6db2eb2..0cc29f6e7 100644 --- a/gyp/mbgl-osx.gypi +++ b/gyp/mbgl-osx.gypi @@ -14,8 +14,9 @@ '../platform/darwin/log_nslog.mm', '../platform/darwin/string_nsstring.mm', '../platform/darwin/http_request_baton_cocoa.mm', + '../platform/darwin/application_root.mm', '../platform/darwin/image.mm', - '../platform/default/file_request_libuv.cpp', + '../platform/default/asset_request_libuv.cpp', ], 'include_dirs': [ '../include', diff --git a/include/mbgl/platform/platform.hpp b/include/mbgl/platform/platform.hpp index 9edcc23cc..02c612c83 100644 --- a/include/mbgl/platform/platform.hpp +++ b/include/mbgl/platform/platform.hpp @@ -23,6 +23,8 @@ std::string defaultCacheDatabase(); // Returns the path to the default shader cache on this system. std::string defaultShaderCache(); +std::string applicationRoot(); + // Shows an alpha image with the specified dimensions in a named window. void show_debug_image(std::string name, const char *data, size_t width, size_t height); diff --git a/include/mbgl/storage/asset_request.hpp b/include/mbgl/storage/asset_request.hpp new file mode 100644 index 000000000..3114d41ad --- /dev/null +++ b/include/mbgl/storage/asset_request.hpp @@ -0,0 +1,27 @@ +#ifndef MBGL_STORAGE_ASSET_REQUEST +#define MBGL_STORAGE_ASSET_REQUEST + +#include + +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 diff --git a/include/mbgl/storage/file_request.hpp b/include/mbgl/storage/file_request.hpp deleted file mode 100644 index 3de2d5b60..000000000 --- a/include/mbgl/storage/file_request.hpp +++ /dev/null @@ -1,27 +0,0 @@ -#ifndef MBGL_STORAGE_FILE_REQUEST -#define MBGL_STORAGE_FILE_REQUEST - -#include - -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 diff --git a/ios/mapbox-gl-cocoa b/ios/mapbox-gl-cocoa index 1533d3288..27442b681 160000 --- a/ios/mapbox-gl-cocoa +++ b/ios/mapbox-gl-cocoa @@ -1 +1 @@ -Subproject commit 1533d3288f5678d13bc550b43fb7dbb6ecdfa29b +Subproject commit 27442b6816ff7df9054f0898968471283ca773c0 diff --git a/linux/main.cpp b/linux/main.cpp index c5b2569ba..e2965a8ea 100644 --- a/linux/main.cpp +++ b/linux/main.cpp @@ -49,7 +49,7 @@ int main(int argc, char *argv[]) { // handle fullscreen_flag break; case 's': - style = std::string("file://") + std::string(optarg); + style = std::string("asset://") + std::string(optarg); default: break; } @@ -83,7 +83,7 @@ int main(int argc, char *argv[]) { // Load style if (style.empty()) - style = std::string("file://") + uv::cwd() + std::string("/styles/bright-v6.json"); + style = std::string("asset://") + std::string("styles/bright-v6.json"); map.setStyleURL(style); diff --git a/macosx/main.mm b/macosx/main.mm index 228b05f0b..39ddde10d 100644 --- a/macosx/main.mm +++ b/macosx/main.mm @@ -102,9 +102,7 @@ int main() { if (accessToken) fileSource.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]); // Load style - const std::string path([[[NSBundle mainBundle] pathForResource:@"bright-v6" ofType:@"json" inDirectory:@"styles/"] UTF8String]); - - map.setStyleURL(std::string("file://") + path); + map.setStyleURL("asset://styles/bright-v6.json"); int ret = view.run(); diff --git a/platform/darwin/application_root.mm b/platform/darwin/application_root.mm new file mode 100644 index 000000000..19b872c54 --- /dev/null +++ b/platform/darwin/application_root.mm @@ -0,0 +1,18 @@ +#import + +#include + +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 000000000..e6f73e021 --- /dev/null +++ b/platform/default/application_root.cpp @@ -0,0 +1,14 @@ +#include + +#include + +namespace mbgl { +namespace platform { + +// Returns the path to the default cache database on this system. +std::string applicationRoot() { + return uv::cwd(); +} + +} +} diff --git a/platform/default/file_request_libuv.cpp b/platform/default/asset_request_libuv.cpp similarity index 70% rename from platform/default/file_request_libuv.cpp rename to platform/default/asset_request_libuv.cpp index f874bed8d..202e39967 100644 --- a/platform/default/file_request_libuv.cpp +++ b/platform/default/asset_request_libuv.cpp @@ -1,5 +1,6 @@ -#include +#include #include +#include #include #include @@ -8,9 +9,9 @@ namespace mbgl { -struct FileRequestBaton { - FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop); - ~FileRequestBaton(); +struct AssetRequestBaton { + AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop); + ~AssetRequestBaton(); void cancel(); static void file_opened(uv_fs_t *req); @@ -21,7 +22,7 @@ struct FileRequestBaton { static void cleanup(uv_fs_t *req); const std::thread::id thread_id; - FileRequest *request = nullptr; + AssetRequest *request = nullptr; uv_fs_t req; uv_file fd = -1; bool canceled = false; @@ -29,16 +30,16 @@ struct FileRequestBaton { uv_buf_t buffer; }; -FileRequestBaton::FileRequestBaton(FileRequest *request_, const std::string &path, uv_loop_t *loop) +AssetRequestBaton::AssetRequestBaton(AssetRequest *request_, const std::string &path, uv_loop_t *loop) : thread_id(std::this_thread::get_id()), request(request_) { req.data = this; uv_fs_open(loop, &req, path.c_str(), O_RDONLY, S_IRUSR, file_opened); } -FileRequestBaton::~FileRequestBaton() { +AssetRequestBaton::~AssetRequestBaton() { } -void FileRequestBaton::cancel() { +void AssetRequestBaton::cancel() { canceled = true; // uv_cancel fails frequently when the request has already been started. @@ -47,8 +48,8 @@ void FileRequestBaton::cancel() { uv_cancel((uv_req_t *)&req); } -void FileRequestBaton::notify_error(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; +void AssetRequestBaton::notify_error(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; assert(ptr->thread_id == std::this_thread::get_id()); if (ptr->request && req->result < 0 && !ptr->canceled && req->result != UV_ECANCELED) { @@ -63,8 +64,8 @@ void FileRequestBaton::notify_error(uv_fs_t *req) { } } -void FileRequestBaton::file_opened(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; +void AssetRequestBaton::file_opened(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; assert(ptr->thread_id == std::this_thread::get_id()); if (req->result < 0) { @@ -78,7 +79,7 @@ void FileRequestBaton::file_opened(uv_fs_t *req) { uv_fs_req_cleanup(req); if (ptr->canceled || !ptr->request) { - // Either the FileRequest object has been destructed, or the + // Either the AssetRequest object has been destructed, or the // request was canceled. uv_fs_close(req->loop, req, fd, file_closed); } else { @@ -88,8 +89,8 @@ void FileRequestBaton::file_opened(uv_fs_t *req) { } } -void FileRequestBaton::file_stated(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; +void AssetRequestBaton::file_stated(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; assert(ptr->thread_id == std::this_thread::get_id()); if (req->result != 0 || ptr->canceled || !ptr->request) { @@ -135,8 +136,8 @@ void FileRequestBaton::file_stated(uv_fs_t *req) { } } -void FileRequestBaton::file_read(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; +void AssetRequestBaton::file_read(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; assert(ptr->thread_id == std::this_thread::get_id()); if (req->result < 0 || ptr->canceled || !ptr->request) { @@ -157,8 +158,8 @@ void FileRequestBaton::file_read(uv_fs_t *req) { uv_fs_close(req->loop, req, ptr->fd, file_closed); } -void FileRequestBaton::file_closed(uv_fs_t *req) { - assert(((FileRequestBaton *)req->data)->thread_id == std::this_thread::get_id()); +void AssetRequestBaton::file_closed(uv_fs_t *req) { + assert(((AssetRequestBaton *)req->data)->thread_id == std::this_thread::get_id()); if (req->result < 0) { // Closing the file failed. But there isn't anything we can do. @@ -167,8 +168,8 @@ void FileRequestBaton::file_closed(uv_fs_t *req) { cleanup(req); } -void FileRequestBaton::cleanup(uv_fs_t *req) { - FileRequestBaton *ptr = (FileRequestBaton *)req->data; +void AssetRequestBaton::cleanup(uv_fs_t *req) { + AssetRequestBaton *ptr = (AssetRequestBaton *)req->data; assert(ptr->thread_id == std::this_thread::get_id()); if (ptr->request) { @@ -180,17 +181,29 @@ void FileRequestBaton::cleanup(uv_fs_t *req) { } -FileRequest::FileRequest(const std::string &path_, uv_loop_t *loop) - : BaseRequest(path_), ptr(new FileRequestBaton(this, path, loop)) { +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->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 FileRequest::cancel() { +void AssetRequest::cancel() { assert(thread_id == std::this_thread::get_id()); if (ptr) { ptr->cancel(); - // When deleting a FileRequest object with a uv_fs_* call is in progress, we are making sure + // 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; @@ -199,11 +212,11 @@ void FileRequest::cancel() { notify(); } -FileRequest::~FileRequest() { +AssetRequest::~AssetRequest() { assert(thread_id == std::this_thread::get_id()); cancel(); - // Note: The FileRequestBaton object is deleted in FileRequestBaton::cleanup(). + // Note: The AssetRequestBaton object is deleted in AssetRequestBaton::cleanup(). } } diff --git a/src/mbgl/storage/caching_http_file_source.cpp b/src/mbgl/storage/caching_http_file_source.cpp index ddd65118f..eed3bdd05 100644 --- a/src/mbgl/storage/caching_http_file_source.cpp +++ b/src/mbgl/storage/caching_http_file_source.cpp @@ -1,5 +1,5 @@ #include -#include +#include #include #include #include @@ -95,8 +95,8 @@ std::unique_ptr CachingHTTPFileSource::request(ResourceType type, const } if (!req) { - if (url.substr(0, 7) == "file://") { - req = std::make_shared(url.substr(7), loop); + if (url.substr(0, 8) == "asset://") { + req = std::make_shared(url.substr(8), loop); } else { req = std::make_shared(type, url, loop, store); } From abcf978174437746c105cbd3e27c58ac55498cc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Wed, 17 Dec 2014 15:01:15 +0100 Subject: [PATCH 6/6] change comment and cache value --- platform/default/application_root.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/platform/default/application_root.cpp b/platform/default/application_root.cpp index e6f73e021..f25a44d46 100644 --- a/platform/default/application_root.cpp +++ b/platform/default/application_root.cpp @@ -5,9 +5,10 @@ namespace mbgl { namespace platform { -// Returns the path to the default cache database on this system. +// Returns the path the application root. std::string applicationRoot() { - return uv::cwd(); + static const std::string root = uv::cwd(); + return root; } }