5 #define FML_USED_ON_EMBEDDER
9 #import <Metal/Metal.h>
10 #import <UIKit/UIKit.h>
14 #include "flutter/common/constants.h"
15 #include "flutter/fml/build_config.h"
16 #include "flutter/shell/common/switches.h"
22 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
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, *)) {
38 result = [device supportsFamily:MTLGPUFamilyApple2];
41 result = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v2];
59 bool hasExplicitBundle = bundle != nil;
64 auto settings = flutter::SettingsFromCommandLine(command_line);
66 settings.task_observer_add = [](intptr_t key,
const fml::closure& callback) {
67 fml::MessageLoop::GetCurrent().AddTaskObserver(key, callback);
70 settings.task_observer_remove = [](intptr_t key) {
71 fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
74 settings.log_message_callback = [](
const std::string& tag,
const std::string& message) {
77 std::stringstream stream;
79 stream << tag <<
": ";
82 std::string log = stream.str();
83 syslog(LOG_ALERT,
"%.*s", (
int)log.size(), log.c_str());
86 settings.enable_platform_isolates =
true;
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;
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);
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);
121 if (settings.application_library_path.empty()) {
122 NSString* applicationFrameworkPath = [mainBundle pathForResource:
@"Frameworks/App.framework"
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);
135 if (settings.assets_path.empty()) {
138 if (assetsPath.length == 0) {
139 NSLog(
@"Failed to find assets path for \"%@\
"", bundle);
141 settings.assets_path = assetsPath.UTF8String;
146 if (!flutter::DartVM::IsRunningPrecompiledCode()) {
147 NSURL* applicationKernelSnapshotURL =
149 relativeToURL:[NSURL fileURLWithPath:assetsPath]];
151 if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
152 settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
154 NSLog(
@"Failed to find snapshot at %@: %@", applicationKernelSnapshotURL.path, error);
163 settings.may_insecurely_connect_to_all_domains =
true;
164 settings.domain_network_policy =
"";
167 #if TARGET_OS_SIMULATOR
170 settings.enable_wide_gamut =
false;
174 NSNumber* nsEnableWideGamut = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableWideGamut"];
175 BOOL enableWideGamut =
177 settings.enable_wide_gamut = enableWideGamut;
180 NSNumber* nsAntialiasLines = [mainBundle objectForInfoDictionaryKey:
@"FLTAntialiasLines"];
181 settings.impeller_antialiased_lines = (nsAntialiasLines ? nsAntialiasLines.boolValue : NO);
183 settings.warn_on_impeller_opt_out =
true;
185 NSNumber* enableTraceSystrace = [mainBundle objectForInfoDictionaryKey:
@"FLTTraceSystrace"];
187 if (enableTraceSystrace != nil) {
188 settings.trace_systrace = enableTraceSystrace.boolValue;
191 NSNumber* enableDartAsserts = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartAsserts"];
192 if (enableDartAsserts != nil) {
193 settings.dart_flags.push_back(
"--enable-asserts");
196 NSNumber* enableDartProfiling = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartProfiling"];
198 if (enableDartProfiling != nil) {
199 settings.enable_dart_profiling = enableDartProfiling.boolValue;
203 NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:
@"FLTLeakDartVM"];
205 if (leakDartVM != nil) {
206 settings.leak_vm = leakDartVM.boolValue;
209 NSNumber* enableMergedPlatformUIThread =
210 [mainBundle objectForInfoDictionaryKey:
@"FLTEnableMergedPlatformUIThread"];
211 if (enableMergedPlatformUIThread != nil) {
212 settings.merged_platform_ui_thread = enableMergedPlatformUIThread.boolValue;
215 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
218 auto make_mapping_callback = [](
const uint8_t* mapping,
size_t size) {
219 return [mapping, size]() {
return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
222 settings.dart_library_sources_kernel =
232 if (settings.old_gen_heap_size <= 0) {
233 settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
234 flutter::kMegaByteSizeInBytes);
239 CGFloat scale = [UIScreen mainScreen].scale;
240 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width * scale;
241 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * scale;
242 settings.resource_cache_max_bytes_threshold = screenWidth * screenHeight * 12 * 4;
245 NSNumber* enable_embedder_api =
246 [mainBundle objectForInfoDictionaryKey:
@"FLTEnableIOSEmbedderAPI"];
248 if (enable_embedder_api) {
249 settings.enable_embedder_api = enable_embedder_api.boolValue;
256 flutter::Settings _settings;
262 @dynamic dartEntrypointArguments;
264 #pragma mark - Override base class designated initializers
266 - (instancetype)init {
267 return [
self initWithPrecompiledDartBundle:nil];
270 #pragma mark - Designated initializers
272 - (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle {
282 - (instancetype)initWithSettings:(const
flutter::Settings&)settings {
283 self = [
self initWithPrecompiledDartBundle:nil];
286 _settings = settings;
292 #pragma mark - PlatformData accessors
294 - (const
flutter::PlatformData)defaultPlatformData {
295 flutter::PlatformData PlatformData;
296 PlatformData.lifecycle_state = std::string(
"AppLifecycleState.detached");
300 #pragma mark - Settings accessors
302 - (const
flutter::Settings&)settings {
306 - (
flutter::RunConfiguration)runConfiguration {
307 return [
self runConfigurationForEntrypoint:nil];
310 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil {
311 return [
self runConfigurationForEntrypoint:entrypointOrNil libraryOrNil:nil];
314 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
315 libraryOrNil:(nullable NSString*)dartLibraryOrNil {
316 return [
self runConfigurationForEntrypoint:entrypointOrNil
317 libraryOrNil:dartLibraryOrNil
321 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
322 libraryOrNil:(nullable NSString*)dartLibraryOrNil
324 (nullable NSArray<NSString*>*)entrypointArgs {
325 auto config = flutter::RunConfiguration::InferFromSettings(_settings);
326 if (dartLibraryOrNil && entrypointOrNil) {
327 config.SetEntrypointAndLibrary(std::string([entrypointOrNil UTF8String]),
328 std::string([dartLibraryOrNil UTF8String]));
330 }
else if (entrypointOrNil) {
331 config.SetEntrypoint(std::string([entrypointOrNil UTF8String]));
334 if (entrypointArgs.count) {
335 std::vector<std::string> cppEntrypointArgs;
336 for (NSString* arg in entrypointArgs) {
337 cppEntrypointArgs.push_back(std::string([arg UTF8String]));
339 config.SetEntrypointArgs(std::move(cppEntrypointArgs));
345 #pragma mark - Assets-related utilities
347 + (NSString*)flutterAssetsName:(NSBundle*)bundle {
354 + (NSString*)domainNetworkPolicy:(NSDictionary*)appTransportSecurity {
356 NSDictionary* exceptionDomains = appTransportSecurity[@"NSExceptionDomains"];
357 if (exceptionDomains == nil) {
360 NSMutableArray* networkConfigArray = [[NSMutableArray alloc] init];
361 for (NSString* domain in exceptionDomains) {
362 NSDictionary* domainConfiguration = exceptionDomains[domain];
364 bool includesSubDomains = [domainConfiguration[@"NSIncludesSubdomains"] boolValue];
365 bool allowsCleartextCommunication =
366 [domainConfiguration[@"NSExceptionAllowsInsecureHTTPLoads"] boolValue];
367 [networkConfigArray addObject:@[
368 domain, includesSubDomains ? @YES : @NO, allowsCleartextCommunication ? @YES : @NO
371 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:networkConfigArray
374 return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
377 + (bool)allowsArbitraryLoads:(NSDictionary*)appTransportSecurity {
378 return [appTransportSecurity[@"NSAllowsArbitraryLoads"] boolValue];
381 + (NSString*)lookupKeyForAsset:(NSString*)asset {
382 return [
self lookupKeyForAsset:asset fromBundle:nil];
385 + (NSString*)lookupKeyForAsset:(NSString*)asset fromBundle:(nullable NSBundle*)bundle {
387 return [NSString stringWithFormat:@"%@/%@", flutterAssetsName, asset];
390 + (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
391 return [
self lookupKeyForAsset:asset fromPackage:package fromBundle:nil];
394 + (NSString*)lookupKeyForAsset:(NSString*)asset
395 fromPackage:(NSString*)package
396 fromBundle:(nullable NSBundle*)bundle {
397 return [
self lookupKeyForAsset:[NSString stringWithFormat:@"packages/%@/%@", package, asset]
401 + (NSString*)defaultBundleIdentifier {
402 return @"io.flutter.flutter.app";
405 - (BOOL)isWideGamutEnabled {
406 return _settings.enable_wide_gamut;
static const char * kApplicationKernelSnapshotFileName
const intptr_t kPlatformStrongDillSize
FLUTTER_ASSERT_ARC const uint8_t kPlatformStrongDill[]
flutter::Settings FLTDefaultSettingsForBundle(NSBundle *bundle, NSProcessInfo *processInfoOrNil)
static BOOL DoesHardwareSupportWideGamut()
NSString * FLTAssetPath(NSBundle *bundle)
NSBundle * FLTGetApplicationBundle()
NSBundle * FLTFrameworkBundleWithIdentifier(NSString *flutterFrameworkBundleID)
NSString * FLTAssetsPathFromBundle(NSBundle *bundle)
NSString * flutterAssetsName:(NSBundle *bundle)
fml::CommandLine CommandLineFromNSProcessInfo(NSProcessInfo *processInfoOrNil=nil)