Flutter Engine
FlutterEngine.h
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 #ifndef FLUTTER_FLUTTERENGINE_H_
6 #define FLUTTER_FLUTTERENGINE_H_
7 
8 #import <Foundation/Foundation.h>
9 #import <UIKit/UIKit.h>
10 
12 #import "FlutterDartProject.h"
13 #import "FlutterMacros.h"
14 #import "FlutterPlugin.h"
15 #import "FlutterTexture.h"
16 
18 
20 
21 /**
22  * The dart entrypoint that is associated with `main()`. This is to be used as an argument to the
23  * `runWithEntrypoint*` methods.
24  */
25 extern NSString* const FlutterDefaultDartEntrypoint;
26 
27 /**
28  * The default Flutter initial route ("/").
29  */
30 extern NSString* const FlutterDefaultInitialRoute;
31 
32 /**
33  * The FlutterEngine class coordinates a single instance of execution for a
34  * `FlutterDartProject`. It may have zero or one `FlutterViewController` at a
35  * time, which can be specified via `-setViewController:`.
36  * `FlutterViewController`'s `initWithEngine` initializer will automatically call
37  * `-setViewController:` for itself.
38  *
39  * A FlutterEngine can be created independently of a `FlutterViewController` for
40  * headless execution. It can also persist across the lifespan of multiple
41  * `FlutterViewController` instances to maintain state and/or asynchronous tasks
42  * (such as downloading a large file).
43  *
44  * A FlutterEngine can also be used to prewarm the Dart execution environment and reduce the
45  * latency of showing the Flutter screen when a `FlutterViewController` is created and presented.
46  * See http://flutter.dev/docs/development/add-to-app/performance for more details on loading
47  * performance.
48  *
49  * Alternatively, you can simply create a new `FlutterViewController` with only a
50  * `FlutterDartProject`. That `FlutterViewController` will internally manage its
51  * own instance of a FlutterEngine, but will not guarantee survival of the engine
52  * beyond the life of the ViewController.
53  *
54  * A newly initialized FlutterEngine will not actually run a Dart Isolate until
55  * either `-runWithEntrypoint:` or `-runWithEntrypoint:libraryURI` is invoked.
56  * One of these methods must be invoked before calling `-setViewController:`.
57  */
59 @interface FlutterEngine : NSObject <FlutterTextureRegistry, FlutterPluginRegistry>
60 
61 /**
62  * Default initializer for a FlutterEngine.
63  *
64  * Threads created by this FlutterEngine will appear as "FlutterEngine #" in
65  * Instruments. The prefix can be customized using `initWithName`.
66  *
67  * The engine will execute the project located in the bundle with the identifier
68  * "io.flutter.flutter.app" (the default for Flutter projects).
69  *
70  * A newly initialized engine will not run until either `-runWithEntrypoint:` or
71  * `-runWithEntrypoint:libraryURI:` is called.
72  *
73  * FlutterEngine created with this method will have allowHeadlessExecution set to `YES`.
74  * This means that the engine will continue to run regardless of whether a `FlutterViewController`
75  * is attached to it or not, until `-destroyContext:` is called or the process finishes.
76  */
77 - (instancetype)init;
78 
79 /**
80  * Initialize this FlutterEngine.
81  *
82  * The engine will execute the project located in the bundle with the identifier
83  * "io.flutter.flutter.app" (the default for Flutter projects).
84  *
85  * A newly initialized engine will not run until either `-runWithEntrypoint:` or
86  * `-runWithEntrypoint:libraryURI:` is called.
87  *
88  * FlutterEngine created with this method will have allowHeadlessExecution set to `YES`.
89  * This means that the engine will continue to run regardless of whether a `FlutterViewController`
90  * is attached to it or not, until `-destroyContext:` is called or the process finishes.
91  *
92  * @param labelPrefix The label prefix used to identify threads for this instance. Should
93  * be unique across FlutterEngine instances, and is used in instrumentation to label
94  * the threads used by this FlutterEngine.
95  */
96 - (instancetype)initWithName:(NSString*)labelPrefix;
97 
98 /**
99  * Initialize this FlutterEngine with a `FlutterDartProject`.
100  *
101  * If the FlutterDartProject is not specified, the FlutterEngine will attempt to locate
102  * the project in a default location (the flutter_assets folder in the iOS application
103  * bundle).
104  *
105  * A newly initialized engine will not run the `FlutterDartProject` until either
106  * `-runWithEntrypoint:` or `-runWithEntrypoint:libraryURI:` is called.
107  *
108  * FlutterEngine created with this method will have allowHeadlessExecution set to `YES`.
109  * This means that the engine will continue to run regardless of whether a `FlutterViewController`
110  * is attached to it or not, until `-destroyContext:` is called or the process finishes.
111  *
112  * @param labelPrefix The label prefix used to identify threads for this instance. Should
113  * be unique across FlutterEngine instances, and is used in instrumentation to label
114  * the threads used by this FlutterEngine.
115  * @param project The `FlutterDartProject` to run.
116  */
117 - (instancetype)initWithName:(NSString*)labelPrefix project:(nullable FlutterDartProject*)project;
118 
119 /**
120  * Initialize this FlutterEngine with a `FlutterDartProject`.
121  *
122  * If the FlutterDartProject is not specified, the FlutterEngine will attempt to locate
123  * the project in a default location (the flutter_assets folder in the iOS application
124  * bundle).
125  *
126  * A newly initialized engine will not run the `FlutterDartProject` until either
127  * `-runWithEntrypoint:` or `-runWithEntrypoint:libraryURI:` is called.
128  *
129  * @param labelPrefix The label prefix used to identify threads for this instance. Should
130  * be unique across FlutterEngine instances, and is used in instrumentation to label
131  * the threads used by this FlutterEngine.
132  * @param project The `FlutterDartProject` to run.
133  * @param allowHeadlessExecution Whether or not to allow this instance to continue
134  * running after passing a nil `FlutterViewController` to `-setViewController:`.
135  */
136 - (instancetype)initWithName:(NSString*)labelPrefix
137  project:(nullable FlutterDartProject*)project
138  allowHeadlessExecution:(BOOL)allowHeadlessExecution NS_DESIGNATED_INITIALIZER;
139 
140 + (instancetype)new NS_UNAVAILABLE;
141 
142 /**
143  * Runs a Dart program on an Isolate from the main Dart library (i.e. the library that
144  * contains `main()`), using `main()` as the entrypoint (the default for Flutter projects),
145  * and using "/" (the default route) as the initial route.
146  *
147  * The first call to this method will create a new Isolate. Subsequent calls will return
148  * immediately and have no effect.
149  *
150  * @return YES if the call succeeds in creating and running a Flutter Engine instance; NO otherwise.
151  */
152 - (BOOL)run;
153 
154 /**
155  * Runs a Dart program on an Isolate from the main Dart library (i.e. the library that
156  * contains `main()`), using "/" (the default route) as the initial route.
157  *
158  * The first call to this method will create a new Isolate. Subsequent calls will return
159  * immediately and have no effect.
160  *
161  * @param entrypoint The name of a top-level function from the same Dart
162  * library that contains the app's main() function. If this is FlutterDefaultDartEntrypoint (or
163  * nil) it will default to `main()`. If it is not the app's main() function, that function must
164  * be decorated with `@pragma(vm:entry-point)` to ensure the method is not tree-shaken by the Dart
165  * compiler.
166  * @return YES if the call succeeds in creating and running a Flutter Engine instance; NO otherwise.
167  */
168 - (BOOL)runWithEntrypoint:(nullable NSString*)entrypoint;
169 
170 /**
171  * Runs a Dart program on an Isolate from the main Dart library (i.e. the library that
172  * contains `main()`).
173  *
174  * The first call to this method will create a new Isolate. Subsequent calls will return
175  * immediately and have no effect.
176  *
177  * @param entrypoint The name of a top-level function from the same Dart
178  * library that contains the app's main() function. If this is FlutterDefaultDartEntrypoint (or
179  * nil), it will default to `main()`. If it is not the app's main() function, that function must
180  * be decorated with `@pragma(vm:entry-point)` to ensure the method is not tree-shaken by the Dart
181  * compiler.
182  * @param initialRoute The name of the initial Flutter `Navigator` `Route` to load. If this is
183  * FlutterDefaultInitialRoute (or nil), it will default to the "/" route.
184  * @return YES if the call succeeds in creating and running a Flutter Engine instance; NO otherwise.
185  */
186 - (BOOL)runWithEntrypoint:(nullable NSString*)entrypoint
187  initialRoute:(nullable NSString*)initialRoute;
188 
189 /**
190  * Runs a Dart program on an Isolate using the specified entrypoint and Dart library,
191  * which may not be the same as the library containing the Dart program's `main()` function.
192  *
193  * The first call to this method will create a new Isolate. Subsequent calls will return
194  * immediately and have no effect.
195  *
196  * @param entrypoint The name of a top-level function from a Dart library. If this is
197  * FlutterDefaultDartEntrypoint (or nil); this will default to `main()`. If it is not the app's
198  * main() function, that function must be decorated with `@pragma(vm:entry-point)` to ensure the
199  * method is not tree-shaken by the Dart compiler.
200  * @param uri The URI of the Dart library which contains the entrypoint method. IF nil,
201  * this will default to the same library as the `main()` function in the Dart program.
202  * @return YES if the call succeeds in creating and running a Flutter Engine instance; NO otherwise.
203  */
204 - (BOOL)runWithEntrypoint:(nullable NSString*)entrypoint libraryURI:(nullable NSString*)uri;
205 
206 /**
207  * Destroy running context for an engine.
208  *
209  * This method can be used to force the FlutterEngine object to release all resources.
210  * After sending this message, the object will be in an unusable state until it is deallocated.
211  * Accessing properties or sending messages to it will result in undefined behavior or runtime
212  * errors.
213  */
214 - (void)destroyContext;
215 
216 /**
217  * Ensures that Flutter will generate a semantics tree.
218  *
219  * This is enabled by default if certain accessibility services are turned on by
220  * the user, or when using a Simulator. This method allows a user to turn
221  * semantics on when they would not ordinarily be generated and the performance
222  * overhead is not a concern, e.g. for UI testing. Note that semantics should
223  * never be programmatically turned off, as it would potentially disable
224  * accessibility services an end user has requested.
225  *
226  * This method must only be called after launching the engine via
227  * `-runWithEntrypoint:` or `-runWithEntryPoint:libraryURI`.
228  *
229  * Although this method returns synchronously, it does not guarantee that a
230  * semantics tree is actually available when the method returns. It
231  * synchronously ensures that the next frame the Flutter framework creates will
232  * have a semantics tree.
233  *
234  * You can subscribe to semantics updates via `NSNotificationCenter` by adding
235  * an observer for the name `FlutterSemanticsUpdateNotification`. The `object`
236  * parameter will be the `FlutterViewController` associated with the semantics
237  * update. This will asynchronously fire after a semantics tree has actually
238  * built (which may be some time after the frame has been rendered).
239  */
240 - (void)ensureSemanticsEnabled;
241 
242 /**
243  * Sets the `FlutterViewController` for this instance. The FlutterEngine must be
244  * running (e.g. a successful call to `-runWithEntrypoint:` or `-runWithEntrypoint:libraryURI`)
245  * before calling this method. Callers may pass nil to remove the viewController
246  * and have the engine run headless in the current process.
247  *
248  * A FlutterEngine can only have one `FlutterViewController` at a time. If there is
249  * already a `FlutterViewController` associated with this instance, this method will replace
250  * the engine's current viewController with the newly specified one.
251  *
252  * Setting the viewController will signal the engine to start animations and drawing, and unsetting
253  * it will signal the engine to stop animations and drawing. However, neither will impact the state
254  * of the Dart program's execution.
255  */
256 @property(nonatomic, weak) FlutterViewController* viewController;
257 
258 /**
259  * The `FlutterMethodChannel` used for localization related platform messages, such as
260  * setting the locale.
261  *
262  * Can be nil after `destroyContext` is called.
263  */
264 @property(nonatomic, readonly, nullable) FlutterMethodChannel* localizationChannel;
265 /**
266  * The `FlutterMethodChannel` used for navigation related platform messages.
267  *
268  * Can be nil after `destroyContext` is called.
269  *
270  * @see [Navigation
271  * Channel](https://docs.flutter.io/flutter/services/SystemChannels/navigation-constant.html)
272  * @see [Navigator Widget](https://docs.flutter.io/flutter/widgets/Navigator-class.html)
273  */
274 @property(nonatomic, readonly) FlutterMethodChannel* navigationChannel;
275 
276 /**
277  * The `FlutterMethodChannel` used for core platform messages, such as
278  * information about the screen orientation.
279  *
280  * Can be nil after `destroyContext` is called.
281  */
282 @property(nonatomic, readonly) FlutterMethodChannel* platformChannel;
283 
284 /**
285  * The `FlutterMethodChannel` used to communicate text input events to the
286  * Dart Isolate.
287  *
288  * Can be nil after `destroyContext` is called.
289  *
290  * @see [Text Input
291  * Channel](https://docs.flutter.io/flutter/services/SystemChannels/textInput-constant.html)
292  */
293 @property(nonatomic, readonly) FlutterMethodChannel* textInputChannel;
294 
295 /**
296  * The `FlutterBasicMessageChannel` used to communicate app lifecycle events
297  * to the Dart Isolate.
298  *
299  * Can be nil after `destroyContext` is called.
300  *
301  * @see [Lifecycle
302  * Channel](https://docs.flutter.io/flutter/services/SystemChannels/lifecycle-constant.html)
303  */
304 @property(nonatomic, readonly) FlutterBasicMessageChannel* lifecycleChannel;
305 
306 /**
307  * The `FlutterBasicMessageChannel` used for communicating system events, such as
308  * memory pressure events.
309  *
310  * Can be nil after `destroyContext` is called.
311  *
312  * @see [System
313  * Channel](https://docs.flutter.io/flutter/services/SystemChannels/system-constant.html)
314  */
315 @property(nonatomic, readonly) FlutterBasicMessageChannel* systemChannel;
316 
317 /**
318  * The `FlutterBasicMessageChannel` used for communicating user settings such as
319  * clock format and text scale.
320  *
321  * Can be nil after `destroyContext` is called.
322  */
323 @property(nonatomic, readonly) FlutterBasicMessageChannel* settingsChannel;
324 
325 /**
326  * The `FlutterBasicMessageChannel` used for communicating key events
327  * from physical keyboards
328  *
329  * Can be nil after `destroyContext` is called.
330  */
331 @property(nonatomic, readonly) FlutterBasicMessageChannel* keyEventChannel;
332 
333 /**
334  * The `NSURL` of the observatory for the service isolate.
335  *
336  * This is only set in debug and profile runtime modes, and only after the
337  * observatory service is ready. In release mode or before the observatory has
338  * started, it returns `nil`.
339  */
340 @property(nonatomic, readonly, nullable) NSURL* observatoryUrl;
341 
342 /**
343  * The `FlutterBinaryMessenger` associated with this FlutterEngine (used for communicating with
344  * channels).
345  */
346 @property(nonatomic, readonly) NSObject<FlutterBinaryMessenger>* binaryMessenger;
347 
348 /**
349  * The UI Isolate ID of the engine.
350  *
351  * This property will be nil if the engine is not running.
352  */
353 @property(nonatomic, readonly, copy, nullable) NSString* isolateId;
354 
355 /**
356  * Whether or not GPU calls are allowed.
357  *
358  * Typically this is set when the app is backgrounded and foregrounded.
359  */
360 @property(nonatomic, assign) BOOL isGpuDisabled;
361 
362 @end
363 
365 
366 #endif // FLUTTER_FLUTTERENGINE_H_
FlutterMethodChannel * localizationChannel
NSObject< FlutterBinaryMessenger > * binaryMessenger
FlutterMethodChannel * textInputChannel
#define NS_ASSUME_NONNULL_END
Definition: FlutterMacros.h:20
NSString * isolateId
#define NS_ASSUME_NONNULL_BEGIN
Definition: FlutterMacros.h:19
FlutterMethodChannel * navigationChannel
instancetype NS_UNAVAILABLE()
FlutterBasicMessageChannel * systemChannel
NS_ASSUME_NONNULL_BEGIN NSString *const FlutterDefaultDartEntrypoint
NSString *const FlutterDefaultInitialRoute
NSURL * observatoryUrl
void ensureSemanticsEnabled()
FlutterMethodChannel * platformChannel
FlutterBasicMessageChannel * settingsChannel
FlutterBasicMessageChannel * keyEventChannel
FlutterBasicMessageChannel * lifecycleChannel
instancetype init()
#define FLUTTER_EXPORT
FlutterViewController * viewController