Flutter Engine
FlutterDartProject.mm File Reference
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
#include "flutter/common/constants.h"
#include "flutter/common/task_runners.h"
#include "flutter/fml/mapping.h"
#include "flutter/fml/message_loop.h"
#include "flutter/fml/platform/darwin/scoped_nsobject.h"
#include "flutter/runtime/dart_vm.h"
#include "flutter/shell/common/shell.h"
#include "flutter/shell/common/switches.h"
#import "flutter/shell/platform/darwin/common/command_line.h"
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h"

Go to the source code of this file.

Macros

#define FML_USED_ON_EMBEDDER
 

Functions

static flutter::Settings DefaultSettingsForProcess (NSBundle *bundle=nil)
 

Variables

const uint8_t kPlatformStrongDill []
 
const intptr_t kPlatformStrongDillSize
 
static const char * kApplicationKernelSnapshotFileName = "kernel_blob.bin"
 

Macro Definition Documentation

◆ FML_USED_ON_EMBEDDER

#define FML_USED_ON_EMBEDDER

Definition at line 5 of file FlutterDartProject.mm.

Function Documentation

◆ DefaultSettingsForProcess()

static flutter::Settings DefaultSettingsForProcess ( NSBundle *  bundle = nil)
static

Definition at line 30 of file FlutterDartProject.mm.

References fml::MessageLoop::AddTaskObserver(), flutter::CommandLineFromNSProcessInfo(), fml::MessageLoop::GetCurrent(), flutter::DartVM::IsRunningPrecompiledCode(), kApplicationKernelSnapshotFileName, flutter::kMegaByteSizeInBytes, kPlatformStrongDill, kPlatformStrongDillSize, fml::MessageLoop::RemoveTaskObserver(), flutter::SettingsFromCommandLine(), and fml::size().

Referenced by FlutterDartProject::initWithPrecompiledDartBundle:.

