| Flutter macOS Embedder |

FlutterDartProject.mm
Go to the documentation of this file.
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 #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterDartProject.h"
6 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
7
8 #include <vector>
9
10 #include "flutter/shell/platform/common/engine_switches.h"
11
12 static NSString* const kICUBundlePath = @"icudtl.dat";
13 static NSString* const kAppBundleIdentifier = @"io.flutter.flutter.app";
14
15 #pragma mark - Private interface declaration.
16 @interface FlutterDartProject ()
17 /**
18 Get the Flutter assets name path by pass the bundle. If bundle is nil, we use the main bundle as
19 default.
20 */
21 + (NSString*)flutterAssetsNameWithBundle:(NSBundle*)bundle;
22 @end
23
24 @implementation FlutterDartProject {
25 NSBundle* _dartBundle;
26 NSString* _assetsPath;
27 NSString* _ICUDataPath;
28 }
29
30 - (instancetype)init {
31 return [self initWithPrecompiledDartBundle:nil];
32 }
33
34 - (instancetype)initWithPrecompiledDartBundle:(NSBundle*)bundle {
35 self = [super init];
36 NSAssert(self, @"Super init cannot be nil");
37
38 _dartBundle = bundle ?: FLTFrameworkBundleWithIdentifier(kAppBundleIdentifier);
39 if (_dartBundle == nil) {
40 // The bundle isn't loaded and can't be found by bundle ID. Find it by path.
41 _dartBundle = [NSBundle bundleWithURL:[NSBundle.mainBundle.privateFrameworksURL
42 URLByAppendingPathComponent:@"App.framework"]];
43 }
44 if (!_dartBundle.isLoaded) {
45 [_dartBundle load];
46 }
47 _dartEntrypointArguments = [[NSProcessInfo processInfo] arguments];
48 // Remove the first element as it's the binary name
49 _dartEntrypointArguments = [_dartEntrypointArguments
50 subarrayWithRange:NSMakeRange(1, _dartEntrypointArguments.count - 1)];
51 return self;
52 }
53
54 - (instancetype)initWithAssetsPath:(NSString*)assets ICUDataPath:(NSString*)icuPath {
55 self = [super init];
56 NSAssert(self, @"Super init cannot be nil");
57 _assetsPath = assets;
58 _ICUDataPath = icuPath;
59 return self;
60 }
61
62 - (BOOL)enableImpeller {
63 NSNumber* enableImpeller =
64 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FLTEnableImpeller"];
65 if (enableImpeller != nil) {
66 return enableImpeller.boolValue;
67 }
68 return NO;
69 }
70
71 - (NSString*)assetsPath {
72 if (_assetsPath) {
73 return _assetsPath;
74 }
75
76 // If there's no App.framework, fall back to checking the main bundle for assets.
77 NSBundle* assetBundle = _dartBundle ?: [NSBundle mainBundle];
78 NSString* flutterAssetsName = [assetBundle objectForInfoDictionaryKey:@"FLTAssetsPath"];
79 if (flutterAssetsName == nil) {
80 flutterAssetsName = @"flutter_assets";
81 }
82 NSString* path = [assetBundle pathForResource:flutterAssetsName ofType:@""];
83 if (!path) {
84 NSLog(@"Failed to find path for \"%@\"", flutterAssetsName);
85 }
86 return path;
87 }
88
89 - (NSString*)ICUDataPath {
90 if (_ICUDataPath) {
91 return _ICUDataPath;
92 }
93
94 NSString* path = [[NSBundle bundleForClass:[self class]] pathForResource:kICUBundlePath
95 ofType:nil];
96 if (!path) {
97 NSLog(@"Failed to find path for \"%@\"", kICUBundlePath);
98 }
99 return path;
100 }
101
102 + (NSString*)flutterAssetsNameWithBundle:(NSBundle*)bundle {
103 if (bundle == nil) {
104 bundle = FLTFrameworkBundleWithIdentifier(kAppBundleIdentifier);
105 }
106 if (bundle == nil) {
107 bundle = [NSBundle mainBundle];
108 }
109 NSString* flutterAssetsName = [bundle objectForInfoDictionaryKey:@"FLTAssetsPath"];
110 if (flutterAssetsName == nil) {
111 flutterAssetsName = @"Contents/Frameworks/App.framework/Resources/flutter_assets";
112 }
113 return flutterAssetsName;
114 }
115
116 + (NSString*)lookupKeyForAsset:(NSString*)asset {
117 return [self lookupKeyForAsset:asset fromBundle:nil];
118 }
119
120 + (NSString*)lookupKeyForAsset:(NSString*)asset fromBundle:(nullable NSBundle*)bundle {
121 NSString* flutterAssetsName = [FlutterDartProject flutterAssetsNameWithBundle:bundle];
122 return [NSString stringWithFormat:@"%@/%@", flutterAssetsName, asset];
123 }
124
125 + (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
126 return [self lookupKeyForAsset:asset fromPackage:package fromBundle:nil];
127 }
128
129 + (NSString*)lookupKeyForAsset:(NSString*)asset
130 fromPackage:(NSString*)package
131 fromBundle:(nullable NSBundle*)bundle {
132 return [self lookupKeyForAsset:[NSString stringWithFormat:@"packages/%@/%@", package, asset]
133 fromBundle:bundle];
134 }
135
136 + (NSString*)defaultBundleIdentifier {
137 return kAppBundleIdentifier;
138 }
139
140 @end
FlutterDartProject.h
kAppBundleIdentifier
static NSString *const kAppBundleIdentifier
Definition: FlutterDartProject.mm:13
kICUBundlePath
static NSString *const kICUBundlePath
Definition: FlutterDartProject.mm:12
_ICUDataPath
NSString * _ICUDataPath
Definition: FlutterDartProject.mm:27
_assetsPath
NSString * _assetsPath
Definition: FlutterDartProject.mm:24
FlutterDartProject_Internal.h
FLTFrameworkBundleWithIdentifier
NSBundle * FLTFrameworkBundleWithIdentifier(NSString *flutterFrameworkBundleID)
Definition: FlutterNSBundleUtils.mm:46
FlutterDartProject
Definition: FlutterDartProject.mm:24
engine_switches.h