Flutter Engine
FlutterDartProject.mm File Reference
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
#include <syslog.h>
#include <sstream>
#include <string>
#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

flutter::Settings FLTDefaultSettingsForBundle (NSBundle *bundle)
 

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

◆ FLTDefaultSettingsForBundle()

flutter::Settings FLTDefaultSettingsForBundle ( NSBundle *  bundle)

Definition at line 35 of file FlutterDartProject.mm.

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

Referenced by FlutterDartProject::initWithPrecompiledDartBundle:.

35  {
36  auto command_line = flutter::CommandLineFromNSProcessInfo();
37 
38  // Precedence:
39  // 1. Settings from the specified NSBundle.
40  // 2. Settings passed explicitly via command-line arguments.
41  // 3. Settings from the NSBundle with the default bundle ID.
42  // 4. Settings from the main NSBundle and default values.
43 
44  NSBundle* mainBundle = [NSBundle mainBundle];
45  NSBundle* engineBundle = [NSBundle bundleForClass:[FlutterViewController class]];
46 
47  bool hasExplicitBundle = bundle != nil;
48  if (bundle == nil) {
49  bundle = [NSBundle bundleWithIdentifier:[FlutterDartProject defaultBundleIdentifier]];
50  }
51  if (bundle == nil) {
52  bundle = mainBundle;
53  }
54 
55  auto settings = flutter::SettingsFromCommandLine(command_line);
56 
57  settings.task_observer_add = [](intptr_t key, fml::closure callback) {
59  };
60 
61  settings.task_observer_remove = [](intptr_t key) {
63  };
64 
65  settings.log_message_callback = [](const std::string& tag, const std::string& message) {
66  // TODO(cbracken): replace this with os_log-based approach.
67  // https://github.com/flutter/flutter/issues/44030
68  std::stringstream stream;
69  if (tag.size() > 0) {
70  stream << tag << ": ";
71  }
72  stream << message;
73  std::string log = stream.str();
74  syslog(LOG_ALERT, "%.*s", (int)log.size(), log.c_str());
75  };
76 
77  // The command line arguments may not always be complete. If they aren't, attempt to fill in
78  // defaults.
79 
80  // Flutter ships the ICU data file in the bundle of the engine. Look for it there.
81  if (settings.icu_data_path.size() == 0) {
82  NSString* icuDataPath = [engineBundle pathForResource:@"icudtl" ofType:@"dat"];
83  if (icuDataPath.length > 0) {
84  settings.icu_data_path = icuDataPath.UTF8String;
85  }
86  }
87 
89  if (hasExplicitBundle) {
90  NSString* executablePath = bundle.executablePath;
91  if ([[NSFileManager defaultManager] fileExistsAtPath:executablePath]) {
92  settings.application_library_path.push_back(executablePath.UTF8String);
93  }
94  }
95 
96  // No application bundle specified. Try a known location from the main bundle's Info.plist.
97  if (settings.application_library_path.size() == 0) {
98  NSString* libraryName = [mainBundle objectForInfoDictionaryKey:@"FLTLibraryPath"];
99  NSString* libraryPath = [mainBundle pathForResource:libraryName ofType:@""];
100  if (libraryPath.length > 0) {
101  NSString* executablePath = [NSBundle bundleWithPath:libraryPath].executablePath;
102  if (executablePath.length > 0) {
103  settings.application_library_path.push_back(executablePath.UTF8String);
104  }
105  }
106  }
107 
108  // In case the application bundle is still not specified, look for the App.framework in the
109  // Frameworks directory.
110  if (settings.application_library_path.size() == 0) {
111  NSString* applicationFrameworkPath = [mainBundle pathForResource:@"Frameworks/App.framework"
112  ofType:@""];
113  if (applicationFrameworkPath.length > 0) {
114  NSString* executablePath =
115  [NSBundle bundleWithPath:applicationFrameworkPath].executablePath;
116  if (executablePath.length > 0) {
117  settings.application_library_path.push_back(executablePath.UTF8String);
118  }
119  }
120  }
121  }
122 
123  // Checks to see if the flutter assets directory is already present.
124  if (settings.assets_path.size() == 0) {
125  NSString* assetsName = [FlutterDartProject flutterAssetsName:bundle];
126  NSString* assetsPath = [bundle pathForResource:assetsName ofType:@""];
127 
128  if (assetsPath.length == 0) {
129  assetsPath = [mainBundle pathForResource:assetsName ofType:@""];
130  }
131 
132  if (assetsPath.length == 0) {
133  NSLog(@"Failed to find assets path for \"%@\"", assetsName);
134  } else {
135  settings.assets_path = assetsPath.UTF8String;
136 
137  // Check if there is an application kernel snapshot in the assets directory we could
138  // potentially use. Looking for the snapshot makes sense only if we have a VM that can use
139  // it.
141  NSURL* applicationKernelSnapshotURL =
142  [NSURL URLWithString:@(kApplicationKernelSnapshotFileName)
143  relativeToURL:[NSURL fileURLWithPath:assetsPath]];
144  if ([[NSFileManager defaultManager] fileExistsAtPath:applicationKernelSnapshotURL.path]) {
145  settings.application_kernel_asset = applicationKernelSnapshotURL.path.UTF8String;
146  } else {
147  NSLog(@"Failed to find snapshot: %@", applicationKernelSnapshotURL.path);
148  }
149  }
150  }
151  }
152 
153  // Domain network configuration
154  // Disabled in https://github.com/flutter/flutter/issues/72723.
155  // Re-enable in https://github.com/flutter/flutter/issues/54448.
156  settings.may_insecurely_connect_to_all_domains = true;
157  settings.domain_network_policy = "";
158 
159  // SkParagraph text layout library
160  NSNumber* enableSkParagraph = [mainBundle objectForInfoDictionaryKey:@"FLTEnableSkParagraph"];
161  settings.enable_skparagraph = (enableSkParagraph != nil) ? enableSkParagraph.boolValue : false;
162 
163 #if FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
164  // There are no ownership concerns here as all mappings are owned by the
165  // embedder and not the engine.
166  auto make_mapping_callback = [](const uint8_t* mapping, size_t size) {
167  return [mapping, size]() { return std::make_unique<fml::NonOwnedMapping>(mapping, size); };
168  };
169 
170  settings.dart_library_sources_kernel =
171  make_mapping_callback(kPlatformStrongDill, kPlatformStrongDillSize);
172 #endif // FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG
173 
174  // If we even support setting this e.g. from the command line or the plist,
175  // we should let the user override it.
176  // Otherwise, we want to set this to a value that will avoid having the OS
177  // kill us. On most iOS devices, that happens somewhere near half
178  // the available memory.
179  // The VM expects this value to be in megabytes.
180  if (settings.old_gen_heap_size <= 0) {
181  settings.old_gen_heap_size = std::round([NSProcessInfo processInfo].physicalMemory * .48 /
183  }
184  return settings;
185 }
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:228
const uint8_t kPlatformStrongDill[]
FlKeyEvent FlKeyResponderAsyncCallback callback
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:202
void RemoveTaskObserver(intptr_t key)
Definition: message_loop.cc:68

Variable Documentation

◆ kApplicationKernelSnapshotFileName

const char* kApplicationKernelSnapshotFileName = "kernel_blob.bin"
static

Definition at line 33 of file FlutterDartProject.mm.

Referenced by FLTDefaultSettingsForBundle(), and FlutterEngineInitialize().

◆ kPlatformStrongDill

const uint8_t kPlatformStrongDill[]

◆ kPlatformStrongDillSize

const intptr_t kPlatformStrongDillSize