diff options
author | Jesse Bounds <jesse@rebounds.net> | 2016-07-07 15:40:07 -0700 |
---|---|---|
committer | Jesse Bounds <jesse@rebounds.net> | 2016-07-07 15:40:07 -0700 |
commit | 23caa9f7aa9a128896afc0743482e015d2c60373 (patch) | |
tree | 0d927ad1f66351c52ad53fe295fe4bed855963a3 /platform/darwin/src | |
parent | 2e10c0a8660af9cdf6ff897aaa39e15fe62c6582 (diff) | |
parent | bb057409c728968cf37c9faca2c1eb4c589c5716 (diff) | |
download | qtlocation-mapboxgl-23caa9f7aa9a128896afc0743482e015d2c60373.tar.gz |
Merge branch 'release-ios-v3.3.0' into master
Diffstat (limited to 'platform/darwin/src')
-rw-r--r-- | platform/darwin/src/MGLOfflineStorage.mm | 122 | ||||
-rw-r--r-- | platform/darwin/src/NSData+MGLAdditions.h | 15 | ||||
-rw-r--r-- | platform/darwin/src/NSData+MGLAdditions.mm | 23 | ||||
-rw-r--r-- | platform/darwin/src/async_task.cpp | 2 |
4 files changed, 117 insertions, 45 deletions
diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index a59fb69943..dd15920eab 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -40,58 +40,92 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = @"MaximumCount"; return sharedOfflineStorage; } +/** + Returns the file URL to the offline cache, with the option to omit the private + subdirectory for legacy (v3.2.0 - v3.2.3) migration purposes. + + The cache is located in a directory specific to the application, so that packs + downloaded by other applications don’t count toward this application’s limits. + + The cache is located at: + ~/Library/Application Support/tld.app.bundle.id/.mapbox/cache.db + + The subdirectory-less cache was located at: + ~/Library/Application Support/tld.app.bundle.id/cache.db + */ ++ (NSURL *)cacheURLIncludingSubdirectory:(BOOL)useSubdirectory { + NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil + create:YES + error:nil]; + NSString *bundleIdentifier = [NSBundle mainBundle].bundleIdentifier; + if (!bundleIdentifier) { + // There’s no main bundle identifier when running in a unit test bundle. + bundleIdentifier = [NSBundle bundleForClass:self].bundleIdentifier; + } + cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier]; + if (useSubdirectory) { + cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:@".mapbox"]; + } + [[NSFileManager defaultManager] createDirectoryAtURL:cacheDirectoryURL + withIntermediateDirectories:YES + attributes:nil + error:nil]; + if (useSubdirectory) { + // Avoid backing up the offline cache onto iCloud, because it can be + // redownloaded. Ideally, we’d even put the ambient cache in Caches, so + // it can be reclaimed by the system when disk space runs low. But + // unfortunately it has to live in the same file as offline resources. + [cacheDirectoryURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:NULL]; + } + return [cacheDirectoryURL URLByAppendingPathComponent:MGLOfflineStorageFileName]; +} + +/** + Returns the absolute path to the location where v3.2.0-beta.1 placed the + offline cache. + */ ++ (NSString *)legacyCachePath { +#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR + // ~/Documents/offline.db + NSArray *legacyPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); + NSString *legacyCachePath = [legacyPaths.firstObject stringByAppendingPathComponent:MGLOfflineStorageFileName3_2_0_beta_1]; +#elif TARGET_OS_MAC + // ~/Library/Caches/tld.app.bundle.id/offline.db + NSString *bundleIdentifier = [NSBundle mainBundle].bundleIdentifier; + NSURL *legacyCacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory + inDomain:NSUserDomainMask + appropriateForURL:nil + create:NO + error:nil]; + legacyCacheDirectoryURL = [legacyCacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier]; + NSURL *legacyCacheURL = [legacyCacheDirectoryURL URLByAppendingPathComponent:MGLOfflineStorageFileName3_2_0_beta_1]; + NSString *legacyCachePath = legacyCacheURL ? legacyCacheURL.path : @""; +#endif + return legacyCachePath; +} + - (instancetype)init { if (self = [super init]) { - // Place the cache in a location specific to the application, so that - // packs downloaded by other applications don’t count toward this - // application’s limits. - // ~/Library/Application Support/tld.app.bundle.id/cache.db - NSURL *cacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSApplicationSupportDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:YES - error:nil]; - NSString *bundleIdentifier = [NSBundle mainBundle].bundleIdentifier; - if (!bundleIdentifier) { - // There’s no main bundle identifier when running in a unit test bundle. - bundleIdentifier = [NSBundle bundleForClass:[self class]].bundleIdentifier; - } - cacheDirectoryURL = [cacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier]; - [[NSFileManager defaultManager] createDirectoryAtURL:cacheDirectoryURL - withIntermediateDirectories:YES - attributes:nil - error:nil]; - NSURL *cacheURL = [cacheDirectoryURL URLByAppendingPathComponent:MGLOfflineStorageFileName]; - NSString *cachePath = cacheURL ? cacheURL.path : @""; - + NSURL *cacheURL = [[self class] cacheURLIncludingSubdirectory:YES]; + NSString *cachePath = cacheURL.path ?: @""; + // Move the offline cache from v3.2.0-beta.1 to a location that can also // be used for ambient caching. -#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR - // ~/Documents/offline.db - NSArray *legacyPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - NSString *legacyCachePath = [legacyPaths.firstObject stringByAppendingPathComponent:MGLOfflineStorageFileName3_2_0_beta_1]; -#elif TARGET_OS_MAC - // ~/Library/Caches/tld.app.bundle.id/offline.db - NSURL *legacyCacheDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSCachesDirectory - inDomain:NSUserDomainMask - appropriateForURL:nil - create:NO - error:nil]; - legacyCacheDirectoryURL = [legacyCacheDirectoryURL URLByAppendingPathComponent:bundleIdentifier]; - NSURL *legacyCacheURL = [legacyCacheDirectoryURL URLByAppendingPathComponent:MGLOfflineStorageFileName3_2_0_beta_1]; - NSString *legacyCachePath = legacyCacheURL ? legacyCacheURL.path : @""; -#endif if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath]) { + NSString *legacyCachePath = [[self class] legacyCachePath]; [[NSFileManager defaultManager] moveItemAtPath:legacyCachePath toPath:cachePath error:NULL]; } - - _mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String); - // Avoid backing up the offline cache onto iCloud, because it can be - // redownloaded. Ideally, we’d even put the ambient cache in Caches, so - // it can be reclaimed by the system when disk space runs low. But - // unfortunately it has to live in the same file as offline resources. - [cacheURL setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:NULL]; + // Move the offline file cache from v3.2.x path to a subdirectory that + // can be reliably excluded from backups. + if (![[NSFileManager defaultManager] fileExistsAtPath:cachePath]) { + NSURL *subdirectorylessCacheURL = [[self class] cacheURLIncludingSubdirectory:NO]; + [[NSFileManager defaultManager] moveItemAtPath:subdirectorylessCacheURL.path toPath:cachePath error:NULL]; + } + + _mbglFileSource = new mbgl::DefaultFileSource(cachePath.UTF8String, [NSBundle mainBundle].resourceURL.path.UTF8String); // Observe for changes to the global access token (and find out the current one). [[MGLAccountManager sharedManager] addObserver:self diff --git a/platform/darwin/src/NSData+MGLAdditions.h b/platform/darwin/src/NSData+MGLAdditions.h new file mode 100644 index 0000000000..0c68c81f45 --- /dev/null +++ b/platform/darwin/src/NSData+MGLAdditions.h @@ -0,0 +1,15 @@ +#import <Foundation/Foundation.h> + +#import "MGLTypes.h" + +NS_ASSUME_NONNULL_BEGIN + +@interface NSData (MGLAdditions) + +- (NSData *)mgl_compressedData; + +- (NSData *)mgl_decompressedData; + +@end + +NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/NSData+MGLAdditions.mm b/platform/darwin/src/NSData+MGLAdditions.mm new file mode 100644 index 0000000000..ef171c5e1e --- /dev/null +++ b/platform/darwin/src/NSData+MGLAdditions.mm @@ -0,0 +1,23 @@ +#import "NSData+MGLAdditions.h" + +#include <mbgl/util/compression.hpp> + +@implementation NSData (MGLAdditions) + +- (NSData *)mgl_compressedData +{ + std::string string(static_cast<const char*>(self.bytes), self.length); + std::string compressed_string = mbgl::util::compress(string); + + return [NSData dataWithBytes:&compressed_string[0] length:compressed_string.length()]; +} + +- (NSData *)mgl_decompressedData +{ + std::string string(static_cast<const char*>(self.bytes), self.length); + std::string decompressed_string = mbgl::util::decompress(string); + + return [NSData dataWithBytes:&decompressed_string[0] length:decompressed_string.length()]; +} + +@end diff --git a/platform/darwin/src/async_task.cpp b/platform/darwin/src/async_task.cpp index 513629726b..48457d24a8 100644 --- a/platform/darwin/src/async_task.cpp +++ b/platform/darwin/src/async_task.cpp @@ -25,7 +25,7 @@ public: perform }; source = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &context); - CFRunLoopAddSource(loop, source, kCFRunLoopDefaultMode); + CFRunLoopAddSource(loop, source, kCFRunLoopCommonModes); } ~Impl() { |