Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 140c67c

Browse files
[iOS] sprinkle some null checks on BringLayersIntoView. (#55334)
A user reported hitting a null check error in this code. Add some null checks.
1 parent 95c5a09 commit 140c67c

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

shell/platform/darwin/ios/framework/Source/FlutterPlatformViewsTest.mm

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ @implementation FlutterPlatformViewsTestMockFlutterPlatformFactory
8383

8484
@end
8585

86+
@interface FlutterPlatformViewsTestNilFlutterPlatformFactory : NSObject <FlutterPlatformViewFactory>
87+
@end
88+
89+
@implementation FlutterPlatformViewsTestNilFlutterPlatformFactory
90+
- (NSObject<FlutterPlatformView>*)createWithFrame:(CGRect)frame
91+
viewIdentifier:(int64_t)viewId
92+
arguments:(id _Nullable)args {
93+
return nil;
94+
}
95+
96+
@end
97+
8698
namespace flutter {
8799
namespace {
88100
class FlutterPlatformViewsTestMockPlatformViewDelegate : public PlatformView::Delegate {
@@ -3692,6 +3704,73 @@ - (void)testOnlyPlatformViewsAreRemovedWhenReset {
36923704
XCTAssertEqual(flutterView.subviews.firstObject, someView);
36933705
}
36943706

3707+
- (void)testNilPlatformViewDoesntCrash {
3708+
flutter::FlutterPlatformViewsTestMockPlatformViewDelegate mock_delegate;
3709+
3710+
flutter::TaskRunners runners(/*label=*/self.name.UTF8String,
3711+
/*platform=*/GetDefaultTaskRunner(),
3712+
/*raster=*/GetDefaultTaskRunner(),
3713+
/*ui=*/GetDefaultTaskRunner(),
3714+
/*io=*/GetDefaultTaskRunner());
3715+
auto flutterPlatformViewsController = std::make_shared<flutter::PlatformViewsController>();
3716+
flutterPlatformViewsController->SetTaskRunner(GetDefaultTaskRunner());
3717+
auto platform_view = std::make_unique<flutter::PlatformViewIOS>(
3718+
/*delegate=*/mock_delegate,
3719+
/*rendering_api=*/mock_delegate.settings_.enable_impeller
3720+
? flutter::IOSRenderingAPI::kMetal
3721+
: flutter::IOSRenderingAPI::kSoftware,
3722+
/*platform_views_controller=*/flutterPlatformViewsController,
3723+
/*task_runners=*/runners,
3724+
/*worker_task_runner=*/nil,
3725+
/*is_gpu_disabled_jsync_switch=*/std::make_shared<fml::SyncSwitch>());
3726+
3727+
FlutterPlatformViewsTestNilFlutterPlatformFactory* factory =
3728+
[[FlutterPlatformViewsTestNilFlutterPlatformFactory alloc] init];
3729+
flutterPlatformViewsController->RegisterViewFactory(
3730+
factory, @"MockFlutterPlatformView",
3731+
FlutterPlatformViewGestureRecognizersBlockingPolicyEager);
3732+
FlutterResult result = ^(id result) {
3733+
};
3734+
flutterPlatformViewsController->OnMethodCall(
3735+
[FlutterMethodCall
3736+
methodCallWithMethodName:@"create"
3737+
arguments:@{@"id" : @2, @"viewType" : @"MockFlutterPlatformView"}],
3738+
result);
3739+
UIView* flutterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
3740+
flutterPlatformViewsController->SetFlutterView(flutterView);
3741+
3742+
// Create embedded view params
3743+
flutter::MutatorsStack stack;
3744+
// Layer tree always pushes a screen scale factor to the stack
3745+
SkMatrix screenScaleMatrix =
3746+
SkMatrix::Scale([UIScreen mainScreen].scale, [UIScreen mainScreen].scale);
3747+
stack.PushTransform(screenScaleMatrix);
3748+
// Push a translate matrix
3749+
SkMatrix translateMatrix = SkMatrix::Translate(100, 100);
3750+
stack.PushTransform(translateMatrix);
3751+
SkMatrix finalMatrix;
3752+
finalMatrix.setConcat(screenScaleMatrix, translateMatrix);
3753+
3754+
auto embeddedViewParams =
3755+
std::make_unique<flutter::EmbeddedViewParams>(finalMatrix, SkSize::Make(300, 300), stack);
3756+
3757+
flutterPlatformViewsController->PrerollCompositeEmbeddedView(2, std::move(embeddedViewParams));
3758+
3759+
// SKSurface is required if the root FlutterView is present.
3760+
const SkImageInfo image_info = SkImageInfo::MakeN32Premul(1000, 1000);
3761+
sk_sp<SkSurface> mock_sk_surface = SkSurfaces::Raster(image_info);
3762+
flutter::SurfaceFrame::FramebufferInfo framebuffer_info;
3763+
auto mock_surface = std::make_unique<flutter::SurfaceFrame>(
3764+
std::move(mock_sk_surface), framebuffer_info,
3765+
[](const flutter::SurfaceFrame& surface_frame, flutter::DlCanvas* canvas) { return true; },
3766+
[](const flutter::SurfaceFrame& surface_frame) { return true; },
3767+
/*frame_size=*/SkISize::Make(800, 600));
3768+
3769+
flutterPlatformViewsController->SubmitFrame(nullptr, nullptr, std::move(mock_surface));
3770+
3771+
XCTAssertEqual(flutterView.subviews.count, 1u);
3772+
}
3773+
36953774
- (void)testFlutterTouchInterceptingViewLinksToAccessibilityContainer {
36963775
FlutterTouchInterceptingView* touchInteceptorView = [[FlutterTouchInterceptingView alloc] init];
36973776
NSObject* container = [[NSObject alloc] init];

shell/platform/darwin/ios/framework/Source/platform_views_controller.mm

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -797,13 +797,18 @@ bool ClipRRectContainsPlatformViewBoundingRect(const SkRRect& clip_rrect,
797797
NSMutableArray* desired_platform_subviews = [NSMutableArray array];
798798
for (int64_t platform_view_id : composition_order) {
799799
UIView* platform_view_root = platform_views_[platform_view_id].root_view.get();
800-
[desired_platform_subviews addObject:platform_view_root];
800+
if (platform_view_root != nil) {
801+
[desired_platform_subviews addObject:platform_view_root];
802+
}
801803

802804
auto maybe_layer_data = layer_map.find(platform_view_id);
803805
if (maybe_layer_data != layer_map.end()) {
804-
[desired_platform_subviews addObject:maybe_layer_data->second.layer->overlay_view_wrapper];
806+
auto view = maybe_layer_data->second.layer->overlay_view_wrapper;
807+
if (view != nil) {
808+
[desired_platform_subviews addObject:view];
809+
previous_composition_order_.push_back(platform_view_id);
810+
}
805811
}
806-
previous_composition_order_.push_back(platform_view_id);
807812
}
808813

809814
NSSet* desired_platform_subviews_set = [NSSet setWithArray:desired_platform_subviews];

0 commit comments

Comments
 (0)