[core] Cache file source base URL and access token
This caches the base URL and access token values when they are set so that they can still be retrieved even when the thread is paused.
This commit is contained in:
@@ -115,7 +115,23 @@ public:
|
||||
*/
|
||||
void setOfflineMapboxTileCountLimit(uint64_t) const;
|
||||
|
||||
/*
|
||||
* Pause file request activity.
|
||||
*
|
||||
* If pause is called then no revalidation or network request activity
|
||||
* will occur.
|
||||
*
|
||||
* Note: Calling pause and then calling getAPIBaseURL or getAccessToken
|
||||
* will lock the thread that those calls are made on.
|
||||
*/
|
||||
void pause();
|
||||
|
||||
/*
|
||||
* Resume file request activity.
|
||||
*
|
||||
* Calling resume will unpause the file source and process any tasks that
|
||||
* expired while the file source was paused.
|
||||
*/
|
||||
void resume();
|
||||
|
||||
// For testing only.
|
||||
@@ -127,6 +143,8 @@ private:
|
||||
const std::unique_ptr<util::Thread<Impl>> thread;
|
||||
const std::unique_ptr<FileSource> assetFileSource;
|
||||
const std::unique_ptr<FileSource> localFileSource;
|
||||
std::string cachedBaseURL = mbgl::util::API_BASE_URL;
|
||||
std::string cachedAccessToken;
|
||||
};
|
||||
|
||||
} // namespace mbgl
|
||||
|
||||
@@ -176,19 +176,21 @@ DefaultFileSource::DefaultFileSource(const std::string& cachePath,
|
||||
DefaultFileSource::~DefaultFileSource() = default;
|
||||
|
||||
void DefaultFileSource::setAPIBaseURL(const std::string& baseURL) {
|
||||
thread->invokeSync(&Impl::setAPIBaseURL, baseURL);
|
||||
thread->invoke(&Impl::setAPIBaseURL, baseURL);
|
||||
cachedBaseURL = baseURL;
|
||||
}
|
||||
|
||||
std::string DefaultFileSource::getAPIBaseURL() const {
|
||||
return thread->invokeSync(&Impl::getAPIBaseURL);
|
||||
return cachedBaseURL;
|
||||
}
|
||||
|
||||
void DefaultFileSource::setAccessToken(const std::string& accessToken) {
|
||||
thread->invokeSync(&Impl::setAccessToken, accessToken);
|
||||
thread->invoke(&Impl::setAccessToken, accessToken);
|
||||
cachedAccessToken = accessToken;
|
||||
}
|
||||
|
||||
std::string DefaultFileSource::getAccessToken() const {
|
||||
return thread->invokeSync(&Impl::getAccessToken);
|
||||
return cachedAccessToken;
|
||||
}
|
||||
|
||||
void DefaultFileSource::setResourceTransform(std::function<std::string(Resource::Kind, std::string&&)> transform) {
|
||||
|
||||
@@ -267,6 +267,22 @@ TEST(DefaultFileSource, OptionalExpired) {
|
||||
loop.run();
|
||||
}
|
||||
|
||||
TEST(DefaultFileSource, GetBaseURLAndAccessTokenWhilePaused) {
|
||||
util::RunLoop loop;
|
||||
DefaultFileSource fs(":memory:", ".");
|
||||
|
||||
fs.pause();
|
||||
|
||||
auto baseURL = "http://url";
|
||||
auto accessToken = "access_token";
|
||||
|
||||
fs.setAPIBaseURL(baseURL);
|
||||
fs.setAccessToken(accessToken);
|
||||
|
||||
EXPECT_EQ(fs.getAPIBaseURL(), baseURL);
|
||||
EXPECT_EQ(fs.getAccessToken(), accessToken);
|
||||
}
|
||||
|
||||
TEST(DefaultFileSource, OptionalNotFound) {
|
||||
util::RunLoop loop;
|
||||
DefaultFileSource fs(":memory:", ".");
|
||||
|
||||
Reference in New Issue
Block a user