diff options
author | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2019-10-08 11:33:30 +0300 |
---|---|---|
committer | Alexander Shalamov <alexander.shalamov@mapbox.com> | 2020-01-13 10:57:23 +0200 |
commit | 77c86bc6b9777c30120f4aba98af9f21222b54da (patch) | |
tree | 9bcfce8106fc4280746908b6cbc5895b1c6cdb4f | |
parent | c8024cf00d1fefb8d736e0cb155326264c48372a (diff) | |
download | qtlocation-mapboxgl-77c86bc6b9777c30120f4aba98af9f21222b54da.tar.gz |
[darwin] Use new FileSourceManager interface
-rw-r--r-- | platform/darwin/filesource-files.json | 2 | ||||
-rw-r--r-- | platform/darwin/src/MGLMapSnapshotter.mm | 1 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflinePack.mm | 16 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 50 | ||||
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage_Private.h | 17 | ||||
-rw-r--r-- | platform/darwin/src/http_file_source.mm | 1 | ||||
-rw-r--r-- | platform/darwin/test/MGLOfflineStorageTests.mm | 1 |
7 files changed, 52 insertions, 36 deletions
diff --git a/platform/darwin/filesource-files.json b/platform/darwin/filesource-files.json index 5d72549f73..4b2fe21c06 100644 --- a/platform/darwin/filesource-files.json +++ b/platform/darwin/filesource-files.json @@ -4,8 +4,6 @@ "platform/darwin/src/MGLLoggingConfiguration.mm", "platform/darwin/src/MGLNetworkConfiguration.m", "platform/darwin/src/http_file_source.mm", - "platform/default/src/mbgl/storage/file_source.cpp", - "platform/default/src/mbgl/storage/file_source_manager.cpp", "platform/default/src/mbgl/storage/sqlite3.cpp" ], "public_headers": {}, diff --git a/platform/darwin/src/MGLMapSnapshotter.mm b/platform/darwin/src/MGLMapSnapshotter.mm index 0dde94292c..f3e9e7a41e 100644 --- a/platform/darwin/src/MGLMapSnapshotter.mm +++ b/platform/darwin/src/MGLMapSnapshotter.mm @@ -7,7 +7,6 @@ #import <mbgl/map/map_snapshotter.hpp> #import <mbgl/map/camera.hpp> #import <mbgl/storage/resource_options.hpp> -#import <mbgl/storage/default_file_source.hpp> #import <mbgl/util/string.hpp> #import "MGLOfflineStorage_Private.h" diff --git a/platform/darwin/src/MGLOfflinePack.mm b/platform/darwin/src/MGLOfflinePack.mm index 5f4ae9c838..edee549744 100644 --- a/platform/darwin/src/MGLOfflinePack.mm +++ b/platform/darwin/src/MGLOfflinePack.mm @@ -11,7 +11,7 @@ #import "NSValue+MGLAdditions.h" #include <mbgl/map/map_options.hpp> -#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/database_file_source.hpp> const MGLExceptionName MGLInvalidOfflinePackException = @"MGLInvalidOfflinePackException"; @@ -59,7 +59,7 @@ private: @implementation MGLOfflinePack { BOOL _isSuspending; - std::shared_ptr<mbgl::DefaultFileSource> _mbglFileSource; + std::shared_ptr<mbgl::DatabaseFileSource> _mbglDatabaseFileSource; } - (instancetype)init { @@ -76,8 +76,8 @@ private: _mbglOfflineRegion = region; _state = MGLOfflinePackStateUnknown; - _mbglFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglFileSource]; - _mbglFileSource->setOfflineRegionObserver(*_mbglOfflineRegion, std::make_unique<MBGLOfflineRegionObserver>(self)); + _mbglDatabaseFileSource = [[MGLOfflineStorage sharedOfflineStorage] mbglDatabaseFileSource]; + _mbglDatabaseFileSource->setOfflineRegionObserver(*_mbglOfflineRegion, std::make_unique<MBGLOfflineRegionObserver>(self)); } return self; } @@ -117,7 +117,7 @@ private: self.state = MGLOfflinePackStateActive; - _mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active); + _mbglDatabaseFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active); } - (void)suspend { @@ -129,7 +129,7 @@ private: _isSuspending = YES; } - _mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive); + _mbglDatabaseFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive); } - (void)invalidate { @@ -140,7 +140,7 @@ private: @synchronized (self) { self.state = MGLOfflinePackStateInvalid; if (self.mbglOfflineRegion) { - _mbglFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr); + _mbglDatabaseFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr); } self.mbglOfflineRegion = nil; } @@ -169,7 +169,7 @@ private: MGLAssertOfflinePackIsValid(); __weak MGLOfflinePack *weakSelf = self; - _mbglFileSource->getOfflineRegionStatus(*_mbglOfflineRegion, [&, weakSelf](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) { + _mbglDatabaseFileSource->getOfflineRegionStatus(*_mbglOfflineRegion, [&, weakSelf](mbgl::expected<mbgl::OfflineRegionStatus, std::exception_ptr> status) { if (status) { mbgl::OfflineRegionStatus checkedStatus = *status; dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index 4de406a456..abf67d330e 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -19,7 +19,7 @@ #include <mbgl/actor/actor.hpp> #include <mbgl/actor/scheduler.hpp> -#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/file_source_manager.hpp> #include <mbgl/storage/resource_options.hpp> #include <mbgl/storage/resource_transform.hpp> #include <mbgl/util/chrono.hpp> @@ -45,13 +45,15 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio @interface MGLOfflineStorage () @property (nonatomic, strong, readwrite) NSMutableArray<MGLOfflinePack *> *packs; -@property (nonatomic) std::shared_ptr<mbgl::DefaultFileSource> mbglFileSource; +@property (nonatomic) std::shared_ptr<mbgl::DatabaseFileSource> mbglDatabaseFileSource; +@property (nonatomic) std::shared_ptr<mbgl::OnlineFileSource> mbglOnlineFileSource; +@property (nonatomic) std::shared_ptr<mbgl::FileSource> mbglFileSource; @property (nonatomic) std::string mbglCachePath; @property (nonatomic, getter=isPaused) BOOL paused; @end @implementation MGLOfflineStorage { - std::unique_ptr<mbgl::Actor<mbgl::ResourceTransform>> _mbglResourceTransform; + std::unique_ptr<mbgl::Actor<mbgl::ResourceTransform::TransformCallback>> _mbglResourceTransform; } + (instancetype)sharedOfflineStorage { @@ -91,7 +93,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio MGLLogDebug(@"Setting delegate: %@", newValue); _delegate = newValue; if ([self.delegate respondsToSelector:@selector(offlineStorage:URLForResourceOfKind:withURL:)]) { - _mbglResourceTransform = std::make_unique<mbgl::Actor<mbgl::ResourceTransform>>(*mbgl::Scheduler::GetCurrent(), [offlineStorage = self](auto kind_, const std::string& url_) -> std::string { + _mbglResourceTransform = std::make_unique<mbgl::Actor<mbgl::ResourceTransform::TransformCallback>>(*mbgl::Scheduler::GetCurrent(), [offlineStorage = self](auto kind_, const std::string& url_, mbgl::ResourceTransform::FinishedCallback cb) { NSURL* url = [NSURL URLWithString:[[NSString alloc] initWithBytes:url_.data() length:url_.length() @@ -127,13 +129,15 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio url = [offlineStorage.delegate offlineStorage:offlineStorage URLForResourceOfKind:kind withURL:url]; - return url.absoluteString.UTF8String; + cb(url.absoluteString.UTF8String); }); - _mbglFileSource->setResourceTransform(_mbglResourceTransform->self()); + _mbglOnlineFileSource->setResourceTransform({[actorRef = _mbglResourceTransform->self()](auto kind_, const std::string& url_, mbgl::ResourceTransform::FinishedCallback cb_){ + actorRef.invoke(&mbgl::ResourceTransform::TransformCallback::operator(), kind_, url_, std::move(cb_)); + }}); } else { _mbglResourceTransform.reset(); - _mbglFileSource->setResourceTransform({}); + _mbglOnlineFileSource->setResourceTransform({}); } } @@ -228,7 +232,9 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio mbgl::ResourceOptions options; options.withCachePath(_mbglCachePath) .withAssetPath([NSBundle mainBundle].resourceURL.path.UTF8String); - _mbglFileSource = std::static_pointer_cast<mbgl::DefaultFileSource>(mbgl::FileSource::getSharedFileSource(options)); + _mbglFileSource = mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::ResourceLoader, options); + _mbglOnlineFileSource = std::static_pointer_cast<mbgl::OnlineFileSource>(mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Network, options)); + _mbglDatabaseFileSource = std::static_pointer_cast<mbgl::DatabaseFileSource>(mbgl::FileSourceManager::get()->getFileSource(mbgl::FileSourceType::Database, options)); // Observe for changes to the API base URL (and find out the current one). [[MGLAccountManager sharedManager] addObserver:self @@ -262,14 +268,14 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager]) { NSString *accessToken = change[NSKeyValueChangeNewKey]; if (![accessToken isKindOfClass:[NSNull class]]) { - _mbglFileSource->setAccessToken(accessToken.UTF8String); + _mbglOnlineFileSource->setProperty(mbgl::ACCESS_TOKEN_KEY, accessToken.UTF8String); } } else if ([keyPath isEqualToString:@"apiBaseURL"] && object == [MGLAccountManager sharedManager]) { NSURL *apiBaseURL = change[NSKeyValueChangeNewKey]; if ([apiBaseURL isKindOfClass:[NSNull class]]) { - _mbglFileSource->setAPIBaseURL(mbgl::util::API_BASE_URL); + _mbglFileSource->setProperty(mbgl::API_BASE_URL_KEY, mbgl::util::API_BASE_URL); } else { - _mbglFileSource->setAPIBaseURL(apiBaseURL.absoluteString.UTF8String); + _mbglFileSource->setProperty(mbgl::API_BASE_URL_KEY, apiBaseURL.absoluteString.UTF8String); } } else { [super observeValueForKeyPath:keyPath ofObject:object change:change context:context]; @@ -335,7 +341,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)_addContentsOfFile:(NSString *)filePath withCompletionHandler:(void (^)(NSArray<MGLOfflinePack *> * _Nullable packs, NSError * _Nullable error))completion { - _mbglFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion, filePath](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { + _mbglDatabaseFileSource->mergeOfflineRegions(std::string(static_cast<const char *>([filePath UTF8String])), [&, completion, filePath](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { NSError *error; NSMutableArray *packs; if (!result) { @@ -400,7 +406,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio const mbgl::OfflineRegionDefinition regionDefinition = [(id <MGLOfflineRegion_Private>)region offlineRegionDefinition]; mbgl::OfflineRegionMetadata metadata(context.length); [context getBytes:&metadata[0] length:metadata.size()]; - _mbglFileSource->createOfflineRegion(regionDefinition, metadata, [&, completion](mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> mbglOfflineRegion) { + _mbglDatabaseFileSource->createOfflineRegion(regionDefinition, metadata, [&, completion](mbgl::expected<mbgl::OfflineRegion, std::exception_ptr> mbglOfflineRegion) { NSError *error; if (!mbglOfflineRegion) { NSString *errorDescription = @(mbgl::util::toString(mbglOfflineRegion.error()).c_str()); @@ -441,7 +447,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio return; } - _mbglFileSource->deleteOfflineRegion(std::move(*mbglOfflineRegion), [&, completion](std::exception_ptr exception) { + _mbglDatabaseFileSource->deleteOfflineRegion(std::move(*mbglOfflineRegion), [&, completion](std::exception_ptr exception) { NSError *error; if (exception) { error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ @@ -468,7 +474,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio return; } - _mbglFileSource->invalidateOfflineRegion(region, [&](std::exception_ptr exception) { + _mbglDatabaseFileSource->invalidateOfflineRegion(region, [&](std::exception_ptr exception) { if (exception) { error = [NSError errorWithDomain:MGLErrorDomain code:MGLErrorCodeModifyingOfflineStorageFailed userInfo:@{ NSLocalizedDescriptionKey: @(mbgl::util::toString(exception).c_str()), @@ -496,7 +502,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)getPacksWithCompletionHandler:(void (^)(NSArray<MGLOfflinePack *> *packs, NSError * _Nullable error))completion { - _mbglFileSource->listOfflineRegions([&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { + _mbglDatabaseFileSource->listOfflineRegions([&, completion](mbgl::expected<mbgl::OfflineRegions, std::exception_ptr> result) { NSError *error; NSMutableArray *packs; if (!result) { @@ -524,13 +530,13 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio - (void)setMaximumAllowedMapboxTiles:(uint64_t)maximumCount { MGLLogDebug(@"Setting maximumAllowedMapboxTiles: %lu", (unsigned long)maximumCount); - _mbglFileSource->setOfflineMapboxTileCountLimit(maximumCount); + _mbglDatabaseFileSource->setOfflineMapboxTileCountLimit(maximumCount); } #pragma mark - Ambient Cache management - (void)setMaximumAmbientCacheSize:(NSUInteger)cacheSize withCompletionHandler:(void (^)(NSError * _Nullable))completion { - _mbglFileSource->setMaximumAmbientCacheSize(cacheSize, [&, completion](std::exception_ptr exception) { + _mbglDatabaseFileSource->setMaximumAmbientCacheSize(cacheSize, [&, completion](std::exception_ptr exception) { NSError *error; if (completion) { if (exception) { @@ -549,7 +555,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)invalidateAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable))completion { - _mbglFileSource->invalidateAmbientCache([&, completion](std::exception_ptr exception){ + _mbglDatabaseFileSource->invalidateAmbientCache([&, completion](std::exception_ptr exception){ NSError *error; if (completion) { if (exception) { @@ -569,7 +575,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)clearAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable error))completion { - _mbglFileSource->clearAmbientCache([&, completion](std::exception_ptr exception){ + _mbglDatabaseFileSource->clearAmbientCache([&, completion](std::exception_ptr exception){ NSError *error; if (completion) { if (exception) { @@ -588,7 +594,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio } - (void)resetDatabaseWithCompletionHandler:(void (^)(NSError *_Nullable error))completion { - _mbglFileSource->resetDatabase([&, completion](std::exception_ptr exception) { + _mbglDatabaseFileSource->resetDatabase([&, completion](std::exception_ptr exception) { NSError *error; if (completion) { if (exception) { @@ -636,7 +642,7 @@ const MGLExceptionName MGLUnsupportedRegionTypeException = @"MGLUnsupportedRegio response.expires = mbgl::Timestamp() + std::chrono::duration_cast<mbgl::Seconds>(MGLDurationFromTimeInterval(expires.timeIntervalSince1970)); } - _mbglFileSource->put(resource, response); + _mbglDatabaseFileSource->put(resource, response); } - (void)putResourceWithUrl:(NSURL *)url data:(NSData *)data modified:(nullable NSDate *)modified expires:(nullable NSDate *)expires etag:(nullable NSString *)etag mustRevalidate:(BOOL)mustRevalidate { diff --git a/platform/darwin/src/MGLOfflineStorage_Private.h b/platform/darwin/src/MGLOfflineStorage_Private.h index 5ac64ea995..13bddd7999 100644 --- a/platform/darwin/src/MGLOfflineStorage_Private.h +++ b/platform/darwin/src/MGLOfflineStorage_Private.h @@ -2,7 +2,8 @@ #import "MGLOfflinePack.h" -#include <mbgl/storage/default_file_source.hpp> +#include <mbgl/storage/online_file_source.hpp> +#include <mbgl/storage/database_file_source.hpp> #include <memory> @@ -11,9 +12,19 @@ NS_ASSUME_NONNULL_BEGIN @interface MGLOfflineStorage (Private) /** - The shared file source object owned by the shared offline storage object. + The shared database file source object owned by the shared offline storage object. */ -@property (nonatomic) std::shared_ptr<mbgl::DefaultFileSource> mbglFileSource; +@property (nonatomic) std::shared_ptr<mbgl::DatabaseFileSource> mbglDatabaseFileSource; + +/** + The shared online file source object owned by the shared offline storage object. + */ +@property (nonatomic) std::shared_ptr<mbgl::OnlineFileSource> mbglOnlineFileSource; + +/** + The shared resource loader file source object owned by the shared offline storage object. + */ +@property (nonatomic) std::shared_ptr<mbgl::FileSource> mbglFileSource; /** The shared offline cache path. diff --git a/platform/darwin/src/http_file_source.mm b/platform/darwin/src/http_file_source.mm index 6f68111203..a72a97e299 100644 --- a/platform/darwin/src/http_file_source.mm +++ b/platform/darwin/src/http_file_source.mm @@ -4,6 +4,7 @@ #include <mbgl/util/http_header.hpp> #include <mbgl/util/async_task.hpp> +#include <mbgl/util/async_request.hpp> #include <mbgl/util/version.hpp> #import <Foundation/Foundation.h> diff --git a/platform/darwin/test/MGLOfflineStorageTests.mm b/platform/darwin/test/MGLOfflineStorageTests.mm index b44d4c51cd..8f6114658a 100644 --- a/platform/darwin/test/MGLOfflineStorageTests.mm +++ b/platform/darwin/test/MGLOfflineStorageTests.mm @@ -6,6 +6,7 @@ #import "NSDate+MGLAdditions.h" #import "MGLTestAssertionHandler.h" +#include <mbgl/storage/resource.hpp> #include <mbgl/util/run_loop.hpp> #pragma clang diagnostic ignored "-Wshadow" |