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