diff --git a/platform/darwin/src/MGLOfflineStorage.h b/platform/darwin/src/MGLOfflineStorage.h index 2d376c922..16f134adb 100644 --- a/platform/darwin/src/MGLOfflineStorage.h +++ b/platform/darwin/src/MGLOfflineStorage.h @@ -7,6 +7,7 @@ NS_ASSUME_NONNULL_BEGIN @class MGLOfflinePack; @protocol MGLOfflineRegion; +@protocol MGLOfflineStorageDelegate; /** Posted by the shared `MGLOfflineStorage` object when an `MGLOfflinePack` @@ -131,6 +132,30 @@ typedef void (^MGLOfflinePackAdditionCompletionHandler)(MGLOfflinePack * _Nullab */ typedef void (^MGLOfflinePackRemovalCompletionHandler)(NSError * _Nullable error); +/** + The type of resource that is requested. + */ +typedef NS_ENUM(NSUInteger, MGLResourceKind) { + /** Unknown type */ + MGLResourceKindUnknown, + /** Style sheet JSON file */ + MGLResourceKindStyle, + /** TileJSON file as specified in https://www.mapbox.com/mapbox-gl-js/style-spec/#root-sources */ + MGLResourceKindSource, + /** A vector or raster tile as described in the style sheet at + https://www.mapbox.com/mapbox-gl-js/style-spec/#sources */ + MGLResourceKindTile, + /** Signed distance field glyphs for text rendering. These are the URLs specified in the style + in https://www.mapbox.com/mapbox-gl-js/style-spec/#root-glyphs */ + MGLResourceKindGlyphs, + /** Image part of a sprite sheet. It is constructed of the prefix in + https://www.mapbox.com/mapbox-gl-js/style-spec/#root-sprite and a PNG file extension. */ + MGLResourceKindSpriteImage, + /** JSON part of a sprite sheet. It is constructed of the prefix in + https://www.mapbox.com/mapbox-gl-js/style-spec/#root-sprite and a JSON file extension. */ + MGLResourceKindSpriteJSON, +}; + /** MGLOfflineStorage implements a singleton (shared object) that manages offline packs. All of this class’s instance methods are asynchronous, reflecting the @@ -145,6 +170,20 @@ MGL_EXPORT */ + (instancetype)sharedOfflineStorage; +#pragma mark - Accessing the Delegate + +/** + The receiver’s delegate. + + An offline storage object sends messages to its delegate to allow it to + transform URLs before they are requested from the internet. This can be used + add or remove custom parameters, or reroute certain requests to other servers + or endpoints. + */ +@property(nonatomic, weak, nullable) IBOutlet id delegate; + +#pragma mark - Managing Offline Packs + /** An array of all known offline packs, in the order in which they were created. @@ -250,4 +289,25 @@ MGL_EXPORT @end +/** + The `MGLOfflineStorageDelegate` protocol defines methods that a delegate of an + `MGLOfflineStorage` object can optionally implement to transform various types + of URLs before downloading them via the internet. + */ +@protocol MGLOfflineStorageDelegate + +/** + Sent whenever a URL needs to be transformed. + + @param storage The storage object processing the download. + @param kind The kind of URL to be transformed. + @param url The original URL to be transformed. + @return A URL that will now be downloaded. + */ +- (NSURL *)offlineStorage:(MGLOfflineStorage *)storage + URLForResourceOfKind:(MGLResourceKind)kind + withURL:(NSURL *)url; + +@end + NS_ASSUME_NONNULL_END diff --git a/platform/darwin/src/MGLOfflineStorage.mm b/platform/darwin/src/MGLOfflineStorage.mm index af0f59290..64f9111f1 100644 --- a/platform/darwin/src/MGLOfflineStorage.mm +++ b/platform/darwin/src/MGLOfflineStorage.mm @@ -42,9 +42,53 @@ NSString * const MGLOfflinePackMaximumCountUserInfoKey = MGLOfflinePackUserInfoK sharedOfflineStorage = [[self alloc] init]; [sharedOfflineStorage reloadPacks]; }); + return sharedOfflineStorage; } +- (void)setDelegate:(id)newValue { + _delegate = newValue; + if ([self.delegate respondsToSelector:@selector(offlineStorage:URLForResourceOfKind:withURL:)]) { + _mbglFileSource->setResourceTransform([offlineStorage = self](auto kind_, std::string&& url_) -> std::string { + NSURL* url = + [NSURL URLWithString:[[NSString alloc] initWithBytes:url_.data() + length:url_.length() + encoding:NSUTF8StringEncoding]]; + MGLResourceKind kind = MGLResourceKindUnknown; + switch (kind_) { + case mbgl::Resource::Kind::Tile: + kind = MGLResourceKindTile; + break; + case mbgl::Resource::Kind::Glyphs: + kind = MGLResourceKindGlyphs; + break; + case mbgl::Resource::Kind::Style: + kind = MGLResourceKindStyle; + break; + case mbgl::Resource::Kind::Source: + kind = MGLResourceKindSource; + break; + case mbgl::Resource::Kind::SpriteImage: + kind = MGLResourceKindSpriteImage; + break; + case mbgl::Resource::Kind::SpriteJSON: + kind = MGLResourceKindSpriteJSON; + break; + case mbgl::Resource::Kind::Unknown: + kind = MGLResourceKindUnknown; + break; + + } + url = [offlineStorage.delegate offlineStorage:offlineStorage + URLForResourceOfKind:kind + withURL:url]; + return url.absoluteString.UTF8String; + }); + } else { + _mbglFileSource->setResourceTransform(nullptr); + } +} + /** 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. diff --git a/platform/ios/CHANGELOG.md b/platform/ios/CHANGELOG.md index cd7b44165..d20757644 100644 --- a/platform/ios/CHANGELOG.md +++ b/platform/ios/CHANGELOG.md @@ -40,6 +40,7 @@ Mapbox welcomes participation and contributions from everyone. Please read [CONT * Fixed a memory leak in MGLMapView. ([#7956](https://github.com/mapbox/mapbox-gl-native/pull/7956)) * Fixed an issue that could prevent a cached style from appearing while the device is offline. ([#7770](https://github.com/mapbox/mapbox-gl-native/pull/7770)) * Fixed an issue that could prevent a style from loading when reestablishing a network connection. ([#7902](https://github.com/mapbox/mapbox-gl-native/pull/7902)) +* `MGLOfflineStorage` instances now support a delegate conforming to `MGLOfflineStorageDelegate`, which allows altering URLs before they are requested from the internet. ([#8084](https://github.com/mapbox/mapbox-gl-native/pull/8084)) ### Other changes diff --git a/platform/macos/CHANGELOG.md b/platform/macos/CHANGELOG.md index b00b0cee4..bc0b9a829 100644 --- a/platform/macos/CHANGELOG.md +++ b/platform/macos/CHANGELOG.md @@ -39,6 +39,7 @@ * Fixed a memory leak in MGLMapView. ([#7956](https://github.com/mapbox/mapbox-gl-native/pull/7956)) * Fixed an issue that could prevent a cached style from appearing while the computer is offline. ([#7770](https://github.com/mapbox/mapbox-gl-native/pull/7770)) * Fixed an issue that could prevent a style from loading when reestablishing a network connection. ([#7902](https://github.com/mapbox/mapbox-gl-native/pull/7902)) +* `MGLOfflineStorage` instances now support a delegate conforming to `MGLOfflineStorageDelegate`, which allows altering URLs before they are requested from the internet. ([#8084](https://github.com/mapbox/mapbox-gl-native/pull/8084)) ### Other changes