DefaultFileSource has responsibility for handling mapbox:// URLs
This commit is contained in:
committed by
Konstantin Käfer
parent
f11a9f8fd9
commit
87b1da7611
@@ -373,14 +373,14 @@ nativeSetAccessToken(JNIEnv *env, jobject obj, jlong nativeMapViewPtr, jstring a
|
||||
mbgl::Log::Debug(mbgl::Event::JNI, "nativeSetAccessToken");
|
||||
assert(nativeMapViewPtr != 0);
|
||||
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
|
||||
nativeMapView->getMap().setAccessToken(std_string_from_jstring(env, accessToken));
|
||||
nativeMapView->getFileSource().setAccessToken(std_string_from_jstring(env, accessToken));
|
||||
}
|
||||
|
||||
jstring JNICALL nativeGetAccessToken(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) {
|
||||
mbgl::Log::Debug(mbgl::Event::JNI, "nativeGetAccessToken");
|
||||
assert(nativeMapViewPtr != 0);
|
||||
NativeMapView *nativeMapView = reinterpret_cast<NativeMapView *>(nativeMapViewPtr);
|
||||
return std_string_to_jstring(env, nativeMapView->getMap().getAccessToken());
|
||||
return std_string_to_jstring(env, nativeMapView->getFileSource().getAccessToken());
|
||||
}
|
||||
|
||||
void JNICALL nativeCancelTransitions(JNIEnv *env, jobject obj, jlong nativeMapViewPtr) {
|
||||
|
||||
@@ -81,14 +81,14 @@ int main(int argc, char *argv[]) {
|
||||
}
|
||||
}
|
||||
|
||||
HeadlessView view;
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
|
||||
// Set access token if present
|
||||
if (token.size()) {
|
||||
map.setAccessToken(std::string(token));
|
||||
fileSource.setAccessToken(std::string(token));
|
||||
}
|
||||
|
||||
HeadlessView view;
|
||||
Map map(view, fileSource, MapMode::Still);
|
||||
|
||||
map.setStyleJSON(style, ".");
|
||||
map.setClasses(classes);
|
||||
|
||||
|
||||
@@ -99,10 +99,6 @@ public:
|
||||
uint16_t getWidth() const;
|
||||
uint16_t getHeight() const;
|
||||
|
||||
// API
|
||||
void setAccessToken(const std::string &token);
|
||||
std::string getAccessToken() const;
|
||||
|
||||
// Projection
|
||||
void getWorldBoundsMeters(ProjectedMeters &sw, ProjectedMeters &ne) const;
|
||||
void getWorldBoundsLatLng(LatLng &sw, LatLng &ne) const;
|
||||
|
||||
@@ -15,6 +15,9 @@ public:
|
||||
DefaultFileSource(FileCache *cache, const std::string &root = "");
|
||||
~DefaultFileSource() override;
|
||||
|
||||
void setAccessToken(const std::string& t) { accessToken = t; }
|
||||
std::string getAccessToken() const { return accessToken; }
|
||||
|
||||
// FileSource API
|
||||
Request* request(const Resource&, uv_loop_t*, Callback) override;
|
||||
void cancel(Request*) override;
|
||||
@@ -23,6 +26,7 @@ public:
|
||||
class Impl;
|
||||
private:
|
||||
const std::unique_ptr<util::Thread<Impl>> thread;
|
||||
std::string accessToken;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
@@ -9,10 +9,12 @@ namespace mbgl {
|
||||
struct Resource {
|
||||
enum Kind : uint8_t {
|
||||
Unknown = 0,
|
||||
Tile = 1,
|
||||
Glyphs = 2,
|
||||
Image = 3,
|
||||
JSON = 4,
|
||||
Style,
|
||||
Source,
|
||||
Tile,
|
||||
Glyphs,
|
||||
JSON,
|
||||
Image
|
||||
};
|
||||
|
||||
const Kind kind;
|
||||
|
||||
@@ -70,6 +70,15 @@ int main(int argc, char *argv[]) {
|
||||
|
||||
mbgl::SQLiteCache cache("/tmp/mbgl-cache.db");
|
||||
mbgl::DefaultFileSource fileSource(&cache);
|
||||
|
||||
// Set access token if present
|
||||
const char *token = getenv("MAPBOX_ACCESS_TOKEN");
|
||||
if (token == nullptr) {
|
||||
mbgl::Log::Warning(mbgl::Event::Setup, "no access token set. mapbox.com tiles won't work.");
|
||||
} else {
|
||||
fileSource.setAccessToken(std::string(token));
|
||||
}
|
||||
|
||||
mbgl::Map map(*view, fileSource);
|
||||
|
||||
// Load settings
|
||||
@@ -92,14 +101,6 @@ int main(int argc, char *argv[]) {
|
||||
mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first);
|
||||
});
|
||||
|
||||
// Set access token if present
|
||||
const char *token = getenv("MAPBOX_ACCESS_TOKEN");
|
||||
if (token == nullptr) {
|
||||
mbgl::Log::Warning(mbgl::Event::Setup, "no access token set. mapbox.com tiles won't work.");
|
||||
} else {
|
||||
map.setAccessToken(std::string(token));
|
||||
}
|
||||
|
||||
// Load style
|
||||
if (style.empty()) {
|
||||
const auto& newStyle = mbgl::util::defaultStyles.front();
|
||||
|
||||
@@ -106,6 +106,12 @@ int main() {
|
||||
|
||||
mbgl::SQLiteCache cache(defaultCacheDatabase());
|
||||
mbgl::DefaultFileSource fileSource(&cache);
|
||||
|
||||
// Set access token if present
|
||||
NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"];
|
||||
if (!accessToken) mbgl::Log::Warning(mbgl::Event::Setup, "No access token set. Mapbox vector tiles won't work.");
|
||||
if (accessToken) fileSource.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]);
|
||||
|
||||
mbgl::Map map(view, fileSource);
|
||||
|
||||
URLHandler *handler = [[URLHandler alloc] init];
|
||||
@@ -140,11 +146,6 @@ int main() {
|
||||
mbgl::Log::Info(mbgl::Event::Setup, std::string("Changed style to: ") + newStyle.first);
|
||||
});
|
||||
|
||||
// Set access token if present
|
||||
NSString *accessToken = [[NSProcessInfo processInfo] environment][@"MAPBOX_ACCESS_TOKEN"];
|
||||
if (!accessToken) mbgl::Log::Warning(mbgl::Event::Setup, "No access token set. Mapbox vector tiles won't work.");
|
||||
if (accessToken) map.setAccessToken([accessToken cStringUsingEncoding:[NSString defaultCStringEncoding]]);
|
||||
|
||||
// Load style
|
||||
const auto& newStyle = mbgl::util::defaultStyles.front();
|
||||
map.setStyleURL(newStyle.first);
|
||||
|
||||
@@ -1303,7 +1303,7 @@ std::chrono::steady_clock::duration secondsAsDuration(float duration)
|
||||
if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager])
|
||||
{
|
||||
NSString *accessToken = change[NSKeyValueChangeNewKey];
|
||||
_mbglMap->setAccessToken(accessToken ? (std::string)[accessToken UTF8String] : "");
|
||||
_mbglFileSource->setAccessToken(accessToken ? (std::string)[accessToken UTF8String] : "");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -195,17 +195,6 @@ void Map::resetNorth() {
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Access Token
|
||||
|
||||
void Map::setAccessToken(const std::string &token) {
|
||||
data->setAccessToken(token);
|
||||
}
|
||||
|
||||
std::string Map::getAccessToken() const {
|
||||
return data->getAccessToken();
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Projection
|
||||
|
||||
void Map::getWorldBoundsMeters(ProjectedMeters& sw, ProjectedMeters& ne) const {
|
||||
|
||||
@@ -24,7 +24,6 @@
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
#include <mbgl/util/worker.hpp>
|
||||
#include <mbgl/util/texture_pool.hpp>
|
||||
#include <mbgl/util/mapbox.hpp>
|
||||
#include <mbgl/util/exception.hpp>
|
||||
|
||||
namespace mbgl {
|
||||
@@ -90,7 +89,7 @@ void MapContext::triggerUpdate(const Update u) {
|
||||
}
|
||||
|
||||
void MapContext::setStyleURL(const std::string& url) {
|
||||
styleURL = mbgl::util::mapbox::normalizeStyleURL(url, data.getAccessToken());
|
||||
styleURL = url;
|
||||
styleJSON.clear();
|
||||
|
||||
const size_t pos = styleURL.rfind('/');
|
||||
@@ -99,7 +98,7 @@ void MapContext::setStyleURL(const std::string& url) {
|
||||
base = styleURL.substr(0, pos + 1);
|
||||
}
|
||||
|
||||
env.request({ Resource::Kind::JSON, styleURL }, [this, base](const Response &res) {
|
||||
env.request({ Resource::Kind::Style, styleURL }, [this, base](const Response &res) {
|
||||
if (res.status == Response::Successful) {
|
||||
loadStyleJSON(res.data, base);
|
||||
} else {
|
||||
@@ -127,11 +126,9 @@ void MapContext::loadStyleJSON(const std::string& json, const std::string& base)
|
||||
style->cascade(data.getClasses());
|
||||
style->setDefaultTransitionDuration(data.getDefaultTransitionDuration());
|
||||
|
||||
const std::string glyphURL = util::mapbox::normalizeGlyphsURL(style->glyph_url, data.getAccessToken());
|
||||
glyphStore->setURL(glyphURL);
|
||||
glyphStore->setURL(style->glyph_url);
|
||||
|
||||
resourceLoader = util::make_unique<ResourceLoader>();
|
||||
resourceLoader->setAccessToken(data.getAccessToken());
|
||||
resourceLoader->setObserver(this);
|
||||
resourceLoader->setStyle(style.get());
|
||||
resourceLoader->setGlyphStore(glyphStore.get());
|
||||
|
||||
@@ -27,15 +27,6 @@ public:
|
||||
setDefaultTransitionDuration(Duration::zero());
|
||||
}
|
||||
|
||||
inline std::string getAccessToken() const {
|
||||
Lock lock(mtx);
|
||||
return accessToken;
|
||||
}
|
||||
inline void setAccessToken(const std::string &token) {
|
||||
Lock lock(mtx);
|
||||
accessToken = token;
|
||||
}
|
||||
|
||||
// Adds the class if it's not yet set. Returns true when it added the class, and false when it
|
||||
// was already present.
|
||||
bool addClass(const std::string& klass);
|
||||
@@ -95,7 +86,6 @@ public:
|
||||
private:
|
||||
mutable std::mutex mtx;
|
||||
|
||||
std::string accessToken;
|
||||
std::vector<std::string> classes;
|
||||
std::atomic<uint8_t> debug { false };
|
||||
std::atomic<bool> loaded { false };
|
||||
|
||||
@@ -45,7 +45,7 @@ void ResourceLoader::setStyle(Style* style) {
|
||||
|
||||
for (const auto& source : style->sources) {
|
||||
source->setObserver(this);
|
||||
source->load(accessToken_);
|
||||
source->load();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,11 +60,6 @@ void ResourceLoader::setGlyphStore(GlyphStore* glyphStore) {
|
||||
glyphStore_->setObserver(this);
|
||||
}
|
||||
|
||||
|
||||
void ResourceLoader::setAccessToken(const std::string& accessToken) {
|
||||
accessToken_ = accessToken;
|
||||
}
|
||||
|
||||
void ResourceLoader::update(MapData& data,
|
||||
const TransformState& transform,
|
||||
GlyphAtlas& glyphAtlas,
|
||||
|
||||
@@ -49,9 +49,6 @@ public:
|
||||
// style.
|
||||
void setGlyphStore(GlyphStore* glyphStore);
|
||||
|
||||
// Set the access token to be used for loading the tile data.
|
||||
void setAccessToken(const std::string& accessToken);
|
||||
|
||||
// Fetch the tiles needed by the current viewport and emit a signal when
|
||||
// a tile is ready so observers can render the tile.
|
||||
void update(MapData&, const TransformState&, GlyphAtlas&, SpriteAtlas&, TexturePool&);
|
||||
@@ -81,7 +78,6 @@ private:
|
||||
|
||||
bool shouldReparsePartialTiles_ = false;
|
||||
|
||||
std::string accessToken_;
|
||||
util::ptr<Sprite> sprite_;
|
||||
|
||||
GlyphStore* glyphStore_ = nullptr;
|
||||
|
||||
@@ -140,14 +140,13 @@ bool Source::isLoaded() const {
|
||||
// Note: This is a separate function that must be called exactly once after creation
|
||||
// The reason this isn't part of the constructor is that calling shared_from_this() in
|
||||
// the constructor fails.
|
||||
void Source::load(const std::string& accessToken) {
|
||||
void Source::load() {
|
||||
if (info.url.empty()) {
|
||||
loaded = true;
|
||||
return;
|
||||
}
|
||||
|
||||
const std::string url = util::mapbox::normalizeSourceURL(info.url, accessToken);
|
||||
req = Environment::Get().request({ Resource::Kind::JSON, url }, [this](const Response &res) {
|
||||
req = Environment::Get().request({ Resource::Kind::Source, info.url }, [this](const Response &res) {
|
||||
req = nullptr;
|
||||
|
||||
if (res.status != Response::Successful) {
|
||||
|
||||
@@ -67,7 +67,7 @@ public:
|
||||
Source();
|
||||
~Source();
|
||||
|
||||
void load(const std::string& accessToken);
|
||||
void load();
|
||||
bool isLoaded() const;
|
||||
|
||||
void load(MapData&, Environment&, std::function<void()> callback);
|
||||
|
||||
@@ -5,11 +5,12 @@
|
||||
|
||||
#include <mbgl/storage/response.hpp>
|
||||
#include <mbgl/platform/platform.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
#include <mbgl/util/chrono.hpp>
|
||||
#include <mbgl/util/thread.hpp>
|
||||
#include <mbgl/platform/log.hpp>
|
||||
#include <mbgl/util/mapbox.hpp>
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wshadow"
|
||||
@@ -38,7 +39,27 @@ Request* DefaultFileSource::request(const Resource& resource,
|
||||
uv_loop_t* l,
|
||||
Callback callback) {
|
||||
assert(l);
|
||||
auto req = new Request(resource, l, std::move(callback));
|
||||
|
||||
std::string url;
|
||||
|
||||
switch (resource.kind) {
|
||||
case Resource::Kind::Style:
|
||||
url = mbgl::util::mapbox::normalizeStyleURL(resource.url, accessToken);
|
||||
break;
|
||||
|
||||
case Resource::Kind::Source:
|
||||
url = util::mapbox::normalizeSourceURL(resource.url, accessToken);
|
||||
break;
|
||||
|
||||
case Resource::Kind::Glyphs:
|
||||
url = util::mapbox::normalizeGlyphsURL(resource.url, accessToken);
|
||||
break;
|
||||
|
||||
default:
|
||||
url = resource.url;
|
||||
}
|
||||
|
||||
auto req = new Request({ resource.kind, url }, l, std::move(callback));
|
||||
thread->invoke(&Impl::add, req);
|
||||
return req;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user