From fbab4c0057d638e75bdd8a5d32bfe0e4d6af21b0 Mon Sep 17 00:00:00 2001 From: "Kihara, Takuya" Date: Fri, 10 May 2024 12:40:41 +0900 Subject: [PATCH] [iOS] Fix App crash when use WebView with iOS VoiceOver (flutter/engine#52484) This PR fix the issue [flutter/flutter #140528](https://github.com/flutter/flutter/issues/140528). This fix cleans up declarations regarding `retain` and `release`. `flutterAccessibilityContainer` needs to be managed in `FlutterPlatformViews.mm`, and problems will occur if `retain` or `release` is performed in `SemanticsObject.mm`. This fix is checked by the `testFlutterPlatformViewSemanticsContainer` function in the `SemanticsObjectTest.mm`. The result is not changed. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [ ] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. [Contributor Guide]: https://github.com/flutter/flutter/wiki/Tree-hygiene#overview [Tree Hygiene]: https://github.com/flutter/flutter/wiki/Tree-hygiene [test-exempt]: https://github.com/flutter/flutter/wiki/Tree-hygiene#tests [Flutter Style Guide]: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style [testing the engine]: https://github.com/flutter/flutter/wiki/Testing-the-engine [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/wiki/Tree-hygiene#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/wiki/Chat --- .../framework/Source/FlutterPlatformViews.mm | 13 +++++------- .../Source/FlutterPlatformViews_Internal.h | 2 +- .../ios/framework/Source/SemanticsObject.mm | 5 ++--- .../framework/Source/SemanticsObjectTest.mm | 21 ++++++++++++++----- 4 files changed, 24 insertions(+), 17 deletions(-) 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 56ab003c8d..41295f26a7 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 @@ -981,10 +981,6 @@ void FlutterPlatformViewsController::ResetFrameState() { fml::scoped_nsobject _delayingRecognizer; FlutterPlatformViewGestureRecognizersBlockingPolicy _blockingPolicy; UIView* _embeddedView; - // The used as the accessiblityContainer. - // The `accessiblityContainer` is used in UIKit to determine the parent of this accessibility - // node. - NSObject* _flutterAccessibilityContainer; } - (instancetype)initWithEmbeddedView:(UIView*)embeddedView platformViewsController: @@ -1063,14 +1059,15 @@ void FlutterPlatformViewsController::ResetFrameState() { - (void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event { } -- (void)setFlutterAccessibilityContainer:(NSObject*)flutterAccessibilityContainer { - _flutterAccessibilityContainer = flutterAccessibilityContainer; -} - - (id)accessibilityContainer { return _flutterAccessibilityContainer; } +- (void)dealloc { + [_flutterAccessibilityContainer release]; + [super dealloc]; +} + @end @implementation DelayingGestureRecognizer { 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 b3337a1d69..c2357c3dac 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 @@ -440,7 +440,7 @@ class FlutterPlatformViewsController { - (UIView*)embeddedView; // Sets flutterAccessibilityContainer as this view's accessibilityContainer. -- (void)setFlutterAccessibilityContainer:(NSObject*)flutterAccessibilityContainer; +@property(nonatomic, retain) id flutterAccessibilityContainer; @end @interface UIView (FirstResponder) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm index 7eb0c2384f..cb30bc36a6 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObject.mm @@ -867,7 +867,7 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { @end @interface FlutterPlatformViewSemanticsContainer () -@property(nonatomic, retain) UIView* platformView; +@property(nonatomic, assign) UIView* platformView; @end @implementation FlutterPlatformViewSemanticsContainer @@ -876,14 +876,13 @@ CGRect ConvertRectToGlobal(SemanticsObject* reference, CGRect local_rect) { uid:(int32_t)uid platformView:(nonnull FlutterTouchInterceptingView*)platformView { if (self = [super initWithBridge:bridge uid:uid]) { - _platformView = [platformView retain]; + _platformView = platformView; [platformView setFlutterAccessibilityContainer:self]; } return self; } - (void)dealloc { - [_platformView release]; _platformView = nil; [super dealloc]; } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm index 45285d7cc4..7452c5c097 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/SemanticsObjectTest.mm @@ -994,19 +994,30 @@ FLUTTER_ASSERT_ARC new flutter::testing::MockAccessibilityBridge()); fml::WeakPtr bridge = factory.GetWeakPtr(); __weak FlutterTouchInterceptingView* weakPlatformView; + __weak FlutterPlatformViewSemanticsContainer* weakContainer; @autoreleasepool { FlutterTouchInterceptingView* platformView = [[FlutterTouchInterceptingView alloc] init]; weakPlatformView = platformView; - FlutterPlatformViewSemanticsContainer* container = - [[FlutterPlatformViewSemanticsContainer alloc] initWithBridge:bridge - uid:1 - platformView:platformView]; - XCTAssertEqualObjects(platformView.accessibilityContainer, container); + + @autoreleasepool { + FlutterPlatformViewSemanticsContainer* container = + [[FlutterPlatformViewSemanticsContainer alloc] initWithBridge:bridge + uid:1 + platformView:platformView]; + weakContainer = container; + XCTAssertEqualObjects(platformView.accessibilityContainer, container); + XCTAssertNotNil(weakPlatformView); + XCTAssertNotNil(weakContainer); + } + // Check the variables are still lived. + // `container` is `retain` in `platformView`, so it will not be nil here. XCTAssertNotNil(weakPlatformView); + XCTAssertNotNil(weakContainer); } // Check if there's no more strong references to `platformView` after container and platformView // are released. XCTAssertNil(weakPlatformView); + XCTAssertNil(weakContainer); } - (void)testTextInputSemanticsObject {