Move setAccessToken to CachingHTTPFileSource
This commit is contained in:
@@ -78,8 +78,6 @@ public:
|
||||
void setStyleURL(const std::string &url);
|
||||
void setStyleJSON(std::string newStyleJSON, const std::string &base = "");
|
||||
std::string getStyleJSON() const;
|
||||
void setAccessToken(std::string accessToken);
|
||||
std::string getAccessToken() const;
|
||||
|
||||
// Transition
|
||||
void cancelTransitions();
|
||||
@@ -187,7 +185,6 @@ private:
|
||||
|
||||
std::string styleURL;
|
||||
std::string styleJSON = "";
|
||||
std::string accessToken = "";
|
||||
|
||||
bool debug = false;
|
||||
timestamp animationTime = 0;
|
||||
|
||||
@@ -23,9 +23,11 @@ public:
|
||||
bool hasLoop();
|
||||
void clearLoop();
|
||||
|
||||
// Stores and retrieves the base path/URL for relative requests
|
||||
void setBase(const std::string &value);
|
||||
const std::string &getBase() const;
|
||||
// Set the base path/URL for relative requests
|
||||
void setBase(std::string);
|
||||
|
||||
// Set the Mapbox API access token
|
||||
void setAccessToken(std::string);
|
||||
|
||||
std::unique_ptr<Request> request(ResourceType type, const std::string &url);
|
||||
|
||||
@@ -37,6 +39,9 @@ public:
|
||||
private:
|
||||
std::thread::id thread_id;
|
||||
|
||||
// Mapbox API access token.
|
||||
std::string accessToken;
|
||||
|
||||
// Path to the cache database.
|
||||
std::string path;
|
||||
|
||||
|
||||
@@ -21,7 +21,7 @@ public:
|
||||
virtual bool hasLoop() = 0;
|
||||
virtual void clearLoop() = 0;
|
||||
|
||||
virtual void setBase(const std::string &value) = 0;
|
||||
virtual void setBase(std::string) = 0;
|
||||
virtual std::unique_ptr<Request> request(ResourceType type, const std::string &url) = 0;
|
||||
virtual void prepare(std::function<void()> fn) = 0;
|
||||
};
|
||||
|
||||
Submodule ios/mapbox-gl-cocoa updated: cf7436759f...1533d3288f
@@ -78,7 +78,7 @@ int main(int argc, char *argv[]) {
|
||||
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));
|
||||
fileSource.setAccessToken(std::string(token));
|
||||
}
|
||||
|
||||
// Load style
|
||||
|
||||
@@ -96,11 +96,10 @@ int main() {
|
||||
map.setBearing(settings.bearing);
|
||||
map.setDebug(settings.debug);
|
||||
|
||||
|
||||
// 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]]);
|
||||
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]]);
|
||||
|
||||
// Load style
|
||||
const std::string path([[[NSBundle mainBundle] pathForResource:@"bright-v6" ofType:@"json" inDirectory:@"styles/"] UTF8String]);
|
||||
|
||||
@@ -12,7 +12,6 @@
|
||||
#include <mbgl/util/constants.hpp>
|
||||
#include <mbgl/util/uv_detail.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
#include <mbgl/util/mapbox.hpp>
|
||||
#include <mbgl/style/style.hpp>
|
||||
#include <mbgl/text/glyph_store.hpp>
|
||||
#include <mbgl/geometry/glyph_atlas.hpp>
|
||||
@@ -313,7 +312,7 @@ void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) {
|
||||
}
|
||||
style->loadJSON((const uint8_t *)styleJSON.c_str());
|
||||
fileSource.setBase(base);
|
||||
glyphStore->setURL(util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken()));
|
||||
glyphStore->setURL(style->glyph_url);
|
||||
update();
|
||||
}
|
||||
|
||||
@@ -321,15 +320,6 @@ std::string Map::getStyleJSON() const {
|
||||
return styleJSON;
|
||||
}
|
||||
|
||||
void Map::setAccessToken(std::string accessToken_) {
|
||||
// TODO: Make threadsafe.
|
||||
accessToken.swap(accessToken_);
|
||||
}
|
||||
|
||||
std::string Map::getAccessToken() const {
|
||||
return accessToken;
|
||||
}
|
||||
|
||||
util::ptr<Sprite> Map::getSprite() {
|
||||
const float pixelRatio = state.getPixelRatio();
|
||||
const std::string &sprite_url = style->getSpriteURL();
|
||||
|
||||
@@ -37,10 +37,9 @@ void Source::load(Map& map, FileSource& fileSource) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::string url = util::mapbox::normalizeSourceURL(info.url, map.getAccessToken());
|
||||
util::ptr<Source> source = shared_from_this();
|
||||
|
||||
fileSource.request(ResourceType::JSON, url)->onload([source, &map](const Response &res) {
|
||||
fileSource.request(ResourceType::JSON, info.url)->onload([source, &map](const Response &res) {
|
||||
if (res.code != 200) {
|
||||
Log::Warning(Event::General, "failed to load source TileJSON");
|
||||
return;
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <mbgl/storage/http_request.hpp>
|
||||
#include <mbgl/storage/sqlite_store.hpp>
|
||||
#include <mbgl/util/uv-messenger.h>
|
||||
#include <mbgl/util/mapbox.hpp>
|
||||
#include <mbgl/util/std.hpp>
|
||||
|
||||
#include <uv.h>
|
||||
@@ -52,44 +53,51 @@ void CachingHTTPFileSource::clearLoop() {
|
||||
store.reset();
|
||||
}
|
||||
|
||||
void CachingHTTPFileSource::setBase(const std::string &value) {
|
||||
base = value;
|
||||
void CachingHTTPFileSource::setBase(std::string value) {
|
||||
// TODO: Make threadsafe.
|
||||
base.swap(value);
|
||||
}
|
||||
|
||||
const std::string &CachingHTTPFileSource::getBase() const {
|
||||
return base;
|
||||
void CachingHTTPFileSource::setAccessToken(std::string value) {
|
||||
// TODO: Make threadsafe.
|
||||
accessToken.swap(value);
|
||||
}
|
||||
|
||||
std::unique_ptr<Request> CachingHTTPFileSource::request(ResourceType type, const std::string &url) {
|
||||
std::unique_ptr<Request> CachingHTTPFileSource::request(ResourceType type, const std::string& url_) {
|
||||
assert(thread_id == std::this_thread::get_id());
|
||||
|
||||
std::string url = url_;
|
||||
|
||||
// Make URL absolute.
|
||||
const std::string absoluteURL = [&]() -> std::string {
|
||||
const size_t separator = url.find("://");
|
||||
if (separator == std::string::npos) {
|
||||
// Relative URL.
|
||||
return base + url;
|
||||
} else {
|
||||
return url;
|
||||
}
|
||||
}();
|
||||
const size_t separator = url.find("://");
|
||||
if (separator == std::string::npos) {
|
||||
url = base + url;
|
||||
}
|
||||
|
||||
// Normalize mapbox:// URLs.
|
||||
switch (type) {
|
||||
case ResourceType::Glyphs:
|
||||
url = util::mapbox::normalizeGlyphsURL(url, accessToken);
|
||||
default:
|
||||
url = util::mapbox::normalizeSourceURL(url, accessToken);
|
||||
}
|
||||
|
||||
util::ptr<BaseRequest> req;
|
||||
|
||||
// First, try to find an existing Request object.
|
||||
auto it = pending.find(absoluteURL);
|
||||
auto it = pending.find(url);
|
||||
if (it != pending.end()) {
|
||||
req = it->second.lock();
|
||||
}
|
||||
|
||||
if (!req) {
|
||||
if (absoluteURL.substr(0, 7) == "file://") {
|
||||
req = std::make_shared<FileRequest>(absoluteURL.substr(7), loop);
|
||||
if (url.substr(0, 7) == "file://") {
|
||||
req = std::make_shared<FileRequest>(url.substr(7), loop);
|
||||
} else {
|
||||
req = std::make_shared<HTTPRequest>(type, absoluteURL, loop, store);
|
||||
req = std::make_shared<HTTPRequest>(type, url, loop, store);
|
||||
}
|
||||
|
||||
pending.emplace(absoluteURL, req);
|
||||
pending.emplace(url, req);
|
||||
}
|
||||
|
||||
return util::make_unique<Request>(req);
|
||||
|
||||
Reference in New Issue
Block a user