5 #define FML_USED_ON_EMBEDDER
11 #import <Metal/Metal.h>
15 #include "flutter/common/constants.h"
16 #include "flutter/common/task_runners.h"
17 #include "flutter/fml/mapping.h"
18 #include "flutter/fml/message_loop.h"
19 #include "flutter/fml/platform/darwin/scoped_nsobject.h"
20 #include "flutter/runtime/dart_vm.h"
21 #include "flutter/shell/common/shell.h"
22 #include "flutter/shell/common/switches.h"
29 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
39 static BOOL result = NO;
40 static dispatch_once_t once_token = 0;
41 dispatch_once(&once_token, ^{
42 id<MTLDevice> device = MTLCreateSystemDefaultDevice();
43 if (@available(iOS 13.0, *)) {
45 result = [device supportsFamily:MTLGPUFamilyApple2];
48 result = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v2];
67 bool hasExplicitBundle = bundle != nil;
72 auto settings = flutter::SettingsFromCommandLine(command_line);
74 settings.task_observer_add = [](intptr_t key,
const fml::closure& callback) {
75 fml::MessageLoop::GetCurrent().AddTaskObserver(key, callback);
78 settings.task_observer_remove = [](intptr_t key) {
79 fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
82 settings.log_message_callback = [](
const std::string& tag,
const std::string& message) {
85 std::stringstream stream;
87 stream << tag <<
": ";
90 std::string log = stream.str();
91 syslog(LOG_ALERT,
"%.*s", (
int)log.size(), log.c_str());
98 if (settings.icu_data_path.empty()) {
99 NSString* icuDataPath = [engineBundle pathForResource:
@"icudtl" ofType:
@"dat"];
100 if (icuDataPath.length > 0) {
101 settings.icu_data_path = icuDataPath.UTF8String;
105 if (flutter::DartVM::IsRunningPrecompiledCode()) {
106 if (hasExplicitBundle) {
107 NSString* executablePath = bundle.executablePath;
108 if ([[NSFileManager defaultManager] fileExistsAtPath:executablePath]) {
109 settings.application_library_path.push_back(executablePath.UTF8String);
114 if (settings.application_library_path.empty()) {
115 NSString* libraryName = [mainBundle objectForInfoDictionaryKey:
@"FLTLibraryPath"];
116 NSString* libraryPath = [mainBundle pathForResource:libraryName ofType:
@""];
117 if (libraryPath.length > 0) {
118 NSString* executablePath = [NSBundle bundleWithPath:libraryPath].executablePath;
119 if (executablePath.length > 0) {
120 settings.application_library_path.push_back(executablePath.UTF8String);
127 if (settings.application_library_path.empty()) {
128 NSString* applicationFrameworkPath = [mainBundle pathForResource:
@"Frameworks/App.framework"
130 if (applicationFrameworkPath.length > 0) {
131 NSString* executablePath =
132 [NSBundle bundleWithPath:applicationFrameworkPath].executablePath;
133 if (executablePath.length > 0) {
134 settings.application_library_path.push_back(executablePath.UTF8String);
141 if (settings.assets_path.empty()) {
144 if (assetsPath.length == 0) {
145 NSLog(
@"Failed to find assets path for \"%@\
"", bundle);
147 settings.assets_path = assetsPath.UTF8String;
152 if (!flutter::DartVM::IsRunningPrecompiledCode()) {
153 NSURL* applicationKernelSnapshotURL =
155 relativeToURL:[NSURL fileURLWithPath:assetsPath]];
157 if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
158 settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
160 NSLog(
@"Failed to find snapshot at %@: %@", applicationKernelSnapshotURL.path, error);
169 settings.may_insecurely_connect_to_all_domains =
true;
170 settings.domain_network_policy =
"";
173 #if TARGET_OS_SIMULATOR
176 settings.enable_wide_gamut =
false;
180 NSNumber* nsEnableWideGamut = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableWideGamut"];
181 BOOL enableWideGamut =
183 settings.enable_wide_gamut = enableWideGamut;
190 if (!command_line.HasOption(
"enable-impeller")) {
192 NSNumber* enableImpeller = [bundle objectForInfoDictionaryKey:
@"FLTEnableImpeller"];
193 if (enableImpeller == nil) {
195 enableImpeller = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableImpeller"];
198 if (enableImpeller != nil) {
199 settings.enable_impeller = enableImpeller.boolValue;
203 NSNumber* enableTraceSystrace = [mainBundle objectForInfoDictionaryKey:
@"FLTTraceSystrace"];
205 if (enableTraceSystrace != nil) {
206 settings.trace_systrace = enableTraceSystrace.boolValue;
209 NSNumber* enableDartProfiling = [mainBundle objectForInfoDictionaryKey:
@"FLTEnableDartProfiling"];
211 if (enableDartProfiling != nil) {
212 settings.enable_dart_profiling = enableDartProfiling.boolValue;
216 NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:
@"FLTLeakDartVM"];
218 if (leakDartVM != nil) {
219 settings.leak_vm = leakDartVM.boolValue;
222 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
225 auto make_mapping_callback = [](
const uint8_t* mapping,
size_t size) {
226 return [mapping, size]() {
return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
229 settings.dart_library_sources_kernel =
231 #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
239 if (settings.old_gen_heap_size <= 0) {
240 settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
241 flutter::kMegaByteSizeInBytes);
246 CGFloat scale = [UIScreen mainScreen].scale;
247 CGFloat screenWidth = [UIScreen mainScreen].bounds.size.width * scale;
248 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height * scale;
249 settings.resource_cache_max_bytes_threshold = screenWidth * screenHeight * 12 * 4;
252 NSNumber* enable_embedder_api =
253 [mainBundle objectForInfoDictionaryKey:
@"FLTEnableIOSEmbedderAPI"];
255 if (enable_embedder_api) {
256 settings.enable_embedder_api = enable_embedder_api.boolValue;
263 flutter::Settings _settings;
269 @dynamic dartEntrypointArguments;
271 #pragma mark - Override base class designated initializers
273 - (instancetype)init {
274 return [
self initWithPrecompiledDartBundle:nil];
277 #pragma mark - Designated initializers
279 - (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle {
289 - (instancetype)initWithSettings:(const
flutter::Settings&)settings {
290 self = [
self initWithPrecompiledDartBundle:nil];
293 _settings = settings;
299 #pragma mark - PlatformData accessors
301 - (const
flutter::PlatformData)defaultPlatformData {
302 flutter::PlatformData PlatformData;
303 PlatformData.lifecycle_state = std::string(
"AppLifecycleState.detached");
307 #pragma mark - Settings accessors
309 - (const
flutter::Settings&)settings {
313 - (
flutter::RunConfiguration)runConfiguration {
314 return [
self runConfigurationForEntrypoint:nil];
317 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil {
318 return [
self runConfigurationForEntrypoint:entrypointOrNil libraryOrNil:nil];
321 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
322 libraryOrNil:(nullable NSString*)dartLibraryOrNil {
323 return [
self runConfigurationForEntrypoint:entrypointOrNil
324 libraryOrNil:dartLibraryOrNil
328 - (
flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
329 libraryOrNil:(nullable NSString*)dartLibraryOrNil
331 (nullable NSArray<NSString*>*)entrypointArgs {
332 auto config = flutter::RunConfiguration::InferFromSettings(_settings);
333 if (dartLibraryOrNil && entrypointOrNil) {
334 config.SetEntrypointAndLibrary(std::string([entrypointOrNil UTF8String]),
335 std::string([dartLibraryOrNil UTF8String]));
337 }
else if (entrypointOrNil) {
338 config.SetEntrypoint(std::string([entrypointOrNil UTF8String]));
341 if (entrypointArgs.count) {
342 std::vector<std::string> cppEntrypointArgs;
343 for (NSString* arg in entrypointArgs) {
344 cppEntrypointArgs.push_back(std::string([arg UTF8String]));
346 config.SetEntrypointArgs(std::move(cppEntrypointArgs));
352 #pragma mark - Assets-related utilities
354 + (NSString*)flutterAssetsName:(NSBundle*)bundle {
361 + (NSString*)domainNetworkPolicy:(NSDictionary*)appTransportSecurity {
363 NSDictionary* exceptionDomains = [appTransportSecurity objectForKey:@"NSExceptionDomains"];
364 if (exceptionDomains == nil) {
367 NSMutableArray* networkConfigArray = [[[NSMutableArray alloc] init] autorelease];
368 for (NSString* domain in exceptionDomains) {
369 NSDictionary* domainConfiguration = [exceptionDomains objectForKey:domain];
371 bool includesSubDomains =
372 [[domainConfiguration objectForKey:@"NSIncludesSubdomains"] boolValue];
373 bool allowsCleartextCommunication =
374 [[domainConfiguration objectForKey:@"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] autorelease];
385 + (bool)allowsArbitraryLoads:(NSDictionary*)appTransportSecurity {
386 return [[appTransportSecurity objectForKey:@"NSAllowsArbitraryLoads"] boolValue];
389 + (NSString*)lookupKeyForAsset:(NSString*)asset {
393 + (NSString*)lookupKeyForAsset:(NSString*)asset fromBundle:(nullable NSBundle*)bundle {
395 return [NSString stringWithFormat:@"%@/%@", flutterAssetsName, asset];
398 + (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
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;
417 - (BOOL)isImpellerEnabled {
418 return _settings.enable_impeller;