| Flutter iOS 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 #define FML_USED_ON_EMBEDDER
6
7 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
8
9 #import <Metal/Metal.h>
10 #import <UIKit/UIKit.h>
11
12 #include <syslog.h>
13
14 #include "flutter/common/constants.h"
15 #include "flutter/fml/build_config.h"
16 #include "flutter/shell/common/switches.h"
17 #include "flutter/shell/platform/darwin/common/command_line.h"
18
19 FLUTTER_ASSERT_ARC
20
21 extern "C" {
22 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
23 // Used for debugging dart:* sources.
24 extern const uint8_t kPlatformStrongDill[];
25 extern const intptr_t kPlatformStrongDillSize;
26 #endif
27 }
28
29 static const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin";
30
31 static BOOL DoesHardwareSupportWideGamut() {
32 static BOOL result = NO;
33 static dispatch_once_t once_token = 0;
34 dispatch_once(&once_token, ^{
35 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
36 if (@available(iOS 13.0, *)) {
37 // MTLGPUFamilyApple2 = A9/A10
38 result = [device supportsFamily:MTLGPUFamilyApple2];
39 } else {
40 // A9/A10 on iOS 10+
41 result = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v2];
42 }
43 });
44 return result;
45 }
46
47 flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle, NSProcessInfo* processInfoOrNil) {
48 auto command_line = flutter::CommandLineFromNSProcessInfo(processInfoOrNil);
49
50 // Precedence:
51 // 1. Settings from the specified NSBundle (except for enable-impeller).
52 // 2. Settings passed explicitly via command-line arguments.
53 // 3. Settings from the NSBundle with the default bundle ID.
54 // 4. Settings from the main NSBundle and default values.
55
56 NSBundle* mainBundle = FLTGetApplicationBundle();
57 NSBundle* engineBundle = [NSBundle bundleForClass:[FlutterDartProject class]];
58
59 bool hasExplicitBundle = bundle != nil;
60 if (bundle == nil) {
61 bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]);
62 }
63
64 auto settings = flutter::SettingsFromCommandLine(command_line);
65
66 settings.task_observer_add = [](intptr_t key, const fml::closure& callback) {
67 fml::MessageLoop::GetCurrent().AddTaskObserver(key, callback);
68 };
69
70 settings.task_observer_remove = [](intptr_t key) {
71 fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
72 };
73
74 settings.log_message_callback = [](const std::string& tag, const std::string& message) {
75 // TODO(cbracken): replace this with os_log-based approach.
76 // https://github.com/flutter/flutter/issues/44030
77 std::stringstream stream;
78 if (!tag.empty()) {
79 stream << tag << ": ";
80 }
81 stream << message;
82 std::string log = stream.str();
83 syslog(LOG_ALERT, "%.*s", (int)log.size(), log.c_str());
84 };
85
86 settings.enable_platform_isolates = true;
87
88 // The command line arguments may not always be complete. If they aren't, attempt to fill in
89 // defaults.
90
91 // Flutter ships the ICU data file in the bundle of the engine. Look for it there.
92 if (settings.icu_data_path.empty()) {
93 NSString* icuDataPath = [engineBundle pathForResource:@"icudtl" ofType:@"dat"];
94 if (icuDataPath.length > 0) {
95 settings.icu_data_path = icuDataPath.UTF8String;
96 }
97 }
98
99 if (flutter::DartVM::IsRunningPrecompiledCode()) {
100 if (hasExplicitBundle) {
101 NSString* executablePath = bundle.executablePath;
102 if ([[NSFileManager defaultManager] fileExistsAtPath:executablePath]) {
103 settings.application_library_path.push_back(executablePath.UTF8String);
104 }
105 }
106
107 // No application bundle specified. Try a known location from the main bundle's Info.plist.
108 if (settings.application_library_path.empty()) {
109 NSString* libraryName = [mainBundle objectForInfoDictionaryKey:@"FLTLibraryPath"];
110 NSString* libraryPath = [mainBundle pathForResource:libraryName ofType:@""];
111 if (libraryPath.length > 0) {
112 NSString* executablePath = [NSBundle bundleWithPath:libraryPath].executablePath;
113 if (executablePath.length > 0) {
114 settings.application_library_path.push_back(executablePath.UTF8String);
115 }
116 }
117 }
118
119 // In case the application bundle is still not specified, look for the App.framework in the
120 // Frameworks directory.
121 if (settings.application_library_path.empty()) {
122 NSString* applicationFrameworkPath = [mainBundle pathForResource:@"Frameworks/App.framework"
123 ofType:@""];
124 if (applicationFrameworkPath.length > 0) {
125 NSString* executablePath =
126 [NSBundle bundleWithPath:applicationFrameworkPath].executablePath;
127 if (executablePath.length > 0) {
128 settings.application_library_path.push_back(executablePath.UTF8String);
129 }
130 }
131 }
132 }
133
134 // Checks to see if the flutter assets directory is already present.
135 if (settings.assets_path.empty()) {
136 NSString* assetsPath = FLTAssetsPathFromBundle(bundle);
137
138 if (assetsPath.length == 0) {
139 NSLog(@"Failed to find assets path for \"%@\"", bundle);
140 } else {
141 settings.assets_path = assetsPath.UTF8String;
142
143 // Check if there is an application kernel snapshot in the assets directory we could
144 // potentially use. Looking for the snapshot makes sense only if we have a VM that can use
145 // it.
146 if (!flutter::DartVM::IsRunningPrecompiledCode()) {
147 NSURL* applicationKernelSnapshotURL =
148 [NSURL URLWithString:@(kApplicationKernelSnapshotFileName)
149 relativeToURL:[NSURL fileURLWithPath:assetsPath]];
150 NSError* error;
151 if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
152 settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
153 } else {
154 NSLog(@"Failed to find snapshot at %@: %@", applicationKernelSnapshotURL.path, error);
155 }
156 }
157 }
158 }
159
160 // Domain network configuration
161 // Disabled in https://github.com/flutter/flutter/issues/72723.
162 // Re-enable in https://github.com/flutter/flutter/issues/54448.
163 settings.may_insecurely_connect_to_all_domains = true;
164 settings.domain_network_policy = "";
165
166 // Whether to enable wide gamut colors.
167 #if TARGET_OS_SIMULATOR
168 // As of Xcode 14.1, the wide gamut surface pixel formats are not supported by
169 // the simulator.
170 settings.enable_wide_gamut = false;
171 // Removes unused function warning.
172 (void)DoesHardwareSupportWideGamut;
173 #else
174 NSNumber* nsEnableWideGamut = [mainBundle objectForInfoDictionaryKey:@"FLTEnableWideGamut"];
175 BOOL enableWideGamut =
176 (nsEnableWideGamut ? nsEnableWideGamut.boolValue : YES) && DoesHardwareSupportWideGamut();
177 settings.enable_wide_gamut = enableWideGamut;
178 #endif
179
180 settings.warn_on_impeller_opt_out = true;
181
182 NSNumber* enableTraceSystrace = [mainBundle objectForInfoDictionaryKey:@"FLTTraceSystrace"];
183 // Change the default only if the option is present.
184 if (enableTraceSystrace != nil) {
185 settings.trace_systrace = enableTraceSystrace.boolValue;
186 }
187
188 NSNumber* enableDartAsserts = [mainBundle objectForInfoDictionaryKey:@"FLTEnableDartAsserts"];
189 if (enableDartAsserts != nil) {
190 settings.dart_flags.push_back("--enable-asserts");
191 }
192
193 NSNumber* enableDartProfiling = [mainBundle objectForInfoDictionaryKey:@"FLTEnableDartProfiling"];
194 // Change the default only if the option is present.
195 if (enableDartProfiling != nil) {
196 settings.enable_dart_profiling = enableDartProfiling.boolValue;
197 }
198
199 // Leak Dart VM settings, set whether leave or clean up the VM after the last shell shuts down.
200 NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:@"FLTLeakDartVM"];
201 // It will change the default leak_vm value in settings only if the key exists.
202 if (leakDartVM != nil) {
203 settings.leak_vm = leakDartVM.boolValue;
204 }
205
206 NSNumber* enableMergedPlatformUIThread =
207 [mainBundle objectForInfoDictionaryKey:@"FLTEnableMergedPlatformUIThread"];
208 if (enableMergedPlatformUIThread != nil) {
209 settings.merged_platform_ui_thread = enableMergedPlatformUIThread.boolValue;
210 }
211
212 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
213 // There are no ownership concerns here as all mappings are owned by the
214 // embedder and not the engine.
215 auto make_mapping_callback = [](const uint8_t* mapping, size_t size) {
216 return [mapping, size]() { return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
217 };
218
219 settings.dart_library_sources_kernel =
220 make_mapping_callback(kPlatformStrongDill, kPlatformStrongDillSize);
221 #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
222
223 // If we even support setting this e.g. from the command line or the plist,
224 // we should let the user override it.
225 // Otherwise, we want to set this to a value that will avoid having the OS
226 // kill us. On most iOS devices, that happens somewhere near half
227 // the available memory.
228 // The VM expects this value to be in megabytes.
229 if (settings.old_gen_heap_size <= 0) {
230 settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
231 flutter::kMegaByteSizeInBytes);
232 }
233
234 // This is the formula Android uses.
235 // https://android.googlesource.com/platform/frameworks/base/+/39ae5bac216757bc201490f4c7b8c0f63006c6cd/libs/hwui/renderthread/CacheManager.cpp#45
236 CGFloat scale = [UIScreen mainScreen].scale;
237 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width * scale;
238 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * scale;
239 settings.resource_cache_max_bytes_threshold = screenWidth * screenHeight * 12 * 4;
240
241 // Whether to enable ios embedder api.
242 NSNumber* enable_embedder_api =
243 [mainBundle objectForInfoDictionaryKey:@"FLTEnableIOSEmbedderAPI"];
244 // Change the default only if the option is present.
245 if (enable_embedder_api) {
246 settings.enable_embedder_api = enable_embedder_api.boolValue;
247 }
248
249 return settings;
250 }
251
252 @implementation FlutterDartProject {
253 flutter::Settings _settings;
254 }
255
256 // This property is marked unavailable on iOS in the common header.
257 // That doesn't seem to be enough to prevent this property from being synthesized.
258 // Mark dynamic to avoid warnings.
259 @dynamic dartEntrypointArguments;
260
261 #pragma mark - Override base class designated initializers
262
263 - (instancetype)init {
264 return [self initWithPrecompiledDartBundle:nil];
265 }
266
267 #pragma mark - Designated initializers
268
269 - (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle {
270 self = [super init];
271
272 if (self) {
273 _settings = FLTDefaultSettingsForBundle(bundle);
274 }
275
276 return self;
277 }
278
279 - (instancetype)initWithSettings:(const flutter::Settings&)settings {
280 self = [self initWithPrecompiledDartBundle:nil];
281
282 if (self) {
283 _settings = settings;
284 }
285
286 return self;
287 }
288
289 #pragma mark - PlatformData accessors
290
291 - (const flutter::PlatformData)defaultPlatformData {
292 flutter::PlatformData PlatformData;
293 PlatformData.lifecycle_state = std::string("AppLifecycleState.detached");
294 return PlatformData;
295 }
296
297 #pragma mark - Settings accessors
298
299 - (const flutter::Settings&)settings {
300 return _settings;
301 }
302
303 - (flutter::RunConfiguration)runConfiguration {
304 return [self runConfigurationForEntrypoint:nil];
305 }
306
307 - (flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil {
308 return [self runConfigurationForEntrypoint:entrypointOrNil libraryOrNil:nil];
309 }
310
311 - (flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
312 libraryOrNil:(nullable NSString*)dartLibraryOrNil {
313 return [self runConfigurationForEntrypoint:entrypointOrNil
314 libraryOrNil:dartLibraryOrNil
315 entrypointArgs:nil];
316 }
317
318 - (flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
319 libraryOrNil:(nullable NSString*)dartLibraryOrNil
320 entrypointArgs:
321 (nullable NSArray<NSString*>*)entrypointArgs {
322 auto config = flutter::RunConfiguration::InferFromSettings(_settings);
323 if (dartLibraryOrNil && entrypointOrNil) {
324 config.SetEntrypointAndLibrary(std::string([entrypointOrNil UTF8String]),
325 std::string([dartLibraryOrNil UTF8String]));
326
327 } else if (entrypointOrNil) {
328 config.SetEntrypoint(std::string([entrypointOrNil UTF8String]));
329 }
330
331 if (entrypointArgs.count) {
332 std::vector<std::string> cppEntrypointArgs;
333 for (NSString* arg in entrypointArgs) {
334 cppEntrypointArgs.push_back(std::string([arg UTF8String]));
335 }
336 config.SetEntrypointArgs(std::move(cppEntrypointArgs));
337 }
338
339 return config;
340 }
341
342 #pragma mark - Assets-related utilities
343
344 + (NSString*)flutterAssetsName:(NSBundle*)bundle {
345 if (bundle == nil) {
346 bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]);
347 }
348 return FLTAssetPath(bundle);
349 }
350
351 + (NSString*)domainNetworkPolicy:(NSDictionary*)appTransportSecurity {
352 // https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity/nsexceptiondomains
353 NSDictionary* exceptionDomains = appTransportSecurity[@"NSExceptionDomains"];
354 if (exceptionDomains == nil) {
355 return @"";
356 }
357 NSMutableArray* networkConfigArray = [[NSMutableArray alloc] init];
358 for (NSString* domain in exceptionDomains) {
359 NSDictionary* domainConfiguration = exceptionDomains[domain];
360 // Default value is false.
361 bool includesSubDomains = [domainConfiguration[@"NSIncludesSubdomains"] boolValue];
362 bool allowsCleartextCommunication =
363 [domainConfiguration[@"NSExceptionAllowsInsecureHTTPLoads"] boolValue];
364 [networkConfigArray addObject:@[
365 domain, includesSubDomains ? @YES : @NO, allowsCleartextCommunication ? @YES : @NO
366 ]];
367 }
368 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:networkConfigArray
369 options:0
370 error:NULL];
371 return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
372 }
373
374 + (bool)allowsArbitraryLoads:(NSDictionary*)appTransportSecurity {
375 return [appTransportSecurity[@"NSAllowsArbitraryLoads"] boolValue];
376 }
377
378 + (NSString*)lookupKeyForAsset:(NSString*)asset {
379 return [self lookupKeyForAsset:asset fromBundle:nil];
380 }
381
382 + (NSString*)lookupKeyForAsset:(NSString*)asset fromBundle:(nullable NSBundle*)bundle {
383 NSString* flutterAssetsName = [FlutterDartProject flutterAssetsName:bundle];
384 return [NSString stringWithFormat:@"%@/%@", flutterAssetsName, asset];
385 }
386
387 + (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
388 return [self lookupKeyForAsset:asset fromPackage:package fromBundle:nil];
389 }
390
391 + (NSString*)lookupKeyForAsset:(NSString*)asset
392 fromPackage:(NSString*)package
393 fromBundle:(nullable NSBundle*)bundle {
394 return [self lookupKeyForAsset:[NSString stringWithFormat:@"packages/%@/%@", package, asset]
395 fromBundle:bundle];
396 }
397
398 + (NSString*)defaultBundleIdentifier {
399 return @"io.flutter.flutter.app";
400 }
401
402 - (BOOL)isWideGamutEnabled {
403 return _settings.enable_wide_gamut;
404 }
405
406 - (BOOL)isImpellerEnabled {
407 return _settings.enable_impeller;
408 }
409
410 @end
+[FlutterDartProject lookupKeyForAsset:fromPackage:fromBundle:]
NSString * lookupKeyForAsset:fromPackage:fromBundle:(NSString *asset,[fromPackage] NSString *package,[fromBundle] nullable NSBundle *bundle)
Definition: FlutterDartProject.mm:391
kApplicationKernelSnapshotFileName
static const char * kApplicationKernelSnapshotFileName
Definition: FlutterDartProject.mm:29
kPlatformStrongDillSize
const intptr_t kPlatformStrongDillSize
command_line.h
kPlatformStrongDill
const FLUTTER_ASSERT_ARC uint8_t kPlatformStrongDill[]
DoesHardwareSupportWideGamut
static BOOL DoesHardwareSupportWideGamut()
Definition: FlutterDartProject.mm:31
FLTAssetPath
NSString * FLTAssetPath(NSBundle *bundle)
Definition: FlutterNSBundleUtils.mm:60
FLTGetApplicationBundle
NSBundle * FLTGetApplicationBundle()
Definition: FlutterNSBundleUtils.mm:35
flutter
Definition: accessibility_bridge.h:27
flutter::CommandLineFromNSProcessInfo
fml::CommandLine CommandLineFromNSProcessInfo(NSProcessInfo *processInfoOrNil=nil)
Definition: command_line.mm:13
FLTFrameworkBundleWithIdentifier
NSBundle * FLTFrameworkBundleWithIdentifier(NSString *flutterFrameworkBundleID)
Definition: FlutterNSBundleUtils.mm:46
FlutterDartProject_Internal.h
+[FlutterDartProject flutterAssetsName:]
NSString * flutterAssetsName:(NSBundle *bundle)
FLTDefaultSettingsForBundle
flutter::Settings FLTDefaultSettingsForBundle(NSBundle *bundle, NSProcessInfo *processInfoOrNil)
Definition: FlutterDartProject.mm:47
FLTAssetsPathFromBundle
NSString * FLTAssetsPathFromBundle(NSBundle *bundle)
Definition: FlutterNSBundleUtils.mm:64
FlutterDartProject
Definition: FlutterDartProject.mm:252
+[FlutterDartProject lookupKeyForAsset:fromBundle:]
NSString * lookupKeyForAsset:fromBundle:(NSString *asset,[fromBundle] nullable NSBundle *bundle)
Definition: FlutterDartProject.mm:382
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13