| Flutter macOS Embedder |

FlutterEngine.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 #import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
6 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine_Internal.h"
7
8 #include <algorithm>
9 #include <iostream>
10 #include <sstream>
11 #include <vector>
12
13 #include "flutter/common/constants.h"
14 #include "flutter/shell/platform/common/app_lifecycle_state.h"
15 #include "flutter/shell/platform/common/engine_switches.h"
16 #include "flutter/shell/platform/embedder/embedder.h"
17
18 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
19 #import "flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h"
20 #import "flutter/shell/platform/darwin/macos/InternalFlutterSwift/InternalFlutterSwift.h"
21 #import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterAppDelegate.h"
22 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterAppDelegate_Internal.h"
23 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterCompositor.h"
24 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDartProject_Internal.h"
25 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterDisplayLink.h"
26 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMenuPlugin.h"
27 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterMouseCursorPlugin.h"
28 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h"
29 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h"
30 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTimeConverter.h"
31 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterVSyncWaiter.h"
32 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
33 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController_Internal.h"
34 #import "flutter/shell/platform/darwin/macos/framework/Source/FlutterViewEngineProvider.h"
35
36 #import <CoreVideo/CoreVideo.h>
37 #import <IOSurface/IOSurface.h>
38
39 @class FlutterEngineRegistrar;
40
41 NSString* const kFlutterPlatformChannel = @"flutter/platform";
42 NSString* const kFlutterSettingsChannel = @"flutter/settings";
43 NSString* const kFlutterLifecycleChannel = @"flutter/lifecycle";
44
45 using flutter::kFlutterImplicitViewId;
46
47 /**
48 * Constructs and returns a FlutterLocale struct corresponding to |locale|, which must outlive
49 * the returned struct.
50 */
51 static FlutterLocale FlutterLocaleFromNSLocale(NSLocale* locale) {
52 FlutterLocale flutterLocale = {};
53 flutterLocale.struct_size = sizeof(FlutterLocale);
54 flutterLocale.language_code = [[locale objectForKey:NSLocaleLanguageCode] UTF8String];
55 flutterLocale.country_code = [[locale objectForKey:NSLocaleCountryCode] UTF8String];
56 flutterLocale.script_code = [[locale objectForKey:NSLocaleScriptCode] UTF8String];
57 flutterLocale.variant_code = [[locale objectForKey:NSLocaleVariantCode] UTF8String];
58 return flutterLocale;
59 }
60
61 /// The private notification for voice over.
62 static NSString* const kEnhancedUserInterfaceNotification =
63 @"NSApplicationDidChangeAccessibilityEnhancedUserInterfaceNotification";
64 static NSString* const kEnhancedUserInterfaceKey = @"AXEnhancedUserInterface";
65
66 /// Clipboard plain text format.
67 constexpr char kTextPlainFormat[] = "text/plain";
68
69 #pragma mark -
70
71 // Records an active handler of the messenger (FlutterEngine) that listens to
72 // platform messages on a given channel.
73 @interface FlutterEngineHandlerInfo : NSObject
74
75 - (instancetype)initWithConnection:(NSNumber*)connection
76 handler:(FlutterBinaryMessageHandler)handler;
77
78 @property(nonatomic, readonly) FlutterBinaryMessageHandler handler;
79 @property(nonatomic, readonly) NSNumber* connection;
80
81 @end
82
83 @implementation FlutterEngineHandlerInfo
84 - (instancetype)initWithConnection:(NSNumber*)connection
85 handler:(FlutterBinaryMessageHandler)handler {
86 self = [super init];
87 NSAssert(self, @"Super init cannot be nil");
88 _connection = connection;
89 _handler = handler;
90 return self;
91 }
92 @end
93
94 #pragma mark -
95
96 /**
97 * Private interface declaration for FlutterEngine.
98 */
99 @interface FlutterEngine () <FlutterBinaryMessenger,
100 FlutterMouseCursorPluginDelegate,
101 FlutterKeyboardManagerDelegate,
102 FlutterTextInputPluginDelegate>
103
104 /**
105 * A mutable array that holds one bool value that determines if responses to platform messages are
106 * clear to execute. This value should be read or written only inside of a synchronized block and
107 * will return `NO` after the FlutterEngine has been dealloc'd.
108 */
109 @property(nonatomic, strong) NSMutableArray<NSNumber*>* isResponseValid;
110
111 /**
112 * All delegates added via plugin calls to addApplicationDelegate.
113 */
114 @property(nonatomic, strong) NSPointerArray* pluginAppDelegates;
115
116 /**
117 * All registrars returned from registrarForPlugin:
118 */
119 @property(nonatomic, readonly)
120 NSMutableDictionary<NSString*, FlutterEngineRegistrar*>* pluginRegistrars;
121
122 - (nullable FlutterViewController*)viewControllerForIdentifier:
123 (FlutterViewIdentifier)viewIdentifier;
124
125 /**
126 * An internal method that adds the view controller with the given ID.
127 *
128 * This method assigns the controller with the ID, puts the controller into the
129 * map, and does assertions related to the implicit view ID.
130 */
131 - (void)registerViewController:(FlutterViewController*)controller
132 forIdentifier:(FlutterViewIdentifier)viewIdentifier;
133
134 /**
135 * An internal method that removes the view controller with the given ID.
136 *
137 * This method clears the ID of the controller, removes the controller from the
138 * map. This is an no-op if the view ID is not associated with any view
139 * controllers.
140 */
141 - (void)deregisterViewControllerForIdentifier:(FlutterViewIdentifier)viewIdentifier;
142
143 /**
144 * Shuts down the engine if view requirement is not met, and headless execution
145 * is not allowed.
146 */
147 - (void)shutDownIfNeeded;
148
149 /**
150 * Sends the list of user-preferred locales to the Flutter engine.
151 */
152 - (void)sendUserLocales;
153
154 /**
155 * Handles a platform message from the engine.
156 */
157 - (void)engineCallbackOnPlatformMessage:(const FlutterPlatformMessage*)message;
158
159 /**
160 * Requests that the task be posted back the to the Flutter engine at the target time. The target
161 * time is in the clock used by the Flutter engine.
162 */
163 - (void)postMainThreadTask:(FlutterTask)task targetTimeInNanoseconds:(uint64_t)targetTime;
164
165 /**
166 * Loads the AOT snapshots and instructions from the elf bundle (app_elf_snapshot.so) into _aotData,
167 * if it is present in the assets directory.
168 */
169 - (void)loadAOTData:(NSString*)assetsDir;
170
171 /**
172 * Creates a platform view channel and sets up the method handler.
173 */
174 - (void)setUpPlatformViewChannel;
175
176 /**
177 * Creates an accessibility channel and sets up the message handler.
178 */
179 - (void)setUpAccessibilityChannel;
180
181 /**
182 * Handles messages received from the Flutter engine on the _*Channel channels.
183 */
184 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result;
185
186 @end
187
188 #pragma mark -
189
190 @implementation FlutterEngineTerminationHandler {
191 __weak FlutterEngine* _engine;
192 FlutterTerminationCallback _terminator;
193 }
194
195 - (instancetype)initWithEngine:(FlutterEngine*)engine
196 terminator:(FlutterTerminationCallback)terminator {
197 self = [super init];
198 _acceptingRequests = NO;
199 _engine = engine;
200 _terminator = terminator ? terminator : ^(id sender) {
201 // Default to actually terminating the application. The terminator exists to
202 // allow tests to override it so that an actual exit doesn't occur.
203 [[NSApplication sharedApplication] terminate:sender];
204 };
205 id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
206 if ([appDelegate respondsToSelector:@selector(setTerminationHandler:)]) {
207 FlutterAppDelegate* flutterAppDelegate = reinterpret_cast<FlutterAppDelegate*>(appDelegate);
208 flutterAppDelegate.terminationHandler = self;
209 }
210 return self;
211 }
212
213 // This is called by the method call handler in the engine when the application
214 // requests termination itself.
215 - (void)handleRequestAppExitMethodCall:(NSDictionary<NSString*, id>*)arguments
216 result:(FlutterResult)result {
217 NSString* type = arguments[@"type"];
218 // Ignore the "exitCode" value in the arguments because AppKit doesn't have
219 // any good way to set the process exit code other than calling exit(), and
220 // that bypasses all of the native applicationShouldExit shutdown events,
221 // etc., which we don't want to skip.
222
223 FlutterAppExitType exitType =
224 [type isEqualTo:@"cancelable"] ? kFlutterAppExitTypeCancelable : kFlutterAppExitTypeRequired;
225
226 [self requestApplicationTermination:[NSApplication sharedApplication]
227 exitType:exitType
228 result:result];
229 }
230
231 // This is called by the FlutterAppDelegate whenever any termination request is
232 // received.
233 - (void)requestApplicationTermination:(id)sender
234 exitType:(FlutterAppExitType)type
235 result:(nullable FlutterResult)result {
236 _shouldTerminate = YES;
237 if (![self acceptingRequests]) {
238 // Until the Dart application has signaled that it is ready to handle
239 // termination requests, the app will just terminate when asked.
240 type = kFlutterAppExitTypeRequired;
241 }
242 switch (type) {
243 case kFlutterAppExitTypeCancelable: {
244 FlutterJSONMethodCodec* codec = [FlutterJSONMethodCodec sharedInstance];
245 FlutterMethodCall* methodCall =
246 [FlutterMethodCall methodCallWithMethodName:@"System.requestAppExit" arguments:nil];
247 [_engine sendOnChannel:kFlutterPlatformChannel
248 message:[codec encodeMethodCall:methodCall]
249 binaryReply:^(NSData* _Nullable reply) {
250 NSAssert(_terminator, @"terminator shouldn't be nil");
251 id decoded_reply = [codec decodeEnvelope:reply];
252 if ([decoded_reply isKindOfClass:[FlutterError class]]) {
253 FlutterError* error = (FlutterError*)decoded_reply;
254 NSLog(@"Method call returned error[%@]: %@ %@", [error code], [error message],
255 [error details]);
256 _terminator(sender);
257 return;
258 }
259 if (![decoded_reply isKindOfClass:[NSDictionary class]]) {
260 NSLog(@"Call to System.requestAppExit returned an unexpected object: %@",
261 decoded_reply);
262 _terminator(sender);
263 return;
264 }
265 NSDictionary* replyArgs = (NSDictionary*)decoded_reply;
266 if ([replyArgs[@"response"] isEqual:@"exit"]) {
267 _terminator(sender);
268 } else if ([replyArgs[@"response"] isEqual:@"cancel"]) {
269 _shouldTerminate = NO;
270 }
271 if (result != nil) {
272 result(replyArgs);
273 }
274 }];
275 break;
276 }
277 case kFlutterAppExitTypeRequired:
278 NSAssert(_terminator, @"terminator shouldn't be nil");
279 _terminator(sender);
280 break;
281 }
282 }
283
284 @end
285
286 #pragma mark -
287
288 @implementation FlutterPasteboard
289
290 - (NSInteger)clearContents {
291 return [[NSPasteboard generalPasteboard] clearContents];
292 }
293
294 - (NSString*)stringForType:(NSPasteboardType)dataType {
295 return [[NSPasteboard generalPasteboard] stringForType:dataType];
296 }
297
298 - (BOOL)setString:(nonnull NSString*)string forType:(nonnull NSPasteboardType)dataType {
299 return [[NSPasteboard generalPasteboard] setString:string forType:dataType];
300 }
301
302 @end
303
304 #pragma mark -
305
306 /**
307 * `FlutterPluginRegistrar` implementation handling a single plugin.
308 */
309 @interface FlutterEngineRegistrar : NSObject <FlutterPluginRegistrar>
310 - (instancetype)initWithPlugin:(nonnull NSString*)pluginKey
311 flutterEngine:(nonnull FlutterEngine*)flutterEngine;
312
313 - (nullable NSView*)viewForIdentifier:(FlutterViewIdentifier)viewIdentifier;
314
315 /**
316 * The value published by this plugin, or NSNull if nothing has been published.
317 *
318 * The unusual NSNull is for the documented behavior of valuePublishedByPlugin:.
319 */
320 @property(nonatomic, readonly, nonnull) NSObject* publishedValue;
321 @end
322
323 @implementation FlutterEngineRegistrar {
324 NSString* _pluginKey;
325 __weak FlutterEngine* _flutterEngine;
326 }
327
328 @dynamic view;
329
330 - (instancetype)initWithPlugin:(NSString*)pluginKey flutterEngine:(FlutterEngine*)flutterEngine {
331 self = [super init];
332 if (self) {
333 _pluginKey = [pluginKey copy];
334 _flutterEngine = flutterEngine;
335 _publishedValue = [NSNull null];
336 }
337 return self;
338 }
339
340 #pragma mark - FlutterPluginRegistrar
341
342 - (id<FlutterBinaryMessenger>)messenger {
343 return _flutterEngine.binaryMessenger;
344 }
345
346 - (id<FlutterTextureRegistry>)textures {
347 return _flutterEngine.renderer;
348 }
349
350 - (NSView*)view {
351 return [self viewForIdentifier:kFlutterImplicitViewId];
352 }
353
354 - (NSView*)viewForIdentifier:(FlutterViewIdentifier)viewIdentifier {
355 FlutterViewController* controller = [_flutterEngine viewControllerForIdentifier:viewIdentifier];
356 if (controller == nil) {
357 return nil;
358 }
359 if (!controller.viewLoaded) {
360 [controller loadView];
361 }
362 return controller.flutterView;
363 }
364
365 - (NSViewController*)viewController {
366 return [_flutterEngine viewControllerForIdentifier:kFlutterImplicitViewId];
367 }
368
369 - (void)addMethodCallDelegate:(nonnull id<FlutterPlugin>)delegate
370 channel:(nonnull FlutterMethodChannel*)channel {
371 [channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
372 [delegate handleMethodCall:call result:result];
373 }];
374 }
375
376 - (void)addApplicationDelegate:(NSObject<FlutterAppLifecycleDelegate>*)delegate {
377 id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
378 if ([appDelegate conformsToProtocol:@protocol(FlutterAppLifecycleProvider)]) {
379 id<FlutterAppLifecycleProvider> lifeCycleProvider =
380 static_cast<id<FlutterAppLifecycleProvider>>(appDelegate);
381 [lifeCycleProvider addApplicationLifecycleDelegate:delegate];
382 [_flutterEngine.pluginAppDelegates addPointer:(__bridge void*)delegate];
383 }
384 }
385
386 - (void)registerViewFactory:(nonnull NSObject<FlutterPlatformViewFactory>*)factory
387 withId:(nonnull NSString*)factoryId {
388 [[_flutterEngine platformViewController] registerViewFactory:factory withId:factoryId];
389 }
390
391 - (void)publish:(NSObject*)value {
392 _publishedValue = value;
393 }
394
395 - (NSString*)lookupKeyForAsset:(NSString*)asset {
396 return [FlutterDartProject lookupKeyForAsset:asset];
397 }
398
399 - (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
400 return [FlutterDartProject lookupKeyForAsset:asset fromPackage:package];
401 }
402
403 @end
404
405 // Callbacks provided to the engine. See the called methods for documentation.
406 #pragma mark - Static methods provided to engine configuration
407
408 static void OnPlatformMessage(const FlutterPlatformMessage* message, void* user_data) {
409 FlutterEngine* engine = (__bridge FlutterEngine*)user_data;
410 [engine engineCallbackOnPlatformMessage:message];
411 }
412
413 #pragma mark -
414
415 @implementation FlutterEngine {
416 // The embedding-API-level engine object.
417 FLUTTER_API_SYMBOL(FlutterEngine) _engine;
418
419 // The project being run by this engine.
420 FlutterDartProject* _project;
421
422 // A mapping of channel names to the registered information for those channels.
423 NSMutableDictionary<NSString*, FlutterEngineHandlerInfo*>* _messengerHandlers;
424
425 // A self-incremental integer to assign to newly assigned channels as
426 // identification.
427 FlutterBinaryMessengerConnection _currentMessengerConnection;
428
429 // Whether the engine can continue running after the view controller is removed.
430 BOOL _allowHeadlessExecution;
431
432 // Pointer to the Dart AOT snapshot and instruction data.
433 _FlutterEngineAOTData* _aotData;
434
435 // _macOSCompositor is created when the engine is created and its destruction is handled by ARC
436 // when the engine is destroyed.
437 std::unique_ptr<flutter::FlutterCompositor> _macOSCompositor;
438
439 // The information of all views attached to this engine mapped from IDs.
440 //
441 // It can't use NSDictionary, because the values need to be weak references.
442 NSMapTable* _viewControllers;
443
444 // FlutterCompositor is copied and used in embedder.cc.
445 FlutterCompositor _compositor;
446
447 // Method channel for platform view functions. These functions include creating, disposing and
448 // mutating a platform view.
449 FlutterMethodChannel* _platformViewsChannel;
450
451 // Used to support creation and deletion of platform views and registering platform view
452 // factories. Lifecycle is tied to the engine.
453 FlutterPlatformViewController* _platformViewController;
454
455 // Used to manage Flutter windows created by the Dart application
456 FlutterWindowController* _windowController;
457
458 // A message channel for sending user settings to the flutter engine.
459 FlutterBasicMessageChannel* _settingsChannel;
460
461 // A message channel for accessibility.
462 FlutterBasicMessageChannel* _accessibilityChannel;
463
464 // A method channel for miscellaneous platform functionality.
465 FlutterMethodChannel* _platformChannel;
466
467 // A method channel for taking screenshots via the rasterizer.
468 FlutterMethodChannel* _screenshotChannel;
469
470 // Whether the application is currently the active application.
471 BOOL _active;
472
473 // Whether any portion of the application is currently visible.
474 BOOL _visible;
475
476 // Proxy to allow plugins, channels to hold a weak reference to the binary messenger (self).
477 FlutterBinaryMessengerRelay* _binaryMessenger;
478
479 // Map from ViewId to vsync waiter. Note that this is modified on main thread
480 // but accessed on UI thread, so access must be @synchronized.
481 NSMapTable<NSNumber*, FlutterVSyncWaiter*>* _vsyncWaiters;
482
483 // Weak reference to last view that received a pointer event. This is used to
484 // pair cursor change with a view.
485 __weak FlutterView* _lastViewWithPointerEvent;
486
487 // Pointer to a keyboard manager.
488 FlutterKeyboardManager* _keyboardManager;
489
490 // The text input plugin that handles text editing state for text fields.
491 FlutterTextInputPlugin* _textInputPlugin;
492
493 // Whether the engine is running in multi-window mode. This affects behavior
494 // when adding view controller (it will fail when calling multiple times without
495 // _multiviewEnabled).
496 BOOL _multiViewEnabled;
497
498 // View identifier for the next view to be created.
499 // Only used when multiview is enabled.
500 FlutterViewIdentifier _nextViewIdentifier;
501 }
502
503 @synthesize windowController = _windowController;
504 @synthesize project = _project;
505
506 - (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
507 return [self initWithName:labelPrefix project:project allowHeadlessExecution:YES];
508 }
509
510 static const int kMainThreadPriority = 47;
511
512 static void SetThreadPriority(FlutterThreadPriority priority) {
513 if (priority == kDisplay || priority == kRaster) {
514 pthread_t thread = pthread_self();
515 sched_param param;
516 int policy;
517 if (!pthread_getschedparam(thread, &policy, &param)) {
518 param.sched_priority = kMainThreadPriority;
519 pthread_setschedparam(thread, policy, &param);
520 }
521 pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0);
522 }
523 }
524
525 - (instancetype)initWithName:(NSString*)labelPrefix
526 project:(FlutterDartProject*)project
527 allowHeadlessExecution:(BOOL)allowHeadlessExecution {
528 self = [super init];
529 NSAssert(self, @"Super init cannot be nil");
530
531 [FlutterRunLoop ensureMainLoopInitialized];
532
533 _pasteboard = [[FlutterPasteboard alloc] init];
534 _active = NO;
535 _visible = NO;
536 _project = project ?: [[FlutterDartProject alloc] init];
537 _messengerHandlers = [[NSMutableDictionary alloc] init];
538 _pluginAppDelegates = [NSPointerArray weakObjectsPointerArray];
539 _pluginRegistrars = [[NSMutableDictionary alloc] init];
540 _currentMessengerConnection = 1;
541 _allowHeadlessExecution = allowHeadlessExecution;
542 _semanticsEnabled = NO;
543 _binaryMessenger = [[FlutterBinaryMessengerRelay alloc] initWithParent:self];
544 _isResponseValid = [[NSMutableArray alloc] initWithCapacity:1];
545 [_isResponseValid addObject:@YES];
546 _keyboardManager = [[FlutterKeyboardManager alloc] initWithDelegate:self];
547 _textInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:self];
548 _multiViewEnabled = NO;
549 _nextViewIdentifier = 1;
550
551 _embedderAPI.struct_size = sizeof(FlutterEngineProcTable);
552 FlutterEngineGetProcAddresses(&_embedderAPI);
553
554 _viewControllers = [NSMapTable weakToWeakObjectsMapTable];
555 _renderer = [[FlutterRenderer alloc] initWithFlutterEngine:self];
556
557 NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter];
558 [notificationCenter addObserver:self
559 selector:@selector(sendUserLocales)
560 name:NSCurrentLocaleDidChangeNotification
561 object:nil];
562
563 _platformViewController = [[FlutterPlatformViewController alloc] init];
564 // The macOS compositor must be initialized in the initializer because it is
565 // used when adding views, which might happen before runWithEntrypoint.
566 _macOSCompositor = std::make_unique<flutter::FlutterCompositor>(
567 [[FlutterViewEngineProvider alloc] initWithEngine:self],
568 [[FlutterTimeConverter alloc] initWithEngine:self], _platformViewController);
569
570 [self setUpPlatformViewChannel];
571
572 _windowController = [[FlutterWindowController alloc] init];
573 _windowController.engine = self;
574
575 [self setUpAccessibilityChannel];
576 [self setUpNotificationCenterListeners];
577 id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
578 if ([appDelegate conformsToProtocol:@protocol(FlutterAppLifecycleProvider)]) {
579 _terminationHandler = [[FlutterEngineTerminationHandler alloc] initWithEngine:self
580 terminator:nil];
581 id<FlutterAppLifecycleProvider> lifecycleProvider =
582 static_cast<id<FlutterAppLifecycleProvider>>(appDelegate);
583 [lifecycleProvider addApplicationLifecycleDelegate:self];
584 } else {
585 _terminationHandler = nil;
586 }
587
588 _vsyncWaiters = [NSMapTable strongToStrongObjectsMapTable];
589
590 return self;
591 }
592
593 - (void)dealloc {
594 id<NSApplicationDelegate> appDelegate = [[NSApplication sharedApplication] delegate];
595 if ([appDelegate conformsToProtocol:@protocol(FlutterAppLifecycleProvider)]) {
596 id<FlutterAppLifecycleProvider> lifecycleProvider =
597 static_cast<id<FlutterAppLifecycleProvider>>(appDelegate);
598 [lifecycleProvider removeApplicationLifecycleDelegate:self];
599
600 // Unregister any plugins that registered as app delegates, since they are not guaranteed to
601 // live after the engine is destroyed, and their delegation registration is intended to be bound
602 // to the engine and its lifetime.
603 for (id<FlutterAppLifecycleDelegate> delegate in _pluginAppDelegates) {
604 if (delegate) {
605 [lifecycleProvider removeApplicationLifecycleDelegate:delegate];
606 }
607 }
608 }
609 // Clear any published values, just in case a plugin has created a retain cycle with the
610 // registrar.
611 for (NSString* pluginName in _pluginRegistrars) {
612 [_pluginRegistrars[pluginName] publish:[NSNull null]];
613 }
614 @synchronized(_isResponseValid) {
615 [_isResponseValid removeAllObjects];
616 [_isResponseValid addObject:@NO];
617 }
618 [self shutDownEngine];
619 if (_aotData) {
620 _embedderAPI.CollectAOTData(_aotData);
621 }
622 }
623
624 - (FlutterTaskRunnerDescription)createPlatformThreadTaskDescription {
625 static size_t sTaskRunnerIdentifiers = 0;
626 FlutterTaskRunnerDescription cocoa_task_runner_description = {
627 .struct_size = sizeof(FlutterTaskRunnerDescription),
628 // Retain for use in post_task_callback. Released in destruction_callback.
629 .user_data = (__bridge_retained void*)self,
630 .runs_task_on_current_thread_callback = [](void* user_data) -> bool {
631 return [[NSThread currentThread] isMainThread];
632 },
633 .post_task_callback = [](FlutterTask task, uint64_t target_time_nanos,
634 void* user_data) -> void {
635 FlutterEngine* engine = (__bridge FlutterEngine*)user_data;
636 [engine postMainThreadTask:task targetTimeInNanoseconds:target_time_nanos];
637 },
638 .identifier = ++sTaskRunnerIdentifiers,
639 .destruction_callback =
640 [](void* user_data) {
641 // Balancing release for the retain when setting user_data above.
642 FlutterEngine* engine = (__bridge_transfer FlutterEngine*)user_data;
643 engine = nil;
644 },
645 };
646 return cocoa_task_runner_description;
647 }
648
649 - (void)onFocusChangeRequest:(const FlutterViewFocusChangeRequest*)request {
650 FlutterViewController* controller = [self viewControllerForIdentifier:request->view_id];
651 if (controller == nil) {
652 return;
653 }
654 if (request->state == kFocused) {
655 [controller.flutterView.window makeFirstResponder:controller.flutterView];
656 }
657 }
658
659 - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
660 if (self.running) {
661 return NO;
662 }
663
664 if (!_allowHeadlessExecution && [_viewControllers count] == 0) {
665 NSLog(@"Attempted to run an engine with no view controller without headless mode enabled.");
666 return NO;
667 }
668
669 [self addInternalPlugins];
670
671 // The first argument of argv is required to be the executable name.
672 std::vector<const char*> argv = {[self.executableName UTF8String]};
673 std::vector<std::string> switches = self.switches;
674
675 // Enable Impeller only if specifically asked for from the project or cmdline arguments.
676 if (_project.enableImpeller ||
677 std::find(switches.begin(), switches.end(), "--enable-impeller=true") != switches.end()) {
678 switches.push_back("--enable-impeller=true");
679 }
680
681 if (_project.enableFlutterGPU ||
682 std::find(switches.begin(), switches.end(), "--enable-flutter-gpu=true") != switches.end()) {
683 switches.push_back("--enable-flutter-gpu=true");
684 }
685
686 std::transform(switches.begin(), switches.end(), std::back_inserter(argv),
687 [](const std::string& arg) -> const char* { return arg.c_str(); });
688
689 std::vector<const char*> dartEntrypointArgs;
690 for (NSString* argument in [_project dartEntrypointArguments]) {
691 dartEntrypointArgs.push_back([argument UTF8String]);
692 }
693
694 FlutterProjectArgs flutterArguments = {};
695 flutterArguments.struct_size = sizeof(FlutterProjectArgs);
696 flutterArguments.assets_path = _project.assetsPath.UTF8String;
697 flutterArguments.icu_data_path = _project.ICUDataPath.UTF8String;
698 flutterArguments.command_line_argc = static_cast<int>(argv.size());
699 flutterArguments.command_line_argv = argv.empty() ? nullptr : argv.data();
700 flutterArguments.platform_message_callback = (FlutterPlatformMessageCallback)OnPlatformMessage;
701 flutterArguments.update_semantics_callback2 = [](const FlutterSemanticsUpdate2* update,
702 void* user_data) {
703 // TODO(dkwingsmt): This callback only supports single-view, therefore it
704 // only operates on the implicit view. To support multi-view, we need a
705 // way to pass in the ID (probably through FlutterSemanticsUpdate).
706 FlutterEngine* engine = (__bridge FlutterEngine*)user_data;
707 [[engine viewControllerForIdentifier:kFlutterImplicitViewId] updateSemantics:update];
708 };
709 flutterArguments.custom_dart_entrypoint = entrypoint.UTF8String;
710 flutterArguments.shutdown_dart_vm_when_done = true;
711 flutterArguments.dart_entrypoint_argc = dartEntrypointArgs.size();
712 flutterArguments.dart_entrypoint_argv = dartEntrypointArgs.data();
713 flutterArguments.root_isolate_create_callback = _project.rootIsolateCreateCallback;
714 flutterArguments.log_message_callback = [](const char* tag, const char* message,
715 void* user_data) {
716 std::stringstream stream;
717 if (tag && tag[0]) {
718 stream << tag << ": ";
719 }
720 stream << message;
721 std::string log = stream.str();
722 [FlutterLogger logDirect:[NSString stringWithUTF8String:log.c_str()]];
723 };
724
725 flutterArguments.engine_id = reinterpret_cast<int64_t>((__bridge void*)self);
726 flutterArguments.enable_wide_gamut = _project.enableWideGamut;
727
728 BOOL mergedPlatformUIThread = YES;
729 NSNumber* enableMergedPlatformUIThread =
730 [[NSBundle mainBundle] objectForInfoDictionaryKey:@"FLTEnableMergedPlatformUIThread"];
731 if (enableMergedPlatformUIThread != nil) {
732 mergedPlatformUIThread = enableMergedPlatformUIThread.boolValue;
733 }
734
735 if (mergedPlatformUIThread) {
736 NSLog(@"Running with merged UI and platform thread. Experimental.");
737 }
738
739 // The task description needs to be created separately for platform task
740 // runner and UI task runner because each one has their own __bridge_retained
741 // engine user data.
742 FlutterTaskRunnerDescription platformTaskRunnerDescription =
743 [self createPlatformThreadTaskDescription];
744 std::optional<FlutterTaskRunnerDescription> uiTaskRunnerDescription;
745 if (mergedPlatformUIThread) {
746 uiTaskRunnerDescription = [self createPlatformThreadTaskDescription];
747 }
748
749 const FlutterCustomTaskRunners custom_task_runners = {
750 .struct_size = sizeof(FlutterCustomTaskRunners),
751 .platform_task_runner = &platformTaskRunnerDescription,
752 .thread_priority_setter = SetThreadPriority,
753 .ui_task_runner = uiTaskRunnerDescription ? &uiTaskRunnerDescription.value() : nullptr,
754 };
755 flutterArguments.custom_task_runners = &custom_task_runners;
756
757 [self loadAOTData:_project.assetsPath];
758 if (_aotData) {
759 flutterArguments.aot_data = _aotData;
760 }
761
762 flutterArguments.compositor = [self createFlutterCompositor];
763
764 flutterArguments.on_pre_engine_restart_callback = [](void* user_data) {
765 FlutterEngine* engine = (__bridge FlutterEngine*)user_data;
766 [engine engineCallbackOnPreEngineRestart];
767 };
768
769 flutterArguments.vsync_callback = [](void* user_data, intptr_t baton) {
770 FlutterEngine* engine = (__bridge FlutterEngine*)user_data;
771 [engine onVSync:baton];
772 };
773
774 flutterArguments.view_focus_change_request_callback =
775 [](const FlutterViewFocusChangeRequest* request, void* user_data) {
776 FlutterEngine* engine = (__bridge FlutterEngine*)user_data;
777 [engine onFocusChangeRequest:request];
778 };
779
780 FlutterRendererConfig rendererConfig = [_renderer createRendererConfig];
781 FlutterEngineResult result = _embedderAPI.Initialize(
782 FLUTTER_ENGINE_VERSION, &rendererConfig, &flutterArguments, (__bridge void*)(self), &_engine);
783 if (result != kSuccess) {
784 NSLog(@"Failed to initialize Flutter engine: error %d", result);
785 return NO;
786 }
787
788 result = _embedderAPI.RunInitialized(_engine);
789 if (result != kSuccess) {
790 NSLog(@"Failed to run an initialized engine: error %d", result);
791 return NO;
792 }
793
794 [self sendUserLocales];
795
796 // Update window metric for all view controllers.
797 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
798 FlutterViewController* nextViewController;
799 while ((nextViewController = [viewControllerEnumerator nextObject])) {
800 [self updateWindowMetricsForViewController:nextViewController];
801 }
802
803 [self updateDisplayConfig];
804 // Send the initial user settings such as brightness and text scale factor
805 // to the engine.
806 [self sendInitialSettings];
807 return YES;
808 }
809
810 - (void)loadAOTData:(NSString*)assetsDir {
811 if (!_embedderAPI.RunsAOTCompiledDartCode()) {
812 return;
813 }
814
815 BOOL isDirOut = false; // required for NSFileManager fileExistsAtPath.
816 NSFileManager* fileManager = [NSFileManager defaultManager];
817
818 // This is the location where the test fixture places the snapshot file.
819 // For applications built by Flutter tool, this is in "App.framework".
820 NSString* elfPath = [NSString pathWithComponents:@[ assetsDir, @"app_elf_snapshot.so" ]];
821
822 if (![fileManager fileExistsAtPath:elfPath isDirectory:&isDirOut]) {
823 return;
824 }
825
826 FlutterEngineAOTDataSource source = {};
827 source.type = kFlutterEngineAOTDataSourceTypeElfPath;
828 source.elf_path = [elfPath cStringUsingEncoding:NSUTF8StringEncoding];
829
830 auto result = _embedderAPI.CreateAOTData(&source, &_aotData);
831 if (result != kSuccess) {
832 NSLog(@"Failed to load AOT data from: %@", elfPath);
833 }
834 }
835
836 - (void)registerViewController:(FlutterViewController*)controller
837 forIdentifier:(FlutterViewIdentifier)viewIdentifier {
838 _macOSCompositor->AddView(viewIdentifier);
839 NSAssert(controller != nil, @"The controller must not be nil.");
840 if (!_multiViewEnabled) {
841 NSAssert(controller.engine == nil,
842 @"The FlutterViewController is unexpectedly attached to "
843 @"engine %@ before initialization.",
844 controller.engine);
845 }
846 NSAssert([_viewControllers objectForKey:@(viewIdentifier)] == nil,
847 @"The requested view ID is occupied.");
848 [_viewControllers setObject:controller forKey:@(viewIdentifier)];
849 [controller setUpWithEngine:self viewIdentifier:viewIdentifier];
850 NSAssert(controller.viewIdentifier == viewIdentifier, @"Failed to assign view ID.");
851 // Verify that the controller's property are updated accordingly. Failing the
852 // assertions is likely because either the FlutterViewController or the
853 // FlutterEngine is mocked. Please subclass these classes instead.
854 NSAssert(controller.attached, @"The FlutterViewController should switch to the attached mode "
855 @"after it is added to a FlutterEngine.");
856 NSAssert(controller.engine == self,
857 @"The FlutterViewController was added to %@, but its engine unexpectedly became %@.",
858 self, controller.engine);
859
860 if (controller.viewLoaded) {
861 [self viewControllerViewDidLoad:controller];
862 }
863
864 if (viewIdentifier != kFlutterImplicitViewId) {
865 // These will be overriden immediately after the FlutterView is created
866 // by actual values.
867 FlutterWindowMetricsEvent metrics{
868 .struct_size = sizeof(FlutterWindowMetricsEvent),
869 .width = 0,
870 .height = 0,
871 .pixel_ratio = 1.0,
872 };
873 bool added = false;
874 FlutterAddViewInfo info{.struct_size = sizeof(FlutterAddViewInfo),
875 .view_id = viewIdentifier,
876 .view_metrics = &metrics,
877 .user_data = &added,
878 .add_view_callback = [](const FlutterAddViewResult* r) {
879 auto added = reinterpret_cast<bool*>(r->user_data);
880 *added = true;
881 }};
882 // The callback should be called synchronously from platform thread.
883 _embedderAPI.AddView(_engine, &info);
884 FML_DCHECK(added);
885 if (!added) {
886 NSLog(@"Failed to add view with ID %llu", viewIdentifier);
887 }
888 }
889 }
890
891 - (void)viewControllerViewDidLoad:(FlutterViewController*)viewController {
892 __weak FlutterEngine* weakSelf = self;
893 FlutterTimeConverter* timeConverter = [[FlutterTimeConverter alloc] initWithEngine:self];
894 FlutterVSyncWaiter* waiter = [[FlutterVSyncWaiter alloc]
895 initWithDisplayLink:[FlutterDisplayLink displayLinkWithView:viewController.view]
896 block:^(CFTimeInterval timestamp, CFTimeInterval targetTimestamp,
897 uintptr_t baton) {
898 uint64_t timeNanos = [timeConverter CAMediaTimeToEngineTime:timestamp];
899 uint64_t targetTimeNanos =
900 [timeConverter CAMediaTimeToEngineTime:targetTimestamp];
901 FlutterEngine* engine = weakSelf;
902 if (engine) {
903 engine->_embedderAPI.OnVsync(_engine, baton, timeNanos, targetTimeNanos);
904 }
905 }];
906 @synchronized(_vsyncWaiters) {
907 FML_DCHECK([_vsyncWaiters objectForKey:@(viewController.viewIdentifier)] == nil);
908 [_vsyncWaiters setObject:waiter forKey:@(viewController.viewIdentifier)];
909 }
910 }
911
912 - (void)deregisterViewControllerForIdentifier:(FlutterViewIdentifier)viewIdentifier {
913 if (viewIdentifier != kFlutterImplicitViewId) {
914 bool removed = false;
915 FlutterRemoveViewInfo info;
916 info.struct_size = sizeof(FlutterRemoveViewInfo);
917 info.view_id = viewIdentifier;
918 info.user_data = &removed;
919 // RemoveViewCallback is not finished synchronously, the remove_view_callback
920 // is called from raster thread when the engine knows for sure that the resources
921 // associated with the view are no longer needed.
922 info.remove_view_callback = [](const FlutterRemoveViewResult* r) {
923 auto removed = reinterpret_cast<bool*>(r->user_data);
924 [FlutterRunLoop.mainRunLoop performBlock:^{
925 *removed = true;
926 }];
927 };
928 _embedderAPI.RemoveView(_engine, &info);
929 while (!removed) {
930 [[FlutterRunLoop mainRunLoop] pollFlutterMessagesOnce];
931 }
932 }
933
934 _macOSCompositor->RemoveView(viewIdentifier);
935
936 FlutterViewController* controller = [self viewControllerForIdentifier:viewIdentifier];
937 // The controller can be nil. The engine stores only a weak ref, and this
938 // method could have been called from the controller's dealloc.
939 if (controller != nil) {
940 [controller detachFromEngine];
941 NSAssert(!controller.attached,
942 @"The FlutterViewController unexpectedly stays attached after being removed. "
943 @"In unit tests, this is likely because either the FlutterViewController or "
944 @"the FlutterEngine is mocked. Please subclass these classes instead.");
945 }
946 [_viewControllers removeObjectForKey:@(viewIdentifier)];
947
948 FlutterVSyncWaiter* waiter = nil;
949 @synchronized(_vsyncWaiters) {
950 waiter = [_vsyncWaiters objectForKey:@(viewIdentifier)];
951 [_vsyncWaiters removeObjectForKey:@(viewIdentifier)];
952 }
953 [waiter invalidate];
954 }
955
956 - (void)shutDownIfNeeded {
957 if ([_viewControllers count] == 0 && !_allowHeadlessExecution) {
958 [self shutDownEngine];
959 }
960 }
961
962 - (FlutterViewController*)viewControllerForIdentifier:(FlutterViewIdentifier)viewIdentifier {
963 FlutterViewController* controller = [_viewControllers objectForKey:@(viewIdentifier)];
964 NSAssert(controller == nil || controller.viewIdentifier == viewIdentifier,
965 @"The stored controller has unexpected view ID.");
966 return controller;
967 }
968
969 - (void)setViewController:(FlutterViewController*)controller {
970 FlutterViewController* currentController =
971 [_viewControllers objectForKey:@(kFlutterImplicitViewId)];
972 if (currentController == controller) {
973 // From nil to nil, or from non-nil to the same controller.
974 return;
975 }
976 if (currentController == nil && controller != nil) {
977 // From nil to non-nil.
978 NSAssert(controller.engine == nil,
979 @"Failed to set view controller to the engine: "
980 @"The given FlutterViewController is already attached to an engine %@. "
981 @"If you wanted to create an FlutterViewController and set it to an existing engine, "
982 @"you should use FlutterViewController#init(engine:, nibName, bundle:) instead.",
983 controller.engine);
984 [self registerViewController:controller forIdentifier:kFlutterImplicitViewId];
985 } else if (currentController != nil && controller == nil) {
986 NSAssert(currentController.viewIdentifier == kFlutterImplicitViewId,
987 @"The default controller has an unexpected ID %llu", currentController.viewIdentifier);
988 // From non-nil to nil.
989 [self deregisterViewControllerForIdentifier:kFlutterImplicitViewId];
990 [self shutDownIfNeeded];
991 } else {
992 // From non-nil to a different non-nil view controller.
993 NSAssert(NO,
994 @"Failed to set view controller to the engine: "
995 @"The engine already has an implicit view controller %@. "
996 @"If you wanted to make the implicit view render in a different window, "
997 @"you should attach the current view controller to the window instead.",
998 [_viewControllers objectForKey:@(kFlutterImplicitViewId)]);
999 }
1000 }
1001
1002 - (FlutterViewController*)viewController {
1003 return [self viewControllerForIdentifier:kFlutterImplicitViewId];
1004 }
1005
1006 - (FlutterCompositor*)createFlutterCompositor {
1007 _compositor = {};
1008 _compositor.struct_size = sizeof(FlutterCompositor);
1009 _compositor.user_data = _macOSCompositor.get();
1010
1011 _compositor.create_backing_store_callback = [](const FlutterBackingStoreConfig* config, //
1012 FlutterBackingStore* backing_store_out, //
1013 void* user_data //
1014 ) {
1015 return reinterpret_cast<flutter::FlutterCompositor*>(user_data)->CreateBackingStore(
1016 config, backing_store_out);
1017 };
1018
1019 _compositor.collect_backing_store_callback = [](const FlutterBackingStore* backing_store, //
1020 void* user_data //
1021 ) { return true; };
1022
1023 _compositor.present_view_callback = [](const FlutterPresentViewInfo* info) {
1024 return reinterpret_cast<flutter::FlutterCompositor*>(info->user_data)
1025 ->Present(info->view_id, info->layers, info->layers_count);
1026 };
1027
1028 _compositor.avoid_backing_store_cache = true;
1029
1030 return &_compositor;
1031 }
1032
1033 - (id<FlutterBinaryMessenger>)binaryMessenger {
1034 return _binaryMessenger;
1035 }
1036
1037 #pragma mark - Framework-internal methods
1038
1039 - (void)addViewController:(FlutterViewController*)controller {
1040 if (!_multiViewEnabled) {
1041 // When multiview is disabled, the engine will only assign views to the implicit view ID.
1042 // The implicit view ID can be reused if and only if the implicit view is unassigned.
1043 NSAssert(self.viewController == nil,
1044 @"The engine already has a view controller for the implicit view.");
1045 self.viewController = controller;
1046 } else {
1047 // When multiview is enabled, the engine will assign views to a self-incrementing ID.
1048 // The implicit view ID can not be reused.
1049 FlutterViewIdentifier viewIdentifier = _nextViewIdentifier++;
1050 [self registerViewController:controller forIdentifier:viewIdentifier];
1051 }
1052 }
1053
1054 - (void)enableMultiView {
1055 if (!_multiViewEnabled) {
1056 NSAssert(self.viewController == nil,
1057 @"Multiview can only be enabled before adding any view controllers.");
1058 _multiViewEnabled = YES;
1059 }
1060 }
1061
1062 - (void)windowDidBecomeKey:(FlutterViewIdentifier)viewIdentifier {
1063 FlutterViewFocusEvent event{
1064 .struct_size = sizeof(FlutterViewFocusEvent),
1065 .view_id = viewIdentifier,
1066 .state = kFocused,
1067 .direction = kUndefined,
1068 };
1069 _embedderAPI.SendViewFocusEvent(_engine, &event);
1070 }
1071
1072 - (void)windowDidResignKey:(FlutterViewIdentifier)viewIdentifier {
1073 FlutterViewFocusEvent event{
1074 .struct_size = sizeof(FlutterViewFocusEvent),
1075 .view_id = viewIdentifier,
1076 .state = kUnfocused,
1077 .direction = kUndefined,
1078 };
1079 _embedderAPI.SendViewFocusEvent(_engine, &event);
1080 }
1081
1082 - (void)removeViewController:(nonnull FlutterViewController*)viewController {
1083 [self deregisterViewControllerForIdentifier:viewController.viewIdentifier];
1084 [self shutDownIfNeeded];
1085 }
1086
1087 - (BOOL)running {
1088 return _engine != nullptr;
1089 }
1090
1091 - (void)updateDisplayConfig:(NSNotification*)notification {
1092 [self updateDisplayConfig];
1093 }
1094
1095 - (NSArray<NSScreen*>*)screens {
1096 return [NSScreen screens];
1097 }
1098
1099 - (void)updateDisplayConfig {
1100 if (!_engine) {
1101 return;
1102 }
1103
1104 std::vector<FlutterEngineDisplay> displays;
1105 for (NSScreen* screen : [self screens]) {
1106 CGDirectDisplayID displayID =
1107 static_cast<CGDirectDisplayID>([screen.deviceDescription[@"NSScreenNumber"] integerValue]);
1108
1109 double devicePixelRatio = screen.backingScaleFactor;
1110 FlutterEngineDisplay display;
1111 display.struct_size = sizeof(display);
1112 display.display_id = displayID;
1113 display.single_display = false;
1114 display.width = static_cast<size_t>(screen.frame.size.width) * devicePixelRatio;
1115 display.height = static_cast<size_t>(screen.frame.size.height) * devicePixelRatio;
1116 display.device_pixel_ratio = devicePixelRatio;
1117
1118 CVDisplayLinkRef displayLinkRef = nil;
1119 CVReturn error = CVDisplayLinkCreateWithCGDisplay(displayID, &displayLinkRef);
1120
1121 if (error == 0) {
1122 CVTime nominal = CVDisplayLinkGetNominalOutputVideoRefreshPeriod(displayLinkRef);
1123 if (!(nominal.flags & kCVTimeIsIndefinite)) {
1124 double refreshRate = static_cast<double>(nominal.timeScale) / nominal.timeValue;
1125 display.refresh_rate = round(refreshRate);
1126 }
1127 CVDisplayLinkRelease(displayLinkRef);
1128 } else {
1129 display.refresh_rate = 0;
1130 }
1131
1132 displays.push_back(display);
1133 }
1134 _embedderAPI.NotifyDisplayUpdate(_engine, kFlutterEngineDisplaysUpdateTypeStartup,
1135 displays.data(), displays.size());
1136 }
1137
1138 - (void)onSettingsChanged:(NSNotification*)notification {
1139 // TODO(jonahwilliams): https://github.com/flutter/flutter/issues/32015.
1140 NSString* brightness =
1141 [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
1142 [_settingsChannel sendMessage:@{
1143 @"platformBrightness" : [brightness isEqualToString:@"Dark"] ? @"dark" : @"light",
1144 // TODO(jonahwilliams): https://github.com/flutter/flutter/issues/32006.
1145 @"textScaleFactor" : @1.0,
1146 @"alwaysUse24HourFormat" : @([FlutterHourFormat isAlwaysUse24HourFormat]),
1147 }];
1148 }
1149
1150 - (void)sendInitialSettings {
1151 // TODO(jonahwilliams): https://github.com/flutter/flutter/issues/32015.
1152 [[NSDistributedNotificationCenter defaultCenter]
1153 addObserver:self
1154 selector:@selector(onSettingsChanged:)
1155 name:@"AppleInterfaceThemeChangedNotification"
1156 object:nil];
1157 [self onSettingsChanged:nil];
1158 }
1159
1160 - (FlutterEngineProcTable&)embedderAPI {
1161 return _embedderAPI;
1162 }
1163
1164 - (nonnull NSString*)executableName {
1165 return [[[NSProcessInfo processInfo] arguments] firstObject] ?: @"Flutter";
1166 }
1167
1168 - (void)updateWindowMetricsForViewController:(FlutterViewController*)viewController {
1169 if (!_engine || !viewController || !viewController.viewLoaded) {
1170 return;
1171 }
1172 NSAssert([self viewControllerForIdentifier:viewController.viewIdentifier] == viewController,
1173 @"The provided view controller is not attached to this engine.");
1174 FlutterView* view = viewController.flutterView;
1175 CGRect scaledBounds = [view convertRectToBacking:view.bounds];
1176 CGSize scaledSize = scaledBounds.size;
1177 double pixelRatio = view.layer.contentsScale;
1178 auto displayId = [view.window.screen.deviceDescription[@"NSScreenNumber"] integerValue];
1179 FlutterWindowMetricsEvent windowMetricsEvent = {
1180 .struct_size = sizeof(windowMetricsEvent),
1181 .width = static_cast<size_t>(scaledSize.width),
1182 .height = static_cast<size_t>(scaledSize.height),
1183 .pixel_ratio = pixelRatio,
1184 .left = static_cast<size_t>(scaledBounds.origin.x),
1185 .top = static_cast<size_t>(scaledBounds.origin.y),
1186 .display_id = static_cast<uint64_t>(displayId),
1187 .view_id = viewController.viewIdentifier,
1188 };
1189 if (view.sizedToContents) {
1190 CGSize maximumContentSize = [view convertSizeToBacking:view.maximumContentSize];
1191 CGSize minimumContentSize = [view convertSizeToBacking:view.minimumContentSize];
1192 windowMetricsEvent.has_constraints = true;
1193 windowMetricsEvent.min_width_constraint = static_cast<size_t>(minimumContentSize.width);
1194 windowMetricsEvent.min_height_constraint = static_cast<size_t>(minimumContentSize.height);
1195 windowMetricsEvent.max_width_constraint = static_cast<size_t>(maximumContentSize.width);
1196 windowMetricsEvent.max_height_constraint = static_cast<size_t>(maximumContentSize.height);
1197 } else {
1198 windowMetricsEvent.min_width_constraint = static_cast<size_t>(scaledSize.width);
1199 windowMetricsEvent.min_height_constraint = static_cast<size_t>(scaledSize.height);
1200 windowMetricsEvent.max_width_constraint = static_cast<size_t>(scaledSize.width);
1201 windowMetricsEvent.max_height_constraint = static_cast<size_t>(scaledSize.height);
1202 }
1203 _embedderAPI.SendWindowMetricsEvent(_engine, &windowMetricsEvent);
1204 }
1205
1206 - (void)sendPointerEvent:(const FlutterPointerEvent&)event {
1207 _embedderAPI.SendPointerEvent(_engine, &event, 1);
1208 _lastViewWithPointerEvent = [self viewControllerForIdentifier:kFlutterImplicitViewId].flutterView;
1209 }
1210
1211 - (void)setSemanticsEnabled:(BOOL)enabled {
1212 if (_semanticsEnabled == enabled) {
1213 return;
1214 }
1215 _semanticsEnabled = enabled;
1216
1217 // Update all view controllers' bridges.
1218 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
1219 FlutterViewController* nextViewController;
1220 while ((nextViewController = [viewControllerEnumerator nextObject])) {
1221 [nextViewController notifySemanticsEnabledChanged];
1222 }
1223
1224 _embedderAPI.UpdateSemanticsEnabled(_engine, _semanticsEnabled);
1225 }
1226
1227 - (void)dispatchSemanticsAction:(FlutterSemanticsAction)action
1228 toTarget:(uint16_t)target
1229 withData:(fml::MallocMapping)data {
1230 _embedderAPI.DispatchSemanticsAction(_engine, target, action, data.GetMapping(), data.GetSize());
1231 }
1232
1233 - (FlutterPlatformViewController*)platformViewController {
1234 return _platformViewController;
1235 }
1236
1237 #pragma mark - Private methods
1238
1239 - (void)sendUserLocales {
1240 if (!self.running) {
1241 return;
1242 }
1243
1244 // Create a list of FlutterLocales corresponding to the preferred languages.
1245 NSMutableArray<NSLocale*>* locales = [NSMutableArray array];
1246 std::vector<FlutterLocale> flutterLocales;
1247 flutterLocales.reserve(locales.count);
1248 for (NSString* localeID in [NSLocale preferredLanguages]) {
1249 NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:localeID];
1250 [locales addObject:locale];
1251 flutterLocales.push_back(FlutterLocaleFromNSLocale(locale));
1252 }
1253 // Convert to a list of pointers, and send to the engine.
1254 std::vector<const FlutterLocale*> flutterLocaleList;
1255 flutterLocaleList.reserve(flutterLocales.size());
1256 std::transform(flutterLocales.begin(), flutterLocales.end(),
1257 std::back_inserter(flutterLocaleList),
1258 [](const auto& arg) -> const auto* { return &arg; });
1259 _embedderAPI.UpdateLocales(_engine, flutterLocaleList.data(), flutterLocaleList.size());
1260 }
1261
1262 - (void)engineCallbackOnPlatformMessage:(const FlutterPlatformMessage*)message {
1263 NSData* messageData = nil;
1264 if (message->message_size > 0) {
1265 messageData = [NSData dataWithBytesNoCopy:(void*)message->message
1266 length:message->message_size
1267 freeWhenDone:NO];
1268 }
1269 NSString* channel = @(message->channel);
1270 __block const FlutterPlatformMessageResponseHandle* responseHandle = message->response_handle;
1271 __block FlutterEngine* weakSelf = self;
1272 NSMutableArray* isResponseValid = self.isResponseValid;
1273 FlutterEngineSendPlatformMessageResponseFnPtr sendPlatformMessageResponse =
1274 _embedderAPI.SendPlatformMessageResponse;
1275 FlutterBinaryReply binaryResponseHandler = ^(NSData* response) {
1276 @synchronized(isResponseValid) {
1277 if (![isResponseValid[0] boolValue]) {
1278 // Ignore, engine was killed.
1279 return;
1280 }
1281 if (responseHandle) {
1282 sendPlatformMessageResponse(weakSelf->_engine, responseHandle,
1283 static_cast<const uint8_t*>(response.bytes), response.length);
1284 responseHandle = NULL;
1285 } else {
1286 NSLog(@"Error: Message responses can be sent only once. Ignoring duplicate response "
1287 "on channel '%@'.",
1288 channel);
1289 }
1290 }
1291 };
1292
1293 FlutterEngineHandlerInfo* handlerInfo = _messengerHandlers[channel];
1294 if (handlerInfo) {
1295 handlerInfo.handler(messageData, binaryResponseHandler);
1296 } else {
1297 binaryResponseHandler(nil);
1298 }
1299 }
1300
1301 - (void)engineCallbackOnPreEngineRestart {
1302 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
1303 FlutterViewController* nextViewController;
1304 while ((nextViewController = [viewControllerEnumerator nextObject])) {
1305 [nextViewController onPreEngineRestart];
1306 }
1307 [_windowController closeAllWindows];
1308 [_platformViewController reset];
1309 _keyboardManager = [[FlutterKeyboardManager alloc] initWithDelegate:self];
1310 }
1311
1312 // This will be called on UI thread, which maybe or may not be platform thread,
1313 // depending on the configuration.
1314 - (void)onVSync:(uintptr_t)baton {
1315 auto block = ^{
1316 // TODO(knopp): Use vsync waiter for correct view.
1317 // https://github.com/flutter/flutter/issues/142845
1318 FlutterVSyncWaiter* waiter =
1319 [_vsyncWaiters objectForKey:[_vsyncWaiters.keyEnumerator nextObject]];
1320 if (waiter != nil) {
1321 [waiter waitForVSync:baton];
1322 } else {
1323 // Sometimes there is a vsync request right after the last view is removed.
1324 // It still need to be handled, otherwise the engine will stop producing frames
1325 // even if a new view is added later.
1326 self.embedderAPI.OnVsync(_engine, baton, 0, 0);
1327 }
1328 };
1329 if ([NSThread isMainThread]) {
1330 block();
1331 } else {
1332 [FlutterRunLoop.mainRunLoop performBlock:block];
1333 }
1334 }
1335
1336 /**
1337 * Note: Called from dealloc. Should not use accessors or other methods.
1338 */
1339 - (void)shutDownEngine {
1340 if (_engine == nullptr) {
1341 return;
1342 }
1343
1344 FlutterEngineResult result = _embedderAPI.Deinitialize(_engine);
1345 if (result != kSuccess) {
1346 NSLog(@"Could not de-initialize the Flutter engine: error %d", result);
1347 }
1348
1349 result = _embedderAPI.Shutdown(_engine);
1350 if (result != kSuccess) {
1351 NSLog(@"Failed to shut down Flutter engine: error %d", result);
1352 }
1353 _engine = nullptr;
1354 }
1355
1356 + (FlutterEngine*)engineForIdentifier:(int64_t)identifier {
1357 NSAssert([[NSThread currentThread] isMainThread], @"Must be called on the main thread.");
1358 return (__bridge FlutterEngine*)reinterpret_cast<void*>(identifier);
1359 }
1360
1361 - (void)setUpPlatformViewChannel {
1362 _platformViewsChannel =
1363 [FlutterMethodChannel methodChannelWithName:@"flutter/platform_views"
1364 binaryMessenger:self.binaryMessenger
1365 codec:[FlutterStandardMethodCodec sharedInstance]];
1366
1367 __weak FlutterEngine* weakSelf = self;
1368 [_platformViewsChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
1369 [[weakSelf platformViewController] handleMethodCall:call result:result];
1370 }];
1371 }
1372
1373 - (void)setUpAccessibilityChannel {
1374 _accessibilityChannel = [FlutterBasicMessageChannel
1375 messageChannelWithName:@"flutter/accessibility"
1376 binaryMessenger:self.binaryMessenger
1377 codec:[FlutterStandardMessageCodec sharedInstance]];
1378 __weak FlutterEngine* weakSelf = self;
1379 [_accessibilityChannel setMessageHandler:^(id message, FlutterReply reply) {
1380 [weakSelf handleAccessibilityEvent:message];
1381 }];
1382 }
1383 - (void)setUpNotificationCenterListeners {
1384 NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
1385 // macOS fires this private message when VoiceOver turns on or off.
1386 [center addObserver:self
1387 selector:@selector(onAccessibilityStatusChanged:)
1388 name:kEnhancedUserInterfaceNotification
1389 object:nil];
1390 [center addObserver:self
1391 selector:@selector(applicationWillTerminate:)
1392 name:NSApplicationWillTerminateNotification
1393 object:nil];
1394 [center addObserver:self
1395 selector:@selector(windowDidChangeScreen:)
1396 name:NSWindowDidChangeScreenNotification
1397 object:nil];
1398 [center addObserver:self
1399 selector:@selector(updateDisplayConfig:)
1400 name:NSApplicationDidChangeScreenParametersNotification
1401 object:nil];
1402 }
1403
1404 - (void)addInternalPlugins {
1405 __weak FlutterEngine* weakSelf = self;
1406 [FlutterMouseCursorPlugin registerWithRegistrar:[self registrarForPlugin:@"mousecursor"]
1407 delegate:self];
1408 [FlutterMenuPlugin registerWithRegistrar:[self registrarForPlugin:@"menu"]];
1409
1410 _settingsChannel =
1411 [FlutterBasicMessageChannel messageChannelWithName:kFlutterSettingsChannel
1412 binaryMessenger:self.binaryMessenger
1413 codec:[FlutterJSONMessageCodec sharedInstance]];
1414 _platformChannel =
1415 [FlutterMethodChannel methodChannelWithName:kFlutterPlatformChannel
1416 binaryMessenger:self.binaryMessenger
1417 codec:[FlutterJSONMethodCodec sharedInstance]];
1418 [_platformChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
1419 [weakSelf handleMethodCall:call result:result];
1420 }];
1421
1422 _screenshotChannel =
1423 [FlutterMethodChannel methodChannelWithName:@"flutter/screenshot"
1424 binaryMessenger:self.binaryMessenger
1425 codec:[FlutterStandardMethodCodec sharedInstance]];
1426 [_screenshotChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
1427 FlutterEngine* strongSelf = weakSelf;
1428 if (!strongSelf) {
1429 return result([FlutterError errorWithCode:@"invalid_state"
1430 message:@"Engine deallocated."
1431 details:nil]);
1432 }
1433
1434 FlutterViewController* viewController =
1435 [strongSelf viewControllerForIdentifier:flutter::kFlutterImplicitViewId];
1436 if (!viewController) {
1437 return result([FlutterError errorWithCode:@"failure"
1438 message:@"No view controller."
1439 details:nil]);
1440 }
1441
1442 NSArray<FlutterSurface*>* frontSurfaces =
1443 viewController.flutterView.surfaceManager.frontSurfaces;
1444 if (frontSurfaces.count == 0) {
1445 return result([FlutterError errorWithCode:@"failure"
1446 message:@"No front surfaces."
1447 details:nil]);
1448 }
1449
1450 // Use the first front surface (the main backing store).
1451 FlutterSurface* surface = frontSurfaces.firstObject;
1452 IOSurfaceRef ioSurface = surface.ioSurface;
1453
1454 size_t width = IOSurfaceGetWidth(ioSurface);
1455 size_t height = IOSurfaceGetHeight(ioSurface);
1456 size_t bytesPerRow = IOSurfaceGetBytesPerRow(ioSurface);
1457 size_t bytesPerElement = IOSurfaceGetBytesPerElement(ioSurface);
1458 uint32_t pixelFormat = (uint32_t)IOSurfaceGetPixelFormat(ioSurface);
1459
1460 NSString* formatString;
1461 switch (pixelFormat) {
1462 case kCVPixelFormatType_40ARGBLEWideGamut:
1463 formatString = @"MTLPixelFormatBGRA10_XR";
1464 break;
1465 case kCVPixelFormatType_32BGRA:
1466 formatString = @"MTLPixelFormatBGRA8Unorm";
1467 break;
1468 default:
1469 formatString = [NSString stringWithFormat:@"Unknown(%u)", pixelFormat];
1470 break;
1471 }
1472
1473 IOSurfaceLock(ioSurface, kIOSurfaceLockReadOnly, nil);
1474 void* baseAddress = IOSurfaceGetBaseAddress(ioSurface);
1475
1476 // Copy pixel data row by row into a tightly-packed buffer.
1477 size_t packedBytesPerRow = width * bytesPerElement;
1478 NSMutableData* packedData = [NSMutableData dataWithLength:packedBytesPerRow * height];
1479 uint8_t* dest = (uint8_t*)packedData.mutableBytes;
1480 for (size_t row = 0; row < height; row++) {
1481 memcpy(dest + row * packedBytesPerRow, (uint8_t*)baseAddress + row * bytesPerRow,
1482 packedBytesPerRow);
1483 }
1484
1485 IOSurfaceUnlock(ioSurface, kIOSurfaceLockReadOnly, nil);
1486
1487 return result(@[
1488 @(width),
1489 @(height),
1490 formatString,
1491 [FlutterStandardTypedData typedDataWithBytes:packedData],
1492 ]);
1493 }];
1494 }
1495
1496 - (void)didUpdateMouseCursor:(NSCursor*)cursor {
1497 // Mouse cursor plugin does not specify which view is responsible for changing the cursor,
1498 // so the reasonable assumption here is that cursor change is a result of a mouse movement
1499 // and thus the cursor will be paired with last Flutter view that reveived mouse event.
1500 [_lastViewWithPointerEvent didUpdateMouseCursor:cursor];
1501 }
1502
1503 - (void)applicationWillTerminate:(NSNotification*)notification {
1504 [self shutDownEngine];
1505 }
1506
1507 - (void)windowDidChangeScreen:(NSNotification*)notification {
1508 // Update window metric for all view controllers since the display_id has
1509 // changed.
1510 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
1511 FlutterViewController* nextViewController;
1512 while ((nextViewController = [viewControllerEnumerator nextObject])) {
1513 [self updateWindowMetricsForViewController:nextViewController];
1514 [nextViewController updateWideGamutForScreen];
1515 }
1516 }
1517
1518 - (void)onAccessibilityStatusChanged:(NSNotification*)notification {
1519 BOOL enabled = [notification.userInfo[kEnhancedUserInterfaceKey] boolValue];
1520 NSEnumerator* viewControllerEnumerator = [_viewControllers objectEnumerator];
1521 FlutterViewController* nextViewController;
1522 while ((nextViewController = [viewControllerEnumerator nextObject])) {
1523 [nextViewController onAccessibilityStatusChanged:enabled];
1524 }
1525
1526 self.semanticsEnabled = enabled;
1527 }
1528 - (void)handleAccessibilityEvent:(NSDictionary<NSString*, id>*)annotatedEvent {
1529 NSString* type = annotatedEvent[@"type"];
1530 if ([type isEqualToString:@"announce"]) {
1531 NSString* message = annotatedEvent[@"data"][@"message"];
1532 NSNumber* assertiveness = annotatedEvent[@"data"][@"assertiveness"];
1533 if (message == nil) {
1534 return;
1535 }
1536
1537 NSAccessibilityPriorityLevel priority = [assertiveness isEqualToNumber:@1]
1538 ? NSAccessibilityPriorityHigh
1539 : NSAccessibilityPriorityMedium;
1540
1541 [self announceAccessibilityMessage:message withPriority:priority];
1542 }
1543 }
1544
1545 - (void)announceAccessibilityMessage:(NSString*)message
1546 withPriority:(NSAccessibilityPriorityLevel)priority {
1547 NSAccessibilityPostNotificationWithUserInfo(
1548 [self viewControllerForIdentifier:kFlutterImplicitViewId].flutterView,
1549 NSAccessibilityAnnouncementRequestedNotification,
1550 @{NSAccessibilityAnnouncementKey : message, NSAccessibilityPriorityKey : @(priority)});
1551 }
1552 - (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
1553 if ([call.method isEqualToString:@"SystemNavigator.pop"]) {
1554 [[NSApplication sharedApplication] terminate:self];
1555 result(nil);
1556 } else if ([call.method isEqualToString:@"SystemSound.play"]) {
1557 [self playSystemSound:call.arguments];
1558 result(nil);
1559 } else if ([call.method isEqualToString:@"Clipboard.getData"]) {
1560 result([self getClipboardData:call.arguments]);
1561 } else if ([call.method isEqualToString:@"Clipboard.setData"]) {
1562 [self setClipboardData:call.arguments];
1563 result(nil);
1564 } else if ([call.method isEqualToString:@"Clipboard.hasStrings"]) {
1565 result(@{@"value" : @([self clipboardHasStrings])});
1566 } else if ([call.method isEqualToString:@"System.exitApplication"]) {
1567 if ([self terminationHandler] == nil) {
1568 // If the termination handler isn't set, then either we haven't
1569 // initialized it yet, or (more likely) the NSApp delegate isn't a
1570 // FlutterAppDelegate, so it can't cancel requests to exit. So, in that
1571 // case, just terminate when requested.
1572 [NSApp terminate:self];
1573 result(nil);
1574 } else {
1575 [[self terminationHandler] handleRequestAppExitMethodCall:call.arguments result:result];
1576 }
1577 } else if ([call.method isEqualToString:@"System.initializationComplete"]) {
1578 if ([self terminationHandler] != nil) {
1579 [self terminationHandler].acceptingRequests = YES;
1580 }
1581 result(nil);
1582 } else {
1583 result(FlutterMethodNotImplemented);
1584 }
1585 }
1586
1587 - (void)playSystemSound:(NSString*)soundType {
1588 if ([soundType isEqualToString:@"SystemSoundType.alert"]) {
1589 NSBeep();
1590 }
1591 }
1592
1593 - (NSDictionary*)getClipboardData:(NSString*)format {
1594 if ([format isEqualToString:@(kTextPlainFormat)]) {
1595 NSString* stringInPasteboard = [self.pasteboard stringForType:NSPasteboardTypeString];
1596 return stringInPasteboard == nil ? nil : @{@"text" : stringInPasteboard};
1597 }
1598 return nil;
1599 }
1600
1601 - (void)setClipboardData:(NSDictionary*)data {
1602 NSString* text = data[@"text"];
1603 [self.pasteboard clearContents];
1604 if (text && ![text isEqual:[NSNull null]]) {
1605 [self.pasteboard setString:text forType:NSPasteboardTypeString];
1606 }
1607 }
1608
1609 - (BOOL)clipboardHasStrings {
1610 return [self.pasteboard stringForType:NSPasteboardTypeString].length > 0;
1611 }
1612
1613 - (std::vector<std::string>)switches {
1614 return flutter::GetSwitchesFromEnvironment();
1615 }
1616
1617 #pragma mark - FlutterAppLifecycleDelegate
1618
1619 - (void)setApplicationState:(flutter::AppLifecycleState)state {
1620 NSString* nextState =
1621 [[NSString alloc] initWithCString:flutter::AppLifecycleStateToString(state)];
1622 [self sendOnChannel:kFlutterLifecycleChannel
1623 message:[nextState dataUsingEncoding:NSUTF8StringEncoding]];
1624 }
1625
1626 /**
1627 * Called when the |FlutterAppDelegate| gets the applicationWillBecomeActive
1628 * notification.
1629 */
1630 - (void)handleWillBecomeActive:(NSNotification*)notification {
1631 _active = YES;
1632 if (!_visible) {
1633 [self setApplicationState:flutter::AppLifecycleState::kHidden];
1634 } else {
1635 [self setApplicationState:flutter::AppLifecycleState::kResumed];
1636 }
1637 }
1638
1639 /**
1640 * Called when the |FlutterAppDelegate| gets the applicationWillResignActive
1641 * notification.
1642 */
1643 - (void)handleWillResignActive:(NSNotification*)notification {
1644 _active = NO;
1645 if (!_visible) {
1646 [self setApplicationState:flutter::AppLifecycleState::kHidden];
1647 } else {
1648 [self setApplicationState:flutter::AppLifecycleState::kInactive];
1649 }
1650 }
1651
1652 /**
1653 * Called when the |FlutterAppDelegate| gets the applicationDidUnhide
1654 * notification.
1655 */
1656 - (void)handleDidChangeOcclusionState:(NSNotification*)notification {
1657 NSApplicationOcclusionState occlusionState = [[NSApplication sharedApplication] occlusionState];
1658 if (occlusionState & NSApplicationOcclusionStateVisible) {
1659 _visible = YES;
1660 if (_active) {
1661 [self setApplicationState:flutter::AppLifecycleState::kResumed];
1662 } else {
1663 [self setApplicationState:flutter::AppLifecycleState::kInactive];
1664 }
1665 } else {
1666 _visible = NO;
1667 [self setApplicationState:flutter::AppLifecycleState::kHidden];
1668 }
1669 }
1670
1671 #pragma mark - FlutterBinaryMessenger
1672
1673 - (void)sendOnChannel:(nonnull NSString*)channel message:(nullable NSData*)message {
1674 [self sendOnChannel:channel message:message binaryReply:nil];
1675 }
1676
1677 - (void)sendOnChannel:(NSString*)channel
1678 message:(NSData* _Nullable)message
1679 binaryReply:(FlutterBinaryReply _Nullable)callback {
1680 FlutterPlatformMessageResponseHandle* response_handle = nullptr;
1681 if (callback) {
1682 struct Captures {
1683 FlutterBinaryReply reply;
1684 };
1685 auto captures = std::make_unique<Captures>();
1686 captures->reply = callback;
1687 auto message_reply = [](const uint8_t* data, size_t data_size, void* user_data) {
1688 auto captures = reinterpret_cast<Captures*>(user_data);
1689 NSData* reply_data = nil;
1690 if (data != nullptr && data_size > 0) {
1691 reply_data = [NSData dataWithBytes:static_cast<const void*>(data) length:data_size];
1692 }
1693 captures->reply(reply_data);
1694 delete captures;
1695 };
1696
1697 FlutterEngineResult create_result = _embedderAPI.PlatformMessageCreateResponseHandle(
1698 _engine, message_reply, captures.get(), &response_handle);
1699 if (create_result != kSuccess) {
1700 NSLog(@"Failed to create a FlutterPlatformMessageResponseHandle (%d)", create_result);
1701 return;
1702 }
1703 captures.release();
1704 }
1705
1706 FlutterPlatformMessage platformMessage = {
1707 .struct_size = sizeof(FlutterPlatformMessage),
1708 .channel = [channel UTF8String],
1709 .message = static_cast<const uint8_t*>(message.bytes),
1710 .message_size = message.length,
1711 .response_handle = response_handle,
1712 };
1713
1714 FlutterEngineResult message_result = _embedderAPI.SendPlatformMessage(_engine, &platformMessage);
1715 if (message_result != kSuccess) {
1716 NSLog(@"Failed to send message to Flutter engine on channel '%@' (%d).", channel,
1717 message_result);
1718 }
1719
1720 if (response_handle != nullptr) {
1721 FlutterEngineResult release_result =
1722 _embedderAPI.PlatformMessageReleaseResponseHandle(_engine, response_handle);
1723 if (release_result != kSuccess) {
1724 NSLog(@"Failed to release the response handle (%d).", release_result);
1725 };
1726 }
1727 }
1728
1729 - (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(nonnull NSString*)channel
1730 binaryMessageHandler:
1731 (nullable FlutterBinaryMessageHandler)handler {
1732 _currentMessengerConnection += 1;
1733 _messengerHandlers[channel] =
1734 [[FlutterEngineHandlerInfo alloc] initWithConnection:@(_currentMessengerConnection)
1735 handler:[handler copy]];
1736 return _currentMessengerConnection;
1737 }
1738
1739 - (void)cleanUpConnection:(FlutterBinaryMessengerConnection)connection {
1740 // Find the _messengerHandlers that has the required connection, and record its
1741 // channel.
1742 NSString* foundChannel = nil;
1743 for (NSString* key in [_messengerHandlers allKeys]) {
1744 FlutterEngineHandlerInfo* handlerInfo = [_messengerHandlers objectForKey:key];
1745 if ([handlerInfo.connection isEqual:@(connection)]) {
1746 foundChannel = key;
1747 break;
1748 }
1749 }
1750 if (foundChannel) {
1751 [_messengerHandlers removeObjectForKey:foundChannel];
1752 }
1753 }
1754
1755 #pragma mark - FlutterPluginRegistry
1756
1757 - (id<FlutterPluginRegistrar>)registrarForPlugin:(NSString*)pluginName {
1758 id<FlutterPluginRegistrar> registrar = self.pluginRegistrars[pluginName];
1759 if (!registrar) {
1760 FlutterEngineRegistrar* registrarImpl =
1761 [[FlutterEngineRegistrar alloc] initWithPlugin:pluginName flutterEngine:self];
1762 self.pluginRegistrars[pluginName] = registrarImpl;
1763 registrar = registrarImpl;
1764 }
1765 return registrar;
1766 }
1767
1768 - (nullable NSObject*)valuePublishedByPlugin:(NSString*)pluginName {
1769 return self.pluginRegistrars[pluginName].publishedValue;
1770 }
1771
1772 #pragma mark - FlutterTextureRegistrar
1773
1774 - (int64_t)registerTexture:(id<FlutterTexture>)texture {
1775 return [_renderer registerTexture:texture];
1776 }
1777
1778 - (BOOL)registerTextureWithID:(int64_t)textureId {
1779 return _embedderAPI.RegisterExternalTexture(_engine, textureId) == kSuccess;
1780 }
1781
1782 - (void)textureFrameAvailable:(int64_t)textureID {
1783 [_renderer textureFrameAvailable:textureID];
1784 }
1785
1786 - (BOOL)markTextureFrameAvailable:(int64_t)textureID {
1787 return _embedderAPI.MarkExternalTextureFrameAvailable(_engine, textureID) == kSuccess;
1788 }
1789
1790 - (void)unregisterTexture:(int64_t)textureID {
1791 [_renderer unregisterTexture:textureID];
1792 }
1793
1794 - (BOOL)unregisterTextureWithID:(int64_t)textureID {
1795 return _embedderAPI.UnregisterExternalTexture(_engine, textureID) == kSuccess;
1796 }
1797
1798 #pragma mark - Task runner integration
1799
1800 - (void)postMainThreadTask:(FlutterTask)task targetTimeInNanoseconds:(uint64_t)targetTime {
1801 __weak FlutterEngine* weakSelf = self;
1802
1803 const auto engine_time = _embedderAPI.GetCurrentTime();
1804 [FlutterRunLoop.mainRunLoop
1805 performAfterDelay:(targetTime - (double)engine_time) / NSEC_PER_SEC
1806 block:^{
1807 FlutterEngine* self = weakSelf;
1808 if (self != nil && self->_engine != nil) {
1809 auto result = _embedderAPI.RunTask(self->_engine, &task);
1810 if (result != kSuccess) {
1811 NSLog(@"Could not post a task to the Flutter engine.");
1812 }
1813 }
1814 }];
1815 }
1816
1817 // Getter used by test harness, only exposed through the FlutterEngine(Test) category
1818 - (flutter::FlutterCompositor*)macOSCompositor {
1819 return _macOSCompositor.get();
1820 }
1821
1822 #pragma mark - FlutterKeyboardManagerDelegate
1823
1824 /**
1825 * Dispatches the given pointer event data to engine.
1826 */
1827 - (void)sendKeyEvent:(const FlutterKeyEvent&)event
1828 callback:(FlutterKeyEventCallback)callback
1829 userData:(void*)userData {
1830 _embedderAPI.SendKeyEvent(_engine, &event, callback, userData);
1831 }
1832
1833 @end
FlutterAppDelegate.h
FlutterAppDelegate_Internal.h
FlutterBinaryReply
NS_ASSUME_NONNULL_BEGIN typedef void(^ FlutterBinaryReply)(NSData *_Nullable reply)
FlutterBinaryMessageHandler
void(^ FlutterBinaryMessageHandler)(NSData *_Nullable message, FlutterBinaryReply reply)
Definition: FlutterBinaryMessenger.h:30
FlutterBinaryMessengerConnection
int64_t FlutterBinaryMessengerConnection
Definition: FlutterBinaryMessenger.h:32
FlutterBinaryMessengerRelay.h
FlutterResult
void(^ FlutterResult)(id _Nullable result)
Definition: FlutterChannels.h:194
FlutterMethodNotImplemented
FLUTTER_DARWIN_EXPORT NSObject const * FlutterMethodNotImplemented
_connection
FlutterBinaryMessengerConnection _connection
Definition: FlutterChannels.mm:72
FlutterCompositor.h
FlutterDartProject_Internal.h
FlutterDisplayLink.h
FlutterEngine.h
_platformViewsChannel
FlutterMethodChannel * _platformViewsChannel
Definition: FlutterEngine.mm:449
_aotData
_FlutterEngineAOTData * _aotData
Definition: FlutterEngine.mm:433
_macOSCompositor
std::unique_ptr< flutter::FlutterCompositor > _macOSCompositor
Definition: FlutterEngine.mm:437
kMainThreadPriority
static const int kMainThreadPriority
Definition: FlutterEngine.mm:510
OnPlatformMessage
static void OnPlatformMessage(const FlutterPlatformMessage *message, void *user_data)
Definition: FlutterEngine.mm:408
_platformViewController
FlutterPlatformViewController * _platformViewController
Definition: FlutterEngine.mm:453
_accessibilityChannel
FlutterBasicMessageChannel * _accessibilityChannel
Definition: FlutterEngine.mm:462
_settingsChannel
FlutterBasicMessageChannel * _settingsChannel
Definition: FlutterEngine.mm:459
FlutterLocaleFromNSLocale
static FlutterLocale FlutterLocaleFromNSLocale(NSLocale *locale)
Definition: FlutterEngine.mm:51
_allowHeadlessExecution
BOOL _allowHeadlessExecution
Definition: FlutterEngine.mm:430
_messengerHandlers
NSMutableDictionary< NSString *, FlutterEngineHandlerInfo * > * _messengerHandlers
Definition: FlutterEngine.mm:423
_currentMessengerConnection
FlutterBinaryMessengerConnection _currentMessengerConnection
Definition: FlutterEngine.mm:427
_platformChannel
FlutterMethodChannel * _platformChannel
Definition: FlutterEngine.mm:465
kFlutterLifecycleChannel
NSString *const kFlutterLifecycleChannel
Definition: FlutterEngine.mm:43
kEnhancedUserInterfaceNotification
static NSString *const kEnhancedUserInterfaceNotification
The private notification for voice over.
Definition: FlutterEngine.mm:62
_vsyncWaiters
NSMapTable< NSNumber *, FlutterVSyncWaiter * > * _vsyncWaiters
Definition: FlutterEngine.mm:481
_nextViewIdentifier
FlutterViewIdentifier _nextViewIdentifier
Definition: FlutterEngine.mm:500
kFlutterPlatformChannel
NSString *const kFlutterPlatformChannel
Definition: FlutterEngine.mm:41
_screenshotChannel
FlutterMethodChannel * _screenshotChannel
Definition: FlutterEngine.mm:468
_textInputPlugin
FlutterTextInputPlugin * _textInputPlugin
Definition: FlutterEngine.mm:491
_project
FlutterDartProject * _project
Definition: FlutterEngine.mm:415
_viewControllers
NSMapTable * _viewControllers
Definition: FlutterEngine.mm:442
_multiViewEnabled
BOOL _multiViewEnabled
Definition: FlutterEngine.mm:496
_compositor
FlutterCompositor _compositor
Definition: FlutterEngine.mm:445
_lastViewWithPointerEvent
__weak FlutterView * _lastViewWithPointerEvent
Definition: FlutterEngine.mm:485
_keyboardManager
FlutterKeyboardManager * _keyboardManager
Definition: FlutterEngine.mm:488
_windowController
FlutterWindowController * _windowController
Definition: FlutterEngine.mm:456
_terminator
FlutterTerminationCallback _terminator
Definition: FlutterEngine.mm:190
kTextPlainFormat
constexpr char kTextPlainFormat[]
Clipboard plain text format.
Definition: FlutterEngine.mm:67
_flutterEngine
__weak FlutterEngine * _flutterEngine
Definition: FlutterEngine.mm:323
_visible
BOOL _visible
Definition: FlutterEngine.mm:474
_active
BOOL _active
Definition: FlutterEngine.mm:471
_binaryMessenger
FlutterBinaryMessengerRelay * _binaryMessenger
Definition: FlutterEngine.mm:477
kEnhancedUserInterfaceKey
static NSString *const kEnhancedUserInterfaceKey
Definition: FlutterEngine.mm:64
kFlutterSettingsChannel
NSString *const kFlutterSettingsChannel
Definition: FlutterEngine.mm:42
FlutterEngine_Internal.h
FlutterTerminationCallback
NS_ASSUME_NONNULL_BEGIN typedef void(^ FlutterTerminationCallback)(id _Nullable sender)
FlutterMouseCursorPlugin.h
FlutterPlatformViewController.h
FlutterRenderer.h
FlutterTimeConverter.h
FlutterVSyncWaiter.h
FlutterView.h
FlutterViewIdentifier
int64_t FlutterViewIdentifier
Definition: FlutterViewController.h:21
FlutterViewController_Internal.h
FlutterViewEngineProvider.h
app_lifecycle_state.h
FlutterDartProject
Definition: FlutterDartProject.mm:39
+[FlutterDartProject lookupKeyForAsset:fromPackage:]
NSString * lookupKeyForAsset:fromPackage:(NSString *asset,[fromPackage] NSString *package)
Definition: FlutterDartProject.mm:158
+[FlutterDartProject lookupKeyForAsset:]
NSString * lookupKeyForAsset:(NSString *asset)
Definition: FlutterDartProject.mm:149
FlutterEngineTerminationHandler
Definition: FlutterEngine.mm:190
FlutterPasteboard
Definition: FlutterEngine.mm:288
-[FlutterPasteboard clearContents]
NSInteger clearContents()
Definition: FlutterEngine.mm:290
flutter::FlutterCompositor
Definition: FlutterCompositor.h:36
engine_switches.h
FlutterAppDelegate
Definition: FlutterAppDelegate.h:54
FlutterBasicMessageChannel
Definition: FlutterChannels.h:38
+[FlutterBasicMessageChannel messageChannelWithName:binaryMessenger:codec:]
instancetype messageChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMessageCodec > *codec)
Definition: FlutterChannels.mm:82
FlutterBinaryMessengerRelay
Definition: FlutterBinaryMessengerRelay.h:14
FlutterDisplayLink
Definition: FlutterDisplayLink.h:22
+[FlutterDisplayLink displayLinkWithView:]
instancetype displayLinkWithView:(NSView *view)
Definition: FlutterDisplayLink.mm:292
FlutterEngineHandlerInfo
Definition: FlutterEngine.mm:74
FlutterEngineHandlerInfo::connection
NSNumber * connection
Definition: FlutterEngine.mm:79
FlutterEngineHandlerInfo::handler
FlutterBinaryMessageHandler handler
Definition: FlutterEngine.mm:78
FlutterEngine
Definition: FlutterEngine.h:32
FlutterEngine::binaryMessenger
id< FlutterBinaryMessenger > binaryMessenger
Definition: FlutterEngine.h:92
FlutterEngineRegistrar
Definition: FlutterEngine.mm:309
FlutterEngineRegistrar::publishedValue
NSObject * publishedValue
Definition: FlutterEngine.mm:320
FlutterError
Definition: FlutterCodecs.h:247
+[FlutterError errorWithCode:message:details:]
instancetype errorWithCode:message:details:(NSString *code,[message] NSString *_Nullable message,[details] id _Nullable details)
FlutterHourFormat
Definition: FlutterHourFormat.h:11
+[FlutterHourFormat isAlwaysUse24HourFormat]
BOOL isAlwaysUse24HourFormat()
Definition: FlutterHourFormat.mm:8
FlutterJSONMessageCodec
Definition: FlutterCodecs.h:81
FlutterJSONMethodCodec
Definition: FlutterCodecs.h:455
FlutterKeyboardManager
Definition: FlutterKeyboardManager.h:79
FlutterMethodCall
Definition: FlutterCodecs.h:221
+[FlutterMethodCall methodCallWithMethodName:arguments:]
instancetype methodCallWithMethodName:arguments:(NSString *method,[arguments] id _Nullable arguments)
FlutterMethodCall::method
NSString * method
Definition: FlutterCodecs.h:233
FlutterMethodCall::arguments
id arguments
Definition: FlutterCodecs.h:238
FlutterMethodChannel
Definition: FlutterChannels.h:221
-[FlutterMethodChannel setMethodCallHandler:]
void setMethodCallHandler:(FlutterMethodCallHandler _Nullable handler)
+[FlutterMethodChannel methodChannelWithName:binaryMessenger:codec:]
instancetype methodChannelWithName:binaryMessenger:codec:(NSString *name,[binaryMessenger] NSObject< FlutterBinaryMessenger > *messenger,[codec] NSObject< FlutterMethodCodec > *codec)
FlutterMouseCursorPlugin
Definition: FlutterMouseCursorPlugin.h:23
+[FlutterMouseCursorPlugin registerWithRegistrar:delegate:]
void registerWithRegistrar:delegate:(nonnull id< FlutterPluginRegistrar > registrar,[delegate] nullable id< FlutterMouseCursorPluginDelegate > delegate)
FlutterPlatformViewController
Definition: FlutterPlatformViewController.h:18
FlutterRenderer
Definition: FlutterRenderer.h:19
FlutterStandardMessageCodec
Definition: FlutterCodecs.h:209
FlutterStandardMethodCodec
Definition: FlutterCodecs.h:469
FlutterStandardTypedData
Definition: FlutterCodecs.h:301
+[FlutterStandardTypedData typedDataWithBytes:]
instancetype typedDataWithBytes:(NSData *data)
Definition: FlutterStandardCodec.mm:146
FlutterTextInputPlugin
Definition: FlutterTextInputPlugin.h:42
FlutterTimeConverter
Converts between the time representation used by Flutter Engine and CAMediaTime.
Definition: FlutterTimeConverter.h:14
-[FlutterTimeConverter CAMediaTimeToEngineTime:]
uint64_t CAMediaTimeToEngineTime:(CFTimeInterval time)
Definition: FlutterTimeConverter.mm:26
FlutterVSyncWaiter
Definition: FlutterVSyncWaiter.h:13
-[FlutterVSyncWaiter invalidate]
void invalidate()
Definition: FlutterVSyncWaiter.mm:155
-[FlutterVSyncWaiter waitForVSync:]
void waitForVSync:(uintptr_t baton)
Definition: FlutterVSyncWaiter.mm:98
FlutterViewController
Definition: FlutterViewController.h:73
-[FlutterViewController onPreEngineRestart]
void onPreEngineRestart()
Definition: FlutterViewController.mm:455
FlutterViewController::engine
FlutterEngine * engine
Definition: FlutterViewController.h:78
-[FlutterViewController attached]
BOOL attached()
Definition: FlutterViewController.mm:490
FlutterViewController::viewIdentifier
FlutterViewIdentifier viewIdentifier
Definition: FlutterViewController.h:130
-[FlutterViewController onAccessibilityStatusChanged:]
void onAccessibilityStatusChanged:(BOOL enabled)
FlutterViewEngineProvider
Definition: FlutterViewEngineProvider.h:18
FlutterView
Definition: FlutterView.h:69
FlutterView::sizedToContents
BOOL sizedToContents
Definition: FlutterView.h:121
FlutterWindowController
Definition: FlutterWindowController.h:16
FlutterWindowController::engine
FlutterEngine * engine
Definition: FlutterWindowController.h:17
flutter
Definition: AccessibilityBridgeMac.h:16
flutter::GetSwitchesFromEnvironment
std::vector< std::string > GetSwitchesFromEnvironment()
Definition: engine_switches.cc:14
flutter::AppLifecycleState
AppLifecycleState
Definition: app_lifecycle_state.h:32
FlutterAppLifecycleDelegate-p
Definition: FlutterAppLifecycleDelegate.h:21
FlutterAppLifecycleProvider-p
Definition: FlutterAppDelegate.h:21
FlutterBinaryMessenger-p
Definition: FlutterBinaryMessenger.h:49
FlutterKeyboardManagerDelegate-p
Definition: FlutterKeyboardManager.h:13
+[FlutterMessageCodec-p sharedInstance]
instancetype sharedInstance()
FlutterMouseCursorPluginDelegate-p
Definition: FlutterMouseCursorPlugin.h:13
FlutterPlatformViewFactory-p
Definition: FlutterPlatformViews.h:13
FlutterPlugin-p
Definition: FlutterPluginMacOS.h:29
-[FlutterPlugin-p handleMethodCall:result:]
void handleMethodCall:result:(FlutterMethodCall *call,[result] FlutterResult result)
FlutterPluginRegistrar-p
Definition: FlutterPluginRegistrarMacOS.h:28
FlutterTextInputPluginDelegate-p
Definition: FlutterTextInputPlugin.h:18
FlutterTexture-p
Definition: FlutterTexture.h:21
FlutterTextureRegistry-p
Definition: FlutterTexture.h:45
user_data
void * user_data
Definition: texture_registrar_unittests.cc:27