Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
DefaultFileSource has responsibility for handling mapbox:// URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed May 22, 2015
1 parent 0689ad6 commit c7afc7b
Show file tree
Hide file tree
Showing 15 changed files with 61 additions and 70 deletions.
4 changes: 2 additions & 2 deletions android/cpp/jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions bin/render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 0 additions & 4 deletions include/mbgl/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,6 @@ class Map : private util::noncopyable {
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;
Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/storage/default_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class DefaultFileSource : public FileSource {
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;
Expand All @@ -23,6 +26,7 @@ class DefaultFileSource : public FileSource {
class Impl;
private:
const std::unique_ptr<util::Thread<Impl>> thread;
std::string accessToken;
};

}
Expand Down
10 changes: 6 additions & 4 deletions include/mbgl/storage/resource.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
17 changes: 9 additions & 8 deletions linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand Down
11 changes: 6 additions & 5 deletions macosx/main.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 0 additions & 11 deletions src/mbgl/map/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
9 changes: 3 additions & 6 deletions src/mbgl/map/map_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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('/');
Expand All @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down
10 changes: 0 additions & 10 deletions src/mbgl/map/map_data.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ class MapData {
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);
Expand Down Expand Up @@ -95,7 +86,6 @@ class MapData {
private:
mutable std::mutex mtx;

std::string accessToken;
std::vector<std::string> classes;
std::atomic<uint8_t> debug { false };
std::atomic<bool> loaded { false };
Expand Down
7 changes: 1 addition & 6 deletions src/mbgl/map/resource_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ void ResourceLoader::setStyle(Style* style) {

for (const auto& source : style->sources) {
source->setObserver(this);
source->load(accessToken_);
source->load();
}
}

Expand All @@ -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,
Expand Down
4 changes: 0 additions & 4 deletions src/mbgl/map/resource_loader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@ class ResourceLoader : public GlyphStore::Observer,
// 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&);
Expand All @@ -75,7 +72,6 @@ class ResourceLoader : public GlyphStore::Observer,

bool shouldReparsePartialTiles_ = false;

std::string accessToken_;
util::ptr<Sprite> sprite_;

GlyphStore* glyphStore_ = nullptr;
Expand Down
5 changes: 2 additions & 3 deletions src/mbgl/map/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,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) {
Expand Down
2 changes: 1 addition & 1 deletion src/mbgl/map/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class Source : private util::noncopyable {
Source();
~Source();

void load(const std::string& accessToken);
void load();
bool isLoaded() const;

void load(MapData&, Environment&, std::function<void()> callback);
Expand Down
25 changes: 23 additions & 2 deletions src/mbgl/storage/default_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit c7afc7b

Please sign in to comment.