|
32 | 32 |
|
33 | 33 | static const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin"; |
34 | 34 |
|
| 35 | +// Finds a bundle with the named `bundleID` within `searchURL`. |
| 36 | +// |
| 37 | +// Returns `nil` if the bundle cannot be found or if errors are encountered. |
| 38 | +NSBundle* FLTFrameworkBundleInternal(NSString* bundleID, NSURL* searchURL) { |
| 39 | + NSDirectoryEnumerator<NSURL*>* frameworkEnumerator = [NSFileManager.defaultManager |
| 40 | + enumeratorAtURL:searchURL |
| 41 | + includingPropertiesForKeys:nil |
| 42 | + options:NSDirectoryEnumerationSkipsSubdirectoryDescendants | |
| 43 | + NSDirectoryEnumerationSkipsHiddenFiles |
| 44 | + // Skip directories where errors are encountered. |
| 45 | + errorHandler:nil]; |
| 46 | + |
| 47 | + for (NSURL* candidate in frameworkEnumerator) { |
| 48 | + NSBundle* bundle = [NSBundle bundleWithURL:candidate]; |
| 49 | + if ([bundle.bundleIdentifier isEqualToString:bundleID]) { |
| 50 | + return bundle; |
| 51 | + } |
| 52 | + } |
| 53 | + return nil; |
| 54 | +} |
| 55 | + |
| 56 | +// Finds a bundle with the named `bundleID`. |
| 57 | +// |
| 58 | +// `+[NSBundle bundleWithIdentifier:]` is slow, and can take in the order of |
| 59 | +// tens of milliseconds in a minimal flutter app, and closer to 100 milliseconds |
| 60 | +// in a medium sized Flutter app on an iPhone 13. It is likely that the slowness |
| 61 | +// comes from having to traverse and load all bundles known to the process. |
| 62 | +// Using `+[NSBundle allframeworks]` and filtering also suffers from the same |
| 63 | +// problem. |
| 64 | +// |
| 65 | +// This implementation is an optimization to first limit the search space to |
| 66 | +// `+[NSBundle privateFrameworksURL]` of the main bundle, which is usually where |
| 67 | +// frameworks used by this file are placed. If the desired bundle cannot be |
| 68 | +// found here, the implementation falls back to |
| 69 | +// `+[NSBundle bundleWithIdentifier:]`. |
| 70 | +NS_INLINE NSBundle* FLTFrameworkBundleWithIdentifier(NSString* bundleID) { |
| 71 | + NSBundle* bundle = FLTFrameworkBundleInternal(bundleID, NSBundle.mainBundle.privateFrameworksURL); |
| 72 | + if (bundle != nil) { |
| 73 | + return bundle; |
| 74 | + } |
| 75 | + // Fallback to slow implementation. |
| 76 | + return [NSBundle bundleWithIdentifier:bundleID]; |
| 77 | +} |
| 78 | + |
35 | 79 | flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle) { |
36 | 80 | auto command_line = flutter::CommandLineFromNSProcessInfo(); |
37 | 81 |
|
|
46 | 90 |
|
47 | 91 | bool hasExplicitBundle = bundle != nil; |
48 | 92 | if (bundle == nil) { |
49 | | - bundle = [NSBundle bundleWithIdentifier:[FlutterDartProject defaultBundleIdentifier]]; |
| 93 | + bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]); |
50 | 94 | } |
51 | 95 | if (bundle == nil) { |
52 | 96 | bundle = mainBundle; |
@@ -317,7 +361,7 @@ - (instancetype)initWithSettings:(const flutter::Settings&)settings { |
317 | 361 |
|
318 | 362 | + (NSString*)flutterAssetsName:(NSBundle*)bundle { |
319 | 363 | if (bundle == nil) { |
320 | | - bundle = [NSBundle bundleWithIdentifier:[FlutterDartProject defaultBundleIdentifier]]; |
| 364 | + bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]); |
321 | 365 | } |
322 | 366 | if (bundle == nil) { |
323 | 367 | bundle = [NSBundle mainBundle]; |
|
0 commit comments