From bc8e93271b13766468fe5eb7609421d2c08baac0 Mon Sep 17 00:00:00 2001 From: Chris Yang Date: Wed, 20 Sep 2023 12:55:47 -0700 Subject: [PATCH] [ios] fix asset url not found when loading app extension (flutter/engine#46073) In https://github.com/flutter/engine/commit/b3fc48b91b692deb202bd1445ba3fb249ad4147d, I refactored the assetsPath to use NSURL. It turns out that when the app bundle is not loaded (during launching app exgtension), the assetURL will return nil using the `URLForResource`, but the `pathForResource` successfully returns the raw path. This PR reverts back to the raw path solution. part of https://github.com/flutter/flutter/issues/124287 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../framework/Source/FlutterNSBundleUtils.h | 2 +- .../framework/Source/FlutterNSBundleUtils.mm | 14 +++++---- .../framework/Source/FlutterDartProject.mm | 9 +++--- .../Source/FlutterDartProjectTest.mm | 29 +++++++------------ 4 files changed, 25 insertions(+), 29 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.h b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.h index e9c9f323cc..9e0ea22e5f 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.h +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.h @@ -48,7 +48,7 @@ NSString* FLTAssetPath(NSBundle* bundle); // If the key is not set, `flutter_assets` is used as the raw path value. // // If no valid asset is found under the raw path, returns nil. -NSURL* FLTAssetsURLFromBundle(NSBundle* bundle); +NSString* FLTAssetsPathFromBundle(NSBundle* bundle); NS_ASSUME_NONNULL_END diff --git a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.mm b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.mm index bf0002d9ad..4198fd33e0 100644 --- a/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.mm +++ b/engine/src/flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.mm @@ -54,15 +54,17 @@ NSBundle* FLTFrameworkBundleWithIdentifier(NSString* flutterFrameworkBundleID) { } NSString* FLTAssetPath(NSBundle* bundle) { - return [bundle objectForInfoDictionaryKey:@"FLTAssetsPath"] ?: kDefaultAssetPath; + return [bundle objectForInfoDictionaryKey:@"FLTAssetsPath"] ?: @"flutter_assets"; } -NSURL* FLTAssetsURLFromBundle(NSBundle* bundle) { +NSString* FLTAssetsPathFromBundle(NSBundle* bundle) { NSString* flutterAssetsPath = FLTAssetPath(bundle); - NSURL* assets = [bundle URLForResource:flutterAssetsPath withExtension:nil]; + // Use the raw path solution so that asset path can be returned from unloaded bundles. + // See https://github.com/flutter/engine/pull/46073 + NSString* assetsPath = [bundle pathForResource:flutterAssetsPath ofType:@""]; - if (!assets) { - assets = [[NSBundle mainBundle] URLForResource:flutterAssetsPath withExtension:nil]; + if (assetsPath.length == 0) { + assetsPath = [[NSBundle mainBundle] pathForResource:flutterAssetsPath ofType:@""]; } - return assets; + return assetsPath; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm index 04bf8b9b33..70c1ac7a47 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm @@ -139,19 +139,20 @@ flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle, NSProcessInfo* p // Checks to see if the flutter assets directory is already present. if (settings.assets_path.empty()) { - NSURL* assetsURL = FLTAssetsURLFromBundle(bundle); + NSString* assetsPath = FLTAssetsPathFromBundle(bundle); - if (!assetsURL) { + if (assetsPath.length == 0) { NSLog(@"Failed to find assets path for \"%@\"", bundle); } else { - settings.assets_path = assetsURL.path.UTF8String; + settings.assets_path = assetsPath.UTF8String; // Check if there is an application kernel snapshot in the assets directory we could // potentially use. Looking for the snapshot makes sense only if we have a VM that can use // it. if (!flutter::DartVM::IsRunningPrecompiledCode()) { NSURL* applicationKernelSnapshotURL = - [assetsURL URLByAppendingPathComponent:@(kApplicationKernelSnapshotFileName)]; + [NSURL URLWithString:@(kApplicationKernelSnapshotFileName) + relativeToURL:[NSURL fileURLWithPath:assetsPath]]; NSError* error; if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) { settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm index a28aaf8bee..19774d21bc 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProjectTest.mm @@ -89,31 +89,24 @@ FLUTTER_ASSERT_ARC - (void)testFLTAssetsURLFromBundle { { - // Found asset path in info.plist (even not reachable) + // Found asset path in info.plist id mockBundle = OCMClassMock([NSBundle class]); OCMStub([mockBundle objectForInfoDictionaryKey:@"FLTAssetsPath"]).andReturn(@"foo/assets"); - NSURL* mockAssetsURL = OCMClassMock([NSURL class]); - OCMStub([mockBundle URLForResource:@"foo/assets" withExtension:nil]).andReturn(mockAssetsURL); - OCMStub([mockAssetsURL checkResourceIsReachableAndReturnError:NULL]).andReturn(NO); - OCMStub([mockAssetsURL path]).andReturn(@"foo/assets"); - NSURL* url = FLTAssetsURLFromBundle(mockBundle); - XCTAssertEqualObjects(url.path, @"foo/assets"); + NSString* resultAssetsPath = @"path/to/foo/assets"; + OCMStub([mockBundle pathForResource:@"foo/assets" ofType:@""]).andReturn(resultAssetsPath); + NSString* path = FLTAssetsPathFromBundle(mockBundle); + XCTAssertEqualObjects(path, @"path/to/foo/assets"); } { // No asset path in info.plist, defaults to main bundle id mockBundle = OCMClassMock([NSBundle class]); id mockMainBundle = OCMPartialMock([NSBundle mainBundle]); - NSURL* mockAssetsURL = OCMClassMock([NSURL class]); - OCMStub([mockBundle URLForResource:@"Frameworks/App.framework/flutter_assets" - withExtension:nil]) - .andReturn(nil); - OCMStub([mockAssetsURL checkResourceIsReachableAndReturnError:NULL]).andReturn(NO); - OCMStub([mockAssetsURL path]).andReturn(@"path/to/foo/assets"); - OCMStub([mockMainBundle URLForResource:@"Frameworks/App.framework/flutter_assets" - withExtension:nil]) - .andReturn(mockAssetsURL); - NSURL* url = FLTAssetsURLFromBundle(mockBundle); - XCTAssertEqualObjects(url.path, @"path/to/foo/assets"); + NSString* resultAssetsPath = @"path/to/foo/assets"; + OCMStub([mockBundle pathForResource:@"flutter_assets" ofType:@""]).andReturn(nil); + OCMStub([mockMainBundle pathForResource:@"flutter_assets" ofType:@""]) + .andReturn(resultAssetsPath); + NSString* path = FLTAssetsPathFromBundle(mockBundle); + XCTAssertEqualObjects(path, @"path/to/foo/assets"); } }