Flutter iOS Embedder
FlutterDartProject.mm
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #define FML_USED_ON_EMBEDDER
6 
8 
9 #import <Metal/Metal.h>
10 #import <UIKit/UIKit.h>
11 
12 #include <syslog.h>
13 
14 #include "flutter/common/constants.h"
15 #include "flutter/shell/common/switches.h"
17 
19 
20 extern "C" {
21 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
22 // Used for debugging dart:* sources.
23 extern const uint8_t kPlatformStrongDill[];
24 extern const intptr_t kPlatformStrongDillSize;
25 #endif
26 }
27 
28 static const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin";
29 
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, *)) {
36  // MTLGPUFamilyApple2 = A9/A10
37  result = [device supportsFamily:MTLGPUFamilyApple2];
38  } else {
39  // A9/A10 on iOS 10+
40  result = [device supportsFeatureSet:MTLFeatureSet_iOS_GPUFamily3_v2];
41  }
42  });
43  return result;
44 }
45 
46 flutter::Settings FLTDefaultSettingsForBundle(NSBundle* bundle, NSProcessInfo* processInfoOrNil) {
47  auto command_line = flutter::CommandLineFromNSProcessInfo(processInfoOrNil);
48 
49  // Precedence:
50  // 1. Settings from the specified NSBundle (except for enable-impeller).
51  // 2. Settings passed explicitly via command-line arguments.
52  // 3. Settings from the NSBundle with the default bundle ID.
53  // 4. Settings from the main NSBundle and default values.
54 
55  NSBundle* mainBundle = FLTGetApplicationBundle();
56  NSBundle* engineBundle = [NSBundle bundleForClass:[FlutterDartProject class]];
57 
58  bool hasExplicitBundle = bundle != nil;
59  if (bundle == nil) {
60  bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]);
61  }
62 
63  auto settings = flutter::SettingsFromCommandLine(command_line);
64 
65  settings.task_observer_add = [](intptr_t key, const fml::closure& callback) {
66  fml::MessageLoop::GetCurrent().AddTaskObserver(key, callback);
67  };
68 
69  settings.task_observer_remove = [](intptr_t key) {
70  fml::MessageLoop::GetCurrent().RemoveTaskObserver(key);
71  };
72 
73  settings.log_message_callback = [](const std::string& tag, const std::string& message) {
74  // TODO(cbracken): replace this with os_log-based approach.
75  // https://github.com/flutter/flutter/issues/44030
76  std::stringstream stream;
77  if (!tag.empty()) {
78  stream << tag << ": ";
79  }
80  stream << message;
81  std::string log = stream.str();
82  syslog(LOG_ALERT, "%.*s", (int)log.size(), log.c_str());
83  };
84 
85  settings.enable_platform_isolates = true;
86 
87  // The command line arguments may not always be complete. If they aren't, attempt to fill in
88  // defaults.
89 
90  // Flutter ships the ICU data file in the bundle of the engine. Look for it there.
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;
95  }
96  }
97 
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);
103  }
104  }
105 
106  // No application bundle specified. Try a known location from the main bundle's Info.plist.
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);
114  }
115  }
116  }
117 
118  // In case the application bundle is still not specified, look for the App.framework in the
119  // Frameworks directory.
120  if (settings.application_library_path.empty()) {
121  NSString* applicationFrameworkPath = [mainBundle pathForResource:@"Frameworks/App.framework"
122  ofType:@""];
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);
128  }
129  }
130  }
131  }
132 
133  // Checks to see if the flutter assets directory is already present.
134  if (settings.assets_path.empty()) {
135  NSString* assetsPath = FLTAssetsPathFromBundle(bundle);
136 
137  if (assetsPath.length == 0) {
138  NSLog(@"Failed to find assets path for \"%@\"", bundle);
139  } else {
140  settings.assets_path = assetsPath.UTF8String;
141 
142  // Check if there is an application kernel snapshot in the assets directory we could
143  // potentially use. Looking for the snapshot makes sense only if we have a VM that can use
144  // it.
145  if (!flutter::DartVM::IsRunningPrecompiledCode()) {
146  NSURL* applicationKernelSnapshotURL =
147  [NSURL URLWithString:@(kApplicationKernelSnapshotFileName)
148  relativeToURL:[NSURL fileURLWithPath:assetsPath]];
149  NSError* error;
150  if ([applicationKernelSnapshotURL checkResourceIsReachableAndReturnError:&error]) {
151  settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
152  } else {
153  NSLog(@"Failed to find snapshot at %@: %@", applicationKernelSnapshotURL.path, error);
154  }
155  }
156  }
157  }
158 
159  // Domain network configuration
160  // Disabled in https://github.com/flutter/flutter/issues/72723.
161  // Re-enable in https://github.com/flutter/flutter/issues/54448.
162  settings.may_insecurely_connect_to_all_domains = true;
163  settings.domain_network_policy = "";
164 
165  // Whether to enable wide gamut colors.
166 #if TARGET_OS_SIMULATOR
167  // As of Xcode 14.1, the wide gamut surface pixel formats are not supported by
168  // the simulator.
169  settings.enable_wide_gamut = false;
170  // Removes unused function warning.
172 #else
173  NSNumber* nsEnableWideGamut = [mainBundle objectForInfoDictionaryKey:@"FLTEnableWideGamut"];
174  BOOL enableWideGamut =
175  (nsEnableWideGamut ? nsEnableWideGamut.boolValue : YES) && DoesHardwareSupportWideGamut();
176  settings.enable_wide_gamut = enableWideGamut;
177 #endif
178 
179  if (!command_line.HasOption("enable-impeller")) {
180  // Next, look in the app bundle.
181  NSNumber* enableImpeller = [bundle objectForInfoDictionaryKey:@"FLTEnableImpeller"];
182  if (enableImpeller == nil) {
183  // If it isn't in the app bundle, look in the main bundle.
184  enableImpeller = [mainBundle objectForInfoDictionaryKey:@"FLTEnableImpeller"];
185  }
186  // Change the default only if the option is present.
187  if (enableImpeller != nil) {
188  settings.enable_impeller = enableImpeller.boolValue;
189  }
190  }
191 
192  settings.warn_on_impeller_opt_out = true;
193 
194  NSNumber* enableTraceSystrace = [mainBundle objectForInfoDictionaryKey:@"FLTTraceSystrace"];
195  // Change the default only if the option is present.
196  if (enableTraceSystrace != nil) {
197  settings.trace_systrace = enableTraceSystrace.boolValue;
198  }
199 
200  NSNumber* enableDartAsserts = [mainBundle objectForInfoDictionaryKey:@"FLTEnableDartAsserts"];
201  if (enableDartAsserts != nil) {
202  settings.dart_flags.push_back("--enable-asserts");
203  }
204 
205  NSNumber* enableDartProfiling = [mainBundle objectForInfoDictionaryKey:@"FLTEnableDartProfiling"];
206  // Change the default only if the option is present.
207  if (enableDartProfiling != nil) {
208  settings.enable_dart_profiling = enableDartProfiling.boolValue;
209  }
210 
211  // Leak Dart VM settings, set whether leave or clean up the VM after the last shell shuts down.
212  NSNumber* leakDartVM = [mainBundle objectForInfoDictionaryKey:@"FLTLeakDartVM"];
213  // It will change the default leak_vm value in settings only if the key exists.
214  if (leakDartVM != nil) {
215  settings.leak_vm = leakDartVM.boolValue;
216  }
217 
218 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
219  // There are no ownership concerns here as all mappings are owned by the
220  // embedder and not the engine.
221  auto make_mapping_callback = [](const uint8_t* mapping, size_t size) {
222  return [mapping, size]() { return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
223  };
224 
225  settings.dart_library_sources_kernel =
226  make_mapping_callback(kPlatformStrongDill, kPlatformStrongDillSize);
227 #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
228 
229  // If we even support setting this e.g. from the command line or the plist,
230  // we should let the user override it.
231  // Otherwise, we want to set this to a value that will avoid having the OS
232  // kill us. On most iOS devices, that happens somewhere near half
233  // the available memory.
234  // The VM expects this value to be in megabytes.
235  if (settings.old_gen_heap_size <= 0) {
236  settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
237  flutter::kMegaByteSizeInBytes);
238  }
239 
240  // This is the formula Android uses.
241  // https://android.googlesource.com/platform/frameworks/base/+/39ae5bac216757bc201490f4c7b8c0f63006c6cd/libs/hwui/renderthread/CacheManager.cpp#45
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;
246 
247  // Whether to enable ios embedder api.
248  NSNumber* enable_embedder_api =
249  [mainBundle objectForInfoDictionaryKey:@"FLTEnableIOSEmbedderAPI"];
250  // Change the default only if the option is present.
251  if (enable_embedder_api) {
252  settings.enable_embedder_api = enable_embedder_api.boolValue;
253  }
254 
255  return settings;
256 }
257 
258 @implementation FlutterDartProject {
259  flutter::Settings _settings;
260 }
261 
262 // This property is marked unavailable on iOS in the common header.
263 // That doesn't seem to be enough to prevent this property from being synthesized.
264 // Mark dynamic to avoid warnings.
265 @dynamic dartEntrypointArguments;
266 
267 #pragma mark - Override base class designated initializers
268 
269 - (instancetype)init {
270  return [self initWithPrecompiledDartBundle:nil];
271 }
272 
273 #pragma mark - Designated initializers
274 
275 - (instancetype)initWithPrecompiledDartBundle:(nullable NSBundle*)bundle {
276  self = [super init];
277 
278  if (self) {
279  _settings = FLTDefaultSettingsForBundle(bundle);
280  }
281 
282  return self;
283 }
284 
285 - (instancetype)initWithSettings:(const flutter::Settings&)settings {
286  self = [self initWithPrecompiledDartBundle:nil];
287 
288  if (self) {
289  _settings = settings;
290  }
291 
292  return self;
293 }
294 
295 #pragma mark - PlatformData accessors
296 
297 - (const flutter::PlatformData)defaultPlatformData {
298  flutter::PlatformData PlatformData;
299  PlatformData.lifecycle_state = std::string("AppLifecycleState.detached");
300  return PlatformData;
301 }
302 
303 #pragma mark - Settings accessors
304 
305 - (const flutter::Settings&)settings {
306  return _settings;
307 }
308 
309 - (flutter::RunConfiguration)runConfiguration {
310  return [self runConfigurationForEntrypoint:nil];
311 }
312 
313 - (flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil {
314  return [self runConfigurationForEntrypoint:entrypointOrNil libraryOrNil:nil];
315 }
316 
317 - (flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
318  libraryOrNil:(nullable NSString*)dartLibraryOrNil {
319  return [self runConfigurationForEntrypoint:entrypointOrNil
320  libraryOrNil:dartLibraryOrNil
321  entrypointArgs:nil];
322 }
323 
324 - (flutter::RunConfiguration)runConfigurationForEntrypoint:(nullable NSString*)entrypointOrNil
325  libraryOrNil:(nullable NSString*)dartLibraryOrNil
326  entrypointArgs:
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]));
332 
333  } else if (entrypointOrNil) {
334  config.SetEntrypoint(std::string([entrypointOrNil UTF8String]));
335  }
336 
337  if (entrypointArgs.count) {
338  std::vector<std::string> cppEntrypointArgs;
339  for (NSString* arg in entrypointArgs) {
340  cppEntrypointArgs.push_back(std::string([arg UTF8String]));
341  }
342  config.SetEntrypointArgs(std::move(cppEntrypointArgs));
343  }
344 
345  return config;
346 }
347 
348 #pragma mark - Assets-related utilities
349 
350 + (NSString*)flutterAssetsName:(NSBundle*)bundle {
351  if (bundle == nil) {
352  bundle = FLTFrameworkBundleWithIdentifier([FlutterDartProject defaultBundleIdentifier]);
353  }
354  return FLTAssetPath(bundle);
355 }
356 
357 + (NSString*)domainNetworkPolicy:(NSDictionary*)appTransportSecurity {
358  // https://developer.apple.com/documentation/bundleresources/information_property_list/nsapptransportsecurity/nsexceptiondomains
359  NSDictionary* exceptionDomains = appTransportSecurity[@"NSExceptionDomains"];
360  if (exceptionDomains == nil) {
361  return @"";
362  }
363  NSMutableArray* networkConfigArray = [[NSMutableArray alloc] init];
364  for (NSString* domain in exceptionDomains) {
365  NSDictionary* domainConfiguration = exceptionDomains[domain];
366  // Default value is false.
367  bool includesSubDomains = [domainConfiguration[@"NSIncludesSubdomains"] boolValue];
368  bool allowsCleartextCommunication =
369  [domainConfiguration[@"NSExceptionAllowsInsecureHTTPLoads"] boolValue];
370  [networkConfigArray addObject:@[
371  domain, includesSubDomains ? @YES : @NO, allowsCleartextCommunication ? @YES : @NO
372  ]];
373  }
374  NSData* jsonData = [NSJSONSerialization dataWithJSONObject:networkConfigArray
375  options:0
376  error:NULL];
377  return [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
378 }
379 
380 + (bool)allowsArbitraryLoads:(NSDictionary*)appTransportSecurity {
381  return [appTransportSecurity[@"NSAllowsArbitraryLoads"] boolValue];
382 }
383 
384 + (NSString*)lookupKeyForAsset:(NSString*)asset {
385  return [self lookupKeyForAsset:asset fromBundle:nil];
386 }
387 
388 + (NSString*)lookupKeyForAsset:(NSString*)asset fromBundle:(nullable NSBundle*)bundle {
389  NSString* flutterAssetsName = [FlutterDartProject flutterAssetsName:bundle];
390  return [NSString stringWithFormat:@"%@/%@", flutterAssetsName, asset];
391 }
392 
393 + (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
394  return [self lookupKeyForAsset:asset fromPackage:package fromBundle:nil];
395 }
396 
397 + (NSString*)lookupKeyForAsset:(NSString*)asset
398  fromPackage:(NSString*)package
399  fromBundle:(nullable NSBundle*)bundle {
400  return [self lookupKeyForAsset:[NSString stringWithFormat:@"packages/%@/%@", package, asset]
401  fromBundle:bundle];
402 }
403 
404 + (NSString*)defaultBundleIdentifier {
405  return @"io.flutter.flutter.app";
406 }
407 
408 - (BOOL)isWideGamutEnabled {
409  return _settings.enable_wide_gamut;
410 }
411 
412 - (BOOL)isImpellerEnabled {
413  return _settings.enable_impeller;
414 }
415 
416 @end
+[FlutterDartProject lookupKeyForAsset:fromPackage:fromBundle:]
NSString * lookupKeyForAsset:fromPackage:fromBundle:(NSString *asset,[fromPackage] NSString *package,[fromBundle] nullable NSBundle *bundle)
Definition: FlutterDartProject.mm:397
kApplicationKernelSnapshotFileName
static const char * kApplicationKernelSnapshotFileName
Definition: FlutterDartProject.mm:28
kPlatformStrongDillSize
const intptr_t kPlatformStrongDillSize
command_line.h
kPlatformStrongDill
const FLUTTER_ASSERT_ARC uint8_t kPlatformStrongDill[]
DoesHardwareSupportWideGamut
static BOOL DoesHardwareSupportWideGamut()
Definition: FlutterDartProject.mm:30
FLTAssetPath
NSString * FLTAssetPath(NSBundle *bundle)
Definition: FlutterNSBundleUtils.mm:57
FLTGetApplicationBundle
NSBundle * FLTGetApplicationBundle()
Definition: FlutterNSBundleUtils.mm:32
flutter
Definition: accessibility_bridge.h:28
flutter::CommandLineFromNSProcessInfo
fml::CommandLine CommandLineFromNSProcessInfo(NSProcessInfo *processInfoOrNil=nil)
Definition: command_line.mm:11
FLTFrameworkBundleWithIdentifier
NSBundle * FLTFrameworkBundleWithIdentifier(NSString *flutterFrameworkBundleID)
Definition: FlutterNSBundleUtils.mm:43
FlutterDartProject_Internal.h
+[FlutterDartProject flutterAssetsName:]
NSString * flutterAssetsName:(NSBundle *bundle)
FLTDefaultSettingsForBundle
flutter::Settings FLTDefaultSettingsForBundle(NSBundle *bundle, NSProcessInfo *processInfoOrNil)
Definition: FlutterDartProject.mm:46
FLTAssetsPathFromBundle
NSString * FLTAssetsPathFromBundle(NSBundle *bundle)
Definition: FlutterNSBundleUtils.mm:61
FlutterDartProject
Definition: FlutterDartProject.mm:258
+[FlutterDartProject lookupKeyForAsset:fromBundle:]
NSString * lookupKeyForAsset:fromBundle:(NSString *asset,[fromBundle] nullable NSBundle *bundle)
Definition: FlutterDartProject.mm:388
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13