| Flutter iOS 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 #include <UIKit/UIKit.h>
6 #include "common/settings.h"
7 #define FML_USED_ON_EMBEDDER
8
9 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h"
10
11 #include <memory>
12
13 #include "flutter/common/constants.h"
14 #include "flutter/fml/message_loop.h"
15 #include "flutter/fml/platform/darwin/platform_version.h"
16 #include "flutter/fml/trace_event.h"
17 #include "flutter/runtime/ptrace_check.h"
18 #include "flutter/shell/common/engine.h"
19 #include "flutter/shell/common/platform_view.h"
20 #include "flutter/shell/common/shell.h"
21 #include "flutter/shell/common/switches.h"
22 #include "flutter/shell/common/thread_host.h"
23 #include "flutter/shell/common/variable_refresh_rate_display.h"
24 #import "flutter/shell/platform/darwin/common/InternalFlutterSwiftCommon/InternalFlutterSwiftCommon.h"
25 #import "flutter/shell/platform/darwin/common/command_line.h"
26 #import "flutter/shell/platform/darwin/common/framework/Source/FlutterBinaryMessengerRelay.h"
27 #import "flutter/shell/platform/darwin/ios/InternalFlutterSwift/InternalFlutterSwift.h"
28 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartProject_Internal.h"
29 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterDartVMServicePublisher.h"
30 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterIndirectScribbleDelegate.h"
31 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformPlugin.h"
32 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterSharedApplication.h"
33 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterSpellCheckPlugin.h"
34 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputDelegate.h"
35 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextureRegistryRelay.h"
36 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerDelegate.h"
37 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterUndoManagerPlugin.h"
38 #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
39 #import "flutter/shell/platform/darwin/ios/framework/Source/UIViewController+FlutterScreenAndSceneIfLoaded.h"
40 #import "flutter/shell/platform/darwin/ios/framework/Source/platform_message_response_darwin.h"
41 #import "flutter/shell/platform/darwin/ios/framework/Source/profiler_metrics_ios.h"
42 #import "flutter/shell/platform/darwin/ios/framework/Source/vsync_waiter_ios.h"
43 #import "flutter/shell/platform/darwin/ios/platform_view_ios.h"
44 #import "flutter/shell/platform/darwin/ios/rendering_api_selection.h"
45 #include "flutter/shell/profiling/sampling_profiler.h"
46
47 FLUTTER_ASSERT_ARC
48
49 /// Inheriting ThreadConfigurer and use iOS platform thread API to configure the thread priorities
50 /// Using iOS platform thread API to configure thread priority
51 static void IOSPlatformThreadConfigSetter(const fml::Thread::ThreadConfig& config) {
52 // set thread name
53 fml::Thread::SetCurrentThreadName(config);
54
55 // set thread priority
56 switch (config.priority) {
57 case fml::Thread::ThreadPriority::kBackground: {
58 pthread_set_qos_class_self_np(QOS_CLASS_BACKGROUND, 0);
59 [[NSThread currentThread] setThreadPriority:0];
60 break;
61 }
62 case fml::Thread::ThreadPriority::kNormal: {
63 pthread_set_qos_class_self_np(QOS_CLASS_DEFAULT, 0);
64 [[NSThread currentThread] setThreadPriority:0.5];
65 break;
66 }
67 case fml::Thread::ThreadPriority::kRaster:
68 case fml::Thread::ThreadPriority::kDisplay: {
69 pthread_set_qos_class_self_np(QOS_CLASS_USER_INTERACTIVE, 0);
70 [[NSThread currentThread] setThreadPriority:1.0];
71 sched_param param;
72 int policy;
73 pthread_t thread = pthread_self();
74 if (!pthread_getschedparam(thread, &policy, &param)) {
75 param.sched_priority = 50;
76 pthread_setschedparam(thread, policy, &param);
77 }
78 break;
79 }
80 }
81 }
82
83 #pragma mark - Public exported constants
84
85 NSString* const FlutterDefaultDartEntrypoint = nil;
86 NSString* const FlutterDefaultInitialRoute = nil;
87
88 #pragma mark - Internal constants
89
90 NSString* const kFlutterKeyDataChannel = @"flutter/keydata";
91 static constexpr int kNumProfilerSamplesPerSec = 5;
92 NSString* const kFlutterApplicationRegistrarKey = @"io.flutter.flutter.application_registrar";
93
94 @interface FlutterEngineBaseRegistrar : NSObject <FlutterBaseRegistrar>
95
96 @property(nonatomic, weak) FlutterEngine* flutterEngine;
97 @property(nonatomic, readonly) NSString* key;
98
99 - (instancetype)initWithKey:(NSString*)key flutterEngine:(FlutterEngine*)flutterEngine;
100
101 @end
102
103 @interface FlutterEngineApplicationRegistrar
104 : FlutterEngineBaseRegistrar <FlutterApplicationRegistrar>
105 @end
106
107 @interface FlutterEnginePluginRegistrar : FlutterEngineBaseRegistrar <FlutterPluginRegistrar>
108 @end
109
110 @interface FlutterEngine () <FlutterIndirectScribbleDelegate,
111 FlutterUndoManagerDelegate,
112 FlutterTextInputDelegate,
113 FlutterBinaryMessenger,
114 FlutterTextureRegistry>
115
116 #pragma mark - Properties
117
118 @property(nonatomic, readonly) FlutterDartProject* dartProject;
119 @property(nonatomic, readonly, copy) NSString* labelPrefix;
120 @property(nonatomic, readonly, assign) BOOL allowHeadlessExecution;
121 @property(nonatomic, readonly, assign) BOOL restorationEnabled;
122
123 @property(nonatomic, strong) FlutterPlatformViewsController* platformViewsController;
124 @property(nonatomic, strong) FlutterEnginePluginSceneLifeCycleDelegate* sceneLifeCycleDelegate;
125
126 // Maintains a dictionary of plugin names that have registered with the engine. Used by
127 // FlutterEnginePluginRegistrar to implement a FlutterPluginRegistrar.
128 @property(nonatomic, readonly) NSMutableDictionary* pluginPublications;
129 @property(nonatomic, readonly)
130 NSMutableDictionary<NSString*, FlutterEngineBaseRegistrar*>* registrars;
131
132 @property(nonatomic, readwrite, copy) NSString* isolateId;
133 @property(nonatomic, copy) NSString* initialRoute;
134 @property(nonatomic, strong) id<NSObject> flutterViewControllerWillDeallocObserver;
135 @property(nonatomic, strong) FlutterDartVMServicePublisher* publisher;
136 @property(nonatomic, strong) FlutterConnectionCollection* connections;
137 @property(nonatomic, assign) int64_t nextTextureId;
138
139 #pragma mark - Channel properties
140
141 @property(nonatomic, strong) FlutterPlatformPlugin* platformPlugin;
142 @property(nonatomic, strong) FlutterTextInputPlugin* textInputPlugin;
143 @property(nonatomic, strong) FlutterUndoManagerPlugin* undoManagerPlugin;
144 @property(nonatomic, strong) FlutterSpellCheckPlugin* spellCheckPlugin;
145 @property(nonatomic, strong) FlutterRestorationPlugin* restorationPlugin;
146 @property(nonatomic, strong) FlutterMethodChannel* localizationChannel;
147 @property(nonatomic, strong) FlutterMethodChannel* navigationChannel;
148 @property(nonatomic, strong) FlutterMethodChannel* restorationChannel;
149 @property(nonatomic, strong) FlutterMethodChannel* platformChannel;
150 @property(nonatomic, strong) FlutterMethodChannel* platformViewsChannel;
151 @property(nonatomic, strong) FlutterMethodChannel* textInputChannel;
152 @property(nonatomic, strong) FlutterMethodChannel* undoManagerChannel;
153 @property(nonatomic, strong) FlutterMethodChannel* scribbleChannel;
154 @property(nonatomic, strong) FlutterMethodChannel* spellCheckChannel;
155 @property(nonatomic, strong) FlutterBasicMessageChannel* lifecycleChannel;
156 @property(nonatomic, strong) FlutterBasicMessageChannel* systemChannel;
157 @property(nonatomic, strong) FlutterBasicMessageChannel* settingsChannel;
158 @property(nonatomic, strong) FlutterBasicMessageChannel* keyEventChannel;
159 @property(nonatomic, strong) FlutterMethodChannel* screenshotChannel;
160
161 #pragma mark - Embedder API properties
162
163 @property(nonatomic, assign) BOOL enableEmbedderAPI;
164 // Function pointers for interacting with the embedder.h API.
165 @property(nonatomic) FlutterEngineProcTable& embedderAPI;
166
167 @end
168
169 @implementation FlutterImplicitEngineBridgeImpl {
170 FlutterEngine* _engine;
171 NSObject<FlutterApplicationRegistrar>* _appRegistrar;
172 }
173
174 - (instancetype)initWithEngine:(FlutterEngine*)engine {
175 self = [super init];
176 if (self) {
177 _engine = engine;
178 _appRegistrar = [engine registrarForApplication:kFlutterApplicationRegistrarKey];
179 }
180 return self;
181 }
182
183 - (NSObject<FlutterPluginRegistry>*)pluginRegistry {
184 return _engine;
185 }
186
187 - (NSObject<FlutterApplicationRegistrar>*)applicationRegistrar {
188 return _appRegistrar;
189 }
190 @end
191
192 @implementation FlutterEngine {
193 std::shared_ptr<flutter::ThreadHost> _threadHost;
194 std::unique_ptr<flutter::Shell> _shell;
195
196 flutter::IOSRenderingAPI _renderingApi;
197 std::shared_ptr<flutter::SamplingProfiler> _profiler;
198
199 FlutterBinaryMessengerRelay* _binaryMessenger;
200 FlutterTextureRegistryRelay* _textureRegistry;
201 }
202
203 - (int64_t)engineIdentifier {
204 return reinterpret_cast<int64_t>((__bridge void*)self);
205 }
206
207 - (instancetype)init {
208 return [self initWithName:@"FlutterEngine" project:nil allowHeadlessExecution:YES];
209 }
210
211 - (instancetype)initWithName:(NSString*)labelPrefix {
212 return [self initWithName:labelPrefix project:nil allowHeadlessExecution:YES];
213 }
214
215 - (instancetype)initWithName:(NSString*)labelPrefix project:(FlutterDartProject*)project {
216 return [self initWithName:labelPrefix project:project allowHeadlessExecution:YES];
217 }
218
219 - (instancetype)initWithName:(NSString*)labelPrefix
220 project:(FlutterDartProject*)project
221 allowHeadlessExecution:(BOOL)allowHeadlessExecution {
222 return [self initWithName:labelPrefix
223 project:project
224 allowHeadlessExecution:allowHeadlessExecution
225 restorationEnabled:NO];
226 }
227
228 - (instancetype)initWithName:(NSString*)labelPrefix
229 project:(FlutterDartProject*)project
230 allowHeadlessExecution:(BOOL)allowHeadlessExecution
231 restorationEnabled:(BOOL)restorationEnabled {
232 self = [super init];
233 NSAssert(self, @"Super init cannot be nil");
234 NSAssert(labelPrefix, @"labelPrefix is required");
235
236 _restorationEnabled = restorationEnabled;
237 _allowHeadlessExecution = allowHeadlessExecution;
238 _labelPrefix = [labelPrefix copy];
239 _dartProject = project ?: [[FlutterDartProject alloc] init];
240
241 _enableEmbedderAPI = _dartProject.settings.enable_embedder_api;
242 if (_enableEmbedderAPI) {
243 NSLog(@"============== iOS: enable_embedder_api is on ==============");
244 _embedderAPI.struct_size = sizeof(FlutterEngineProcTable);
245 FlutterEngineGetProcAddresses(&_embedderAPI);
246 }
247
248 if (!EnableTracingIfNecessary(_dartProject.settings)) {
249 NSLog(
250 @"Cannot create a FlutterEngine instance in debug mode without Flutter tooling or "
251 @"Xcode.\n\nTo launch in debug mode in iOS 14+, run flutter run from Flutter tools, run "
252 @"from an IDE with a Flutter IDE plugin or run the iOS project from Xcode.\nAlternatively "
253 @"profile and release mode apps can be launched from the home screen.");
254 return nil;
255 }
256
257 _pluginPublications = [[NSMutableDictionary alloc] init];
258 _registrars = [[NSMutableDictionary alloc] init];
259 [self recreatePlatformViewsController];
260 _binaryMessenger = [[FlutterBinaryMessengerRelay alloc] initWithParent:self];
261 _textureRegistry = [[FlutterTextureRegistryRelay alloc] initWithParent:self];
262 _connections = [[FlutterConnectionCollection alloc] init];
263
264 NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
265 [center addObserver:self
266 selector:@selector(onMemoryWarning:)
267 name:UIApplicationDidReceiveMemoryWarningNotification
268 object:nil];
269
270 [self setUpLifecycleNotifications:center];
271
272 [center addObserver:self
273 selector:@selector(onLocaleUpdated:)
274 name:NSCurrentLocaleDidChangeNotification
275 object:nil];
276
277 self.sceneLifeCycleDelegate = [[FlutterEnginePluginSceneLifeCycleDelegate alloc] init];
278
279 return self;
280 }
281
282 + (FlutterEngine*)engineForIdentifier:(int64_t)identifier {
283 NSAssert([[NSThread currentThread] isMainThread], @"Must be called on the main thread.");
284 return (__bridge FlutterEngine*)reinterpret_cast<void*>(identifier);
285 }
286
287 - (void)setUpLifecycleNotifications:(NSNotificationCenter*)center {
288 // If the application is not available, use the scene for lifecycle notifications if available.
289 [center addObserver:self
290 selector:@selector(sceneWillConnect:)
291 name:UISceneWillConnectNotification
292 object:nil];
293 if (!FlutterSharedApplication.isAvailable) {
294 [center addObserver:self
295 selector:@selector(sceneWillEnterForeground:)
296 name:UISceneWillEnterForegroundNotification
297 object:nil];
298 [center addObserver:self
299 selector:@selector(sceneDidEnterBackground:)
300 name:UISceneDidEnterBackgroundNotification
301 object:nil];
302 return;
303 }
304 [center addObserver:self
305 selector:@selector(applicationWillEnterForeground:)
306 name:UIApplicationWillEnterForegroundNotification
307 object:nil];
308 [center addObserver:self
309 selector:@selector(applicationDidEnterBackground:)
310 name:UIApplicationDidEnterBackgroundNotification
311 object:nil];
312 }
313
314 - (void)sceneWillConnect:(NSNotification*)notification API_AVAILABLE(ios(13.0)) {
315 UIScene* scene = notification.object;
316 if (!FlutterSharedApplication.application.supportsMultipleScenes) {
317 // Since there is only one scene, we can assume that the FlutterEngine is within this scene and
318 // register it to the scene.
319 // The FlutterEngine needs to be registered with the scene when the scene connects in order for
320 // plugins to receive the `scene:willConnectToSession:options` event.
321 // If we want to support multi-window on iPad later, we may need to add a way for deveopers to
322 // register their FlutterEngine to the scene manually during this event.
323 FlutterPluginSceneLifeCycleDelegate* sceneLifeCycleDelegate =
324 [FlutterPluginSceneLifeCycleDelegate fromScene:scene];
325 if (sceneLifeCycleDelegate != nil) {
326 return [sceneLifeCycleDelegate engine:self receivedConnectNotificationFor:scene];
327 }
328 }
329 }
330
331 - (void)recreatePlatformViewsController {
332 _renderingApi = flutter::GetRenderingAPIForProcess(/*force_software=*/false);
333 _platformViewsController = [[FlutterPlatformViewsController alloc] init];
334 }
335
336 - (flutter::IOSRenderingAPI)platformViewsRenderingAPI {
337 return _renderingApi;
338 }
339
340 - (void)dealloc {
341 /// Notify plugins of dealloc. This should happen first in dealloc since the
342 /// plugins may be talking to things like the binaryMessenger.
343 [_pluginPublications enumerateKeysAndObjectsUsingBlock:^(id key, id object, BOOL* stop) {
344 if ([object respondsToSelector:@selector(detachFromEngineForRegistrar:)]) {
345 FlutterEngineBaseRegistrar* registrar = self.registrars[key];
346 if ([registrar conformsToProtocol:@protocol(FlutterPluginRegistrar)]) {
347 [object detachFromEngineForRegistrar:((id<FlutterPluginRegistrar>)registrar)];
348 }
349 }
350 }];
351
352 // nil out weak references.
353 // TODO(cbracken): https://github.com/flutter/flutter/issues/156222
354 // Ensure that FlutterEnginePluginRegistrar is using weak pointers, then eliminate this code.
355 [_registrars enumerateKeysAndObjectsUsingBlock:^(id key, FlutterEngineBaseRegistrar* registrar,
356 BOOL* stop) {
357 registrar.flutterEngine = nil;
358 }];
359
360 _binaryMessenger.parent = nil;
361 _textureRegistry.parent = nil;
362
363 NSNotificationCenter* center = [NSNotificationCenter defaultCenter];
364 if (_flutterViewControllerWillDeallocObserver) {
365 [center removeObserver:_flutterViewControllerWillDeallocObserver];
366 }
367 [center removeObserver:self];
368 }
369
370 - (flutter::Shell&)shell {
371 FML_DCHECK(_shell);
372 return *_shell;
373 }
374
375 - (void)updateViewportMetrics:(flutter::ViewportMetrics)viewportMetrics {
376 if (!self.platformView) {
377 return;
378 }
379 self.platformView->SetViewportMetrics(flutter::kFlutterImplicitViewId, viewportMetrics);
380 }
381
382 - (void)dispatchPointerDataPacket:(std::unique_ptr<flutter::PointerDataPacket>)packet {
383 if (!self.platformView) {
384 return;
385 }
386 self.platformView->DispatchPointerDataPacket(std::move(packet));
387 }
388
389 - (void)installFirstFrameCallback:(void (^)(void))block {
390 if (!self.platformView) {
391 return;
392 }
393
394 __weak FlutterEngine* weakSelf = self;
395 self.platformView->SetNextFrameCallback([weakSelf, block] {
396 FlutterEngine* strongSelf = weakSelf;
397 if (!strongSelf) {
398 return;
399 }
400 FML_DCHECK(strongSelf.platformTaskRunner);
401 FML_DCHECK(strongSelf.rasterTaskRunner);
402 FML_DCHECK(strongSelf.rasterTaskRunner->RunsTasksOnCurrentThread());
403 // Get callback on raster thread and jump back to platform thread.
404 strongSelf.platformTaskRunner->PostTask([block]() { block(); });
405 });
406 }
407
408 - (void)enableSemantics:(BOOL)enabled withFlags:(int64_t)flags {
409 if (!self.platformView) {
410 return;
411 }
412 self.platformView->SetSemanticsEnabled(enabled);
413 self.platformView->SetAccessibilityFeatures(flags);
414 }
415
416 - (void)notifyViewCreated {
417 if (!self.platformView) {
418 return;
419 }
420 self.platformView->NotifyCreated();
421 }
422
423 - (void)notifyViewDestroyed {
424 if (!self.platformView) {
425 return;
426 }
427 self.platformView->NotifyDestroyed();
428 }
429
430 - (flutter::PlatformViewIOS*)platformView {
431 if (!_shell) {
432 return nullptr;
433 }
434 return static_cast<flutter::PlatformViewIOS*>(_shell->GetPlatformView().get());
435 }
436
437 - (fml::RefPtr<fml::TaskRunner>)platformTaskRunner {
438 if (!_shell) {
439 return {};
440 }
441 return _shell->GetTaskRunners().GetPlatformTaskRunner();
442 }
443
444 - (fml::RefPtr<fml::TaskRunner>)uiTaskRunner {
445 if (!_shell) {
446 return {};
447 }
448 return _shell->GetTaskRunners().GetUITaskRunner();
449 }
450
451 - (fml::RefPtr<fml::TaskRunner>)rasterTaskRunner {
452 if (!_shell) {
453 return {};
454 }
455 return _shell->GetTaskRunners().GetRasterTaskRunner();
456 }
457
458 - (void)sendKeyEvent:(const FlutterKeyEvent&)event
459 callback:(FlutterKeyEventCallback)callback
460 userData:(void*)userData API_AVAILABLE(ios(13.4)) {
461 if (@available(iOS 13.4, *)) {
462 } else {
463 return;
464 }
465 if (!self.platformView) {
466 return;
467 }
468 const char* character = event.character;
469
470 flutter::KeyData key_data;
471 key_data.Clear();
472 key_data.timestamp = (uint64_t)event.timestamp;
473 switch (event.type) {
474 case kFlutterKeyEventTypeUp:
475 key_data.type = flutter::KeyEventType::kUp;
476 break;
477 case kFlutterKeyEventTypeDown:
478 key_data.type = flutter::KeyEventType::kDown;
479 break;
480 case kFlutterKeyEventTypeRepeat:
481 key_data.type = flutter::KeyEventType::kRepeat;
482 break;
483 }
484 key_data.physical = event.physical;
485 key_data.logical = event.logical;
486 key_data.synthesized = event.synthesized;
487
488 auto packet = std::make_unique<flutter::KeyDataPacket>(key_data, character);
489 NSData* message = [NSData dataWithBytes:packet->data().data() length:packet->data().size()];
490
491 auto response = ^(NSData* reply) {
492 if (callback == nullptr) {
493 return;
494 }
495 BOOL handled = FALSE;
496 if (reply.length == 1 && *reinterpret_cast<const uint8_t*>(reply.bytes) == 1) {
497 handled = TRUE;
498 }
499 callback(handled, userData);
500 };
501
502 [self sendOnChannel:kFlutterKeyDataChannel message:message binaryReply:response];
503 }
504
505 - (void)ensureSemanticsEnabled {
506 if (!self.platformView) {
507 return;
508 }
509 self.platformView->SetSemanticsEnabled(true);
510 }
511
512 - (void)setViewController:(FlutterViewController*)viewController {
513 FML_DCHECK(self.platformView);
514 _viewController = viewController;
515 self.platformView->SetOwnerViewController(_viewController);
516 [self maybeSetupPlatformViewChannels];
517 [self updateDisplays];
518 self.textInputPlugin.viewController = viewController;
519
520 if (viewController) {
521 __weak __block FlutterEngine* weakSelf = self;
522 self.flutterViewControllerWillDeallocObserver =
523 [[NSNotificationCenter defaultCenter] addObserverForName:FlutterViewControllerWillDealloc
524 object:viewController
525 queue:[NSOperationQueue mainQueue]
526 usingBlock:^(NSNotification* note) {
527 [weakSelf notifyViewControllerDeallocated];
528 }];
529 } else {
530 self.flutterViewControllerWillDeallocObserver = nil;
531 [self notifyLowMemory];
532 }
533 }
534
535 - (void)attachView {
536 FML_DCHECK(self.platformView);
537 self.platformView->attachView();
538 }
539
540 - (void)setFlutterViewControllerWillDeallocObserver:(id<NSObject>)observer {
541 if (observer != _flutterViewControllerWillDeallocObserver) {
542 if (_flutterViewControllerWillDeallocObserver) {
543 [[NSNotificationCenter defaultCenter]
544 removeObserver:_flutterViewControllerWillDeallocObserver];
545 }
546 _flutterViewControllerWillDeallocObserver = observer;
547 }
548 }
549
550 - (void)notifyViewControllerDeallocated {
551 [self.lifecycleChannel sendMessage:@"AppLifecycleState.detached"];
552 self.textInputPlugin.viewController = nil;
553 if (!self.allowHeadlessExecution) {
554 [self destroyContext];
555 } else if (self.platformView) {
556 self.platformView->SetOwnerViewController({});
557 }
558 [self.textInputPlugin resetViewResponder];
559 _viewController = nil;
560 }
561
562 - (void)destroyContext {
563 [self resetChannels];
564 self.isolateId = nil;
565 _shell.reset();
566 _profiler.reset();
567 _threadHost.reset();
568 _platformViewsController = nil;
569 }
570
571 - (NSURL*)vmServiceUrl {
572 return self.publisher.url;
573 }
574
575 - (void)resetChannels {
576 self.localizationChannel = nil;
577 self.navigationChannel = nil;
578 self.restorationChannel = nil;
579 self.platformChannel = nil;
580 self.platformViewsChannel = nil;
581 self.textInputChannel = nil;
582 self.undoManagerChannel = nil;
583 self.scribbleChannel = nil;
584 self.lifecycleChannel = nil;
585 self.systemChannel = nil;
586 self.settingsChannel = nil;
587 self.keyEventChannel = nil;
588 self.spellCheckChannel = nil;
589 }
590
591 - (void)startProfiler {
592 FML_DCHECK(!_threadHost->name_prefix.empty());
593 _profiler = std::make_shared<flutter::SamplingProfiler>(
594 _threadHost->name_prefix.c_str(), _threadHost->profiler_thread->GetTaskRunner(),
595 []() {
596 flutter::ProfilerMetricsIOS profiler_metrics;
597 return profiler_metrics.GenerateSample();
598 },
599 kNumProfilerSamplesPerSec);
600 _profiler->Start();
601 }
602
603 // If you add a channel, be sure to also update `resetChannels`.
604 // Channels get a reference to the engine, and therefore need manual
605 // cleanup for proper collection.
606 - (void)setUpChannels {
607 // This will be invoked once the shell is done setting up and the isolate ID
608 // for the UI isolate is available.
609 __weak FlutterEngine* weakSelf = self;
610 [_binaryMessenger setMessageHandlerOnChannel:@"flutter/isolate"
611 binaryMessageHandler:^(NSData* message, FlutterBinaryReply reply) {
612 if (weakSelf) {
613 weakSelf.isolateId =
614 [[FlutterStringCodec sharedInstance] decode:message];
615 }
616 }];
617
618 self.localizationChannel =
619 [[FlutterMethodChannel alloc] initWithName:@"flutter/localization"
620 binaryMessenger:self.binaryMessenger
621 codec:[FlutterJSONMethodCodec sharedInstance]];
622
623 self.navigationChannel =
624 [[FlutterMethodChannel alloc] initWithName:@"flutter/navigation"
625 binaryMessenger:self.binaryMessenger
626 codec:[FlutterJSONMethodCodec sharedInstance]];
627
628 if ([_initialRoute length] > 0) {
629 // Flutter isn't ready to receive this method call yet but the channel buffer will cache this.
630 [self.navigationChannel invokeMethod:@"setInitialRoute" arguments:_initialRoute];
631 _initialRoute = nil;
632 }
633
634 self.restorationChannel =
635 [[FlutterMethodChannel alloc] initWithName:@"flutter/restoration"
636 binaryMessenger:self.binaryMessenger
637 codec:[FlutterStandardMethodCodec sharedInstance]];
638
639 self.platformChannel =
640 [[FlutterMethodChannel alloc] initWithName:@"flutter/platform"
641 binaryMessenger:self.binaryMessenger
642 codec:[FlutterJSONMethodCodec sharedInstance]];
643
644 self.platformViewsChannel =
645 [[FlutterMethodChannel alloc] initWithName:@"flutter/platform_views"
646 binaryMessenger:self.binaryMessenger
647 codec:[FlutterStandardMethodCodec sharedInstance]];
648
649 self.textInputChannel =
650 [[FlutterMethodChannel alloc] initWithName:@"flutter/textinput"
651 binaryMessenger:self.binaryMessenger
652 codec:[FlutterJSONMethodCodec sharedInstance]];
653
654 self.undoManagerChannel =
655 [[FlutterMethodChannel alloc] initWithName:@"flutter/undomanager"
656 binaryMessenger:self.binaryMessenger
657 codec:[FlutterJSONMethodCodec sharedInstance]];
658
659 self.scribbleChannel =
660 [[FlutterMethodChannel alloc] initWithName:@"flutter/scribble"
661 binaryMessenger:self.binaryMessenger
662 codec:[FlutterJSONMethodCodec sharedInstance]];
663
664 self.spellCheckChannel =
665 [[FlutterMethodChannel alloc] initWithName:@"flutter/spellcheck"
666 binaryMessenger:self.binaryMessenger
667 codec:[FlutterStandardMethodCodec sharedInstance]];
668
669 self.lifecycleChannel =
670 [[FlutterBasicMessageChannel alloc] initWithName:@"flutter/lifecycle"
671 binaryMessenger:self.binaryMessenger
672 codec:[FlutterStringCodec sharedInstance]];
673
674 self.systemChannel =
675 [[FlutterBasicMessageChannel alloc] initWithName:@"flutter/system"
676 binaryMessenger:self.binaryMessenger
677 codec:[FlutterJSONMessageCodec sharedInstance]];
678
679 self.settingsChannel =
680 [[FlutterBasicMessageChannel alloc] initWithName:@"flutter/settings"
681 binaryMessenger:self.binaryMessenger
682 codec:[FlutterJSONMessageCodec sharedInstance]];
683
684 self.keyEventChannel =
685 [[FlutterBasicMessageChannel alloc] initWithName:@"flutter/keyevent"
686 binaryMessenger:self.binaryMessenger
687 codec:[FlutterJSONMessageCodec sharedInstance]];
688
689 self.textInputPlugin = [[FlutterTextInputPlugin alloc] initWithDelegate:self];
690 self.textInputPlugin.indirectScribbleDelegate = self;
691 [self.textInputPlugin setUpIndirectScribbleInteraction:self.viewController];
692
693 self.undoManagerPlugin = [[FlutterUndoManagerPlugin alloc] initWithDelegate:self];
694 self.platformPlugin = [[FlutterPlatformPlugin alloc] initWithEngine:self];
695
696 self.restorationPlugin =
697 [[FlutterRestorationPlugin alloc] initWithChannel:self.restorationChannel
698 restorationEnabled:self.restorationEnabled];
699 self.spellCheckPlugin = [[FlutterSpellCheckPlugin alloc] init];
700
701 self.screenshotChannel =
702 [[FlutterMethodChannel alloc] initWithName:@"flutter/screenshot"
703 binaryMessenger:self.binaryMessenger
704 codec:[FlutterStandardMethodCodec sharedInstance]];
705
706 [self.screenshotChannel setMethodCallHandler:^(FlutterMethodCall* _Nonnull call,
707 FlutterResult _Nonnull result) {
708 FlutterEngine* strongSelf = weakSelf;
709 if (!(strongSelf && strongSelf->_shell && strongSelf->_shell->IsSetup())) {
710 return result([FlutterError
711 errorWithCode:@"invalid_state"
712 message:@"Requesting screenshot while engine is not running."
713 details:nil]);
714 }
715 flutter::Rasterizer::Screenshot screenshot =
716 [strongSelf screenshot:flutter::Rasterizer::ScreenshotType::SurfaceData base64Encode:NO];
717 if (!screenshot.data) {
718 return result([FlutterError errorWithCode:@"failure"
719 message:@"Unable to get screenshot."
720 details:nil]);
721 }
722 // TODO(gaaclarke): Find way to eliminate this data copy.
723 NSData* data = [NSData dataWithBytes:screenshot.data->writable_data()
724 length:screenshot.data->size()];
725 NSString* format = [NSString stringWithUTF8String:screenshot.format.c_str()];
726 NSNumber* width = @(screenshot.frame_size.width);
727 NSNumber* height = @(screenshot.frame_size.height);
728 return result(@[ width, height, format ?: [NSNull null], data ]);
729 }];
730 }
731
732 - (void)maybeSetupPlatformViewChannels {
733 if (_shell && self.shell.IsSetup()) {
734 __weak FlutterEngine* weakSelf = self;
735
736 [self.platformChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
737 [weakSelf.platformPlugin handleMethodCall:call result:result];
738 }];
739
740 [self.platformViewsChannel
741 setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
742 if (weakSelf) {
743 [weakSelf.platformViewsController onMethodCall:call result:result];
744 }
745 }];
746
747 [self.textInputChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
748 [weakSelf.textInputPlugin handleMethodCall:call result:result];
749 }];
750
751 [self.undoManagerChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
752 [weakSelf.undoManagerPlugin handleMethodCall:call result:result];
753 }];
754
755 [self.spellCheckChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
756 [weakSelf.spellCheckPlugin handleMethodCall:call result:result];
757 }];
758 }
759 }
760
761 - (flutter::Rasterizer::Screenshot)screenshot:(flutter::Rasterizer::ScreenshotType)type
762 base64Encode:(bool)base64Encode {
763 return self.shell.Screenshot(type, base64Encode);
764 }
765
766 - (void)launchEngine:(NSString*)entrypoint
767 libraryURI:(NSString*)libraryOrNil
768 entrypointArgs:(NSArray<NSString*>*)entrypointArgs {
769 // Launch the Dart application with the inferred run configuration.
770 flutter::RunConfiguration configuration =
771 [self.dartProject runConfigurationForEntrypoint:entrypoint
772 libraryOrNil:libraryOrNil
773 entrypointArgs:entrypointArgs];
774
775 configuration.SetEngineId(self.engineIdentifier);
776 self.shell.RunEngine(std::move(configuration));
777 }
778
779 - (void)setUpShell:(std::unique_ptr<flutter::Shell>)shell
780 withVMServicePublication:(BOOL)doesVMServicePublication {
781 _shell = std::move(shell);
782 [self setUpChannels];
783 [self onLocaleUpdated:nil];
784 [self updateDisplays];
785 self.publisher = [[FlutterDartVMServicePublisher alloc]
786 initWithEnableVMServicePublication:doesVMServicePublication];
787 [self maybeSetupPlatformViewChannels];
788 _shell->SetGpuAvailability(_isGpuDisabled ? flutter::GpuAvailability::kUnavailable
789 : flutter::GpuAvailability::kAvailable);
790 }
791
792 + (BOOL)isProfilerEnabled {
793 bool profilerEnabled = false;
794 #if (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_DEBUG) || \
795 (FLUTTER_RUNTIME_MODE == FLUTTER_RUNTIME_MODE_PROFILE)
796 profilerEnabled = true;
797 #endif
798 return profilerEnabled;
799 }
800
801 + (NSString*)generateThreadLabel:(NSString*)labelPrefix {
802 static size_t s_shellCount = 0;
803 return [NSString stringWithFormat:@"%@.%zu", labelPrefix, ++s_shellCount];
804 }
805
806 static flutter::ThreadHost MakeThreadHost(NSString* thread_label,
807 const flutter::Settings& settings) {
808 // The current thread will be used as the platform thread. Ensure that the message loop is
809 // initialized.
810 fml::MessageLoop::EnsureInitializedForCurrentThread();
811
812 uint32_t threadHostType = flutter::ThreadHost::Type::kRaster | flutter::ThreadHost::Type::kIo;
813 if (settings.merged_platform_ui_thread != flutter::Settings::MergedPlatformUIThread::kEnabled) {
814 threadHostType |= flutter::ThreadHost::Type::kUi;
815 }
816
817 if ([FlutterEngine isProfilerEnabled]) {
818 threadHostType = threadHostType | flutter::ThreadHost::Type::kProfiler;
819 }
820
821 flutter::ThreadHost::ThreadHostConfig host_config(thread_label.UTF8String, threadHostType,
822 IOSPlatformThreadConfigSetter);
823
824 host_config.ui_config =
825 fml::Thread::ThreadConfig(flutter::ThreadHost::ThreadHostConfig::MakeThreadName(
826 flutter::ThreadHost::Type::kUi, thread_label.UTF8String),
827 fml::Thread::ThreadPriority::kDisplay);
828 host_config.raster_config =
829 fml::Thread::ThreadConfig(flutter::ThreadHost::ThreadHostConfig::MakeThreadName(
830 flutter::ThreadHost::Type::kRaster, thread_label.UTF8String),
831 fml::Thread::ThreadPriority::kRaster);
832
833 host_config.io_config =
834 fml::Thread::ThreadConfig(flutter::ThreadHost::ThreadHostConfig::MakeThreadName(
835 flutter::ThreadHost::Type::kIo, thread_label.UTF8String),
836 fml::Thread::ThreadPriority::kNormal);
837
838 return (flutter::ThreadHost){host_config};
839 }
840
841 static void SetEntryPoint(flutter::Settings* settings, NSString* entrypoint, NSString* libraryURI) {
842 if (libraryURI) {
843 FML_DCHECK(entrypoint) << "Must specify entrypoint if specifying library";
844 settings->advisory_script_entrypoint = entrypoint.UTF8String;
845 settings->advisory_script_uri = libraryURI.UTF8String;
846 } else if (entrypoint) {
847 settings->advisory_script_entrypoint = entrypoint.UTF8String;
848 settings->advisory_script_uri = std::string("main.dart");
849 } else {
850 settings->advisory_script_entrypoint = std::string("main");
851 settings->advisory_script_uri = std::string("main.dart");
852 }
853 }
854
855 - (BOOL)createShell:(NSString*)entrypoint
856 libraryURI:(NSString*)libraryURI
857 initialRoute:(NSString*)initialRoute {
858 if (_shell != nullptr) {
859 [FlutterLogger logWarning:@"This FlutterEngine was already invoked."];
860 return NO;
861 }
862
863 self.initialRoute = initialRoute;
864
865 auto settings = [self.dartProject settings];
866 if (initialRoute != nil) {
867 self.initialRoute = initialRoute;
868 } else if (settings.route.empty() == false) {
869 self.initialRoute = [NSString stringWithUTF8String:settings.route.c_str()];
870 }
871
872 auto platformData = [self.dartProject defaultPlatformData];
873
874 SetEntryPoint(&settings, entrypoint, libraryURI);
875
876 NSString* threadLabel = [FlutterEngine generateThreadLabel:self.labelPrefix];
877 _threadHost = std::make_shared<flutter::ThreadHost>();
878 *_threadHost = MakeThreadHost(threadLabel, settings);
879
880 __weak FlutterEngine* weakSelf = self;
881 flutter::Shell::CreateCallback<flutter::PlatformView> on_create_platform_view =
882 [weakSelf](flutter::Shell& shell) {
883 FlutterEngine* strongSelf = weakSelf;
884 if (!strongSelf) {
885 return std::unique_ptr<flutter::PlatformViewIOS>();
886 }
887 [strongSelf recreatePlatformViewsController];
888 strongSelf.platformViewsController.taskRunner =
889 shell.GetTaskRunners().GetPlatformTaskRunner();
890 return std::make_unique<flutter::PlatformViewIOS>(
891 shell, strongSelf->_renderingApi, strongSelf.platformViewsController,
892 shell.GetTaskRunners(), shell.GetConcurrentWorkerTaskRunner(),
893 shell.GetIsGpuDisabledSyncSwitch());
894 };
895
896 flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer =
897 [](flutter::Shell& shell) { return std::make_unique<flutter::Rasterizer>(shell); };
898
899 fml::RefPtr<fml::TaskRunner> ui_runner;
900 if (settings.enable_impeller &&
901 settings.merged_platform_ui_thread == flutter::Settings::MergedPlatformUIThread::kEnabled) {
902 ui_runner = fml::MessageLoop::GetCurrent().GetTaskRunner();
903 } else {
904 ui_runner = _threadHost->ui_thread->GetTaskRunner();
905 }
906 flutter::TaskRunners task_runners(threadLabel.UTF8String, // label
907 fml::MessageLoop::GetCurrent().GetTaskRunner(), // platform
908 _threadHost->raster_thread->GetTaskRunner(), // raster
909 ui_runner, // ui
910 _threadHost->io_thread->GetTaskRunner() // io
911 );
912
913 // Disable GPU if the app or scene is running in the background.
914 self.isGpuDisabled = self.viewController
915 ? self.viewController.stateIsBackground
916 : FlutterSharedApplication.application &&
917 FlutterSharedApplication.application.applicationState ==
918 UIApplicationStateBackground;
919
920 // Create the shell. This is a blocking operation.
921 std::unique_ptr<flutter::Shell> shell = flutter::Shell::Create(
922 /*platform_data=*/platformData,
923 /*task_runners=*/task_runners,
924 /*settings=*/settings,
925 /*on_create_platform_view=*/on_create_platform_view,
926 /*on_create_rasterizer=*/on_create_rasterizer,
927 /*is_gpu_disabled=*/_isGpuDisabled);
928
929 if (shell == nullptr) {
930 NSString* errorMessage = [NSString
931 stringWithFormat:@"Could not start a shell FlutterEngine with entrypoint: %@", entrypoint];
932 [FlutterLogger logError:errorMessage];
933 } else {
934 [self setUpShell:std::move(shell)
935 withVMServicePublication:settings.enable_vm_service_publication];
936 if ([FlutterEngine isProfilerEnabled]) {
937 [self startProfiler];
938 }
939 }
940
941 return _shell != nullptr;
942 }
943
944 - (BOOL)performImplicitEngineCallback {
945 id appDelegate = FlutterSharedApplication.application.delegate;
946 if ([appDelegate conformsToProtocol:@protocol(FlutterImplicitEngineDelegate)]) {
947 id<FlutterImplicitEngineDelegate> provider = (id<FlutterImplicitEngineDelegate>)appDelegate;
948 [provider didInitializeImplicitFlutterEngine:[[FlutterImplicitEngineBridgeImpl alloc]
949 initWithEngine:self]];
950 return YES;
951 }
952 return NO;
953 }
954
955 - (void)updateDisplays {
956 if (!_shell) {
957 // Tests may do this.
958 return;
959 }
960 auto vsync_waiter = _shell->GetVsyncWaiter().lock();
961 auto vsync_waiter_ios = std::static_pointer_cast<flutter::VsyncWaiterIOS>(vsync_waiter);
962 std::vector<std::unique_ptr<flutter::Display>> displays;
963 auto screen_size = UIScreen.mainScreen.nativeBounds.size;
964 auto scale = UIScreen.mainScreen.scale;
965 displays.push_back(std::make_unique<flutter::VariableRefreshRateDisplay>(
966 0, vsync_waiter_ios, screen_size.width, screen_size.height, scale));
967 _shell->OnDisplayUpdates(std::move(displays));
968 }
969
970 - (BOOL)run {
971 return [self runWithEntrypoint:FlutterDefaultDartEntrypoint
972 libraryURI:nil
973 initialRoute:FlutterDefaultInitialRoute];
974 }
975
976 - (BOOL)runWithEntrypoint:(NSString*)entrypoint libraryURI:(NSString*)libraryURI {
977 return [self runWithEntrypoint:entrypoint
978 libraryURI:libraryURI
979 initialRoute:FlutterDefaultInitialRoute];
980 }
981
982 - (BOOL)runWithEntrypoint:(NSString*)entrypoint {
983 return [self runWithEntrypoint:entrypoint libraryURI:nil initialRoute:FlutterDefaultInitialRoute];
984 }
985
986 - (BOOL)runWithEntrypoint:(NSString*)entrypoint initialRoute:(NSString*)initialRoute {
987 return [self runWithEntrypoint:entrypoint libraryURI:nil initialRoute:initialRoute];
988 }
989
990 - (BOOL)runWithEntrypoint:(NSString*)entrypoint
991 libraryURI:(NSString*)libraryURI
992 initialRoute:(NSString*)initialRoute {
993 return [self runWithEntrypoint:entrypoint
994 libraryURI:libraryURI
995 initialRoute:initialRoute
996 entrypointArgs:nil];
997 }
998
999 - (BOOL)runWithEntrypoint:(NSString*)entrypoint
1000 libraryURI:(NSString*)libraryURI
1001 initialRoute:(NSString*)initialRoute
1002 entrypointArgs:(NSArray<NSString*>*)entrypointArgs {
1003 if ([self createShell:entrypoint libraryURI:libraryURI initialRoute:initialRoute]) {
1004 [self launchEngine:entrypoint libraryURI:libraryURI entrypointArgs:entrypointArgs];
1005 }
1006
1007 return _shell != nullptr;
1008 }
1009
1010 - (void)notifyLowMemory {
1011 if (_shell) {
1012 _shell->NotifyLowMemoryWarning();
1013 }
1014 [self.systemChannel sendMessage:@{@"type" : @"memoryPressure"}];
1015 }
1016
1017 #pragma mark - Text input delegate
1018
1019 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1020 updateEditingClient:(int)client
1021 withState:(NSDictionary*)state {
1022 [self.textInputChannel invokeMethod:@"TextInputClient.updateEditingState"
1023 arguments:@[ @(client), state ]];
1024 }
1025
1026 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1027 updateEditingClient:(int)client
1028 withState:(NSDictionary*)state
1029 withTag:(NSString*)tag {
1030 [self.textInputChannel invokeMethod:@"TextInputClient.updateEditingStateWithTag"
1031 arguments:@[ @(client), @{tag : state} ]];
1032 }
1033
1034 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1035 updateEditingClient:(int)client
1036 withDelta:(NSDictionary*)delta {
1037 [self.textInputChannel invokeMethod:@"TextInputClient.updateEditingStateWithDeltas"
1038 arguments:@[ @(client), delta ]];
1039 }
1040
1041 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1042 updateFloatingCursor:(FlutterFloatingCursorDragState)state
1043 withClient:(int)client
1044 withPosition:(NSDictionary*)position {
1045 NSString* stateString;
1046 switch (state) {
1047 case FlutterFloatingCursorDragStateStart:
1048 stateString = @"FloatingCursorDragState.start";
1049 break;
1050 case FlutterFloatingCursorDragStateUpdate:
1051 stateString = @"FloatingCursorDragState.update";
1052 break;
1053 case FlutterFloatingCursorDragStateEnd:
1054 stateString = @"FloatingCursorDragState.end";
1055 break;
1056 }
1057 [self.textInputChannel invokeMethod:@"TextInputClient.updateFloatingCursor"
1058 arguments:@[ @(client), stateString, position ]];
1059 }
1060
1061 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1062 performAction:(FlutterTextInputAction)action
1063 withClient:(int)client {
1064 NSString* actionString;
1065 switch (action) {
1066 case FlutterTextInputActionUnspecified:
1067 // Where did the term "unspecified" come from? iOS has a "default" and Android
1068 // has "unspecified." These 2 terms seem to mean the same thing but we need
1069 // to pick just one. "unspecified" was chosen because "default" is often a
1070 // reserved word in languages with switch statements (dart, java, etc).
1071 actionString = @"TextInputAction.unspecified";
1072 break;
1073 case FlutterTextInputActionDone:
1074 actionString = @"TextInputAction.done";
1075 break;
1076 case FlutterTextInputActionGo:
1077 actionString = @"TextInputAction.go";
1078 break;
1079 case FlutterTextInputActionSend:
1080 actionString = @"TextInputAction.send";
1081 break;
1082 case FlutterTextInputActionSearch:
1083 actionString = @"TextInputAction.search";
1084 break;
1085 case FlutterTextInputActionNext:
1086 actionString = @"TextInputAction.next";
1087 break;
1088 case FlutterTextInputActionContinue:
1089 actionString = @"TextInputAction.continueAction";
1090 break;
1091 case FlutterTextInputActionJoin:
1092 actionString = @"TextInputAction.join";
1093 break;
1094 case FlutterTextInputActionRoute:
1095 actionString = @"TextInputAction.route";
1096 break;
1097 case FlutterTextInputActionEmergencyCall:
1098 actionString = @"TextInputAction.emergencyCall";
1099 break;
1100 case FlutterTextInputActionNewline:
1101 actionString = @"TextInputAction.newline";
1102 break;
1103 }
1104 [self.textInputChannel invokeMethod:@"TextInputClient.performAction"
1105 arguments:@[ @(client), actionString ]];
1106 }
1107
1108 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1109 showAutocorrectionPromptRectForStart:(NSUInteger)start
1110 end:(NSUInteger)end
1111 withClient:(int)client {
1112 [self.textInputChannel invokeMethod:@"TextInputClient.showAutocorrectionPromptRect"
1113 arguments:@[ @(client), @(start), @(end) ]];
1114 }
1115
1116 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1117 willDismissEditMenuWithTextInputClient:(int)client {
1118 [self.platformChannel invokeMethod:@"ContextMenu.onDismissSystemContextMenu"
1119 arguments:@[ @(client) ]];
1120 }
1121
1122 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1123 shareSelectedText:(NSString*)selectedText {
1124 [self.platformPlugin showShareViewController:selectedText];
1125 }
1126
1127 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1128 searchWebWithSelectedText:(NSString*)selectedText {
1129 [self.platformPlugin searchWeb:selectedText];
1130 }
1131
1132 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1133 lookUpSelectedText:(NSString*)selectedText {
1134 [self.platformPlugin showLookUpViewController:selectedText];
1135 }
1136
1137 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1138 performContextMenuCustomActionWithActionID:(NSString*)actionID
1139 textInputClient:(int)client {
1140 [self.platformChannel invokeMethod:@"ContextMenu.onPerformCustomAction"
1141 arguments:@[ @(client), actionID ]];
1142 }
1143
1144 #pragma mark - FlutterViewEngineDelegate
1145
1146 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView showToolbar:(int)client {
1147 // TODO(justinmc): Switch from the TextInputClient to Scribble channel when
1148 // the framework has finished transitioning to the Scribble channel.
1149 // https://github.com/flutter/flutter/pull/115296
1150 [self.textInputChannel invokeMethod:@"TextInputClient.showToolbar" arguments:@[ @(client) ]];
1151 }
1152
1153 - (void)flutterTextInputPlugin:(FlutterTextInputPlugin*)textInputPlugin
1154 focusElement:(UIScribbleElementIdentifier)elementIdentifier
1155 atPoint:(CGPoint)referencePoint
1156 result:(FlutterResult)callback {
1157 // TODO(justinmc): Switch from the TextInputClient to Scribble channel when
1158 // the framework has finished transitioning to the Scribble channel.
1159 // https://github.com/flutter/flutter/pull/115296
1160 [self.textInputChannel
1161 invokeMethod:@"TextInputClient.focusElement"
1162 arguments:@[ elementIdentifier, @(referencePoint.x), @(referencePoint.y) ]
1163 result:callback];
1164 }
1165
1166 - (void)flutterTextInputPlugin:(FlutterTextInputPlugin*)textInputPlugin
1167 requestElementsInRect:(CGRect)rect
1168 result:(FlutterResult)callback {
1169 // TODO(justinmc): Switch from the TextInputClient to Scribble channel when
1170 // the framework has finished transitioning to the Scribble channel.
1171 // https://github.com/flutter/flutter/pull/115296
1172 [self.textInputChannel
1173 invokeMethod:@"TextInputClient.requestElementsInRect"
1174 arguments:@[ @(rect.origin.x), @(rect.origin.y), @(rect.size.width), @(rect.size.height) ]
1175 result:callback];
1176 }
1177
1178 - (void)flutterTextInputViewScribbleInteractionBegan:(FlutterTextInputView*)textInputView {
1179 // TODO(justinmc): Switch from the TextInputClient to Scribble channel when
1180 // the framework has finished transitioning to the Scribble channel.
1181 // https://github.com/flutter/flutter/pull/115296
1182 [self.textInputChannel invokeMethod:@"TextInputClient.scribbleInteractionBegan" arguments:nil];
1183 }
1184
1185 - (void)flutterTextInputViewScribbleInteractionFinished:(FlutterTextInputView*)textInputView {
1186 // TODO(justinmc): Switch from the TextInputClient to Scribble channel when
1187 // the framework has finished transitioning to the Scribble channel.
1188 // https://github.com/flutter/flutter/pull/115296
1189 [self.textInputChannel invokeMethod:@"TextInputClient.scribbleInteractionFinished" arguments:nil];
1190 }
1191
1192 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1193 insertTextPlaceholderWithSize:(CGSize)size
1194 withClient:(int)client {
1195 // TODO(justinmc): Switch from the TextInputClient to Scribble channel when
1196 // the framework has finished transitioning to the Scribble channel.
1197 // https://github.com/flutter/flutter/pull/115296
1198 [self.textInputChannel invokeMethod:@"TextInputClient.insertTextPlaceholder"
1199 arguments:@[ @(client), @(size.width), @(size.height) ]];
1200 }
1201
1202 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1203 removeTextPlaceholder:(int)client {
1204 // TODO(justinmc): Switch from the TextInputClient to Scribble channel when
1205 // the framework has finished transitioning to the Scribble channel.
1206 // https://github.com/flutter/flutter/pull/115296
1207 [self.textInputChannel invokeMethod:@"TextInputClient.removeTextPlaceholder"
1208 arguments:@[ @(client) ]];
1209 }
1210
1211 - (void)flutterTextInputView:(FlutterTextInputView*)textInputView
1212 didResignFirstResponderWithTextInputClient:(int)client {
1213 // When flutter text input view resign first responder, send a message to
1214 // framework to ensure the focus state is correct. This is useful when close
1215 // keyboard from platform side.
1216 [self.textInputChannel invokeMethod:@"TextInputClient.onConnectionClosed"
1217 arguments:@[ @(client) ]];
1218
1219 // Platform view's first responder detection logic:
1220 //
1221 // All text input widgets (e.g. EditableText) are backed by a dummy UITextInput view
1222 // in the TextInputPlugin. When this dummy UITextInput view resigns first responder,
1223 // check if any platform view becomes first responder. If any platform view becomes
1224 // first responder, send a "viewFocused" channel message to inform the framework to un-focus
1225 // the previously focused text input.
1226 //
1227 // Caveat:
1228 // 1. This detection logic does not cover the scenario when a platform view becomes
1229 // first responder without any flutter text input resigning its first responder status
1230 // (e.g. user tapping on platform view first). For now it works fine because the TextInputPlugin
1231 // does not track the focused platform view id (which is different from Android implementation).
1232 //
1233 // 2. This detection logic assumes that all text input widgets are backed by a dummy
1234 // UITextInput view in the TextInputPlugin, which may not hold true in the future.
1235
1236 // Have to check in the next run loop, because iOS requests the previous first responder to
1237 // resign before requesting the next view to become first responder.
1238 dispatch_async(dispatch_get_main_queue(), ^(void) {
1239 long platform_view_id = [self.platformViewsController firstResponderPlatformViewId];
1240 if (platform_view_id == -1) {
1241 return;
1242 }
1243
1244 [self.platformViewsChannel invokeMethod:@"viewFocused" arguments:@(platform_view_id)];
1245 });
1246 }
1247
1248 #pragma mark - Undo Manager Delegate
1249
1250 - (void)handleUndoWithDirection:(FlutterUndoRedoDirection)direction {
1251 NSString* action = (direction == FlutterUndoRedoDirectionUndo) ? @"undo" : @"redo";
1252 [self.undoManagerChannel invokeMethod:@"UndoManagerClient.handleUndo" arguments:@[ action ]];
1253 }
1254
1255 - (UIView<UITextInput>*)activeTextInputView {
1256 return [[self textInputPlugin] textInputView];
1257 }
1258
1259 - (NSUndoManager*)undoManager {
1260 return self.viewController.undoManager;
1261 }
1262
1263 #pragma mark - Screenshot Delegate
1264
1265 - (flutter::Rasterizer::Screenshot)takeScreenshot:(flutter::Rasterizer::ScreenshotType)type
1266 asBase64Encoded:(BOOL)base64Encode {
1267 FML_DCHECK(_shell) << "Cannot takeScreenshot without a shell";
1268 return _shell->Screenshot(type, base64Encode);
1269 }
1270
1271 - (void)flutterViewAccessibilityDidCall {
1272 if (self.viewController.view.accessibilityElements == nil) {
1273 [self ensureSemanticsEnabled];
1274 }
1275 }
1276
1277 - (NSObject<FlutterBinaryMessenger>*)binaryMessenger {
1278 return _binaryMessenger;
1279 }
1280
1281 - (NSObject<FlutterTextureRegistry>*)textureRegistry {
1282 return _textureRegistry;
1283 }
1284
1285 // For test only. Ideally we should create a dependency injector for all dependencies and
1286 // remove this.
1287 - (void)setBinaryMessenger:(FlutterBinaryMessengerRelay*)binaryMessenger {
1288 // Discard the previous messenger and keep the new one.
1289 if (binaryMessenger != _binaryMessenger) {
1290 _binaryMessenger.parent = nil;
1291 _binaryMessenger = binaryMessenger;
1292 }
1293 }
1294
1295 #pragma mark - FlutterBinaryMessenger
1296
1297 - (void)sendOnChannel:(NSString*)channel message:(NSData*)message {
1298 [self sendOnChannel:channel message:message binaryReply:nil];
1299 }
1300
1301 - (void)sendOnChannel:(NSString*)channel
1302 message:(NSData*)message
1303 binaryReply:(FlutterBinaryReply)callback {
1304 NSParameterAssert(channel);
1305 NSAssert(_shell && _shell->IsSetup(),
1306 @"Sending a message before the FlutterEngine has been run.");
1307 fml::RefPtr<flutter::PlatformMessageResponseDarwin> response =
1308 (callback == nil) ? nullptr
1309 : fml::MakeRefCounted<flutter::PlatformMessageResponseDarwin>(
1310 ^(NSData* reply) {
1311 callback(reply);
1312 },
1313 _shell->GetTaskRunners().GetPlatformTaskRunner());
1314 std::unique_ptr<flutter::PlatformMessage> platformMessage =
1315 (message == nil) ? std::make_unique<flutter::PlatformMessage>(channel.UTF8String, response)
1316 : std::make_unique<flutter::PlatformMessage>(
1317 channel.UTF8String, flutter::CopyNSDataToMapping(message), response);
1318
1319 _shell->GetPlatformView()->DispatchPlatformMessage(std::move(platformMessage));
1320 // platformMessage takes ownership of response.
1321 // NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
1322 }
1323
1324 - (NSObject<FlutterTaskQueue>*)makeBackgroundTaskQueue {
1325 return flutter::PlatformMessageHandlerIos::MakeBackgroundTaskQueue();
1326 }
1327
1328 - (FlutterBinaryMessengerConnection)setMessageHandlerOnChannel:(NSString*)channel
1329 binaryMessageHandler:
1330 (FlutterBinaryMessageHandler)handler {
1331 return [self setMessageHandlerOnChannel:channel binaryMessageHandler:handler taskQueue:nil];
1332 }
1333
1334 - (FlutterBinaryMessengerConnection)
1335 setMessageHandlerOnChannel:(NSString*)channel
1336 binaryMessageHandler:(FlutterBinaryMessageHandler)handler
1337 taskQueue:(NSObject<FlutterTaskQueue>* _Nullable)taskQueue {
1338 NSParameterAssert(channel);
1339 if (_shell && _shell->IsSetup()) {
1340 self.platformView->GetPlatformMessageHandlerIos()->SetMessageHandler(channel.UTF8String,
1341 handler, taskQueue);
1342 return [self.connections acquireConnectionForChannel:channel];
1343 } else {
1344 NSAssert(!handler, @"Setting a message handler before the FlutterEngine has been run.");
1345 // Setting a handler to nil for a channel that has not yet been set up is a no-op.
1346 return [FlutterConnectionCollection makeErrorConnectionWithErrorCode:-1L];
1347 }
1348 }
1349
1350 - (void)cleanUpConnection:(FlutterBinaryMessengerConnection)connection {
1351 if (_shell && _shell->IsSetup()) {
1352 NSString* channel = [self.connections cleanupConnectionWithID:connection];
1353 if (channel.length > 0) {
1354 self.platformView->GetPlatformMessageHandlerIos()->SetMessageHandler(channel.UTF8String, nil,
1355 nil);
1356 }
1357 }
1358 }
1359
1360 #pragma mark - FlutterTextureRegistry
1361
1362 - (int64_t)registerTexture:(NSObject<FlutterTexture>*)texture {
1363 FML_DCHECK(self.platformView);
1364 int64_t textureId = self.nextTextureId++;
1365 self.platformView->RegisterExternalTexture(textureId, texture);
1366 return textureId;
1367 }
1368
1369 - (void)unregisterTexture:(int64_t)textureId {
1370 _shell->GetPlatformView()->UnregisterTexture(textureId);
1371 }
1372
1373 - (void)textureFrameAvailable:(int64_t)textureId {
1374 _shell->GetPlatformView()->MarkTextureFrameAvailable(textureId);
1375 }
1376
1377 - (NSString*)lookupKeyForAsset:(NSString*)asset {
1378 return [FlutterDartProject lookupKeyForAsset:asset];
1379 }
1380
1381 - (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
1382 return [FlutterDartProject lookupKeyForAsset:asset fromPackage:package];
1383 }
1384
1385 - (id<FlutterPluginRegistry>)pluginRegistry {
1386 return self;
1387 }
1388
1389 #pragma mark - FlutterPluginRegistry
1390
1391 - (NSObject<FlutterPluginRegistrar>*)registrarForPlugin:(NSString*)pluginKey {
1392 NSAssert(self.pluginPublications[pluginKey] == nil, @"Duplicate plugin key: %@", pluginKey);
1393 self.pluginPublications[pluginKey] = [NSNull null];
1394 FlutterEnginePluginRegistrar* result = [[FlutterEnginePluginRegistrar alloc] initWithKey:pluginKey
1395 flutterEngine:self];
1396 self.registrars[pluginKey] = result;
1397 return result;
1398 }
1399
1400 - (NSObject<FlutterApplicationRegistrar>*)registrarForApplication:(NSString*)key {
1401 NSAssert(self.pluginPublications[key] == nil, @"Duplicate key: %@", key);
1402 self.pluginPublications[key] = [NSNull null];
1403 FlutterEngineApplicationRegistrar* result =
1404 [[FlutterEngineApplicationRegistrar alloc] initWithKey:key flutterEngine:self];
1405 self.registrars[key] = result;
1406 return result;
1407 }
1408
1409 - (BOOL)hasPlugin:(NSString*)pluginKey {
1410 return _pluginPublications[pluginKey] != nil;
1411 }
1412
1413 - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey {
1414 return _pluginPublications[pluginKey];
1415 }
1416
1417 - (void)addSceneLifeCycleDelegate:(NSObject<FlutterSceneLifeCycleDelegate>*)delegate {
1418 [self.sceneLifeCycleDelegate addDelegate:delegate];
1419 }
1420
1421 #pragma mark - Notifications
1422
1423 - (void)sceneWillEnterForeground:(NSNotification*)notification API_AVAILABLE(ios(13.0)) {
1424 [self flutterWillEnterForeground:notification];
1425 }
1426
1427 - (void)sceneDidEnterBackground:(NSNotification*)notification API_AVAILABLE(ios(13.0)) {
1428 [self flutterDidEnterBackground:notification];
1429 }
1430
1431 - (void)applicationWillEnterForeground:(NSNotification*)notification {
1432 [self flutterWillEnterForeground:notification];
1433 }
1434
1435 - (void)applicationDidEnterBackground:(NSNotification*)notification {
1436 [self flutterDidEnterBackground:notification];
1437 }
1438
1439 - (void)flutterWillEnterForeground:(NSNotification*)notification {
1440 [self setIsGpuDisabled:NO];
1441 }
1442
1443 - (void)flutterDidEnterBackground:(NSNotification*)notification {
1444 [self setIsGpuDisabled:YES];
1445 [self notifyLowMemory];
1446 }
1447
1448 - (void)onMemoryWarning:(NSNotification*)notification {
1449 [self notifyLowMemory];
1450 }
1451
1452 - (void)setIsGpuDisabled:(BOOL)value {
1453 if (_shell) {
1454 _shell->SetGpuAvailability(value ? flutter::GpuAvailability::kUnavailable
1455 : flutter::GpuAvailability::kAvailable);
1456 }
1457 _isGpuDisabled = value;
1458 }
1459
1460 #pragma mark - Locale updates
1461
1462 - (void)onLocaleUpdated:(NSNotification*)notification {
1463 // Get and pass the user's preferred locale list to dart:ui.
1464 NSMutableArray<NSString*>* localeData = [[NSMutableArray alloc] init];
1465 NSArray<NSString*>* preferredLocales = [NSLocale preferredLanguages];
1466 for (NSString* localeID in preferredLocales) {
1467 NSLocale* locale = [[NSLocale alloc] initWithLocaleIdentifier:localeID];
1468 NSString* languageCode = [locale objectForKey:NSLocaleLanguageCode];
1469 NSString* countryCode = [locale objectForKey:NSLocaleCountryCode];
1470 NSString* scriptCode = [locale objectForKey:NSLocaleScriptCode];
1471 NSString* variantCode = [locale objectForKey:NSLocaleVariantCode];
1472 if (!languageCode) {
1473 continue;
1474 }
1475 [localeData addObject:languageCode];
1476 [localeData addObject:(countryCode ? countryCode : @"")];
1477 [localeData addObject:(scriptCode ? scriptCode : @"")];
1478 [localeData addObject:(variantCode ? variantCode : @"")];
1479 }
1480 if (localeData.count == 0) {
1481 return;
1482 }
1483 [self.localizationChannel invokeMethod:@"setLocale" arguments:localeData];
1484 }
1485
1486 - (void)waitForFirstFrameSync:(NSTimeInterval)timeout
1487 callback:(NS_NOESCAPE void (^_Nonnull)(BOOL didTimeout))callback {
1488 fml::TimeDelta waitTime = fml::TimeDelta::FromMilliseconds(timeout * 1000);
1489 fml::Status status = self.shell.WaitForFirstFrame(waitTime);
1490 callback(status.code() == fml::StatusCode::kDeadlineExceeded);
1491 }
1492
1493 - (void)waitForFirstFrame:(NSTimeInterval)timeout
1494 callback:(void (^_Nonnull)(BOOL didTimeout))callback {
1495 dispatch_queue_t queue = dispatch_get_global_queue(QOS_CLASS_BACKGROUND, 0);
1496 dispatch_group_t group = dispatch_group_create();
1497
1498 __weak FlutterEngine* weakSelf = self;
1499 __block BOOL didTimeout = NO;
1500 dispatch_group_async(group, queue, ^{
1501 FlutterEngine* strongSelf = weakSelf;
1502 if (!strongSelf) {
1503 return;
1504 }
1505
1506 fml::TimeDelta waitTime = fml::TimeDelta::FromMilliseconds(timeout * 1000);
1507 fml::Status status = strongSelf.shell.WaitForFirstFrame(waitTime);
1508 didTimeout = status.code() == fml::StatusCode::kDeadlineExceeded;
1509 });
1510
1511 // Only execute the main queue task once the background task has completely finished executing.
1512 dispatch_group_notify(group, dispatch_get_main_queue(), ^{
1513 // Strongly capture self on the task dispatched to the main thread.
1514 //
1515 // When we capture weakSelf strongly in the above block on a background thread, we risk the
1516 // possibility that all other strong references to FlutterEngine go out of scope while the block
1517 // executes and that the engine is dealloc'ed at the end of the above block on a background
1518 // thread. FlutterEngine is not safe to release on any thread other than the main thread.
1519 //
1520 // self is never nil here since it's a strong reference that's verified non-nil above, but we
1521 // use a conditional check to avoid an unused expression compiler warning.
1522 FlutterEngine* strongSelf = self;
1523 if (!strongSelf) {
1524 return;
1525 }
1526 callback(didTimeout);
1527 });
1528 }
1529
1530 - (FlutterEngine*)spawnWithEntrypoint:(/*nullable*/ NSString*)entrypoint
1531 libraryURI:(/*nullable*/ NSString*)libraryURI
1532 initialRoute:(/*nullable*/ NSString*)initialRoute
1533 entrypointArgs:(/*nullable*/ NSArray<NSString*>*)entrypointArgs {
1534 NSAssert(_shell, @"Spawning from an engine without a shell (possibly not run).");
1535 FlutterEngine* result = [[FlutterEngine alloc] initWithName:self.labelPrefix
1536 project:self.dartProject
1537 allowHeadlessExecution:self.allowHeadlessExecution];
1538 flutter::RunConfiguration configuration =
1539 [self.dartProject runConfigurationForEntrypoint:entrypoint
1540 libraryOrNil:libraryURI
1541 entrypointArgs:entrypointArgs];
1542
1543 configuration.SetEngineId(result.engineIdentifier);
1544
1545 fml::WeakPtr<flutter::PlatformView> platform_view = _shell->GetPlatformView();
1546 FML_DCHECK(platform_view);
1547 // Static-cast safe since this class always creates PlatformViewIOS instances.
1548 flutter::PlatformViewIOS* ios_platform_view =
1549 static_cast<flutter::PlatformViewIOS*>(platform_view.get());
1550 std::shared_ptr<flutter::IOSContext> context = ios_platform_view->GetIosContext();
1551 FML_DCHECK(context);
1552
1553 // Lambda captures by pointers to ObjC objects are fine here because the
1554 // create call is synchronous.
1555 flutter::Shell::CreateCallback<flutter::PlatformView> on_create_platform_view =
1556 [result, context](flutter::Shell& shell) {
1557 [result recreatePlatformViewsController];
1558 result.platformViewsController.taskRunner = shell.GetTaskRunners().GetPlatformTaskRunner();
1559 return std::make_unique<flutter::PlatformViewIOS>(
1560 shell, context, result.platformViewsController, shell.GetTaskRunners());
1561 };
1562
1563 flutter::Shell::CreateCallback<flutter::Rasterizer> on_create_rasterizer =
1564 [](flutter::Shell& shell) { return std::make_unique<flutter::Rasterizer>(shell); };
1565
1566 std::string cppInitialRoute;
1567 if (initialRoute) {
1568 cppInitialRoute = [initialRoute UTF8String];
1569 }
1570
1571 std::unique_ptr<flutter::Shell> shell = _shell->Spawn(
1572 std::move(configuration), cppInitialRoute, on_create_platform_view, on_create_rasterizer);
1573
1574 result->_threadHost = _threadHost;
1575 result->_profiler = _profiler;
1576 result->_isGpuDisabled = _isGpuDisabled;
1577 [result setUpShell:std::move(shell) withVMServicePublication:NO];
1578 return result;
1579 }
1580
1581 - (const flutter::ThreadHost&)threadHost {
1582 return *_threadHost;
1583 }
1584
1585 - (FlutterDartProject*)project {
1586 return self.dartProject;
1587 }
1588
1589 - (void)sendDeepLinkToFramework:(NSURL*)url completionHandler:(void (^)(BOOL success))completion {
1590 __weak FlutterEngine* weakSelf = self;
1591 [self waitForFirstFrame:3.0
1592 callback:^(BOOL didTimeout) {
1593 if (didTimeout) {
1594 [FlutterLogger
1595 logError:@"Timeout waiting for first frame when launching a URL."];
1596 completion(NO);
1597 } else {
1598 // invove the method and get the result
1599 [weakSelf.navigationChannel
1600 invokeMethod:@"pushRouteInformation"
1601 arguments:@{
1602 @"location" : url.absoluteString ?: [NSNull null],
1603 }
1604 result:^(id _Nullable result) {
1605 BOOL success =
1606 [result isKindOfClass:[NSNumber class]] && [result boolValue];
1607 if (!success) {
1608 // Logging the error if the result is not successful
1609 [FlutterLogger
1610 logError:@"Failed to handle route information in Flutter."];
1611 }
1612 completion(success);
1613 }];
1614 }
1615 }];
1616 }
1617
1618 @end
1619
1620 @implementation FlutterEngineBaseRegistrar
1621
1622 - (instancetype)initWithKey:(NSString*)key flutterEngine:(FlutterEngine*)flutterEngine {
1623 self = [super init];
1624 NSAssert(self, @"Super init cannot be nil");
1625 _key = [key copy];
1626 _flutterEngine = flutterEngine;
1627 return self;
1628 }
1629
1630 - (NSObject<FlutterBinaryMessenger>*)messenger {
1631 return _flutterEngine.binaryMessenger;
1632 }
1633 - (NSObject<FlutterTextureRegistry>*)textures {
1634 return _flutterEngine.textureRegistry;
1635 }
1636
1637 - (void)registerViewFactory:(NSObject<FlutterPlatformViewFactory>*)factory
1638 withId:(NSString*)factoryId {
1639 [self registerViewFactory:factory
1640 withId:factoryId
1641 gestureRecognizersBlockingPolicy:FlutterPlatformViewGestureRecognizersBlockingPolicyEager];
1642 }
1643
1644 - (void)registerViewFactory:(NSObject<FlutterPlatformViewFactory>*)factory
1645 withId:(NSString*)factoryId
1646 gestureRecognizersBlockingPolicy:
1647 (FlutterPlatformViewGestureRecognizersBlockingPolicy)gestureRecognizersBlockingPolicy {
1648 [_flutterEngine.platformViewsController registerViewFactory:factory
1649 withId:factoryId
1650 gestureRecognizersBlockingPolicy:gestureRecognizersBlockingPolicy];
1651 }
1652
1653 @end
1654
1655 @implementation FlutterEnginePluginRegistrar
1656
1657 - (nullable UIViewController*)viewController {
1658 return self.flutterEngine.viewController;
1659 }
1660
1661 - (void)publish:(NSObject*)value {
1662 self.flutterEngine.pluginPublications[self.key] = value;
1663 }
1664
1665 - (void)addMethodCallDelegate:(NSObject<FlutterPlugin>*)delegate
1666 channel:(FlutterMethodChannel*)channel {
1667 [channel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) {
1668 [delegate handleMethodCall:call result:result];
1669 }];
1670 }
1671
1672 - (void)addApplicationDelegate:(NSObject<FlutterPlugin>*)delegate {
1673 id<UIApplicationDelegate> appDelegate = FlutterSharedApplication.application.delegate;
1674 if ([appDelegate conformsToProtocol:@protocol(FlutterAppLifeCycleProvider)]) {
1675 id<FlutterAppLifeCycleProvider> lifeCycleProvider =
1676 (id<FlutterAppLifeCycleProvider>)appDelegate;
1677 [lifeCycleProvider addApplicationLifeCycleDelegate:delegate];
1678 }
1679 if (![delegate conformsToProtocol:@protocol(FlutterSceneLifeCycleDelegate)]) {
1680 // TODO(vashworth): If the plugin doesn't conform to the FlutterSceneLifeCycleDelegate,
1681 // print a warning pointing to documentation: https://github.com/flutter/flutter/issues/175956
1682 // [FlutterLogger logWarning:[NSString stringWithFormat:@"Plugin %@ has not migrated to
1683 // scenes.", self.key]];
1684 }
1685 }
1686
1687 - (void)addSceneDelegate:(NSObject<FlutterSceneLifeCycleDelegate>*)delegate {
1688 // If the plugin conforms to FlutterSceneLifeCycleDelegate, add it to the engine.
1689 [self.flutterEngine addSceneLifeCycleDelegate:delegate];
1690 }
1691
1692 - (NSString*)lookupKeyForAsset:(NSString*)asset {
1693 return [self.flutterEngine lookupKeyForAsset:asset];
1694 }
1695
1696 - (NSString*)lookupKeyForAsset:(NSString*)asset fromPackage:(NSString*)package {
1697 return [self.flutterEngine lookupKeyForAsset:asset fromPackage:package];
1698 }
1699
1700 @end
1701
1702 @implementation FlutterEngineApplicationRegistrar
1703 @end
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
FlutterDartProject_Internal.h
FlutterDartVMServicePublisher.h
FlutterDefaultDartEntrypoint
NSString *const FlutterDefaultDartEntrypoint
Definition: FlutterEngine.mm:85
_profiler
std::shared_ptr< flutter::SamplingProfiler > _profiler
Definition: FlutterEngine.mm:197
_shell
std::unique_ptr< flutter::Shell > _shell
Definition: FlutterEngine.mm:192
_appRegistrar
NSObject< FlutterApplicationRegistrar > * _appRegistrar
Definition: FlutterEngine.mm:169
kFlutterKeyDataChannel
NSString *const kFlutterKeyDataChannel
Definition: FlutterEngine.mm:90
FlutterDefaultInitialRoute
NSString *const FlutterDefaultInitialRoute
Definition: FlutterEngine.mm:86
_renderingApi
flutter::IOSRenderingAPI _renderingApi
Definition: FlutterEngine.mm:196
_textureRegistry
FlutterTextureRegistryRelay * _textureRegistry
Definition: FlutterEngine.mm:200
IOSPlatformThreadConfigSetter
static FLUTTER_ASSERT_ARC void IOSPlatformThreadConfigSetter(const fml::Thread::ThreadConfig &config)
Definition: FlutterEngine.mm:51
kNumProfilerSamplesPerSec
static constexpr int kNumProfilerSamplesPerSec
Definition: FlutterEngine.mm:91
kFlutterApplicationRegistrarKey
NSString *const kFlutterApplicationRegistrarKey
Definition: FlutterEngine.mm:92
_binaryMessenger
FlutterBinaryMessengerRelay * _binaryMessenger
Definition: FlutterEngine.mm:199
FlutterEngine_Internal.h
platform_view
std::unique_ptr< flutter::PlatformViewIOS > platform_view
Definition: FlutterEnginePlatformViewTest.mm:68
FlutterIndirectScribbleDelegate.h
FlutterPlatformPlugin.h
FlutterPlatformViewGestureRecognizersBlockingPolicy
FlutterPlatformViewGestureRecognizersBlockingPolicy
Definition: FlutterPlugin.h:253
_restorationEnabled
BOOL _restorationEnabled
Definition: FlutterRestorationPlugin.mm:18
FlutterSharedApplication.h
FlutterSpellCheckPlugin.h
FlutterTextInputDelegate.h
viewController
FlutterViewController * viewController
Definition: FlutterTextInputPluginTest.mm:139
textInputPlugin
FlutterTextInputPlugin * textInputPlugin
Definition: FlutterTextInputPluginTest.mm:137
engine
id engine
Definition: FlutterTextInputPluginTest.mm:136
FlutterTextureRegistryRelay.h
FlutterUndoManagerDelegate.h
FlutterUndoManagerPlugin.h
FlutterViewController_Internal.h
UIViewController+FlutterScreenAndSceneIfLoaded.h
FlutterEngine(Test)::enableEmbedderAPI
BOOL enableEmbedderAPI
Definition: FlutterEngine_Test.h:25
FlutterEngine(Test)::restorationPlugin
FlutterRestorationPlugin * restorationPlugin
Definition: FlutterEngine_Test.h:27
FlutterEngine(Test)::pluginPublications
NSMutableDictionary * pluginPublications
Definition: FlutterEngine_Test.h:26
FlutterEngine(Test)::embedderAPI
FlutterEngineProcTable & embedderAPI
Definition: FlutterEngine_Test.h:24
FlutterDartProject
Definition: FlutterDartProject.mm:268
+[FlutterDartProject lookupKeyForAsset:fromPackage:]
NSString * lookupKeyForAsset:fromPackage:(NSString *asset,[fromPackage] NSString *package)
Definition: FlutterDartProject.mm:403
-[FlutterDartProject settings]
const flutter::Settings & settings()
+[FlutterDartProject lookupKeyForAsset:]
NSString * lookupKeyForAsset:(NSString *asset)
Definition: FlutterDartProject.mm:394
FlutterEnginePluginSceneLifeCycleDelegate
Definition: FlutterSceneLifeCycle.mm:511
FlutterImplicitEngineBridgeImpl
Definition: FlutterEngine.mm:169
FlutterTextInputView
Definition: FlutterTextInputPlugin.mm:812
flutter::PlatformMessageHandlerIos::MakeBackgroundTaskQueue
static NSObject< FlutterTaskQueue > * MakeBackgroundTaskQueue()
Definition: platform_message_handler_ios.mm:36
flutter::PlatformViewIOS
Definition: platform_view_ios.h:40
flutter::PlatformViewIOS::GetIosContext
const std::shared_ptr< IOSContext > & GetIosContext()
Definition: platform_view_ios.h:128
flutter::PlatformViewIOS::SetSemanticsEnabled
void SetSemanticsEnabled(bool enabled) override
Definition: platform_view_ios.mm:140
command_line.h
FlutterBasicMessageChannel
Definition: FlutterChannels.h:38
FlutterBinaryMessengerRelay
Definition: FlutterBinaryMessengerRelay.h:14
FlutterBinaryMessengerRelay::parent
NSObject< FlutterBinaryMessenger > * parent
Definition: FlutterBinaryMessengerRelay.h:15
FlutterDartVMServicePublisher
Definition: FlutterDartVMServicePublisher.h:11
FlutterEngineApplicationRegistrar
Definition: FlutterEngine.mm:104
FlutterEngineBaseRegistrar
Definition: FlutterEngine.mm:94
FlutterEngineBaseRegistrar::key
NSString * key
Definition: FlutterEngine.mm:97
FlutterEngineBaseRegistrar::flutterEngine
FlutterEngine * flutterEngine
Definition: FlutterEngine.mm:96
FlutterEngine
Definition: FlutterEngine.h:61
FlutterEngine::textInputChannel
FlutterMethodChannel * textInputChannel
Definition: FlutterEngine.h:374
-[FlutterEngine platformView]
flutter::PlatformViewIOS * platformView()
-[FlutterEngine shell]
flutter::Shell & shell()
FlutterEngine::navigationChannel
FlutterMethodChannel * navigationChannel
Definition: FlutterEngine.h:345
FlutterEngine::keyEventChannel
FlutterBasicMessageChannel * keyEventChannel
Definition: FlutterEngine.h:412
FlutterEngine::lifecycleChannel
FlutterBasicMessageChannel * lifecycleChannel
Definition: FlutterEngine.h:385
FlutterEngine::platformChannel
FlutterMethodChannel * platformChannel
Definition: FlutterEngine.h:363
FlutterEngine::localizationChannel
FlutterMethodChannel * localizationChannel
Definition: FlutterEngine.h:335
FlutterEngine::isolateId
NSString * isolateId
Definition: FlutterEngine.h:439
FlutterEngine::systemChannel
FlutterBasicMessageChannel * systemChannel
Definition: FlutterEngine.h:396
FlutterEngine::settingsChannel
FlutterBasicMessageChannel * settingsChannel
Definition: FlutterEngine.h:404
FlutterEngine::restorationChannel
FlutterMethodChannel * restorationChannel
Definition: FlutterEngine.h:355
FlutterEnginePluginRegistrar
Definition: FlutterEngine.mm:107
FlutterError
Definition: FlutterCodecs.h:247
+[FlutterError errorWithCode:message:details:]
instancetype errorWithCode:message:details:(NSString *code,[message] NSString *_Nullable message,[details] id _Nullable details)
FlutterJSONMessageCodec
Definition: FlutterCodecs.h:81
FlutterJSONMethodCodec
Definition: FlutterCodecs.h:455
FlutterMethodChannel
Definition: FlutterChannels.h:221
-[FlutterMethodChannel setMethodCallHandler:]
void setMethodCallHandler:(FlutterMethodCallHandler _Nullable handler)
FlutterPlatformPlugin
Definition: FlutterPlatformPlugin.h:12
FlutterPlatformViewsController
Definition: FlutterPlatformViewsController.h:31
FlutterPluginSceneLifeCycleDelegate
Definition: FlutterSceneLifeCycle.h:149
FlutterRestorationPlugin
Definition: FlutterRestorationPlugin.h:13
FlutterSharedApplication
Definition: FlutterSharedApplication.h:13
FlutterSharedApplication::application
UIApplication * application
Definition: FlutterSharedApplication.h:27
FlutterSharedApplication::isAvailable
BOOL isAvailable
Definition: FlutterSharedApplication.h:22
FlutterSpellCheckPlugin
Definition: FlutterSpellCheckPlugin.h:12
FlutterStandardMethodCodec
Definition: FlutterCodecs.h:469
FlutterStringCodec
Definition: FlutterCodecs.h:63
FlutterTextInputPlugin
Definition: FlutterTextInputPlugin.h:34
FlutterTexture
Definition: FlutterMetalLayer.mm:61
FlutterTextureRegistryRelay
Definition: FlutterTextureRegistryRelay.h:20
FlutterTextureRegistryRelay::parent
NSObject< FlutterTextureRegistry > * parent
Definition: FlutterTextureRegistryRelay.h:25
FlutterUndoManagerPlugin
Definition: FlutterUndoManagerPlugin.h:14
FlutterViewController
Definition: FlutterViewController.h:57
FLUTTER_ASSERT_ARC
Definition: FlutterChannelKeyResponder.mm:13
flutter
Definition: accessibility_bridge.h:26
flutter::CopyNSDataToMapping
fml::MallocMapping CopyNSDataToMapping(NSData *data)
Definition: buffer_conversions.mm:29
flutter::GetRenderingAPIForProcess
IOSRenderingAPI GetRenderingAPIForProcess(bool force_software)
Definition: rendering_api_selection.mm:31
flutter::IOSRenderingAPI
IOSRenderingAPI
Definition: rendering_api_selection.h:14
fml
Definition: profiler_metrics_ios.mm:41
platform_message_response_darwin.h
platform_view_ios.h
profiler_metrics_ios.h
FlutterAppLifeCycleProvider-p
Definition: FlutterPlugin.h:501
FlutterApplicationRegistrar-p
Definition: FlutterPlugin.h:337
FlutterBaseRegistrar-p
Definition: FlutterPlugin.h:280
-[FlutterBaseRegistrar-p textures]
NSObject< FlutterTextureRegistry > * textures()
-[FlutterBaseRegistrar-p messenger]
NSObject< FlutterBinaryMessenger > * messenger()
FlutterBinaryMessenger-p
Definition: FlutterBinaryMessenger.h:49
FlutterImplicitEngineDelegate-p
Definition: FlutterEngine.h:481
FlutterIndirectScribbleDelegate-p
Definition: FlutterIndirectScribbleDelegate.h:13
+[FlutterMessageCodec-p sharedInstance]
instancetype sharedInstance()
FlutterPlatformViewFactory-p
Definition: FlutterPlatformViews.h:26
FlutterPlugin-p
Definition: FlutterPlugin.h:190
-[FlutterPlugin-p handleMethodCall:result:]
void handleMethodCall:result:(FlutterMethodCall *call,[result] FlutterResult result)
FlutterPluginRegistrar-p
Definition: FlutterPlugin.h:349
FlutterSceneLifeCycleDelegate-p
Definition: FlutterSceneLifeCycle.h:31
FlutterTaskQueue-p
Definition: FlutterBinaryMessenger.h:34
FlutterTextInputDelegate-p
Definition: FlutterTextInputDelegate.h:37
FlutterTextureRegistry-p
Definition: FlutterTexture.h:45
FlutterUndoManagerDelegate-p
Definition: FlutterUndoManagerDelegate.h:23
rendering_api_selection.h
vsync_waiter_ios.h