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/shell/common/switches.h"
21 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
31 static BOOL result = NO;
32 static dispatch_once_t once_token = 0;
33 dispatch_once(&once_token, ^{
34 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
35 if (@available(iOS 13.0, *)) {
37 result = [device supportsFamily:MTLGPUFamilyApple2];
40 result = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v2];
58 bool hasExplicitBundle = bundle != nil;
63 auto settings = flutter::SettingsFromCommandLine(command_line);
65 settings.task_observer_add = [](intptr_t key,
const fml::closure& callback) {
66 fml::MessageLoop::GetCurrent().AddTaskObserver(key, callback);
69 settings.task_observer_remove = [](intptr_t key) {
70 fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
73 settings.log_message_callback = [](
const std::string& tag,
const std::string& message) {
76 std::stringstream stream;
78 stream << tag <<
": ";
81 std::string log = stream.str();
82 syslog(LOG_ALERT,
"%.*s", (
int)log.size(), log.c_str());
85 settings.enable_platform_isolates =
true;
91 if (settings.icu_data_path.empty()) {
92 NSString* icuDataPath = [engineBundle pathForResource:
@"icudtl" ofType:
@"dat"];
93 if (icuDataPath.length > 0) {
94 settings.icu_data_path = icuDataPath.UTF8String;
98 if (flutter::DartVM::IsRunningPrecompiledCode()) {
99 if (hasExplicitBundle) {
100 NSString* executablePath = bundle.executablePath;
101 if ([[NSFileManager defaultManager] fileExistsAtPath:executablePath]) {
102 settings.application_library_path.push_back(executablePath.UTF8String);
107 if (settings.application_library_path.empty()) {
108 NSString* libraryName = [mainBundle objectForInfoDictionaryKey:
@"FLTLibraryPath"];
109 NSString* libraryPath = [mainBundle pathForResource:libraryName ofType:
@""];
110 if (libraryPath.length > 0) {
111 NSString* executablePath = [NSBundle bundleWithPath:libraryPath].executablePath;
112 if (executablePath.length > 0) {
113 settings.application_library_path.push_back(executablePath.UTF8String);
120 if (settings.application_library_path.empty()) {
121 NSString* applicationFrameworkPath = [mainBundle pathForResource:
@"Frameworks/App.framework"
123 if (applicationFrameworkPath.length > 0) {
124 NSString* executablePath =
125 [NSBundle bundleWithPath:applicationFrameworkPath].executablePath;
126 if (executablePath.length > 0) {
127 settings.application_library_path.push_back(executablePath.UTF8String);
134 if (settings.assets_path.empty()) {
137 if (assetsPath.length == 0) {
138 NSLog(
@"Failed to find assets path for \"%@\
"", bundle);
140 settings.assets_path = assetsPath.UTF8String;
145 if (!flutter::DartVM::IsRunningPrecompiledCode()) {
146 NSURL* applicationKernelSnapshotURL =
148 relativeToURL:[NSURL fileURLWithPath:assetsPath]];
150 if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
151 settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
153 NSLog(
@"Failed to find snapshot at %@: %@", applicationKernelSnapshotURL.path, error);
162 settings.may_insecurely_connect_to_all_domains =
true;
163 settings.domain_network_policy =
"";
166 #if TARGET_OS_SIMULATOR
169 settings.enable_wide_gamut =
false;
173 NSNumber* nsEnableWideGamut = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableWideGamut"];
174 BOOL enableWideGamut =
176 settings.enable_wide_gamut = enableWideGamut;
179 if (!command_line.HasOption(
"enable-impeller")) {
181 NSNumber* enableImpeller = [bundle objectForInfoDictionaryKey:
@"FLTEnableImpeller"];
182 if (enableImpeller == nil) {
184 enableImpeller = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableImpeller"];
187 if (enableImpeller != nil) {
188 settings.enable_impeller = enableImpeller.boolValue;
192 settings.warn_on_impeller_opt_out =
true;
194 NSNumber* enableTraceSystrace = [mainBundle objectForInfoDictionaryKey:
@"FLTTraceSystrace"];
196 if (enableTraceSystrace != nil) {
197 settings.trace_systrace = enableTraceSystrace.boolValue;
200 NSNumber* enableDartAsserts = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartAsserts"];
201 if (enableDartAsserts != nil) {
202 settings.dart_flags.push_back(
"--enable-asserts");
205 NSNumber* enableDartProfiling = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartProfiling"];
207 if (enableDartProfiling != nil) {
208 settings.enable_dart_profiling = enableDartProfiling.boolValue;
212 NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:
@"FLTLeakDartVM"];
214 if (leakDartVM != nil) {
215 settings.leak_vm = leakDartVM.boolValue;
218 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
221 auto make_mapping_callback = [](
const uint8_t* mapping,
size_t size) {
222 return [mapping, size]() {
return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
225 settings.dart_library_sources_kernel =
227 #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
235 if (settings.old_gen_heap_size <= 0) {
236 settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
237 flutter::kMegaByteSizeInBytes);
242 CGFloat scale = [UIScreen mainScreen].scale;
243 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width * scale;
244 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * scale;
245 settings.resource_cache_max_bytes_threshold = screenWidth * screenHeight * 12 * 4;
248 NSNumber* enable_embedder_api =
249 [mainBundle objectForInfoDictionaryKey:
@"FLTEnableIOSEmbedderAPI"];
251 if (enable_embedder_api) {
252 settings.enable_embedder_api = enable_embedder_api.boolValue;
259 flutter::Settings _settings;
265 @dynamic dartEntrypointArguments;
267 #pragma mark - Override base class designated initializers
269 - (instancetype)init {
270 return [
self initWithPrecompiledDartBundle:nil];
273 #pragma mark - Designated initializers
275 - (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle {
285 - (instancetype)initWithSettings:(const
flutter::Settings&)settings {
286 self = [
self initWithPrecompiledDartBundle:nil];
289 _settings = settings;
295 #pragma mark - PlatformData accessors
297 - (const
flutter::PlatformData)defaultPlatformData {
298 flutter::PlatformData PlatformData;
299 PlatformData.lifecycle_state = std::string(
"AppLifecycleState.detached");
303 #pragma mark - Settings accessors
305 - (const
flutter::Settings&)settings {
309 - (
flutter::RunConfiguration)runConfiguration {
310 return [
self runConfigurationForEntrypoint:nil];
313 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil {
314 return [
self runConfigurationForEntrypoint:entrypointOrNil libraryOrNil:nil];
317 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
318 libraryOrNil:(nullable NSString*)dartLibraryOrNil {
319 return [
self runConfigurationForEntrypoint:entrypointOrNil
320 libraryOrNil:dartLibraryOrNil
324 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
325 libraryOrNil:(nullable NSString*)dartLibraryOrNil
327 (nullable NSArray<NSString*>*)entrypointArgs {
328 auto config = flutter::RunConfiguration::InferFromSettings(_settings);
329 if (dartLibraryOrNil && entrypointOrNil) {
330 config.SetEntrypointAndLibrary(std::string([entrypointOrNil UTF8String]),
331 std::string([dartLibraryOrNil UTF8String]));
333 }
else if (entrypointOrNil) {
334 config.SetEntrypoint(std::string([entrypointOrNil UTF8String]));
337 if (entrypointArgs.count) {
338 std::vector<std::string> cppEntrypointArgs;
339 for (NSString* arg in entrypointArgs) {
340 cppEntrypointArgs.push_back(std::string([arg UTF8String]));
342 config.SetEntrypointArgs(std::move(cppEntrypointArgs));
348 #pragma mark - Assets-related utilities
350 + (NSString*)flutterAssetsName:(NSBundle*)bundle {
357 + (NSString*)domainNetworkPolicy:(NSDictionary*)appTransportSecurity {
359 NSDictionary* exceptionDomains = appTransportSecurity[@"NSExceptionDomains"];
360 if (exceptionDomains == nil) {
363 NSMutableArray* networkConfigArray = [[NSMutableArray alloc] init];
364 for (NSString* domain in exceptionDomains) {
365 NSDictionary* domainConfiguration = exceptionDomains[domain];
367 bool includesSubDomains = [domainConfiguration[@"NSIncludesSubdomains"] boolValue];
368 bool allowsCleartextCommunication =
369 [domainConfiguration[@"NSExceptionAllowsInsecureHTTPLoads"] boolValue];
370 [networkConfigArray addObject:@[
371 domain, includesSubDomains ? @YES : @NO, allowsCleartextCommunication ? @YES : @NO
374 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:networkConfigArray
377 return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
380 + (bool)allowsArbitraryLoads:(NSDictionary*)appTransportSecurity {
381 return [appTransportSecurity[@"NSAllowsArbitraryLoads"] boolValue];
384 + (NSString*)lookupKeyForAsset:(NSString*)asset {
388 + (NSString*)lookupKeyForAsset:(NSString*)asset fromBundle:(nullable NSBundle*)bundle {
390 return [NSString stringWithFormat:@"%@/%@", flutterAssetsName, asset];
393 + (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
397 + (NSString*)lookupKeyForAsset:(NSString*)asset
398 fromPackage:(NSString*)package
399 fromBundle:(nullable NSBundle*)bundle {
400 return [
self lookupKeyForAsset:[NSString stringWithFormat:@"packages/%@/%@", package, asset]
404 + (NSString*)defaultBundleIdentifier {
405 return @"io.flutter.flutter.app";
408 - (BOOL)isWideGamutEnabled {
409 return _settings.enable_wide_gamut;
412 - (BOOL)isImpellerEnabled {
413 return _settings.enable_impeller;