30  {
31  auto command_line = flutter::CommandLineFromNSProcessInfo();
32 
33  // Precedence:
34  // 1. Settings from the specified NSBundle.
35  // 2. Settings passed explicitly via command-line arguments.
36  // 3. Settings from the NSBundle with the default bundle ID.
37  // 4. Settings from the main NSBundle and default values.
38 
39  NSBundle* mainBundle = [NSBundle mainBundle];
40  NSBundle* engineBundle = [NSBundle bundleForClass:[FlutterViewController class]];
41 
42  bool hasExplicitBundle = bundle != nil;
43  if (bundle == nil) {
44  bundle = [NSBundle bundleWithIdentifier:[FlutterDartProject defaultBundleIdentifier]];
45  }
46  if (bundle == nil) {
47  bundle = mainBundle;
48  }
49 
50  auto settings = flutter::SettingsFromCommandLine(command_line);
51 
52  settings.task_observer_add = [](intptr_t key, fml::closure callback) {
53  fml::MessageLoop::GetCurrent().AddTaskObserver(key, std::move(callback));
54  };
55 
56  settings.task_observer_remove = [](intptr_t key) {
58  };
59 
60  // The command line arguments may not always be complete. If they aren't, attempt to fill in
61  // defaults.
62 
63  // Flutter ships the ICU data file in the bundle of the engine. Look for it there.
64  if (settings.icu_data_path.size() == 0) {
65  NSString* icuDataPath = [engineBundle pathForResource:@"icudtl" ofType:@"dat"];
66  if (icuDataPath.length > 0) {
67  settings.icu_data_path = icuDataPath.UTF8String;
68  }
69  }
70 
72  if (hasExplicitBundle) {
73  NSString* executablePath = bundle.executablePath;
74  if ([[NSFileManager defaultManager] fileExistsAtPath:executablePath]) {
75  settings.application_library_path.push_back(executablePath.UTF8String);
76  }
77  }
78 
79  // No application bundle specified. Try a known location from the main bundle's Info.plist.
80  if (settings.application_library_path.size() == 0) {
81  NSString* libraryName = [mainBundle objectForInfoDictionaryKey:@"FLTLibraryPath"];
82  NSString* libraryPath = [mainBundle pathForResource:libraryName ofType:@""];
83  if (libraryPath.length > 0) {
84  NSString* executablePath = [NSBundle bundleWithPath:libraryPath].executablePath;
85  if (executablePath.length > 0) {
86  settings.application_library_path.push_back(executablePath.UTF8String);
87  }
88  }
89  }
90 
91  // In case the application bundle is still not specified, look for the App.framework in the
92  // Frameworks directory.
93  if (settings.application_library_path.size() == 0) {
94  NSString* applicationFrameworkPath = [mainBundle pathForResource:@"Frameworks/App.framework"
95  ofType:@""];
96  if (applicationFrameworkPath.length > 0) {
97  NSString* executablePath =
98  [NSBundle bundleWithPath:applicationFrameworkPath].executablePath;
99  if (executablePath.length > 0) {
100  settings.application_library_path.push_back(executablePath.UTF8String);
101  }
102  }
103  }
104  }
105 
106  // Checks to see if the flutter assets directory is already present.
107  if (settings.assets_path.size() == 0) {
108  NSString* assetsName = [FlutterDartProject flutterAssetsName:bundle];
109  NSString* assetsPath = [bundle pathForResource:assetsName ofType:@""];
110 
111  if (assetsPath.length == 0) {
112  assetsPath = [mainBundle pathForResource:assetsName ofType:@""];
113  }
114 
115  if (assetsPath.length == 0) {
116  NSLog(@"Failed to find assets path for \"%@\"", assetsName);
117  } else {
118  settings.assets_path = assetsPath.UTF8String;
119 
120  // Check if there is an application kernel snapshot in the assets directory we could
121  // potentially use. Looking for the snapshot makes sense only if we have a VM that can use
122  // it.
124  NSURL* applicationKernelSnapshotURL =
125  [NSURL URLWithString:@(kApplicationKernelSnapshotFileName)
126  relativeToURL:[NSURL fileURLWithPath:assetsPath]];
127  if ([[NSFileManager defaultManager] fileExistsAtPath:applicationKernelSnapshotURL.path]) {
128  settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
129  } else {
130  NSLog(@"Failed to find snapshot: %@", applicationKernelSnapshotURL.path);
131  }
132  }
133  }
134  }
135 
136  // Domain network configuration
137  NSDictionary* appTransportSecurity =
138  [mainBundle objectForInfoDictionaryKey:@"NSAppTransportSecurity"];
139  settings.may_insecurely_connect_to_all_domains =
140  [FlutterDartProject allowsArbitraryLoads:appTransportSecurity];
141  settings.domain_network_policy =
142  [FlutterDartProject domainNetworkPolicy:appTransportSecurity].UTF8String;
143 
144 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
145  // There are no ownership concerns here as all mappings are owned by the
146  // embedder and not the engine.
147  auto make_mapping_callback = [](const uint8_t* mapping, size_t size) {
148  return [mapping, size]() { return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
149  };
150 
151  settings.dart_library_sources_kernel =
152  make_mapping_callback(kPlatformStrongDill, kPlatformStrongDillSize);
153 #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
154 
155  // If we even support setting this e.g. from the command line or the plist,
156  // we should let the user override it.
157  // Otherwise, we want to set this to a value that will avoid having the OS
158  // kill us. On most iOS devices, that happens somewhere near half
159  // the available memory.
160  // The VM expects this value to be in megabytes.
161  if (settings.old_gen_heap_size <= 0) {
162  settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
164  }
165  return settings;
166 }
constexpr double kMegaByteSizeInBytes
Definition: constants.h:6
void AddTaskObserver(intptr_t key, const fml::closure &callback)
Definition: message_loop.cc:64
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
Definition: message_loop.cc:19
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
Settings SettingsFromCommandLine(const fml::CommandLine &command_line)
Definition: switches.cc:216
const uint8_t kPlatformStrongDill[]
static const char * kApplicationKernelSnapshotFileName
fml::CommandLine CommandLineFromNSProcessInfo()
Definition: command_line.mm:11
std::function< void()> closure
Definition: closure.h:14
const intptr_t kPlatformStrongDillSize
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:199
void RemoveTaskObserver(intptr_t key)
Definition: message_loop.cc:68

Variable Documentation

◆ kApplicationKernelSnapshotFileName

const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin"
static

Definition at line 28 of file FlutterDartProject.mm.

Referenced by DefaultSettingsForProcess(), and FlutterEngineInitialize().

◆ kPlatformStrongDill

const uint8_t kPlatformStrongDill[]

◆ kPlatformStrongDillSize

const intptr_t kPlatformStrongDillSize