FlutterEngine.h 13 KB

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