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

Commit

Permalink
[core] Cache file source base URL and access token
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
boundsj committed Feb 28, 2017
1 parent 147654a commit e277b6e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
18 changes: 18 additions & 0 deletions include/mbgl/storage/default_file_source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,23 @@ class DefaultFileSource : public FileSource {
*/
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.
Expand All @@ -127,6 +143,8 @@ class DefaultFileSource : public FileSource {
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
10 changes: 6 additions & 4 deletions platform/default/default_file_source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
16 changes: 16 additions & 0 deletions test/storage/default_file_source.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:", ".");
Expand Down

0 comments on commit e277b6e

Please sign in to comment.