diff options
-rw-r--r-- | include/mbgl/storage/resource.hpp | 41 | ||||
-rw-r--r-- | src/mbgl/map/map_context.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/map/raster_tile_data.cpp | 15 | ||||
-rw-r--r-- | src/mbgl/map/source.cpp | 2 | ||||
-rw-r--r-- | src/mbgl/map/vector_tile.cpp | 14 | ||||
-rw-r--r-- | src/mbgl/sprite/sprite_store.cpp | 36 | ||||
-rw-r--r-- | src/mbgl/storage/resource.cpp | 82 | ||||
-rw-r--r-- | src/mbgl/text/glyph_pbf.cpp | 9 | ||||
-rw-r--r-- | src/mbgl/util/url.cpp | 24 | ||||
-rw-r--r-- | src/mbgl/util/url.hpp | 4 | ||||
-rw-r--r-- | test/storage/resource.cpp | 50 | ||||
-rw-r--r-- | test/test.gypi | 1 |
12 files changed, 180 insertions, 100 deletions
diff --git a/include/mbgl/storage/resource.hpp b/include/mbgl/storage/resource.hpp index 532feff14a..d684e16d1b 100644 --- a/include/mbgl/storage/resource.hpp +++ b/include/mbgl/storage/resource.hpp @@ -20,20 +20,6 @@ public: SpriteJSON }; - Resource(Kind kind_, const std::string& url_) - : kind(kind_), - url(url_) { - } - - const Kind kind; - const std::string url; - - optional<SystemTimePoint> priorModified; - optional<SystemTimePoint> priorExpires; - optional<std::string> priorEtag; - - // Includes auxiliary data if this is a tile request. - struct TileData { std::string urlTemplate; float pixelRatio; @@ -42,7 +28,34 @@ public: int8_t z; }; + Resource(Kind kind_, const std::string& url_, optional<TileData> tileData_ = {}) + : kind(kind_), + url(url_), + tileData(tileData_) { + } + + static Resource style(const std::string& url); + static Resource source(const std::string& url); + static Resource tile(const std::string& urlTemplate, + float pixelRatio, + int32_t x, + int32_t y, + int8_t z); + static Resource glyphs(const std::string& urlTemplate, + const std::string& fontStack, + const std::pair<uint16_t, uint16_t>& glyphRange); + static Resource spriteImage(const std::string& base, float pixelRatio); + static Resource spriteJSON(const std::string& base, float pixelRatio); + + const Kind kind; + const std::string url; + + // Includes auxiliary data if this is a tile request. optional<TileData> tileData; + + optional<SystemTimePoint> priorModified = {}; + optional<SystemTimePoint> priorExpires = {}; + optional<std::string> priorEtag = {}; }; } // namespace mbgl diff --git a/src/mbgl/map/map_context.cpp b/src/mbgl/map/map_context.cpp index 821741e87e..dcb4bd0a9e 100644 --- a/src/mbgl/map/map_context.cpp +++ b/src/mbgl/map/map_context.cpp @@ -104,7 +104,7 @@ void MapContext::setStyleURL(const std::string& url) { } FileSource* fs = util::ThreadContext::getFileSource(); - styleRequest = fs->request({ Resource::Kind::Style, styleURL }, [this, base](Response res) { + styleRequest = fs->request(Resource::style(styleURL), [this, base](Response res) { if (res.error) { if (res.error->reason == Response::Error::Reason::NotFound && util::mapbox::isMapboxURL(styleURL)) { diff --git a/src/mbgl/map/raster_tile_data.cpp b/src/mbgl/map/raster_tile_data.cpp index fa0bd298b1..f9f5480197 100644 --- a/src/mbgl/map/raster_tile_data.cpp +++ b/src/mbgl/map/raster_tile_data.cpp @@ -5,7 +5,6 @@ #include <mbgl/storage/file_source.hpp> #include <mbgl/util/worker.hpp> #include <mbgl/util/work_request.hpp> -#include <mbgl/util/url.hpp> using namespace mbgl; @@ -20,19 +19,7 @@ RasterTileData::RasterTileData(const TileID& id_, worker(worker_) { state = State::loading; - Resource resource { - Resource::Kind::Tile, - util::templateTileURL(urlTemplate, id, pixelRatio) - }; - - resource.tileData = Resource::TileData { - urlTemplate, - pixelRatio, - id.x, - id.y, - id.z - }; - + const Resource resource = Resource::tile(urlTemplate, pixelRatio, id.x, id.y, id.sourceZ); req = util::ThreadContext::getFileSource()->request(resource, [callback, this](Response res) { if (res.error) { std::exception_ptr error; diff --git a/src/mbgl/map/source.cpp b/src/mbgl/map/source.cpp index fbe276f324..65b9de7abb 100644 --- a/src/mbgl/map/source.cpp +++ b/src/mbgl/map/source.cpp @@ -91,7 +91,7 @@ void Source::load() { // URL may either be a TileJSON file, or a GeoJSON file. FileSource* fs = util::ThreadContext::getFileSource(); - req = fs->request({ Resource::Kind::Source, url }, [this](Response res) { + req = fs->request(Resource::source(url), [this](Response res) { if (res.error) { observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(res.error->message))); return; diff --git a/src/mbgl/map/vector_tile.cpp b/src/mbgl/map/vector_tile.cpp index cfe78d228d..f837714b8d 100644 --- a/src/mbgl/map/vector_tile.cpp +++ b/src/mbgl/map/vector_tile.cpp @@ -185,19 +185,7 @@ VectorTileMonitor::VectorTileMonitor(const TileID& tileID_, float pixelRatio_, c } std::unique_ptr<FileRequest> VectorTileMonitor::monitorTile(const GeometryTileMonitor::Callback& callback) { - Resource resource { - Resource::Kind::Tile, - util::templateTileURL(urlTemplate, tileID, pixelRatio) - }; - - resource.tileData = Resource::TileData { - urlTemplate, - pixelRatio, - tileID.x, - tileID.y, - tileID.z - }; - + const Resource resource = Resource::tile(urlTemplate, pixelRatio, tileID.x, tileID.y, tileID.sourceZ); return util::ThreadContext::getFileSource()->request(resource, [callback, this](Response res) { if (res.notModified) { // We got the same data again. Abort early. diff --git a/src/mbgl/sprite/sprite_store.cpp b/src/mbgl/sprite/sprite_store.cpp index e051b969a1..15a345cc4d 100644 --- a/src/mbgl/sprite/sprite_store.cpp +++ b/src/mbgl/sprite/sprite_store.cpp @@ -32,14 +32,10 @@ void SpriteStore::setURL(const std::string& url) { return; } - std::string spriteURL(url + (pixelRatio > 1 ? "@2x" : "") + ".png"); - std::string jsonURL(url + (pixelRatio > 1 ? "@2x" : "") + ".json"); - loader = std::make_unique<Loader>(); FileSource* fs = util::ThreadContext::getFileSource(); - loader->jsonRequest = fs->request({ Resource::Kind::SpriteJSON, jsonURL }, - [this, jsonURL](Response res) { + loader->jsonRequest = fs->request(Resource::spriteJSON(url, pixelRatio), [this](Response res) { if (res.error) { observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message))); return; @@ -57,24 +53,22 @@ void SpriteStore::setURL(const std::string& url) { } }); - loader->spriteRequest = - fs->request({ Resource::Kind::SpriteImage, spriteURL }, - [this, spriteURL](Response res) { - if (res.error) { - observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message))); - return; - } + loader->spriteRequest = fs->request(Resource::spriteImage(url, pixelRatio), [this](Response res) { + if (res.error) { + observer->onSpriteError(std::make_exception_ptr(std::runtime_error(res.error->message))); + return; + } - if (res.notModified) { - // We got the same data back as last time. Abort early. - return; - } + if (res.notModified) { + // We got the same data back as last time. Abort early. + return; + } - if (!loader->image || *loader->image != *res.data) { - loader->image = res.data; - emitSpriteLoadedIfComplete(); - } - }); + if (!loader->image || *loader->image != *res.data) { + loader->image = res.data; + emitSpriteLoadedIfComplete(); + } + }); } void SpriteStore::emitSpriteLoadedIfComplete() { diff --git a/src/mbgl/storage/resource.cpp b/src/mbgl/storage/resource.cpp new file mode 100644 index 0000000000..9cdedb59eb --- /dev/null +++ b/src/mbgl/storage/resource.cpp @@ -0,0 +1,82 @@ +#include <mbgl/storage/resource.hpp> +#include <mbgl/util/string.hpp> +#include <mbgl/util/token.hpp> +#include <mbgl/util/url.hpp> + +namespace mbgl { + +Resource Resource::style(const std::string& url) { + return Resource { + Resource::Kind::Style, + url + }; +} + +Resource Resource::source(const std::string& url) { + return Resource { + Resource::Kind::Source, + url + }; +} + +Resource Resource::spriteImage(const std::string& base, float pixelRatio) { + return Resource { + Resource::Kind::SpriteImage, + base + (pixelRatio > 1 ? "@2x" : "") + ".png" + }; +} + +Resource Resource::spriteJSON(const std::string& base, float pixelRatio) { + return Resource { + Resource::Kind::SpriteJSON, + base + (pixelRatio > 1 ? "@2x" : "") + ".json" + }; +} + +Resource Resource::glyphs(const std::string& urlTemplate, const std::string& fontStack, const std::pair<uint16_t, uint16_t>& glyphRange) { + return Resource { + Resource::Kind::Glyphs, + util::replaceTokens(urlTemplate, [&](const std::string& token) { + if (token == "fontstack") { + return util::percentEncode(fontStack); + } else if (token == "range") { + return util::toString(glyphRange.first) + "-" + util::toString(glyphRange.second); + } else { + return std::string(); + } + }) + }; +} + +Resource Resource::tile(const std::string& urlTemplate, float pixelRatio, int32_t x, int32_t y, int8_t z) { + return Resource { + Resource::Kind::Tile, + util::replaceTokens(urlTemplate, [&](const std::string& token) { + if (token == "z") { + return util::toString(z); + } else if (token == "x") { + return util::toString(x); + } else if (token == "y") { + return util::toString(y); + } else if (token == "prefix") { + std::string prefix{ 2 }; + prefix[0] = "0123456789abcdef"[x % 16]; + prefix[1] = "0123456789abcdef"[y % 16]; + return prefix; + } else if (token == "ratio") { + return std::string(pixelRatio > 1.0 ? "@2x" : ""); + } else { + return std::string(); + } + }), + Resource::TileData { + urlTemplate, + pixelRatio, + x, + y, + z + } + }; +} + +} diff --git a/src/mbgl/text/glyph_pbf.cpp b/src/mbgl/text/glyph_pbf.cpp index 37b7cfc9fb..0dfed6b430 100644 --- a/src/mbgl/text/glyph_pbf.cpp +++ b/src/mbgl/text/glyph_pbf.cpp @@ -67,15 +67,8 @@ GlyphPBF::GlyphPBF(GlyphStore* store, GlyphStore::Observer* observer_) : parsed(false), observer(observer_) { - // Load the glyph set URL - std::string url = util::replaceTokens(store->getURL(), [&](const std::string &name) -> std::string { - if (name == "fontstack") return util::percentEncode(fontStack); - if (name == "range") return util::toString(glyphRange.first) + "-" + util::toString(glyphRange.second); - return ""; - }); - FileSource* fs = util::ThreadContext::getFileSource(); - req = fs->request({ Resource::Kind::Glyphs, url }, [this, store, fontStack, glyphRange](Response res) { + req = fs->request(Resource::glyphs(store->getURL(), fontStack, glyphRange), [this, store, fontStack, glyphRange](Response res) { if (res.error) { observer->onGlyphsError(fontStack, glyphRange, std::make_exception_ptr(std::runtime_error(res.error->message))); return; diff --git a/src/mbgl/util/url.cpp b/src/mbgl/util/url.cpp index 85d8bc5bd0..bf6fc70ff5 100644 --- a/src/mbgl/util/url.cpp +++ b/src/mbgl/util/url.cpp @@ -1,7 +1,4 @@ #include <mbgl/util/url.hpp> -#include <mbgl/util/token.hpp> -#include <mbgl/util/string.hpp> -#include <mbgl/map/tile_id.hpp> #include <cctype> #include <iomanip> @@ -50,26 +47,5 @@ std::string percentDecode(const std::string& input) { return decoded; } -std::string templateTileURL(const std::string& url, const TileID& id, float pixelRatio) { - return util::replaceTokens(url, [&](const std::string& token) -> std::string { - if (token == "z") { - return util::toString(id.sourceZ); - } else if (token == "x") { - return util::toString(id.x); - } else if (token == "y") { - return util::toString(id.y); - } else if (token == "prefix") { - std::string prefix{ 2 }; - prefix[0] = "0123456789abcdef"[id.x % 16]; - prefix[1] = "0123456789abcdef"[id.y % 16]; - return prefix; - } else if (token == "ratio") { - return pixelRatio > 1.0 ? "@2x" : ""; - } else { - return ""; - } - }); -} - } // namespace util } // namespace mbgl diff --git a/src/mbgl/util/url.hpp b/src/mbgl/util/url.hpp index 550fff8aa1..8baa94be1d 100644 --- a/src/mbgl/util/url.hpp +++ b/src/mbgl/util/url.hpp @@ -5,15 +5,11 @@ namespace mbgl { -class TileID; - namespace util { std::string percentEncode(const std::string&); std::string percentDecode(const std::string&); -std::string templateTileURL(const std::string& url, const TileID& id, float pixelRatio = 1); - } // namespace util } // namespace mbgl diff --git a/test/storage/resource.cpp b/test/storage/resource.cpp new file mode 100644 index 0000000000..30c1597df2 --- /dev/null +++ b/test/storage/resource.cpp @@ -0,0 +1,50 @@ +#include <mbgl/storage/resource.hpp> + +#include <gtest/gtest.h> + +TEST(Resource, Style) { + using namespace mbgl; + Resource resource = Resource::style("http://example.com"); + EXPECT_EQ(Resource::Kind::Style, resource.kind); + EXPECT_EQ("http://example.com", resource.url); +} + +TEST(Resource, Source) { + using namespace mbgl; + Resource resource = Resource::source("http://example.com"); + EXPECT_EQ(Resource::Kind::Source, resource.kind); + EXPECT_EQ("http://example.com", resource.url); +} + +TEST(Resource, Tile) { + using namespace mbgl; + Resource resource = Resource::tile("http://example.com/{z}/{x}/{y}{ratio}.png", 2.0, 1, 2, 3); + EXPECT_EQ(Resource::Kind::Tile, resource.kind); + EXPECT_EQ("http://example.com/3/1/2@2x.png", resource.url); + EXPECT_EQ("http://example.com/{z}/{x}/{y}{ratio}.png", resource.tileData->urlTemplate); + EXPECT_EQ(2.0, resource.tileData->pixelRatio); + EXPECT_EQ(1, resource.tileData->x); + EXPECT_EQ(2, resource.tileData->y); + EXPECT_EQ(3, resource.tileData->z); +} + +TEST(Resource, Glyphs) { + using namespace mbgl; + Resource resource = Resource::glyphs("http://example.com/{fontstack}/{range}", "stack", {0, 255}); + EXPECT_EQ(Resource::Kind::Glyphs, resource.kind); + EXPECT_EQ("http://example.com/stack/0-255", resource.url); +} + +TEST(Resource, SpriteImage) { + using namespace mbgl; + Resource resource = Resource::spriteImage("http://example.com/sprite", 2.0); + EXPECT_EQ(Resource::Kind::SpriteImage, resource.kind); + EXPECT_EQ("http://example.com/sprite@2x.png", resource.url); +} + +TEST(Resource, SpriteJSON) { + using namespace mbgl; + Resource resource = Resource::spriteJSON("http://example.com/sprite", 2.0); + EXPECT_EQ(Resource::Kind::SpriteJSON, resource.kind); + EXPECT_EQ("http://example.com/sprite@2x.json", resource.url); +} diff --git a/test/test.gypi b/test/test.gypi index e0d0951759..628ae3eb3e 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -87,6 +87,7 @@ 'storage/http_retry_network_status.cpp', 'storage/http_reading.cpp', 'storage/http_timeout.cpp', + 'storage/resource.cpp', 'style/glyph_store.cpp', 'style/pending_resources.cpp', |