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