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

Commit

Permalink
[darwin] Use new FileSourceManager interface
Browse files Browse the repository at this point in the history
  • Loading branch information
alexshalamov committed Oct 14, 2019
1 parent bb2c870 commit d8eb4e7
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 72 deletions.
1 change: 0 additions & 1 deletion platform/darwin/filesource-files.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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"
],
Expand Down
1 change: 0 additions & 1 deletion platform/darwin/src/MGLMapSnapshotter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 8 additions & 8 deletions platform/darwin/src/MGLOfflinePack.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -59,7 +59,7 @@ @interface MGLOfflinePack ()

@implementation MGLOfflinePack {
BOOL _isSuspending;
std::shared_ptr<mbgl::DefaultFileSource> _mbglFileSource;
std::shared_ptr<mbgl::DatabaseFileSource> _mbglDatabaseFileSource;
}

- (instancetype)init {
Expand All @@ -76,8 +76,8 @@ - (instancetype)initWithMBGLRegion:(mbgl::OfflineRegion *)region {
_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;
}
Expand Down Expand Up @@ -117,7 +117,7 @@ - (void)resume {

self.state = MGLOfflinePackStateActive;

_mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active);
_mbglDatabaseFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Active);
}

- (void)suspend {
Expand All @@ -129,7 +129,7 @@ - (void)suspend {
_isSuspending = YES;
}

_mbglFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive);
_mbglDatabaseFileSource->setOfflineRegionDownloadState(*_mbglOfflineRegion, mbgl::OfflineRegionDownloadState::Inactive);
}

- (void)invalidate {
Expand All @@ -140,7 +140,7 @@ - (void)invalidate {
@synchronized (self) {
self.state = MGLOfflinePackStateInvalid;
if (self.mbglOfflineRegion) {
_mbglFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr);
_mbglDatabaseFileSource->setOfflineRegionObserver(*self.mbglOfflineRegion, nullptr);
}
self.mbglOfflineRegion = nil;
}
Expand Down Expand Up @@ -169,7 +169,7 @@ - (void)requestProgress {
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(), ^{
Expand Down
50 changes: 28 additions & 22 deletions platform/darwin/src/MGLOfflineStorage.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -45,13 +45,15 @@
@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 {
Expand Down Expand Up @@ -91,7 +93,7 @@ - (void)setDelegate:(id<MGLOfflineStorageDelegate>)newValue {
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()
Expand Down Expand Up @@ -127,13 +129,15 @@ - (void)setDelegate:(id<MGLOfflineStorageDelegate>)newValue {
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({});
}
}

Expand Down Expand Up @@ -228,7 +232,9 @@ - (instancetype)init {
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
Expand Down Expand Up @@ -262,14 +268,14 @@ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(N
if ([keyPath isEqualToString:@"accessToken"] && object == [MGLAccountManager sharedManager]) {
NSString *accessToken = change[NSKeyValueChangeNewKey];
if (![accessToken isKindOfClass:[NSNull class]]) {
_mbglFileSource->setAccessToken(accessToken.UTF8String);
_mbglOnlineFileSource->setProperty("access-token", 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("api-base-url", mbgl::util::API_BASE_URL);
} else {
_mbglFileSource->setAPIBaseURL(apiBaseURL.absoluteString.UTF8String);
_mbglFileSource->setProperty("api-base-url", apiBaseURL.absoluteString.UTF8String);
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
Expand Down Expand Up @@ -335,7 +341,7 @@ - (void)addContentsOfURL:(NSURL *)fileURL withCompletionHandler:(MGLBatchedOffli
}

- (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) {
Expand Down Expand Up @@ -400,7 +406,7 @@ - (void)_addPackForRegion:(id <MGLOfflineRegion>)region withContext:(NSData *)co
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());
Expand Down Expand Up @@ -441,7 +447,7 @@ - (void)_removePack:(MGLOfflinePack *)pack withCompletionHandler:(MGLOfflinePack
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:@{
Expand All @@ -468,7 +474,7 @@ - (void)invalidatePack:(MGLOfflinePack *)pack withCompletionHandler:(void (^)(NS
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()),
Expand Down Expand Up @@ -496,7 +502,7 @@ - (void)reloadPacks {
}

- (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) {
Expand Down Expand Up @@ -524,13 +530,13 @@ - (void)getPacksWithCompletionHandler:(void (^)(NSArray<MGLOfflinePack *> *packs

- (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) {
Expand All @@ -549,7 +555,7 @@ - (void)setMaximumAmbientCacheSize:(NSUInteger)cacheSize withCompletionHandler:(
}

- (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) {
Expand All @@ -569,7 +575,7 @@ - (void)invalidateAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable
}

- (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) {
Expand All @@ -588,7 +594,7 @@ - (void)clearAmbientCacheWithCompletionHandler:(void (^)(NSError *_Nullable erro
}

- (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) {
Expand Down Expand Up @@ -636,7 +642,7 @@ - (void)preloadData:(NSData *)data forURL:(NSURL *)url modificationDate:(nullabl
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 {
Expand Down
17 changes: 14 additions & 3 deletions platform/darwin/src/MGLOfflineStorage_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>

Expand All @@ -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.
Expand Down
1 change: 1 addition & 0 deletions platform/darwin/src/http_file_source.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions platform/darwin/test/MGLOfflineStorageTests.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
37 changes: 0 additions & 37 deletions src/mbgl/storage/file_source.cpp

This file was deleted.

0 comments on commit d8eb4e7

Please sign in to comment.