Guard accessibility_bridge UIView access (flutter/engine#36580)

This commit is contained in:
Jenn Magder
2022-10-04 15:50:02 -07:00
committed by GitHub
parent f69e7d0e99
commit ef8d23749a
3 changed files with 56 additions and 7 deletions

View File

@@ -505,7 +505,7 @@ static void SendFakeTouchEvent(FlutterEngine* engine,
- (void)setDisplayingFlutterUI:(BOOL)displayingFlutterUI {
if (_displayingFlutterUI != displayingFlutterUI) {
if (displayingFlutterUI == YES) {
if (!self.isViewLoaded || !self.view.window) {
if (!self.viewIfLoaded.window) {
return;
}
}
@@ -901,9 +901,9 @@ static void SendFakeTouchEvent(FlutterEngine* engine,
// Make this transition only while this current view controller is visible.
- (void)goToApplicationLifecycle:(nonnull NSString*)state {
// Accessing self.view will create the view. Check whether the view is organically loaded
// first before checking whether the view is attached to window.
if (self.isViewLoaded && self.view.window) {
// Accessing self.view will create the view. Instead use viewIfLoaded
// to check whether the view is attached to window.
if (self.viewIfLoaded.window) {
[[_engine.get() lifecycleChannel] sendMessage:state];
}
}

View File

@@ -63,7 +63,7 @@ AccessibilityBridge::AccessibilityBridge(
AccessibilityBridge::~AccessibilityBridge() {
[accessibility_channel_.get() setMessageHandler:nil];
clearState();
view_controller_.view.accessibilityElements = nil;
view_controller_.viewIfLoaded.accessibilityElements = nil;
}
UIView<UITextInput>* AccessibilityBridge::textInputView() {
@@ -183,7 +183,7 @@ void AccessibilityBridge::UpdateSemantics(flutter::SemanticsNodeUpdates nodes,
previous_routes_.push_back([route uid]);
}
} else {
view_controller_.view.accessibilityElements = nil;
view_controller_.viewIfLoaded.accessibilityElements = nil;
}
NSMutableArray<NSNumber*>* doomed_uids = [NSMutableArray arrayWithArray:[objects_ allKeys]];

View File

@@ -10,6 +10,7 @@
#import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterPlatformViews.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterPlatformViews_Internal.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterSemanticsScrollView.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h"
#import "flutter/shell/platform/darwin/ios/framework/Source/accessibility_bridge.h"
#import "flutter/shell/platform/darwin/ios/platform_view_ios.h"
@@ -171,7 +172,7 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(std::string name) {
/*task_runners=*/runners);
id mockFlutterView = OCMClassMock([FlutterView class]);
id mockFlutterViewController = OCMClassMock([FlutterViewController class]);
OCMStub([mockFlutterViewController view]).andReturn(mockFlutterView);
OCMStub([mockFlutterViewController viewIfLoaded]).andReturn(mockFlutterView);
OCMExpect([mockFlutterView setAccessibilityElements:[OCMArg isNil]]);
auto bridge =
std::make_unique<flutter::AccessibilityBridge>(/*view_controller=*/mockFlutterViewController,
@@ -308,6 +309,54 @@ fml::RefPtr<fml::TaskRunner> CreateNewThread(std::string name) {
XCTAssertNil(gMockPlatformView);
}
- (void)testSemanticsDeallocatedWithoutLoadingView {
id engine = OCMClassMock([FlutterEngine class]);
FlutterViewController* flutterViewController =
[[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil];
@autoreleasepool {
flutter::MockDelegate mock_delegate;
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
/*platform=*/thread_task_runner,
/*raster=*/thread_task_runner,
/*ui=*/thread_task_runner,
/*io=*/thread_task_runner);
auto flutterPlatformViewsController =
std::make_shared<flutter::FlutterPlatformViewsController>();
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
/*delegate=*/mock_delegate,
/*rendering_api=*/flutter::IOSRenderingAPI::kSoftware,
/*platform_views_controller=*/flutterPlatformViewsController,
/*task_runners=*/runners);
MockFlutterPlatformFactory* factory = [[MockFlutterPlatformFactory new] autorelease];
flutterPlatformViewsController->RegisterViewFactory(
factory, @"MockFlutterPlatformView",
FlutterPlatformViewGestureRecognizersBlockingPolicyEager);
FlutterResult result = ^(id result) {
};
flutterPlatformViewsController->OnMethodCall(
[FlutterMethodCall
methodCallWithMethodName:@"create"
arguments:@{@"id" : @2, @"viewType" : @"MockFlutterPlatformView"}],
result);
auto bridge = std::make_unique<flutter::AccessibilityBridge>(
/*view_controller=*/flutterViewController,
/*platform_view=*/platform_view.get(),
/*platform_views_controller=*/flutterPlatformViewsController);
XCTAssertNotNil(gMockPlatformView);
flutterPlatformViewsController->Reset();
platform_view->NotifyDestroyed();
}
XCTAssertNil(gMockPlatformView);
XCTAssertNil(flutterViewController.viewIfLoaded);
[flutterViewController deregisterNotifications];
[flutterViewController release];
}
- (void)testReplacedSemanticsDoesNotCleanupChildren {
flutter::MockDelegate mock_delegate;
auto thread_task_runner = CreateNewThread("AccessibilityBridgeTest");