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"
17 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
23 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
33 static BOOL result = NO;
34 static dispatch_once_t once_token = 0;
35 dispatch_once(&once_token, ^{
36 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
37 result = [device supportsFamily:MTLGPUFamilyApple2];
43 NSBundle* _Nullable mainBundle) {
44 NSNumber* nsEnableWideGamut = [bundle objectForInfoDictionaryKey:
@"FLTEnableWideGamut"];
45 if (nsEnableWideGamut == nil && bundle != mainBundle) {
46 nsEnableWideGamut = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableWideGamut"];
48 return nsEnableWideGamut;
63 bool hasExplicitBundle = bundle != nil;
68 auto settings = flutter::SettingsFromCommandLine(command_line,
true);
70 settings.task_observer_add = [](intptr_t key,
const fml::closure& callback) {
71 fml::TaskQueueId queue_id = fml::MessageLoop::GetCurrentTaskQueueId();
72 fml::MessageLoopTaskQueues::GetInstance()->AddTaskObserver(queue_id, key, callback);
76 settings.task_observer_remove = [](fml::TaskQueueId queue_id, intptr_t key) {
77 fml::MessageLoopTaskQueues::GetInstance()->RemoveTaskObserver(queue_id, key);
80 settings.log_message_callback = [](
const std::string& tag,
const std::string& message) {
81 std::stringstream stream;
83 stream << tag <<
": ";
86 std::string log = stream.str();
87 [FlutterLogger logDirect:[NSString stringWithUTF8String:log.c_str()]];
90 settings.enable_platform_isolates =
true;
96 if (settings.icu_data_path.empty()) {
97 NSString* icuDataPath = [engineBundle pathForResource:
@"icudtl" ofType:
@"dat"];
98 if (icuDataPath.length > 0) {
99 settings.icu_data_path = icuDataPath.UTF8String;
103 if (flutter::DartVM::IsRunningPrecompiledCode()) {
104 if (hasExplicitBundle) {
105 NSString* executablePath = bundle.executablePath;
106 if ([[NSFileManager defaultManager] fileExistsAtPath:executablePath]) {
107 settings.application_library_paths.push_back(executablePath.UTF8String);
112 if (settings.application_library_paths.empty()) {
113 NSString* libraryName = [mainBundle objectForInfoDictionaryKey:
@"FLTLibraryPath"];
114 NSString* libraryPath = [mainBundle pathForResource:libraryName ofType:
@""];
115 if (libraryPath.length > 0) {
116 NSString* executablePath = [NSBundle bundleWithPath:libraryPath].executablePath;
117 if (executablePath.length > 0) {
118 settings.application_library_paths.push_back(executablePath.UTF8String);
125 if (settings.application_library_paths.empty()) {
126 NSString* applicationFrameworkPath = [mainBundle pathForResource:
@"Frameworks/App.framework"
128 if (applicationFrameworkPath.length > 0) {
129 NSString* executablePath =
130 [NSBundle bundleWithPath:applicationFrameworkPath].executablePath;
131 if (executablePath.length > 0) {
132 settings.application_library_paths.push_back(executablePath.UTF8String);
139 if (settings.assets_path.empty()) {
142 if (assetsPath.length == 0) {
143 NSLog(
@"Failed to find assets path for \"%@\
"", bundle);
145 settings.assets_path = assetsPath.UTF8String;
150 if (!flutter::DartVM::IsRunningPrecompiledCode()) {
151 NSURL* applicationKernelSnapshotURL =
153 relativeToURL:[NSURL fileURLWithPath:assetsPath]];
155 if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
156 settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
158 NSLog(
@"Failed to find snapshot at %@: %@", applicationKernelSnapshotURL.path, error);
167 settings.may_insecurely_connect_to_all_domains =
true;
168 settings.domain_network_policy =
"";
171 #if TARGET_OS_SIMULATOR
174 settings.enable_wide_gamut =
false;
179 BOOL enableWideGamut =
181 settings.enable_wide_gamut = enableWideGamut;
184 NSNumber* nsAntialiasLines = [mainBundle objectForInfoDictionaryKey:
@"FLTAntialiasLines"];
185 settings.impeller_antialiased_lines = (nsAntialiasLines ? nsAntialiasLines.boolValue : NO);
187 settings.warn_on_impeller_opt_out =
true;
189 NSNumber* nsEnableSDFs = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableSDFs"];
190 settings.impeller_use_sdfs = (nsEnableSDFs ? nsEnableSDFs.boolValue : NO);
192 NSNumber* enableTraceSystrace = [mainBundle objectForInfoDictionaryKey:
@"FLTTraceSystrace"];
194 if (enableTraceSystrace != nil) {
195 settings.trace_systrace = enableTraceSystrace.boolValue;
198 NSNumber* profileMicrotasks = [mainBundle objectForInfoDictionaryKey:
@"FLTProfileMicrotasks"];
200 if (profileMicrotasks != nil) {
201 settings.profile_microtasks = profileMicrotasks.boolValue;
204 NSNumber* enableDartAsserts = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartAsserts"];
205 if (enableDartAsserts != nil) {
206 settings.dart_flags.push_back(
"--enable-asserts");
209 NSNumber* enableDartProfiling = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartProfiling"];
211 if (enableDartProfiling != nil) {
212 settings.enable_dart_profiling = enableDartProfiling.boolValue;
215 NSNumber* profileStartup = [mainBundle objectForInfoDictionaryKey:
@"FLTProfileStartup"];
217 if (profileStartup != nil) {
218 settings.profile_startup = profileStartup.boolValue;
222 NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:
@"FLTLeakDartVM"];
224 if (leakDartVM != nil) {
225 settings.leak_vm = leakDartVM.boolValue;
228 NSNumber* enableMergedPlatformUIThread =
229 [mainBundle objectForInfoDictionaryKey:
@"FLTEnableMergedPlatformUIThread"];
230 if (enableMergedPlatformUIThread != nil) {
231 FML_CHECK(enableMergedPlatformUIThread.boolValue)
232 <<
"FLTEnableMergedPlatformUIThread=false is no longer allowed.";
235 NSNumber* enableFlutterGPU = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableFlutterGPU"];
236 if (enableFlutterGPU != nil) {
237 settings.enable_flutter_gpu = enableFlutterGPU.boolValue;
240 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
243 auto make_mapping_callback = [](
const uint8_t* mapping,
size_t size) {
244 return [mapping, size]() {
return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
247 settings.dart_library_sources_kernel =
257 if (settings.old_gen_heap_size <= 0) {
258 settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
259 flutter::kMegaByteSizeInBytes);
264 CGFloat scale = [UIScreen mainScreen].scale;
265 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width * scale;
266 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * scale;
267 settings.resource_cache_max_bytes_threshold = screenWidth * screenHeight * 12 * 4;
270 NSNumber* enable_embedder_api =
271 [mainBundle objectForInfoDictionaryKey:
@"FLTEnableIOSEmbedderAPI"];
273 if (enable_embedder_api) {
274 settings.enable_embedder_api = enable_embedder_api.boolValue;
281 flutter::Settings _settings;
287 @dynamic dartEntrypointArguments;
289 #pragma mark - Override base class designated initializers
291 - (instancetype)init {
292 return [
self initWithPrecompiledDartBundle:nil];
295 #pragma mark - Designated initializers
297 - (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle {
307 - (instancetype)initWithSettings:(const
flutter::Settings&)settings {
308 self = [
self initWithPrecompiledDartBundle:nil];
311 _settings = settings;
317 #pragma mark - PlatformData accessors
319 - (const
flutter::PlatformData)defaultPlatformData {
320 flutter::PlatformData PlatformData;
321 PlatformData.lifecycle_state = std::string(
"AppLifecycleState.detached");
325 #pragma mark - Settings accessors
327 - (const
flutter::Settings&)settings {
331 - (
flutter::RunConfiguration)runConfiguration {
332 return [
self runConfigurationForEntrypoint:nil];
335 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil {
336 return [
self runConfigurationForEntrypoint:entrypointOrNil libraryOrNil:nil];
339 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
340 libraryOrNil:(nullable NSString*)dartLibraryOrNil {
341 return [
self runConfigurationForEntrypoint:entrypointOrNil
342 libraryOrNil:dartLibraryOrNil
346 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
347 libraryOrNil:(nullable NSString*)dartLibraryOrNil
349 (nullable NSArray<NSString*>*)entrypointArgs {
350 auto config = flutter::RunConfiguration::InferFromSettings(_settings);
351 if (dartLibraryOrNil && entrypointOrNil) {
352 config.SetEntrypointAndLibrary(std::string([entrypointOrNil UTF8String]),
353 std::string([dartLibraryOrNil UTF8String]));
355 }
else if (entrypointOrNil) {
356 config.SetEntrypoint(std::string([entrypointOrNil UTF8String]));
359 if (entrypointArgs.count) {
360 std::vector<std::string> cppEntrypointArgs;
361 for (NSString* arg in entrypointArgs) {
362 cppEntrypointArgs.push_back(std::string([arg UTF8String]));
364 config.SetEntrypointArgs(std::move(cppEntrypointArgs));
370 #pragma mark - Assets-related utilities
372 + (NSString*)flutterAssetsName:(NSBundle*)bundle {
379 + (NSString*)domainNetworkPolicy:(NSDictionary*)appTransportSecurity {
381 NSDictionary* exceptionDomains = appTransportSecurity[@"NSExceptionDomains"];
382 if (exceptionDomains == nil) {
385 NSMutableArray* networkConfigArray = [[NSMutableArray alloc] init];
386 for (NSString* domain in exceptionDomains) {
387 NSDictionary* domainConfiguration = exceptionDomains[domain];
389 bool includesSubDomains = [domainConfiguration[@"NSIncludesSubdomains"] boolValue];
390 bool allowsCleartextCommunication =
391 [domainConfiguration[@"NSExceptionAllowsInsecureHTTPLoads"] boolValue];
392 [networkConfigArray addObject:@[
393 domain, includesSubDomains ? @YES : @NO, allowsCleartextCommunication ? @YES : @NO
396 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:networkConfigArray
399 return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
402 + (bool)allowsArbitraryLoads:(NSDictionary*)appTransportSecurity {
403 return [appTransportSecurity[@"NSAllowsArbitraryLoads"] boolValue];
406 + (NSString*)lookupKeyForAsset:(NSString*)asset {
407 return [
self lookupKeyForAsset:asset fromBundle:nil];
410 + (NSString*)lookupKeyForAsset:(NSString*)asset fromBundle:(nullable NSBundle*)bundle {
412 return [NSString stringWithFormat:@"%@/%@", flutterAssetsName, asset];
415 + (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
416 return [
self lookupKeyForAsset:asset fromPackage:package fromBundle:nil];
419 + (NSString*)lookupKeyForAsset:(NSString*)asset
420 fromPackage:(NSString*)package
421 fromBundle:(nullable NSBundle*)bundle {
422 return [
self lookupKeyForAsset:[NSString stringWithFormat:@"packages/%@/%@", package, asset]
426 + (NSString*)defaultBundleIdentifier {
427 return @"io.flutter.flutter.app";
430 - (BOOL)isWideGamutEnabled {
431 return _settings.enable_wide_gamut;
NSNumber *_Nullable FLTEnableWideGamutFromBundle(NSBundle *_Nullable bundle, NSBundle *_Nullable mainBundle)
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)