Skip to content

Commit 46d5ce4

Browse files
authored
[macOS] Add lookupKeyForAsset to FlutterPluginRegistrar (flutter#37421)
Fixes flutter#47681 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent a687d62 commit 46d5ce4

18 files changed

Lines changed: 253 additions & 109 deletions

ci/licenses_golden/licenses_flutter

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,11 +2464,14 @@ ORIGIN: ../../../flutter/shell/platform/darwin/common/command_line.mm + ../../..
24642464
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h + ../../../flutter/LICENSE
24652465
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h + ../../../flutter/LICENSE
24662466
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h + ../../../flutter/LICENSE
2467+
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterDartProject.h + ../../../flutter/LICENSE
24672468
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h + ../../../flutter/LICENSE
24682469
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h + ../../../flutter/LICENSE
24692470
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm + ../../../flutter/LICENSE
24702471
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m + ../../../flutter/LICENSE
24712472
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm + ../../../flutter/LICENSE
2473+
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.h + ../../../flutter/LICENSE
2474+
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.mm + ../../../flutter/LICENSE
24722475
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodec.mm + ../../../flutter/LICENSE
24732476
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.cc + ../../../flutter/LICENSE
24742477
ORIGIN: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.h + ../../../flutter/LICENSE
@@ -2482,7 +2485,6 @@ ORIGIN: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTex
24822485
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h + ../../../flutter/LICENSE
24832486
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h + ../../../flutter/LICENSE
24842487
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterCallbackCache.h + ../../../flutter/LICENSE
2485-
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterDartProject.h + ../../../flutter/LICENSE
24862488
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h + ../../../flutter/LICENSE
24872489
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h + ../../../flutter/LICENSE
24882490
ORIGIN: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadlessDartRunner.h + ../../../flutter/LICENSE
@@ -2618,7 +2620,6 @@ ORIGIN: ../../../flutter/shell/platform/darwin/ios/platform_view_ios.mm + ../../
26182620
ORIGIN: ../../../flutter/shell/platform/darwin/ios/rendering_api_selection.h + ../../../flutter/LICENSE
26192621
ORIGIN: ../../../flutter/shell/platform/darwin/ios/rendering_api_selection.mm + ../../../flutter/LICENSE
26202622
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterAppDelegate.h + ../../../flutter/LICENSE
2621-
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h + ../../../flutter/LICENSE
26222623
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h + ../../../flutter/LICENSE
26232624
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h + ../../../flutter/LICENSE
26242625
ORIGIN: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPlatformViews.h + ../../../flutter/LICENSE
@@ -5069,11 +5070,14 @@ FILE: ../../../flutter/shell/platform/darwin/common/command_line.mm
50695070
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h
50705071
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h
50715072
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h
5073+
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterDartProject.h
50725074
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h
50735075
FILE: ../../../flutter/shell/platform/darwin/common/framework/Headers/FlutterTexture.h
50745076
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannels.mm
50755077
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterChannelsTest.m
50765078
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterCodecs.mm
5079+
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.h
5080+
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.mm
50775081
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodec.mm
50785082
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.cc
50795083
FILE: ../../../flutter/shell/platform/darwin/common/framework/Source/FlutterStandardCodecHelper.h
@@ -5087,7 +5091,6 @@ FILE: ../../../flutter/shell/platform/darwin/graphics/FlutterDarwinExternalTextu
50875091
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/Flutter.h
50885092
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterAppDelegate.h
50895093
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterCallbackCache.h
5090-
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterDartProject.h
50915094
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngine.h
50925095
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterEngineGroup.h
50935096
FILE: ../../../flutter/shell/platform/darwin/ios/framework/Headers/FlutterHeadlessDartRunner.h
@@ -5225,7 +5228,6 @@ FILE: ../../../flutter/shell/platform/darwin/ios/platform_view_ios.mm
52255228
FILE: ../../../flutter/shell/platform/darwin/ios/rendering_api_selection.h
52265229
FILE: ../../../flutter/shell/platform/darwin/ios/rendering_api_selection.mm
52275230
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterAppDelegate.h
5228-
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterDartProject.h
52295231
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h
52305232
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterMacOS.h
52315233
FILE: ../../../flutter/shell/platform/darwin/macos/framework/Headers/FlutterPlatformViews.h

shell/platform/darwin/common/BUILD.gn

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,17 @@ source_set("framework_shared") {
3636
sources = [
3737
"framework/Source/FlutterChannels.mm",
3838
"framework/Source/FlutterCodecs.mm",
39+
"framework/Source/FlutterNSBundleUtils.h",
40+
"framework/Source/FlutterNSBundleUtils.mm",
3941
"framework/Source/FlutterStandardCodec.mm",
4042
"framework/Source/FlutterStandardCodecHelper.cc",
4143
"framework/Source/FlutterStandardCodec_Internal.h",
4244
]
4345

4446
public = framework_shared_headers
4547

48+
public += [ "framework/Source/FlutterNSBundleUtils.h" ]
49+
4650
defines = [ "FLUTTER_FRAMEWORK" ]
4751

4852
public_configs = [

shell/platform/darwin/ios/framework/Headers/FlutterDartProject.h renamed to shell/platform/darwin/common/framework/Headers/FlutterDartProject.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,57 @@
66
#define FLUTTER_FLUTTERDARTPROJECT_H_
77

88
#import <Foundation/Foundation.h>
9+
#import <TargetConditionals.h>
910

1011
#import "FlutterMacros.h"
1112

1213
NS_ASSUME_NONNULL_BEGIN
1314

1415
/**
1516
* A set of Flutter and Dart assets used by a `FlutterEngine` to initialize execution.
17+
*
1618
*/
1719
FLUTTER_DARWIN_EXPORT
1820
@interface FlutterDartProject : NSObject
1921

2022
/**
2123
* Initializes a Flutter Dart project from a bundle.
24+
*
25+
* The bundle must either contain a flutter_assets resource directory, or set the Info.plist key
26+
* FLTAssetsPath to override that name (if you are doing a custom build using a different name).
27+
*
28+
* @param bundle The bundle containing the Flutter assets directory. If nil, the App framework
29+
* created by Flutter will be used.
2230
*/
2331
- (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle NS_DESIGNATED_INITIALIZER;
24-
2532
/**
2633
* Unavailable - use `init` instead.
2734
*/
28-
- (instancetype)initFromDefaultSourceForConfiguration FLUTTER_UNAVAILABLE("Use -init instead.");
35+
- (instancetype)initFromDefaultSourceForConfiguration API_UNAVAILABLE(macos)
36+
FLUTTER_UNAVAILABLE("Use -init instead.");
37+
38+
/**
39+
* Returns the default identifier for the bundle where we expect to find the Flutter Dart
40+
* application.
41+
*/
42+
+ (NSString*)defaultBundleIdentifier;
43+
44+
/**
45+
* An NSArray of NSStrings to be passed as command line arguments to the Dart entrypoint.
46+
*
47+
* If this is not explicitly set, this will default to the contents of
48+
* [NSProcessInfo arguments], without the binary name.
49+
*
50+
* Set this to nil to pass no arguments to the Dart entrypoint.
51+
*/
52+
@property(nonatomic, nullable, copy)
53+
NSArray<NSString*>* dartEntrypointArguments API_UNAVAILABLE(ios);
2954

3055
/**
3156
* Returns the file name for the given asset. If the bundle with the identifier
3257
* "io.flutter.flutter.app" exists, it will try use that bundle; otherwise, it
3358
* will use the main bundle. To specify a different bundle, use
34-
* `-lookupKeyForAsset:asset:fromBundle`.
59+
* `+lookupKeyForAsset:fromBundle`.
3560
*
3661
* @param asset The name of the asset. The name can be hierarchical.
3762
* @return the file name to be used for lookup in the main bundle.
@@ -71,12 +96,6 @@ FLUTTER_DARWIN_EXPORT
7196
fromPackage:(NSString*)package
7297
fromBundle:(nullable NSBundle*)bundle;
7398

74-
/**
75-
* Returns the default identifier for the bundle where we expect to find the Flutter Dart
76-
* application.
77-
*/
78-
+ (NSString*)defaultBundleIdentifier;
79-
8099
@end
81100

82101
NS_ASSUME_NONNULL_END
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#ifndef SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERNSBUNDLEUTILS_H_
6+
#define SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERNSBUNDLEUTILS_H_
7+
8+
#import <Foundation/Foundation.h>
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
// Finds a bundle with the named `bundleID` within `searchURL`.
13+
//
14+
// Returns `nil` if the bundle cannot be found or if errors are encountered.
15+
NSBundle* FLTFrameworkBundleInternal(NSString* bundleID, NSURL* searchURL);
16+
17+
// Finds a bundle with the named `bundleID`.
18+
//
19+
// `+[NSBundle bundleWithIdentifier:]` is slow, and can take in the order of
20+
// tens of milliseconds in a minimal flutter app, and closer to 100 milliseconds
21+
// in a medium sized Flutter app on an iPhone 13. It is likely that the slowness
22+
// comes from having to traverse and load all bundles known to the process.
23+
// Using `+[NSBundle allframeworks]` and filtering also suffers from the same
24+
// problem.
25+
//
26+
// This implementation is an optimization to first limit the search space to
27+
// `+[NSBundle privateFrameworksURL]` of the main bundle, which is usually where
28+
// frameworks used by this file are placed. If the desired bundle cannot be
29+
// found here, the implementation falls back to
30+
// `+[NSBundle bundleWithIdentifier:]`.
31+
NSBundle* FLTFrameworkBundleWithIdentifier(NSString* bundleID);
32+
33+
NS_ASSUME_NONNULL_END
34+
35+
#endif // SHELL_PLATFORM_DARWIN_COMMON_FRAMEWORK_SOURCE_FLUTTERNSBUNDLEUTILS_H_
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include <Foundation/Foundation.h>
6+
7+
NSBundle* FLTFrameworkBundleInternal(NSString* bundleID, NSURL* searchURL) {
8+
NSDirectoryEnumerator<NSURL*>* frameworkEnumerator = [NSFileManager.defaultManager
9+
enumeratorAtURL:searchURL
10+
includingPropertiesForKeys:nil
11+
options:NSDirectoryEnumerationSkipsSubdirectoryDescendants |
12+
NSDirectoryEnumerationSkipsHiddenFiles
13+
// Skip directories where errors are encountered.
14+
errorHandler:nil];
15+
16+
for (NSURL* candidate in frameworkEnumerator) {
17+
NSBundle* bundle = [NSBundle bundleWithURL:candidate];
18+
if ([bundle.bundleIdentifier isEqualToString:bundleID]) {
19+
return bundle;
20+
}
21+
}
22+
return nil;
23+
}
24+
25+
NSBundle* FLTFrameworkBundleWithIdentifier(NSString* bundleID) {
26+
NSBundle* bundle = FLTFrameworkBundleInternal(bundleID, NSBundle.mainBundle.privateFrameworksURL);
27+
if (bundle != nil) {
28+
return bundle;
29+
}
30+
// Fallback to slow implementation.
31+
return [NSBundle bundleWithIdentifier:bundleID];
32+
}

shell/platform/darwin/common/framework_shared.gni

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ framework_shared_headers =
99
"framework/Headers/FlutterChannels.h",
1010
"framework/Headers/FlutterCodecs.h",
1111
"framework/Headers/FlutterTexture.h",
12+
"framework/Headers/FlutterDartProject.h",
1213
],
1314
"abspath")

shell/platform/darwin/ios/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ _flutter_framework_headers = [
2727
"framework/Headers/Flutter.h",
2828
"framework/Headers/FlutterAppDelegate.h",
2929
"framework/Headers/FlutterCallbackCache.h",
30-
"framework/Headers/FlutterDartProject.h",
3130
"framework/Headers/FlutterEngine.h",
3231
"framework/Headers/FlutterEngineGroup.h",
3332
"framework/Headers/FlutterHeadlessDartRunner.h",

shell/platform/darwin/ios/framework/Source/FlutterDartProject.mm

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -32,50 +32,6 @@
3232

3333
static const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin";
3434

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-
7935
flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle, NSProcessInfo* processInfoOrNil) {
8036
auto command_line = flutter::CommandLineFromNSProcessInfo(processInfoOrNil);
8137

@@ -286,6 +242,11 @@ @implementation FlutterDartProject {
286242
flutter::Settings _settings;
287243
}
288244

245+
// This property is marked unavailable on iOS in the common header.
246+
// That doesn't seem to be enough to prevent this property from being synthesized.
247+
// Mark dynamic to avoid warnings.
248+
@dynamic dartEntrypointArguments;
249+
289250
#pragma mark - Override base class designated initializers
290251

291252
- (instancetype)init {

shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
#include "flutter/common/settings.h"
99
#include "flutter/runtime/platform_data.h"
1010
#include "flutter/shell/common/engine.h"
11-
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterDartProject.h"
11+
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterDartProject.h"
12+
#import "flutter/shell/platform/darwin/common/framework/Source/FlutterNSBundleUtils.h"
1213

1314
NS_ASSUME_NONNULL_BEGIN
1415

15-
NSBundle* FLTFrameworkBundleInternal(NSString* bundleID, NSURL* searchURL);
16-
1716
flutter::Settings FLTDefaultSettingsForBundle(NSBundle* _Nullable bundle = nil,
1817
NSProcessInfo* _Nullable processInfoOrNil = nil);
1918

shell/platform/darwin/macos/BUILD.gn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ _framework_binary_subpath = "Versions/A/$_flutter_framework_name"
3838
# the Flutter engine source root.
3939
_flutter_framework_headers = [
4040
"framework/Headers/FlutterAppDelegate.h",
41-
"framework/Headers/FlutterDartProject.h",
4241
"framework/Headers/FlutterEngine.h",
4342
"framework/Headers/FlutterMacOS.h",
4443
"framework/Headers/FlutterPlatformViews.h",

0 commit comments

Comments
 (0)