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];
54 bool hasExplicitBundle = bundle != nil;
59 auto settings = flutter::SettingsFromCommandLine(command_line);
61 settings.task_observer_add = [](intptr_t key,
const fml::closure& callback) {
62 fml::TaskQueueId queue_id = fml::MessageLoop::GetCurrentTaskQueueId();
63 fml::MessageLoopTaskQueues::GetInstance()->AddTaskObserver(queue_id, key, callback);
67 settings.task_observer_remove = [](fml::TaskQueueId queue_id, intptr_t key) {
68 fml::MessageLoopTaskQueues::GetInstance()->RemoveTaskObserver(queue_id, key);
71 settings.log_message_callback = [](
const std::string& tag,
const std::string& message) {
72 std::stringstream stream;
74 stream << tag <<
": ";
77 std::string log = stream.str();
78 [FlutterLogger logDirect:[NSString stringWithUTF8String:log.c_str()]];
81 settings.enable_platform_isolates =
true;
87 if (settings.icu_data_path.empty()) {
88 NSString* icuDataPath = [engineBundle pathForResource:
@"icudtl" ofType:
@"dat"];
89 if (icuDataPath.length > 0) {
90 settings.icu_data_path = icuDataPath.UTF8String;
94 if (flutter::DartVM::IsRunningPrecompiledCode()) {
95 if (hasExplicitBundle) {
96 NSString* executablePath = bundle.executablePath;
97 if ([[NSFileManager defaultManager] fileExistsAtPath:executablePath]) {
98 settings.application_library_path.push_back(executablePath.UTF8String);
103 if (settings.application_library_path.empty()) {
104 NSString* libraryName = [mainBundle objectForInfoDictionaryKey:
@"FLTLibraryPath"];
105 NSString* libraryPath = [mainBundle pathForResource:libraryName ofType:
@""];
106 if (libraryPath.length > 0) {
107 NSString* executablePath = [NSBundle bundleWithPath:libraryPath].executablePath;
108 if (executablePath.length > 0) {
109 settings.application_library_path.push_back(executablePath.UTF8String);
116 if (settings.application_library_path.empty()) {
117 NSString* applicationFrameworkPath = [mainBundle pathForResource:
@"Frameworks/App.framework"
119 if (applicationFrameworkPath.length > 0) {
120 NSString* executablePath =
121 [NSBundle bundleWithPath:applicationFrameworkPath].executablePath;
122 if (executablePath.length > 0) {
123 settings.application_library_path.push_back(executablePath.UTF8String);
130 if (settings.assets_path.empty()) {
133 if (assetsPath.length == 0) {
134 NSLog(
@"Failed to find assets path for \"%@\
"", bundle);
136 settings.assets_path = assetsPath.UTF8String;
141 if (!flutter::DartVM::IsRunningPrecompiledCode()) {
142 NSURL* applicationKernelSnapshotURL =
144 relativeToURL:[NSURL fileURLWithPath:assetsPath]];
146 if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
147 settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
149 NSLog(
@"Failed to find snapshot at %@: %@", applicationKernelSnapshotURL.path, error);
158 settings.may_insecurely_connect_to_all_domains =
true;
159 settings.domain_network_policy =
"";
162 #if TARGET_OS_SIMULATOR
165 settings.enable_wide_gamut =
false;
169 NSNumber* nsEnableWideGamut = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableWideGamut"];
170 BOOL enableWideGamut =
172 settings.enable_wide_gamut = enableWideGamut;
175 NSNumber* nsAntialiasLines = [mainBundle objectForInfoDictionaryKey:
@"FLTAntialiasLines"];
176 settings.impeller_antialiased_lines = (nsAntialiasLines ? nsAntialiasLines.boolValue : NO);
178 settings.warn_on_impeller_opt_out =
true;
180 NSNumber* enableTraceSystrace = [mainBundle objectForInfoDictionaryKey:
@"FLTTraceSystrace"];
182 if (enableTraceSystrace != nil) {
183 settings.trace_systrace = enableTraceSystrace.boolValue;
186 NSNumber* profileMicrotasks = [mainBundle objectForInfoDictionaryKey:
@"FLTProfileMicrotasks"];
188 if (profileMicrotasks != nil) {
189 settings.profile_microtasks = profileMicrotasks.boolValue;
192 NSNumber* enableDartAsserts = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartAsserts"];
193 if (enableDartAsserts != nil) {
194 settings.dart_flags.push_back(
"--enable-asserts");
197 NSNumber* enableDartProfiling = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartProfiling"];
199 if (enableDartProfiling != nil) {
200 settings.enable_dart_profiling = enableDartProfiling.boolValue;
204 NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:
@"FLTLeakDartVM"];
206 if (leakDartVM != nil) {
207 settings.leak_vm = leakDartVM.boolValue;
210 NSNumber* enableMergedPlatformUIThread =
211 [mainBundle objectForInfoDictionaryKey:
@"FLTEnableMergedPlatformUIThread"];
212 if (enableMergedPlatformUIThread != nil) {
213 settings.merged_platform_ui_thread = enableMergedPlatformUIThread.boolValue
214 ? flutter::Settings::MergedPlatformUIThread::kEnabled
215 : flutter::Settings::MergedPlatformUIThread::kDisabled;
218 NSNumber* enableFlutterGPU = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableFlutterGPU"];
219 if (enableFlutterGPU != nil) {
220 settings.enable_flutter_gpu = enableFlutterGPU.boolValue;
223 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
226 auto make_mapping_callback = [](
const uint8_t* mapping,
size_t size) {
227 return [mapping, size]() {
return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
230 settings.dart_library_sources_kernel =
240 if (settings.old_gen_heap_size <= 0) {
241 settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
242 flutter::kMegaByteSizeInBytes);
247 CGFloat scale = [UIScreen mainScreen].scale;
248 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width * scale;
249 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * scale;
250 settings.resource_cache_max_bytes_threshold = screenWidth * screenHeight * 12 * 4;
253 NSNumber* enable_embedder_api =
254 [mainBundle objectForInfoDictionaryKey:
@"FLTEnableIOSEmbedderAPI"];
256 if (enable_embedder_api) {
257 settings.enable_embedder_api = enable_embedder_api.boolValue;
264 flutter::Settings _settings;
270 @dynamic dartEntrypointArguments;
272 #pragma mark - Override base class designated initializers
274 - (instancetype)init {
275 return [
self initWithPrecompiledDartBundle:nil];
278 #pragma mark - Designated initializers
280 - (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle {
290 - (instancetype)initWithSettings:(const
flutter::Settings&)settings {
291 self = [
self initWithPrecompiledDartBundle:nil];
294 _settings = settings;
300 #pragma mark - PlatformData accessors
302 - (const
flutter::PlatformData)defaultPlatformData {
303 flutter::PlatformData PlatformData;
304 PlatformData.lifecycle_state = std::string(
"AppLifecycleState.detached");
308 #pragma mark - Settings accessors
310 - (const
flutter::Settings&)settings {
314 - (
flutter::RunConfiguration)runConfiguration {
315 return [
self runConfigurationForEntrypoint:nil];
318 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil {
319 return [
self runConfigurationForEntrypoint:entrypointOrNil libraryOrNil:nil];
322 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
323 libraryOrNil:(nullable NSString*)dartLibraryOrNil {
324 return [
self runConfigurationForEntrypoint:entrypointOrNil
325 libraryOrNil:dartLibraryOrNil
329 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
330 libraryOrNil:(nullable NSString*)dartLibraryOrNil
332 (nullable NSArray<NSString*>*)entrypointArgs {
333 auto config = flutter::RunConfiguration::InferFromSettings(_settings);
334 if (dartLibraryOrNil && entrypointOrNil) {
335 config.SetEntrypointAndLibrary(std::string([entrypointOrNil UTF8String]),
336 std::string([dartLibraryOrNil UTF8String]));
338 }
else if (entrypointOrNil) {
339 config.SetEntrypoint(std::string([entrypointOrNil UTF8String]));
342 if (entrypointArgs.count) {
343 std::vector<std::string> cppEntrypointArgs;
344 for (NSString* arg in entrypointArgs) {
345 cppEntrypointArgs.push_back(std::string([arg UTF8String]));
347 config.SetEntrypointArgs(std::move(cppEntrypointArgs));
353 #pragma mark - Assets-related utilities
355 + (NSString*)flutterAssetsName:(NSBundle*)bundle {
362 + (NSString*)domainNetworkPolicy:(NSDictionary*)appTransportSecurity {
364 NSDictionary* exceptionDomains = appTransportSecurity[@"NSExceptionDomains"];
365 if (exceptionDomains == nil) {
368 NSMutableArray* networkConfigArray = [[NSMutableArray alloc] init];
369 for (NSString* domain in exceptionDomains) {
370 NSDictionary* domainConfiguration = exceptionDomains[domain];
372 bool includesSubDomains = [domainConfiguration[@"NSIncludesSubdomains"] boolValue];
373 bool allowsCleartextCommunication =
374 [domainConfiguration[@"NSExceptionAllowsInsecureHTTPLoads"] boolValue];
375 [networkConfigArray addObject:@[
376 domain, includesSubDomains ? @YES : @NO, allowsCleartextCommunication ? @YES : @NO
379 NSData* jsonData = [NSJSONSerialization dataWithJSONObject:networkConfigArray
382 return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
385 + (bool)allowsArbitraryLoads:(NSDictionary*)appTransportSecurity {
386 return [appTransportSecurity[@"NSAllowsArbitraryLoads"] boolValue];
389 + (NSString*)lookupKeyForAsset:(NSString*)asset {
390 return [
self lookupKeyForAsset:asset fromBundle:nil];
393 + (NSString*)lookupKeyForAsset:(NSString*)asset fromBundle:(nullable NSBundle*)bundle {
395 return [NSString stringWithFormat:@"%@/%@", flutterAssetsName, asset];
398 + (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
399 return [
self lookupKeyForAsset:asset fromPackage:package fromBundle:nil];
402 + (NSString*)lookupKeyForAsset:(NSString*)asset
403 fromPackage:(NSString*)package
404 fromBundle:(nullable NSBundle*)bundle {
405 return [
self lookupKeyForAsset:[NSString stringWithFormat:@"packages/%@/%@", package, asset]
409 + (NSString*)defaultBundleIdentifier {
410 return @"io.flutter.flutter.app";
413 - (BOOL)isWideGamutEnabled {
414 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)