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

Introduce pause/resume API on default file source #8125

Merged
merged 2 commits into from
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer valid.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fyi this was fixed in #8465 @tmpsantos

* 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
15 changes: 15 additions & 0 deletions platform/darwin/src/MGLOfflineStorage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,26 @@ + (instancetype)sharedOfflineStorage {
static MGLOfflineStorage *sharedOfflineStorage;
dispatch_once(&onceToken, ^{
sharedOfflineStorage = [[self alloc] init];
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
[[NSNotificationCenter defaultCenter] addObserver:sharedOfflineStorage selector:@selector(unpauseFileSource:) name:UIApplicationWillEnterForegroundNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:sharedOfflineStorage selector:@selector(pauseFileSource:) name:UIApplicationDidEnterBackgroundNotification object:nil];
#endif
[sharedOfflineStorage reloadPacks];
});

return sharedOfflineStorage;
}

#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
- (void)pauseFileSource:(__unused NSNotification *)notification {
_mbglFileSource->pause();
}

- (void)unpauseFileSource:(__unused NSNotification *)notification {
_mbglFileSource->resume();
}
#endif

- (void)setDelegate:(id<MGLOfflineStorageDelegate>)newValue {
_delegate = newValue;
if ([self.delegate respondsToSelector:@selector(offlineStorage:URLForResourceOfKind:withURL:)]) {
Expand Down Expand Up @@ -203,6 +217,7 @@ + (NSString *)bundleIdentifier {
}

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[MGLNetworkConfiguration sharedManager] removeObserver:self forKeyPath:@"apiBaseURL"];
[[MGLAccountManager sharedManager] removeObserver:self forKeyPath:@"accessToken"];

Expand Down
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