From 80cd050cae44d276c4532ea3c2de2752cefa9c7b Mon Sep 17 00:00:00 2001 From: Jenn Magder Date: Mon, 22 Apr 2024 12:03:49 -0700 Subject: [PATCH] Break dependency cycle of FlutterViewController <-> FlutterPlatformView (flutter/engine#52271) Trying to migrate the iOS embedder MRC to ARC, I'm attempting to break some dependency cycles so it's easier to migrate the "leaf" dependencies to ARC, working my way up. The cycle is: **`FlutterViewController`** -> `FlutterView` -> `FlutterPlatformView` -> (Before this PR)**`FlutterViewController`** `FlutterViewController` depends on many other large MRC classes, like FlutterEngine., so I'd like to pull that one out so `FlutterView`, `FlutterPlatformView`, and `FlutterOverlayView` can be migrated to ARC in a smaller PR. `FlutterPlatformView` only depends on `FlutterViewController` in this one place, casting a `UIViewController` and calling `-forceTouchesCancelled:`. https://github.com/flutter/engine/blob/0e6143504c0fa60038f3b181438600e1c5c12bf7/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm#L1201 Instead, move `-forceTouchesCancelled:` to the existing `FlutterViewResponder` protocol with the other touch events, which `FlutterViewController` already implements. The cast can then be removed, breaking the cycle. Clean up all the imports. Part of https://github.com/flutter/flutter/issues/137801, though this doesn't actually migrate anything to ARC. --- .../ios/framework/Source/FlutterOverlayView.h | 9 ------ .../framework/Source/FlutterOverlayView.mm | 15 ++-------- .../framework/Source/FlutterPlatformViews.mm | 26 ++++++----------- .../Source/FlutterPlatformViewsTest.mm | 5 ++-- .../Source/FlutterPlatformViews_Internal.h | 22 +++++++------- .../darwin/ios/framework/Source/FlutterView.h | 9 ++---- .../ios/framework/Source/FlutterView.mm | 11 ------- .../Source/FlutterViewController_Internal.h | 3 -- .../framework/Source/FlutterViewResponder.h | 29 +++++++++++++++++++ .../ios/framework/Source/FlutterViewTest.mm | 2 -- .../Source/accessibility_bridge_test.mm | 2 +- 11 files changed, 58 insertions(+), 75 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h index f94a0c4028..fe050d1ffc 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h @@ -8,15 +8,6 @@ #include #include -#include - -#include "flutter/fml/memory/weak_ptr.h" -#include "flutter/shell/common/shell.h" -#import "flutter/shell/platform/darwin/ios/ios_surface.h" -#include "fml/platform/darwin/cf_utils.h" - -#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h" - /// UIViews that are used by |FlutterPlatformViews| to present Flutter /// rendering on top of system compositor rendering (ex. a web view). /// diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.mm index 1bf00d8b40..dc29cd85dd 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.mm @@ -3,20 +3,11 @@ // found in the LICENSE file. #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h" -#include -#include -#include "flutter/common/settings.h" -#include "flutter/common/task_runners.h" -#include "flutter/flow/layers/layer_tree.h" -#include "flutter/fml/platform/darwin/cf_utils.h" -#include "flutter/fml/synchronization/waitable_event.h" -#include "flutter/fml/trace_event.h" -#include "flutter/shell/common/platform_view.h" -#include "flutter/shell/common/rasterizer.h" +#include + #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h" -#import "flutter/shell/platform/darwin/ios/ios_surface_software.h" -#include "third_party/skia/include/utils/mac/SkCGUtils.h" +#include "fml/platform/darwin/cf_utils.h" // This is mostly a duplication of FlutterView. // TODO(amirh): once GL support is in evaluate if we can merge this with FlutterView. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm index 6d605414e5..56ab003c8d 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews.mm @@ -2,21 +2,13 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. -#include -#import - -#include -#include -#include -#include - -#include "flutter/common/graphics/persistent_cache.h" -#include "flutter/fml/platform/darwin/scoped_nsobject.h" -#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" -#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h" + +#include + +#include "flutter/fml/platform/darwin/scoped_nsobject.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterOverlayView.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h" -#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" #import "flutter/shell/platform/darwin/ios/ios_surface.h" @implementation UIView (FirstResponder) @@ -171,11 +163,11 @@ void FlutterPlatformViewsController::SetFlutterView(UIView* flutter_view) { } void FlutterPlatformViewsController::SetFlutterViewController( - UIViewController* flutter_view_controller) { + UIViewController* flutter_view_controller) { flutter_view_controller_.reset([flutter_view_controller retain]); } -UIViewController* FlutterPlatformViewsController::getFlutterViewController() { +UIViewController* FlutterPlatformViewsController::getFlutterViewController() { return flutter_view_controller_.get(); } @@ -1147,7 +1139,7 @@ void FlutterPlatformViewsController::ResetFrameState() { // This gesture recognizer retains the `FlutterViewController` until the // end of a gesture sequence, that is all the touches in touchesBegan are concluded // with |touchesCancelled| or |touchesEnded|. - fml::scoped_nsobject _flutterViewController; + fml::scoped_nsobject> _flutterViewController; } - (instancetype)initWithTarget:(id)target @@ -1198,7 +1190,7 @@ void FlutterPlatformViewsController::ResetFrameState() { // Flutter needs all the cancelled touches to be "cancelled" change types in order to correctly // handle gesture sequence. // We always override the change type to "cancelled". - [((FlutterViewController*)_flutterViewController.get()) forceTouchesCancelled:touches]; + [_flutterViewController.get() forceTouchesCancelled:touches]; _currentTouchPointersCount -= touches.count; if (_currentTouchPointersCount == 0) { self.state = UIGestureRecognizerStateFailed; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm index aad952fa87..2dc397d756 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm @@ -6,13 +6,12 @@ #import #import -#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h" +#import "flutter/fml/thread.h" #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTouchInterceptingView_Test.h" -#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" #import "flutter/shell/platform/darwin/ios/platform_view_ios.h" FLUTTER_ASSERT_ARC @@ -2158,7 +2157,7 @@ fml::RefPtr CreateNewThread(const std::string& name) { [forwardGectureRecognizer touchesBegan:touches1 withEvent:event1]; OCMVerify([mockFlutterViewContoller touchesBegan:touches1 withEvent:event1]); - UIViewController* mockFlutterViewContoller2 = OCMClassMock([UIViewController class]); + FlutterViewController* mockFlutterViewContoller2 = OCMClassMock([FlutterViewController class]); flutterPlatformViewsController->SetFlutterViewController(mockFlutterViewContoller2); // Touch events should still send to the old FlutterViewController if FlutterViewController diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h index 6dba0efd89..b3337a1d69 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h @@ -5,15 +5,17 @@ #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_ #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWS_INTERNAL_H_ -#include -#include "flutter/flow/embedded_views.h" -#include "flutter/fml/platform/darwin/scoped_nsobject.h" -#include "flutter/shell/common/shell.h" -#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h" -#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h" + +#include + +#include "flutter/flow/surface.h" +#include "flutter/fml/memory/weak_ptr.h" +#include "flutter/fml/platform/darwin/scoped_nsobject.h" +#include "flutter/fml/trace_event.h" +#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlugin.h" -#import "flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h" #import "flutter/shell/platform/darwin/ios/ios_context.h" @class FlutterTouchInterceptingView; @@ -210,9 +212,9 @@ class FlutterPlatformViewsController { void SetFlutterView(UIView* flutter_view); - void SetFlutterViewController(UIViewController* flutter_view_controller); + void SetFlutterViewController(UIViewController* flutter_view_controller); - UIViewController* getFlutterViewController(); + UIViewController* getFlutterViewController(); void RegisterViewFactory( NSObject* factory, @@ -353,7 +355,7 @@ class FlutterPlatformViewsController { fml::scoped_nsobject channel_; fml::scoped_nsobject flutter_view_; - fml::scoped_nsobject flutter_view_controller_; + fml::scoped_nsobject> flutter_view_controller_; fml::scoped_nsobject mask_view_pool_; std::map>> factories_; std::map>> views_; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h index 83d101dba1..53e3bf330b 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h @@ -5,16 +5,11 @@ #ifndef FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERVIEW_H_ #define FLUTTER_SHELL_PLATFORM_DARWIN_IOS_FRAMEWORK_SOURCE_FLUTTERVIEW_H_ -#include +#import #import -#include - -#include "flutter/fml/memory/weak_ptr.h" -#include "flutter/shell/common/shell.h" +#include "flutter/shell/common/rasterizer.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h" -#import "flutter/shell/platform/darwin/ios/ios_context.h" -#import "flutter/shell/platform/darwin/ios/ios_surface.h" @protocol FlutterViewEngineDelegate diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm index 60dae32662..1576e0cb8e 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterView.mm @@ -3,19 +3,8 @@ // found in the LICENSE file. #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h" -#include -#include "flutter/common/settings.h" -#include "flutter/common/task_runners.h" -#include "flutter/flow/layers/layer_tree.h" #include "flutter/fml/platform/darwin/cf_utils.h" -#include "flutter/fml/synchronization/waitable_event.h" -#include "flutter/fml/trace_event.h" -#include "flutter/shell/common/platform_view.h" -#include "flutter/shell/common/rasterizer.h" -#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" -#import "flutter/shell/platform/darwin/ios/ios_surface_software.h" -#include "third_party/skia/include/utils/mac/SkCGUtils.h" @implementation FlutterView { id _delegate; diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h index ed99a3339b..c1dc2f61cf 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h @@ -60,9 +60,6 @@ typedef void (^FlutterKeyboardAnimationCallback)(fml::TimePoint); - (fml::WeakNSObject)getWeakNSObject; - (std::shared_ptr&)platformViewsController; - (FlutterRestorationPlugin*)restorationPlugin; -// Send touches to the Flutter Engine while forcing the change type to be cancelled. -// The `phase`s in `touches` are ignored. -- (void)forceTouchesCancelled:(NSSet*)touches; // Accepts keypress events, and then calls |nextAction| if the event was not // handled. diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h index 5cb25ca05d..e1f4aafd73 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewResponder.h @@ -8,16 +8,45 @@ #import NS_ASSUME_NONNULL_BEGIN + +/** + * Protocol to send touch events, typically a `FlutterViewController`. + */ @protocol FlutterViewResponder @property(nonatomic, strong) UIView* view; +/** + * See `-[UIResponder touchesBegan:withEvent:]` + */ - (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event; + +/** + * See `-[UIResponder touchesMoved:withEvent:]` + */ - (void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event; + +/** + * See `-[UIResponder touchesEnded:withEvent:]` + */ - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event; + +/** + * See `-[UIResponder touchesCancelled:withEvent:]` + */ - (void)touchesCancelled:(NSSet*)touches withEvent:(UIEvent*)event; + +/** + * See `-[UIResponder touchesEstimatedPropertiesUpdated:]` + */ - (void)touchesEstimatedPropertiesUpdated:(NSSet*)touches; +/** + * Send touches to the Flutter Engine while forcing the change type to be cancelled. + * The `phase`s in `touches` are ignored. + */ +- (void)forceTouchesCancelled:(NSSet*)touches; + @end NS_ASSUME_NONNULL_END diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewTest.mm index e1c1846873..4a514f8e20 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewTest.mm @@ -4,8 +4,6 @@ #import -#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" -#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine_Internal.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterView.h" FLUTTER_ASSERT_ARC diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm index 7d90da63cc..ec82329368 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge_test.mm @@ -5,7 +5,7 @@ #import #import -#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h" +#import "flutter/fml/thread.h" #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h"